[llvm] 7593a48 - [Dominators] Use Instruction::comesBefore for block-local queries, NFC

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 20 16:42:10 PST 2020


Author: Vedant Kumar
Date: 2020-02-20T16:41:51-08:00
New Revision: 7593a480dbce4e26f7dda4aa8f15bffd03acbfdb

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

LOG: [Dominators] Use Instruction::comesBefore for block-local queries, NFC

Use the lazy instruction ordering facility for block-local dominance
queries.

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

Added: 
    

Modified: 
    llvm/lib/IR/Dominators.cpp
    llvm/unittests/IR/DominatorTreeTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Dominators.cpp b/llvm/lib/IR/Dominators.cpp
index 03dc4da273a3..4062ade645ba 100644
--- a/llvm/lib/IR/Dominators.cpp
+++ b/llvm/lib/IR/Dominators.cpp
@@ -140,12 +140,7 @@ bool DominatorTree::dominates(const Instruction *Def,
   if (DefBB != UseBB)
     return dominates(DefBB, UseBB);
 
-  // Loop through the basic block until we find Def or User.
-  BasicBlock::const_iterator I = DefBB->begin();
-  for (; &*I != Def && &*I != User; ++I)
-    /*empty*/;
-
-  return &*I == Def;
+  return Def->comesBefore(User);
 }
 
 // true if Def would dominate a use in any instruction in UseBB.
@@ -289,12 +284,7 @@ bool DominatorTree::dominates(const Instruction *Def, const Use &U) const {
   if (isa<PHINode>(UserInst))
     return true;
 
-  // Otherwise, just loop through the basic block until we find Def or User.
-  BasicBlock::const_iterator I = DefBB->begin();
-  for (; &*I != Def && &*I != UserInst; ++I)
-    /*empty*/;
-
-  return &*I != UserInst;
+  return Def->comesBefore(UserInst);
 }
 
 bool DominatorTree::isReachableFromEntry(const Use &U) const {

diff  --git a/llvm/unittests/IR/DominatorTreeTest.cpp b/llvm/unittests/IR/DominatorTreeTest.cpp
index 7773856b6bc3..22d2003de351 100644
--- a/llvm/unittests/IR/DominatorTreeTest.cpp
+++ b/llvm/unittests/IR/DominatorTreeTest.cpp
@@ -43,6 +43,37 @@ static std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
   return M;
 }
 
+TEST(DominatorTree, PHIs) {
+  StringRef ModuleString = R"(
+      define void @f() {
+      bb1:
+        br label %bb1
+      bb2:
+        %a = phi i32 [0, %bb1], [1, %bb2]
+        %b = phi i32 [2, %bb1], [%a, %bb2]
+        br label %bb2
+      };
+  )";
+
+  // Parse the module.
+  LLVMContext Context;
+  std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
+
+  runWithDomTree(*M, "f",
+                 [&](Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
+                   auto FI = F.begin();
+                   ++FI;
+                   BasicBlock *BB2 = &*FI;
+                   auto BI = BB2->begin();
+                   Instruction *PhiA = &*BI++;
+                   Instruction *PhiB = &*BI;
+
+                   // Phis are thought to execute "instantly, together".
+                   EXPECT_TRUE(DT->dominates(PhiA, PhiB));
+                   EXPECT_TRUE(DT->dominates(PhiB, PhiA));
+                 });
+}
+
 TEST(DominatorTree, Unreachable) {
   StringRef ModuleString =
       "declare i32 @g()\n"


        


More information about the llvm-commits mailing list