[BasicAA] Avoid duplicate query for GEPs with identical offsets (NFCI)
authorNikita Popov <nikita.ppv@gmail.com>
Sat, 13 Feb 2021 22:43:57 +0000 (23:43 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Sun, 14 Feb 2021 16:18:28 +0000 (17:18 +0100)
For two GEPs with identical offsets, we currently first perform
a base address query without size information, and then if it is
MayAlias, perform another with size information. This is pointless,
as the latter query should produce strictly better results.

This was not quite true historically due to the way that NoAlias
assumptions were handled, but that issue has since been resolved.

llvm/lib/Analysis/BasicAliasAnalysis.cpp

index 8f07aa6..b7af511 100644 (file)
@@ -1116,21 +1116,17 @@ AliasResult BasicAAResult::aliasGEP(
     DecompGEP1.Offset -= DecompGEP2.Offset;
     GetIndexDifference(DecompGEP1.VarIndices, DecompGEP2.VarIndices);
 
-    // Do the base pointers alias?
-    AliasResult BaseAlias = getBestAAResults().alias(
-        MemoryLocation::getBeforeOrAfter(UnderlyingV1),
-        MemoryLocation::getBeforeOrAfter(UnderlyingV2), AAQI);
-
     // For GEPs with identical offsets, we can preserve the size and AAInfo
     // when performing the alias check on the underlying objects.
-    if (BaseAlias == MayAlias && DecompGEP1.Offset == 0 &&
-        DecompGEP1.VarIndices.empty()) {
-      AliasResult PreciseBaseAlias = getBestAAResults().alias(
+    if (DecompGEP1.Offset == 0 && DecompGEP1.VarIndices.empty())
+      return getBestAAResults().alias(
           MemoryLocation(UnderlyingV1, V1Size, V1AAInfo),
           MemoryLocation(UnderlyingV2, V2Size, V2AAInfo), AAQI);
-      if (PreciseBaseAlias == NoAlias)
-        return NoAlias;
-    }
+
+    // Do the base pointers alias?
+    AliasResult BaseAlias = getBestAAResults().alias(
+        MemoryLocation::getBeforeOrAfter(UnderlyingV1),
+        MemoryLocation::getBeforeOrAfter(UnderlyingV2), AAQI);
 
     // If we get a No or May, then return it immediately, no amount of analysis
     // will improve this situation.