[llvm] [InferAddressSpaces] Infer pointer stored and then loaded from global variable (PR #159755)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 19 04:56:08 PDT 2025


================
@@ -373,6 +399,19 @@ getPointerOperands(const Value &V, const DataLayout &DL,
     auto *P2I = cast<Operator>(Op.getOperand(0));
     return {P2I->getOperand(0)};
   }
+  case Instruction::Load: {
+    assert(V.getType()->isPtrOrPtrVectorTy());
+    if (const auto *GV = cast<GlobalVariable>(Op.getOperand(0))) {
+      assert(isLocallyAccessedBySimpleLoadsStores(
+          GV, cast<LoadInst>(&V)->getFunction()));
+      SmallVector<Value *, 2> PtrOps;
+      for (const auto *U : GV->users())
+        if (isa<StoreInst>(U))
+          PtrOps.push_back(cast<Operator>(U)->getOperand(0));
----------------
jmmartinez wrote:

Nitpick: you could use a `dyn_cast` to avoid the `isa + cast<>`. It also makes obvious which operand we're getting (I had to check if 0 was the value or the pointer).

```suggestion
        if (auto *S = dyn_cast<StoreInst>(U))
          PtrOps.push_back(S->getValueOperand());
```

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


More information about the llvm-commits mailing list