Chandler Carruth | c779e96 | 2013-01-07 15:35:46 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/IRBuilderTest.cpp - IRBuilder tests ---------------===// |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 10 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 5a88dda | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/OwningPtr.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 12 | #include "llvm/IR/BasicBlock.h" |
| 13 | #include "llvm/IR/DataLayout.h" |
| 14 | #include "llvm/IR/Function.h" |
| 15 | #include "llvm/IR/IntrinsicInst.h" |
| 16 | #include "llvm/IR/LLVMContext.h" |
| 17 | #include "llvm/IR/MDBuilder.h" |
| 18 | #include "llvm/IR/Module.h" |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 23 | namespace { |
Chandler Carruth | 1880e2d | 2012-06-20 08:39:27 +0000 | [diff] [blame] | 24 | |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 25 | class IRBuilderTest : public testing::Test { |
| 26 | protected: |
| 27 | virtual void SetUp() { |
| 28 | M.reset(new Module("MyModule", getGlobalContext())); |
| 29 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(getGlobalContext()), |
| 30 | /*isVarArg=*/false); |
Chandler Carruth | 4b31c4d | 2012-07-16 07:44:51 +0000 | [diff] [blame] | 31 | F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 32 | BB = BasicBlock::Create(getGlobalContext(), "", F); |
NAKAMURA Takumi | f25b003 | 2013-01-23 08:30:39 +0000 | [diff] [blame] | 33 | GV = new GlobalVariable(*M, Type::getFloatTy(getGlobalContext()), true, |
| 34 | GlobalValue::ExternalLinkage, 0); |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | virtual void TearDown() { |
| 38 | BB = 0; |
| 39 | M.reset(); |
| 40 | } |
| 41 | |
| 42 | OwningPtr<Module> M; |
Chandler Carruth | 4b31c4d | 2012-07-16 07:44:51 +0000 | [diff] [blame] | 43 | Function *F; |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 44 | BasicBlock *BB; |
Michael Ilseman | f564e9f | 2012-11-28 21:17:34 +0000 | [diff] [blame] | 45 | GlobalVariable *GV; |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 46 | }; |
Nick Lewycky | 0cf5156 | 2011-05-21 23:14:36 +0000 | [diff] [blame] | 47 | |
| 48 | TEST_F(IRBuilderTest, Lifetime) { |
| 49 | IRBuilder<> Builder(BB); |
| 50 | AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty()); |
| 51 | AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty()); |
| 52 | AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(), |
| 53 | Builder.getInt32(123)); |
| 54 | |
| 55 | CallInst *Start1 = Builder.CreateLifetimeStart(Var1); |
| 56 | CallInst *Start2 = Builder.CreateLifetimeStart(Var2); |
| 57 | CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100)); |
| 58 | |
| 59 | EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1)); |
| 60 | EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1)); |
| 61 | EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100)); |
| 62 | |
| 63 | EXPECT_EQ(Start1->getArgOperand(1), Var1); |
| 64 | EXPECT_NE(Start2->getArgOperand(1), Var2); |
| 65 | EXPECT_EQ(Start3->getArgOperand(1), Var3); |
| 66 | |
| 67 | Value *End1 = Builder.CreateLifetimeEnd(Var1); |
| 68 | Builder.CreateLifetimeEnd(Var2); |
| 69 | Builder.CreateLifetimeEnd(Var3); |
| 70 | |
| 71 | IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1); |
| 72 | IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1); |
| 73 | ASSERT_TRUE(II_Start1 != NULL); |
| 74 | EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start); |
| 75 | ASSERT_TRUE(II_End1 != NULL); |
| 76 | EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end); |
| 77 | } |
Chandler Carruth | 1880e2d | 2012-06-20 08:39:27 +0000 | [diff] [blame] | 78 | |
Chandler Carruth | 4b31c4d | 2012-07-16 07:44:51 +0000 | [diff] [blame] | 79 | TEST_F(IRBuilderTest, CreateCondBr) { |
| 80 | IRBuilder<> Builder(BB); |
| 81 | BasicBlock *TBB = BasicBlock::Create(getGlobalContext(), "", F); |
| 82 | BasicBlock *FBB = BasicBlock::Create(getGlobalContext(), "", F); |
| 83 | |
| 84 | BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB); |
| 85 | TerminatorInst *TI = BB->getTerminator(); |
| 86 | EXPECT_EQ(BI, TI); |
| 87 | EXPECT_EQ(2u, TI->getNumSuccessors()); |
| 88 | EXPECT_EQ(TBB, TI->getSuccessor(0)); |
| 89 | EXPECT_EQ(FBB, TI->getSuccessor(1)); |
Chandler Carruth | 2994937 | 2012-07-16 07:45:06 +0000 | [diff] [blame] | 90 | |
| 91 | BI->eraseFromParent(); |
| 92 | MDNode *Weights = MDBuilder(getGlobalContext()).createBranchWeights(42, 13); |
| 93 | BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights); |
| 94 | TI = BB->getTerminator(); |
| 95 | EXPECT_EQ(BI, TI); |
| 96 | EXPECT_EQ(2u, TI->getNumSuccessors()); |
| 97 | EXPECT_EQ(TBB, TI->getSuccessor(0)); |
| 98 | EXPECT_EQ(FBB, TI->getSuccessor(1)); |
| 99 | EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof)); |
Chandler Carruth | 4b31c4d | 2012-07-16 07:44:51 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Duncan Sands | 6e591ea | 2012-12-21 12:03:03 +0000 | [diff] [blame] | 102 | TEST_F(IRBuilderTest, LandingPadName) { |
| 103 | IRBuilder<> Builder(BB); |
| 104 | LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), |
| 105 | Builder.getInt32(0), 0, "LP"); |
| 106 | EXPECT_EQ(LP->getName(), "LP"); |
| 107 | } |
| 108 | |
Evgeniy Stepanov | 0ceb855 | 2012-10-31 09:50:01 +0000 | [diff] [blame] | 109 | TEST_F(IRBuilderTest, GetIntTy) { |
| 110 | IRBuilder<> Builder(BB); |
| 111 | IntegerType *Ty1 = Builder.getInt1Ty(); |
| 112 | EXPECT_EQ(Ty1, IntegerType::get(getGlobalContext(), 1)); |
| 113 | |
| 114 | DataLayout* DL = new DataLayout(M.get()); |
| 115 | IntegerType *IntPtrTy = Builder.getIntPtrTy(DL); |
| 116 | unsigned IntPtrBitSize = DL->getPointerSizeInBits(0); |
| 117 | EXPECT_EQ(IntPtrTy, IntegerType::get(getGlobalContext(), IntPtrBitSize)); |
NAKAMURA Takumi | e82a5fa | 2013-01-23 08:31:28 +0000 | [diff] [blame] | 118 | delete DL; |
Evgeniy Stepanov | 0ceb855 | 2012-10-31 09:50:01 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Michael Ilseman | f564e9f | 2012-11-28 21:17:34 +0000 | [diff] [blame] | 121 | TEST_F(IRBuilderTest, FastMathFlags) { |
| 122 | IRBuilder<> Builder(BB); |
| 123 | Value *F; |
| 124 | Instruction *FDiv, *FAdd; |
| 125 | |
| 126 | F = Builder.CreateLoad(GV); |
| 127 | F = Builder.CreateFAdd(F, F); |
| 128 | |
Michael Ilseman | 9eec659 | 2012-11-28 21:19:52 +0000 | [diff] [blame] | 129 | EXPECT_FALSE(Builder.getFastMathFlags().any()); |
| 130 | ASSERT_TRUE(isa<Instruction>(F)); |
| 131 | FAdd = cast<Instruction>(F); |
| 132 | EXPECT_FALSE(FAdd->hasNoNaNs()); |
| 133 | |
| 134 | FastMathFlags FMF; |
| 135 | Builder.SetFastMathFlags(FMF); |
| 136 | |
| 137 | F = Builder.CreateFAdd(F, F); |
| 138 | EXPECT_FALSE(Builder.getFastMathFlags().any()); |
| 139 | |
Michael Ilseman | 1638b83 | 2012-12-09 21:12:04 +0000 | [diff] [blame] | 140 | FMF.setUnsafeAlgebra(); |
Michael Ilseman | 9eec659 | 2012-11-28 21:19:52 +0000 | [diff] [blame] | 141 | Builder.SetFastMathFlags(FMF); |
| 142 | |
| 143 | F = Builder.CreateFAdd(F, F); |
| 144 | EXPECT_TRUE(Builder.getFastMathFlags().any()); |
| 145 | ASSERT_TRUE(isa<Instruction>(F)); |
| 146 | FAdd = cast<Instruction>(F); |
| 147 | EXPECT_TRUE(FAdd->hasNoNaNs()); |
| 148 | |
| 149 | F = Builder.CreateFDiv(F, F); |
| 150 | EXPECT_TRUE(Builder.getFastMathFlags().any()); |
| 151 | EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra); |
| 152 | ASSERT_TRUE(isa<Instruction>(F)); |
| 153 | FDiv = cast<Instruction>(F); |
| 154 | EXPECT_TRUE(FDiv->hasAllowReciprocal()); |
| 155 | |
| 156 | Builder.clearFastMathFlags(); |
| 157 | |
| 158 | F = Builder.CreateFDiv(F, F); |
| 159 | ASSERT_TRUE(isa<Instruction>(F)); |
| 160 | FDiv = cast<Instruction>(F); |
| 161 | EXPECT_FALSE(FDiv->hasAllowReciprocal()); |
| 162 | |
| 163 | FMF.clear(); |
Michael Ilseman | 1638b83 | 2012-12-09 21:12:04 +0000 | [diff] [blame] | 164 | FMF.setAllowReciprocal(); |
Michael Ilseman | 9eec659 | 2012-11-28 21:19:52 +0000 | [diff] [blame] | 165 | Builder.SetFastMathFlags(FMF); |
| 166 | |
| 167 | F = Builder.CreateFDiv(F, F); |
| 168 | EXPECT_TRUE(Builder.getFastMathFlags().any()); |
| 169 | EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal); |
| 170 | ASSERT_TRUE(isa<Instruction>(F)); |
| 171 | FDiv = cast<Instruction>(F); |
| 172 | EXPECT_TRUE(FDiv->hasAllowReciprocal()); |
| 173 | |
Michael Ilseman | 4b896dd | 2012-11-29 21:25:12 +0000 | [diff] [blame] | 174 | Builder.clearFastMathFlags(); |
| 175 | |
| 176 | F = Builder.CreateFDiv(F, F); |
| 177 | ASSERT_TRUE(isa<Instruction>(F)); |
| 178 | FDiv = cast<Instruction>(F); |
| 179 | EXPECT_FALSE(FDiv->getFastMathFlags().any()); |
| 180 | FDiv->copyFastMathFlags(FAdd); |
| 181 | EXPECT_TRUE(FDiv->hasNoNaNs()); |
| 182 | |
Michael Ilseman | 9eec659 | 2012-11-28 21:19:52 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chandler Carruth | 1880e2d | 2012-06-20 08:39:27 +0000 | [diff] [blame] | 185 | } |