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
|
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -passes='sroa<preserve-cfg>' < %s | FileCheck %s --check-prefixes=CHECK,CHECK-PRESERVE-CFG
; RUN: opt -S -passes='sroa<modify-cfg>' < %s | FileCheck %s --check-prefixes=CHECK,CHECK-MODIFY-CFG
%pair = type { i32, i32 }
define i32 @test_sroa_select_gep(i1 %cond) {
; CHECK-LABEL: @test_sroa_select_gep(
; CHECK-NEXT: bb:
; CHECK-NEXT: [[LOAD_SROA_SPECULATED:%.*]] = select i1 [[COND:%.*]], i32 1, i32 2
; CHECK-NEXT: ret i32 [[LOAD_SROA_SPECULATED]]
;
bb:
%a = alloca %pair, align 4
%b = alloca %pair, align 4
%gep_a = getelementptr inbounds %pair, ptr %a, i32 0, i32 1
%gep_b = getelementptr inbounds %pair, ptr %b, i32 0, i32 1
store i32 1, ptr %gep_a, align 4
store i32 2, ptr %gep_b, align 4
%select = select i1 %cond, ptr %a, ptr %b
%gep = getelementptr inbounds %pair, ptr %select, i32 0, i32 1
%load = load i32, ptr %gep, align 4
ret i32 %load
}
define i32 @test_sroa_select_gep_non_inbound(i1 %cond) {
; CHECK-LABEL: @test_sroa_select_gep_non_inbound(
; CHECK-NEXT: bb:
; CHECK-NEXT: [[LOAD_SROA_SPECULATED:%.*]] = select i1 [[COND:%.*]], i32 1, i32 2
; CHECK-NEXT: ret i32 [[LOAD_SROA_SPECULATED]]
;
bb:
%a = alloca %pair, align 4
%b = alloca %pair, align 4
%gep_a = getelementptr %pair, ptr %a, i32 0, i32 1
%gep_b = getelementptr %pair, ptr %b, i32 0, i32 1
store i32 1, ptr %gep_a, align 4
store i32 2, ptr %gep_b, align 4
%select = select i1 %cond, ptr %a, ptr %b
%gep = getelementptr %pair, ptr %select, i32 0, i32 1
%load = load i32, ptr %gep, align 4
ret i32 %load
}
define i32 @test_sroa_select_gep_volatile_load(i1 %cond) {
; CHECK-LABEL: @test_sroa_select_gep_volatile_load(
; CHECK-NEXT: bb:
; CHECK-NEXT: [[A_SROA_0:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[A_SROA_2:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B_SROA_0:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B_SROA_2:%.*]] = alloca i32, align 4
; CHECK-NEXT: store i32 11, ptr [[A_SROA_0]], align 4
; CHECK-NEXT: store i32 12, ptr [[B_SROA_0]], align 4
; CHECK-NEXT: store i32 21, ptr [[A_SROA_2]], align 4
; CHECK-NEXT: store i32 22, ptr [[B_SROA_2]], align 4
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND:%.*]], ptr [[A_SROA_0]], ptr [[B_SROA_0]]
; CHECK-NEXT: [[LOAD1:%.*]] = load volatile i32, ptr [[SELECT]], align 4
; CHECK-NEXT: [[SELECT_SROA_SEL:%.*]] = select i1 [[COND]], ptr [[A_SROA_2]], ptr [[B_SROA_2]]
; CHECK-NEXT: [[LOAD2:%.*]] = load volatile i32, ptr [[SELECT_SROA_SEL]], align 4
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[LOAD1]], [[LOAD2]]
; CHECK-NEXT: ret i32 [[ADD]]
;
bb:
%a = alloca %pair, align 4
%b = alloca %pair, align 4
store i32 11, ptr %a, align 4
store i32 12, ptr %b, align 4
%gep_a1 = getelementptr inbounds %pair, ptr %a, i32 0, i32 1
%gep_b1 = getelementptr inbounds %pair, ptr %b, i32 0, i32 1
store i32 21, ptr %gep_a1, align 4
store i32 22, ptr %gep_b1, align 4
%select = select i1 %cond, ptr %a, ptr %b
%load1 = load volatile i32, ptr %select, align 4
%gep2 = getelementptr inbounds %pair, ptr %select, i32 0, i32 1
%load2 = load volatile i32, ptr %gep2, align 4
%add = add i32 %load1, %load2
ret i32 %add
}
define i32 @test_sroa_select_gep_poison(i1 %cond) {
; CHECK-PRESERVE-CFG-LABEL: @test_sroa_select_gep_poison(
; CHECK-PRESERVE-CFG-NEXT: bb:
; CHECK-PRESERVE-CFG-NEXT: [[A_SROA_0:%.*]] = alloca i32, align 4
; CHECK-PRESERVE-CFG-NEXT: [[SELECT_SROA_SEL:%.*]] = select i1 [[COND:%.*]], ptr [[A_SROA_0]], ptr poison
; CHECK-PRESERVE-CFG-NEXT: [[LOAD:%.*]] = load i32, ptr [[SELECT_SROA_SEL]], align 4
; CHECK-PRESERVE-CFG-NEXT: ret i32 [[LOAD]]
;
; CHECK-MODIFY-CFG-LABEL: @test_sroa_select_gep_poison(
; CHECK-MODIFY-CFG-NEXT: bb:
; CHECK-MODIFY-CFG-NEXT: br i1 [[COND:%.*]], label [[BB_CONT:%.*]], label [[BB_ELSE:%.*]]
; CHECK-MODIFY-CFG: bb.else:
; CHECK-MODIFY-CFG-NEXT: [[LOAD_ELSE_VAL:%.*]] = load i32, ptr poison, align 4
; CHECK-MODIFY-CFG-NEXT: br label [[BB_CONT]]
; CHECK-MODIFY-CFG: bb.cont:
; CHECK-MODIFY-CFG-NEXT: [[LOAD:%.*]] = phi i32 [ undef, [[BB:%.*]] ], [ [[LOAD_ELSE_VAL]], [[BB_ELSE]] ]
; CHECK-MODIFY-CFG-NEXT: ret i32 [[LOAD]]
;
bb:
%a = alloca %pair, align 4
%select = select i1 %cond, ptr %a, ptr poison
%gep = getelementptr inbounds %pair, ptr %select, i32 0, i32 1
%load = load i32, ptr %gep, align 4
ret i32 %load
}
define i32 @test_sroa_gep_select_gep(i1 %cond) {
; CHECK-LABEL: @test_sroa_gep_select_gep(
; CHECK-NEXT: bb:
; CHECK-NEXT: [[A_SROA_0:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B_SROA_0:%.*]] = alloca i32, align 4
; CHECK-NEXT: store i32 1, ptr [[A_SROA_0]], align 4
; CHECK-NEXT: store i32 2, ptr [[B_SROA_0]], align 4
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND:%.*]], ptr [[A_SROA_0]], ptr [[B_SROA_0]]
; CHECK-NEXT: [[SELECT2:%.*]] = select i1 [[COND]], ptr [[SELECT]], ptr [[A_SROA_0]]
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[SELECT2]], align 4
; CHECK-NEXT: ret i32 [[LOAD]]
;
bb:
%a = alloca %pair, align 4
%b = alloca %pair, align 4
%gep_a = getelementptr inbounds %pair, ptr %a, i32 0, i32 1
%gep_b = getelementptr inbounds %pair, ptr %b, i32 0, i32 1
store i32 1, ptr %gep_a, align 4
store i32 2, ptr %gep_b, align 4
%select = select i1 %cond, ptr %gep_a, ptr %gep_b
%select2 = select i1 %cond, ptr %select, ptr %gep_a
%load = load i32, ptr %select2, align 4
ret i32 %load
}
define i32 @test_sroa_gep_select_gep_nonconst_idx(i1 %cond, i32 %idx) {
; CHECK-LABEL: @test_sroa_gep_select_gep_nonconst_idx(
; CHECK-NEXT: bb:
; CHECK-NEXT: [[A:%.*]] = alloca [[PAIR:%.*]], align 4
; CHECK-NEXT: [[B:%.*]] = alloca [[PAIR]], align 4
; CHECK-NEXT: [[GEP_A:%.*]] = getelementptr inbounds [[PAIR]], ptr [[A]], i32 0, i32 1
; CHECK-NEXT: [[GEP_B:%.*]] = getelementptr inbounds [[PAIR]], ptr [[B]], i32 0, i32 1
; CHECK-NEXT: store i32 1, ptr [[GEP_A]], align 4
; CHECK-NEXT: store i32 2, ptr [[GEP_B]], align 4
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND:%.*]], ptr [[A]], ptr [[B]]
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [[PAIR]], ptr [[SELECT]], i32 [[IDX:%.*]], i32 1
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
; CHECK-NEXT: ret i32 [[LOAD]]
;
bb:
%a = alloca %pair, align 4
%b = alloca %pair, align 4
%gep_a = getelementptr inbounds %pair, ptr %a, i32 0, i32 1
%gep_b = getelementptr inbounds %pair, ptr %b, i32 0, i32 1
store i32 1, ptr %gep_a, align 4
store i32 2, ptr %gep_b, align 4
%select = select i1 %cond, ptr %a, ptr %b
%gep = getelementptr inbounds %pair, ptr %select, i32 %idx, i32 1
%load = load i32, ptr %gep, align 4
ret i32 %load
}
; Test gep of index select unfolding on an alloca that is splittable, but not
; promotable. The allocas here will be optimized away by subsequent passes.
define i32 @test_select_idx_memcpy(i1 %c, ptr %p) {
; CHECK-LABEL: @test_select_idx_memcpy(
; CHECK-NEXT: [[ALLOCA_SROA_0:%.*]] = alloca [4 x i8], align 8
; CHECK-NEXT: [[ALLOCA_SROA_2:%.*]] = alloca [20 x i8], align 4
; CHECK-NEXT: [[ALLOCA_SROA_22:%.*]] = alloca [4 x i8], align 8
; CHECK-NEXT: [[ALLOCA_SROA_3:%.*]] = alloca [132 x i8], align 4
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[ALLOCA_SROA_0]], ptr align 1 [[P:%.*]], i64 4, i1 false)
; CHECK-NEXT: [[ALLOCA_SROA_2_0_P_SROA_IDX:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 4
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[ALLOCA_SROA_2]], ptr align 1 [[ALLOCA_SROA_2_0_P_SROA_IDX]], i64 20, i1 false)
; CHECK-NEXT: [[ALLOCA_SROA_22_0_P_SROA_IDX:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 24
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[ALLOCA_SROA_22]], ptr align 1 [[ALLOCA_SROA_22_0_P_SROA_IDX]], i64 4, i1 false)
; CHECK-NEXT: [[ALLOCA_SROA_3_0_P_SROA_IDX:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 28
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[ALLOCA_SROA_3]], ptr align 1 [[ALLOCA_SROA_3_0_P_SROA_IDX]], i64 132, i1 false)
; CHECK-NEXT: [[IDX:%.*]] = select i1 [[C:%.*]], i64 24, i64 0
; CHECK-NEXT: [[IDX_SROA_SEL:%.*]] = select i1 [[C]], ptr [[ALLOCA_SROA_22]], ptr [[ALLOCA_SROA_0]]
; CHECK-NEXT: [[RES:%.*]] = load i32, ptr [[IDX_SROA_SEL]], align 4
; CHECK-NEXT: ret i32 [[RES]]
;
%alloca = alloca [20 x i64], align 8
call void @llvm.memcpy.p0.p0.i64(ptr %alloca, ptr %p, i64 160, i1 false)
%idx = select i1 %c, i64 24, i64 0
%gep = getelementptr inbounds i8, ptr %alloca, i64 %idx
%res = load i32, ptr %gep, align 4
ret i32 %res
}
; Test gep of index select unfolding on an alloca that is splittable and
; promotable.
define i32 @test_select_idx_mem2reg(i1 %c) {
; CHECK-LABEL: @test_select_idx_mem2reg(
; CHECK-NEXT: [[IDX:%.*]] = select i1 [[C:%.*]], i64 24, i64 0
; CHECK-NEXT: [[RES_SROA_SPECULATED:%.*]] = select i1 [[C]], i32 2, i32 1
; CHECK-NEXT: ret i32 [[RES_SROA_SPECULATED]]
;
%alloca = alloca [20 x i64], align 8
store i32 1, ptr %alloca
%gep1 = getelementptr inbounds i8, ptr %alloca, i64 24
store i32 2, ptr %gep1
%idx = select i1 %c, i64 24, i64 0
%gep2 = getelementptr inbounds i8, ptr %alloca, i64 %idx
%res = load i32, ptr %gep2, align 4
ret i32 %res
}
; Test gep of index select unfolding on an alloca that escaped, and as such
; is not splittable or promotable.
; FIXME: Ideally, no transform would take place in this case.
define i32 @test_select_idx_escaped(i1 %c, ptr %p) {
; CHECK-LABEL: @test_select_idx_escaped(
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [20 x i64], align 8
; CHECK-NEXT: store ptr [[ALLOCA]], ptr [[P:%.*]], align 8
; CHECK-NEXT: store i32 1, ptr [[ALLOCA]], align 4
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i8, ptr [[ALLOCA]], i64 24
; CHECK-NEXT: store i32 2, ptr [[GEP1]], align 4
; CHECK-NEXT: [[IDX:%.*]] = select i1 [[C:%.*]], i64 24, i64 0
; CHECK-NEXT: [[DOTSROA_GEP:%.*]] = getelementptr inbounds i8, ptr [[ALLOCA]], i64 24
; CHECK-NEXT: [[DOTSROA_GEP1:%.*]] = getelementptr inbounds i8, ptr [[ALLOCA]], i64 0
; CHECK-NEXT: [[IDX_SROA_SEL:%.*]] = select i1 [[C]], ptr [[DOTSROA_GEP]], ptr [[DOTSROA_GEP1]]
; CHECK-NEXT: [[RES:%.*]] = load i32, ptr [[IDX_SROA_SEL]], align 4
; CHECK-NEXT: ret i32 [[RES]]
;
%alloca = alloca [20 x i64], align 8
store ptr %alloca, ptr %p
store i32 1, ptr %alloca
%gep1 = getelementptr inbounds i8, ptr %alloca, i64 24
store i32 2, ptr %gep1
%idx = select i1 %c, i64 24, i64 0
%gep2 = getelementptr inbounds i8, ptr %alloca, i64 %idx
%res = load i32, ptr %gep2, align 4
ret i32 %res
}
; FIXME: Should we allow recursive select unfolding if all the leaves are
; constants?
define i32 @test_select_idx_nested(i1 %c, i1 %c2) {
; CHECK-LABEL: @test_select_idx_nested(
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [20 x i64], align 8
; CHECK-NEXT: store i32 1, ptr [[ALLOCA]], align 4
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i8, ptr [[ALLOCA]], i64 8
; CHECK-NEXT: store i32 2, ptr [[GEP1]], align 4
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds i8, ptr [[ALLOCA]], i64 24
; CHECK-NEXT: store i32 3, ptr [[GEP2]], align 4
; CHECK-NEXT: [[IDX1:%.*]] = select i1 [[C:%.*]], i64 24, i64 0
; CHECK-NEXT: [[IDX2:%.*]] = select i1 [[C2:%.*]], i64 [[IDX1]], i64 8
; CHECK-NEXT: [[GEP3:%.*]] = getelementptr inbounds i8, ptr [[ALLOCA]], i64 [[IDX2]]
; CHECK-NEXT: [[RES:%.*]] = load i32, ptr [[GEP3]], align 4
; CHECK-NEXT: ret i32 [[RES]]
;
%alloca = alloca [20 x i64], align 8
store i32 1, ptr %alloca
%gep1 = getelementptr inbounds i8, ptr %alloca, i64 8
store i32 2, ptr %gep1
%gep2 = getelementptr inbounds i8, ptr %alloca, i64 24
store i32 3, ptr %gep2
%idx1 = select i1 %c, i64 24, i64 0
%idx2 = select i1 %c2, i64 %idx1, i64 8
%gep3 = getelementptr inbounds i8, ptr %alloca, i64 %idx2
%res = load i32, ptr %gep3, align 4
ret i32 %res
}
; The following cases involve non-constant indices and should not be
; transformed.
define i32 @test_select_idx_not_constant1(i1 %c, ptr %p, i64 %arg) {
; CHECK-LABEL: @test_select_idx_not_constant1(
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [20 x i64], align 8
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[ALLOCA]], ptr [[P:%.*]], i64 160, i1 false)
; CHECK-NEXT: [[IDX:%.*]] = select i1 [[C:%.*]], i64 24, i64 [[ARG:%.*]]
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[ALLOCA]], i64 [[IDX]]
; CHECK-NEXT: [[RES:%.*]] = load i32, ptr [[GEP]], align 4
; CHECK-NEXT: ret i32 [[RES]]
;
%alloca = alloca [20 x i64], align 8
call void @llvm.memcpy.p0.p0.i64(ptr %alloca, ptr %p, i64 160, i1 false)
%idx = select i1 %c, i64 24, i64 %arg
%gep = getelementptr inbounds i8, ptr %alloca, i64 %idx
%res = load i32, ptr %gep, align 4
ret i32 %res
}
define i32 @test_select_idx_not_constant2(i1 %c, ptr %p, i64 %arg) {
; CHECK-LABEL: @test_select_idx_not_constant2(
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [20 x i64], align 8
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[ALLOCA]], ptr [[P:%.*]], i64 160, i1 false)
; CHECK-NEXT: [[IDX:%.*]] = select i1 [[C:%.*]], i64 [[ARG:%.*]], i64 0
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[ALLOCA]], i64 [[IDX]]
; CHECK-NEXT: [[RES:%.*]] = load i32, ptr [[GEP]], align 4
; CHECK-NEXT: ret i32 [[RES]]
;
%alloca = alloca [20 x i64], align 8
call void @llvm.memcpy.p0.p0.i64(ptr %alloca, ptr %p, i64 160, i1 false)
%idx = select i1 %c, i64 %arg, i64 0
%gep = getelementptr inbounds i8, ptr %alloca, i64 %idx
%res = load i32, ptr %gep, align 4
ret i32 %res
}
define i32 @test_select_idx_not_constant3(i1 %c, ptr %p, i64 %arg) {
; CHECK-LABEL: @test_select_idx_not_constant3(
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [20 x i64], align 8
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[ALLOCA]], ptr [[P:%.*]], i64 160, i1 false)
; CHECK-NEXT: [[IDX:%.*]] = select i1 [[C:%.*]], i64 24, i64 0
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [1 x i8], ptr [[ALLOCA]], i64 [[IDX]], i64 [[ARG:%.*]]
; CHECK-NEXT: [[RES:%.*]] = load i32, ptr [[GEP]], align 4
; CHECK-NEXT: ret i32 [[RES]]
;
%alloca = alloca [20 x i64], align 8
call void @llvm.memcpy.p0.p0.i64(ptr %alloca, ptr %p, i64 160, i1 false)
%idx = select i1 %c, i64 24, i64 0
%gep = getelementptr inbounds [1 x i8], ptr %alloca, i64 %idx, i64 %arg
%res = load i32, ptr %gep, align 4
ret i32 %res
}
|