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/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/MDBuilder.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/Operator.h"
23 #include "gtest/gtest.h"
29 TEST(InstructionsTest, ReturnInst) {
30 LLVMContext &C(getGlobalContext());
33 const ReturnInst* r0 = ReturnInst::Create(C);
34 EXPECT_EQ(r0->getNumOperands(), 0U);
35 EXPECT_EQ(r0->op_begin(), r0->op_end());
37 IntegerType* Int1 = IntegerType::get(C, 1);
38 Constant* One = ConstantInt::get(Int1, 1, true);
39 const ReturnInst* r1 = ReturnInst::Create(C, One);
40 EXPECT_EQ(1U, r1->getNumOperands());
41 User::const_op_iterator b(r1->op_begin());
42 EXPECT_NE(r1->op_end(), b);
44 EXPECT_EQ(One, r1->getOperand(0));
46 EXPECT_EQ(r1->op_end(), b);
53 TEST(InstructionsTest, CallInst) {
54 LLVMContext &C(getGlobalContext());
55 std::unique_ptr<Module> M(new Module("MyModule", C));
57 Type *ArgTypes[] = {Type::getInt8Ty(C), Type::getInt32Ty(C),
59 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), ArgTypes, false);
60 Function *F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
62 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(C), 20),
63 ConstantInt::get(Type::getInt32Ty(C), 9999),
64 ConstantInt::get(Type::getInt64Ty(C), 42)};
65 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
67 // Make sure iteration over a call's arguments works as expected.
69 for (Value *Arg : Call->arg_operands()) {
70 EXPECT_EQ(ArgTypes[Idx], Arg->getType());
71 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
76 TEST(InstructionsTest, BranchInst) {
77 LLVMContext &C(getGlobalContext());
80 BasicBlock* bb0 = BasicBlock::Create(C);
81 BasicBlock* bb1 = BasicBlock::Create(C);
83 // Mandatory BranchInst
84 const BranchInst* b0 = BranchInst::Create(bb0);
86 EXPECT_TRUE(b0->isUnconditional());
87 EXPECT_FALSE(b0->isConditional());
88 EXPECT_EQ(1U, b0->getNumSuccessors());
91 EXPECT_EQ(1U, b0->getNumOperands());
93 EXPECT_NE(b0->op_begin(), b0->op_end());
94 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
96 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
98 IntegerType* Int1 = IntegerType::get(C, 1);
99 Constant* One = ConstantInt::get(Int1, 1, true);
101 // Conditional BranchInst
102 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
104 EXPECT_FALSE(b1->isUnconditional());
105 EXPECT_TRUE(b1->isConditional());
106 EXPECT_EQ(2U, b1->getNumSuccessors());
108 // check num operands
109 EXPECT_EQ(3U, b1->getNumOperands());
111 User::const_op_iterator b(b1->op_begin());
114 EXPECT_NE(b, b1->op_end());
116 EXPECT_EQ(One, b1->getOperand(0));
117 EXPECT_EQ(One, b1->getCondition());
122 EXPECT_EQ(bb1, b1->getOperand(1));
123 EXPECT_EQ(bb1, b1->getSuccessor(1));
128 EXPECT_EQ(bb0, b1->getOperand(2));
129 EXPECT_EQ(bb0, b1->getSuccessor(0));
132 EXPECT_EQ(b1->op_end(), b);
142 TEST(InstructionsTest, CastInst) {
143 LLVMContext &C(getGlobalContext());
145 Type *Int8Ty = Type::getInt8Ty(C);
146 Type *Int16Ty = Type::getInt16Ty(C);
147 Type *Int32Ty = Type::getInt32Ty(C);
148 Type *Int64Ty = Type::getInt64Ty(C);
149 Type *V8x8Ty = VectorType::get(Int8Ty, 8);
150 Type *V8x64Ty = VectorType::get(Int64Ty, 8);
151 Type *X86MMXTy = Type::getX86_MMXTy(C);
153 Type *HalfTy = Type::getHalfTy(C);
154 Type *FloatTy = Type::getFloatTy(C);
155 Type *DoubleTy = Type::getDoubleTy(C);
157 Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
158 Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
159 Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
161 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
162 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
164 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
165 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
167 Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
168 Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
169 Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
170 Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
172 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
173 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
174 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
176 const Constant* c8 = Constant::getNullValue(V8x8Ty);
177 const Constant* c64 = Constant::getNullValue(V8x64Ty);
179 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
181 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
182 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
183 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
184 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
185 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
186 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
187 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
189 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
190 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
191 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
192 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
193 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
195 // Check address space casts are rejected since we don't know the sizes here
196 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
197 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
198 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
199 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
200 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
201 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
202 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
206 // Test mismatched number of elements for pointers
207 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
208 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
209 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
210 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
211 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
213 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
214 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
215 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
216 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
217 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
218 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
219 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
220 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
221 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
223 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
224 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
225 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
227 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
228 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
229 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
230 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
231 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
232 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
235 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
236 Constant::getNullValue(V4Int32PtrTy),
238 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
239 Constant::getNullValue(V2Int32PtrTy),
242 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
243 Constant::getNullValue(V4Int32PtrAS1Ty),
245 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
246 Constant::getNullValue(V2Int32PtrTy),
250 // Check that assertion is not hit when creating a cast with a vector of
253 BasicBlock *BB = BasicBlock::Create(C);
254 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
255 CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
258 CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
261 TEST(InstructionsTest, VectorGep) {
262 LLVMContext &C(getGlobalContext());
265 PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
266 PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 32), 0);
268 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
269 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
271 // Test different aspects of the vector-of-pointers type
272 // and GEPs which use this type.
273 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
274 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
275 std::vector<Constant*> ConstVa(2, Ci32a);
276 std::vector<Constant*> ConstVb(2, Ci32b);
277 Constant *C2xi32a = ConstantVector::get(ConstVa);
278 Constant *C2xi32b = ConstantVector::get(ConstVb);
280 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
281 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
283 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
284 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
285 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
287 BasicBlock* BB0 = BasicBlock::Create(C);
288 // Test InsertAtEnd ICmpInst constructor.
289 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
290 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
292 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
293 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
294 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
295 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
297 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
298 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
299 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
300 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
302 Value *S0 = BTC0->stripPointerCasts();
303 Value *S1 = BTC1->stripPointerCasts();
304 Value *S2 = BTC2->stripPointerCasts();
305 Value *S3 = BTC3->stripPointerCasts();
313 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
314 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
315 ":128:128-n8:16:32:64-S128");
316 // Make sure we don't crash
317 GetPointerBaseWithConstantOffset(Gep0, Offset, &TD);
318 GetPointerBaseWithConstantOffset(Gep1, Offset, &TD);
319 GetPointerBaseWithConstantOffset(Gep2, Offset, &TD);
320 GetPointerBaseWithConstantOffset(Gep3, Offset, &TD);
323 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
324 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
325 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
326 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
328 EXPECT_EQ(GepII0->getNumIndices(), 1u);
329 EXPECT_EQ(GepII1->getNumIndices(), 1u);
330 EXPECT_EQ(GepII2->getNumIndices(), 1u);
331 EXPECT_EQ(GepII3->getNumIndices(), 1u);
333 EXPECT_FALSE(GepII0->hasAllZeroIndices());
334 EXPECT_FALSE(GepII1->hasAllZeroIndices());
335 EXPECT_FALSE(GepII2->hasAllZeroIndices());
336 EXPECT_FALSE(GepII3->hasAllZeroIndices());
353 ICmp2->eraseFromParent();
362 TEST(InstructionsTest, FPMathOperator) {
363 LLVMContext &Context = getGlobalContext();
364 IRBuilder<> Builder(Context);
365 MDBuilder MDHelper(Context);
366 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
367 MDNode *MD1 = MDHelper.createFPMath(1.0);
368 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
369 EXPECT_TRUE(isa<FPMathOperator>(V1));
370 FPMathOperator *O1 = cast<FPMathOperator>(V1);
371 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
377 TEST(InstructionsTest, isEliminableCastPair) {
378 LLVMContext &C(getGlobalContext());
380 Type* Int16Ty = Type::getInt16Ty(C);
381 Type* Int32Ty = Type::getInt32Ty(C);
382 Type* Int64Ty = Type::getInt64Ty(C);
383 Type* Int64PtrTy = Type::getInt64PtrTy(C);
385 // Source and destination pointers have same size -> bitcast.
386 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
388 Int64PtrTy, Int64Ty, Int64PtrTy,
389 Int32Ty, 0, Int32Ty),
392 // Source and destination have unknown sizes, but the same address space and
393 // the intermediate int is the maximum pointer size -> bitcast
394 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
396 Int64PtrTy, Int64Ty, Int64PtrTy,
400 // Source and destination have unknown sizes, but the same address space and
401 // the intermediate int is not the maximum pointer size -> nothing
402 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
404 Int64PtrTy, Int32Ty, Int64PtrTy,
408 // Middle pointer big enough -> bitcast.
409 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
411 Int64Ty, Int64PtrTy, Int64Ty,
415 // Middle pointer too small -> fail.
416 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
418 Int64Ty, Int64PtrTy, Int64Ty,
422 // Test that we don't eliminate bitcasts between different address spaces,
423 // or if we don't have available pointer size information.
424 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
425 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
426 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
428 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
429 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
431 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
432 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
434 // Cannot simplify inttoptr, addrspacecast
435 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
436 CastInst::AddrSpaceCast,
437 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
438 0, Int16SizePtr, Int64SizePtr),
441 // Cannot simplify addrspacecast, ptrtoint
442 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
444 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
445 Int64SizePtr, Int16SizePtr, 0),
448 // Pass since the bitcast address spaces are the same
449 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
451 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
457 } // end anonymous namespace
458 } // end namespace llvm