Add the select optimization recently added to instcombine to constant folding.
authorNick Lewycky <nicholas@mxc.ca>
Sat, 29 Jan 2011 20:35:06 +0000 (20:35 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Sat, 29 Jan 2011 20:35:06 +0000 (20:35 +0000)
This is the one where one of the branches of the select is another select on
the same condition.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124547 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/ConstantFold.cpp
test/Transforms/ConstProp/constant-expr.ll

index 3fea1910ffe153b1c8325f4a255d51e34384af61..c3be1197d3c3a5791a62555ed3c9f2a12de59cca 100644 (file)
@@ -693,6 +693,18 @@ Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
   if (isa<UndefValue>(V2)) return V1;
   if (isa<UndefValue>(Cond)) return V1;
   if (V1 == V2) return V1;
+
+  if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) {
+    if (TrueVal->getOpcode() == Instruction::Select)
+      if (TrueVal->getOperand(0) == Cond)
+       return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2);
+  }
+  if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) {
+    if (FalseVal->getOpcode() == Instruction::Select)
+      if (FalseVal->getOperand(0) == Cond)
+       return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2));
+  }
+
   return 0;
 }
 
index 33ed1a53c3e0110a145a0f9fc4a92a443dae7e19..1088fa6959ad909ce95f5502e8ed388e264ad8b4 100644 (file)
 ; CHECK: pr9011_14 = constant i128 0
 @pr9011_15 = constant i128 bitcast (<4 x i32> zeroinitializer to i128)
 ; CHECK: pr9011_15 = constant i128 0
+
+@select = internal constant
+          i32 select (i1 icmp ult (i32 ptrtoint (i8* @X to i32),
+                                   i32 ptrtoint (i8* @Y to i32)),
+            i32 select (i1 icmp ult (i32 ptrtoint (i8* @X to i32),
+                                     i32 ptrtoint (i8* @Y to i32)),
+               i32 10, i32 20),
+            i32 30)
+; CHECK: select = internal constant i32 select {{.*}} i32 10, i32 30