[DomTree] Accept Value as Def (NFC)
Non-instruction defs like arguments, constants or global values
always dominate all instructions/uses inside the function. This
case currently needs to be treated separately by the caller, see
https://reviews.llvm.org/D89623#inline-832818 for an example.
This patch makes the dominator tree APIs accept a Value instead of
an Instruction and always returns true for the non-Instruction case.
A complication here is that BasicBlocks are also Values. For that
reason we can't support the dominates(Value *, BasicBlock *)
variant, as it would conflict with dominates(BasicBlock *, BasicBlock *),
which has different semantics. For the other two APIs we assert
that the passed value is not a BasicBlock.
Differential Revision: https://reviews.llvm.org/D89632
diff --git a/llvm/unittests/IR/DominatorTreeTest.cpp b/llvm/unittests/IR/DominatorTreeTest.cpp
index afb620f..21f11c8 100644
--- a/llvm/unittests/IR/DominatorTreeTest.cpp
+++ b/llvm/unittests/IR/DominatorTreeTest.cpp
@@ -1071,3 +1071,32 @@
EXPECT_TRUE(DT->dominates(E23, E23));
});
}
+
+TEST(DominatorTree, ValueDomination) {
+ StringRef ModuleString = R"(
+ @foo = global i8 0
+ define i8 @f(i8 %arg) {
+ ret i8 %arg
+ }
+ )";
+
+ LLVMContext Context;
+ std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
+
+ runWithDomTree(*M, "f",
+ [&](Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
+ Argument *A = F.getArg(0);
+ GlobalValue *G = M->getNamedValue("foo");
+ Constant *C = ConstantInt::getNullValue(Type::getInt8Ty(Context));
+
+ Instruction *I = F.getEntryBlock().getTerminator();
+ EXPECT_TRUE(DT->dominates(A, I));
+ EXPECT_TRUE(DT->dominates(G, I));
+ EXPECT_TRUE(DT->dominates(C, I));
+
+ const Use &U = I->getOperandUse(0);
+ EXPECT_TRUE(DT->dominates(A, U));
+ EXPECT_TRUE(DT->dominates(G, U));
+ EXPECT_TRUE(DT->dominates(C, U));
+ });
+}