File: hoist-global-addr-base.ll

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (339 lines) | stat: -rw-r--r-- 11,444 bytes parent folder | download
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
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=riscv32  < %s | FileCheck  %s --check-prefixes=CHECK,RV32,RV32I
; RUN: llc -mtriple=riscv64  < %s | FileCheck  %s --check-prefixes=CHECK,RV64,RV64I
; RUN: llc -mtriple=riscv32 -mattr=+zba < %s | FileCheck  %s --check-prefixes=CHECK,RV32,RV32ZBA
; RUN: llc -mtriple=riscv64 -mattr=+zba < %s | FileCheck  %s --check-prefixes=CHECK,RV64,RV64ZBA

%struct.S = type { [40 x i32], i32, i32, i32, [4100 x i32], i32, i32, i32 }
@s = common dso_local global %struct.S zeroinitializer, align 4
@foo = global [6 x i16] [i16 1, i16 2, i16 3, i16 4, i16 5, i16 0], align 2
@g = global [1048576 x i8] zeroinitializer, align 1
@bar = external global [0 x i8], align 1


define dso_local void @multiple_stores() local_unnamed_addr nounwind {
; CHECK-LABEL: multiple_stores:
; CHECK:       # %bb.0: # %entry
; CHECK-NEXT:    lui a0, %hi(s)
; CHECK-NEXT:    addi a0, a0, %lo(s)
; CHECK-NEXT:    li a1, 10
; CHECK-NEXT:    sw a1, 160(a0)
; CHECK-NEXT:    li a1, 20
; CHECK-NEXT:    sw a1, 164(a0)
; CHECK-NEXT:    ret
entry:
  store i32 10, i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 1), align 4
  store i32 20, i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 2), align 4
  ret void
}

define dso_local void @control_flow_with_mem_access() local_unnamed_addr nounwind {
; CHECK-LABEL: control_flow_with_mem_access:
; CHECK:       # %bb.0: # %entry
; CHECK-NEXT:    lui a0, %hi(s)
; CHECK-NEXT:    addi a0, a0, %lo(s)
; CHECK-NEXT:    lw a1, 164(a0)
; CHECK-NEXT:    blez a1, .LBB1_2
; CHECK-NEXT:  # %bb.1: # %if.then
; CHECK-NEXT:    li a1, 10
; CHECK-NEXT:    sw a1, 160(a0)
; CHECK-NEXT:  .LBB1_2: # %if.end
; CHECK-NEXT:    ret
entry:
  %0 = load i32, i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 2), align 4
  %cmp = icmp sgt i32 %0, 0
  br i1 %cmp, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  store i32 10, i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 1), align 4
  br label %if.end

if.end:                                           ; preds = %if.then, %entry
  ret void
}

; This test checks that the offset is reconstructed correctly when
; "addi" of the big offset has a negative immediate.
; without peephole this generates:
; lui  a1, %hi(g)
; addi a1, a0, %lo(g)
; lui  a0, 18     ---> offset
; addi a0, a0, -160
; add  a0, a0, a1  ---> base + offset.
define i8* @big_offset_neg_addi() nounwind {
; CHECK-LABEL: big_offset_neg_addi:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lui a0, %hi(g+73568)
; CHECK-NEXT:    addi a0, a0, %lo(g+73568)
; CHECK-NEXT:    ret
  ret i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @g, i32 0, i32 73568)
}

; This test checks for the case where the offset is only an LUI.
; without peephole this generates:
; lui  a0, %hi(g)
; addi a0, a0, %lo(g)
; lui  a1, 128     ---> offset
; add  a0, a0, a1  ---> base + offset.
define i8* @big_offset_lui_tail() nounwind {
; CHECK-LABEL: big_offset_lui_tail:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lui a0, %hi(g+524288)
; CHECK-NEXT:    addi a0, a0, %lo(g+524288)
; CHECK-NEXT:    ret
  ret i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @g, i32 0, i32 524288)
}

define i8* @big_offset_neg_lui_tail() {
; CHECK-LABEL: big_offset_neg_lui_tail:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lui a0, %hi(bar-8192)
; CHECK-NEXT:    addi a0, a0, %lo(bar-8192)
; CHECK-NEXT:    ret
  ret i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i32 -8192)
}

define dso_local i32* @big_offset_one_use() local_unnamed_addr nounwind {
; CHECK-LABEL: big_offset_one_use:
; CHECK:       # %bb.0: # %entry
; CHECK-NEXT:    lui a0, %hi(s+16572)
; CHECK-NEXT:    addi a0, a0, %lo(s+16572)
; CHECK-NEXT:    ret
entry:
  ret i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 5)
}

define dso_local i32* @small_offset_one_use() local_unnamed_addr nounwind {
; CHECK-LABEL: small_offset_one_use:
; CHECK:       # %bb.0: # %entry
; CHECK-NEXT:    lui a0, %hi(s+160)
; CHECK-NEXT:    addi a0, a0, %lo(s+160)
; CHECK-NEXT:    ret
entry:
  ret i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 1)
}

define dso_local i32* @control_flow_no_mem(i32 %n) local_unnamed_addr nounwind {
; CHECK-LABEL: control_flow_no_mem:
; CHECK:       # %bb.0: # %entry
; CHECK-NEXT:    lui a0, %hi(s)
; CHECK-NEXT:    addi a0, a0, %lo(s)
; CHECK-NEXT:    lw a1, 164(a0)
; CHECK-NEXT:    beqz a1, .LBB7_2
; CHECK-NEXT:  # %bb.1: # %if.end
; CHECK-NEXT:    addi a0, a0, 168
; CHECK-NEXT:    ret
; CHECK-NEXT:  .LBB7_2: # %if.then
; CHECK-NEXT:    addi a0, a0, 160
; CHECK-NEXT:    ret
entry:
  %0 = load i32, i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 2), align 4
  %cmp = icmp eq i32 %0, 0
  br i1 %cmp, label %if.then, label %if.end
if.then:                                          ; preds = %entry
  ret i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 1)
if.end:                                           ; preds = %if.then, %entry
  ret i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 3)
}

define dso_local i32 @load_half() nounwind {
; RV32-LABEL: load_half:
; RV32:       # %bb.0: # %entry
; RV32-NEXT:    addi sp, sp, -16
; RV32-NEXT:    sw ra, 12(sp) # 4-byte Folded Spill
; RV32-NEXT:    lui a0, %hi(foo+8)
; RV32-NEXT:    lhu a0, %lo(foo+8)(a0)
; RV32-NEXT:    li a1, 140
; RV32-NEXT:    bne a0, a1, .LBB8_2
; RV32-NEXT:  # %bb.1: # %if.end
; RV32-NEXT:    li a0, 0
; RV32-NEXT:    lw ra, 12(sp) # 4-byte Folded Reload
; RV32-NEXT:    addi sp, sp, 16
; RV32-NEXT:    ret
; RV32-NEXT:  .LBB8_2: # %if.then
; RV32-NEXT:    call abort@plt
;
; RV64-LABEL: load_half:
; RV64:       # %bb.0: # %entry
; RV64-NEXT:    addi sp, sp, -16
; RV64-NEXT:    sd ra, 8(sp) # 8-byte Folded Spill
; RV64-NEXT:    lui a0, %hi(foo+8)
; RV64-NEXT:    lhu a0, %lo(foo+8)(a0)
; RV64-NEXT:    li a1, 140
; RV64-NEXT:    bne a0, a1, .LBB8_2
; RV64-NEXT:  # %bb.1: # %if.end
; RV64-NEXT:    li a0, 0
; RV64-NEXT:    ld ra, 8(sp) # 8-byte Folded Reload
; RV64-NEXT:    addi sp, sp, 16
; RV64-NEXT:    ret
; RV64-NEXT:  .LBB8_2: # %if.then
; RV64-NEXT:    call abort@plt
entry:
  %0 = load i16, i16* getelementptr inbounds ([6 x i16], [6 x i16]* @foo, i32 0, i32 4), align 2
  %cmp = icmp eq i16 %0, 140
  br i1 %cmp, label %if.end, label %if.then

if.then:
  tail call void @abort()
  unreachable

if.end:
  ret i32 0
}

declare void @abort()

define dso_local void @one_store() local_unnamed_addr nounwind {
; CHECK-LABEL: one_store:
; CHECK:       # %bb.0: # %entry
; CHECK-NEXT:    lui a0, %hi(s+160)
; CHECK-NEXT:    li a1, 10
; CHECK-NEXT:    sw a1, %lo(s+160)(a0)
; CHECK-NEXT:    ret
entry:
  store i32 10, i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 1), align 4
  ret void
}

define i8* @neg_offset() {
; CHECK-LABEL: neg_offset:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lui a0, %hi(bar-8191)
; CHECK-NEXT:    addi a0, a0, %lo(bar-8191)
; CHECK-NEXT:    ret
    ret i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i32 -8191)
}

; This uses an LUI+ADDI on RV64 that does not produce a simm32. For RV32, we'll
; truncate the offset.
define i8* @neg_offset_not_simm32() {
; RV32-LABEL: neg_offset_not_simm32:
; RV32:       # %bb.0:
; RV32-NEXT:    lui a0, %hi(bar+2147482283)
; RV32-NEXT:    addi a0, a0, %lo(bar+2147482283)
; RV32-NEXT:    ret
;
; RV64-LABEL: neg_offset_not_simm32:
; RV64:       # %bb.0:
; RV64-NEXT:    lui a0, %hi(bar)
; RV64-NEXT:    addi a0, a0, %lo(bar)
; RV64-NEXT:    lui a1, 524288
; RV64-NEXT:    addi a1, a1, -1365
; RV64-NEXT:    add a0, a0, a1
; RV64-NEXT:    ret
    ret i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i64 -2147485013)
}

define i8* @offset_addi_addi() {
; CHECK-LABEL: offset_addi_addi:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lui a0, %hi(bar+3211)
; CHECK-NEXT:    addi a0, a0, %lo(bar+3211)
; CHECK-NEXT:    ret
    ret i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i64 3211)
}

define i8* @offset_addi_addi_neg() {
; CHECK-LABEL: offset_addi_addi_neg:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lui a0, %hi(bar-4000)
; CHECK-NEXT:    addi a0, a0, %lo(bar-4000)
; CHECK-NEXT:    ret
    ret i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i64 -4000)
}

; With Zba the constant 6424 is created with LI+SH2ADD.
define i8* @offset_sh2add() {
; CHECK-LABEL: offset_sh2add:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lui a0, %hi(bar+6424)
; CHECK-NEXT:    addi a0, a0, %lo(bar+6424)
; CHECK-NEXT:    ret
    ret i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i64 6424)
}

; With Zba the constant 12848 is created with LI+SH3ADD.
define i8* @offset_sh3add() {
; CHECK-LABEL: offset_sh3add:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lui a0, %hi(bar+12848)
; CHECK-NEXT:    addi a0, a0, %lo(bar+12848)
; CHECK-NEXT:    ret
    ret i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i64 12848)
}

define dso_local void @read_modify_write() local_unnamed_addr nounwind {
; RV32-LABEL: read_modify_write:
; RV32:       # %bb.0: # %entry
; RV32-NEXT:    lui a0, %hi(s+160)
; RV32-NEXT:    lw a1, %lo(s+160)(a0)
; RV32-NEXT:    addi a1, a1, 10
; RV32-NEXT:    sw a1, %lo(s+160)(a0)
; RV32-NEXT:    ret
;
; RV64-LABEL: read_modify_write:
; RV64:       # %bb.0: # %entry
; RV64-NEXT:    lui a0, %hi(s+160)
; RV64-NEXT:    lw a1, %lo(s+160)(a0)
; RV64-NEXT:    addiw a1, a1, 10
; RV64-NEXT:    sw a1, %lo(s+160)(a0)
; RV64-NEXT:    ret
entry:
  %x = load i32, i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 1), align 4
  %y = add i32 %x, 10
  store i32 %y, i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 1), align 4
  ret void
}

define dso_local void @rmw_with_control_flow() nounwind {
; CHECK-LABEL: rmw_with_control_flow:
; CHECK:       # %bb.0: # %entry
; CHECK-NEXT:    lui a0, %hi(s+164)
; CHECK-NEXT:    lw a1, %lo(s+164)(a0)
; CHECK-NEXT:    blez a1, .LBB17_2
; CHECK-NEXT:  # %bb.1: # %if.then
; CHECK-NEXT:    li a1, 10
; CHECK-NEXT:    sw a1, %lo(s+164)(a0)
; CHECK-NEXT:  .LBB17_2: # %if.end
; CHECK-NEXT:    ret
entry:
  %0 = load i32, i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 2), align 4
  %cmp = icmp sgt i32 %0, 0
  br i1 %cmp, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  store i32 10, i32* getelementptr inbounds (%struct.S, %struct.S* @s, i32 0, i32 2), align 4
  br label %if.end

if.end:                                           ; preds = %if.then, %entry
  ret void
}

%struct.foo = type { i32, %struct.foo* }

@f = global %struct.foo zeroinitializer, align 8

; Test the case where the store value and base pointer are the same register.
define void @self_store() {
; RV32-LABEL: self_store:
; RV32:       # %bb.0:
; RV32-NEXT:    lui a0, %hi(f)
; RV32-NEXT:    addi a1, a0, %lo(f)
; RV32-NEXT:    sw a1, %lo(f+4)(a0)
; RV32-NEXT:    ret
;
; RV64-LABEL: self_store:
; RV64:       # %bb.0:
; RV64-NEXT:    lui a0, %hi(f)
; RV64-NEXT:    addi a0, a0, %lo(f)
; RV64-NEXT:    sd a0, 8(a0)
; RV64-NEXT:    ret
  store %struct.foo* @f, %struct.foo** getelementptr inbounds (%struct.foo, %struct.foo* @f, i64 0, i32 1), align 8
  ret void
}

;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
; RV32I: {{.*}}
; RV32ZBA: {{.*}}
; RV64I: {{.*}}
; RV64ZBA: {{.*}}