aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/CodeGen/RISCV/split-offsets.ll
blob: fc35bc4d2a16d88e2a9e8445c94326ec6d5ad9b9 (plain)
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
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=riscv32 -verify-machineinstrs -riscv-enable-sink-fold < %s \
; RUN:   | FileCheck %s -check-prefix=RV32I
; RUN: llc -mtriple=riscv64 -verify-machineinstrs -riscv-enable-sink-fold < %s \
; RUN:   | FileCheck %s -check-prefix=RV64I

; Check that memory accesses to array elements with large offsets have those
; offsets split into a base offset, plus a smaller offset that is folded into
; the memory operation. We should also only compute that base offset once,
; since it can be shared for all memory operations in this test.
define void @test1(ptr %sp, ptr %t, i32 %n) {
; RV32I-LABEL: test1:
; RV32I:       # %bb.0: # %entry
; RV32I-NEXT:    lw a0, 0(a0)
; RV32I-NEXT:    lui a2, 20
; RV32I-NEXT:    addi a2, a2, -1920
; RV32I-NEXT:    add a1, a1, a2
; RV32I-NEXT:    add a0, a0, a2
; RV32I-NEXT:    li a2, 2
; RV32I-NEXT:    sw a2, 0(a0)
; RV32I-NEXT:    li a3, 1
; RV32I-NEXT:    sw a3, 4(a0)
; RV32I-NEXT:    sw a3, 0(a1)
; RV32I-NEXT:    sw a2, 4(a1)
; RV32I-NEXT:    ret
;
; RV64I-LABEL: test1:
; RV64I:       # %bb.0: # %entry
; RV64I-NEXT:    ld a0, 0(a0)
; RV64I-NEXT:    lui a2, 20
; RV64I-NEXT:    addiw a2, a2, -1920
; RV64I-NEXT:    add a1, a1, a2
; RV64I-NEXT:    add a0, a0, a2
; RV64I-NEXT:    li a2, 2
; RV64I-NEXT:    sw a2, 0(a0)
; RV64I-NEXT:    li a3, 1
; RV64I-NEXT:    sw a3, 4(a0)
; RV64I-NEXT:    sw a3, 0(a1)
; RV64I-NEXT:    sw a2, 4(a1)
; RV64I-NEXT:    ret
entry:
  %s = load ptr, ptr %sp
  %gep0 = getelementptr [65536 x i32], ptr %s, i64 0, i32 20000
  %gep1 = getelementptr [65536 x i32], ptr %s, i64 0, i32 20001
  %gep2 = getelementptr [65536 x i32], ptr %t, i64 0, i32 20000
  %gep3 = getelementptr [65536 x i32], ptr %t, i64 0, i32 20001
  store i32 2, ptr %gep0
  store i32 1, ptr %gep1
  store i32 1, ptr %gep2
  store i32 2, ptr %gep3
  ret void
}

; Ditto. Check it when the GEPs are not in the entry block.
define void @test2(ptr %sp, ptr %t, i32 %n) {
; RV32I-LABEL: test2:
; RV32I:       # %bb.0: # %entry
; RV32I-NEXT:    li a3, 0
; RV32I-NEXT:    lw a0, 0(a0)
; RV32I-NEXT:    lui a4, 20
; RV32I-NEXT:    addi a4, a4, -1920
; RV32I-NEXT:    add a1, a1, a4
; RV32I-NEXT:    add a0, a0, a4
; RV32I-NEXT:    blez a2, .LBB1_2
; RV32I-NEXT:  .LBB1_1: # %while_body
; RV32I-NEXT:    # =>This Inner Loop Header: Depth=1
; RV32I-NEXT:    addi a4, a3, 1
; RV32I-NEXT:    sw a4, 0(a0)
; RV32I-NEXT:    sw a3, 4(a0)
; RV32I-NEXT:    sw a4, 0(a1)
; RV32I-NEXT:    sw a3, 4(a1)
; RV32I-NEXT:    mv a3, a4
; RV32I-NEXT:    blt a4, a2, .LBB1_1
; RV32I-NEXT:  .LBB1_2: # %while_end
; RV32I-NEXT:    ret
;
; RV64I-LABEL: test2:
; RV64I:       # %bb.0: # %entry
; RV64I-NEXT:    li a3, 0
; RV64I-NEXT:    ld a0, 0(a0)
; RV64I-NEXT:    lui a4, 20
; RV64I-NEXT:    addiw a4, a4, -1920
; RV64I-NEXT:    add a1, a1, a4
; RV64I-NEXT:    add a0, a0, a4
; RV64I-NEXT:    sext.w a2, a2
; RV64I-NEXT:    blez a2, .LBB1_2
; RV64I-NEXT:  .LBB1_1: # %while_body
; RV64I-NEXT:    # =>This Inner Loop Header: Depth=1
; RV64I-NEXT:    addiw a4, a3, 1
; RV64I-NEXT:    sw a4, 0(a0)
; RV64I-NEXT:    sw a3, 4(a0)
; RV64I-NEXT:    sw a4, 0(a1)
; RV64I-NEXT:    sw a3, 4(a1)
; RV64I-NEXT:    mv a3, a4
; RV64I-NEXT:    blt a4, a2, .LBB1_1
; RV64I-NEXT:  .LBB1_2: # %while_end
; RV64I-NEXT:    ret
entry:
  %s = load ptr, ptr %sp
  br label %while_cond
while_cond:
  %phi = phi i32 [ 0, %entry ], [ %i, %while_body ]
  %gep0 = getelementptr [65536 x i32], ptr %s, i64 0, i32 20000
  %gep1 = getelementptr [65536 x i32], ptr %s, i64 0, i32 20001
  %gep2 = getelementptr [65536 x i32], ptr %t, i64 0, i32 20000
  %gep3 = getelementptr [65536 x i32], ptr %t, i64 0, i32 20001
  %cmp = icmp slt i32 %phi, %n
  br i1 %cmp, label %while_body, label %while_end
while_body:
  %i = add i32 %phi, 1
  %j = add i32 %phi, 2
  store i32 %i, ptr %gep0
  store i32 %phi, ptr %gep1
  store i32 %i, ptr %gep2
  store i32 %phi, ptr %gep3
  br label %while_cond
while_end:
  ret void
}

; GEPs have been manually split so the base GEP does not get used by any memory
; instructions. Make sure we use an offset and common base for each of the
; stores.
define void @test3(ptr %t) {
; RV32I-LABEL: test3:
; RV32I:       # %bb.0: # %entry
; RV32I-NEXT:    lui a1, 20
; RV32I-NEXT:    addi a1, a1, -1920
; RV32I-NEXT:    add a0, a0, a1
; RV32I-NEXT:    li a1, 2
; RV32I-NEXT:    sw a1, 4(a0)
; RV32I-NEXT:    li a1, 3
; RV32I-NEXT:    sw a1, 8(a0)
; RV32I-NEXT:    ret
;
; RV64I-LABEL: test3:
; RV64I:       # %bb.0: # %entry
; RV64I-NEXT:    lui a1, 20
; RV64I-NEXT:    addiw a1, a1, -1920
; RV64I-NEXT:    add a0, a0, a1
; RV64I-NEXT:    li a1, 2
; RV64I-NEXT:    sw a1, 4(a0)
; RV64I-NEXT:    li a1, 3
; RV64I-NEXT:    sw a1, 8(a0)
; RV64I-NEXT:    ret
entry:
  %splitgep = getelementptr i8, ptr %t, i64 80000
  %0 = getelementptr i8, ptr %splitgep, i64 4
  %1 = getelementptr i8, ptr %splitgep, i64 8
  store i32 2, ptr %0, align 4
  store i32 3, ptr %1, align 4
  ret void
}

; Test from PR62734.
define void @test4(ptr %dest) {
; RV32I-LABEL: test4:
; RV32I:       # %bb.0:
; RV32I-NEXT:    addi a0, a0, 2047
; RV32I-NEXT:    li a1, 1
; RV32I-NEXT:    sb a1, 1(a0)
; RV32I-NEXT:    sb a1, 2(a0)
; RV32I-NEXT:    sb a1, 3(a0)
; RV32I-NEXT:    sb a1, 4(a0)
; RV32I-NEXT:    ret
;
; RV64I-LABEL: test4:
; RV64I:       # %bb.0:
; RV64I-NEXT:    addi a0, a0, 2047
; RV64I-NEXT:    li a1, 1
; RV64I-NEXT:    sb a1, 1(a0)
; RV64I-NEXT:    sb a1, 2(a0)
; RV64I-NEXT:    sb a1, 3(a0)
; RV64I-NEXT:    sb a1, 4(a0)
; RV64I-NEXT:    ret
  %p1 = getelementptr i8, ptr %dest, i32 2048
  store i8 1, ptr %p1
  %p2 = getelementptr i8, ptr %dest, i32 2049
  store i8 1, ptr %p2
  %p3 = getelementptr i8, ptr %dest, i32 2050
  store i8 1, ptr %p3
  %p4 = getelementptr i8, ptr %dest, i32 2051
  store i8 1, ptr %p4
  ret void
}