1 //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/Instructions.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Analysis/ValueTracking.h"
13 #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"
21 #include "gtest/gtest.h"
26 TEST(InstructionsTest, ReturnInst) {
27 LLVMContext &C(getGlobalContext());
30 const ReturnInst* r0 = ReturnInst::Create(C);
31 EXPECT_EQ(r0->getNumOperands(), 0U);
32 EXPECT_EQ(r0->op_begin(), r0->op_end());
34 IntegerType* Int1 = IntegerType::get(C, 1);
35 Constant* One = ConstantInt::get(Int1, 1, true);
36 const ReturnInst* r1 = ReturnInst::Create(C, One);
37 EXPECT_EQ(1U, r1->getNumOperands());
38 User::const_op_iterator b(r1->op_begin());
39 EXPECT_NE(r1->op_end(), b);
41 EXPECT_EQ(One, r1->getOperand(0));
43 EXPECT_EQ(r1->op_end(), b);
50 TEST(InstructionsTest, BranchInst) {
51 LLVMContext &C(getGlobalContext());
54 BasicBlock* bb0 = BasicBlock::Create(C);
55 BasicBlock* bb1 = BasicBlock::Create(C);
57 // Mandatory BranchInst
58 const BranchInst* b0 = BranchInst::Create(bb0);
60 EXPECT_TRUE(b0->isUnconditional());
61 EXPECT_FALSE(b0->isConditional());
62 EXPECT_EQ(1U, b0->getNumSuccessors());
65 EXPECT_EQ(1U, b0->getNumOperands());
67 EXPECT_NE(b0->op_begin(), b0->op_end());
68 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
70 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
72 IntegerType* Int1 = IntegerType::get(C, 1);
73 Constant* One = ConstantInt::get(Int1, 1, true);
75 // Conditional BranchInst
76 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
78 EXPECT_FALSE(b1->isUnconditional());
79 EXPECT_TRUE(b1->isConditional());
80 EXPECT_EQ(2U, b1->getNumSuccessors());
83 EXPECT_EQ(3U, b1->getNumOperands());
85 User::const_op_iterator b(b1->op_begin());
88 EXPECT_NE(b, b1->op_end());
90 EXPECT_EQ(One, b1->getOperand(0));
91 EXPECT_EQ(One, b1->getCondition());
96 EXPECT_EQ(bb1, b1->getOperand(1));
97 EXPECT_EQ(bb1, b1->getSuccessor(1));
102 EXPECT_EQ(bb0, b1->getOperand(2));
103 EXPECT_EQ(bb0, b1->getSuccessor(0));
106 EXPECT_EQ(b1->op_end(), b);
116 TEST(InstructionsTest, CastInst) {
117 LLVMContext &C(getGlobalContext());
119 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);
127 Type *HalfTy = Type::getHalfTy(C);
128 Type *FloatTy = Type::getFloatTy(C);
129 Type *DoubleTy = Type::getDoubleTy(C);
131 Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
132 Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
133 Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
135 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
136 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
138 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
139 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
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);
146 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
147 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
148 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
150 const Constant* c8 = Constant::getNullValue(V8x8Ty);
151 const Constant* c64 = Constant::getNullValue(V8x64Ty);
153 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
155 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
156 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
157 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
158 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
159 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
160 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
161 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
163 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
164 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
165 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
166 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
167 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
169 // Check address space casts are rejected since we don't know the sizes here
170 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
171 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
172 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
173 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
174 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
175 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
176 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
180 // Test mismatched number of elements for pointers
181 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
182 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
183 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
184 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
185 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
187 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
188 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
189 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
190 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
191 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
192 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
193 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
194 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
195 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
197 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
198 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
199 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
201 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
202 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
203 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
204 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
205 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
206 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
209 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
210 Constant::getNullValue(V4Int32PtrTy),
212 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
213 Constant::getNullValue(V2Int32PtrTy),
216 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
217 Constant::getNullValue(V4Int32PtrAS1Ty),
219 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
220 Constant::getNullValue(V2Int32PtrTy),
224 // Check that assertion is not hit when creating a cast with a vector of
227 BasicBlock *BB = BasicBlock::Create(C);
228 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
229 CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
232 CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
235 TEST(InstructionsTest, VectorGep) {
236 LLVMContext &C(getGlobalContext());
239 PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
240 PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 32), 0);
242 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
243 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
245 // Test different aspects of the vector-of-pointers type
246 // and GEPs which use this type.
247 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
248 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
249 std::vector<Constant*> ConstVa(2, Ci32a);
250 std::vector<Constant*> ConstVb(2, Ci32b);
251 Constant *C2xi32a = ConstantVector::get(ConstVa);
252 Constant *C2xi32b = ConstantVector::get(ConstVb);
254 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
255 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
257 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
258 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
259 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
261 BasicBlock* BB0 = BasicBlock::Create(C);
262 // Test InsertAtEnd ICmpInst constructor.
263 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
264 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
266 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
267 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
268 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
269 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
271 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
272 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
273 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
274 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
276 Value *S0 = BTC0->stripPointerCasts();
277 Value *S1 = BTC1->stripPointerCasts();
278 Value *S2 = BTC2->stripPointerCasts();
279 Value *S3 = BTC3->stripPointerCasts();
287 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
288 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
289 ":128:128-n8:16:32:64-S128");
290 // Make sure we don't crash
291 GetPointerBaseWithConstantOffset(Gep0, Offset, &TD);
292 GetPointerBaseWithConstantOffset(Gep1, Offset, &TD);
293 GetPointerBaseWithConstantOffset(Gep2, Offset, &TD);
294 GetPointerBaseWithConstantOffset(Gep3, Offset, &TD);
297 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
298 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
299 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
300 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
302 EXPECT_EQ(GepII0->getNumIndices(), 1u);
303 EXPECT_EQ(GepII1->getNumIndices(), 1u);
304 EXPECT_EQ(GepII2->getNumIndices(), 1u);
305 EXPECT_EQ(GepII3->getNumIndices(), 1u);
307 EXPECT_FALSE(GepII0->hasAllZeroIndices());
308 EXPECT_FALSE(GepII1->hasAllZeroIndices());
309 EXPECT_FALSE(GepII2->hasAllZeroIndices());
310 EXPECT_FALSE(GepII3->hasAllZeroIndices());
327 ICmp2->eraseFromParent();
336 TEST(InstructionsTest, FPMathOperator) {
337 LLVMContext &Context = getGlobalContext();
338 IRBuilder<> Builder(Context);
339 MDBuilder MDHelper(Context);
340 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
341 MDNode *MD1 = MDHelper.createFPMath(1.0);
342 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
343 EXPECT_TRUE(isa<FPMathOperator>(V1));
344 FPMathOperator *O1 = cast<FPMathOperator>(V1);
345 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
351 TEST(InstructionsTest, isEliminableCastPair) {
352 LLVMContext &C(getGlobalContext());
354 Type* Int16Ty = Type::getInt16Ty(C);
355 Type* Int32Ty = Type::getInt32Ty(C);
356 Type* Int64Ty = Type::getInt64Ty(C);
357 Type* Int64PtrTy = Type::getInt64PtrTy(C);
359 // Source and destination pointers have same size -> bitcast.
360 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
362 Int64PtrTy, Int64Ty, Int64PtrTy,
363 Int32Ty, 0, Int32Ty),
366 // Source and destination have unknown sizes, but the same address space and
367 // the intermediate int is the maximum pointer size -> bitcast
368 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
370 Int64PtrTy, Int64Ty, Int64PtrTy,
374 // Source and destination have unknown sizes, but the same address space and
375 // the intermediate int is not the maximum pointer size -> nothing
376 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
378 Int64PtrTy, Int32Ty, Int64PtrTy,
382 // Middle pointer big enough -> bitcast.
383 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
385 Int64Ty, Int64PtrTy, Int64Ty,
389 // Middle pointer too small -> fail.
390 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
392 Int64Ty, Int64PtrTy, Int64Ty,
396 // Test that we don't eliminate bitcasts between different address spaces,
397 // or if we don't have available pointer size information.
398 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
399 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
400 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
402 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
403 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
405 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
406 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
408 // Cannot simplify inttoptr, addrspacecast
409 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
410 CastInst::AddrSpaceCast,
411 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
412 0, Int16SizePtr, Int64SizePtr),
415 // Cannot simplify addrspacecast, ptrtoint
416 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
418 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
419 Int64SizePtr, Int16SizePtr, 0),
422 // Pass since the bitcast address spaces are the same
423 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
425 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
431 } // end anonymous namespace
432 } // end namespace llvm