blob: fecc4a4fe6b4121500fc5b361c392e9431e6a010 [file] [log] [blame]
Chandler Carruthc779e962013-01-07 15:35:46 +00001//===- llvm/unittest/IR/IRBuilderTest.cpp - IRBuilder tests ---------------===//
Nick Lewycky0cf51562011-05-21 23:14:36 +00002//
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 Carruth0b8c9a82013-01-02 11:36:10 +000010#include "llvm/IR/IRBuilder.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000011#include "llvm/ADT/OwningPtr.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000012#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 Lewycky0cf51562011-05-21 23:14:36 +000019#include "gtest/gtest.h"
20
21using namespace llvm;
22
David Blaikie2d24e2a2011-12-20 02:50:00 +000023namespace {
Chandler Carruth1880e2d2012-06-20 08:39:27 +000024
Nick Lewycky0cf51562011-05-21 23:14:36 +000025class IRBuilderTest : public testing::Test {
26protected:
27 virtual void SetUp() {
28 M.reset(new Module("MyModule", getGlobalContext()));
29 FunctionType *FTy = FunctionType::get(Type::getVoidTy(getGlobalContext()),
30 /*isVarArg=*/false);
Chandler Carruth4b31c4d2012-07-16 07:44:51 +000031 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
Nick Lewycky0cf51562011-05-21 23:14:36 +000032 BB = BasicBlock::Create(getGlobalContext(), "", F);
NAKAMURA Takumif25b0032013-01-23 08:30:39 +000033 GV = new GlobalVariable(*M, Type::getFloatTy(getGlobalContext()), true,
34 GlobalValue::ExternalLinkage, 0);
Nick Lewycky0cf51562011-05-21 23:14:36 +000035 }
36
37 virtual void TearDown() {
38 BB = 0;
39 M.reset();
40 }
41
42 OwningPtr<Module> M;
Chandler Carruth4b31c4d2012-07-16 07:44:51 +000043 Function *F;
Nick Lewycky0cf51562011-05-21 23:14:36 +000044 BasicBlock *BB;
Michael Ilsemanf564e9f2012-11-28 21:17:34 +000045 GlobalVariable *GV;
Nick Lewycky0cf51562011-05-21 23:14:36 +000046};
Nick Lewycky0cf51562011-05-21 23:14:36 +000047
48TEST_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 Carruth1880e2d2012-06-20 08:39:27 +000078
Chandler Carruth4b31c4d2012-07-16 07:44:51 +000079TEST_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 Carruth29949372012-07-16 07:45:06 +000090
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 Carruth4b31c4d2012-07-16 07:44:51 +0000100}
101
Duncan Sands6e591ea2012-12-21 12:03:03 +0000102TEST_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 Stepanov0ceb8552012-10-31 09:50:01 +0000109TEST_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 Takumie82a5fa2013-01-23 08:31:28 +0000118 delete DL;
Evgeniy Stepanov0ceb8552012-10-31 09:50:01 +0000119}
120
Michael Ilsemanf564e9f2012-11-28 21:17:34 +0000121TEST_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 Ilseman9eec6592012-11-28 21:19:52 +0000129 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 Ilseman1638b832012-12-09 21:12:04 +0000140 FMF.setUnsafeAlgebra();
Michael Ilseman9eec6592012-11-28 21:19:52 +0000141 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 Ilseman1638b832012-12-09 21:12:04 +0000164 FMF.setAllowReciprocal();
Michael Ilseman9eec6592012-11-28 21:19:52 +0000165 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 Ilseman4b896dd2012-11-29 21:25:12 +0000174 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 Ilseman9eec6592012-11-28 21:19:52 +0000183}
184
Chandler Carruth1880e2d2012-06-20 08:39:27 +0000185}