This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Optimization of the switch statement


Hi!

I'm using the gcc 2.8.1 on Linux (Pentium).
It doesn't optimize the switch statement with a constant parameter
in an inline function by replacing it with the corresponding case.

In the sample below I would expect the optimization of the call of
Test::func in main, like
t.func() --> {return t.func2();} instead of {switch (m_) {...}}

I know, the IBM VisualAge C++ compiler does make such optimization.
Does the egsc optimize the switch statement in the way I described?


//test.h

class Test {
  public:
    enum mode {alpha, beta};

    Test(mode m);
    inline int func();
  protected:
    int func1();
    int func2();
    int func3();
  private:
    const mode m_; // a constant value
};


inline int
Test :: func()
{
  switch (m_) { // m_ is a constant, there's only one possibility
     case alpha:
        return func1();
     case beta:
        return func2();
     default:
        return func3();
  }
}

//test.cc
Test :: Test(mode m) : m_(m){}
int Test :: func1() {return 1;}
int Test :: func2() {return 2;}
int Test :: func3() {return 3;}

//main.cc
# include "test.h"

int main()
{
   Test t(Test::beta);
   t.func(); // After compiling with -O2 the switch is still here!
   return 0;
}

-- 
Ryszard Kabatek


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]