Skip to content

Commit 1ac17a1

Browse files
committed
[NFC] SILCombine: Extract processing function.
The pass is structured to drain an instruction worklist and perform a sequence of operations on each popped instruction. Extract that sequence of operations into a new processInstruction function. Enables testing the sequence on a single instruction.
1 parent 1391db4 commit 1ac17a1

File tree

2 files changed

+78
-69
lines changed

2 files changed

+78
-69
lines changed

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 75 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -418,90 +418,96 @@ bool SILCombiner::doOneIteration(SILFunction &F, unsigned Iteration) {
418418
if (!parentTransform->continueWithNextSubpassRun(I))
419419
return false;
420420

421-
// Check to see if we can DCE the instruction.
422-
if (isInstructionTriviallyDead(I)) {
423-
LLVM_DEBUG(llvm::dbgs() << "SC: DCE: " << *I << '\n');
424-
eraseInstFromFunction(*I);
425-
++NumDeadInst;
426-
MadeChange = true;
427-
continue;
428-
}
421+
processInstruction(I, scCanonicalize, MadeChange);
422+
}
423+
424+
Worklist.resetChecked();
425+
return MadeChange;
426+
}
427+
428+
void SILCombiner::processInstruction(SILInstruction *I,
429+
SILCombineCanonicalize &scCanonicalize,
430+
bool &MadeChange) {
431+
// Check to see if we can DCE the instruction.
432+
if (isInstructionTriviallyDead(I)) {
433+
LLVM_DEBUG(llvm::dbgs() << "SC: DCE: " << *I << '\n');
434+
eraseInstFromFunction(*I);
435+
++NumDeadInst;
436+
MadeChange = true;
437+
return;
438+
}
429439
#ifndef NDEBUG
430-
std::string OrigIStr;
440+
std::string OrigIStr;
431441
#endif
432-
LLVM_DEBUG(llvm::raw_string_ostream SS(OrigIStr); I->print(SS);
433-
OrigIStr = SS.str(););
434-
LLVM_DEBUG(llvm::dbgs() << "SC: Visiting: " << OrigIStr << '\n');
442+
LLVM_DEBUG(llvm::raw_string_ostream SS(OrigIStr); I->print(SS);
443+
OrigIStr = SS.str(););
444+
LLVM_DEBUG(llvm::dbgs() << "SC: Visiting: " << OrigIStr << '\n');
435445

436-
// Canonicalize the instruction.
437-
if (scCanonicalize.tryCanonicalize(I)) {
438-
MadeChange = true;
439-
continue;
440-
}
446+
// Canonicalize the instruction.
447+
if (scCanonicalize.tryCanonicalize(I)) {
448+
MadeChange = true;
449+
return;
450+
}
441451

442-
// If we have reached this point, all attempts to do simple simplifications
443-
// have failed. First if we have an owned forwarding value, we try to
444-
// sink. Otherwise, we perform the actual SILCombine operation.
445-
if (EnableSinkingOwnedForwardingInstToUses) {
446-
// If we have an ownership forwarding single value inst that forwards
447-
// through its first argument and it is trivially duplicatable, see if it
448-
// only has consuming uses. If so, we can duplicate the instruction into
449-
// the consuming use blocks and destroy any destroy_value uses of it that
450-
// we see. This makes it easier for SILCombine to fold instructions with
451-
// owned parameters since chains of these values will be in the same
452-
// block.
453-
if (auto *svi = dyn_cast<SingleValueInstruction>(I)) {
454-
if (auto fwdOp = ForwardingOperation(svi)) {
455-
if (fwdOp.getSingleForwardingOperand() &&
456-
SILValue(svi)->getOwnershipKind() == OwnershipKind::Owned) {
457-
// Try to sink the value. If we sank the value and deleted it,
458-
// continue. If we didn't optimize or sank but we are still able to
459-
// optimize further, we fall through to SILCombine below.
460-
if (trySinkOwnedForwardingInst(svi)) {
461-
continue;
462-
}
452+
// If we have reached this point, all attempts to do simple simplifications
453+
// have failed. First if we have an owned forwarding value, we try to
454+
// sink. Otherwise, we perform the actual SILCombine operation.
455+
if (EnableSinkingOwnedForwardingInstToUses) {
456+
// If we have an ownership forwarding single value inst that forwards
457+
// through its first argument and it is trivially duplicatable, see if it
458+
// only has consuming uses. If so, we can duplicate the instruction into
459+
// the consuming use blocks and destroy any destroy_value uses of it that
460+
// we see. This makes it easier for SILCombine to fold instructions with
461+
// owned parameters since chains of these values will be in the same
462+
// block.
463+
if (auto *svi = dyn_cast<SingleValueInstruction>(I)) {
464+
if (auto fwdOp = ForwardingOperation(svi)) {
465+
if (fwdOp.getSingleForwardingOperand() &&
466+
SILValue(svi)->getOwnershipKind() == OwnershipKind::Owned) {
467+
// Try to sink the value. If we sank the value and deleted it,
468+
// return. If we didn't optimize or sank but we are still able to
469+
// optimize further, we fall through to SILCombine below.
470+
if (trySinkOwnedForwardingInst(svi)) {
471+
return;
463472
}
464473
}
465474
}
466475
}
476+
}
467477

468-
// Then begin... SILCombine.
469-
Builder.setInsertionPoint(I);
478+
// Then begin... SILCombine.
479+
Builder.setInsertionPoint(I);
470480

471-
SILInstruction *currentInst = I;
472-
if (SILInstruction *Result = visit(I)) {
473-
++NumCombined;
474-
// Should we replace the old instruction with a new one?
475-
Worklist.replaceInstructionWithInstruction(I, Result
481+
SILInstruction *currentInst = I;
482+
if (SILInstruction *Result = visit(I)) {
483+
++NumCombined;
484+
// Should we replace the old instruction with a new one?
485+
Worklist.replaceInstructionWithInstruction(I, Result
476486
#ifndef NDEBUG
477-
,
478-
OrigIStr
487+
,
488+
OrigIStr
479489
#endif
480-
);
481-
currentInst = Result;
482-
MadeChange = true;
483-
}
490+
);
491+
currentInst = Result;
492+
MadeChange = true;
493+
}
484494

485-
// Eliminate copies created that this SILCombine iteration may have
486-
// introduced during OSSA-RAUW.
487-
canonicalizeOSSALifetimes(currentInst->isDeleted() ? nullptr : currentInst);
488-
489-
// Builder's tracking list has been accumulating instructions created by the
490-
// during this SILCombine iteration. To finish this iteration, go through
491-
// the tracking list and add its contents to the worklist and then clear
492-
// said list in preparation for the next iteration.
493-
for (SILInstruction *I : *Builder.getTrackingList()) {
494-
if (!I->isDeleted()) {
495-
LLVM_DEBUG(llvm::dbgs()
496-
<< "SC: add " << *I << " from tracking list to worklist\n");
497-
Worklist.add(I);
498-
}
495+
// Eliminate copies created that this SILCombine iteration may have
496+
// introduced during OSSA-RAUW.
497+
canonicalizeOSSALifetimes(currentInst->isDeleted() ? nullptr : currentInst);
498+
499+
// Builder's tracking list has been accumulating instructions created by the
500+
// during this SILCombine iteration. To finish this iteration, go through
501+
// the tracking list and add its contents to the worklist and then clear
502+
// said list in preparation for the next iteration.
503+
for (SILInstruction *I : *Builder.getTrackingList()) {
504+
if (!I->isDeleted()) {
505+
LLVM_DEBUG(llvm::dbgs()
506+
<< "SC: add " << *I << " from tracking list to worklist\n");
507+
Worklist.add(I);
499508
}
500-
Builder.getTrackingList()->clear();
501509
}
502-
503-
Worklist.resetChecked();
504-
return MadeChange;
510+
Builder.getTrackingList()->clear();
505511
}
506512

507513
namespace swift::test {

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ class SILCombiner :
417417
/// Perform one SILCombine iteration.
418418
bool doOneIteration(SILFunction &F, unsigned Iteration);
419419

420+
void processInstruction(SILInstruction *instruction,
421+
SILCombineCanonicalize &scCanonicalize,
422+
bool &MadeChange);
420423
/// Add reachable code to the worklist. Meant to be used when starting to
421424
/// process a new function.
422425
void addReachableCodeToWorklist(SILBasicBlock *BB);

0 commit comments

Comments
 (0)