diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp --- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -2238,8 +2238,19 @@ BI->setSuccessor(1 - ClonedSucc, LoopPH); Value *Cond = skipTrivialSelect(BI->getCondition()); if (InsertFreeze) { - if (!isGuaranteedNotToBeUndefOrPoison(Cond, &AC, BI, &DT)) - Cond = new FreezeInst(Cond, Cond->getName() + ".fr", BI); + if (!isGuaranteedNotToBeUndefOrPoison(Cond, &AC, BI, &DT)) { + // directly insert the freeze inst to the place it will be hoisted by + // LICM, so that we do not require LICM as a following pass + Instruction *InsertPoint = BI; + while (Loop *OuterLoop = LI.getLoopFor(InsertPoint->getParent())) { + if (OuterLoop->isLoopInvariant(Cond)) { + InsertPoint = OuterLoop->getLoopPreheader()->getTerminator(); + } else { + break; + } + } + Cond = new FreezeInst(Cond, Cond->getName() + ".fr", InsertPoint); + } } BI->setCondition(Cond); DTUpdates.push_back({DominatorTree::Insert, SplitBB, ClonedPH}); diff --git a/llvm/test/Transforms/SimpleLoopUnswitch/exponential-nontrivial-unswitch-nested.ll b/llvm/test/Transforms/SimpleLoopUnswitch/exponential-nontrivial-unswitch-nested.ll --- a/llvm/test/Transforms/SimpleLoopUnswitch/exponential-nontrivial-unswitch-nested.ll +++ b/llvm/test/Transforms/SimpleLoopUnswitch/exponential-nontrivial-unswitch-nested.ll @@ -35,6 +35,11 @@ ; RUN: -passes='loop-mssa(licm,simple-loop-unswitch),print' -disable-output 2>&1 | \ ; RUN: sort -b -k 1 | FileCheck %s --check-prefixes=LOOP-UNSCALE4-DIV1 ; +; RUN: opt < %s -enable-unswitch-cost-multiplier=true \ +; RUN: -unswitch-num-initial-unscaled-candidates=4 -unswitch-siblings-toplevel-div=1 \ +; RUN: -passes='loop-unswitch-func,print' -disable-output 2>&1 | \ +; RUN: sort -b -k 1 | FileCheck %s --check-prefixes=LOOP-UNSCALE4-DIV1 +; ; NB: sort -b is essential here and below, otherwise blanks might lead to different ; order depending on locale. ; @@ -43,6 +48,11 @@ ; RUN: -passes='loop-mssa(licm,simple-loop-unswitch),print' -disable-output 2>&1 | \ ; RUN: sort -b -k 1 | FileCheck %s --check-prefixes=LOOP-UNSCALE4-DIV2 ; +; RUN: opt < %s -enable-unswitch-cost-multiplier=true \ +; RUN: -unswitch-num-initial-unscaled-candidates=4 -unswitch-siblings-toplevel-div=2 \ +; RUN: -passes='loop-unswitch-func,print' -disable-output 2>&1 | \ +; RUN: sort -b -k 1 | FileCheck %s --check-prefixes=LOOP-UNSCALE4-DIV2 +; ; Get ; 2^(num conds) == 2^5 = 32 ; loop nests when cost multiplier is disabled: @@ -51,6 +61,10 @@ ; RUN: -passes='loop-mssa(licm,simple-loop-unswitch),print' -disable-output 2>&1 | \ ; RUN: sort -b -k 1 | FileCheck %s --check-prefixes=LOOP32 ; +; RUN: opt < %s -enable-unswitch-cost-multiplier=false \ +; RUN: -passes='loop-unswitch-func,print' -disable-output 2>&1 | \ +; RUN: sort -b -k 1 | FileCheck %s --check-prefixes=LOOP32 +; ; Single loop nest, not unswitched ; LOOP1: Loop at depth 1 containing: ; LOOP1: Loop at depth 2 containing: diff --git a/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll b/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll --- a/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll +++ b/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll @@ -2437,8 +2437,9 @@ br label %bb3 ; CHECK-NOT: br i1 %a ; -; CHECK: %[[FROZEN:.+]] = freeze i1 %a -; CHECK-NEXT: br i1 %[[FROZEN]], label %[[BB_SPLIT_US:.*]], label %[[BB_SPLIT:.*]] +; CHECK: %[[FROZENA:.+]] = freeze i1 %a +; CHECK: %[[FROZENB:.+]] = freeze i1 %b +; CHECK-NEXT: br i1 %[[FROZENA]], label %[[BB_SPLIT_US:.*]], label %[[BB_SPLIT:.*]] ; ; CHECK-NOT: br i1 %a ; CHECK-NOT: br i1 %b