[LLVMdev] Exploiting 'unreachable' for optimization purposes

Stephan Tolksdorf st at quanttec.com
Thu Oct 24 05:22:27 PDT 2013


Hi,

When clang/llvm compiles the following sample (with -O2) it optimizes 
away the second comparison in test1 but not in test2. Is this handling 
of 'unreachable' by purpose, or is this just a shortcoming of the 
current optimization passes? GCC and MSVC (with the equivalent code 
using the __assume intrinsic) both optimize away the comparison in test2.


void f1();

void f2();

void abort() __attribute__((noreturn));

void test1(int x) {
   if (x < 0) abort();
   // the following comparison is optimized away
   if (x < 0) f1(); else f2();
}

void test2(int x) {
   if (x < 0) __builtin_unreachable();
   // the following comparison is NOT optimized away
   if (x < 0) f1(); else f2();
}

- Stephan



More information about the llvm-dev mailing list