diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp --- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -5579,16 +5579,23 @@ << "\n"); return; } - // Bail out if we have a PHI on an EHPad that gets a value from a - // CatchSwitchInst. Because the CatchSwitchInst cannot be split, there is - // no good place to stick any instructions. + if (auto *PN = dyn_cast(U.getUser())) { auto *FirstNonPHI = PN->getParent()->getFirstNonPHI(); if (isa(FirstNonPHI) || isa(FirstNonPHI)) + // Bail out if we have a PHI on an EHPad that gets a value from a + // CatchSwitchInst. Because the CatchSwitchInst cannot be split, there + // is no good place to stick any instructions. for (BasicBlock *PredBB : PN->blocks()) if (isa(PredBB->getFirstNonPHI())) return; + + // Also bail out if we have a PHI with a value from a block ending in a + // CallBrInst, because those also can't be split for similar reasons. + for (BasicBlock *PredBB : PN->blocks()) + if (isa(PredBB->getTerminator())) + return; } } diff --git a/llvm/test/Transforms/LoopStrengthReduce/callbr-critical-edge-splitting.ll b/llvm/test/Transforms/LoopStrengthReduce/callbr-critical-edge-splitting.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/LoopStrengthReduce/callbr-critical-edge-splitting.ll @@ -0,0 +1,38 @@ +; RUN: opt < %s -o /dev/null -loop-reduce + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; Test that we don't try to break a critical edge to a block ending in a callbr +; instruction. See https://github.com/ClangBuiltLinux/linux/issues/1252 + +define dso_local i32 @test1() local_unnamed_addr { +entry: + br label %for.cond + +for.cond: ; preds = %for.inc, %entry + callbr void asm sideeffect "", "X,X,~{dirflag},~{fpsr},~{flags}"(i8* blockaddress(@test1, %cond.true.i), i8* blockaddress(@test1, %for.end)) + to label %asm.fallthrough.i.i [label %cond.true.i, label %for.end] + +asm.fallthrough.i.i: ; preds = %for.cond + unreachable + +cond.true.i: ; preds = %for.cond + br label %do.body.i.i.do.body.i.i_crit_edge + +do.body.i.i.do.body.i.i_crit_edge: ; preds = %do.body.i.i.do.body.i.i_crit_edge, %cond.true.i + %pgocount711 = phi i64 [ %0, %do.body.i.i.do.body.i.i_crit_edge ], [ 0, %cond.true.i ] + %0 = add nuw nsw i64 %pgocount711, 1 + br i1 undef, label %do.body.i.i.rdrand_int.exit.i_crit_edge, label %do.body.i.i.do.body.i.i_crit_edge + +do.body.i.i.rdrand_int.exit.i_crit_edge: ; preds = %do.body.i.i.do.body.i.i_crit_edge + %1 = add i64 %0, undef + br i1 undef, label %for.end, label %for.inc + +for.inc: ; preds = %do.body.i.i.rdrand_int.exit.i_crit_edge + br label %for.cond + +for.end: ; preds = %do.body.i.i.rdrand_int.exit.i_crit_edge, %for.cond + %pgocount.promoted24 = phi i64 [ undef, %for.cond ], [ %1, %do.body.i.i.rdrand_int.exit.i_crit_edge ] + ret i32 undef +}