1 ; "PLAIN" - No optimizations. This tests the target-independent
3 ; RUN: opt -S -o - %s | FileCheck --check-prefix=PLAIN %s
5 target datalayout = "e-p:128:128:128-p1:32:32:32-p2:8:8:8-p3:16:16:16-p4:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:32"
7 ; The automatic constant folder in opt does not have targetdata access, so
8 ; it can't fold gep arithmetic, in general. However, the constant folder run
9 ; from instcombine and global opt can use targetdata.
10 ; PLAIN: @G8 = global ptr addrspace(1) getelementptr (i8, ptr addrspace(1) inttoptr (i32 1 to ptr addrspace(1)), i32 -1)
11 @G8 = global ptr addrspace(1) getelementptr (i8, ptr addrspace(1) inttoptr (i32 1 to ptr addrspace(1)), i32 -1)
12 ; PLAIN: @G1 = global ptr addrspace(2) getelementptr (i1, ptr addrspace(2) inttoptr (i8 1 to ptr addrspace(2)), i8 -1)
13 @G1 = global ptr addrspace(2) getelementptr (i1, ptr addrspace(2) inttoptr (i8 1 to ptr addrspace(2)), i8 -1)
14 ; PLAIN: @F8 = global ptr addrspace(1) getelementptr (i8, ptr addrspace(1) inttoptr (i32 1 to ptr addrspace(1)), i32 -2)
15 @F8 = global ptr addrspace(1) getelementptr (i8, ptr addrspace(1) inttoptr (i32 1 to ptr addrspace(1)), i32 -2)
16 ; PLAIN: @F1 = global ptr addrspace(2) getelementptr (i1, ptr addrspace(2) inttoptr (i8 1 to ptr addrspace(2)), i8 -2)
17 @F1 = global ptr addrspace(2) getelementptr (i1, ptr addrspace(2) inttoptr (i8 1 to ptr addrspace(2)), i8 -2)
18 ; PLAIN: @H8 = global ptr addrspace(1) getelementptr (i8, ptr addrspace(1) null, i32 -1)
19 @H8 = global ptr addrspace(1) getelementptr (i8, ptr addrspace(1) inttoptr (i32 0 to ptr addrspace(1)), i32 -1)
20 ; PLAIN: @H1 = global ptr addrspace(2) getelementptr (i1, ptr addrspace(2) null, i8 -1)
21 @H1 = global ptr addrspace(2) getelementptr (i1, ptr addrspace(2) inttoptr (i8 0 to ptr addrspace(2)), i8 -1)
24 ; The target-independent folder should be able to do some clever
25 ; simplifications on sizeof, alignof, and offsetof expressions. The
26 ; target-dependent folder should fold these down to constants.
27 ; PLAIN-X: @a = constant i64 mul (i64 ptrtoint (ptr addrspace(4) getelementptr (double, ptr addrspace(4) null, i32 1) to i64), i64 2310)
28 @a = constant i64 mul (i64 3, i64 mul (i64 ptrtoint (ptr addrspace(4) getelementptr ({[7 x double], [7 x double]}, ptr addrspace(4) null, i64 11) to i64), i64 5))
30 ; PLAIN-X: @b = constant i64 ptrtoint (ptr addrspace(4) getelementptr ({ i1, double }, ptr null, i64 0, i32 1) to i64)
31 @b = constant i64 ptrtoint (ptr addrspace(4) getelementptr ({i1, [13 x double]}, ptr addrspace(4) null, i64 0, i32 1) to i64)
33 ; PLAIN-X: @c = constant i64 mul nuw (i64 ptrtoint (ptr addrspace(4) getelementptr (double, ptr addrspace(4) null, i32 1) to i64), i64 2)
34 @c = constant i64 ptrtoint (ptr addrspace(4) getelementptr ({double, double, double, double}, ptr addrspace(4) null, i64 0, i32 2) to i64)
36 ; PLAIN-X: @d = constant i64 mul nuw (i64 ptrtoint (ptr addrspace(4) getelementptr (double, ptr addrspace(4) null, i32 1) to i64), i64 11)
37 @d = constant i64 ptrtoint (ptr addrspace(4) getelementptr ([13 x double], ptr addrspace(4) null, i64 0, i32 11) to i64)
39 ; PLAIN-X: @e = constant i64 ptrtoint (ptr addrspace(4) getelementptr ({ double, float, double, double }, ptr null, i64 0, i32 2) to i64)
40 @e = constant i64 ptrtoint (ptr addrspace(4) getelementptr ({double, float, double, double}, ptr addrspace(4) null, i64 0, i32 2) to i64)
42 ; PLAIN-X: @f = constant i64 1
43 @f = constant i64 ptrtoint (ptr addrspace(4) getelementptr ({i1, <{ i16, i128 }>}, ptr addrspace(4) null, i64 0, i32 1) to i64)
45 ; PLAIN-X: @g = constant i64 ptrtoint (ptr addrspace(4) getelementptr ({ i1, double }, ptr null, i64 0, i32 1) to i64)
46 @g = constant i64 ptrtoint (ptr addrspace(4) getelementptr ({i1, {double, double}}, ptr addrspace(4) null, i64 0, i32 1) to i64)
48 ; PLAIN-X: @h = constant i64 ptrtoint (ptr addrspace(2) getelementptr (i1, ptr addrspace(2) null, i32 1) to i64)
49 @h = constant i64 ptrtoint (ptr addrspace(4) getelementptr (double, ptr addrspace(4) null, i64 1) to i64)
51 ; PLAIN-X: @i = constant i64 ptrtoint (ptr addrspace(2) getelementptr ({ i1, ptr addrspace(2) }, ptr null, i64 0, i32 1) to i64)
52 @i = constant i64 ptrtoint (ptr addrspace(4) getelementptr ({i1, double}, ptr addrspace(4) null, i64 0, i32 1) to i64)
54 ; The target-dependent folder should cast GEP indices to integer-sized pointers.
56 ; PLAIN: @M = constant ptr addrspace(4) getelementptr (i64, ptr addrspace(4) null, i32 1)
57 ; PLAIN: @N = constant ptr addrspace(4) getelementptr ({ i64, i64 }, ptr addrspace(4) null, i32 0, i32 1)
58 ; PLAIN: @O = constant ptr addrspace(4) getelementptr ([2 x i64], ptr addrspace(4) null, i32 0, i32 1)
60 @M = constant ptr addrspace(4) getelementptr (i64, ptr addrspace(4) null, i32 1)
61 @N = constant ptr addrspace(4) getelementptr ({ i64, i64 }, ptr addrspace(4) null, i32 0, i32 1)
62 @O = constant ptr addrspace(4) getelementptr ([2 x i64], ptr addrspace(4) null, i32 0, i32 1)
64 ; Fold GEP of a GEP. Very simple cases are folded.
66 ; PLAIN-X: @Y = global ptraddrspace(3) getelementptr inbounds ([3 x { i32, i32 }], ptraddrspace(3) @ext, i64 2)
67 @ext = external addrspace(3) global [3 x { i32, i32 }]
68 @Y = global ptr addrspace(3) getelementptr inbounds ([3 x { i32, i32 }], ptr addrspace(3) getelementptr inbounds ([3 x { i32, i32 }], ptr addrspace(3) @ext, i64 1), i64 1)
70 ; PLAIN-X: @Z = global ptraddrspace(3) getelementptr inbounds (i32, ptr addrspace(3) getelementptr inbounds ([3 x { i32, i32 }], ptr addrspace(3) @ext, i64 0, i64 1, i32 0), i64 1)
71 @Z = global ptr addrspace(3) getelementptr inbounds (i32, ptr addrspace(3) getelementptr inbounds ([3 x { i32, i32 }], ptr addrspace(3) @ext, i64 0, i64 1, i32 0), i64 1)
74 ; Duplicate all of the above as function return values rather than
75 ; global initializers.
77 ; PLAIN: define ptr addrspace(1) @goo8() #0 {
78 ; PLAIN: %t = bitcast ptr addrspace(1) getelementptr (i8, ptr addrspace(1) inttoptr (i32 1 to ptr addrspace(1)), i32 -1) to ptr addrspace(1)
79 ; PLAIN: ret ptr addrspace(1) %t
81 ; PLAIN: define ptr addrspace(2) @goo1() #0 {
82 ; PLAIN: %t = bitcast ptr addrspace(2) getelementptr (i1, ptr addrspace(2) inttoptr (i32 1 to ptr addrspace(2)), i32 -1) to ptr addrspace(2)
83 ; PLAIN: ret ptr addrspace(2) %t
85 ; PLAIN: define ptr addrspace(1) @foo8() #0 {
86 ; PLAIN: %t = bitcast ptr addrspace(1) getelementptr (i8, ptr addrspace(1) inttoptr (i32 1 to ptr addrspace(1)), i32 -2) to ptr addrspace(1)
87 ; PLAIN: ret ptr addrspace(1) %t
89 ; PLAIN: define ptr addrspace(2) @foo1() #0 {
90 ; PLAIN: %t = bitcast ptr addrspace(2) getelementptr (i1, ptr addrspace(2) inttoptr (i32 1 to ptr addrspace(2)), i32 -2) to ptr addrspace(2)
91 ; PLAIN: ret ptr addrspace(2) %t
93 ; PLAIN: define ptr addrspace(1) @hoo8() #0 {
94 ; PLAIN: %t = bitcast ptr addrspace(1) getelementptr (i8, ptr addrspace(1) null, i32 -1) to ptr addrspace(1)
95 ; PLAIN: ret ptr addrspace(1) %t
97 ; PLAIN: define ptr addrspace(2) @hoo1() #0 {
98 ; PLAIN: %t = bitcast ptr addrspace(2) getelementptr (i1, ptr addrspace(2) null, i32 -1) to ptr addrspace(2)
99 ; PLAIN: ret ptr addrspace(2) %t
101 define ptr addrspace(1) @goo8() #0 {
102 %t = bitcast ptr addrspace(1) getelementptr (i8, ptr addrspace(1) inttoptr (i32 1 to ptr addrspace(1)), i32 -1) to ptr addrspace(1)
103 ret ptr addrspace(1) %t
105 define ptr addrspace(2) @goo1() #0 {
106 %t = bitcast ptr addrspace(2) getelementptr (i1, ptr addrspace(2) inttoptr (i32 1 to ptr addrspace(2)), i32 -1) to ptr addrspace(2)
107 ret ptr addrspace(2) %t
109 define ptr addrspace(1) @foo8() #0 {
110 %t = bitcast ptr addrspace(1) getelementptr (i8, ptr addrspace(1) inttoptr (i32 1 to ptr addrspace(1)), i32 -2) to ptr addrspace(1)
111 ret ptr addrspace(1) %t
113 define ptr addrspace(2) @foo1() #0 {
114 %t = bitcast ptr addrspace(2) getelementptr (i1, ptr addrspace(2) inttoptr (i32 1 to ptr addrspace(2)), i32 -2) to ptr addrspace(2)
115 ret ptr addrspace(2) %t
117 define ptr addrspace(1) @hoo8() #0 {
118 %t = bitcast ptr addrspace(1) getelementptr (i8, ptr addrspace(1) inttoptr (i32 0 to ptr addrspace(1)), i32 -1) to ptr addrspace(1)
119 ret ptr addrspace(1) %t
121 define ptr addrspace(2) @hoo1() #0 {
122 %t = bitcast ptr addrspace(2) getelementptr (i1, ptr addrspace(2) inttoptr (i32 0 to ptr addrspace(2)), i32 -1) to ptr addrspace(2)
123 ret ptr addrspace(2) %t
126 ; PLAIN-X: define i64 @fa() #0 {
127 ; PLAIN-X: %t = bitcast i64 mul (i64 ptrtoint (ptr addrspace(4) getelementptr (double, ptr addrspace(4) null, i32 1) to i64), i64 2310) to i64
128 ; PLAIN-X: ret i64 %t
130 ; PLAIN-X: define i64 @fb() #0 {
131 ; PLAIN-X: %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr ({ i1, double }, ptr null, i64 0, i32 1) to i64) to i64
132 ; PLAIN-X: ret i64 %t
134 ; PLAIN-X: define i64 @fc() #0 {
135 ; PLAIN-X: %t = bitcast i64 mul nuw (i64 ptrtoint (ptr addrspace(4) getelementptr (double, ptr addrspace(4) null, i32 1) to i64), i64 2) to i64
136 ; PLAIN-X: ret i64 %t
138 ; PLAIN-X: define i64 @fd() #0 {
139 ; PLAIN-X: %t = bitcast i64 mul nuw (i64 ptrtoint (ptr addrspace(4) getelementptr (double, ptr addrspace(4) null, i32 1) to i64), i64 11) to i64
140 ; PLAIN-X: ret i64 %t
142 ; PLAIN-X: define i64 @fe() #0 {
143 ; PLAIN-X: %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr ({ double, float, double, double }, ptr null, i64 0, i32 2) to i64) to i64
144 ; PLAIN-X: ret i64 %t
146 ; PLAIN-X: define i64 @ff() #0 {
147 ; PLAIN-X: %t = bitcast i64 1 to i64
148 ; PLAIN-X: ret i64 %t
150 ; PLAIN-X: define i64 @fg() #0 {
151 ; PLAIN-X: %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr ({ i1, double }, ptr null, i64 0, i32 1) to i64) to i64
152 ; PLAIN-X: ret i64 %t
154 ; PLAIN-X: define i64 @fh() #0 {
155 ; PLAIN-X: %t = bitcast i64 ptrtoint (ptr addrspace(2) getelementptr (i1, ptr addrspace(2) null, i32 1) to i64) to i64
156 ; PLAIN-X: ret i64 %t
158 ; PLAIN-X: define i64 @fi() #0 {
159 ; PLAIN-X: %t = bitcast i64 ptrtoint (ptr addrspace(2) getelementptr ({ i1, ptr addrspace(2) }, ptr null, i64 0, i32 1) to i64) to i64
160 ; PLAIN-X: ret i64 %t
162 define i64 @fa() #0 {
163 %t = bitcast i64 mul (i64 3, i64 mul (i64 ptrtoint (ptr getelementptr ({[7 x double], [7 x double]}, ptr null, i64 11) to i64), i64 5)) to i64
166 define i64 @fb() #0 {
167 %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr ({i1, [13 x double]}, ptr addrspace(4) null, i64 0, i32 1) to i64) to i64
170 define i64 @fc() #0 {
171 %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr ({double, double, double, double}, ptr addrspace(4) null, i64 0, i32 2) to i64) to i64
174 define i64 @fd() #0 {
175 %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr ([13 x double], ptr addrspace(4) null, i64 0, i32 11) to i64) to i64
178 define i64 @fe() #0 {
179 %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr ({double, float, double, double}, ptr addrspace(4) null, i64 0, i32 2) to i64) to i64
182 define i64 @ff() #0 {
183 %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr ({i1, <{ i16, i128 }>}, ptr addrspace(4) null, i64 0, i32 1) to i64) to i64
186 define i64 @fg() #0 {
187 %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr ({i1, {double, double}}, ptr addrspace(4) null, i64 0, i32 1) to i64) to i64
190 define i64 @fh() #0 {
191 %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr (double, ptr addrspace(4) null, i32 1) to i64) to i64
194 define i64 @fi() #0 {
195 %t = bitcast i64 ptrtoint (ptr addrspace(4) getelementptr ({i1, double}, ptr addrspace(4) null, i64 0, i32 1) to i64) to i64
199 ; PLAIN: define ptr @fM() #0 {
200 ; PLAIN: %t = bitcast ptr getelementptr (i64, ptr null, i32 1) to ptr
203 ; PLAIN: define ptr @fN() #0 {
204 ; PLAIN: %t = bitcast ptr getelementptr ({ i64, i64 }, ptr null, i32 0, i32 1) to ptr
207 ; PLAIN: define ptr @fO() #0 {
208 ; PLAIN: %t = bitcast ptr getelementptr ([2 x i64], ptr null, i32 0, i32 1) to ptr
212 define ptr @fM() #0 {
213 %t = bitcast ptr getelementptr (i64, ptr null, i32 1) to ptr
216 define ptr @fN() #0 {
217 %t = bitcast ptr getelementptr ({ i64, i64 }, ptr null, i32 0, i32 1) to ptr
220 define ptr @fO() #0 {
221 %t = bitcast ptr getelementptr ([2 x i64], ptr null, i32 0, i32 1) to ptr
225 ; PLAIN: define ptr addrspace(1) @fZ() #0 {
226 ; PLAIN: %t = bitcast ptr addrspace(1) getelementptr inbounds (i32, ptr addrspace(1) getelementptr inbounds ([3 x { i32, i32 }], ptr addrspace(1) @ext2, i64 0, i64 1, i32 0), i64 1) to ptr addrspace(1)
227 ; PLAIN: ret ptr addrspace(1) %t
229 @ext2 = external addrspace(1) global [3 x { i32, i32 }]
230 define ptr addrspace(1) @fZ() #0 {
231 %t = bitcast ptr addrspace(1) getelementptr inbounds (i32, ptr addrspace(1) getelementptr inbounds ([3 x { i32, i32 }], ptr addrspace(1) @ext2, i64 0, i64 1, i32 0), i64 1) to ptr addrspace(1)
232 ret ptr addrspace(1) %t
235 attributes #0 = { nounwind }