1 //===- OrderedBasicBlockTest.cpp - OrderedBasicBlock unit tests -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Analysis/OrderedBasicBlock.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/BasicBlock.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "gtest/gtest.h"
19
20 namespace llvm {
21 namespace {
22
23 class OrderedBasicBlockTest : public testing::Test {
24 protected:
25 LLVMContext C;
26
makeLLVMModule()27 std::unique_ptr<Module> makeLLVMModule() {
28 const char *ModuleString = R"(define i32 @f(i32 %x) {
29 %add = add i32 %x, 42
30 ret i32 %add
31 })";
32 SMDiagnostic Err;
33 auto foo = parseAssemblyString(ModuleString, Err, C);
34 return foo;
35 }
36 };
37
TEST_F(OrderedBasicBlockTest,Basic)38 TEST_F(OrderedBasicBlockTest, Basic) {
39 auto M = makeLLVMModule();
40 Function *F = M->getFunction("f");
41 BasicBlock::iterator I = F->front().begin();
42 Instruction *Add = &*I++;
43 Instruction *Ret = &*I++;
44
45 OrderedBasicBlock OBB(&F->front());
46 // Intentionally duplicated to verify cached and uncached are the same.
47 EXPECT_FALSE(OBB.dominates(Add, Add));
48 EXPECT_FALSE(OBB.dominates(Add, Add));
49 EXPECT_TRUE(OBB.dominates(Add, Ret));
50 EXPECT_TRUE(OBB.dominates(Add, Ret));
51 EXPECT_FALSE(OBB.dominates(Ret, Add));
52 EXPECT_FALSE(OBB.dominates(Ret, Add));
53 EXPECT_FALSE(OBB.dominates(Ret, Ret));
54 EXPECT_FALSE(OBB.dominates(Ret, Ret));
55 }
56
57 } // end anonymous namespace
58 } // end namespace llvm
59