Skip to content

Commit 2e68e4d

Browse files
committed
[InstCombine] make fold for icmp with sext more efficient; NFC
We were creating 2 instructions and relying on a subsequent fold to invert a not(icmp). Create the final icmp directly instead. llvm-svn: 369411
1 parent 22ac9f3 commit 2e68e4d

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4128,27 +4128,21 @@ Instruction *InstCombiner::foldICmpWithCastOp(ICmpInst &ICmp) {
41284128
// The re-extended constant changed, partly changed (in the case of a vector),
41294129
// or could not be determined to be equal (in the case of a constant
41304130
// expression), so the constant cannot be represented in the shorter type.
4131-
// Consequently, we cannot emit a simple comparison.
41324131
// All the cases that fold to true or false will have already been handled
41334132
// by SimplifyICmpInst, so only deal with the tricky case.
4134-
41354133
if (isSignedCmp || !isSignedExt || !isa<ConstantInt>(C))
41364134
return nullptr;
41374135

4138-
// Evaluate the comparison for LT (we invert for GT below). LE and GE cases
4139-
// should have been folded away previously and not enter in here.
4140-
4141-
// We're performing an unsigned comp with a sign extended value.
4142-
// This is true if the input is >= 0. [aka >s -1]
4143-
Constant *NegOne = Constant::getAllOnesValue(SrcTy);
4144-
Value *Result = Builder.CreateICmpSGT(Op0Src, NegOne, ICmp.getName());
4145-
4146-
// Finally, return the value computed.
4136+
// Is source op positive?
4137+
// icmp ult (sext X), C --> icmp sgt X, -1
41474138
if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
4148-
return replaceInstUsesWith(ICmp, Result);
4139+
return new ICmpInst(CmpInst::ICMP_SGT, Op0Src,
4140+
Constant::getAllOnesValue(SrcTy));
41494141

4142+
// Is source op negative?
4143+
// icmp ugt (sext X), C --> icmp slt X, 0
41504144
assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
4151-
return BinaryOperator::CreateNot(Result);
4145+
return new ICmpInst(CmpInst::ICMP_SLT, Op0Src, Constant::getNullValue(SrcTy));
41524146
}
41534147

41544148
static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS) {

0 commit comments

Comments
 (0)