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]
Other format: [Raw text]

Re: -funswitch-loops not effective?


On Mon, 17 Jan 2005 18:43:49 +0100, Giovanni Bajo <rasky@develer.com> wrote:
> Andrew Pinski <pinskia@physics.uc.edu> wrote:
> 
> >> What I don't quite understand is why in the following testcase,
> >> unswitching
> >> is happening:
> >>
> >> void foo(int);
> >> void bar(int);
> >>
> >> extern const _Bool flag;
> >>
> >> void foobar(int n)
> >> {
> >>         int i;
> >>         for (i=0; i<n; ++i)
> >>                 if (flag)
> >>                         foo(i);
> >>                 else
> >>                         bar(i);
> >> }
> >
> >
> > Because even though flag is const in this TU, but does
> > not have to be in a different TU
> 
> Is this allowed in C++ too?

At least it "works" in C++, too.  Though with C++ I may have

extern const bool flag;
void foo(int)
{
   const bool *cb = &flag;
   bool *b = const_cast<bool*>(cb);
   *b = !*b;
}

? Or is this ill-formed? In the case this is well formed, we
miscompile the loop in foobar.  I guess it's ill-formed, as
I could circumvent const in C by a cast, too, and Joseph
says in C it is ok to do the optimization.

Of course, this doesn't say where there is the well-formed
leak of a pointer to the const member in the class in the first
place:

class Foo {
public:
   Foo(bool b) : flag(b) {}
   bool get() const { return flag; }
private:
   const bool flag;
};

Is there a well-formed way to write to Foo::flag?  Couldn't we
detect that there isn't one?

Thanks again,
Richard.


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