llvm.org GIT mirror llvm / 562b1d8
llvm/ConstantFolding.cpp: Make ReadDataFromGlobal() and FoldReinterpretLoadFromConstPtr() Big-endian-aware. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@167595 91177308-0d34-0410-b5e6-96231b3b80d8 NAKAMURA Takumi 8 years ago
5 changed file(s) with 133 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
291291 unsigned IntBytes = unsigned(CI->getBitWidth()/8);
292292
293293 for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
294 CurPtr[i] = (unsigned char)(Val >> (ByteOffset * 8));
294 int n = ByteOffset;
295 if (!TD.isLittleEndian())
296 n = IntBytes - n - 1;
297 CurPtr[i] = (unsigned char)(Val >> (n * 8));
295298 ++ByteOffset;
296299 }
297300 return true;
441444 BytesLoaded, TD))
442445 return 0;
443446
444 APInt ResultVal = APInt(IntType->getBitWidth(), RawBytes[BytesLoaded-1]);
445 for (unsigned i = 1; i != BytesLoaded; ++i) {
446 ResultVal <<= 8;
447 ResultVal |= RawBytes[BytesLoaded-1-i];
447 APInt ResultVal = APInt(IntType->getBitWidth(), 0);
448 if (TD.isLittleEndian()) {
449 ResultVal = RawBytes[BytesLoaded - 1];
450 for (unsigned i = 1; i != BytesLoaded; ++i) {
451 ResultVal <<= 8;
452 ResultVal |= RawBytes[BytesLoaded-1-i];
453 }
454 } else {
455 ResultVal = RawBytes[0];
456 for (unsigned i = 1; i != BytesLoaded; ++i) {
457 ResultVal <<= 8;
458 ResultVal |= RawBytes[i];
459 }
448460 }
449461
450462 return ConstantInt::get(IntType->getContext(), ResultVal);
520532 }
521533 }
522534
523 // Try hard to fold loads from bitcasted strange and non-type-safe things. We
524 // currently don't do any of this for big endian systems. It can be
525 // generalized in the future if someone is interested.
526 if (TD && TD->isLittleEndian())
535 // Try hard to fold loads from bitcasted strange and non-type-safe things.
536 if (TD)
527537 return FoldReinterpretLoadFromConstPtr(CE, *TD);
528538 return 0;
529539 }
None ; RUN: opt < %s -instcombine -S | FileCheck %s
1
2 target datalayout = "e-p: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"
3
0 ; RUN: opt < %s -default-data-layout="e-p: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" -instcombine -S | FileCheck %s --check-prefix=LE
1 ; RUN: opt < %s -default-data-layout="E-p: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" -instcombine -S | FileCheck %s --check-prefix=BE
2
3 ; {{ 0xDEADBEEF, 0xBA }, 0xCAFEBABE}
44 @g1 = constant {{i32,i8},i32} {{i32,i8} { i32 -559038737, i8 186 }, i32 -889275714 }
55 @g2 = constant double 1.0
6 ; { 0x7B, 0x06B1BFF8 }
67 @g3 = constant {i64, i64} { i64 123, i64 112312312 }
78
89 ; Simple load
910 define i32 @test1() {
1011 %r = load i32* getelementptr ({{i32,i8},i32}* @g1, i32 0, i32 0, i32 0)
1112 ret i32 %r
12 ; CHECK: @test1
13 ; CHECK: ret i32 -559038737
13
14 ; 0xDEADBEEF
15 ; LE: @test1
16 ; LE: ret i32 -559038737
17
18 ; 0xDEADBEEF
19 ; BE: @test1
20 ; BE: ret i32 -559038737
1421 }
1522
1623 ; PR3152
1926 %r = load i16* bitcast(i32* getelementptr ({{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16*)
2027 ret i16 %r
2128
22 ; CHECK: @test2
23 ; CHECK: ret i16 -16657
29 ; 0xBEEF
30 ; LE: @test2
31 ; LE: ret i16 -16657
32
33 ; 0xDEAD
34 ; BE: @test2
35 ; BE: ret i16 -8531
2436 }
2537
2638 ; Load of second 16 bits of 32-bit value.
2840 %r = load i16* getelementptr(i16* bitcast(i32* getelementptr ({{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16*), i32 1)
2941 ret i16 %r
3042
31 ; CHECK: @test3
32 ; CHECK: ret i16 -8531
43 ; 0xDEAD
44 ; LE: @test3
45 ; LE: ret i16 -8531
46
47 ; 0xBEEF
48 ; BE: @test3
49 ; BE: ret i16 -16657
3350 }
3451
3552 ; Load of 8 bit field + tail padding.
3653 define i16 @test4() {
3754 %r = load i16* getelementptr(i16* bitcast(i32* getelementptr ({{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16*), i32 2)
3855 ret i16 %r
39 ; CHECK: @test4
40 ; CHECK: ret i16 186
56
57 ; 0x00BA
58 ; LE: @test4
59 ; LE: ret i16 186
60
61 ; 0xBA00
62 ; BE: @test4
63 ; BE: ret i16 -17920
4164 }
4265
4366 ; Load of double bits.
4568 %r = load i64* bitcast(double* @g2 to i64*)
4669 ret i64 %r
4770
48 ; CHECK: @test6
49 ; CHECK: ret i64 4607182418800017408
71 ; 0x3FF_0000000000000
72 ; LE: @test6
73 ; LE: ret i64 4607182418800017408
74
75 ; 0x3FF_0000000000000
76 ; BE: @test6
77 ; BE: ret i64 4607182418800017408
5078 }
5179
5280 ; Load of double bits.
5482 %r = load i16* bitcast(double* @g2 to i16*)
5583 ret i16 %r
5684
57 ; CHECK: @test7
58 ; CHECK: ret i16 0
85 ; 0x0000
86 ; LE: @test7
87 ; LE: ret i16 0
88
89 ; 0x3FF0
90 ; BE: @test7
91 ; BE: ret i16 16368
5992 }
6093
6194 ; Double load.
6396 %r = load double* bitcast({{i32,i8},i32}* @g1 to double*)
6497 ret double %r
6598
66 ; CHECK: @test8
67 ; CHECK: ret double 0xBADEADBEEF
99 ; LE: @test8
100 ; LE: ret double 0xBADEADBEEF
101
102 ; BE: @test8
103 ; BE: ret double 0xDEADBEEFBA000000
68104 }
69105
70106
73109 %r = load i128* bitcast({i64, i64}* @g3 to i128*)
74110 ret i128 %r
75111
76 ; CHECK: @test9
77 ; CHECK: ret i128 2071796475790618158476296315
112 ; 0x00000000_06B1BFF8_00000000_0000007B
113 ; LE: @test9
114 ; LE: ret i128 2071796475790618158476296315
115
116 ; 0x00000000_0000007B_00000000_06B1BFF8
117 ; BE: @test9
118 ; BE: ret i128 2268949521066387161080
78119 }
79120
80121 ; vector load.
82123 %r = load <2 x i64>* bitcast({i64, i64}* @g3 to <2 x i64>*)
83124 ret <2 x i64> %r
84125
85 ; CHECK: @test10
86 ; CHECK: ret <2 x i64>
126 ; LE: @test10
127 ; LE: ret <2 x i64>
128
129 ; BE: @test10
130 ; BE: ret <2 x i64>
87131 }
88132
89133
90134 ; PR5287
135 ; { 0xA1, 0x08 }
91136 @g4 = internal constant { i8, i8 } { i8 -95, i8 8 }
92137
93138 define i16 @test11() nounwind {
94139 entry:
95140 %a = load i16* bitcast ({ i8, i8 }* @g4 to i16*)
96141 ret i16 %a
97
98 ; CHECK: @test11
99 ; CHECK: ret i16 2209
142
143 ; 0x08A1
144 ; LE: @test11
145 ; LE: ret i16 2209
146
147 ; 0xA108
148 ; BE: @test11
149 ; BE: ret i16 -24312
100150 }
101151
102152
106156 define i16 @test12() {
107157 %a = load i16* getelementptr inbounds ([3 x i16]* bitcast ([6 x i8]* @test12g to [3 x i16]*), i32 0, i64 1)
108158 ret i16 %a
109 ; CHECK: @test12
110 ; CHECK: ret i16 98
159
160 ; 0x0062
161 ; LE: @test12
162 ; LE: ret i16 98
163
164 ; 0x6200
165 ; BE: @test12
166 ; BE: ret i16 25088
111167 }
112168
113169
116172 define i1 @test13() {
117173 %A = load i1* bitcast (i8* @g5 to i1*)
118174 ret i1 %A
119 ; CHECK: @test13
120 ; CHECK: ret i1 false
175
176 ; LE: @test13
177 ; LE: ret i1 false
178
179 ; BE: @test13
180 ; BE: ret i1 false
121181 }
122182
123183 @g6 = constant [2 x i8*] [i8* inttoptr (i64 1 to i8*), i8* inttoptr (i64 2 to i8*)]
125185 entry:
126186 %tmp = load i64* bitcast ([2 x i8*]* @g6 to i64*)
127187 ret i64 %tmp
128 ; CHECK: @test14
129 ; CHECK: ret i64 1
188
189 ; LE: @test14
190 ; LE: ret i64 1
191
192 ; BE: @test14
193 ; BE: ret i64 1
130194 }
131195
132196 define i64 @test15() nounwind {
133197 entry:
134198 %tmp = load i64* bitcast (i8** getelementptr inbounds ([2 x i8*]* @g6, i32 0, i64 1) to i64*)
135199 ret i64 %tmp
136 ; CHECK: @test15
137 ; CHECK: ret i64 2
138 }
200
201 ; LE: @test15
202 ; LE: ret i64 2
203
204 ; BE: @test15
205 ; BE: ret i64 2
206 }
None ; RUN: opt < %s -basicaa -gvn -S -die | FileCheck %s
1
2 ; 32-bit little endian target.
3 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:128:128-n8:16:32"
0 ; RUN: opt < %s -default-data-layout="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-n8:16:32" -basicaa -gvn -S -die | FileCheck %s
1 ; RUN: opt < %s -default-data-layout="E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-n32" -basicaa -gvn -S -die | FileCheck %s
42
53 ;; Trivial RLE test.
64 define i32 @test0(i32 %V, i32* %P) {
317315 %P4 = getelementptr i8* %P3, i32 2
318316 br i1 %cond, label %T, label %F
319317 T:
320 store i32 42, i32* %P
318 store i32 57005, i32* %P
321319 br label %Cont
322320
323321 F:
None ; RUN: opt < %s -instcombine -S | FileCheck %s
0 ; RUN: opt < %s -default-data-layout="e-p:32:32:32" -instcombine -S | FileCheck %s --check-prefix=LE
1 ; RUN: opt < %s -default-data-layout="E-p:32:32:32" -instcombine -S | FileCheck %s --check-prefix=BE
12 ; PR13442
2
3 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"
43
54 @test = constant [4 x i32] [i32 1, i32 2, i32 3, i32 4]
65
76 define i64 @foo() {
87 %ret = load i64* bitcast (i8* getelementptr (i8* bitcast ([4 x i32]* @test to i8*), i64 2) to i64*), align 1
98 ret i64 %ret
10 ; CHECK: ret i64 844424930263040
9 ; 0x00030000_00020000 in [01 00/00 00 02 00 00 00 03 00/00 00 04 00 00 00]
10 ; LE: ret i64 844424930263040
11 ; 0x00000200_00000300 in [00 00/00 01 00 00 00 02 00 00/00 03 00 00 00 04]
12 ; BE: ret i64 281474976841728
1113 }
0 ; This test makes sure that these instructions are properly constant propagated.
11
2 target datalayout = "e-p:32:32"
2 ; RUN: opt < %s -default-data-layout="e-p:32:32" -sccp -S | FileCheck %s
3 ; RUN: opt < %s -default-data-layout="E-p:32:32" -sccp -S | FileCheck %s
34
4 ; RUN: opt < %s -sccp -S | not grep load
5 ; CHECK-NOT: load
56
67
78 @X = constant i32 42 ; [#uses=1]