2013-01-23 16:30:10 +08:00
|
|
|
//===- llvm/unittests/IR/DominatorTreeTest.cpp - Constants unit tests -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-12-02 22:08:27 +08:00
|
|
|
#include "llvm/Analysis/PostDominators.h"
|
2014-01-07 20:34:26 +08:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2015-04-15 03:49:26 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2012-03-31 00:46:21 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
void initializeDPassPass(PassRegistry&);
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct DPass : public FunctionPass {
|
|
|
|
static char ID;
|
2015-04-11 10:11:45 +08:00
|
|
|
bool runOnFunction(Function &F) override {
|
2014-01-13 21:07:17 +08:00
|
|
|
DominatorTree *DT =
|
|
|
|
&getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2016-02-26 01:54:07 +08:00
|
|
|
PostDominatorTree *PDT =
|
|
|
|
&getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
|
2012-03-31 00:46:21 +08:00
|
|
|
Function::iterator FI = F.begin();
|
|
|
|
|
2015-10-21 02:30:20 +08:00
|
|
|
BasicBlock *BB0 = &*FI++;
|
2012-03-31 00:46:21 +08:00
|
|
|
BasicBlock::iterator BBI = BB0->begin();
|
2015-10-21 02:30:20 +08:00
|
|
|
Instruction *Y1 = &*BBI++;
|
|
|
|
Instruction *Y2 = &*BBI++;
|
|
|
|
Instruction *Y3 = &*BBI++;
|
2012-03-31 00:46:21 +08:00
|
|
|
|
2015-10-21 02:30:20 +08:00
|
|
|
BasicBlock *BB1 = &*FI++;
|
2012-03-31 00:46:21 +08:00
|
|
|
BBI = BB1->begin();
|
2015-10-21 02:30:20 +08:00
|
|
|
Instruction *Y4 = &*BBI++;
|
2012-03-31 00:46:21 +08:00
|
|
|
|
2015-10-21 02:30:20 +08:00
|
|
|
BasicBlock *BB2 = &*FI++;
|
2012-03-31 00:46:21 +08:00
|
|
|
BBI = BB2->begin();
|
2015-10-21 02:30:20 +08:00
|
|
|
Instruction *Y5 = &*BBI++;
|
2012-03-31 00:46:21 +08:00
|
|
|
|
2015-10-21 02:30:20 +08:00
|
|
|
BasicBlock *BB3 = &*FI++;
|
2012-03-31 00:46:21 +08:00
|
|
|
BBI = BB3->begin();
|
2015-10-21 02:30:20 +08:00
|
|
|
Instruction *Y6 = &*BBI++;
|
|
|
|
Instruction *Y7 = &*BBI++;
|
2012-03-31 00:46:21 +08:00
|
|
|
|
2015-10-21 02:30:20 +08:00
|
|
|
BasicBlock *BB4 = &*FI++;
|
2012-03-31 00:46:21 +08:00
|
|
|
BBI = BB4->begin();
|
2015-10-21 02:30:20 +08:00
|
|
|
Instruction *Y8 = &*BBI++;
|
|
|
|
Instruction *Y9 = &*BBI++;
|
2012-03-31 00:46:21 +08:00
|
|
|
|
|
|
|
// Reachability
|
|
|
|
EXPECT_TRUE(DT->isReachableFromEntry(BB0));
|
|
|
|
EXPECT_TRUE(DT->isReachableFromEntry(BB1));
|
|
|
|
EXPECT_TRUE(DT->isReachableFromEntry(BB2));
|
|
|
|
EXPECT_FALSE(DT->isReachableFromEntry(BB3));
|
|
|
|
EXPECT_TRUE(DT->isReachableFromEntry(BB4));
|
|
|
|
|
|
|
|
// BB dominance
|
|
|
|
EXPECT_TRUE(DT->dominates(BB0, BB0));
|
|
|
|
EXPECT_TRUE(DT->dominates(BB0, BB1));
|
|
|
|
EXPECT_TRUE(DT->dominates(BB0, BB2));
|
|
|
|
EXPECT_TRUE(DT->dominates(BB0, BB3));
|
|
|
|
EXPECT_TRUE(DT->dominates(BB0, BB4));
|
|
|
|
|
|
|
|
EXPECT_FALSE(DT->dominates(BB1, BB0));
|
|
|
|
EXPECT_TRUE(DT->dominates(BB1, BB1));
|
|
|
|
EXPECT_FALSE(DT->dominates(BB1, BB2));
|
|
|
|
EXPECT_TRUE(DT->dominates(BB1, BB3));
|
|
|
|
EXPECT_FALSE(DT->dominates(BB1, BB4));
|
|
|
|
|
|
|
|
EXPECT_FALSE(DT->dominates(BB2, BB0));
|
|
|
|
EXPECT_FALSE(DT->dominates(BB2, BB1));
|
|
|
|
EXPECT_TRUE(DT->dominates(BB2, BB2));
|
|
|
|
EXPECT_TRUE(DT->dominates(BB2, BB3));
|
|
|
|
EXPECT_FALSE(DT->dominates(BB2, BB4));
|
|
|
|
|
|
|
|
EXPECT_FALSE(DT->dominates(BB3, BB0));
|
|
|
|
EXPECT_FALSE(DT->dominates(BB3, BB1));
|
|
|
|
EXPECT_FALSE(DT->dominates(BB3, BB2));
|
|
|
|
EXPECT_TRUE(DT->dominates(BB3, BB3));
|
|
|
|
EXPECT_FALSE(DT->dominates(BB3, BB4));
|
|
|
|
|
|
|
|
// BB proper dominance
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB0, BB0));
|
|
|
|
EXPECT_TRUE(DT->properlyDominates(BB0, BB1));
|
|
|
|
EXPECT_TRUE(DT->properlyDominates(BB0, BB2));
|
|
|
|
EXPECT_TRUE(DT->properlyDominates(BB0, BB3));
|
|
|
|
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB1, BB0));
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB1, BB1));
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB1, BB2));
|
|
|
|
EXPECT_TRUE(DT->properlyDominates(BB1, BB3));
|
|
|
|
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB2, BB0));
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB2, BB1));
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB2, BB2));
|
|
|
|
EXPECT_TRUE(DT->properlyDominates(BB2, BB3));
|
|
|
|
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB3, BB0));
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB3, BB1));
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB3, BB2));
|
|
|
|
EXPECT_FALSE(DT->properlyDominates(BB3, BB3));
|
|
|
|
|
|
|
|
// Instruction dominance in the same reachable BB
|
|
|
|
EXPECT_FALSE(DT->dominates(Y1, Y1));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y1, Y2));
|
|
|
|
EXPECT_FALSE(DT->dominates(Y2, Y1));
|
|
|
|
EXPECT_FALSE(DT->dominates(Y2, Y2));
|
|
|
|
|
|
|
|
// Instruction dominance in the same unreachable BB
|
|
|
|
EXPECT_TRUE(DT->dominates(Y6, Y6));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y6, Y7));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y7, Y6));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y7, Y7));
|
|
|
|
|
|
|
|
// Invoke
|
|
|
|
EXPECT_TRUE(DT->dominates(Y3, Y4));
|
|
|
|
EXPECT_FALSE(DT->dominates(Y3, Y5));
|
|
|
|
|
|
|
|
// Phi
|
|
|
|
EXPECT_TRUE(DT->dominates(Y2, Y9));
|
|
|
|
EXPECT_FALSE(DT->dominates(Y3, Y9));
|
|
|
|
EXPECT_FALSE(DT->dominates(Y8, Y9));
|
|
|
|
|
|
|
|
// Anything dominates unreachable
|
|
|
|
EXPECT_TRUE(DT->dominates(Y1, Y6));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y3, Y6));
|
|
|
|
|
|
|
|
// Unreachable doesn't dominate reachable
|
|
|
|
EXPECT_FALSE(DT->dominates(Y6, Y1));
|
|
|
|
|
|
|
|
// Instruction, BB dominance
|
|
|
|
EXPECT_FALSE(DT->dominates(Y1, BB0));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y1, BB1));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y1, BB2));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y1, BB3));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y1, BB4));
|
|
|
|
|
|
|
|
EXPECT_FALSE(DT->dominates(Y3, BB0));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y3, BB1));
|
|
|
|
EXPECT_FALSE(DT->dominates(Y3, BB2));
|
|
|
|
EXPECT_TRUE(DT->dominates(Y3, BB3));
|
|
|
|
EXPECT_FALSE(DT->dominates(Y3, BB4));
|
|
|
|
|
|
|
|
EXPECT_TRUE(DT->dominates(Y6, BB3));
|
|
|
|
|
2013-12-02 22:08:27 +08:00
|
|
|
// Post dominance.
|
|
|
|
EXPECT_TRUE(PDT->dominates(BB0, BB0));
|
|
|
|
EXPECT_FALSE(PDT->dominates(BB1, BB0));
|
|
|
|
EXPECT_FALSE(PDT->dominates(BB2, BB0));
|
|
|
|
EXPECT_FALSE(PDT->dominates(BB3, BB0));
|
|
|
|
EXPECT_TRUE(PDT->dominates(BB4, BB1));
|
|
|
|
|
|
|
|
// Dominance descendants.
|
|
|
|
SmallVector<BasicBlock *, 8> DominatedBBs, PostDominatedBBs;
|
|
|
|
|
|
|
|
DT->getDescendants(BB0, DominatedBBs);
|
|
|
|
PDT->getDescendants(BB0, PostDominatedBBs);
|
|
|
|
EXPECT_EQ(DominatedBBs.size(), 4UL);
|
|
|
|
EXPECT_EQ(PostDominatedBBs.size(), 1UL);
|
|
|
|
|
|
|
|
// BB3 is unreachable. It should have no dominators nor postdominators.
|
|
|
|
DominatedBBs.clear();
|
|
|
|
PostDominatedBBs.clear();
|
|
|
|
DT->getDescendants(BB3, DominatedBBs);
|
|
|
|
DT->getDescendants(BB3, PostDominatedBBs);
|
|
|
|
EXPECT_EQ(DominatedBBs.size(), 0UL);
|
|
|
|
EXPECT_EQ(PostDominatedBBs.size(), 0UL);
|
|
|
|
|
2015-04-15 03:49:26 +08:00
|
|
|
// Check DFS Numbers before
|
|
|
|
EXPECT_EQ(DT->getNode(BB0)->getDFSNumIn(), 0UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB0)->getDFSNumOut(), 7UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB1)->getDFSNumIn(), 1UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB1)->getDFSNumOut(), 2UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB2)->getDFSNumIn(), 5UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB2)->getDFSNumOut(), 6UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB4)->getDFSNumIn(), 3UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB4)->getDFSNumOut(), 4UL);
|
|
|
|
|
|
|
|
// Reattach block 3 to block 1 and recalculate
|
|
|
|
BB1->getTerminator()->eraseFromParent();
|
|
|
|
BranchInst::Create(BB4, BB3, ConstantInt::getTrue(F.getContext()), BB1);
|
|
|
|
DT->recalculate(F);
|
|
|
|
|
|
|
|
// Check DFS Numbers after
|
|
|
|
EXPECT_EQ(DT->getNode(BB0)->getDFSNumIn(), 0UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB0)->getDFSNumOut(), 9UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB1)->getDFSNumIn(), 1UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB1)->getDFSNumOut(), 4UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB2)->getDFSNumIn(), 7UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB2)->getDFSNumOut(), 8UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB3)->getDFSNumIn(), 2UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB3)->getDFSNumOut(), 3UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB4)->getDFSNumIn(), 5UL);
|
|
|
|
EXPECT_EQ(DT->getNode(BB4)->getDFSNumOut(), 6UL);
|
|
|
|
|
2017-01-10 10:50:47 +08:00
|
|
|
// Change root node
|
|
|
|
DT->verifyDomTree();
|
|
|
|
BasicBlock *NewEntry = BasicBlock::Create(F.getContext(), "new_entry",
|
|
|
|
&F, BB0);
|
|
|
|
BranchInst::Create(BB0, NewEntry);
|
|
|
|
EXPECT_EQ(F.begin()->getName(), NewEntry->getName());
|
|
|
|
EXPECT_TRUE(&F.getEntryBlock() == NewEntry);
|
|
|
|
DT->setNewRoot(NewEntry);
|
|
|
|
DT->verifyDomTree();
|
|
|
|
|
2012-03-31 00:46:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-11 10:11:45 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-13 21:07:17 +08:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2016-02-26 01:54:07 +08:00
|
|
|
AU.addRequired<PostDominatorTreeWrapperPass>();
|
2012-03-31 00:46:21 +08:00
|
|
|
}
|
|
|
|
DPass() : FunctionPass(ID) {
|
|
|
|
initializeDPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
char DPass::ID = 0;
|
|
|
|
|
2016-04-15 05:59:01 +08:00
|
|
|
std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context, DPass *P) {
|
2017-05-21 03:40:24 +08:00
|
|
|
const char *ModuleString =
|
2012-03-31 00:46:21 +08:00
|
|
|
"declare i32 @g()\n" \
|
2015-06-18 04:52:32 +08:00
|
|
|
"define void @f(i32 %x) personality i32 ()* @g {\n" \
|
2012-03-31 00:46:21 +08:00
|
|
|
"bb0:\n" \
|
|
|
|
" %y1 = add i32 %x, 1\n" \
|
|
|
|
" %y2 = add i32 %x, 1\n" \
|
|
|
|
" %y3 = invoke i32 @g() to label %bb1 unwind label %bb2\n" \
|
|
|
|
"bb1:\n" \
|
|
|
|
" %y4 = add i32 %x, 1\n" \
|
|
|
|
" br label %bb4\n" \
|
|
|
|
"bb2:\n" \
|
2015-06-18 04:52:32 +08:00
|
|
|
" %y5 = landingpad i32\n" \
|
2012-03-31 00:46:21 +08:00
|
|
|
" cleanup\n" \
|
|
|
|
" br label %bb4\n" \
|
|
|
|
"bb3:\n" \
|
|
|
|
" %y6 = add i32 %x, 1\n" \
|
|
|
|
" %y7 = add i32 %x, 1\n" \
|
|
|
|
" ret void\n" \
|
|
|
|
"bb4:\n" \
|
|
|
|
" %y8 = phi i32 [0, %bb2], [%y4, %bb1]\n"
|
|
|
|
" %y9 = phi i32 [0, %bb2], [%y4, %bb1]\n"
|
|
|
|
" ret void\n" \
|
|
|
|
"}\n";
|
|
|
|
SMDiagnostic Err;
|
2017-05-21 03:40:24 +08:00
|
|
|
return parseAssemblyString(ModuleString, Err, Context);
|
2012-03-31 00:46:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DominatorTree, Unreachable) {
|
|
|
|
DPass *P = new DPass();
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext Context;
|
|
|
|
std::unique_ptr<Module> M = makeLLVMModule(Context, P);
|
2015-02-13 18:01:29 +08:00
|
|
|
legacy::PassManager Passes;
|
2012-03-31 00:46:21 +08:00
|
|
|
Passes.add(P);
|
|
|
|
Passes.run(*M);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(DPass, "dpass", "dpass", false, false)
|
2014-01-13 21:07:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2016-02-26 01:54:07 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
|
2012-03-31 00:46:21 +08:00
|
|
|
INITIALIZE_PASS_END(DPass, "dpass", "dpass", false, false)
|