forked from llvm-mirror/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstructionsTest.cpp
443 lines (356 loc) · 16 KB
/
InstructionsTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/Instructions.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Operator.h"
#include "gtest/gtest.h"
namespace llvm {
namespace {
TEST(InstructionsTest, ReturnInst) {
LLVMContext &C(getGlobalContext());
// test for PR6589
const ReturnInst* r0 = ReturnInst::Create(C);
EXPECT_EQ(r0->getNumOperands(), 0U);
EXPECT_EQ(r0->op_begin(), r0->op_end());
IntegerType* Int1 = IntegerType::get(C, 1);
Constant* One = ConstantInt::get(Int1, 1, true);
const ReturnInst* r1 = ReturnInst::Create(C, One);
EXPECT_EQ(1U, r1->getNumOperands());
User::const_op_iterator b(r1->op_begin());
EXPECT_NE(r1->op_end(), b);
EXPECT_EQ(One, *b);
EXPECT_EQ(One, r1->getOperand(0));
++b;
EXPECT_EQ(r1->op_end(), b);
// clean up
delete r0;
delete r1;
}
TEST(InstructionsTest, BranchInst) {
LLVMContext &C(getGlobalContext());
// Make a BasicBlocks
BasicBlock* bb0 = BasicBlock::Create(C);
BasicBlock* bb1 = BasicBlock::Create(C);
// Mandatory BranchInst
const BranchInst* b0 = BranchInst::Create(bb0);
EXPECT_TRUE(b0->isUnconditional());
EXPECT_FALSE(b0->isConditional());
EXPECT_EQ(1U, b0->getNumSuccessors());
// check num operands
EXPECT_EQ(1U, b0->getNumOperands());
EXPECT_NE(b0->op_begin(), b0->op_end());
EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
IntegerType* Int1 = IntegerType::get(C, 1);
Constant* One = ConstantInt::get(Int1, 1, true);
// Conditional BranchInst
BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
EXPECT_FALSE(b1->isUnconditional());
EXPECT_TRUE(b1->isConditional());
EXPECT_EQ(2U, b1->getNumSuccessors());
// check num operands
EXPECT_EQ(3U, b1->getNumOperands());
User::const_op_iterator b(b1->op_begin());
// check COND
EXPECT_NE(b, b1->op_end());
EXPECT_EQ(One, *b);
EXPECT_EQ(One, b1->getOperand(0));
EXPECT_EQ(One, b1->getCondition());
++b;
// check ELSE
EXPECT_EQ(bb1, *b);
EXPECT_EQ(bb1, b1->getOperand(1));
EXPECT_EQ(bb1, b1->getSuccessor(1));
++b;
// check THEN
EXPECT_EQ(bb0, *b);
EXPECT_EQ(bb0, b1->getOperand(2));
EXPECT_EQ(bb0, b1->getSuccessor(0));
++b;
EXPECT_EQ(b1->op_end(), b);
// clean up
delete b0;
delete b1;
delete bb0;
delete bb1;
}
TEST(InstructionsTest, CastInst) {
LLVMContext &C(getGlobalContext());
Type *Int8Ty = Type::getInt8Ty(C);
Type *Int16Ty = Type::getInt16Ty(C);
Type *Int32Ty = Type::getInt32Ty(C);
Type *Int64Ty = Type::getInt64Ty(C);
Type *V8x8Ty = VectorType::get(Int8Ty, 8);
Type *V8x64Ty = VectorType::get(Int64Ty, 8);
Type *X86MMXTy = Type::getX86_MMXTy(C);
Type *HalfTy = Type::getHalfTy(C);
Type *FloatTy = Type::getFloatTy(C);
Type *DoubleTy = Type::getDoubleTy(C);
Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
const Constant* c8 = Constant::getNullValue(V8x8Ty);
const Constant* c64 = Constant::getNullValue(V8x64Ty);
EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
// Check address space casts are rejected since we don't know the sizes here
EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
// Test mismatched number of elements for pointers
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
// Check that assertion is not hit when creating a cast with a vector of
// pointers
// First form
BasicBlock *BB = BasicBlock::Create(C);
Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
// Second form
CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
}
TEST(InstructionsTest, VectorGep) {
LLVMContext &C(getGlobalContext());
// Type Definitions
PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 32), 0);
VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
// Test different aspects of the vector-of-pointers type
// and GEPs which use this type.
ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
std::vector<Constant*> ConstVa(2, Ci32a);
std::vector<Constant*> ConstVb(2, Ci32b);
Constant *C2xi32a = ConstantVector::get(ConstVa);
Constant *C2xi32b = ConstantVector::get(ConstVb);
CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
EXPECT_NE(ICmp0, ICmp1); // suppress warning.
BasicBlock* BB0 = BasicBlock::Create(C);
// Test InsertAtEnd ICmpInst constructor.
ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
EXPECT_NE(ICmp0, ICmp2); // suppress warning.
GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
Value *S0 = BTC0->stripPointerCasts();
Value *S1 = BTC1->stripPointerCasts();
Value *S2 = BTC2->stripPointerCasts();
Value *S3 = BTC3->stripPointerCasts();
EXPECT_NE(S0, Gep0);
EXPECT_NE(S1, Gep1);
EXPECT_NE(S2, Gep2);
EXPECT_NE(S3, Gep3);
int64_t Offset;
DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
"2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80"
":128:128-n8:16:32:64-S128");
// Make sure we don't crash
GetPointerBaseWithConstantOffset(Gep0, Offset, &TD);
GetPointerBaseWithConstantOffset(Gep1, Offset, &TD);
GetPointerBaseWithConstantOffset(Gep2, Offset, &TD);
GetPointerBaseWithConstantOffset(Gep3, Offset, &TD);
// Gep of Geps
GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
EXPECT_EQ(GepII0->getNumIndices(), 1u);
EXPECT_EQ(GepII1->getNumIndices(), 1u);
EXPECT_EQ(GepII2->getNumIndices(), 1u);
EXPECT_EQ(GepII3->getNumIndices(), 1u);
EXPECT_FALSE(GepII0->hasAllZeroIndices());
EXPECT_FALSE(GepII1->hasAllZeroIndices());
EXPECT_FALSE(GepII2->hasAllZeroIndices());
EXPECT_FALSE(GepII3->hasAllZeroIndices());
delete GepII0;
delete GepII1;
delete GepII2;
delete GepII3;
delete BTC0;
delete BTC1;
delete BTC2;
delete BTC3;
delete Gep0;
delete Gep1;
delete Gep2;
delete Gep3;
ICmp2->eraseFromParent();
delete BB0;
delete ICmp0;
delete ICmp1;
delete PtrVecA;
delete PtrVecB;
}
TEST(InstructionsTest, FPMathOperator) {
LLVMContext &Context = getGlobalContext();
IRBuilder<> Builder(Context);
MDBuilder MDHelper(Context);
Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
MDNode *MD1 = MDHelper.createFPMath(1.0);
Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
EXPECT_TRUE(isa<FPMathOperator>(V1));
FPMathOperator *O1 = cast<FPMathOperator>(V1);
EXPECT_EQ(O1->getFPAccuracy(), 1.0);
delete V1;
delete I;
}
TEST(InstructionsTest, isEliminableCastPair) {
LLVMContext &C(getGlobalContext());
Type* Int16Ty = Type::getInt16Ty(C);
Type* Int32Ty = Type::getInt32Ty(C);
Type* Int64Ty = Type::getInt64Ty(C);
Type* Int64PtrTy = Type::getInt64PtrTy(C);
// Source and destination pointers have same size -> bitcast.
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
CastInst::IntToPtr,
Int64PtrTy, Int64Ty, Int64PtrTy,
Int32Ty, 0, Int32Ty),
CastInst::BitCast);
// Source and destination have unknown sizes, but the same address space and
// the intermediate int is the maximum pointer size -> bitcast
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
CastInst::IntToPtr,
Int64PtrTy, Int64Ty, Int64PtrTy,
0, 0, 0),
CastInst::BitCast);
// Source and destination have unknown sizes, but the same address space and
// the intermediate int is not the maximum pointer size -> nothing
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
CastInst::IntToPtr,
Int64PtrTy, Int32Ty, Int64PtrTy,
0, 0, 0),
0U);
// Middle pointer big enough -> bitcast.
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
CastInst::PtrToInt,
Int64Ty, Int64PtrTy, Int64Ty,
0, Int64Ty, 0),
CastInst::BitCast);
// Middle pointer too small -> fail.
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
CastInst::PtrToInt,
Int64Ty, Int64PtrTy, Int64Ty,
0, Int32Ty, 0),
0U);
// Test that we don't eliminate bitcasts between different address spaces,
// or if we don't have available pointer size information.
DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
"-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
"-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128");
Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
// Fail since the ptr int types are not provided
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
CastInst::BitCast,
Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
0, 0, 0),
0U);
// Fail since the the bitcast is between different sized address spaces
EXPECT_EQ(CastInst::isEliminableCastPair(
CastInst::IntToPtr,
CastInst::BitCast,
Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
0, Int16SizePtr, Int64SizePtr),
0U);
// Fail since the the bitcast is between different sized address spaces
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
CastInst::BitCast,
Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
0, Int16SizePtr, Int64SizePtr),
0U);
// Pass since the bitcast address spaces are the same
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
CastInst::BitCast,
Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
0, 0, 0),
CastInst::IntToPtr);
// Fail without known pointer sizes and different address spaces
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::BitCast,
CastInst::PtrToInt,
Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
0, 0, 0),
0U);
// Pass since the address spaces are the same, even though the pointer sizes
// are unknown
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::BitCast,
CastInst::PtrToInt,
Int64PtrTyAS1, Int64PtrTyAS1, Int32Ty,
0, 0, 0),
Instruction::PtrToInt);
// Fail since the bitcast is the wrong size
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::BitCast,
CastInst::PtrToInt,
Int64PtrTyAS1, Int64PtrTyAS2, Int64Ty,
Int16SizePtr, Int64SizePtr, 0),
0U);
}
} // end anonymous namespace
} // end namespace llvm