Stephan Tolksdorf
2013-Oct-24 12:22 UTC
[LLVMdev] 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
Mark Lacey
2013-Oct-24 17:05 UTC
[LLVMdev] Exploiting 'unreachable' for optimization purposes
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
Stephan Tolksdorf
2013-Oct-24 18:05 UTC
[LLVMdev] Exploiting 'unreachable' for optimization purposes
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 On 24.10.13 19:05, Mark Lacey wrote:> > 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 >