[llvm] 9d0bf76 - [InstCombine] don't try to fold a constant expression that can trap (PR50906)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 28 14:00:30 PDT 2021


Author: Sanjay Patel
Date: 2021-06-28T17:00:21-04:00
New Revision: 9d0bf7699c0292041b65a0c0bac371003e067ef3

URL: https://github.com/llvm/llvm-project/commit/9d0bf7699c0292041b65a0c0bac371003e067ef3
DIFF: https://github.com/llvm/llvm-project/commit/9d0bf7699c0292041b65a0c0bac371003e067ef3.diff

LOG: [InstCombine] don't try to fold a constant expression that can trap (PR50906)

We could use a bigger hammer and bail out on any constant
expression, but there's a regression test that appears to
validly do the transform (although it may not have been
intending to check that optimization).

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/test/Transforms/InstCombine/indexed-gep-compares.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index e141d614851ba..e5a1b8eaf24f1 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1440,7 +1440,7 @@ Instruction *InstCombinerImpl::foldICmpWithConstant(ICmpInst &Cmp) {
 
   // icmp(phi(C1, C2, ...), C) -> phi(icmp(C1, C), icmp(C2, C), ...).
   Constant *C = dyn_cast<Constant>(Op1);
-  if (!C)
+  if (!C || C->canTrap())
     return nullptr;
 
   if (auto *Phi = dyn_cast<PHINode>(Op0))

diff  --git a/llvm/test/Transforms/InstCombine/indexed-gep-compares.ll b/llvm/test/Transforms/InstCombine/indexed-gep-compares.ll
index 24410a125d261..d2a6c438527cd 100644
--- a/llvm/test/Transforms/InstCombine/indexed-gep-compares.ll
+++ b/llvm/test/Transforms/InstCombine/indexed-gep-compares.ll
@@ -249,7 +249,6 @@ bb10:
   ret i1 %cmp
 }
 
-; FIXME:
 ; It is not generally safe to hoist an expression (sdiv) that may trap.
 
 define i1 @PR50906() {
@@ -257,9 +256,10 @@ define i1 @PR50906() {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       loop:
-; CHECK-NEXT:    [[CMP:%.*]] = phi i1 [ icmp sgt (i32 sdiv (i32 7, i32 ptrtoint (i1 ()* @PR50906 to i32)), i32 1), [[NEXT:%.*]] ], [ icmp sgt (i32 sdiv (i32 7, i32 ptrtoint (i1 ()* @PR50906 to i32)), i32 0), [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 1, [[NEXT:%.*]] ]
 ; CHECK-NEXT:    br label [[NEXT]]
 ; CHECK:       next:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[PHI]], sdiv (i32 7, i32 ptrtoint (i1 ()* @PR50906 to i32))
 ; CHECK-NEXT:    br i1 [[CMP]], label [[EXIT:%.*]], label [[LOOP]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    ret i1 [[CMP]]


        


More information about the llvm-commits mailing list