Skip to content

Commit b373b59

Browse files
author
Andy Kaylor
committed
Enabling the copy-constant-to-alloca optimization in more instances
Patch by Mohammad Fawaz This patch allows lifetime calls to be ignored (and later erased) if we know that the copy-constant-to-alloca optimization is going to happen. The case that is missed is when the global variable is in a different address space than the alloca (as shown in the example added to the lit test.) This used to work before llvm@6da31fa Differential Revision: https://reviews.llvm.org/D106573
1 parent a5dd6c6 commit b373b59

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ bool PointerReplacer::collectUsers(Instruction &I) {
272272
return false;
273273
} else if (isa<MemTransferInst>(Inst)) {
274274
Worklist.insert(Inst);
275+
} else if (Inst->isLifetimeStartOrEnd()) {
276+
continue;
275277
} else {
276278
LLVM_DEBUG(dbgs() << "Cannot handle pointer user: " << *U << '\n');
277279
return false;

llvm/test/Transforms/InstCombine/memcpy-from-global.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ declare void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* nocapture, i8 addrspace
7373

7474
@G = constant %T {i8 1, [123 x i8] zeroinitializer }
7575
@H = constant [2 x %U] zeroinitializer, align 16
76+
@I = internal addrspace(1) constant [4 x float] zeroinitializer , align 4
7677

7778
define void @test2() {
7879
; CHECK-LABEL: @test2(
@@ -323,4 +324,22 @@ entry:
323324
ret void
324325
}
325326

327+
; Should replace alloca with global even when the global is in a different address space
328+
define float @test11(i64 %i) {
329+
; CHECK-LABEL: @test11(
330+
; CHECK-NEXT: entry:
331+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr [4 x float], [4 x float] addrspace(1)* @I, i64 0, i64 %i
332+
; CHECK-NEXT: [[LD:%.*]] = load float, float addrspace(1)* [[GEP]]
333+
; CHECK-NEXT: ret float [[LD]]
334+
335+
entry:
336+
%a = alloca [4 x float], align 4
337+
%b = bitcast [4 x float]* %a to i8*
338+
call void @llvm.lifetime.start.p0i8(i64 16, i8* %b)
339+
call void @llvm.memcpy.p0i8.p1i8.i64(i8* align 4 %b, i8 addrspace(1)* align 4 bitcast ([4 x float] addrspace(1)* @I to i8 addrspace(1)*), i64 16, i1 false)
340+
%g = getelementptr inbounds [4 x float], [4 x float]* %a, i64 0, i64 %i
341+
%r = load float, float* %g, align 4
342+
ret float %r
343+
}
344+
326345
attributes #0 = { null_pointer_is_valid }

0 commit comments

Comments
 (0)