Instcombine: Fix pattern where the sext did not dominate the icmp using it
authorTobias Grosser <grosser@fim.uni-passau.de>
Sun, 9 Jan 2011 16:00:11 +0000 (16:00 +0000)
committerTobias Grosser <grosser@fim.uni-passau.de>
Sun, 9 Jan 2011 16:00:11 +0000 (16:00 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123121 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineSelect.cpp
test/Transforms/InstCombine/select.ll

index 71a286ef69c47de6fbb230065d12b2c7998be8c0..ff41ab834b121f87e0a59c5ec3858cf193c6ccf8 100644 (file)
@@ -281,8 +281,8 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
   Value *FalseVal = SI.getFalseValue();
 
   // Check cases where the comparison is with a constant that
-  // can be adjusted to fit the min/max idiom. We may edit ICI in
-  // place here, so make sure the select is the only user.
+  // can be adjusted to fit the min/max idiom. We may move or edit ICI
+  // here, so make sure the select is the only user.
   if (ICI->hasOneUse())
     if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) {
       // X < MIN ? T : F  -->  F
@@ -364,6 +364,11 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
         ICI->setOperand(1, CmpRHS);
         SI.setOperand(1, TrueVal);
         SI.setOperand(2, FalseVal);
+
+        // Move ICI instruction right before the select instruction. Otherwise
+        // the sext/zext value may be defined after the ICI instruction uses it.
+        ICI->moveBefore(&SI);
+
         Changed = true;
         break;
       }
index c9b880d92f91f65983491a13138ca97e7b575d1f..6633564c2b6b76bb7f8019cdea72ff2de5949b8a 100644 (file)
@@ -678,3 +678,14 @@ define i64 @test49(i32 %a) nounwind {
 ; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
 ; CHECK-NEXT: ret i64 %min
 }
+define i64 @test50(i32 %a) nounwind {
+       %is_a_nonpositive = icmp ult i32 %a, 3
+       %a_ext = sext i32 %a to i64
+       %min = select i1 %is_a_nonpositive, i64 2, i64 %a_ext
+       ret i64 %min
+; CHECK: @test50
+; CHECK-NEXT: %a_ext = sext i32 %a to i64
+; CHECK-NEXT: %is_a_nonpositive = icmp ugt i64 %a_ext, 2
+; CHECK-NEXT: %min = select i1 %is_a_nonpositive, i64 %a_ext, i64 2
+; CHECK-NEXT: ret i64 %min
+}