[PATCH] D47576: [InstCombine] Fix div handling

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 31 01:44:32 PDT 2018


skatkov created this revision.
skatkov added reviewers: spatel, craig.topper.

When we optimize select basing on fact that div by 0 is undef
we should not traverse the instruction which are not guaranteed to
transfer execution to next instruction. Guard intrinsic is an example.


https://reviews.llvm.org/D47576

Files:
  lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
  test/Transforms/InstCombine/sdiv-guard.ll


Index: test/Transforms/InstCombine/sdiv-guard.ll
===================================================================
--- /dev/null
+++ test/Transforms/InstCombine/sdiv-guard.ll
@@ -0,0 +1,15 @@
+; RUN: opt < %s -instcombine -inline -S | FileCheck %s
+
+declare void @llvm.experimental.guard(i1, ...)
+
+; Regression test. If %flag is false then %s == 0 and guard should be triggered.
+define i32 @a(i1 %flag, i32 %X) nounwind readnone {
+entry:
+;CHECK: call
+;CHECK: sdiv i32 100, %X
+  %s = select i1 %flag, i32 %X, i32 0
+  %cmp = icmp ne i32 %s, 0
+  call void(i1, ...) @llvm.experimental.guard( i1 %cmp )[ "deopt"() ]
+  %r = sdiv i32 100, %s
+  ret i32 %r
+}
Index: lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -583,9 +583,9 @@
   Type *CondTy = SelectCond->getType();
   while (BBI != BBFront) {
     --BBI;
-    // If we found a call to a function, we can't assume it will return, so
+    // If we found an instruction that we can't assume it will return, so
     // information from below it cannot be propagated above it.
-    if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
+    if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI))
       break;
 
     // Replace uses of the select or its condition with the known values.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47576.149247.patch
Type: text/x-patch
Size: 1431 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180531/a5478f5d/attachment.bin>


More information about the llvm-commits mailing list