Skip to content

Commit 80414ca

Browse files
committed
[NFC] Reorganize LoopRotate
1 parent 04f1cde commit 80414ca

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

lib/SILOptimizer/LoopTransforms/LoopRotate.cpp

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,45 +69,59 @@ canDuplicateOrMoveToPreheader(SILLoop *loop, SILBasicBlock *preheader,
6969
llvm::DenseSet<SILInstruction *> invariants;
7070
int cost = 0;
7171
for (auto &instRef : *bb) {
72-
OwnershipForwardingMixin *ofm = nullptr;
7372
auto *inst = &instRef;
74-
if (auto *MI = dyn_cast<MethodInst>(inst)) {
75-
if (MI->getMember().isForeign)
76-
return false;
77-
if (!hasLoopInvariantOperands(inst, loop, invariants))
78-
continue;
79-
moves.push_back(inst);
80-
invariants.insert(inst);
81-
} else if (!inst->isTriviallyDuplicatable())
73+
if (!inst->isTriviallyDuplicatable()) {
8274
return false;
75+
}
8376
// It wouldn't make sense to rotate dealloc_stack without also rotating the
8477
// alloc_stack, which is covered by isTriviallyDuplicatable.
85-
else if (isa<DeallocStackInst>(inst))
78+
if (isa<DeallocStackInst>(inst)) {
8679
return false;
87-
else if (isa<FunctionRefInst>(inst)) {
80+
}
81+
OwnershipForwardingMixin *ofm = nullptr;
82+
if ((ofm = OwnershipForwardingMixin::get(inst)) &&
83+
ofm->getForwardingOwnershipKind() == OwnershipKind::Guaranteed) {
84+
return false;
85+
}
86+
if (isa<FunctionRefInst>(inst)) {
8887
moves.push_back(inst);
8988
invariants.insert(inst);
90-
} else if (isa<DynamicFunctionRefInst>(inst)) {
89+
continue;
90+
}
91+
if (isa<DynamicFunctionRefInst>(inst)) {
92+
moves.push_back(inst);
93+
invariants.insert(inst);
94+
continue;
95+
}
96+
if (isa<PreviousDynamicFunctionRefInst>(inst)) {
9197
moves.push_back(inst);
9298
invariants.insert(inst);
93-
} else if (isa<PreviousDynamicFunctionRefInst>(inst)) {
99+
continue;
100+
}
101+
if (isa<IntegerLiteralInst>(inst)) {
94102
moves.push_back(inst);
95103
invariants.insert(inst);
96-
} else if (isa<IntegerLiteralInst>(inst)) {
104+
continue;
105+
}
106+
if (auto *MI = dyn_cast<MethodInst>(inst)) {
107+
if (MI->getMember().isForeign)
108+
return false;
109+
if (!hasLoopInvariantOperands(inst, loop, invariants))
110+
continue;
97111
moves.push_back(inst);
98112
invariants.insert(inst);
99-
} else if ((ofm = OwnershipForwardingMixin::get(inst)) &&
100-
ofm->getForwardingOwnershipKind() == OwnershipKind::Guaranteed) {
101-
return false;
102-
} else if (!inst->mayHaveSideEffects() && !inst->mayReadFromMemory()
103-
&& !isa<TermInst>(inst) && !isa<AllocationInst>(inst)
104-
&& /* not marked mayhavesideffects */
105-
hasLoopInvariantOperands(inst, loop, invariants)) {
113+
continue;
114+
}
115+
if (!inst->mayHaveSideEffects() && !inst->mayReadFromMemory() &&
116+
!isa<TermInst>(inst) &&
117+
!isa<AllocationInst>(inst) && /* not marked mayhavesideffects */
118+
hasLoopInvariantOperands(inst, loop, invariants)) {
106119
moves.push_back(inst);
107120
invariants.insert(inst);
108-
} else {
109-
cost += (int)instructionInlineCost(instRef);
121+
continue;
110122
}
123+
124+
cost += (int)instructionInlineCost(instRef);
111125
}
112126

113127
return cost < LoopRotateSizeLimit;
@@ -425,6 +439,7 @@ bool swift::rotateLoop(SILLoop *loop, DominanceInfo *domInfo,
425439

426440
// The other instructions are just cloned to the preheader.
427441
TermInst *preheaderBranch = preheader->getTerminator();
442+
428443
for (auto &inst : *header) {
429444
if (SILInstruction *cloned = inst.clone(preheaderBranch)) {
430445
mapOperands(cloned, valueMap);
@@ -488,16 +503,10 @@ namespace {
488503
class LoopRotation : public SILFunctionTransform {
489504

490505
void run() override {
506+
SILFunction *f = getFunction();
491507
SILLoopAnalysis *loopAnalysis = PM->getAnalysis<SILLoopAnalysis>();
492-
assert(loopAnalysis);
493508
DominanceAnalysis *domAnalysis = PM->getAnalysis<DominanceAnalysis>();
494-
assert(domAnalysis);
495-
496-
SILFunction *f = getFunction();
497-
assert(f);
498-
499509
SILLoopInfo *loopInfo = loopAnalysis->get(f);
500-
assert(loopInfo);
501510
DominanceInfo *domInfo = domAnalysis->get(f);
502511

503512
if (loopInfo->empty()) {

0 commit comments

Comments
 (0)