Skip to content

Commit 4f5b59f

Browse files
committed
[GlobalOpt] Preven widenDestArray from shrinking an alloca.
If the destination alloca for one of the memcpy calls we are modifying is already larger than our desired size, we shouldn't replace it with a smaller alloca.
1 parent 0eebbdc commit 4f5b59f

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,10 @@ static void widenDestArray(CallInst *CI, const unsigned NumBytesToPad,
21032103
unsigned ElementByteWidth = SourceDataArray->getElementByteSize();
21042104
unsigned int TotalBytes = NumBytesToCopy + NumBytesToPad;
21052105
unsigned NumElementsToCopy = divideCeil(TotalBytes, ElementByteWidth);
2106+
// Don't change size if already wide enough.
2107+
if (Alloca->getAllocatedType()->getArrayNumElements() >= NumElementsToCopy)
2108+
return;
2109+
21062110
// Update destination array to be word aligned (memcpy(X,...,...))
21072111
IRBuilder<> BuildAlloca(Alloca);
21082112
AllocaInst *NewAlloca = BuildAlloca.CreateAlloca(ArrayType::get(

llvm/test/Transforms/GlobalOpt/ARM/arm-widen-large-alloca.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ define void @memcpy() {
77
; CHECK-LABEL: define void @memcpy() local_unnamed_addr {
88
; CHECK-NEXT: [[ENTRY:.*:]]
99
; CHECK-NEXT: [[ALLOCA1:%.*]] = alloca [4 x i8], align 1
10-
; CHECK-NEXT: [[ALLOCA2:%.*]] = alloca [4 x i8], align 1
10+
; CHECK-NEXT: [[ALLOCA2:%.*]] = alloca [5 x i8], align 1
1111
; CHECK-NEXT: [[CALL1:%.*]] = call i32 @bar(ptr nonnull [[ALLOCA1]])
1212
; CHECK-NEXT: [[CALL2:%.*]] = call i32 @bar(ptr nonnull [[ALLOCA2]])
1313
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(3) [[ALLOCA1]], ptr noundef nonnull align 1 dereferenceable(3) @.i8, i32 4, i1 false)

0 commit comments

Comments
 (0)