ConstantFoldInstOperands doesn't like compares, hand it off to instsimplify instead.
authorBenjamin Kramer <benny.kra@googlemail.com>
Sat, 28 May 2011 10:16:58 +0000 (10:16 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sat, 28 May 2011 10:16:58 +0000 (10:16 +0000)
Fixes PR10040.

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

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

index 2a451ce5f12ace995269c29ffcf04a43b443fae8..aeb3c3e880fa7805e2ab869822442b727c123598 100644 (file)
@@ -298,6 +298,16 @@ static Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
       return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, TD);
   }
 
+  // Same for CmpInsts.
+  if (CmpInst *C = dyn_cast<CmpInst>(I)) {
+    if (C->getOperand(0) == Op)
+      return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), TD);
+    if (C->getOperand(1) == Op)
+      return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, TD);
+  }
+
+  // TODO: We could hand off more cases to instsimplify here.
+
   // If all operands are constant after substituting Op for RepOp then we can
   // constant fold the instruction.
   if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) {
index 379228512cd1b9f41266563782a5284dd412d5d6..4ca9bd2c07c08a9bab3315c76623e51a5fd34bba 100644 (file)
@@ -789,3 +789,13 @@ define i32 @test59(i32 %x, i32 %y) nounwind {
 ; CHECK-NEXT: and i32 %x, %y
 ; CHECK-NEXT: ret
 }
+
+define i1 @test60(i32 %x, i1* %y) nounwind {
+  %cmp = icmp eq i32 %x, 0
+  %load = load i1* %y, align 1
+  %cmp1 = icmp slt i32 %x, 1
+  %sel = select i1 %cmp, i1 %load, i1 %cmp1
+  ret i1 %sel
+; CHECK: @test60
+; CHECK: select
+}