[llvm] r351475 - [InstCombine] Don't sink dynamic allocas
Hans Wennborg via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 18 01:03:42 PST 2019
Merged to 8.0 in r351530.
On Thu, Jan 17, 2019 at 9:50 PM Reid Kleckner via llvm-commits
<llvm-commits at lists.llvm.org> wrote:
>
> Author: rnk
> Date: Thu Jan 17 12:46:53 2019
> New Revision: 351475
>
> URL: http://llvm.org/viewvc/llvm-project?rev=351475&view=rev
> Log:
> [InstCombine] Don't sink dynamic allocas
>
> Summary:
> InstCombine's sinking algorithm only thinks about memory. It doesn't
> think about non-memory constraints like stack object lifetime. It can
> sink dynamic allocas across a stacksave call, which may be used with
> stackrestore, which can incorrectly reduce the lifetime of the dynamic
> alloca.
>
> Fixes PR40365
>
> Reviewers: hfinkel, efriedma
>
> Subscribers: hiraditya, llvm-commits
>
> Differential Revision: https://reviews.llvm.org/D56872
>
> Added:
> llvm/trunk/test/Transforms/InstCombine/sink-alloca.ll
> Modified:
> llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
>
> Modified: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=351475&r1=351474&r2=351475&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp (original)
> +++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp Thu Jan 17 12:46:53 2019
> @@ -3065,9 +3065,11 @@ static bool TryToSinkInstruction(Instruc
> I->isTerminator())
> return false;
>
> - // Do not sink alloca instructions out of the entry block.
> - if (isa<AllocaInst>(I) && I->getParent() ==
> - &DestBlock->getParent()->getEntryBlock())
> + // Do not sink static or dynamic alloca instructions. Static allocas must
> + // remain in the entry block, and dynamic allocas must not be sunk in between
> + // a stacksave / stackrestore pair, which would incorrectly shorten its
> + // lifetime.
> + if (isa<AllocaInst>(I))
> return false;
>
> // Do not sink into catchswitch blocks.
>
> Added: llvm/trunk/test/Transforms/InstCombine/sink-alloca.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/sink-alloca.ll?rev=351475&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/InstCombine/sink-alloca.ll (added)
> +++ llvm/trunk/test/Transforms/InstCombine/sink-alloca.ll Thu Jan 17 12:46:53 2019
> @@ -0,0 +1,52 @@
> +; RUN: opt -instcombine -S < %s | FileCheck %s
> +
> +target datalayout = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"
> +target triple = "i686-unknown-linux-gnu"
> +
> +; Check that instcombine doesn't sink dynamic allocas across llvm.stacksave.
> +
> +; Helper to generate branch conditions.
> +declare i1 @cond()
> +
> +declare i32* @use_and_return(i32*)
> +
> +declare i8* @llvm.stacksave() #0
> +
> +declare void @llvm.stackrestore(i8*) #0
> +
> +define void @foo(i32 %x) {
> +entry:
> + %c1 = call i1 @cond()
> + br i1 %c1, label %ret, label %nonentry
> +
> +nonentry: ; preds = %entry
> + %argmem = alloca i32, i32 %x, align 4
> + %sp = call i8* @llvm.stacksave()
> + %c2 = call i1 @cond()
> + br i1 %c2, label %ret, label %sinktarget
> +
> +sinktarget: ; preds = %nonentry
> + ; Arrange for there to be a single use of %argmem by returning it.
> + %p = call i32* @use_and_return(i32* nonnull %argmem)
> + store i32 13, i32* %p, align 4
> + call void @llvm.stackrestore(i8* %sp)
> + %0 = call i32* @use_and_return(i32* %p)
> + br label %ret
> +
> +ret: ; preds = %sinktarget, %nonentry, %entry
> + ret void
> +}
> +
> +; CHECK-LABEL: define void @foo(i32 %x)
> +; CHECK: nonentry:
> +; CHECK: %argmem = alloca i32, i32 %x
> +; CHECK: %sp = call i8* @llvm.stacksave()
> +; CHECK: %c2 = call i1 @cond()
> +; CHECK: br i1 %c2, label %ret, label %sinktarget
> +; CHECK: sinktarget:
> +; CHECK: %p = call i32* @use_and_return(i32* nonnull %argmem)
> +; CHECK: store i32 13, i32* %p
> +; CHECK: call void @llvm.stackrestore(i8* %sp)
> +; CHECK: %0 = call i32* @use_and_return(i32* %p)
> +
> +attributes #0 = { nounwind }
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list