[LLVMdev] Exploiting 'unreachable' for optimization purposes

Mark Lacey mark.lacey at apple.com
Thu Oct 24 10:05:04 PDT 2013


On Oct 24, 2013, at 5:22 AM, Stephan Tolksdorf <st at quanttec.com> wrote:

> 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

> 
> 
> 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
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list