Skip to content

Commit baab923

Browse files
committed
DeadCodeElimination: a small cleanup
Split `markValueLive(SILNode *)` into `markValueLive(SILValue)` and `markInstructionLive(SILInstruction*)`
1 parent 65ecb69 commit baab923

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ class DCE : public SILFunctionTransform {
188188
void computeMinPredecessorLevels(PostDomTreeNode *root);
189189
void insertControllingInfo(SILBasicBlock *Block, unsigned Level);
190190

191-
void markValueLive(SILNode *V);
191+
void markValueLive(SILValue V);
192+
void markInstructionLive(SILInstruction *Inst);
192193
void markTerminatorArgsLive(SILBasicBlock *Pred, SILBasicBlock *Succ,
193194
size_t ArgIndex);
194195
void markControllingTerminatorsLive(SILBasicBlock *Block);
@@ -208,32 +209,30 @@ class DCE : public SILFunctionTransform {
208209

209210
// Keep track of the fact that V is live and add it to our worklist
210211
// so that we can process the values it depends on.
211-
void DCE::markValueLive(SILNode *V) {
212-
V = V->getRepresentativeSILNodeInObject();
213-
if (LiveValues.count(V) || isa<SILUndef>(V))
214-
return;
215-
216-
LLVM_DEBUG(llvm::dbgs() << "Marking as live:\n");
217-
LLVM_DEBUG(V->dump());
218-
219-
LiveValues.insert(V);
212+
void DCE::markValueLive(SILValue V) {
213+
if (SILInstruction *inst = V->getDefiningInstruction())
214+
return markInstructionLive(inst);
220215

221-
if (auto *Def = dyn_cast<SILInstruction>(V)) {
222-
markControllingTerminatorsLive(Def->getParent());
223-
Worklist.push_back(Def);
216+
if (!LiveValues.insert(V).second || isa<SILUndef>(V))
224217
return;
225-
}
226-
227-
// TODO: MultiValueInstruction
228218

229-
assert(isa<SILArgument>(V) &&
230-
"Only expected instructions and arguments!");
219+
LLVM_DEBUG(llvm::dbgs() << "Marking as live: " << *V);
231220

232221
auto *Arg = cast<SILArgument>(V);
233222
markControllingTerminatorsLive(Arg->getParent());
234223
propagateLiveBlockArgument(Arg);
235224
}
236225

226+
void DCE::markInstructionLive(SILInstruction *Inst) {
227+
if (!LiveValues.insert(Inst).second)
228+
return;
229+
230+
LLVM_DEBUG(llvm::dbgs() << "Marking as live: " << *Inst);
231+
232+
markControllingTerminatorsLive(Inst->getParent());
233+
Worklist.push_back(Inst);
234+
}
235+
237236
/// Gets the producing instruction of a cond_fail condition. Currently these
238237
/// are overflow builtins but may be extended to other instructions in the
239238
/// future.
@@ -263,7 +262,7 @@ void DCE::markLive(SILFunction &F) {
263262
if (auto *Prod = getProducer(cast<CondFailInst>(&I))) {
264263
addReverseDependency(Prod, &I);
265264
} else {
266-
markValueLive(&I);
265+
markInstructionLive(&I);
267266
}
268267
break;
269268
}
@@ -272,7 +271,7 @@ void DCE::markLive(SILFunction &F) {
272271
if (!Op->getType().isAddress()) {
273272
addReverseDependency(Op, &I);
274273
} else {
275-
markValueLive(&I);
274+
markInstructionLive(&I);
276275
}
277276
break;
278277
}
@@ -290,7 +289,7 @@ void DCE::markLive(SILFunction &F) {
290289
}
291290
default:
292291
if (seemsUseful(&I))
293-
markValueLive(&I);
292+
markInstructionLive(&I);
294293
}
295294
}
296295
}
@@ -319,7 +318,7 @@ void DCE::markTerminatorArgsLive(SILBasicBlock *Pred,
319318

320319
// If the arguments are live, we need to keep the terminator that
321320
// delivers those arguments.
322-
markValueLive(Term);
321+
markInstructionLive(Term);
323322

324323
switch (Term->getTermKind()) {
325324
case TermKind::ReturnInst:
@@ -380,11 +379,11 @@ void DCE::propagateLiveBlockArgument(SILArgument *Arg) {
380379
// is in reverse direction: Only if its definition (the Arg) is alive, also
381380
// the debug_value instruction is alive.
382381
for (Operand *DU : getDebugUses(Arg))
383-
markValueLive(DU->getUser());
382+
markInstructionLive(DU->getUser());
384383

385384
// Mark all reverse dependencies on the Arg live
386385
for (auto *depInst : ReverseDependencies.lookup(Arg)) {
387-
markValueLive(depInst);
386+
markInstructionLive(depInst);
388387
}
389388

390389
auto *Block = Arg->getParent();
@@ -406,14 +405,14 @@ void DCE::propagateLiveness(SILInstruction *I) {
406405
// debug_value instruction is alive.
407406
for (auto result : I->getResults())
408407
for (Operand *DU : getDebugUses(result))
409-
markValueLive(DU->getUser());
408+
markInstructionLive(DU->getUser());
410409

411410
// Handle all other reverse-dependency instructions, like cond_fail,
412411
// fix_lifetime, destroy_value, etc. Only if the definition is alive, the
413412
// user itself is alive.
414413
for (auto res : I->getResults()) {
415414
for (auto *depInst : ReverseDependencies.lookup(res)) {
416-
markValueLive(depInst);
415+
markInstructionLive(depInst);
417416
}
418417
}
419418
return;
@@ -802,7 +801,7 @@ void DCE::markControllingTerminatorsLive(SILBasicBlock *Block) {
802801
collectControllingBlocks(Block, ControllingBlocks);
803802

804803
for (auto BB : ControllingBlocks)
805-
markValueLive(BB->getTerminator());
804+
markInstructionLive(BB->getTerminator());
806805
}
807806

808807
} // end anonymous namespace

0 commit comments

Comments
 (0)