Skip to content

Commit 4d33cf4

Browse files
committed
[MemCpyOpt] Avoid moving lifetime marker above def (PR58903)
This is unlikely to happen with opaque pointers, so just bail out of the transform, rather than trying to move bitcasts/etc as well. Fixes llvm/llvm-project#58903.
1 parent e000c2b commit 4d33cf4

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,17 @@ bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
931931
return false;
932932
}
933933

934+
// If we need to move a lifetime.start above the call, make sure that we can
935+
// actually do so. If the argument is bitcasted for example, we would have to
936+
// move the bitcast as well, which we don't handle.
937+
if (SkippedLifetimeStart) {
938+
auto *LifetimeArg =
939+
dyn_cast<Instruction>(SkippedLifetimeStart->getOperand(1));
940+
if (LifetimeArg && LifetimeArg->getParent() == C->getParent() &&
941+
C->comesBefore(LifetimeArg))
942+
return false;
943+
}
944+
934945
// Check that accessing the first srcSize bytes of dest will not cause a
935946
// trap. Otherwise the transform is invalid since it might cause a trap
936947
// to occur earlier than it otherwise would.

llvm/test/Transforms/MemCpyOpt/lifetime.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,22 @@ define i32 @call_slot_clobber_before_lifetime_start() {
116116
%v = load i32, ptr %dst
117117
ret i32 %v
118118
}
119+
120+
define void @call_slot_lifetime_bitcast(ptr %ptr) {
121+
; CHECK-LABEL: @call_slot_lifetime_bitcast(
122+
; CHECK-NEXT: [[TMP1:%.*]] = alloca i32, align 4
123+
; CHECK-NEXT: [[TMP2:%.*]] = alloca i32, align 4
124+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[TMP2]], ptr align 4 [[PTR:%.*]], i64 4, i1 false)
125+
; CHECK-NEXT: [[TMP1_CAST:%.*]] = bitcast ptr [[TMP1]] to ptr
126+
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 4, ptr nonnull [[TMP1_CAST]])
127+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[TMP1_CAST]], ptr align 4 [[PTR]], i64 4, i1 false)
128+
; CHECK-NEXT: ret void
129+
;
130+
%tmp1 = alloca i32
131+
%tmp2 = alloca i32
132+
call void @llvm.memcpy.p0.p0.i64(ptr align 8 %tmp2, ptr align 4 %ptr, i64 4, i1 false)
133+
%tmp1.cast = bitcast ptr %tmp1 to ptr
134+
call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %tmp1.cast)
135+
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %tmp1.cast, ptr align 4 %tmp2, i64 4, i1 false)
136+
ret void
137+
}

0 commit comments

Comments
 (0)