inline void assume(_Bool b) { if (!b) __builtin_unreachable(); } _Bool f(int n) { assume(n >= 1); typedef int A[n]; ++n; A a; int b[n]; n -= 2; typedef int C[n]; C c; return (sizeof(a) < sizeof(b)) && (sizeof(a) > sizeof(c)); } This C code (this will have different results in C++ with GCC) should always `return true`. LLVM makes this transformation, but GCC does not. Also, extra question, is the fact that this always returns `false` when compiled as C++ normal ? Clang has it return `true` if compiled as C++.
gcc computes sizeof(a) as 4ul*(size_t)n, and unsigned types don't provide nice overflow guarantees, so that complicates things.
(In reply to Marc Glisse from comment #1) > gcc computes sizeof(a) as 4ul*(size_t)n, and unsigned types don't provide > nice overflow guarantees, so that complicates things. While the C++ front-end does: (sizetype) ((ssizetype) n + -1) + 1) * 4)
Since VLA is an extension for compatibility with C, it is strange that it behaves differently (does one use the value of n at the time of the typedef and the other at the time of the declaration?). This bug is about the optimization, maybe a separate report about the C++ behavior would make sense.
# RANGE [irange] int [1, +INF] NONZERO 0x7fffffff intD.7 n_11(D) = nD.4385; sizetype _2; _BoolD.4372 _16; ;; basic block 2, loop depth 0, count 1073741824 (estimated locally), maybe hot ;; prev block 0, next block 1, flags: (NEW, REACHABLE, VISITED) ;; pred: ENTRY [always] count:1073741824 (estimated locally) (FALLTHRU,EXECUTABLE) # RANGE [irange] sizetype [1, 2147483647] NONZERO 0x7fffffff _1 = (sizetype) n_11(D); # RANGE [irange] sizetype [4, 8589934588] NONZERO 0x1fffffffc _2 = _1 * 4; _16 = _2 > 3; We have the information to do the folding in optimize but not earlier ...
Also, as an extra note, w.r.t. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94911#c3, I've just noticed that I had indeed made a separate bug report at https://gcc.gnu.org/PR94912 (which ended up being closed as a duplicate of https://gcc.gnu.org/PR68531) - just wanted to clarify that so nobody ends up filing more duplicates like I almost just did