Skip to content

Commit edd1360

Browse files
authored
[InstCombine] Preserve metadata from orig load in select fold. (#115605)
When replacing load with a select on the address with a select and 2 loads of the values, copy poison-generating metadata from the original load to the newly created loads, which are placed at the same place as the original loads. We cannot copy metadata that may trigger UB. PR: #115605
1 parent cd92aed commit edd1360

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,10 @@ Instruction *InstCombinerImpl::visitLoadInst(LoadInst &LI) {
10601060
V1->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
10611061
V2->setAlignment(Alignment);
10621062
V2->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
1063+
// It is safe to copy any metadata that does not trigger UB. Copy any
1064+
// poison-generating metadata.
1065+
V1->copyMetadata(LI, Metadata::PoisonGeneratingIDs);
1066+
V2->copyMetadata(LI, Metadata::PoisonGeneratingIDs);
10631067
return SelectInst::Create(SI->getCondition(), V1, V2);
10641068
}
10651069

llvm/test/Transforms/InstCombine/loadstore-metadata.ll

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ entry:
186186
ret i32 %c
187187
}
188188

189-
; FIXME: Should preserve none-UB metadata on loads.
189+
; Preserve none-UB metadata on loads.
190190
define ptr @preserve_load_metadata_after_select_transform1(i1 %c, ptr dereferenceable(8) %a, ptr dereferenceable(8) %b) {
191191
; CHECK-LABEL: @preserve_load_metadata_after_select_transform1(
192192
; CHECK-NEXT: entry:
193-
; CHECK-NEXT: [[B_VAL:%.*]] = load ptr, ptr [[B:%.*]], align 1
194-
; CHECK-NEXT: [[A_VAL:%.*]] = load ptr, ptr [[A:%.*]], align 1
193+
; CHECK-NEXT: [[B_VAL:%.*]] = load ptr, ptr [[B:%.*]], align 1, !nonnull [[META6]], !align [[META8]]
194+
; CHECK-NEXT: [[A_VAL:%.*]] = load ptr, ptr [[A:%.*]], align 1, !nonnull [[META6]], !align [[META8]]
195195
; CHECK-NEXT: [[L_SEL:%.*]] = select i1 [[C:%.*]], ptr [[B_VAL]], ptr [[A_VAL]]
196196
; CHECK-NEXT: ret ptr [[L_SEL]]
197197
;
@@ -201,12 +201,12 @@ entry:
201201
ret ptr %l.sel
202202
}
203203

204-
; FIXME: Should preserve none-UB metadata on loads.
204+
; Preserve none-UB metadata on loads.
205205
define i32 @preserve_load_metadata_after_select_transform_range(i1 %c, ptr dereferenceable(8) %a, ptr dereferenceable(8) %b) {
206206
; CHECK-LABEL: @preserve_load_metadata_after_select_transform_range(
207207
; CHECK-NEXT: entry:
208-
; CHECK-NEXT: [[B_VAL:%.*]] = load i32, ptr [[B:%.*]], align 1
209-
; CHECK-NEXT: [[A_VAL:%.*]] = load i32, ptr [[A:%.*]], align 1
208+
; CHECK-NEXT: [[B_VAL:%.*]] = load i32, ptr [[B:%.*]], align 1, !range [[RNG10:![0-9]+]]
209+
; CHECK-NEXT: [[A_VAL:%.*]] = load i32, ptr [[A:%.*]], align 1, !range [[RNG10]]
210210
; CHECK-NEXT: [[L_SEL:%.*]] = select i1 [[C:%.*]], i32 [[B_VAL]], i32 [[A_VAL]]
211211
; CHECK-NEXT: ret i32 [[L_SEL]]
212212
;
@@ -294,7 +294,7 @@ define double @preserve_load_metadata_after_select_transform_metadata_missing_4(
294294
; CHECK-LABEL: @preserve_load_metadata_after_select_transform_metadata_missing_4(
295295
; CHECK-NEXT: entry:
296296
; CHECK-NEXT: [[L_A:%.*]] = load double, ptr [[A:%.*]], align 8, !tbaa [[TBAA0]], !alias.scope [[META3]], !noalias [[META3]], !llvm.access.group [[META6]]
297-
; CHECK-NEXT: [[L_B:%.*]] = load double, ptr [[B:%.*]], align 8, !tbaa [[TBAA0]], !alias.scope [[META10:![0-9]+]], !noalias [[META10]], !llvm.access.group [[ACC_GRP13:![0-9]+]]
297+
; CHECK-NEXT: [[L_B:%.*]] = load double, ptr [[B:%.*]], align 8, !tbaa [[TBAA0]], !alias.scope [[META11:![0-9]+]], !noalias [[META11]], !llvm.access.group [[ACC_GRP14:![0-9]+]]
298298
; CHECK-NEXT: [[CMP_I:%.*]] = fcmp fast olt double [[L_A]], [[L_B]]
299299
; CHECK-NEXT: [[L_SEL:%.*]] = select i1 [[CMP_I]], double [[L_B]], double [[L_A]]
300300
; CHECK-NEXT: ret double [[L_SEL]]
@@ -337,8 +337,9 @@ entry:
337337
; CHECK: [[META7]] = !{i32 1}
338338
; CHECK: [[META8]] = !{i64 8}
339339
; CHECK: [[ACC_GRP9]] = distinct !{}
340-
; CHECK: [[META10]] = !{[[META11:![0-9]+]]}
341-
; CHECK: [[META11]] = distinct !{[[META11]], [[META12:![0-9]+]]}
342-
; CHECK: [[META12]] = distinct !{[[META12]]}
343-
; CHECK: [[ACC_GRP13]] = distinct !{}
340+
; CHECK: [[RNG10]] = !{i32 0, i32 42}
341+
; CHECK: [[META11]] = !{[[META12:![0-9]+]]}
342+
; CHECK: [[META12]] = distinct !{[[META12]], [[META13:![0-9]+]]}
343+
; CHECK: [[META13]] = distinct !{[[META13]]}
344+
; CHECK: [[ACC_GRP14]] = distinct !{}
344345
;.

0 commit comments

Comments
 (0)