]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/blob - unittests/Linker/LinkModulesTest.cpp
Revert "Run clang-format on file."
[opencl/llvm.git] / unittests / Linker / LinkModulesTest.cpp
1 //===- llvm/unittest/Linker/LinkModulesTest.cpp - IRBuilder 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 //===----------------------------------------------------------------------===//
10 #include "llvm/Linker.h"
11 #include "llvm/IR/IRBuilder.h"
12 #include "llvm/IR/BasicBlock.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Module.h"
16 #include "gtest/gtest.h"
18 using namespace llvm;
20 namespace {
22 class LinkModuleTest : public testing::Test {
23 protected:
24   virtual void SetUp() {
25     LLVMContext &Ctx = getGlobalContext();
26     M.reset(new Module("MyModule", Ctx));
27     FunctionType *FTy = FunctionType::get(Type::getInt8PtrTy(Ctx),
28                                           Type::getInt32Ty(Ctx),
29                                           false /*=isVarArg*/);
30     F = Function::Create(FTy, Function::ExternalLinkage, "ba_func", M.get());
31     F->setCallingConv(CallingConv::C);
33     EntryBB = BasicBlock::Create(Ctx, "entry", F);
34     SwitchCase1BB = BasicBlock::Create(Ctx, "switch.case.1", F);
35     SwitchCase2BB = BasicBlock::Create(Ctx, "switch.case.2", F);
36     ExitBB = BasicBlock::Create(Ctx, "exit", F);
38     ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
40     GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/,
41                             GlobalValue::InternalLinkage,
42                             0, "switch.bas");
45     // Global Initializer
46     std::vector<Constant*> Init;
47     Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB);
48     Init.push_back(SwitchCase1BA);
50     Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB);
51     Init.push_back(SwitchCase2BA);
53     ConstantInt *One = ConstantInt::get(Type::getInt32Ty(Ctx), 1);
54     Constant *OnePtr = ConstantExpr::getCast(Instruction::IntToPtr,
55                                              One, Type::getInt8PtrTy(Ctx));
56     Init.push_back(OnePtr);
58     GV->setInitializer(ConstantArray::get(AT, Init));
59   }
61   virtual void TearDown() {
62     M.reset();
63   }
65   OwningPtr<Module> M;
66   Function *F;
67   GlobalVariable *GV;
68   BasicBlock *EntryBB;
69   BasicBlock *SwitchCase1BB;
70   BasicBlock *SwitchCase2BB;
71   BasicBlock *ExitBB;
72 };
74 TEST_F(LinkModuleTest, BlockAddress) {
75   LLVMContext &Ctx = getGlobalContext();
76   IRBuilder<> Builder(EntryBB);
78   std::vector<Value*> GEPIndices;
79   GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
80   GEPIndices.push_back(F->arg_begin());
82   Value *GEP = Builder.CreateGEP(GV, GEPIndices, "switch.gep");
83   Value *Load = Builder.CreateLoad(GEP, "switch.load");
85   Builder.CreateRet(Load);
87   Builder.SetInsertPoint(SwitchCase1BB);
88   Builder.CreateBr(ExitBB);
90   Builder.SetInsertPoint(SwitchCase2BB);
91   Builder.CreateBr(ExitBB);
93   Builder.SetInsertPoint(ExitBB);
94   Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
96   Module *LinkedModule = new Module("MyModuleLinked", getGlobalContext());
97   Linker::LinkModules(LinkedModule, M.get(), Linker::PreserveSource, 0);
99   // Delete the original module.
100   M.reset();
102   // Check that the global "@switch.bas" is well-formed.
103   const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
104   const Constant *Init = LinkedGV->getInitializer();
106   // @switch.bas = internal global [3 x i8*]
107   //   [i8* blockaddress(@ba_func, %switch.case.1),
108   //    i8* blockaddress(@ba_func, %switch.case.2),
109   //    i8* inttoptr (i32 1 to i8*)]
111   ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
112   EXPECT_EQ(AT, Init->getType());
114   Value *Elem = Init->getOperand(0);
115   ASSERT_TRUE(isa<BlockAddress>(Elem));
116   EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
117             LinkedModule->getFunction("ba_func"));
118   EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
119             LinkedModule->getFunction("ba_func"));
120   
121   Elem = Init->getOperand(1);
122   ASSERT_TRUE(isa<BlockAddress>(Elem));
123   EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
124             LinkedModule->getFunction("ba_func"));
125   EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
126             LinkedModule->getFunction("ba_func"));
128   delete LinkedModule;
131 } // end anonymous namespace