Skip to content

Commit f94bbe1

Browse files
committed
[LVI] Refactor getValueFromICmpCondition (NFC)
Rewrite this in a way where the core logic is in a separate function, that is invoked with swapped operands. This makes it easier to add handling for additional icmp patterns.
1 parent 0bfeede commit f94bbe1

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,26 @@ static bool matchICmpOperand(const APInt *&Offset, Value *LHS, Value *Val,
10961096
return false;
10971097
}
10981098

1099+
/// Get value range for a "(Val + Offset) Pred RHS" condition.
1100+
static ValueLatticeElement getValueFromSimpleICmpCondition(
1101+
CmpInst::Predicate Pred, Value *RHS, const APInt *Offset) {
1102+
ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
1103+
/*isFullSet=*/true);
1104+
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
1105+
RHSRange = ConstantRange(CI->getValue());
1106+
else if (Instruction *I = dyn_cast<Instruction>(RHS))
1107+
if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1108+
RHSRange = getConstantRangeFromMetadata(*Ranges);
1109+
1110+
ConstantRange TrueValues =
1111+
ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
1112+
1113+
if (Offset)
1114+
TrueValues = TrueValues.subtract(*Offset);
1115+
1116+
return ValueLatticeElement::getRange(std::move(TrueValues));
1117+
}
1118+
10991119
static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
11001120
bool isTrueDest) {
11011121
Value *LHS = ICI->getOperand(0);
@@ -1118,30 +1138,14 @@ static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
11181138
return ValueLatticeElement::getOverdefined();
11191139

11201140
const APInt *Offset = nullptr;
1121-
if (!matchICmpOperand(Offset, LHS, Val, EdgePred)) {
1122-
std::swap(LHS, RHS);
1123-
EdgePred = CmpInst::getSwappedPredicate(EdgePred);
1124-
if (!matchICmpOperand(Offset, LHS, Val, EdgePred))
1125-
return ValueLatticeElement::getOverdefined();
1126-
}
1127-
1128-
// Calculate the range of values that are allowed by the comparison.
1129-
ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
1130-
/*isFullSet=*/true);
1131-
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
1132-
RHSRange = ConstantRange(CI->getValue());
1133-
else if (Instruction *I = dyn_cast<Instruction>(RHS))
1134-
if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1135-
RHSRange = getConstantRangeFromMetadata(*Ranges);
1141+
if (matchICmpOperand(Offset, LHS, Val, EdgePred))
1142+
return getValueFromSimpleICmpCondition(EdgePred, RHS, Offset);
11361143

1137-
// If we're interested in the false dest, invert the condition
1138-
ConstantRange TrueValues =
1139-
ConstantRange::makeAllowedICmpRegion(EdgePred, RHSRange);
1144+
CmpInst::Predicate SwappedPred = CmpInst::getSwappedPredicate(EdgePred);
1145+
if (matchICmpOperand(Offset, RHS, Val, SwappedPred))
1146+
return getValueFromSimpleICmpCondition(SwappedPred, LHS, Offset);
11401147

1141-
if (Offset) // Apply the offset from above.
1142-
TrueValues = TrueValues.subtract(*Offset);
1143-
1144-
return ValueLatticeElement::getRange(std::move(TrueValues));
1148+
return ValueLatticeElement::getOverdefined();
11451149
}
11461150

11471151
// Handle conditions of the form

0 commit comments

Comments
 (0)