blob: 3ca3ad2b6e8376c99828c66137f283a15bd7a290 [file] [log] [blame]
Chandler Carruth74b6a772013-01-07 15:35:46 +00001//===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
Gabor Greif15580382010-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 Carruth9fb823b2013-01-02 11:36:10 +000010#include "llvm/IR/Instructions.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000011#include "llvm/ADT/STLExtras.h"
12#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-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"
Eli Bendersky84aa5e52014-03-26 20:41:15 +000017#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/MDBuilder.h"
Eli Bendersky84aa5e52014-03-26 20:41:15 +000021#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Operator.h"
Gabor Greif15580382010-03-16 09:55:46 +000023#include "gtest/gtest.h"
Eli Bendersky84aa5e52014-03-26 20:41:15 +000024#include <memory>
Gabor Greif15580382010-03-16 09:55:46 +000025
26namespace llvm {
27namespace {
28
Gabor Greif35a9b8b2010-03-16 10:59:48 +000029TEST(InstructionsTest, ReturnInst) {
Gabor Greif15580382010-03-16 09:55:46 +000030 LLVMContext &C(getGlobalContext());
31
Gabor Greif35a9b8b2010-03-16 10:59:48 +000032 // test for PR6589
Gabor Greif15580382010-03-16 09:55:46 +000033 const ReturnInst* r0 = ReturnInst::Create(C);
Gabor Greifc377afc2010-03-16 15:26:09 +000034 EXPECT_EQ(r0->getNumOperands(), 0U);
Gabor Greif35a9b8b2010-03-16 10:59:48 +000035 EXPECT_EQ(r0->op_begin(), r0->op_end());
Gabor Greif86ca5492010-03-16 11:24:53 +000036
Chris Lattner229907c2011-07-18 04:54:35 +000037 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greif86ca5492010-03-16 11:24:53 +000038 Constant* One = ConstantInt::get(Int1, 1, true);
39 const ReturnInst* r1 = ReturnInst::Create(C, One);
John McCalle83797c2011-08-27 19:23:22 +000040 EXPECT_EQ(1U, r1->getNumOperands());
Gabor Greif86ca5492010-03-16 11:24:53 +000041 User::const_op_iterator b(r1->op_begin());
John McCalle83797c2011-08-27 19:23:22 +000042 EXPECT_NE(r1->op_end(), b);
43 EXPECT_EQ(One, *b);
44 EXPECT_EQ(One, r1->getOperand(0));
Gabor Greif86ca5492010-03-16 11:24:53 +000045 ++b;
John McCalle83797c2011-08-27 19:23:22 +000046 EXPECT_EQ(r1->op_end(), b);
Gabor Greif421dd122010-03-16 12:32:03 +000047
48 // clean up
49 delete r0;
50 delete r1;
Gabor Greif15580382010-03-16 09:55:46 +000051}
52
Eli Bendersky84741622014-03-26 21:46:24 +000053// Test fixture that provides a module and a single function within it. Useful
54// for tests that need to refer to the function in some way.
55class ModuleWithFunctionTest : public testing::Test {
56protected:
NAKAMURA Takumibe8556d2014-03-27 11:32:41 +000057 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
NAKAMURA Takumicb5ebf62014-03-27 11:38:28 +000058 FArgTypes.push_back(Type::getInt8Ty(Ctx));
59 FArgTypes.push_back(Type::getInt32Ty(Ctx));
60 FArgTypes.push_back(Type::getInt64Ty(Ctx));
Eli Bendersky84741622014-03-26 21:46:24 +000061 FunctionType *FTy =
62 FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
63 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
64 }
Eli Bendersky84aa5e52014-03-26 20:41:15 +000065
Eli Bendersky84741622014-03-26 21:46:24 +000066 LLVMContext Ctx;
67 std::unique_ptr<Module> M;
NAKAMURA Takumicce8a582014-03-27 11:33:11 +000068 SmallVector<Type *, 3> FArgTypes;
Eli Bendersky84741622014-03-26 21:46:24 +000069 Function *F;
70};
Eli Bendersky84aa5e52014-03-26 20:41:15 +000071
Eli Bendersky84741622014-03-26 21:46:24 +000072TEST_F(ModuleWithFunctionTest, CallInst) {
73 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
74 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
75 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
Eli Benderskyc35c4b32014-03-26 21:11:34 +000076 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
Eli Bendersky84aa5e52014-03-26 20:41:15 +000077
78 // Make sure iteration over a call's arguments works as expected.
79 unsigned Idx = 0;
80 for (Value *Arg : Call->arg_operands()) {
Eli Bendersky84741622014-03-26 21:46:24 +000081 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
Eli Bendersky84aa5e52014-03-26 20:41:15 +000082 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
83 Idx++;
84 }
85}
86
Eli Bendersky84741622014-03-26 21:46:24 +000087TEST_F(ModuleWithFunctionTest, InvokeInst) {
88 BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
89 BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
90
91 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
92 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
93 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
94 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
95
96 // Make sure iteration over invoke's arguments works as expected.
97 unsigned Idx = 0;
98 for (Value *Arg : Invoke->arg_operands()) {
99 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
100 EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
101 Idx++;
102 }
103}
104
Gabor Greifc377afc2010-03-16 15:26:09 +0000105TEST(InstructionsTest, BranchInst) {
106 LLVMContext &C(getGlobalContext());
107
108 // Make a BasicBlocks
109 BasicBlock* bb0 = BasicBlock::Create(C);
110 BasicBlock* bb1 = BasicBlock::Create(C);
111
112 // Mandatory BranchInst
113 const BranchInst* b0 = BranchInst::Create(bb0);
114
Gabor Greife52f3982010-03-16 15:53:58 +0000115 EXPECT_TRUE(b0->isUnconditional());
116 EXPECT_FALSE(b0->isConditional());
John McCalle83797c2011-08-27 19:23:22 +0000117 EXPECT_EQ(1U, b0->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +0000118
Gabor Greifc377afc2010-03-16 15:26:09 +0000119 // check num operands
John McCalle83797c2011-08-27 19:23:22 +0000120 EXPECT_EQ(1U, b0->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +0000121
122 EXPECT_NE(b0->op_begin(), b0->op_end());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000123 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
Gabor Greife52f3982010-03-16 15:53:58 +0000124
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000125 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
Gabor Greifc377afc2010-03-16 15:26:09 +0000126
Chris Lattner229907c2011-07-18 04:54:35 +0000127 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greifc377afc2010-03-16 15:26:09 +0000128 Constant* One = ConstantInt::get(Int1, 1, true);
129
130 // Conditional BranchInst
131 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
132
Gabor Greife52f3982010-03-16 15:53:58 +0000133 EXPECT_FALSE(b1->isUnconditional());
134 EXPECT_TRUE(b1->isConditional());
John McCalle83797c2011-08-27 19:23:22 +0000135 EXPECT_EQ(2U, b1->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +0000136
Gabor Greifc377afc2010-03-16 15:26:09 +0000137 // check num operands
John McCalle83797c2011-08-27 19:23:22 +0000138 EXPECT_EQ(3U, b1->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +0000139
140 User::const_op_iterator b(b1->op_begin());
141
142 // check COND
143 EXPECT_NE(b, b1->op_end());
John McCalle83797c2011-08-27 19:23:22 +0000144 EXPECT_EQ(One, *b);
145 EXPECT_EQ(One, b1->getOperand(0));
146 EXPECT_EQ(One, b1->getCondition());
Gabor Greifc377afc2010-03-16 15:26:09 +0000147 ++b;
148
149 // check ELSE
John McCalle83797c2011-08-27 19:23:22 +0000150 EXPECT_EQ(bb1, *b);
151 EXPECT_EQ(bb1, b1->getOperand(1));
152 EXPECT_EQ(bb1, b1->getSuccessor(1));
Gabor Greifc377afc2010-03-16 15:26:09 +0000153 ++b;
154
155 // check THEN
John McCalle83797c2011-08-27 19:23:22 +0000156 EXPECT_EQ(bb0, *b);
157 EXPECT_EQ(bb0, b1->getOperand(2));
158 EXPECT_EQ(bb0, b1->getSuccessor(0));
Gabor Greifc377afc2010-03-16 15:26:09 +0000159 ++b;
160
John McCalle83797c2011-08-27 19:23:22 +0000161 EXPECT_EQ(b1->op_end(), b);
Gabor Greifc377afc2010-03-16 15:26:09 +0000162
Gabor Greifc377afc2010-03-16 15:26:09 +0000163 // clean up
164 delete b0;
165 delete b1;
166
167 delete bb0;
168 delete bb1;
169}
170
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000171TEST(InstructionsTest, CastInst) {
172 LLVMContext &C(getGlobalContext());
173
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000174 Type *Int8Ty = Type::getInt8Ty(C);
175 Type *Int16Ty = Type::getInt16Ty(C);
176 Type *Int32Ty = Type::getInt32Ty(C);
177 Type *Int64Ty = Type::getInt64Ty(C);
178 Type *V8x8Ty = VectorType::get(Int8Ty, 8);
179 Type *V8x64Ty = VectorType::get(Int64Ty, 8);
180 Type *X86MMXTy = Type::getX86_MMXTy(C);
181
182 Type *HalfTy = Type::getHalfTy(C);
183 Type *FloatTy = Type::getFloatTy(C);
184 Type *DoubleTy = Type::getDoubleTy(C);
185
186 Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
187 Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
188 Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
189
190 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
191 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
192
193 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
194 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
195
196 Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
197 Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
198 Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
199 Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
200
201 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
202 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +0000203 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000204
Duncan Sandsa8514532011-05-18 07:13:41 +0000205 const Constant* c8 = Constant::getNullValue(V8x8Ty);
206 const Constant* c64 = Constant::getNullValue(V8x64Ty);
207
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000208 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
209
Matt Arsenaultb4019ae2013-07-30 22:02:14 +0000210 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
211 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
212 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
213 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
214 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
John McCalle83797c2011-08-27 19:23:22 +0000215 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
216 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000217
218 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
219 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
220 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
221 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
222 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
223
224 // Check address space casts are rejected since we don't know the sizes here
225 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
226 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
227 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
228 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
229 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000230 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
231 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
232 V2Int32PtrAS1Ty,
233 true));
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000234
235 // Test mismatched number of elements for pointers
236 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
237 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
238 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
239 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
240 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
241
242 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
243 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
244 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
245 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
246 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
247 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
248 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
249 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
250 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
251
252 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
253 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
254 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
255
256 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
257 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
258 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
259 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
260 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
261 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
Matt Arsenault065ced92013-07-31 00:17:33 +0000262
263
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +0000264 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
265 Constant::getNullValue(V4Int32PtrTy),
266 V2Int32PtrTy));
267 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
268 Constant::getNullValue(V2Int32PtrTy),
269 V4Int32PtrTy));
270
271 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
272 Constant::getNullValue(V4Int32PtrAS1Ty),
273 V2Int32PtrTy));
274 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
275 Constant::getNullValue(V2Int32PtrTy),
276 V4Int32PtrAS1Ty));
277
278
Matt Arsenault065ced92013-07-31 00:17:33 +0000279 // Check that assertion is not hit when creating a cast with a vector of
280 // pointers
281 // First form
282 BasicBlock *BB = BasicBlock::Create(C);
283 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
284 CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
285
286 // Second form
287 CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000288}
289
Nadav Rotem3924cb02011-12-05 06:29:09 +0000290TEST(InstructionsTest, VectorGep) {
291 LLVMContext &C(getGlobalContext());
292
293 // Type Definitions
David Blaikieb3a39062015-03-14 21:40:10 +0000294 Type *I8Ty = IntegerType::get(C, 8);
295 Type *I32Ty = IntegerType::get(C, 32);
296 PointerType *Ptri8Ty = PointerType::get(I8Ty, 0);
297 PointerType *Ptri32Ty = PointerType::get(I32Ty, 0);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000298
299 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
300 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
301
302 // Test different aspects of the vector-of-pointers type
303 // and GEPs which use this type.
304 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
305 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
306 std::vector<Constant*> ConstVa(2, Ci32a);
307 std::vector<Constant*> ConstVb(2, Ci32b);
308 Constant *C2xi32a = ConstantVector::get(ConstVa);
309 Constant *C2xi32b = ConstantVector::get(ConstVb);
310
311 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
312 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
313
314 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
315 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
316 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
317
Evgeniy Stepanova259b262013-01-16 14:38:50 +0000318 BasicBlock* BB0 = BasicBlock::Create(C);
319 // Test InsertAtEnd ICmpInst constructor.
320 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
321 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
322
David Blaikieb3a39062015-03-14 21:40:10 +0000323 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a);
324 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b);
325 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a);
326 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000327
328 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
329 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
330 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
331 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
332
333 Value *S0 = BTC0->stripPointerCasts();
334 Value *S1 = BTC1->stripPointerCasts();
335 Value *S2 = BTC2->stripPointerCasts();
336 Value *S3 = BTC3->stripPointerCasts();
337
338 EXPECT_NE(S0, Gep0);
339 EXPECT_NE(S1, Gep1);
340 EXPECT_NE(S2, Gep2);
341 EXPECT_NE(S3, Gep3);
342
343 int64_t Offset;
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000344 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
Rafael Espindolac6751622013-12-13 18:56:34 +0000345 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
Nadav Rotem3924cb02011-12-05 06:29:09 +0000346 ":128:128-n8:16:32:64-S128");
347 // Make sure we don't crash
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000348 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
349 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
350 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
351 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000352
353 // Gep of Geps
David Blaikieb3a39062015-03-14 21:40:10 +0000354 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b);
355 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a);
356 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b);
357 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000358
359 EXPECT_EQ(GepII0->getNumIndices(), 1u);
360 EXPECT_EQ(GepII1->getNumIndices(), 1u);
361 EXPECT_EQ(GepII2->getNumIndices(), 1u);
362 EXPECT_EQ(GepII3->getNumIndices(), 1u);
363
364 EXPECT_FALSE(GepII0->hasAllZeroIndices());
365 EXPECT_FALSE(GepII1->hasAllZeroIndices());
366 EXPECT_FALSE(GepII2->hasAllZeroIndices());
367 EXPECT_FALSE(GepII3->hasAllZeroIndices());
368
369 delete GepII0;
370 delete GepII1;
371 delete GepII2;
372 delete GepII3;
373
374 delete BTC0;
375 delete BTC1;
376 delete BTC2;
377 delete BTC3;
378
379 delete Gep0;
380 delete Gep1;
381 delete Gep2;
382 delete Gep3;
383
Evgeniy Stepanova259b262013-01-16 14:38:50 +0000384 ICmp2->eraseFromParent();
385 delete BB0;
386
Nadav Rotem3924cb02011-12-05 06:29:09 +0000387 delete ICmp0;
388 delete ICmp1;
389 delete PtrVecA;
390 delete PtrVecB;
391}
392
Duncan Sands05f4df82012-04-16 16:28:59 +0000393TEST(InstructionsTest, FPMathOperator) {
394 LLVMContext &Context = getGlobalContext();
395 IRBuilder<> Builder(Context);
396 MDBuilder MDHelper(Context);
397 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
398 MDNode *MD1 = MDHelper.createFPMath(1.0);
Duncan Sands05f4df82012-04-16 16:28:59 +0000399 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
Duncan Sands05f4df82012-04-16 16:28:59 +0000400 EXPECT_TRUE(isa<FPMathOperator>(V1));
Duncan Sands05f4df82012-04-16 16:28:59 +0000401 FPMathOperator *O1 = cast<FPMathOperator>(V1);
Duncan Sands05f4df82012-04-16 16:28:59 +0000402 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
Duncan Sands05f4df82012-04-16 16:28:59 +0000403 delete V1;
Duncan Sands05f4df82012-04-16 16:28:59 +0000404 delete I;
405}
406
Duncan Sandse2395dc2012-10-30 16:03:32 +0000407
408TEST(InstructionsTest, isEliminableCastPair) {
409 LLVMContext &C(getGlobalContext());
410
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000411 Type* Int16Ty = Type::getInt16Ty(C);
Duncan Sandse2395dc2012-10-30 16:03:32 +0000412 Type* Int32Ty = Type::getInt32Ty(C);
413 Type* Int64Ty = Type::getInt64Ty(C);
414 Type* Int64PtrTy = Type::getInt64PtrTy(C);
415
416 // Source and destination pointers have same size -> bitcast.
417 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
418 CastInst::IntToPtr,
419 Int64PtrTy, Int64Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000420 Int32Ty, nullptr, Int32Ty),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000421 CastInst::BitCast);
422
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000423 // Source and destination have unknown sizes, but the same address space and
424 // the intermediate int is the maximum pointer size -> bitcast
Duncan Sandse2395dc2012-10-30 16:03:32 +0000425 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
426 CastInst::IntToPtr,
427 Int64PtrTy, Int64Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000428 nullptr, nullptr, nullptr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000429 CastInst::BitCast);
430
431 // Source and destination have unknown sizes, but the same address space and
432 // the intermediate int is not the maximum pointer size -> nothing
433 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
434 CastInst::IntToPtr,
435 Int64PtrTy, Int32Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000436 nullptr, nullptr, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000437 0U);
438
439 // Middle pointer big enough -> bitcast.
440 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
441 CastInst::PtrToInt,
442 Int64Ty, Int64PtrTy, Int64Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000443 nullptr, Int64Ty, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000444 CastInst::BitCast);
445
446 // Middle pointer too small -> fail.
447 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
448 CastInst::PtrToInt,
449 Int64Ty, Int64PtrTy, Int64Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000450 nullptr, Int32Ty, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000451 0U);
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000452
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000453 // Test that we don't eliminate bitcasts between different address spaces,
454 // or if we don't have available pointer size information.
455 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
456 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
Rafael Espindolac6751622013-12-13 18:56:34 +0000457 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000458
459 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
460 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
461
462 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
463 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
464
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000465 // Cannot simplify inttoptr, addrspacecast
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000466 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000467 CastInst::AddrSpaceCast,
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000468 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
Craig Topper66f09ad2014-06-08 22:29:17 +0000469 nullptr, Int16SizePtr, Int64SizePtr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000470 0U);
471
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000472 // Cannot simplify addrspacecast, ptrtoint
473 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
474 CastInst::PtrToInt,
475 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000476 Int64SizePtr, Int16SizePtr, nullptr),
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000477 0U);
478
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000479 // Pass since the bitcast address spaces are the same
480 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
481 CastInst::BitCast,
482 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
Craig Topper66f09ad2014-06-08 22:29:17 +0000483 nullptr, nullptr, nullptr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000484 CastInst::IntToPtr);
485
Duncan Sandse2395dc2012-10-30 16:03:32 +0000486}
487
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000488TEST(InstructionsTest, CloneCall) {
489 LLVMContext &C(getGlobalContext());
490 Type *Int32Ty = Type::getInt32Ty(C);
491 Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
492 Type *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
493 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
494 Value *Args[] = {
495 ConstantInt::get(Int32Ty, 1),
496 ConstantInt::get(Int32Ty, 2),
497 ConstantInt::get(Int32Ty, 3)
498 };
499 std::unique_ptr<CallInst> Call(CallInst::Create(Callee, Args, "result"));
500
501 // Test cloning the tail call kind.
502 CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
503 CallInst::TCK_MustTail};
504 for (CallInst::TailCallKind TCK : Kinds) {
505 Call->setTailCallKind(TCK);
506 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
507 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
508 }
509 Call->setTailCallKind(CallInst::TCK_None);
510
511 // Test cloning an attribute.
512 {
513 AttrBuilder AB;
514 AB.addAttribute(Attribute::ReadOnly);
515 Call->setAttributes(AttributeSet::get(C, AttributeSet::FunctionIndex, AB));
516 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
517 EXPECT_TRUE(Clone->onlyReadsMemory());
518 }
519}
520
Gabor Greif15580382010-03-16 09:55:46 +0000521} // end anonymous namespace
522} // end namespace llvm
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000523
524