blob: 8f4f1945d50cb3b064ba14b851a3ff680e892e8d [file] [log] [blame]
Chandler Carruthc779e962013-01-07 15:35:46 +00001//===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
Gabor Greif5a7069a2010-03-16 09:55:46 +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/Instructions.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000011#include "llvm/ADT/STLExtras.h"
12#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000013#include "llvm/IR/BasicBlock.h"
14#include "llvm/IR/Constants.h"
15#include "llvm/IR/DataLayout.h"
16#include "llvm/IR/DerivedTypes.h"
17#include "llvm/IR/IRBuilder.h"
18#include "llvm/IR/LLVMContext.h"
19#include "llvm/IR/MDBuilder.h"
20#include "llvm/IR/Operator.h"
Gabor Greif5a7069a2010-03-16 09:55:46 +000021#include "gtest/gtest.h"
22
23namespace llvm {
24namespace {
25
Gabor Greif138acfe2010-03-16 10:59:48 +000026TEST(InstructionsTest, ReturnInst) {
Gabor Greif5a7069a2010-03-16 09:55:46 +000027 LLVMContext &C(getGlobalContext());
28
Gabor Greif138acfe2010-03-16 10:59:48 +000029 // test for PR6589
Gabor Greif5a7069a2010-03-16 09:55:46 +000030 const ReturnInst* r0 = ReturnInst::Create(C);
Gabor Greifd165e1a2010-03-16 15:26:09 +000031 EXPECT_EQ(r0->getNumOperands(), 0U);
Gabor Greif138acfe2010-03-16 10:59:48 +000032 EXPECT_EQ(r0->op_begin(), r0->op_end());
Gabor Greif22385eb2010-03-16 11:24:53 +000033
Chris Lattnerdb125cf2011-07-18 04:54:35 +000034 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greif22385eb2010-03-16 11:24:53 +000035 Constant* One = ConstantInt::get(Int1, 1, true);
36 const ReturnInst* r1 = ReturnInst::Create(C, One);
John McCallf5ec9b52011-08-27 19:23:22 +000037 EXPECT_EQ(1U, r1->getNumOperands());
Gabor Greif22385eb2010-03-16 11:24:53 +000038 User::const_op_iterator b(r1->op_begin());
John McCallf5ec9b52011-08-27 19:23:22 +000039 EXPECT_NE(r1->op_end(), b);
40 EXPECT_EQ(One, *b);
41 EXPECT_EQ(One, r1->getOperand(0));
Gabor Greif22385eb2010-03-16 11:24:53 +000042 ++b;
John McCallf5ec9b52011-08-27 19:23:22 +000043 EXPECT_EQ(r1->op_end(), b);
Gabor Greif642c0662010-03-16 12:32:03 +000044
45 // clean up
46 delete r0;
47 delete r1;
Gabor Greif5a7069a2010-03-16 09:55:46 +000048}
49
Gabor Greifd165e1a2010-03-16 15:26:09 +000050TEST(InstructionsTest, BranchInst) {
51 LLVMContext &C(getGlobalContext());
52
53 // Make a BasicBlocks
54 BasicBlock* bb0 = BasicBlock::Create(C);
55 BasicBlock* bb1 = BasicBlock::Create(C);
56
57 // Mandatory BranchInst
58 const BranchInst* b0 = BranchInst::Create(bb0);
59
Gabor Greifabf657f2010-03-16 15:53:58 +000060 EXPECT_TRUE(b0->isUnconditional());
61 EXPECT_FALSE(b0->isConditional());
John McCallf5ec9b52011-08-27 19:23:22 +000062 EXPECT_EQ(1U, b0->getNumSuccessors());
Gabor Greifabf657f2010-03-16 15:53:58 +000063
Gabor Greifd165e1a2010-03-16 15:26:09 +000064 // check num operands
John McCallf5ec9b52011-08-27 19:23:22 +000065 EXPECT_EQ(1U, b0->getNumOperands());
Gabor Greifd165e1a2010-03-16 15:26:09 +000066
67 EXPECT_NE(b0->op_begin(), b0->op_end());
John McCallf5ec9b52011-08-27 19:23:22 +000068 EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
Gabor Greifabf657f2010-03-16 15:53:58 +000069
John McCallf5ec9b52011-08-27 19:23:22 +000070 EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
Gabor Greifd165e1a2010-03-16 15:26:09 +000071
Chris Lattnerdb125cf2011-07-18 04:54:35 +000072 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greifd165e1a2010-03-16 15:26:09 +000073 Constant* One = ConstantInt::get(Int1, 1, true);
74
75 // Conditional BranchInst
76 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
77
Gabor Greifabf657f2010-03-16 15:53:58 +000078 EXPECT_FALSE(b1->isUnconditional());
79 EXPECT_TRUE(b1->isConditional());
John McCallf5ec9b52011-08-27 19:23:22 +000080 EXPECT_EQ(2U, b1->getNumSuccessors());
Gabor Greifabf657f2010-03-16 15:53:58 +000081
Gabor Greifd165e1a2010-03-16 15:26:09 +000082 // check num operands
John McCallf5ec9b52011-08-27 19:23:22 +000083 EXPECT_EQ(3U, b1->getNumOperands());
Gabor Greifd165e1a2010-03-16 15:26:09 +000084
85 User::const_op_iterator b(b1->op_begin());
86
87 // check COND
88 EXPECT_NE(b, b1->op_end());
John McCallf5ec9b52011-08-27 19:23:22 +000089 EXPECT_EQ(One, *b);
90 EXPECT_EQ(One, b1->getOperand(0));
91 EXPECT_EQ(One, b1->getCondition());
Gabor Greifd165e1a2010-03-16 15:26:09 +000092 ++b;
93
94 // check ELSE
John McCallf5ec9b52011-08-27 19:23:22 +000095 EXPECT_EQ(bb1, *b);
96 EXPECT_EQ(bb1, b1->getOperand(1));
97 EXPECT_EQ(bb1, b1->getSuccessor(1));
Gabor Greifd165e1a2010-03-16 15:26:09 +000098 ++b;
99
100 // check THEN
John McCallf5ec9b52011-08-27 19:23:22 +0000101 EXPECT_EQ(bb0, *b);
102 EXPECT_EQ(bb0, b1->getOperand(2));
103 EXPECT_EQ(bb0, b1->getSuccessor(0));
Gabor Greifd165e1a2010-03-16 15:26:09 +0000104 ++b;
105
John McCallf5ec9b52011-08-27 19:23:22 +0000106 EXPECT_EQ(b1->op_end(), b);
Gabor Greifd165e1a2010-03-16 15:26:09 +0000107
Gabor Greifd165e1a2010-03-16 15:26:09 +0000108 // clean up
109 delete b0;
110 delete b1;
111
112 delete bb0;
113 delete bb1;
114}
115
Duncan Sands60794652011-04-01 03:34:54 +0000116TEST(InstructionsTest, CastInst) {
117 LLVMContext &C(getGlobalContext());
118
Matt Arsenaultf34dc422013-07-30 20:45:05 +0000119 Type *Int8Ty = Type::getInt8Ty(C);
120 Type *Int16Ty = Type::getInt16Ty(C);
121 Type *Int32Ty = Type::getInt32Ty(C);
122 Type *Int64Ty = Type::getInt64Ty(C);
123 Type *V8x8Ty = VectorType::get(Int8Ty, 8);
124 Type *V8x64Ty = VectorType::get(Int64Ty, 8);
125 Type *X86MMXTy = Type::getX86_MMXTy(C);
126
127 Type *HalfTy = Type::getHalfTy(C);
128 Type *FloatTy = Type::getFloatTy(C);
129 Type *DoubleTy = Type::getDoubleTy(C);
130
131 Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
132 Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
133 Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
134
135 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
136 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
137
138 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
139 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
140
141 Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
142 Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
143 Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
144 Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
145
146 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
147 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
Duncan Sands60794652011-04-01 03:34:54 +0000148
Duncan Sands117feba2011-05-18 07:13:41 +0000149 const Constant* c8 = Constant::getNullValue(V8x8Ty);
150 const Constant* c64 = Constant::getNullValue(V8x64Ty);
151
John McCallf5ec9b52011-08-27 19:23:22 +0000152 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
153 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
Matt Arsenaultf34dc422013-07-30 20:45:05 +0000154
155 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
156 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
157 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
158 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
159 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
160
161 // Check address space casts are rejected since we don't know the sizes here
162 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
163 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
164 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
165 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
166 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
167
168 // Test mismatched number of elements for pointers
169 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
170 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
171 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
172 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
173 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
174
175 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
176 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
177 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
178 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
179 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
180 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
181 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
182 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
183 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
184
185 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
186 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
187 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
188
189 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
190 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
191 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
192 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
193 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
194 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
Duncan Sands60794652011-04-01 03:34:54 +0000195}
196
Nadav Rotem16087692011-12-05 06:29:09 +0000197TEST(InstructionsTest, VectorGep) {
198 LLVMContext &C(getGlobalContext());
199
200 // Type Definitions
201 PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
Matt Arsenault76bf61f2013-06-28 23:24:10 +0000202 PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 32), 0);
Nadav Rotem16087692011-12-05 06:29:09 +0000203
204 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
205 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
206
207 // Test different aspects of the vector-of-pointers type
208 // and GEPs which use this type.
209 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
210 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
211 std::vector<Constant*> ConstVa(2, Ci32a);
212 std::vector<Constant*> ConstVb(2, Ci32b);
213 Constant *C2xi32a = ConstantVector::get(ConstVa);
214 Constant *C2xi32b = ConstantVector::get(ConstVb);
215
216 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
217 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
218
219 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
220 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
221 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
222
Evgeniy Stepanov4802b9d2013-01-16 14:38:50 +0000223 BasicBlock* BB0 = BasicBlock::Create(C);
224 // Test InsertAtEnd ICmpInst constructor.
225 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
226 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
227
Nadav Rotem16087692011-12-05 06:29:09 +0000228 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
229 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
230 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
231 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
232
233 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
234 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
235 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
236 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
237
238 Value *S0 = BTC0->stripPointerCasts();
239 Value *S1 = BTC1->stripPointerCasts();
240 Value *S2 = BTC2->stripPointerCasts();
241 Value *S3 = BTC3->stripPointerCasts();
242
243 EXPECT_NE(S0, Gep0);
244 EXPECT_NE(S1, Gep1);
245 EXPECT_NE(S2, Gep2);
246 EXPECT_NE(S3, Gep3);
247
248 int64_t Offset;
Micah Villmow791cfc22012-10-08 16:39:34 +0000249 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
Nadav Rotem16087692011-12-05 06:29:09 +0000250 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80"
251 ":128:128-n8:16:32:64-S128");
252 // Make sure we don't crash
Dan Gohmana070d2a2013-01-31 02:00:45 +0000253 GetPointerBaseWithConstantOffset(Gep0, Offset, &TD);
254 GetPointerBaseWithConstantOffset(Gep1, Offset, &TD);
255 GetPointerBaseWithConstantOffset(Gep2, Offset, &TD);
256 GetPointerBaseWithConstantOffset(Gep3, Offset, &TD);
Nadav Rotem16087692011-12-05 06:29:09 +0000257
258 // Gep of Geps
259 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
260 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
261 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
262 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
263
264 EXPECT_EQ(GepII0->getNumIndices(), 1u);
265 EXPECT_EQ(GepII1->getNumIndices(), 1u);
266 EXPECT_EQ(GepII2->getNumIndices(), 1u);
267 EXPECT_EQ(GepII3->getNumIndices(), 1u);
268
269 EXPECT_FALSE(GepII0->hasAllZeroIndices());
270 EXPECT_FALSE(GepII1->hasAllZeroIndices());
271 EXPECT_FALSE(GepII2->hasAllZeroIndices());
272 EXPECT_FALSE(GepII3->hasAllZeroIndices());
273
274 delete GepII0;
275 delete GepII1;
276 delete GepII2;
277 delete GepII3;
278
279 delete BTC0;
280 delete BTC1;
281 delete BTC2;
282 delete BTC3;
283
284 delete Gep0;
285 delete Gep1;
286 delete Gep2;
287 delete Gep3;
288
Evgeniy Stepanov4802b9d2013-01-16 14:38:50 +0000289 ICmp2->eraseFromParent();
290 delete BB0;
291
Nadav Rotem16087692011-12-05 06:29:09 +0000292 delete ICmp0;
293 delete ICmp1;
294 delete PtrVecA;
295 delete PtrVecB;
296}
297
Duncan Sands8883c432012-04-16 16:28:59 +0000298TEST(InstructionsTest, FPMathOperator) {
299 LLVMContext &Context = getGlobalContext();
300 IRBuilder<> Builder(Context);
301 MDBuilder MDHelper(Context);
302 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
303 MDNode *MD1 = MDHelper.createFPMath(1.0);
Duncan Sands8883c432012-04-16 16:28:59 +0000304 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
Duncan Sands8883c432012-04-16 16:28:59 +0000305 EXPECT_TRUE(isa<FPMathOperator>(V1));
Duncan Sands8883c432012-04-16 16:28:59 +0000306 FPMathOperator *O1 = cast<FPMathOperator>(V1);
Duncan Sands8883c432012-04-16 16:28:59 +0000307 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
Duncan Sands8883c432012-04-16 16:28:59 +0000308 delete V1;
Duncan Sands8883c432012-04-16 16:28:59 +0000309 delete I;
310}
311
Duncan Sands446cf942012-10-30 16:03:32 +0000312
313TEST(InstructionsTest, isEliminableCastPair) {
314 LLVMContext &C(getGlobalContext());
315
316 Type* Int32Ty = Type::getInt32Ty(C);
317 Type* Int64Ty = Type::getInt64Ty(C);
318 Type* Int64PtrTy = Type::getInt64PtrTy(C);
319
320 // Source and destination pointers have same size -> bitcast.
321 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
322 CastInst::IntToPtr,
323 Int64PtrTy, Int64Ty, Int64PtrTy,
324 Int32Ty, 0, Int32Ty),
325 CastInst::BitCast);
326
327 // Source and destination pointers have different sizes -> fail.
328 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
329 CastInst::IntToPtr,
330 Int64PtrTy, Int64Ty, Int64PtrTy,
331 Int32Ty, 0, Int64Ty),
332 0U);
333
334 // Middle pointer big enough -> bitcast.
335 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
336 CastInst::PtrToInt,
337 Int64Ty, Int64PtrTy, Int64Ty,
338 0, Int64Ty, 0),
339 CastInst::BitCast);
340
341 // Middle pointer too small -> fail.
342 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
343 CastInst::PtrToInt,
344 Int64Ty, Int64PtrTy, Int64Ty,
345 0, Int32Ty, 0),
346 0U);
347}
348
Gabor Greif5a7069a2010-03-16 09:55:46 +0000349} // end anonymous namespace
350} // end namespace llvm