diff --git a/llvm/lib/Target/ARM/ARMFrameLowering.cpp b/llvm/lib/Target/ARM/ARMFrameLowering.cpp --- a/llvm/lib/Target/ARM/ARMFrameLowering.cpp +++ b/llvm/lib/Target/ARM/ARMFrameLowering.cpp @@ -2562,8 +2562,9 @@ // Make sure the LiveIns are still sorted and unique. MBB->sortUniqueLiveIns(); // Replace the edges to PrologueMBB by edges to the sequences - // we are about to add. - MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]); + // we are about to add, but only update for immediate predecessors. + if (MBB->isSuccessor(&PrologueMBB)) + MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]); } // The required stack size that is aligned to ARM constant criterion. diff --git a/llvm/test/CodeGen/ARM/segmented-stacks.ll b/llvm/test/CodeGen/ARM/segmented-stacks.ll --- a/llvm/test/CodeGen/ARM/segmented-stacks.ll +++ b/llvm/test/CodeGen/ARM/segmented-stacks.ll @@ -280,4 +280,24 @@ } + +declare void @panic() unnamed_addr + +; We used to crash while compiling the following function. +; ARM-linux-LABEL: build_should_not_segfault: +; ARM-android-LABEL: build_should_not_segfault: +define void @build_should_not_segfault(i8 %x) unnamed_addr #0 { +start: + %_0 = icmp ult i8 %x, 16 + %or.cond = select i1 undef, i1 true, i1 %_0 + br i1 %or.cond, label %bb1, label %bb2 + +bb1: + ret void + +bb2: + call void @panic() + unreachable +} + attributes #0 = { "split-stack" } diff --git a/llvm/test/CodeGen/Thumb/segmented-stacks.ll b/llvm/test/CodeGen/Thumb/segmented-stacks.ll --- a/llvm/test/CodeGen/Thumb/segmented-stacks.ll +++ b/llvm/test/CodeGen/Thumb/segmented-stacks.ll @@ -275,4 +275,24 @@ ; Thumb-linux-NOT: bl __morestack } + +declare void @panic() unnamed_addr + +; We used to crash while compiling the following function. +; Thumb-linux-LABEL: build_should_not_segfault: +; Thumb-android-LABEL: build_should_not_segfault: +define void @build_should_not_segfault(i8 %x) unnamed_addr #0 { +start: + %_0 = icmp ult i8 %x, 16 + %or.cond = select i1 undef, i1 true, i1 %_0 + br i1 %or.cond, label %bb1, label %bb2 + +bb1: + ret void + +bb2: + call void @panic() + unreachable +} + attributes #0 = { "split-stack" } diff --git a/llvm/test/CodeGen/Thumb2/segmented-stacks.ll b/llvm/test/CodeGen/Thumb2/segmented-stacks.ll --- a/llvm/test/CodeGen/Thumb2/segmented-stacks.ll +++ b/llvm/test/CodeGen/Thumb2/segmented-stacks.ll @@ -171,4 +171,24 @@ ; ARM-NEXT: .long 40192 } + +declare void @panic() unnamed_addr + +; We used to crash while compiling the following function. +; THUMB-LABEL: build_should_not_segfault: +; ARM-LABEL: build_should_not_segfault: +define void @build_should_not_segfault(i8 %x) unnamed_addr #0 { +start: + %_0 = icmp ult i8 %x, 16 + %or.cond = select i1 undef, i1 true, i1 %_0 + br i1 %or.cond, label %bb1, label %bb2 + +bb1: + ret void + +bb2: + call void @panic() + unreachable +} + attributes #0 = { "split-stack" }