[llvm] 271b5cf - [InstCombine] Fix infinite combine loop (PR61361)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 14 08:43:11 PDT 2023


Author: Nikita Popov
Date: 2023-03-14T16:43:00+01:00
New Revision: 271b5cf562fc6947598377d7c7ba76d9485e71e4

URL: https://github.com/llvm/llvm-project/commit/271b5cf562fc6947598377d7c7ba76d9485e71e4
DIFF: https://github.com/llvm/llvm-project/commit/271b5cf562fc6947598377d7c7ba76d9485e71e4.diff

LOG: [InstCombine] Fix infinite combine loop (PR61361)

In the degenerate case where the select is fed by an unsimplified
icmp with two constant operands, don't try to replace one constant
with another. Wait for the icmp to be simplified first instead.

Fixes https://github.com/llvm/llvm-project/issues/61361.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/test/Transforms/InstCombine/select.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index d1bac8a089388..1f2441bc9fcf9 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -1728,7 +1728,7 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
   ICmpInst::Predicate Pred = ICI->getPredicate();
   Value *CmpLHS = ICI->getOperand(0);
   Value *CmpRHS = ICI->getOperand(1);
-  if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) {
+  if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS) && !isa<Constant>(CmpLHS)) {
     if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
       // Transform (X == C) ? X : Y -> (X == C) ? C : Y
       SI.setOperand(1, CmpRHS);

diff  --git a/llvm/test/Transforms/InstCombine/select.ll b/llvm/test/Transforms/InstCombine/select.ll
index 20e9a3aa0312a..ccb62b027c655 100644
--- a/llvm/test/Transforms/InstCombine/select.ll
+++ b/llvm/test/Transforms/InstCombine/select.ll
@@ -3566,3 +3566,18 @@ define i8 @not_clamp_smax2(i8 %x) {
   %sel = select i1 %cmp, i8 125, i8 %x
   ret i8 %sel
 }
+
+; Used to infinite loop.
+define i32 @pr61361(i32 %arg) {
+; CHECK-LABEL: @pr61361(
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp eq i32 [[ARG:%.*]], 0
+; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[CMP2]], i32 16777215, i32 0
+; CHECK-NEXT:    ret i32 [[SEL2]]
+;
+  %cmp1 = icmp eq i32 %arg, 1
+  %sel1 = select i1 %cmp1, i32 0, i32 33554431
+  %cmp2 = icmp eq i32 %arg, 0
+  %sel2 = select i1 %cmp2, i32 %sel1, i32 0
+  %ashr = ashr i32 %sel2, 1
+  ret i32 %ashr
+}


        


More information about the llvm-commits mailing list