break;
}
}
-
-
+
+ // If the comparison is with the result of a select instruction, check whether
+ // comparing with either branch of the select always yields the same value.
+ if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) {
+ // Make sure the select is on the LHS.
+ if (!isa<SelectInst>(LHS)) {
+ std::swap(LHS, RHS);
+ Pred = CmpInst::getSwappedPredicate(Pred);
+ }
+ SelectInst *SI = cast<SelectInst>(LHS);
+ // Now that we have "icmp select(cond, TV, FV), RHS", analyse it.
+ // Does "icmp TV, RHS" simplify?
+ if (Value *TCmp = SimplifyICmpInst(Pred, SI->getTrueValue(), RHS, TD))
+ // It does! Does "icmp FV, RHS" simplify?
+ if (Value *FCmp = SimplifyICmpInst(Pred, SI->getFalseValue(), RHS, TD))
+ // It does! If they simplified to the same value, then use it as the
+ // result of the original comparison.
+ if (TCmp == FCmp)
+ return TCmp;
+ }
+
return 0;
}
ret i32 %b
}
//===---------------------------------------------------------------------===//
-We should fold this code into "ret i1 false" since neither %zero nor %one can
-ever be null pointers.
-
-define i1 @foo(i1 %cond) {
- %zero = alloca i32
- %one = alloca i32
-
- %ptr = select i1 %cond, i32* %zero, i32* %one
- %isnull = icmp eq i32* %ptr, null
- ret i1 %isnull
-}
-//===---------------------------------------------------------------------===//
; CHECK: or i32 {{.*}}, 1
; CHECK: ret
}
+
+define i1 @test38(i1 %cond) {
+ %zero = alloca i32
+ %one = alloca i32
+ %ptr = select i1 %cond, i32* %zero, i32* %one
+ %isnull = icmp eq i32* %ptr, null
+ ret i1 %isnull
+; CHECK: @test38
+; CHECK: ret i1 false
+}