[PATCH] D19188: [SimplifyCFG] Fold `llvm.guard(false)` to unreachable

Sanjoy Das via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 15 18:51:14 PDT 2016


sanjoy created this revision.
sanjoy added reviewers: hfinkel, pcc.
sanjoy added a subscriber: llvm-commits.
Herald added a subscriber: mcrosier.

http://reviews.llvm.org/D19188

Files:
  lib/Transforms/Utils/Local.cpp
  test/Transforms/SimplifyCFG/guards.ll

Index: test/Transforms/SimplifyCFG/guards.ll
===================================================================
--- /dev/null
+++ test/Transforms/SimplifyCFG/guards.ll
@@ -0,0 +1,36 @@
+; RUN: opt -S -simplifycfg < %s | FileCheck %s
+
+declare void @llvm.experimental.guard(i1, ...)
+
+define i32 @f_0(i1 %c) {
+; CHECK-LABEL: @f_0(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: ret i32 20
+entry:
+  br i1 %c, label %true, label %false
+
+true:
+  call void(i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
+  ret i32 10
+
+false:
+  ret i32 20
+}
+
+define i32 @f_1(i1 %c) {
+; Demonstrate that we do not yet simplify a guard on undef
+
+; CHECK-LABEL: @f_1(
+; CHECK: ret i32 10
+; CHECK: ret i32 20
+
+entry:
+  br i1 %c, label %true, label %false
+
+true:
+  call void(i1, ...) @llvm.experimental.guard(i1 undef) [ "deopt"() ]
+  ret i32 10
+
+false:
+  ret i32 20
+}
Index: lib/Transforms/Utils/Local.cpp
===================================================================
--- lib/Transforms/Utils/Local.cpp
+++ lib/Transforms/Utils/Local.cpp
@@ -1310,7 +1310,7 @@
       // Assumptions that are known to be false are equivalent to unreachable.
       // Also, if the condition is undefined, then we make the choice most
       // beneficial to the optimizer, and choose that to also be unreachable.
-      if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BBI))
+      if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BBI)) {
         if (II->getIntrinsicID() == Intrinsic::assume) {
           bool MakeUnreachable = false;
           if (isa<UndefValue>(II->getArgOperand(0)))
@@ -1327,6 +1327,18 @@
           }
         }
 
+        if (II->getIntrinsicID() == Intrinsic::experimental_guard)
+          // Unlike in llvm.assume, it is not "obviously profitable" for guards
+          // to treat `undef` as `false` since a guard on `undef` can still be
+          // useful for widening.
+          if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
+            if (CI->isZero()) {
+              changeToUnreachable(II, /* UseLLVMTrap = */ false);
+              Changed = true;
+              break;
+            }
+      }
+
       if (CallInst *CI = dyn_cast<CallInst>(BBI)) {
         if (CI->doesNotReturn()) {
           // If we found a call to a no-return function, insert an unreachable


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19188.53977.patch
Type: text/x-patch
Size: 2316 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160416/3babcc3a/attachment.bin>


More information about the llvm-commits mailing list