-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.ll
536 lines (451 loc) · 12.6 KB
/
test.ll
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
; ModuleID = 'test.ll'
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128"
target triple = "i386-pc-linux-gnu"
define i32 @test_return(i32 %arg) {
ret i32 123
}
define i64 @test_return_i64() {
ret i64 1234100100100
}
define i1 @test_return_i1() {
ret i1 1
}
define double @test_return_double() {
ret double 123.456
}
define i32 @test_return_undef() {
ret i32 undef
}
define i64 @test_i64_arg1(i64 %arg1, i64 %arg2) {
ret i64 %arg1
}
define i64 @test_i64_arg2(i64 %arg1, i64 %arg2) {
ret i64 %arg2
}
define i32 @test_add(i32 %arg) {
%1 = add nsw i32 %arg, 100
ret i32 %1
}
define i32 @test_sub(i32 %arg) {
%1 = sub nsw i32 1000, %arg
ret i32 %1
}
define i64 @test_load_int64(i64* %ptr) {
%1 = load i64* %ptr
ret i64 %1
}
define i32 @test_load_int32(i32* %ptr) {
%1 = load i32* %ptr
ret i32 %1
}
define i16 @test_load_int16(i16* %ptr) {
%1 = load i16* %ptr
ret i16 %1
}
define i8 @test_load_int8(i8* %ptr) {
%1 = load i8* %ptr
ret i8 %1
}
define void @test_store_int64(i64* %ptr, i64 %value) {
store i64 %value, i64* %ptr
ret void
}
define void @test_store_int32(i32* %ptr, i32 %value) {
store i32 %value, i32* %ptr
ret void
}
define void @test_store_int16(i16* %ptr, i16 %value) {
store i16 %value, i16* %ptr
ret void
}
define void @test_store_int8(i8* %ptr, i8 %value) {
store i8 %value, i8* %ptr
ret void
}
define i32* @test_load_ptr(i32** %ptr) {
%1 = load i32** %ptr
ret i32* %1
}
define i1 @test_compare(i32 %arg) {
%1 = icmp eq i32 %arg, 99
ret i1 %1
}
define i1 @test_compare_ptr(i32* %arg1, i32* %arg2) {
%1 = icmp eq i32* %arg1, %arg2
ret i1 %1
}
define i32 @test_branch(i32 %arg) {
br label %label
label:
ret i32 101
}
define i32 @test_conditional(i32 %arg) {
entry:
%cmp = icmp eq i32 %arg, 99
br i1 %cmp, label %iftrue, label %iffalse
iftrue:
%ret1 = phi i32 [ 123, %entry ]
ret i32 %ret1
iffalse:
%ret2 = phi i32 [ 456, %entry ]
ret i32 %ret2
}
define i32 @test_conditional_with_i1_overflow(i32 %arg) {
%cond = trunc i32 %arg to i1
br i1 %cond, label %iftrue, label %iffalse
iftrue:
ret i32 1
iffalse:
ret i32 0
}
define i32 @test_switch(i32 %arg) {
entry:
switch i32 %arg, label %default [
i32 1, label %match1
i32 5, label %match5
]
match1:
%ret1 = phi i32 [ 10, %entry ]
ret i32 %ret1
match5:
%ret5 = phi i32 [ 50, %entry ]
ret i32 %ret5
default:
%ret999 = phi i32 [ 999, %entry ]
ret i32 %ret999
}
define i32 @test_phi(i32 %arg) {
%1 = icmp eq i32 %arg, 99
br i1 %1, label %iftrue, label %iffalse
iftrue:
br label %return
iffalse:
br label %return
return:
%2 = phi i32 [ 123, %iftrue ], [ 456, %iffalse ]
ret i32 %2
}
define i64 @test_phi_i64(i64 %arg) {
%1 = icmp eq i64 %arg, 99
br i1 %1, label %iftrue, label %iffalse
iftrue:
br label %return
iffalse:
br label %return
return:
%2 = phi i64 [ 123, %iftrue ], [ 456, %iffalse ]
ret i64 %2
}
define i32 @test_select(i32 %arg) {
%1 = icmp eq i32 %arg, 99
%2 = select i1 %1, i32 123, i32 456
ret i32 %2
}
define i32 @test_call(i32 (i32, i32)* %func, i32 %arg1, i32 %arg2) {
%1 = call i32 %func(i32 %arg1, i32 %arg2)
%2 = add i32 %1, 1000
ret i32 %2
}
define i32 @test_call2(i32 (i32, i32)* %func, i32 %arg1, i32 %arg2) {
%1 = call i32 %func(i32 %arg1, i32 %arg2)
; We are checking that the args don't clobber %1.
%2 = call i32 %func(i32 0, i32 0)
ret i32 %1
}
define i64 @test_call_i64(i64 (i64, i64)* %func, i64 %arg1, i64 %arg2) {
%1 = call i64 %func(i64 %arg1, i64 %arg2)
ret i64 %1
}
define i32 @test_direct_call() {
%1 = call i32 @test_return(i32 0)
ret i32 %1
}
@global1 = global i32 124
define i32* @get_global() {
ret i32* @global1
}
@string = constant [7 x i8] c"Hello!\00"
define i8* @get_global_string() {
ret i8* getelementptr ([7 x i8]* @string, i32 0, i32 0)
}
@array = constant [3 x [2 x i16]]
[[2 x i16] [i16 1, i16 2],
[2 x i16] [i16 3, i16 4],
[2 x i16] [i16 5, i16 6]]
@ptr_reloc = global i32* @global1
@ptr_zero = global i32* null
%MyStruct = type { i8, i32, i8 }
@struct_val = global %MyStruct { i8 11, i32 22, i8 33 }
@struct_zero_init = global %MyStruct zeroinitializer
; Need to handle "undef": Clang generates it for the padding at the
; end of a struct.
@undef_init = global [8 x i8] undef
@global_getelementptr = global i8* getelementptr (%MyStruct* null, i32 0, i32 2)
@global_i64 = global i64 1234100100100
; TODO: Disallow extern_weak global variables instead.
@__ehdr_start = extern_weak global i8
define i8* @get_weak_global() {
ret i8* @__ehdr_start
}
define i32 @test_alloca() {
%addr = alloca i32
store i32 125, i32* %addr
%1 = load i32* %addr
ret i32 %1
}
define void @func_with_args(i32 %arg1, i32 %arg2) {
ret void
}
define i32 @test_alloca2() {
%addr = alloca i32
store i32 125, i32* %addr
; We are checking that the args don't clobber %addr.
call void @func_with_args(i32 98, i32 99)
%1 = load i32* %addr
ret i32 %1
}
declare void @llvm.lifetime.start(i64, i8* nocapture) nounwind
declare void @llvm.lifetime.end(i64, i8* nocapture) nounwind
define void @test_lifetime_start_and_end() {
%addr = alloca i8
call void @llvm.lifetime.start(i64 1, i8* %addr)
call void @llvm.lifetime.end(i64 1, i8* %addr)
ret void
}
define i32* @test_bitcast(i8* %arg) {
%1 = bitcast i8* %arg to i32*
ret i32* %1
}
define i8* @test_bitcast_global() {
%ptr = bitcast i32** @ptr_reloc to i8*
ret i8* %ptr
}
; TODO: Generate all these variants
; Zero-extension
define i32 @test_zext16(i32 %arg) {
%1 = trunc i32 %arg to i16
%2 = zext i16 %1 to i32
ret i32 %2
}
define i32 @test_zext8(i32 %arg) {
%1 = trunc i32 %arg to i8
%2 = zext i8 %1 to i32
ret i32 %2
}
define i32 @test_zext1(i32 %arg) {
%1 = trunc i32 %arg to i1
%2 = zext i1 %1 to i32
ret i32 %2
}
define i64 @test_zext_32_to_64(i32 %arg) {
%1 = zext i32 %arg to i64
ret i64 %1
}
define i64 @test_zext_16_to_64(i16 %arg) {
%1 = zext i16 %arg to i64
ret i64 %1
}
; Sign-extension
define i32 @test_sext16(i32 %arg) {
%1 = trunc i32 %arg to i16
%2 = sext i16 %1 to i32
ret i32 %2
}
define i32 @test_sext8(i32 %arg) {
%1 = trunc i32 %arg to i8
%2 = sext i8 %1 to i32
ret i32 %2
}
define i32 @test_sext1(i32 %arg) {
%1 = trunc i32 %arg to i1
%2 = sext i1 %1 to i32
ret i32 %2
}
define i64 @test_sext_32_to_64(i32 %arg) {
%1 = sext i32 %arg to i64
ret i64 %1
}
define i64 @test_sext_16_to_64(i16 %arg) {
%1 = sext i16 %arg to i64
ret i64 %1
}
define i32 @test_ptrtoint(i8* %arg) {
%1 = ptrtoint i8* %arg to i32
ret i32 %1
}
define i8* @test_inttoptr(i32 %arg) {
%1 = inttoptr i32 %arg to i8*
ret i8* %1
}
define i8* @test_getelementptr1() {
%addr = getelementptr %MyStruct* @struct_val, i32 0, i32 2
ret i8* %addr
}
define i16* @test_getelementptr2() {
%addr = getelementptr [3 x [2 x i16]]* @array, i32 0, i32 2, i32 1
ret i16* %addr
}
define i16* @test_getelementptr_constantexpr() {
ret i16* getelementptr ([3 x [2 x i16]]* @array, i32 0, i32 2, i32 1)
}
define i8* @test_bitcast_constantexpr() {
ret i8* bitcast ([3 x [2 x i16]]* @array to i8*)
}
define i32 @test_ptrtoint_constantexpr() {
ret i32 ptrtoint ([3 x [2 x i16]]* @array to i32)
}
define i8* @test_inttoptr_constantexpr() {
ret i8* inttoptr (i32 123456 to i8*)
}
@constexpr_var1 = global i32 0
@constexpr_var2 = global i32 0
@constexpr_var3 = global i32 0
define i1 @test_icmp_lt_constantexpr() {
; Don't use "icmp eq" here because it gets constant folded when reading
; the bitcode!
ret i1 icmp ult (i32* @constexpr_var1, i32* @constexpr_var2)
}
; Test that ConstantExprs are expanded out recursively.
define i32 @test_add_constantexpr_nested() {
ret i32 add (i32 ptrtoint (i32* @constexpr_var1 to i32),
i32 add (i32 ptrtoint (i32* @constexpr_var2 to i32),
i32 ptrtoint (i32* @constexpr_var3 to i32)))
}
; This tests that ExpandConstantExpr handles PHI nodes correctly.
define i32 @test_add_constantexpr_phi1() {
entry:
br label %label
label:
%val = phi i32 [ add (i32 ptrtoint (i32* @constexpr_var1 to i32),
i32 ptrtoint (i32* @constexpr_var2 to i32)), %entry ]
ret i32 %val
}
; This tests that ExpandConstantExpr correctly handles a PHI node that
; contains the same ConstantExpr twice.
; Using replaceAllUsesWith() is not correct on a PHI node when the
; new instruction has to be added to an incoming block.
define i32 @test_add_constantexpr_phi2(i32 %arg) {
entry:
switch i32 %arg, label %exit [
i32 1, label %match1
i32 2, label %match2
]
match1:
br label %exit
match2:
br label %exit
exit:
%val = phi i32 [ add (i32 ptrtoint (i32* @constexpr_var1 to i32),
i32 ptrtoint (i32* @constexpr_var2 to i32)), %match1 ],
[ add (i32 ptrtoint (i32* @constexpr_var1 to i32),
i32 ptrtoint (i32* @constexpr_var2 to i32)), %match2 ],
[ 456, %entry ]
ret i32 %val
}
; Check that getelementptr is expanded out correctly when it appears
; in a phi node.
define i32* @test_getelementptr_constantexpr_phi() {
entry:
br label %done
done:
%val = phi i32* [ getelementptr (i32* @constexpr_var1, i32 1), %entry ]
ret i32* %val
}
declare void @llvm.memcpy.p0i8.p0i8.i32(i8*, i8*, i32, i32, i1)
declare void @llvm.memmove.p0i8.p0i8.i32(i8*, i8*, i32, i32, i1)
declare void @llvm.memset.p0i8.i32(i8*, i8, i32, i32, i1)
declare void @llvm.memset.p0i8.i64(i8*, i8, i64, i32, i1)
define void @test_memcpy(i8* %dest, i8* %src, i32 %size) {
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dest, i8* %src, i32 %size,
i32 1, i1 false)
ret void
}
define void @test_memmove(i8* %dest, i8* %src, i32 %size) {
call void @llvm.memmove.p0i8.p0i8.i32(i8* %dest, i8* %src, i32 %size,
i32 1, i1 false)
ret void
}
define void @test_memset(i8* %dest, i8 %val, i32 %size) {
call void @llvm.memset.p0i8.i32(i8* %dest, i8 %val, i32 %size,
i32 1, i1 false)
ret void
}
define void @test_memset_i64_size(i8* %dest, i8 %val, i32 %size) {
%size64 = zext i32 %size to i64
call void @llvm.memset.p0i8.i64(i8* %dest, i8 %val, i64 %size64,
i32 1, i1 false)
ret void
}
declare i8* @llvm.nacl.read.tp()
define i8* @test_nacl_read_tp() {
%1 = call i8* @llvm.nacl.read.tp()
ret i8* %1
}
define void @test_unreachable() {
unreachable
}
define i32 @test_atomicrmw_i32_xchg(i32* %ptr, i32 %val) {
%1 = atomicrmw xchg i32* %ptr, i32 %val seq_cst
ret i32 %1
}
define i32 @test_atomicrmw_i32_add(i32* %ptr, i32 %val) {
%1 = atomicrmw add i32* %ptr, i32 %val seq_cst
ret i32 %1
}
declare void @llvm.va_start(i8*)
declare void @llvm.va_end(i8*)
declare void @llvm.va_copy(i8*, i8*)
; PNaCl's va_list is currently 16 bytes.
%va_list = type [4 x i32]
define i32 @varargs_func(i32 %arg, i32* %res1, i64* %res2, i32* %res3, ...) {
%arglist_alloc = alloca %va_list
%arglist = bitcast %va_list* %arglist_alloc to i8*
call void @llvm.va_start(i8* %arglist)
%val1 = va_arg i8* %arglist, i32
store i32 %val1, i32* %res1
%val2 = va_arg i8* %arglist, i64
store i64 %val2, i64* %res2
%val3 = va_arg i8* %arglist, i32
store i32 %val3, i32* %res3
call void @llvm.va_end(i8* %arglist)
ret i32 %arg
}
define i32 @test_varargs_call(i32* %res1, i64* %res2, i32* %res3) {
%result = call i32 (i32, i32*, i64*, i32*, ...)*
@varargs_func(i32 1234,
i32* %res1, i64* %res2, i32* %res3,
i32 111, i64 222, i32 333)
ret i32 %result
}
define i32 @varargs_func_trivial(i32 %arg, ...) {
ret i32 %arg
}
define i32 @test_varargs_call_empty() {
%result = call i32 (i32, ...)* @varargs_func_trivial(i32 2345)
ret i32 %result
}
define i32 @va_copy_func(i32 %arg, ...) {
%arglist1_alloc = alloca %va_list
%arglist1 = bitcast %va_list* %arglist1_alloc to i8*
call void @llvm.va_start(i8* %arglist1)
%arglist2_alloc = alloca %va_list
%arglist2 = bitcast %va_list* %arglist2_alloc to i8*
call void @llvm.va_copy(i8* %arglist2, i8* %arglist1)
%result = va_arg i8* %arglist2, i32
call void @llvm.va_end(i8* %arglist1)
call void @llvm.va_end(i8* %arglist2)
ret i32 %result
}
define i32 @test_varargs_va_copy() {
%result = call i32 (i32, ...)* @va_copy_func(i32 63, i32 5678)
ret i32 %result
}
; These are no-op intrinsics for our code generator.
declare void @llvm.dbg.value(metadata, i64, metadata)
declare void @llvm.dbg.declare(metadata, metadata)
define void @test_debug_info_intrinsics(i32 %arg) {
call void @llvm.dbg.value(metadata !{i32 %arg}, i64 0, metadata !{})
%localvar = alloca i32
call void @llvm.dbg.declare(metadata !{i32* %localvar}, metadata !{})
ret void
}