diff --git a/llvm/lib/Transforms/Utils/FixIrreducible.cpp b/llvm/lib/Transforms/Utils/FixIrreducible.cpp --- a/llvm/lib/Transforms/Utils/FixIrreducible.cpp +++ b/llvm/lib/Transforms/Utils/FixIrreducible.cpp @@ -281,6 +281,9 @@ LLVM_DEBUG(dbgs() << "Found headers:"); for (auto BB : reverse(Blocks)) { for (const auto P : predecessors(BB)) { + // Skip unreachable predecessors. + if (!DT.isReachableFromEntry(P)) + continue; if (!Blocks.count(P)) { LLVM_DEBUG(dbgs() << " " << BB->getName()); Headers.insert(BB); diff --git a/llvm/test/Transforms/FixIrreducible/unreachable.ll b/llvm/test/Transforms/FixIrreducible/unreachable.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/FixIrreducible/unreachable.ll @@ -0,0 +1,24 @@ +; RUN: opt %s -fix-irreducible -S -o - | FileCheck %s + +; CHECK-LABEL: @unreachable( +; CHECK: entry: +; CHECK-NOT: irr.guard: +define void @unreachable(i32 %n) { +entry: + br label %loop.body + +loop.body: + br label %inner.block + +unreachable.block: + br label %inner.block + +inner.block: + br i1 undef, label %loop.exit, label %loop.latch + +loop.latch: + br label %loop.body + +loop.exit: + ret void +}