Skip to content

Commit 17367b0

Browse files
committed
[LVI] Extract helper for binary range calculations; NFC
llvm-svn: 361692
1 parent 46e5052 commit 17367b0

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ namespace {
422422
BasicBlock *BB);
423423
Optional<ConstantRange> getRangeForOperand(unsigned Op, Instruction *I,
424424
BasicBlock *BB);
425+
bool solveBlockValueBinaryOpImpl(
426+
ValueLatticeElement &BBLV, Instruction *I, BasicBlock *BB,
427+
std::function<ConstantRange(const ConstantRange &,
428+
const ConstantRange &)> OpFn);
425429
bool solveBlockValueBinaryOp(ValueLatticeElement &BBLV, BinaryOperator *BBI,
426430
BasicBlock *BB);
427431
bool solveBlockValueCast(ValueLatticeElement &BBLV, CastInst *CI,
@@ -1040,6 +1044,26 @@ bool LazyValueInfoImpl::solveBlockValueCast(ValueLatticeElement &BBLV,
10401044
return true;
10411045
}
10421046

1047+
bool LazyValueInfoImpl::solveBlockValueBinaryOpImpl(
1048+
ValueLatticeElement &BBLV, Instruction *I, BasicBlock *BB,
1049+
std::function<ConstantRange(const ConstantRange &,
1050+
const ConstantRange &)> OpFn) {
1051+
// Figure out the ranges of the operands. If that fails, use a
1052+
// conservative range, but apply the transfer rule anyways. This
1053+
// lets us pick up facts from expressions like "and i32 (call i32
1054+
// @foo()), 32"
1055+
Optional<ConstantRange> LHSRes = getRangeForOperand(0, I, BB);
1056+
Optional<ConstantRange> RHSRes = getRangeForOperand(1, I, BB);
1057+
if (!LHSRes.hasValue() || !RHSRes.hasValue())
1058+
// More work to do before applying this transfer rule.
1059+
return false;
1060+
1061+
ConstantRange LHSRange = LHSRes.getValue();
1062+
ConstantRange RHSRange = RHSRes.getValue();
1063+
BBLV = ValueLatticeElement::getRange(OpFn(LHSRange, RHSRange));
1064+
return true;
1065+
}
1066+
10431067
bool LazyValueInfoImpl::solveBlockValueBinaryOp(ValueLatticeElement &BBLV,
10441068
BinaryOperator *BO,
10451069
BasicBlock *BB) {
@@ -1060,36 +1084,17 @@ bool LazyValueInfoImpl::solveBlockValueBinaryOp(ValueLatticeElement &BBLV,
10601084
case Instruction::AShr:
10611085
case Instruction::And:
10621086
case Instruction::Or:
1063-
// continue into the code below
1064-
break;
1087+
return solveBlockValueBinaryOpImpl(BBLV, BO, BB,
1088+
[BO](const ConstantRange &CR1, const ConstantRange &CR2) {
1089+
return CR1.binaryOp(BO->getOpcode(), CR2);
1090+
});
10651091
default:
10661092
// Unhandled instructions are overdefined.
10671093
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
10681094
<< "' - overdefined (unknown binary operator).\n");
10691095
BBLV = ValueLatticeElement::getOverdefined();
10701096
return true;
10711097
};
1072-
1073-
// Figure out the ranges of the operands. If that fails, use a
1074-
// conservative range, but apply the transfer rule anyways. This
1075-
// lets us pick up facts from expressions like "and i32 (call i32
1076-
// @foo()), 32"
1077-
Optional<ConstantRange> LHSRes = getRangeForOperand(0, BO, BB);
1078-
Optional<ConstantRange> RHSRes = getRangeForOperand(1, BO, BB);
1079-
1080-
if (!LHSRes.hasValue() || !RHSRes.hasValue())
1081-
// More work to do before applying this transfer rule.
1082-
return false;
1083-
1084-
ConstantRange LHSRange = LHSRes.getValue();
1085-
ConstantRange RHSRange = RHSRes.getValue();
1086-
1087-
// NOTE: We're currently limited by the set of operations that ConstantRange
1088-
// can evaluate symbolically. Enhancing that set will allows us to analyze
1089-
// more definitions.
1090-
Instruction::BinaryOps BinOp = BO->getOpcode();
1091-
BBLV = ValueLatticeElement::getRange(LHSRange.binaryOp(BinOp, RHSRange));
1092-
return true;
10931098
}
10941099

10951100
static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,

0 commit comments

Comments
 (0)