Skip to content

Commit 60ea1b4

Browse files
committed
[InstCombine] clean up foldICmpAddConstant(); NFCI
1. Fix variable names 2. Add local variables to reduce code 3. Fix code comments 4. Add early exit to reduce indentation 5. Remove 'else' after if -> return 6. Hoist common predicate llvm-svn: 278864
1 parent e0b8718 commit 60ea1b4

File tree

1 file changed

+41
-44
lines changed

1 file changed

+41
-44
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,58 +2195,55 @@ Instruction *InstCombiner::foldICmpSubConstant(ICmpInst &Cmp, Instruction *Sub,
21952195
return nullptr;
21962196
}
21972197

2198-
Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &ICI, Instruction *LHSI,
2199-
const APInt *RHSV) {
2198+
/// Fold icmp (add X, Y), C.
2199+
Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &Cmp, Instruction *Add,
2200+
const APInt *C) {
22002201
// FIXME: This check restricts all folds under here to scalar types.
2201-
ConstantInt *RHS = dyn_cast<ConstantInt>(ICI.getOperand(1));
2202+
ConstantInt *RHS = dyn_cast<ConstantInt>(Cmp.getOperand(1));
22022203
if (!RHS)
22032204
return nullptr;
22042205

2205-
// Fold: icmp pred (add X, C1), C2
2206-
if (!ICI.isEquality()) {
2207-
ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
2208-
if (!LHSC)
2209-
return nullptr;
2206+
if (Cmp.isEquality())
2207+
return nullptr;
22102208

2211-
const APInt &LHSV = LHSC->getValue();
2212-
ConstantRange CR =
2213-
ICI.makeConstantRange(ICI.getPredicate(), *RHSV).subtract(LHSV);
2214-
2215-
if (ICI.isSigned()) {
2216-
if (CR.getLower().isSignBit()) {
2217-
return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
2218-
Builder->getInt(CR.getUpper()));
2219-
} else if (CR.getUpper().isSignBit()) {
2220-
return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
2221-
Builder->getInt(CR.getLower()));
2222-
}
2223-
} else {
2224-
if (CR.getLower().isMinValue()) {
2225-
return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
2226-
Builder->getInt(CR.getUpper()));
2227-
} else if (CR.getUpper().isMinValue()) {
2228-
return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
2229-
Builder->getInt(CR.getLower()));
2230-
}
2231-
}
2209+
// Fold: icmp pred (add X, C2), C
2210+
Value *X = Add->getOperand(0);
2211+
ConstantInt *AddC = dyn_cast<ConstantInt>(Add->getOperand(1));
2212+
if (!AddC)
2213+
return nullptr;
22322214

2233-
// X-C1 <u C2 -> (X & -C2) == C1
2234-
// iff C1 & (C2-1) == 0
2215+
const APInt &C2 = AddC->getValue();
2216+
ConstantRange CR = Cmp.makeConstantRange(Cmp.getPredicate(), *C).subtract(C2);
2217+
const APInt &Upper = CR.getUpper();
2218+
const APInt &Lower = CR.getLower();
2219+
if (Cmp.isSigned()) {
2220+
if (Lower.isSignBit())
2221+
return new ICmpInst(ICmpInst::ICMP_SLT, X, Builder->getInt(Upper));
2222+
if (Upper.isSignBit())
2223+
return new ICmpInst(ICmpInst::ICMP_SGE, X, Builder->getInt(Lower));
2224+
} else {
2225+
if (Lower.isMinValue())
2226+
return new ICmpInst(ICmpInst::ICMP_ULT, X, Builder->getInt(Upper));
2227+
if (Upper.isMinValue())
2228+
return new ICmpInst(ICmpInst::ICMP_UGE, X, Builder->getInt(Lower));
2229+
}
2230+
2231+
if (Add->hasOneUse()) {
2232+
// X+C <u C2 -> (X & -C2) == C
2233+
// iff C & (C2-1) == 0
22352234
// C2 is a power of 2
2236-
if (ICI.getPredicate() == ICmpInst::ICMP_ULT && LHSI->hasOneUse() &&
2237-
RHSV->isPowerOf2() && (LHSV & (*RHSV - 1)) == 0)
2238-
return new ICmpInst(ICmpInst::ICMP_EQ,
2239-
Builder->CreateAnd(LHSI->getOperand(0), -(*RHSV)),
2240-
ConstantExpr::getNeg(LHSC));
2241-
2242-
// X-C1 >u C2 -> (X & ~C2) != C1
2243-
// iff C1 & C2 == 0
2235+
if (Cmp.getPredicate() == ICmpInst::ICMP_ULT && C->isPowerOf2() &&
2236+
(C2 & (*C - 1)) == 0)
2237+
return new ICmpInst(ICmpInst::ICMP_EQ, Builder->CreateAnd(X, -(*C)),
2238+
ConstantExpr::getNeg(AddC));
2239+
2240+
// X+C >u C2 -> (X & ~C2) != C
2241+
// iff C & C2 == 0
22442242
// C2+1 is a power of 2
2245-
if (ICI.getPredicate() == ICmpInst::ICMP_UGT && LHSI->hasOneUse() &&
2246-
(*RHSV + 1).isPowerOf2() && (LHSV & *RHSV) == 0)
2247-
return new ICmpInst(ICmpInst::ICMP_NE,
2248-
Builder->CreateAnd(LHSI->getOperand(0), ~(*RHSV)),
2249-
ConstantExpr::getNeg(LHSC));
2243+
if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && (*C + 1).isPowerOf2() &&
2244+
(C2 & *C) == 0)
2245+
return new ICmpInst(ICmpInst::ICMP_NE, Builder->CreateAnd(X, ~(*C)),
2246+
ConstantExpr::getNeg(AddC));
22502247
}
22512248
return nullptr;
22522249
}

0 commit comments

Comments
 (0)