[llvm] [llvm] Use LazyValueInfo to improve llvm.objectsize computation (PR #114673)

Harald van Dijk via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 5 12:38:52 PST 2024


================
@@ -709,14 +719,47 @@ OffsetSpan ObjectSizeOffsetVisitor::computeImpl(Value *V) {
   // readjust the APInt as we pass it upwards in order for the APInt to match
   // the type the caller passed in.
   APInt Offset(InitialIntTyBits, 0);
-  V = V->stripAndAccumulateConstantOffsets(
-      DL, Offset, /* AllowNonInbounds */ true, /* AllowInvariantGroup */ true);
+
+  // External Analysis used to compute the Min/Max value of individual Offsets
+  // within a GEP.
+  auto OffsetRangeAnalysis =
+      [this, V](Value &VOffset, APInt &Offset) {
+        if (auto *C = dyn_cast<ConstantInt>(&VOffset)) {
+          Offset = C->getValue();
+          return true;
+        }
+        if (Options.EvalMode != ObjectSizeOpts::Mode::Min &&
+            Options.EvalMode != ObjectSizeOpts::Mode::Max) {
+          return false;
+        }
+        ConstantRange CR = computeConstantRange(
+            &VOffset, /*ForSigned*/ true, /*UseInstrInfo*/ true, /*AC=*/nullptr,
+            /*CtxtI=*/dyn_cast<Instruction>(V), /*DT=*/Options.DT);
+        if (CR.isFullSet())
+          return false;
+
+        if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
+          Offset = CR.getSignedMax();
+          // Upper bound actually unknown.
+          if (Offset.isMaxSignedValue())
+            return false;
+        } else {
+          Offset = CR.getSignedMin();
+          // Lower bound actually unknown.
+          if (Offset.isMinSignedValue())
+            return false;
+        }
+        return true;
+      };
+
+  V = const_cast<Value *>(V->stripAndAccumulateConstantOffsets(
+      DL, Offset, /* AllowNonInbounds */ true, /* AllowInvariantGroup */ true,
+      /*ExternalAnalysis=*/OffsetRangeAnalysis));
----------------
hvdijk wrote:

This jumped out, this looks suspicious, but it is because https://reviews.llvm.org/D76208 only added `ExternalAnalysis` to the `const` overload of `stripAndAccumulateConstantOffsets` that this is needed. That seems like an oversight, can we add `ExternalAnalysis` to the non-`const` version too and take out the `const_cast` here?

https://github.com/llvm/llvm-project/pull/114673


More information about the llvm-commits mailing list