[llvm] a855c94 - [NFC] Don't copy MachineFrameInfo on each invocation of HasAlias

Sanjoy Das via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 6 19:15:13 PST 2021


Author: Sanjoy Das
Date: 2021-01-06T18:59:20-08:00
New Revision: a855c9403fbac6be8acaf1651932084645a00d11

URL: https://github.com/llvm/llvm-project/commit/a855c9403fbac6be8acaf1651932084645a00d11
DIFF: https://github.com/llvm/llvm-project/commit/a855c9403fbac6be8acaf1651932084645a00d11.diff

LOG: [NFC] Don't copy MachineFrameInfo on each invocation of HasAlias

Also fix a typo in a comment.  This fixes a compile time issue in XLA
(https://www.tensorflow.org/xla).

Differential Revision: https://reviews.llvm.org/D94182

Added: 
    

Modified: 
    llvm/lib/CodeGen/MachineInstr.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp
index 900abd2b260d..c1c7849fbe30 100644
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -1253,13 +1253,82 @@ bool MachineInstr::isSafeToMove(AAResults *AA, bool &SawStore) const {
   return true;
 }
 
+static bool MemOperandsHaveAlias(const MachineFrameInfo &MFI, AAResults *AA,
+                                 bool UseTBAA, const MachineMemOperand *MMOa,
+                                 const MachineMemOperand *MMOb) {
+  // The following interface to AA is fashioned after DAGCombiner::isAlias and
+  // operates with MachineMemOperand offset with some important assumptions:
+  //   - LLVM fundamentally assumes flat address spaces.
+  //   - MachineOperand offset can *only* result from legalization and cannot
+  //     affect queries other than the trivial case of overlap checking.
+  //   - These offsets never wrap and never step outside of allocated objects.
+  //   - There should never be any negative offsets here.
+  //
+  // FIXME: Modify API to hide this math from "user"
+  // Even before we go to AA we can reason locally about some memory objects. It
+  // can save compile time, and possibly catch some corner cases not currently
+  // covered.
+
+  int64_t OffsetA = MMOa->getOffset();
+  int64_t OffsetB = MMOb->getOffset();
+  int64_t MinOffset = std::min(OffsetA, OffsetB);
+
+  uint64_t WidthA = MMOa->getSize();
+  uint64_t WidthB = MMOb->getSize();
+  bool KnownWidthA = WidthA != MemoryLocation::UnknownSize;
+  bool KnownWidthB = WidthB != MemoryLocation::UnknownSize;
+
+  const Value *ValA = MMOa->getValue();
+  const Value *ValB = MMOb->getValue();
+  bool SameVal = (ValA && ValB && (ValA == ValB));
+  if (!SameVal) {
+    const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
+    const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
+    if (PSVa && ValB && !PSVa->mayAlias(&MFI))
+      return false;
+    if (PSVb && ValA && !PSVb->mayAlias(&MFI))
+      return false;
+    if (PSVa && PSVb && (PSVa == PSVb))
+      SameVal = true;
+  }
+
+  if (SameVal) {
+    if (!KnownWidthA || !KnownWidthB)
+      return true;
+    int64_t MaxOffset = std::max(OffsetA, OffsetB);
+    int64_t LowWidth = (MinOffset == OffsetA) ? WidthA : WidthB;
+    return (MinOffset + LowWidth > MaxOffset);
+  }
+
+  if (!AA)
+    return true;
+
+  if (!ValA || !ValB)
+    return true;
+
+  assert((OffsetA >= 0) && "Negative MachineMemOperand offset");
+  assert((OffsetB >= 0) && "Negative MachineMemOperand offset");
+
+  int64_t OverlapA =
+      KnownWidthA ? WidthA + OffsetA - MinOffset : MemoryLocation::UnknownSize;
+  int64_t OverlapB =
+      KnownWidthB ? WidthB + OffsetB - MinOffset : MemoryLocation::UnknownSize;
+
+  AliasResult AAResult = AA->alias(
+      MemoryLocation(ValA, OverlapA, UseTBAA ? MMOa->getAAInfo() : AAMDNodes()),
+      MemoryLocation(ValB, OverlapB,
+                     UseTBAA ? MMOb->getAAInfo() : AAMDNodes()));
+
+  return (AAResult != NoAlias);
+}
+
 bool MachineInstr::mayAlias(AAResults *AA, const MachineInstr &Other,
                             bool UseTBAA) const {
   const MachineFunction *MF = getMF();
   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
   const MachineFrameInfo &MFI = MF->getFrameInfo();
 
-  // Execulde call instruction which may alter the memory but can not be handled
+  // Exclude call instruction which may alter the memory but can not be handled
   // by this function.
   if (isCall() || Other.isCall())
     return true;
@@ -1287,83 +1356,11 @@ bool MachineInstr::mayAlias(AAResults *AA, const MachineInstr &Other,
   if (NumChecks > TII->getMemOperandAACheckLimit())
     return true;
 
-  auto HasAlias = [MFI, AA, UseTBAA](const MachineMemOperand *MMOa,
-                                     const MachineMemOperand *MMOb) {
-    // The following interface to AA is fashioned after DAGCombiner::isAlias
-    // and operates with MachineMemOperand offset with some important
-    // assumptions:
-    //   - LLVM fundamentally assumes flat address spaces.
-    //   - MachineOperand offset can *only* result from legalization and
-    //     cannot affect queries other than the trivial case of overlap
-    //     checking.
-    //   - These offsets never wrap and never step outside
-    //     of allocated objects.
-    //   - There should never be any negative offsets here.
-    //
-    // FIXME: Modify API to hide this math from "user"
-    // Even before we go to AA we can reason locally about some
-    // memory objects. It can save compile time, and possibly catch some
-    // corner cases not currently covered.
-
-    int64_t OffsetA = MMOa->getOffset();
-    int64_t OffsetB = MMOb->getOffset();
-    int64_t MinOffset = std::min(OffsetA, OffsetB);
-
-    uint64_t WidthA = MMOa->getSize();
-    uint64_t WidthB = MMOb->getSize();
-    bool KnownWidthA = WidthA != MemoryLocation::UnknownSize;
-    bool KnownWidthB = WidthB != MemoryLocation::UnknownSize;
-
-    const Value *ValA = MMOa->getValue();
-    const Value *ValB = MMOb->getValue();
-    bool SameVal = (ValA && ValB && (ValA == ValB));
-    if (!SameVal) {
-      const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
-      const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
-      if (PSVa && ValB && !PSVa->mayAlias(&MFI))
-        return false;
-      if (PSVb && ValA && !PSVb->mayAlias(&MFI))
-        return false;
-      if (PSVa && PSVb && (PSVa == PSVb))
-        SameVal = true;
-    }
-
-    if (SameVal) {
-      if (!KnownWidthA || !KnownWidthB)
-        return true;
-      int64_t MaxOffset = std::max(OffsetA, OffsetB);
-      int64_t LowWidth = (MinOffset == OffsetA) ? WidthA : WidthB;
-      return (MinOffset + LowWidth > MaxOffset);
-    }
-
-    if (!AA)
-      return true;
-
-    if (!ValA || !ValB)
-      return true;
-
-    assert((OffsetA >= 0) && "Negative MachineMemOperand offset");
-    assert((OffsetB >= 0) && "Negative MachineMemOperand offset");
-
-    int64_t OverlapA = KnownWidthA ? WidthA + OffsetA - MinOffset
-                                   : MemoryLocation::UnknownSize;
-    int64_t OverlapB = KnownWidthB ? WidthB + OffsetB - MinOffset
-                                   : MemoryLocation::UnknownSize;
-
-    AliasResult AAResult =
-        AA->alias(MemoryLocation(ValA, OverlapA,
-                                 UseTBAA ? MMOa->getAAInfo() : AAMDNodes()),
-                  MemoryLocation(ValB, OverlapB,
-                                 UseTBAA ? MMOb->getAAInfo() : AAMDNodes()));
-
-    return (AAResult != NoAlias);
-  };
-
   // Check each pair of memory operands from both instructions, which can't
   // alias only if all pairs won't alias.
   for (auto *MMOa : memoperands())
     for (auto *MMOb : Other.memoperands())
-      if (HasAlias(MMOa, MMOb))
+      if (MemOperandsHaveAlias(MFI, AA, UseTBAA, MMOa, MMOb))
         return true;
 
   return false;


        


More information about the llvm-commits mailing list