[llvm] [FlattenCFG] Fix an Imprecise Usage of AA (PR #128117)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 17 02:04:08 PDT 2025


================
@@ -369,6 +369,22 @@ ModRefInfo AAResults::getModRefInfo(const CallBase *Call1,
   return Result;
 }
 
+// Check whether two instructions may read or write the same memory location.
+ModRefInfo AAResults::getModRefInfo(const Instruction *I1,
+                                    const Instruction *I2) {
+  SimpleAAQueryInfo AAQIP(*this);
+
+  // Early-exit if either instruction does not read or write memory.
+  if (!I1->mayReadOrWriteMemory() || !I2->mayReadOrWriteMemory())
+    return ModRefInfo::NoModRef;
+
+  if (const auto *Call2 = dyn_cast<CallBase>(I2))
+    return getModRefInfo(I1, Call2, AAQIP);
+
+  ModRefInfo MR = getModRefInfo(I1, MemoryLocation::getOrNone(I2), AAQIP);
+  return isModOrRefSet(MR) ? ModRefInfo::ModRef : ModRefInfo::NoModRef;
----------------
nikic wrote:

Basically, what you are currently doing is a very crude check that the two instructions *somehow* access the same memory. It can be made more precise by checking the kind of access. For example, if both instructions only read memory, we can actually return NoModRef, because read-read dependences can be ignored. I guess you could easily handle that part by updating your mayReadOrWriteMemory checks to check that at least one is a write instead.

Then if I1 is a read (and I2 a write) we can return Ref instead. If I1 is write (and I1 a read) we can return Mod. (This is why the order of the argument matters.)

Basically, what the Call-Call handling does starting from: https://github.com/llvm/llvm-project/blob/326c5de54024f7d2d1785a3d9531cc6e87492f27/llvm/lib/Analysis/AliasAnalysis.cpp#L289

I think it's ok if you add a FIXME.

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


More information about the llvm-commits mailing list