[llvm] [llvm] Use LazyValueInfo to improve llvm.objectsize computation (PR #114673)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 5 12:15:30 PST 2024
================
@@ -856,6 +910,48 @@ OffsetSpan ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV) {
return OffsetSpan(Zero, align(Size, GV.getAlign()));
}
+OffsetSpan ObjectSizeOffsetVisitor::visitGetElementPtr(GetElementPtrInst &GEP) {
+ OffsetSpan PtrData = computeImpl(GEP.getPointerOperand());
+ if (!PtrData.bothKnown())
+ return ObjectSizeOffsetVisitor::unknown();
+
+ if (Options.EvalMode == ObjectSizeOpts::Mode::Min ||
+ Options.EvalMode == ObjectSizeOpts::Mode::Max) {
+ unsigned BitWidth = PtrData.After.getBitWidth();
+ APInt ConstantOffset = Zero;
+ SmallMapVector<Value *, APInt, 4> VariableOffsets;
+ if (!GEP.collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
+ return ObjectSizeOffsetVisitor::unknown();
+
+ ConstantRange AccumulatedRange = ConstantOffset;
+ for (auto const &VO : VariableOffsets) {
+ ConstantRange CR = computeConstantRange(
+ VO.first, /*ForSigned*/ true, /*UseInstrInfo*/ true, /*AC=*/nullptr,
+ /*CtxtI=*/&GEP, /*DT=*/Options.DT);
+ if (CR.isFullSet())
+ return ObjectSizeOffsetVisitor::unknown();
+
+ AccumulatedRange = AccumulatedRange.add(CR.multiply(VO.second));
+ }
+
+ APInt Bound;
+ if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
+ Bound = AccumulatedRange.getSignedMax();
+ // Upper bound actually unknown.
+ if (Bound.isMaxSignedValue())
+ return ObjectSizeOffsetVisitor::unknown();
+ } else {
+ Bound = AccumulatedRange.getSignedMin();
+ // Lower bound actually unknown.
+ if (Bound.isMinSignedValue())
+ return ObjectSizeOffsetVisitor::unknown();
+ }
+
+ return {PtrData.Before + Bound, PtrData.After - Bound};
----------------
serge-sans-paille wrote:
it turns out `stripAndAccumulateConstantOffset` accepts an optional argument that makes it possible to plug our analysis, so I used it and as a side effect it delegates the answer to your question to this function ;-)
https://github.com/llvm/llvm-project/pull/114673
More information about the llvm-commits
mailing list