@@ -2827,8 +2827,23 @@ struct NonTrivialUnswitchCandidate {
2827
2827
};
2828
2828
} // end anonymous namespace.
2829
2829
2830
- static Optional<NonTrivialUnswitchCandidate>
2831
- findBestNonTrivialUnswitchCandidate (
2830
+ static bool isSafeToClone (const Loop &L) {
2831
+ if (!L.isSafeToClone ())
2832
+ return false ;
2833
+ for (auto *BB : L.blocks ())
2834
+ for (auto &I : *BB) {
2835
+ if (I.getType ()->isTokenTy () && I.isUsedOutsideOfBlock (BB))
2836
+ return false ;
2837
+ if (auto *CB = dyn_cast<CallBase>(&I)) {
2838
+ assert (!CB->cannotDuplicate () && " Checked by L.isSafeToClone()." );
2839
+ if (CB->isConvergent ())
2840
+ return false ;
2841
+ }
2842
+ }
2843
+ return true ;
2844
+ }
2845
+
2846
+ static NonTrivialUnswitchCandidate findBestNonTrivialUnswitchCandidate (
2832
2847
ArrayRef<std::pair<Instruction *, TinyPtrVector<Value *> > >
2833
2848
UnswitchCandidates, const Loop &L, const DominatorTree &DT,
2834
2849
const LoopInfo &LI, AssumptionCache &AC, const TargetTransformInfo &TTI,
@@ -2857,13 +2872,6 @@ findBestNonTrivialUnswitchCandidate(
2857
2872
for (auto &I : *BB) {
2858
2873
if (EphValues.count (&I))
2859
2874
continue ;
2860
-
2861
- if (I.getType ()->isTokenTy () && I.isUsedOutsideOfBlock (BB))
2862
- return None;
2863
- if (auto *CB = dyn_cast<CallBase>(&I))
2864
- if (CB->isConvergent () || CB->cannotDuplicate ())
2865
- return None;
2866
-
2867
2875
Cost += TTI.getInstructionCost (&I, CostKind);
2868
2876
}
2869
2877
assert (Cost >= 0 && " Must not have negative costs!" );
@@ -3030,31 +3038,28 @@ static bool unswitchBestCondition(
3030
3038
dbgs () << " Considering " << UnswitchCandidates.size ()
3031
3039
<< " non-trivial loop invariant conditions for unswitching.\n " );
3032
3040
3033
- Optional<NonTrivialUnswitchCandidate> Best =
3034
- findBestNonTrivialUnswitchCandidate (UnswitchCandidates, L, DT, LI, AC,
3035
- TTI, PartialIVInfo);
3036
- if (!Best)
3037
- return false ;
3041
+ NonTrivialUnswitchCandidate Best = findBestNonTrivialUnswitchCandidate (
3042
+ UnswitchCandidates, L, DT, LI, AC, TTI, PartialIVInfo);
3038
3043
3039
- assert (Best-> TI && " Failed to find loop unswitch candidate" );
3044
+ assert (Best. TI && " Failed to find loop unswitch candidate" );
3040
3045
3041
- if (Best-> Cost >= UnswitchThreshold) {
3042
- LLVM_DEBUG (dbgs () << " Cannot unswitch, lowest cost found: " << Best-> Cost
3046
+ if (Best. Cost >= UnswitchThreshold) {
3047
+ LLVM_DEBUG (dbgs () << " Cannot unswitch, lowest cost found: " << Best. Cost
3043
3048
<< " \n " );
3044
3049
return false ;
3045
3050
}
3046
3051
3047
- if (Best-> TI != PartialIVCondBranch)
3052
+ if (Best. TI != PartialIVCondBranch)
3048
3053
PartialIVInfo.InstToDuplicate .clear ();
3049
3054
3050
3055
// If the best candidate is a guard, turn it into a branch.
3051
- if (isGuard (Best-> TI ))
3052
- Best-> TI = turnGuardIntoBranch (cast<IntrinsicInst>(Best-> TI ), L, ExitBlocks,
3053
- DT, LI, MSSAU);
3056
+ if (isGuard (Best. TI ))
3057
+ Best. TI = turnGuardIntoBranch (cast<IntrinsicInst>(Best. TI ), L, ExitBlocks,
3058
+ DT, LI, MSSAU);
3054
3059
3055
- LLVM_DEBUG (dbgs () << " Unswitching non-trivial (cost = " << Best-> Cost
3056
- << " ) terminator: " << *Best-> TI << " \n " );
3057
- unswitchNontrivialInvariants (L, *Best-> TI , Best-> Invariants , ExitBlocks,
3060
+ LLVM_DEBUG (dbgs () << " Unswitching non-trivial (cost = " << Best. Cost
3061
+ << " ) terminator: " << *Best. TI << " \n " );
3062
+ unswitchNontrivialInvariants (L, *Best. TI , Best. Invariants , ExitBlocks,
3058
3063
PartialIVInfo, DT, LI, AC, UnswitchCB, SE, MSSAU,
3059
3064
DestroyLoopCB);
3060
3065
return true ;
@@ -3133,7 +3138,7 @@ unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
3133
3138
}
3134
3139
3135
3140
// Skip non-trivial unswitching for loops that cannot be cloned.
3136
- if (!L. isSafeToClone ())
3141
+ if (!isSafeToClone (L ))
3137
3142
return false ;
3138
3143
3139
3144
// For non-trivial unswitching, because it often creates new loops, we rely on
0 commit comments