• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- OrderedInstructions.cpp - Unit tests for OrderedInstructions  ------===//
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/Transforms/Utils/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"
18 
19 using namespace llvm;
20 
21 /// Check intra-basicblock and inter-basicblock dominance using
22 /// OrderedInstruction.
TEST(OrderedInstructionsTest,DominanceTest)23 TEST(OrderedInstructionsTest, DominanceTest) {
24   LLVMContext Ctx;
25   Module M("test", Ctx);
26   IRBuilder<> B(Ctx);
27   FunctionType *FTy =
28       FunctionType::get(Type::getVoidTy(Ctx), {B.getInt8PtrTy()}, false);
29   Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
30 
31   // Create the function as follow and check for dominance relation.
32   //
33   // test():
34   //  bbx:
35   //    loadx;
36   //    loady;
37   //  bby:
38   //    loadz;
39   //    return;
40   //
41   // More specifically, check for loadx -> (dominates) loady,
42   // loady -> loadx and loady -> loadz.
43   //
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);
50 
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);
58 
59   // Intra-BB dominance test.
60   EXPECT_TRUE(OI.dominates(LoadInstX, LoadInstY));
61   EXPECT_FALSE(OI.dominates(LoadInstY, LoadInstX));
62 
63   // Inter-BB dominance test.
64   EXPECT_TRUE(OI.dominates(LoadInstY, LoadInstZ));
65 }
66