blob: 0ae236cb3cc71c280774712821ff1717d6f3dd47 [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"
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 Greif15580382010-03-16 09:55:46 +000021#include "gtest/gtest.h"
22
23namespace llvm {
24namespace {
25
Gabor Greif35a9b8b2010-03-16 10:59:48 +000026TEST(InstructionsTest, ReturnInst) {
Gabor Greif15580382010-03-16 09:55:46 +000027 LLVMContext &C(getGlobalContext());
28
Gabor Greif35a9b8b2010-03-16 10:59:48 +000029 // test for PR6589
Gabor Greif15580382010-03-16 09:55:46 +000030 const ReturnInst* r0 = ReturnInst::Create(C);
Gabor Greifc377afc2010-03-16 15:26:09 +000031 EXPECT_EQ(r0->getNumOperands(), 0U);
Gabor Greif35a9b8b2010-03-16 10:59:48 +000032 EXPECT_EQ(r0->op_begin(), r0->op_end());
Gabor Greif86ca5492010-03-16 11:24:53 +000033
Chris Lattner229907c2011-07-18 04:54:35 +000034 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greif86ca5492010-03-16 11:24:53 +000035 Constant* One = ConstantInt::get(Int1, 1, true);
36 const ReturnInst* r1 = ReturnInst::Create(C, One);
John McCalle83797c2011-08-27 19:23:22 +000037 EXPECT_EQ(1U, r1->getNumOperands());
Gabor Greif86ca5492010-03-16 11:24:53 +000038 User::const_op_iterator b(r1->op_begin());
John McCalle83797c2011-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 Greif86ca5492010-03-16 11:24:53 +000042 ++b;
John McCalle83797c2011-08-27 19:23:22 +000043 EXPECT_EQ(r1->op_end(), b);
Gabor Greif421dd122010-03-16 12:32:03 +000044
45 // clean up
46 delete r0;
47 delete r1;
Gabor Greif15580382010-03-16 09:55:46 +000048}
49
Gabor Greifc377afc2010-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 Greife52f3982010-03-16 15:53:58 +000060 EXPECT_TRUE(b0->isUnconditional());
61 EXPECT_FALSE(b0->isConditional());
John McCalle83797c2011-08-27 19:23:22 +000062 EXPECT_EQ(1U, b0->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +000063
Gabor Greifc377afc2010-03-16 15:26:09 +000064 // check num operands
John McCalle83797c2011-08-27 19:23:22 +000065 EXPECT_EQ(1U, b0->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +000066
67 EXPECT_NE(b0->op_begin(), b0->op_end());
John McCalle83797c2011-08-27 19:23:22 +000068 EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
Gabor Greife52f3982010-03-16 15:53:58 +000069
John McCalle83797c2011-08-27 19:23:22 +000070 EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
Gabor Greifc377afc2010-03-16 15:26:09 +000071
Chris Lattner229907c2011-07-18 04:54:35 +000072 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greifc377afc2010-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 Greife52f3982010-03-16 15:53:58 +000078 EXPECT_FALSE(b1->isUnconditional());
79 EXPECT_TRUE(b1->isConditional());
John McCalle83797c2011-08-27 19:23:22 +000080 EXPECT_EQ(2U, b1->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +000081
Gabor Greifc377afc2010-03-16 15:26:09 +000082 // check num operands
John McCalle83797c2011-08-27 19:23:22 +000083 EXPECT_EQ(3U, b1->getNumOperands());
Gabor Greifc377afc2010-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 McCalle83797c2011-08-27 19:23:22 +000089 EXPECT_EQ(One, *b);
90 EXPECT_EQ(One, b1->getOperand(0));
91 EXPECT_EQ(One, b1->getCondition());
Gabor Greifc377afc2010-03-16 15:26:09 +000092 ++b;
93
94 // check ELSE
John McCalle83797c2011-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 Greifc377afc2010-03-16 15:26:09 +000098 ++b;
99
100 // check THEN
John McCalle83797c2011-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 Greifc377afc2010-03-16 15:26:09 +0000104 ++b;
105
John McCalle83797c2011-08-27 19:23:22 +0000106 EXPECT_EQ(b1->op_end(), b);
Gabor Greifc377afc2010-03-16 15:26:09 +0000107
Gabor Greifc377afc2010-03-16 15:26:09 +0000108 // clean up
109 delete b0;
110 delete b1;
111
112 delete bb0;
113 delete bb1;
114}
115
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000116TEST(InstructionsTest, CastInst) {
117 LLVMContext &C(getGlobalContext());
118
Matt Arsenaultcacbb232013-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 Sands2d3cdd62011-04-01 03:34:54 +0000148
Duncan Sandsa8514532011-05-18 07:13:41 +0000149 const Constant* c8 = Constant::getNullValue(V8x8Ty);
150 const Constant* c64 = Constant::getNullValue(V8x64Ty);
151
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000152 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
153
Matt Arsenaultb4019ae2013-07-30 22:02:14 +0000154 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
155 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
156 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
157 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
158 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
John McCalle83797c2011-08-27 19:23:22 +0000159 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
160 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000161
162 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
163 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
164 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
165 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
166 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
167
168 // Check address space casts are rejected since we don't know the sizes here
169 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
170 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
171 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
172 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
173 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000174 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
175 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
176 V2Int32PtrAS1Ty,
177 true));
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000178
179 // Test mismatched number of elements for pointers
180 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
181 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
182 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
183 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
184 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
185
186 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
187 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
188 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
189 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
190 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
191 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
192 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
193 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
194 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
195
196 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
197 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
198 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
199
200 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
201 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
202 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
203 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
204 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
205 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
Matt Arsenault065ced92013-07-31 00:17:33 +0000206
207
208 // Check that assertion is not hit when creating a cast with a vector of
209 // pointers
210 // First form
211 BasicBlock *BB = BasicBlock::Create(C);
212 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
213 CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
214
215 // Second form
216 CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000217}
218
Nadav Rotem3924cb02011-12-05 06:29:09 +0000219TEST(InstructionsTest, VectorGep) {
220 LLVMContext &C(getGlobalContext());
221
222 // Type Definitions
223 PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
Matt Arsenault50a16a42013-06-28 23:24:10 +0000224 PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 32), 0);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000225
226 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
227 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
228
229 // Test different aspects of the vector-of-pointers type
230 // and GEPs which use this type.
231 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
232 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
233 std::vector<Constant*> ConstVa(2, Ci32a);
234 std::vector<Constant*> ConstVb(2, Ci32b);
235 Constant *C2xi32a = ConstantVector::get(ConstVa);
236 Constant *C2xi32b = ConstantVector::get(ConstVb);
237
238 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
239 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
240
241 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
242 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
243 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
244
Evgeniy Stepanova259b262013-01-16 14:38:50 +0000245 BasicBlock* BB0 = BasicBlock::Create(C);
246 // Test InsertAtEnd ICmpInst constructor.
247 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
248 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
249
Nadav Rotem3924cb02011-12-05 06:29:09 +0000250 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
251 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
252 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
253 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
254
255 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
256 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
257 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
258 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
259
260 Value *S0 = BTC0->stripPointerCasts();
261 Value *S1 = BTC1->stripPointerCasts();
262 Value *S2 = BTC2->stripPointerCasts();
263 Value *S3 = BTC3->stripPointerCasts();
264
265 EXPECT_NE(S0, Gep0);
266 EXPECT_NE(S1, Gep1);
267 EXPECT_NE(S2, Gep2);
268 EXPECT_NE(S3, Gep3);
269
270 int64_t Offset;
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000271 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 +0000272 "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 +0000273 ":128:128-n8:16:32:64-S128");
274 // Make sure we don't crash
Dan Gohman20a2ae92013-01-31 02:00:45 +0000275 GetPointerBaseWithConstantOffset(Gep0, Offset, &TD);
276 GetPointerBaseWithConstantOffset(Gep1, Offset, &TD);
277 GetPointerBaseWithConstantOffset(Gep2, Offset, &TD);
278 GetPointerBaseWithConstantOffset(Gep3, Offset, &TD);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000279
280 // Gep of Geps
281 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
282 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
283 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
284 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
285
286 EXPECT_EQ(GepII0->getNumIndices(), 1u);
287 EXPECT_EQ(GepII1->getNumIndices(), 1u);
288 EXPECT_EQ(GepII2->getNumIndices(), 1u);
289 EXPECT_EQ(GepII3->getNumIndices(), 1u);
290
291 EXPECT_FALSE(GepII0->hasAllZeroIndices());
292 EXPECT_FALSE(GepII1->hasAllZeroIndices());
293 EXPECT_FALSE(GepII2->hasAllZeroIndices());
294 EXPECT_FALSE(GepII3->hasAllZeroIndices());
295
296 delete GepII0;
297 delete GepII1;
298 delete GepII2;
299 delete GepII3;
300
301 delete BTC0;
302 delete BTC1;
303 delete BTC2;
304 delete BTC3;
305
306 delete Gep0;
307 delete Gep1;
308 delete Gep2;
309 delete Gep3;
310
Evgeniy Stepanova259b262013-01-16 14:38:50 +0000311 ICmp2->eraseFromParent();
312 delete BB0;
313
Nadav Rotem3924cb02011-12-05 06:29:09 +0000314 delete ICmp0;
315 delete ICmp1;
316 delete PtrVecA;
317 delete PtrVecB;
318}
319
Duncan Sands05f4df82012-04-16 16:28:59 +0000320TEST(InstructionsTest, FPMathOperator) {
321 LLVMContext &Context = getGlobalContext();
322 IRBuilder<> Builder(Context);
323 MDBuilder MDHelper(Context);
324 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
325 MDNode *MD1 = MDHelper.createFPMath(1.0);
Duncan Sands05f4df82012-04-16 16:28:59 +0000326 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
Duncan Sands05f4df82012-04-16 16:28:59 +0000327 EXPECT_TRUE(isa<FPMathOperator>(V1));
Duncan Sands05f4df82012-04-16 16:28:59 +0000328 FPMathOperator *O1 = cast<FPMathOperator>(V1);
Duncan Sands05f4df82012-04-16 16:28:59 +0000329 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
Duncan Sands05f4df82012-04-16 16:28:59 +0000330 delete V1;
Duncan Sands05f4df82012-04-16 16:28:59 +0000331 delete I;
332}
333
Duncan Sandse2395dc2012-10-30 16:03:32 +0000334
335TEST(InstructionsTest, isEliminableCastPair) {
336 LLVMContext &C(getGlobalContext());
337
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000338 Type* Int16Ty = Type::getInt16Ty(C);
Duncan Sandse2395dc2012-10-30 16:03:32 +0000339 Type* Int32Ty = Type::getInt32Ty(C);
340 Type* Int64Ty = Type::getInt64Ty(C);
341 Type* Int64PtrTy = Type::getInt64PtrTy(C);
342
343 // Source and destination pointers have same size -> bitcast.
344 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
345 CastInst::IntToPtr,
346 Int64PtrTy, Int64Ty, Int64PtrTy,
347 Int32Ty, 0, Int32Ty),
348 CastInst::BitCast);
349
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000350 // Source and destination have unknown sizes, but the same address space and
351 // the intermediate int is the maximum pointer size -> bitcast
Duncan Sandse2395dc2012-10-30 16:03:32 +0000352 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
353 CastInst::IntToPtr,
354 Int64PtrTy, Int64Ty, Int64PtrTy,
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000355 0, 0, 0),
356 CastInst::BitCast);
357
358 // Source and destination have unknown sizes, but the same address space and
359 // the intermediate int is not the maximum pointer size -> nothing
360 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
361 CastInst::IntToPtr,
362 Int64PtrTy, Int32Ty, Int64PtrTy,
363 0, 0, 0),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000364 0U);
365
366 // Middle pointer big enough -> bitcast.
367 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
368 CastInst::PtrToInt,
369 Int64Ty, Int64PtrTy, Int64Ty,
370 0, Int64Ty, 0),
371 CastInst::BitCast);
372
373 // Middle pointer too small -> fail.
374 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
375 CastInst::PtrToInt,
376 Int64Ty, Int64PtrTy, Int64Ty,
377 0, Int32Ty, 0),
378 0U);
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000379
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000380 // Test that we don't eliminate bitcasts between different address spaces,
381 // or if we don't have available pointer size information.
382 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
383 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
Rafael Espindolac6751622013-12-13 18:56:34 +0000384 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000385
386 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
387 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
388
389 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
390 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
391
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000392 // Cannot simplify inttoptr, addrspacecast
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000393 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000394 CastInst::AddrSpaceCast,
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000395 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
396 0, Int16SizePtr, Int64SizePtr),
397 0U);
398
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000399 // Cannot simplify addrspacecast, ptrtoint
400 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
401 CastInst::PtrToInt,
402 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
403 Int64SizePtr, Int16SizePtr, 0),
404 0U);
405
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000406 // Pass since the bitcast address spaces are the same
407 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
408 CastInst::BitCast,
409 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
410 0, 0, 0),
411 CastInst::IntToPtr);
412
Duncan Sandse2395dc2012-10-30 16:03:32 +0000413}
414
Gabor Greif15580382010-03-16 09:55:46 +0000415} // end anonymous namespace
416} // end namespace llvm
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000417
418