[llvm] InferAddressSpaces: Improve handling of instructions with multiple pointer uses (PR #101922)

Artem Belevich via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 5 11:17:12 PDT 2024


================
@@ -1002,31 +1002,50 @@ bool InferAddressSpacesImpl::updateAddressSpace(
   return true;
 }
 
+/// Replace operand \p OpIdx in \p Inst, if the value is the same as \p OldVal
+/// with \p NewVal.
+static bool replaceOperandIfSame(Instruction *Inst, unsigned OpIdx,
+                                 Value *OldVal, Value *NewVal) {
+  Use &U = Inst->getOperandUse(OpIdx);
+  if (U.get() == OldVal) {
+    U.set(NewVal);
+    return true;
+  }
+
+  return false;
+}
+
+template <typename InstrType>
+static bool replaceSimplePointerUse(const TargetTransformInfo &TTI,
+                                    InstrType *MemInstr, unsigned AddrSpace,
+                                    Value *OldV, Value *NewV) {
+  if (!MemInstr->isVolatile() || TTI.hasVolatileVariant(MemInstr, AddrSpace)) {
+    return replaceOperandIfSame(MemInstr, InstrType::getPointerOperandIndex(),
+                                OldV, NewV);
+  }
+
+  return false;
+}
+
 /// \p returns true if \p U is the pointer operand of a memory instruction with
 /// a single pointer operand that can have its address space changed by simply
 /// mutating the use to a new value. If the memory instruction is volatile,
 /// return true only if the target allows the memory instruction to be volatile
 /// in the new address space.
 static bool isSimplePointerUseValidToReplace(const TargetTransformInfo &TTI,
----------------
Artem-B wrote:

We may want to rename this function. `is` implies that it's some sort of query function and I would not expect it to actually change the IR. It used to be the case, but now that we're actually replacing the pointers, the name should reflect that.

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


More information about the llvm-commits mailing list