[llvm] [ArgPromotion] Perform alias analysis on actual arguments of Calls (PR #106216)

Hari Limaye via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 6 11:55:27 PDT 2024


================
@@ -745,6 +748,33 @@ static bool areTypesABICompatible(ArrayRef<Type *> Types, const Function &F,
   });
 }
 
+// Try to prove that all Calls to F do not modify the memory pointed to by Arg.
+// This can provide us with more opportunities to perform Argument Promotion in
+// cases where simply looking at a Function's instructions is insufficient to
+// prove that the pointer argument is not invalidated before all loads from it.
+static bool callDoesNotModifyArg(Function *F, unsigned ArgNo,
+                                 FunctionAnalysisManager &FAM) {
+  // Find all Users of F that are Calls, and see if they may modify Arg.
+  for (User *U : F->users()) {
+    auto *Call = dyn_cast<CallInst>(U);
+    if (!Call)
+      continue;
+
+    Value *ArgOp = Call->getArgOperand(ArgNo);
+    assert(ArgOp->getType()->isPointerTy() && "Argument must be Pointer Type!");
+
+    MemoryLocation Loc = MemoryLocation::getForArgument(Call, ArgNo, nullptr);
----------------
hazzlim wrote:

Ah I see, apologies I was misunderstanding you before. I have added this in [Address review 3](https://github.com/llvm/llvm-project/pull/106216/commits/6b9b01890f86389212cbe762e43bf896215dd9dd)

If I understand correctly we still run into an issue because we are doing an alias query on a CallInst, and because BasicAA is inherently non-interprocedural it disregards the Size of the Loc and uses getBeforeOrAfter anyway when checking if the pointer aliases any of the Call's arguments:
https://github.com/llvm/llvm-project/blob/6c143a86cddbc6d0431dd643bfc7d4f017042512/llvm/lib/Analysis/BasicAliasAnalysis.cpp#L973-L978

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


More information about the llvm-commits mailing list