[cfe-dev] Help with APValue comparisons

Jordan Rose jordan_rose at apple.com
Thu Jun 14 10:12:23 PDT 2012


For LValues that do not come from the same base aggregate, they are known to be != and relational comparisons (< <= >= >) are undefined. This is true even if one of the APValues is a null pointer -- the /representation/ of a null pointer is not guaranteed to be zero, and pointers are not guaranteed to be treated as unsigned.

Be careful about constant folding; this works under C++. (Not sure how to put constexpr into it, though.)

const char * const x = "abc";
const char * const y = "abc";
const bool same = (x == y); // true

For LValues that /do/ come from the same base aggregate, it looks like you can walk the "LValue path" and compare field/element locations along the way. These of course have a well-defined ordering. IIRC base objects always come before fields.

For this, be careful about unions:

const union { int i; unsigned u; } foo;
int * const bar = &foo.i;
unsigned * const baz = &foo.u;
const bool same = (bar == baz); // true 

Hope that helps. Testing against runtime behavior isn't a bad idea whenever you're not sure, of course.

Jordan


On Jun 14, 2012, at 5:06 AM, Andy Gibbs wrote:

> Hi,
> 
> I'm in the middle of cooking a patch which dramatically speeds up 
> compilation of constexpr functions.  I have a partially working patch 
> already which "fixes" the bug at http://llvm.org/bugs/show_bug.cgi?id=12850. 
> I put "fixes" in quotes because at present the fix only works for the 
> "simple" cases as present, by which I mean where arguments evaluate to 
> APValues of type interger / float / complex / array / vector / struct.
> 
> The question I have is this: the above cases have, lets say, fairly obvious 
> ways for comparing whether two APValues are the same, however, I'm not sure 
> about LValue -- does anyone know of a suitable / correct way of comparing 
> two LValue-type APValues?
> 
> Many thanks!
> 
> Andy
> 
> 
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list