[llvm] 2a20c64 - [Attributor][NFCI] Use DominatorTree to short circuit reachability queries

Johannes Doerfert via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 3 00:13:07 PDT 2023


Author: Johannes Doerfert
Date: 2023-08-03T00:12:46-07:00
New Revision: 2a20c64a73b8b29ca0e3638a068217d1eac653d7

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

LOG: [Attributor][NFCI] Use DominatorTree to short circuit reachability queries

If we do not have exlusion blocks and the target is live, we only need
to reach a dominating block.

Added: 
    

Modified: 
    llvm/lib/Transforms/IPO/AttributorAttributes.cpp
    llvm/test/Transforms/Attributor/noalias.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
index e3fafad919ccad..0774b9880590da 100644
--- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
+++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
@@ -3616,7 +3616,10 @@ struct AAIntraFnReachabilityFunction final
     : public CachedReachabilityAA<AAIntraFnReachability, Instruction> {
   using Base = CachedReachabilityAA<AAIntraFnReachability, Instruction>;
   AAIntraFnReachabilityFunction(const IRPosition &IRP, Attributor &A)
-      : Base(IRP, A) {}
+      : Base(IRP, A) {
+    DT = A.getInfoCache().getAnalysisResultForFunction<DominatorTreeAnalysis>(
+        *IRP.getAssociatedFunction());
+  }
 
   bool isAssumedReachable(
       Attributor &A, const Instruction &From, const Instruction &To,
@@ -3686,10 +3689,12 @@ struct AAIntraFnReachabilityFunction final
     if (!WillReachInBlock(ToBB->front(), *RQI.To, RQI.ExclusionSet))
       return rememberResult(A, RQITy::Reachable::No, RQI, UsedExclusionSet);
 
+    const Function *Fn = FromBB->getParent();
     SmallPtrSet<const BasicBlock *, 16> ExclusionBlocks;
     if (RQI.ExclusionSet)
       for (auto *I : *RQI.ExclusionSet)
-        ExclusionBlocks.insert(I->getParent());
+        if (I->getFunction() == Fn)
+          ExclusionBlocks.insert(I->getParent());
 
     // Check if we make it out of the FromBB block at all.
     if (ExclusionBlocks.count(FromBB) &&
@@ -3722,6 +3727,10 @@ struct AAIntraFnReachabilityFunction final
         if (SuccBB == ToBB)
           return rememberResult(A, RQITy::Reachable::Yes, RQI,
                                 UsedExclusionSet);
+        if (DT && ExclusionBlocks.empty() && DT->dominates(BB, ToBB))
+          return rememberResult(A, RQITy::Reachable::Yes, RQI,
+                                UsedExclusionSet);
+
         if (ExclusionBlocks.count(SuccBB)) {
           UsedExclusionSet = true;
           continue;
@@ -3745,6 +3754,9 @@ struct AAIntraFnReachabilityFunction final
   // Set of assumed dead edges we used in the last query. If any changes we
   // update the state.
   DenseSet<std::pair<const BasicBlock *, const BasicBlock *>> DeadEdges;
+
+  /// The dominator tree of the function to short-circuit reasoning.
+  const DominatorTree *DT = nullptr;
 };
 } // namespace
 

diff  --git a/llvm/test/Transforms/Attributor/noalias.ll b/llvm/test/Transforms/Attributor/noalias.ll
index 8bb346586cf0e2..00ca83d0c5d688 100644
--- a/llvm/test/Transforms/Attributor/noalias.ll
+++ b/llvm/test/Transforms/Attributor/noalias.ll
@@ -737,7 +737,7 @@ define internal void @test16_sub(ptr noalias %p, i32 %c1, i32 %c2) {
 ; TUNIT-NEXT:    [[TOBOOL:%.*]] = icmp eq i32 [[C1]], 0
 ; TUNIT-NEXT:    br i1 [[TOBOOL]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
 ; TUNIT:       if.then:
-; TUNIT-NEXT:    tail call void @only_store(ptr noalias nocapture nofree noundef writeonly align 4 [[P]]) #[[ATTR14]]
+; TUNIT-NEXT:    tail call void @only_store(ptr nocapture nofree noundef writeonly align 4 [[P]]) #[[ATTR14]]
 ; TUNIT-NEXT:    tail call void @make_alias(ptr nofree writeonly align 4 [[P]]) #[[ATTR14]]
 ; TUNIT-NEXT:    br label [[IF_END]]
 ; TUNIT:       if.end:


        


More information about the llvm-commits mailing list