Index: lib/CodeGen/ShrinkWrap.cpp =================================================================== --- lib/CodeGen/ShrinkWrap.cpp +++ lib/CodeGen/ShrinkWrap.cpp @@ -282,8 +282,14 @@ if (!Restore) Restore = &MBB; - else + else if (MPDT->getNode(&MBB)) // If the block is not in the post dom tree, it + // means the block never returns. If that's the + // case, we don't want to call + // `findNearestCommonDominator`, which will + // return `Restore`. Restore = MPDT->findNearestCommonDominator(Restore, &MBB); + else + Restore = nullptr; // Make sure we would be able to insert the restore code before the // terminator. Index: test/CodeGen/X86/x86-shrink-wrapping.ll =================================================================== --- test/CodeGen/X86/x86-shrink-wrapping.ll +++ test/CodeGen/X86/x86-shrink-wrapping.ll @@ -270,8 +270,6 @@ ret i32 %sum.1 } -declare void @somethingElse(...) - ; Check with a more complex case that we do not have restore within the loop and ; save outside. ; CHECK-LABEL: loopInfoRestoreOutsideLoop: @@ -982,3 +980,54 @@ } attributes #4 = { "no-frame-pointer-elim"="true" } + +@x = external global i32, align 4 +@y = external global i32, align 4 + +; The post-dominator tree does not include the branch containing the infinite +; loop, which can occur into a misplacement of the restore block, if we're +; looking for the nearest common post-dominator of an "unreachable" block. + +; CHECK-LABEL: infiniteLoopNoSuccessor: +; CHECK: ## BB#0: +; Make sure the prologue happens in the entry block. +; CHECK-NEXT: pushq %rbp +; ... +; Make sure we don't shrink-wrap. +; CHECK: ## BB#1 +; CHECK-NOT: pushq %rbp +; ... +; Make sure the epilogue happens in the exit block. +; CHECK: ## BB#5 +; CHECK: popq %rbp +; CHECK-NEXT: retq +define void @infiniteLoopNoSuccessor() #5 { + %1 = load i32, i32* @x, align 4 + %2 = icmp ne i32 %1, 0 + br i1 %2, label %3, label %4 + +;