[llvm] [ConstraintElim] Use constraints from bounded memory accesses (PR #155253)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 26 07:55:31 PDT 2025


================
@@ -1109,10 +1113,43 @@ void State::addInfoForInductions(BasicBlock &BB) {
   }
 }
 
+static bool getConstraintFromMemoryAccess(GetElementPtrInst &GEP,
+                                          uint64_t AccessSize,
+                                          CmpPredicate &Pred, Value *&A,
+                                          Value *&B, const DataLayout &DL,
+                                          const TargetLibraryInfo &TLI) {
+  auto Offset = collectOffsets(cast<GEPOperator>(GEP), DL);
+  if (!Offset.NW.isInBounds())
+    return false;
+
+  if (Offset.VariableOffsets.size() != 1)
+    return false;
+
+  ObjectSizeOpts Opts;
+  // Workaround for gep inbounds, ptr null, idx.
+  Opts.NullIsUnknownSize = true;
+  ObjectSizeOffsetVisitor Visitor(DL, &TLI, GEP.getContext(), Opts);
+  SizeOffsetAPInt Data = Visitor.compute(Offset.BasePtr);
+  if (!Data.bothKnown() || !Data.Offset.isZero())
+    return false;
+
+  // Index * Scale + ConstOffset + AccessSize <= AllocSize
+  uint64_t BitWidth = Offset.ConstantOffset.getBitWidth();
+  auto &[Index, Scale] = Offset.VariableOffsets.front();
+  APInt MaxIndex =
+      (Data.Size - APInt(BitWidth, AccessSize) - Offset.ConstantOffset)
----------------
nikic wrote:

Should comment why we can ignore overflow here.

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


More information about the llvm-commits mailing list