Skip to content

Commit

Permalink
[ConstraintElim] Add range support for Constraint Elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
dtcxzyw committed Dec 3, 2024
1 parent e776484 commit b8bae44
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1600,8 +1600,68 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B,
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
std::move(ValuesToRelease));

if (!R.IsSigned) {
for (Value *V : NewVariables) {
// Add range information from attributes and metadata.
for (auto *V : NewVariables) {
if (V->getType()->getScalarSizeInBits() >= 64)
continue;
std::optional<ConstantRange> CR;
if (const auto *Arg = dyn_cast<Argument>(V)) {
CR = Arg->getRange();
} else if (const auto *CB = dyn_cast<CallBase>(V)) {
CR = CB->getRange();
} else if (const auto *I = dyn_cast<Instruction>(V)) {
if (auto *Range = I->getMetadata(LLVMContext::MD_range))
CR = getConstantRangeFromMetadata(*Range);
}
if (CR) {
if (R.IsSigned) {
int64_t MaxVal = CR->getSignedMax().getSExtValue();
int64_t MinVal = CR->getSignedMin().getSExtValue();
if (MaxVal != MaxConstraintValue) {
ConstraintTy VarPos(
SmallVector<int64_t, 8>(Value2Index.size() + 1, 0), false,
false, false);
VarPos.Coefficients[0] = MaxVal;
VarPos.Coefficients[Value2Index[V]] = 1;
CSToUse.addVariableRow(VarPos.Coefficients);
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
SmallVector<Value *, 2>());
}
if (MinVal != MinSignedConstraintValue) {
ConstraintTy VarPos(
SmallVector<int64_t, 8>(Value2Index.size() + 1, 0), false,
false, false);
VarPos.Coefficients[0] = MinVal;
VarPos.Coefficients[Value2Index[V]] = -1;
CSToUse.addVariableRow(VarPos.Coefficients);
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
SmallVector<Value *, 2>());
}
} else {
uint64_t MaxVal = CR->getUnsignedMax().getZExtValue();
uint64_t MinVal = CR->getUnsignedMin().getZExtValue();
if (MaxVal < static_cast<uint64_t>(MaxConstraintValue)) {
ConstraintTy VarPos(
SmallVector<int64_t, 8>(Value2Index.size() + 1, 0), false,
false, false);
VarPos.Coefficients[0] = MaxVal;
VarPos.Coefficients[Value2Index[V]] = 1;
CSToUse.addVariableRow(VarPos.Coefficients);
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
SmallVector<Value *, 2>());
}
MinVal =
std::min(MinVal, static_cast<uint64_t>(MaxConstraintValue - 1));
ConstraintTy VarPos(
SmallVector<int64_t, 8>(Value2Index.size() + 1, 0), false, false,
false);
VarPos.Coefficients[0] = MinVal;
VarPos.Coefficients[Value2Index[V]] = -1;
CSToUse.addVariableRow(VarPos.Coefficients);
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
SmallVector<Value *, 2>());
}
} else if (!R.IsSigned) {
ConstraintTy VarPos(SmallVector<int64_t, 8>(Value2Index.size() + 1, 0),
false, false, false);
VarPos.Coefficients[Value2Index[V]] = -1;
Expand Down

0 comments on commit b8bae44

Please sign in to comment.