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,62 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -S -loop-reduce | FileCheck %s + +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 { +; CHECK-LABEL: @test1( +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[FOR_COND:%.*]] +; CHECK: for.cond: +; CHECK-NEXT: callbr void asm sideeffect "", "X,X,~{dirflag},~{fpsr},~{flags}"(i8* blockaddress(@test1, [[COND_TRUE_I:%.*]]), i8* blockaddress(@test1, [[FOR_END:%.*]])) +; CHECK-NEXT: to label [[ASM_FALLTHROUGH_I_I:%.*]] [label [[COND_TRUE_I]], label %for.end] +; CHECK: asm.fallthrough.i.i: +; CHECK-NEXT: unreachable +; CHECK: cond.true.i: +; CHECK-NEXT: br label [[DO_BODY_I_I_DO_BODY_I_I_CRIT_EDGE:%.*]] +; CHECK: do.body.i.i.do.body.i.i_crit_edge: +; CHECK-NEXT: [[PGOCOUNT711:%.*]] = phi i64 [ [[TMP0:%.*]], [[DO_BODY_I_I_DO_BODY_I_I_CRIT_EDGE]] ], [ 0, [[COND_TRUE_I]] ] +; CHECK-NEXT: [[TMP0]] = add nuw nsw i64 [[PGOCOUNT711]], 1 +; CHECK-NEXT: br i1 true, label [[DO_BODY_I_I_RDRAND_INT_EXIT_I_CRIT_EDGE:%.*]], label [[DO_BODY_I_I_DO_BODY_I_I_CRIT_EDGE]] +; CHECK: do.body.i.i.rdrand_int.exit.i_crit_edge: +; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[TMP0]], undef +; CHECK-NEXT: br i1 true, label [[FOR_END]], label [[FOR_INC:%.*]] +; CHECK: for.inc: +; CHECK-NEXT: br label [[FOR_COND]] +; CHECK: for.end: +; CHECK-NEXT: [[PGOCOUNT_PROMOTED24:%.*]] = phi i64 [ undef, [[FOR_COND]] ], [ [[TMP1]], [[DO_BODY_I_I_RDRAND_INT_EXIT_I_CRIT_EDGE]] ] +; CHECK-NEXT: ret i32 undef +; +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 +}