[AliasAnalysis] Give back AA results for fence instructions
authorDavid Majnemer <david.majnemer@gmail.com>
Fri, 15 Jul 2016 17:19:24 +0000 (17:19 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Fri, 15 Jul 2016 17:19:24 +0000 (17:19 +0000)
Calling getModRefInfo with a fence resulted in crashes because fences
don't have a memory location.  Add a new predicate to Instruction
called isFenceLike which indicates that the instruction mutates memory
but not any single memory location in particular. In practice, it is a
proxy for the set of instructions which "mayWriteToMemory" but cannot be
used with MemoryLocation::get.

This fixes PR28570.

llvm-svn: 275581

llvm/include/llvm/IR/Instruction.h
llvm/lib/Analysis/AliasAnalysis.cpp
llvm/lib/Transforms/Utils/MemorySSA.cpp

index 748bb94..df4f8df 100644 (file)
@@ -399,6 +399,23 @@ public:
   /// Return true if this instruction may throw an exception.
   bool mayThrow() const;
 
+  /// Return true if this instruction behaves like a memory fence: it can load
+  /// or store to memory location without being given a memory location.
+  bool isFenceLike() const {
+    switch (getOpcode()) {
+    default:
+      return false;
+    // This list should be kept in sync with the list in mayWriteToMemory for
+    // all opcodes which don't have a memory location.
+    case Instruction::Fence:
+    case Instruction::CatchPad:
+    case Instruction::CatchRet:
+    case Instruction::Call:
+    case Instruction::Invoke:
+      return true;
+    }
+  }
+
   /// Return true if the instruction may have side effects.
   ///
   /// Note that this does not consider malloc and alloca to have side
index ab5f814..f931b6f 100644 (file)
@@ -111,6 +111,9 @@ ModRefInfo AAResults::getModRefInfo(Instruction *I, ImmutableCallSite Call) {
   if (auto CS = ImmutableCallSite(I)) {
     // Check if the two calls modify the same memory
     return getModRefInfo(CS, Call);
+  } else if (I->isFenceLike()) {
+    // If this is a fence, just return MRI_ModRef.
+    return MRI_ModRef;
   } else {
     // Otherwise, check if the call modifies or references the
     // location this memory access defines.  The best we can say
index 0aed133..f93917f 100644 (file)
@@ -1252,7 +1252,7 @@ MemoryAccess *MemorySSA::CachingWalker::getClobberingMemoryAccess(
 
   // Conservatively, fences are always clobbers, so don't perform the walk if we
   // hit a fence.
-  if (isa<FenceInst>(I))
+  if (!ImmutableCallSite(I) && I->isFenceLike())
     return StartingUseOrDef;
 
   UpwardsMemoryQuery Q;
@@ -1287,15 +1287,17 @@ MemorySSA::CachingWalker::getClobberingMemoryAccess(const Instruction *I) {
   // access, since we only map BB's to PHI's. So, this must be a use or def.
   auto *StartingAccess = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(I));
 
-  // We can't sanely do anything with a FenceInst, they conservatively
+  bool IsCall = bool(ImmutableCallSite(I));
+
+  // We can't sanely do anything with a fences, they conservatively
   // clobber all memory, and have no locations to get pointers from to
-  // try to disambiguate
-  if (isa<FenceInst>(I))
+  // try to disambiguate.
+  if (!IsCall && I->isFenceLike())
     return StartingAccess;
 
   UpwardsMemoryQuery Q;
   Q.OriginalAccess = StartingAccess;
-  Q.IsCall = bool(ImmutableCallSite(I));
+  Q.IsCall = IsCall;
   if (!Q.IsCall)
     Q.StartingLoc = MemoryLocation::get(I);
   Q.Inst = I;