[llvm] [InstCombine] Support offsets in `memset` to load forwarding (PR #151924)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 4 08:16:59 PDT 2025


================
@@ -631,9 +631,14 @@ static Value *getAvailableLoadStore(Instruction *Inst, const Value *Ptr,
     if (!Val || !Len)
       return nullptr;
 
-    // TODO: Handle offsets.
-    Value *Dst = MSI->getDest();
-    if (!AreEquivalentAddressValues(Dst, Ptr))
+    // Handle offsets.
+    int64_t StoreOffset = 0, LoadOffset = 0;
+    const Value *StoreBase =
+        GetPointerBaseWithConstantOffset(MSI->getDest(), StoreOffset, DL);
+    const Value *LoadBase =
+        GetPointerBaseWithConstantOffset(Ptr, LoadOffset, DL);
+    int64_t Offset = LoadOffset - StoreOffset;
+    if (StoreBase != LoadBase || Offset < 0)
----------------
nikic wrote:

```suggestion
    if (StoreBase != LoadBase || LoadOffset < StoreOffset)
```
and move Offset calculation down. Otherwise we risk signed integer overflow.

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


More information about the llvm-commits mailing list