Skip to content

Commit 61c66df

Browse files
committed
Remove fake use of lexical copy
1 parent 6be938b commit 61c66df

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

include/swift/SILOptimizer/Utils/CFGOptUtils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ TermInst *changeEdgeValue(TermInst *branch, SILBasicBlock *dest, size_t idx,
6565
/// specified index. Asserts internally that the argument along the edge does
6666
/// not have uses.
6767
TermInst *deleteEdgeValue(TermInst *branch, SILBasicBlock *destBlock,
68-
size_t argIndex);
68+
size_t argIndex, bool cleanupDeadPhiOp = true);
6969

7070
/// Erase the \p argIndex phi argument from \p block. Asserts that the argument
7171
/// is a /real/ phi argument. Removes all incoming values for the argument from
7272
/// predecessor terminators. Asserts internally that it only ever is given
7373
/// "true" phi argument.
74-
void erasePhiArgument(SILBasicBlock *block, unsigned argIndex);
74+
void erasePhiArgument(SILBasicBlock *block, unsigned argIndex,
75+
bool cleanupDeadPhiOp = true);
7576

7677
/// Replace a branch target.
7778
///

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -698,23 +698,6 @@ StoreInst *StackAllocationPromoter::promoteAllocationInBlock(
698698
oldRunningVals->value);
699699
}
700700
runningVals = beginLexicalLifetimeAfterStore(asi, si);
701-
// Create a use of the newly created copy in order to keep phi pruning
702-
// from deleting our lifetime beginning instructions.
703-
//
704-
// TODO: Remove this hack, it is only necessary because erasePhiArgument
705-
// calls deleteEdgeValue which calls
706-
// deleteTriviallyDeadOperandsOfDeadArgument and deletes the copy
707-
// and borrow that we added and want not to have deleted.
708-
SILBuilderWithScope::insertAfter(
709-
runningVals->value.copy->getDefiningInstruction(),
710-
[&](auto builder) {
711-
SILLocation loc = RegularLocation::getAutoGeneratedLocation();
712-
auto *useOfCopy =
713-
builder.createCopyValue(loc, runningVals->value.copy);
714-
// Note that we don't call prepareToDelete useOfCopy because we
715-
// specifically want this instruction to remain a use of copy.
716-
instructionsToDelete.push_back(useOfCopy);
717-
});
718701
}
719702
continue;
720703
}
@@ -1060,7 +1043,8 @@ void StackAllocationPromoter::fixBranchesAndUses(BlockSetVector &phiBlocks,
10601043
auto *phi = cast<SILPhiArgument>(
10611044
block->getArgument(block->getNumArguments() - 1));
10621045
phi->replaceAllUsesWithUndef();
1063-
erasePhiArgument(block, block->getNumArguments() - 1);
1046+
erasePhiArgument(block, block->getNumArguments() - 1,
1047+
/*cleanupDeadPhiOp*/ false);
10641048
};
10651049
for (auto *block : phiBlocks) {
10661050
auto *proactivePhi = cast<SILPhiArgument>(

lib/SILOptimizer/Utils/CFGOptUtils.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ deleteTriviallyDeadOperandsOfDeadArgument(MutableArrayRef<Operand> termOperands,
8888
// Our implementation assumes that our caller is attempting to remove a dead
8989
// SILPhiArgument from a SILBasicBlock and has already RAUWed the argument.
9090
TermInst *swift::deleteEdgeValue(TermInst *branch, SILBasicBlock *destBlock,
91-
size_t argIndex) {
91+
size_t argIndex, bool cleanupDeadPhiOps) {
9292
if (auto *cbi = dyn_cast<CondBranchInst>(branch)) {
9393
SmallVector<SILValue, 8> trueArgs;
9494
SmallVector<SILValue, 8> falseArgs;
@@ -97,14 +97,18 @@ TermInst *swift::deleteEdgeValue(TermInst *branch, SILBasicBlock *destBlock,
9797
llvm::copy(cbi->getFalseArgs(), std::back_inserter(falseArgs));
9898

9999
if (destBlock == cbi->getTrueBB()) {
100-
deleteTriviallyDeadOperandsOfDeadArgument(cbi->getTrueOperands(),
101-
argIndex);
100+
if (cleanupDeadPhiOps) {
101+
deleteTriviallyDeadOperandsOfDeadArgument(cbi->getTrueOperands(),
102+
argIndex);
103+
}
102104
trueArgs.erase(trueArgs.begin() + argIndex);
103105
}
104106

105107
if (destBlock == cbi->getFalseBB()) {
106-
deleteTriviallyDeadOperandsOfDeadArgument(cbi->getFalseOperands(),
107-
argIndex);
108+
if (cleanupDeadPhiOps) {
109+
deleteTriviallyDeadOperandsOfDeadArgument(cbi->getFalseOperands(),
110+
argIndex);
111+
}
108112
falseArgs.erase(falseArgs.begin() + argIndex);
109113
}
110114

@@ -120,8 +124,9 @@ TermInst *swift::deleteEdgeValue(TermInst *branch, SILBasicBlock *destBlock,
120124
if (auto *bi = dyn_cast<BranchInst>(branch)) {
121125
SmallVector<SILValue, 8> args;
122126
llvm::copy(bi->getArgs(), std::back_inserter(args));
123-
124-
deleteTriviallyDeadOperandsOfDeadArgument(bi->getAllOperands(), argIndex);
127+
if (cleanupDeadPhiOps) {
128+
deleteTriviallyDeadOperandsOfDeadArgument(bi->getAllOperands(), argIndex);
129+
}
125130
args.erase(args.begin() + argIndex);
126131
auto *result = SILBuilderWithScope(bi).createBranch(bi->getLoc(),
127132
bi->getDestBB(), args);
@@ -132,7 +137,8 @@ TermInst *swift::deleteEdgeValue(TermInst *branch, SILBasicBlock *destBlock,
132137
llvm_unreachable("unsupported terminator");
133138
}
134139

135-
void swift::erasePhiArgument(SILBasicBlock *block, unsigned argIndex) {
140+
void swift::erasePhiArgument(SILBasicBlock *block, unsigned argIndex,
141+
bool cleanupDeadPhiOps) {
136142
assert(block->getArgument(argIndex)->isPhi()
137143
&& "Only should be used on phi arguments");
138144
block->eraseArgument(argIndex);
@@ -149,7 +155,7 @@ void swift::erasePhiArgument(SILBasicBlock *block, unsigned argIndex) {
149155
predBlocks.insert(pred);
150156

151157
for (auto *pred : predBlocks)
152-
deleteEdgeValue(pred->getTerminator(), block, argIndex);
158+
deleteEdgeValue(pred->getTerminator(), block, argIndex, cleanupDeadPhiOps);
153159
}
154160

155161
/// Changes the edge value between a branch and destination basic block

0 commit comments

Comments
 (0)