[llvm] 4ac9a69 - [NFC] Add API for edge domination check in dom tree
Max Kazantsev via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 17 02:05:31 PDT 2020
Author: Max Kazantsev
Date: 2020-06-17T16:05:05+07:00
New Revision: 4ac9a6902f2c90c5c316748a7594a7c5235cf7ba
URL: https://github.com/llvm/llvm-project/commit/4ac9a6902f2c90c5c316748a7594a7c5235cf7ba
DIFF: https://github.com/llvm/llvm-project/commit/4ac9a6902f2c90c5c316748a7594a7c5235cf7ba.diff
LOG: [NFC] Add API for edge domination check in dom tree
Added:
Modified:
llvm/include/llvm/IR/Dominators.h
llvm/lib/IR/Dominators.cpp
llvm/unittests/IR/DominatorTreeTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/Dominators.h b/llvm/include/llvm/IR/Dominators.h
index 6a14785a6cc3..a79be8779b7e 100644
--- a/llvm/include/llvm/IR/Dominators.h
+++ b/llvm/include/llvm/IR/Dominators.h
@@ -172,6 +172,8 @@ class DominatorTree : public DominatorTreeBase<BasicBlock, false> {
/// never dominate the use.
bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
+ /// Returns true if edge \p BBE1 dominates edge \p BBE2.
+ bool dominates(const BasicBlockEdge &BBE1, const BasicBlockEdge &BBE2) const;
// Ensure base class overloads are visible.
using Base::isReachableFromEntry;
diff --git a/llvm/lib/IR/Dominators.cpp b/llvm/lib/IR/Dominators.cpp
index d4a2026726b8..bb1cc347dcb1 100644
--- a/llvm/lib/IR/Dominators.cpp
+++ b/llvm/lib/IR/Dominators.cpp
@@ -316,6 +316,14 @@ bool DominatorTree::isReachableFromEntry(const Use &U) const {
return isReachableFromEntry(I->getParent());
}
+// Edge BBE1 dominates edge BBE2 if they match or BBE1 dominates start of BBE2.
+bool DominatorTree::dominates(const BasicBlockEdge &BBE1,
+ const BasicBlockEdge &BBE2) const {
+ if (BBE1.getStart() == BBE2.getStart() && BBE1.getEnd() == BBE2.getEnd())
+ return true;
+ return dominates(BBE1, BBE2.getStart());
+}
+
//===----------------------------------------------------------------------===//
// DominatorTreeAnalysis and related pass implementations
//===----------------------------------------------------------------------===//
diff --git a/llvm/unittests/IR/DominatorTreeTest.cpp b/llvm/unittests/IR/DominatorTreeTest.cpp
index 22d2003de351..66e122760ef3 100644
--- a/llvm/unittests/IR/DominatorTreeTest.cpp
+++ b/llvm/unittests/IR/DominatorTreeTest.cpp
@@ -1020,3 +1020,54 @@ TEST(DominatorTree, InsertIntoIrreducible) {
EXPECT_TRUE(DT.verify());
}
+TEST(DominatorTree, EdgeDomination) {
+ StringRef ModuleString = "define i32 @f(i1 %cond) {\n"
+ " bb0:\n"
+ " br i1 %cond, label %bb1, label %bb2\n"
+ " bb1:\n"
+ " br label %bb3\n"
+ " bb2:\n"
+ " br label %bb3\n"
+ " bb3:\n"
+ " ret i32 4"
+ "}\n";
+
+ // Parse the module.
+ LLVMContext Context;
+ std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
+
+ runWithDomTree(*M, "f",
+ [&](Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
+ Function::iterator FI = F.begin();
+
+ BasicBlock *BB0 = &*FI++;
+ BasicBlock *BB1 = &*FI++;
+ BasicBlock *BB2 = &*FI++;
+ BasicBlock *BB3 = &*FI++;
+
+ BasicBlockEdge E01(BB0, BB1);
+ BasicBlockEdge E02(BB0, BB2);
+ BasicBlockEdge E13(BB1, BB3);
+ BasicBlockEdge E23(BB2, BB3);
+
+ EXPECT_TRUE(DT->dominates(E01, E01));
+ EXPECT_FALSE(DT->dominates(E01, E02));
+ EXPECT_TRUE(DT->dominates(E01, E13));
+ EXPECT_FALSE(DT->dominates(E01, E23));
+
+ EXPECT_FALSE(DT->dominates(E02, E01));
+ EXPECT_TRUE(DT->dominates(E02, E02));
+ EXPECT_FALSE(DT->dominates(E02, E13));
+ EXPECT_TRUE(DT->dominates(E02, E23));
+
+ EXPECT_FALSE(DT->dominates(E13, E01));
+ EXPECT_FALSE(DT->dominates(E13, E02));
+ EXPECT_TRUE(DT->dominates(E13, E13));
+ EXPECT_FALSE(DT->dominates(E13, E23));
+
+ EXPECT_FALSE(DT->dominates(E23, E01));
+ EXPECT_FALSE(DT->dominates(E23, E02));
+ EXPECT_FALSE(DT->dominates(E23, E13));
+ EXPECT_TRUE(DT->dominates(E23, E23));
+ });
+}
More information about the llvm-commits
mailing list