Exploiting 'unreachable' for optimization purposes

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

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.

Hi Stephan,

I think this is just a shortcoming. Can you file a bug and assign it to me? I have another limitation related to unreachable that I need to take a look at, and when I do so I will take a look at this as well.

Thanks,

Mark

Hi Mark,

I just dug out a bug from 2006 which since 2009 seems to also have covered efforts to make better use of unreachable for optimization purposes: http://llvm.org/bugs/show_bug.cgi?id=810

I've added a comment and assigned it to you as requested.

Thanks,
   Stephan

Hi Mark,

I just dug out a bug from 2006 which since 2009 seems to also have covered efforts to make better use of unreachable for optimization purposes: http://llvm.org/bugs/show_bug.cgi?id=810

I've added a comment and assigned it to you as requested.

Great, thanks Stephan!

Mark