Fold select + select where both selects are on the same condition.
authorNick Lewycky <nicholas@mxc.ca>
Fri, 28 Jan 2011 03:28:10 +0000 (03:28 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Fri, 28 Jan 2011 03:28:10 +0000 (03:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124469 91177308-0d34-0410-b5e6-96231b3b80d8

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

index ff41ab834b121f87e0a59c5ec3858cf193c6ccf8..97abc769ae5ff81de4361d4ab42da38ea60b5201 100644 (file)
@@ -792,6 +792,19 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
       if (Instruction *NV = FoldOpIntoPhi(SI))
         return NV;
 
+  if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
+    if (TrueSI->getCondition() == CondVal) {
+      SI.setOperand(1, TrueSI->getTrueValue());
+      return &SI;
+    }
+  }
+  if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
+    if (FalseSI->getCondition() == CondVal) {
+      SI.setOperand(2, FalseSI->getFalseValue());
+      return &SI;
+    }
+  }
+
   if (BinaryOperator::isNot(CondVal)) {
     SI.setOperand(0, BinaryOperator::getNotArgument(CondVal));
     SI.setOperand(1, FalseVal);
index 54cfc467ccdd520cca5013df51c7c8a4c11b47a7..b66b22cff6ee29339a760bcb087ea81b19ee4ceb 100644 (file)
@@ -692,7 +692,7 @@ define i64 @test50(i32 %a) nounwind {
 
 ; PR8994
 
-; Theis select instruction can't be eliminated because trying to do so would
+; This select instruction can't be eliminated because trying to do so would
 ; change the number of vector elements. This used to assert.
 define i48 @test51(<3 x i1> %icmp, <3 x i16> %tmp) {
   %select = select <3 x i1> %icmp, <3 x i16> zeroinitializer, <3 x i16> %tmp
@@ -700,3 +700,18 @@ define i48 @test51(<3 x i1> %icmp, <3 x i16> %tmp) {
   %tmp2 = bitcast <3 x i16> %select to i48
   ret i48 %tmp2
 }
+
+; PR8575
+
+@g = common global i32 0, align 4
+define void @test52(i32 %n, i32 %m) nounwind {
+entry:
+  %cmp = icmp sgt i32 %n, %m
+  %. = select i1 %cmp, i32 1, i32 3
+  %add = add nsw i32 %., 3
+  %storemerge = select i1 %cmp, i32 %., i32 %add
+; CHECK: select i1 %cmp, i32 1, i32 6
+  store i32 %storemerge, i32* @g, align 4
+  ret void
+}
+