[LLVMdev] Invalid code generated for on-stack class compares

Sean Buckheister s_buckhe at cs.uni-kl.de
Thu Mar 29 21:20:41 PDT 2012


clang++ generates invalid code for on-stack struct compares with
overloaded operators in release 3.0.

Take the following code:

struct A {
  bool operator<(const A& other)
  { return ((long long int)this) < ((long long int)&other); }
};

int main() {
  A a, b;
  return (a < b) + (b < a);
}

Usually, this program should return 1. Compiling it with "clang++ -Ox"
with x > 1, it returns 2. The online demo shows this nicely, too:

define i32 @main() nounwind uwtable readnone {
  ret i32 2
}

This is apparently fixed in SVN, but it does kind of hurt. As far as I
can tell, this hits everything that is a value on the current stack
frame. Things behind pointers or references and locations off the stack
are safe. Adding an empty destructor to A fixes this problem for
-O[123], but -O4 breaks again.

This puzzles me, and I wonder: what else does this affect? Is some bug
responsible that might as well break completely unrelated pieces of code?



More information about the llvm-dev mailing list