Skip to content

Commit 6bfcac6

Browse files
committed
[SimpleLoopUnswitch][NFC] Separate legality checks from cost computation
These are semantically two different stages, but were entwined in the old implementation. Now cost computation does not do legality checks, and they all are done beforehead.
1 parent 421728b commit 6bfcac6

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,8 +2827,23 @@ struct NonTrivialUnswitchCandidate {
28272827
};
28282828
} // end anonymous namespace.
28292829

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(
28322847
ArrayRef<std::pair<Instruction *, TinyPtrVector<Value *> > >
28332848
UnswitchCandidates, const Loop &L, const DominatorTree &DT,
28342849
const LoopInfo &LI, AssumptionCache &AC, const TargetTransformInfo &TTI,
@@ -2857,13 +2872,6 @@ findBestNonTrivialUnswitchCandidate(
28572872
for (auto &I : *BB) {
28582873
if (EphValues.count(&I))
28592874
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-
28672875
Cost += TTI.getInstructionCost(&I, CostKind);
28682876
}
28692877
assert(Cost >= 0 && "Must not have negative costs!");
@@ -3030,31 +3038,28 @@ static bool unswitchBestCondition(
30303038
dbgs() << "Considering " << UnswitchCandidates.size()
30313039
<< " non-trivial loop invariant conditions for unswitching.\n");
30323040

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);
30383043

3039-
assert(Best->TI && "Failed to find loop unswitch candidate");
3044+
assert(Best.TI && "Failed to find loop unswitch candidate");
30403045

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
30433048
<< "\n");
30443049
return false;
30453050
}
30463051

3047-
if (Best->TI != PartialIVCondBranch)
3052+
if (Best.TI != PartialIVCondBranch)
30483053
PartialIVInfo.InstToDuplicate.clear();
30493054

30503055
// 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);
30543059

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,
30583063
PartialIVInfo, DT, LI, AC, UnswitchCB, SE, MSSAU,
30593064
DestroyLoopCB);
30603065
return true;
@@ -3133,7 +3138,7 @@ unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
31333138
}
31343139

31353140
// Skip non-trivial unswitching for loops that cannot be cloned.
3136-
if (!L.isSafeToClone())
3141+
if (!isSafeToClone(L))
31373142
return false;
31383143

31393144
// For non-trivial unswitching, because it often creates new loops, we rely on

0 commit comments

Comments
 (0)