Skip to content

Commit 96a2a17

Browse files
committed
[SCCP] Simplify code. NFC.
1 parent 175612e commit 96a2a17

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

llvm/lib/Transforms/Utils/SCCPSolver.cpp

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,28 @@ bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
8585
return true;
8686
}
8787

88+
/// Helper for getting ranges from \p Solver. Instructions inserted during
89+
/// simplification are unavailable in the solver, so we return a full range for
90+
/// them.
91+
static ConstantRange getRange(Value *Op, SCCPSolver &Solver,
92+
const SmallPtrSetImpl<Value *> &InsertedValues) {
93+
if (auto *Const = dyn_cast<Constant>(Op))
94+
return Const->toConstantRange();
95+
if (InsertedValues.contains(Op)) {
96+
unsigned Bitwidth = Op->getType()->getScalarSizeInBits();
97+
return ConstantRange::getFull(Bitwidth);
98+
}
99+
return Solver.getLatticeValueFor(Op).asConstantRange(Op->getType(),
100+
/*UndefAllowed=*/false);
101+
}
102+
88103
/// Try to use \p Inst's value range from \p Solver to infer the NUW flag.
89104
static bool refineInstruction(SCCPSolver &Solver,
90105
const SmallPtrSetImpl<Value *> &InsertedValues,
91106
Instruction &Inst) {
92107
bool Changed = false;
93108
auto GetRange = [&Solver, &InsertedValues](Value *Op) {
94-
if (auto *Const = dyn_cast<Constant>(Op))
95-
return Const->toConstantRange();
96-
if (InsertedValues.contains(Op)) {
97-
unsigned Bitwidth = Op->getType()->getScalarSizeInBits();
98-
return ConstantRange::getFull(Bitwidth);
99-
}
100-
return Solver.getLatticeValueFor(Op).asConstantRange(
101-
Op->getType(), /*UndefAllowed=*/false);
109+
return getRange(Op, Solver, InsertedValues);
102110
};
103111

104112
if (isa<OverflowingBinaryOperator>(Inst)) {
@@ -169,16 +177,8 @@ static bool replaceSignedInst(SCCPSolver &Solver,
169177
SmallPtrSetImpl<Value *> &InsertedValues,
170178
Instruction &Inst) {
171179
// Determine if a signed value is known to be >= 0.
172-
auto isNonNegative = [&Solver](Value *V) {
173-
// If this value was constant-folded, it may not have a solver entry.
174-
// Handle integers. Otherwise, return false.
175-
if (auto *C = dyn_cast<Constant>(V)) {
176-
auto *CInt = dyn_cast<ConstantInt>(C);
177-
return CInt && !CInt->isNegative();
178-
}
179-
const ValueLatticeElement &IV = Solver.getLatticeValueFor(V);
180-
return IV.isConstantRange(/*UndefAllowed=*/false) &&
181-
IV.getConstantRange().isAllNonNegative();
180+
auto isNonNegative = [&Solver, &InsertedValues](Value *V) {
181+
return getRange(V, Solver, InsertedValues).isAllNonNegative();
182182
};
183183

184184
Instruction *NewInst = nullptr;
@@ -187,7 +187,7 @@ static bool replaceSignedInst(SCCPSolver &Solver,
187187
case Instruction::SExt: {
188188
// If the source value is not negative, this is a zext/uitofp.
189189
Value *Op0 = Inst.getOperand(0);
190-
if (InsertedValues.count(Op0) || !isNonNegative(Op0))
190+
if (!isNonNegative(Op0))
191191
return false;
192192
NewInst = CastInst::Create(Inst.getOpcode() == Instruction::SExt
193193
? Instruction::ZExt
@@ -199,7 +199,7 @@ static bool replaceSignedInst(SCCPSolver &Solver,
199199
case Instruction::AShr: {
200200
// If the shifted value is not negative, this is a logical shift right.
201201
Value *Op0 = Inst.getOperand(0);
202-
if (InsertedValues.count(Op0) || !isNonNegative(Op0))
202+
if (!isNonNegative(Op0))
203203
return false;
204204
NewInst = BinaryOperator::CreateLShr(Op0, Inst.getOperand(1), "", Inst.getIterator());
205205
NewInst->setIsExact(Inst.isExact());
@@ -209,8 +209,7 @@ static bool replaceSignedInst(SCCPSolver &Solver,
209209
case Instruction::SRem: {
210210
// If both operands are not negative, this is the same as udiv/urem.
211211
Value *Op0 = Inst.getOperand(0), *Op1 = Inst.getOperand(1);
212-
if (InsertedValues.count(Op0) || InsertedValues.count(Op1) ||
213-
!isNonNegative(Op0) || !isNonNegative(Op1))
212+
if (!isNonNegative(Op0) || !isNonNegative(Op1))
214213
return false;
215214
auto NewOpcode = Inst.getOpcode() == Instruction::SDiv ? Instruction::UDiv
216215
: Instruction::URem;
@@ -239,14 +238,7 @@ static Value *simplifyInstruction(SCCPSolver &Solver,
239238
SmallPtrSetImpl<Value *> &InsertedValues,
240239
Instruction &Inst) {
241240
auto GetRange = [&Solver, &InsertedValues](Value *Op) {
242-
if (auto *Const = dyn_cast<Constant>(Op))
243-
return Const->toConstantRange();
244-
if (InsertedValues.contains(Op)) {
245-
unsigned Bitwidth = Op->getType()->getScalarSizeInBits();
246-
return ConstantRange::getFull(Bitwidth);
247-
}
248-
return Solver.getLatticeValueFor(Op).asConstantRange(
249-
Op->getType(), /*UndefAllowed=*/false);
241+
return getRange(Op, Solver, InsertedValues);
250242
};
251243

252244
Value *X;

0 commit comments

Comments
 (0)