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 //===----------------------------------------------------------------------===// 9 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" 17 18 using namespace llvm; 19 20 namespace { 21 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); 32 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); 37 38 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3); 39 40 GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/, 41 GlobalValue::InternalLinkage, 42 0, "switch.bas"); 43 44 45 // Global Initializer 46 std::vector<Constant*> Init; 47 Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB); 48 Init.push_back(SwitchCase1BA); 49 50 Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB); 51 Init.push_back(SwitchCase2BA); 52 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); 57 58 GV->setInitializer(ConstantArray::get(AT, Init)); 59 } 60 61 virtual void TearDown() { 62 M.reset(); 63 } 64 65 OwningPtr<Module> M; 66 Function *F; 67 GlobalVariable *GV; 68 BasicBlock *EntryBB; 69 BasicBlock *SwitchCase1BB; 70 BasicBlock *SwitchCase2BB; 71 BasicBlock *ExitBB; 72 }; 73 74 TEST_F(LinkModuleTest, BlockAddress) { 75 LLVMContext &Ctx = getGlobalContext(); 76 IRBuilder<> Builder(EntryBB); 77 78 std::vector<Value*> GEPIndices; 79 GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0)); 80 GEPIndices.push_back(F->arg_begin()); 81 82 Value *GEP = Builder.CreateGEP(GV, GEPIndices, "switch.gep"); 83 Value *Load = Builder.CreateLoad(GEP, "switch.load"); 84 85 Builder.CreateRet(Load); 86 87 Builder.SetInsertPoint(SwitchCase1BB); 88 Builder.CreateBr(ExitBB); 89 90 Builder.SetInsertPoint(SwitchCase2BB); 91 Builder.CreateBr(ExitBB); 92 93 Builder.SetInsertPoint(ExitBB); 94 Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx))); 95 96 Module *LinkedModule = new Module("MyModuleLinked", getGlobalContext()); 97 Linker::LinkModules(LinkedModule, M.get(), Linker::PreserveSource, 0); 98 99 // Delete the original module. 100 M.reset(); 101 102 // Check that the global "@switch.bas" is well-formed. 103 const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas"); 104 const Constant *Init = LinkedGV->getInitializer(); 105 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*)] 110 111 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3); 112 EXPECT_EQ(AT, Init->getType()); 113 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")); 127 128 delete LinkedModule; 129 } 130 131 TEST_F(LinkModuleTest, EmptyModule) { 132 LLVMContext &Ctx = getGlobalContext(); 133 Module *InternalM = new Module("InternalModule", Ctx); 134 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), 135 Type::getInt8PtrTy(Ctx), 136 false /*=isVarArgs*/); 137 138 F = Function::Create(FTy, Function::InternalLinkage, "bar", InternalM); 139 F->setCallingConv(CallingConv::C); 140 141 BasicBlock *BB = BasicBlock::Create(Ctx, "", F); 142 IRBuilder<> Builder(BB); 143 Builder.CreateRetVoid(); 144 145 StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0)); 146 147 GlobalVariable *GV = 148 new GlobalVariable(*InternalM, STy, false /*=isConstant*/, 149 GlobalValue::InternalLinkage, 0, "g"); 150 151 GV->setInitializer(ConstantStruct::get(STy, F)); 152 153 154 Module *EmptyM = new Module("EmptyModule1", Ctx); 155 Linker::LinkModules(EmptyM, InternalM, Linker::PreserveSource, 0); 156 157 delete EmptyM; 158 EmptyM = new Module("EmptyModule2", Ctx); 159 Linker::LinkModules(InternalM, EmptyM, Linker::PreserveSource, 0); 160 161 delete EmptyM; 162 delete InternalM; 163 } 164 165 } // end anonymous namespace 166