1 //===- OrderedInstructions.cpp - Unit tests for OrderedInstructions ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/OrderedInstructions.h"
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/Dominators.h"
13 #include "llvm/IR/IRBuilder.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "gtest/gtest.h"
21 /// Check intra-basicblock and inter-basicblock dominance using
22 /// OrderedInstruction.
23 TEST(OrderedInstructionsTest
, DominanceTest
) {
25 Module
M("test", Ctx
);
28 FunctionType::get(Type::getVoidTy(Ctx
), {B
.getInt8PtrTy()}, false);
29 Function
*F
= cast
<Function
>(M
.getOrInsertFunction("f", FTy
));
31 // Create the function as follow and check for dominance relation.
41 // More specifically, check for loadx -> (dominates) loady,
42 // loady -> loadx and loady -> loadz.
44 // Create BBX with 2 loads.
45 BasicBlock
*BBX
= BasicBlock::Create(Ctx
, "bbx", F
);
46 B
.SetInsertPoint(BBX
);
47 Argument
*PointerArg
= &*F
->arg_begin();
48 LoadInst
*LoadInstX
= B
.CreateLoad(PointerArg
);
49 LoadInst
*LoadInstY
= B
.CreateLoad(PointerArg
);
51 // Create BBY with 1 load.
52 BasicBlock
*BBY
= BasicBlock::Create(Ctx
, "bby", F
);
53 B
.SetInsertPoint(BBY
);
54 LoadInst
*LoadInstZ
= B
.CreateLoad(PointerArg
);
55 B
.CreateRet(LoadInstZ
);
56 std::unique_ptr
<DominatorTree
> DT(new DominatorTree(*F
));
57 OrderedInstructions
OI(&*DT
);
59 // Intra-BB dominance test.
60 EXPECT_TRUE(OI
.dominates(LoadInstX
, LoadInstY
));
61 EXPECT_FALSE(OI
.dominates(LoadInstY
, LoadInstX
));
63 // Inter-BB dominance test.
64 EXPECT_TRUE(OI
.dominates(LoadInstY
, LoadInstZ
));