@@ -97,7 +97,8 @@ class DCE {
97
97
typedef llvm::DomTreeNodeBase<SILBasicBlock> PostDomTreeNode;
98
98
99
99
SILFunction *F;
100
- llvm::SmallPtrSet<SILNode *, 16 > LiveValues;
100
+ llvm::SmallPtrSet<SILArgument *, 16 > LiveArguments;
101
+ llvm::SmallPtrSet<SILInstruction *, 16 > LiveInstructions;
101
102
BasicBlockSet LiveBlocks;
102
103
llvm::SmallVector<SILInstruction *, 64 > Worklist;
103
104
PostDominanceInfo *PDT;
@@ -138,7 +139,8 @@ class DCE {
138
139
void computeMinPredecessorLevels (PostDomTreeNode *root);
139
140
void insertControllingInfo (SILBasicBlock *Block, unsigned Level);
140
141
141
- void markValueLive (SILNode *V);
142
+ void markValueLive (SILValue V);
143
+ void markInstructionLive (SILInstruction *Inst);
142
144
void markTerminatorArgsLive (SILBasicBlock *Pred, SILBasicBlock *Succ,
143
145
size_t ArgIndex);
144
146
void markControllingTerminatorsLive (SILBasicBlock *Block);
@@ -173,32 +175,33 @@ class DCE {
173
175
174
176
// Keep track of the fact that V is live and add it to our worklist
175
177
// so that we can process the values it depends on.
176
- void DCE::markValueLive (SILNode *V) {
177
- V = V->getRepresentativeSILNodeInObject ();
178
- if (LiveValues.count (V) || isa<SILUndef>(V))
179
- return ;
180
-
181
- LLVM_DEBUG (llvm::dbgs () << " Marking as live:\n " );
182
- LLVM_DEBUG (V->dump ());
178
+ void DCE::markValueLive (SILValue V) {
179
+ if (SILInstruction *inst = V->getDefiningInstruction ())
180
+ return markInstructionLive (inst);
183
181
184
- LiveValues.insert (V);
185
-
186
- if (auto *Def = dyn_cast<SILInstruction>(V)) {
187
- markControllingTerminatorsLive (Def->getParent ());
188
- Worklist.push_back (Def);
182
+ if (isa<SILUndef>(V))
189
183
return ;
190
- }
191
184
192
- // TODO: MultiValueInstruction
193
-
194
- assert (isa<SILArgument>(V) &&
195
- " Only expected instructions and arguments!" );
185
+ LLVM_DEBUG (llvm::dbgs () << " Marking as live: " << *V);
196
186
197
187
auto *Arg = cast<SILArgument>(V);
188
+ if (!LiveArguments.insert (Arg).second )
189
+ return ;
190
+
198
191
markControllingTerminatorsLive (Arg->getParent ());
199
192
propagateLiveBlockArgument (Arg);
200
193
}
201
194
195
+ void DCE::markInstructionLive (SILInstruction *Inst) {
196
+ if (!LiveInstructions.insert (Inst).second )
197
+ return ;
198
+
199
+ LLVM_DEBUG (llvm::dbgs () << " Marking as live: " << *Inst);
200
+
201
+ markControllingTerminatorsLive (Inst->getParent ());
202
+ Worklist.push_back (Inst);
203
+ }
204
+
202
205
// / Gets the producing instruction of a cond_fail condition. Currently these
203
206
// / are overflow builtins but may be extended to other instructions in the
204
207
// / future.
@@ -228,7 +231,7 @@ void DCE::markLive() {
228
231
if (auto *Prod = getProducer (cast<CondFailInst>(&I))) {
229
232
addReverseDependency (Prod, &I);
230
233
} else {
231
- markValueLive (&I);
234
+ markInstructionLive (&I);
232
235
}
233
236
break ;
234
237
}
@@ -237,7 +240,7 @@ void DCE::markLive() {
237
240
if (!Op->getType ().isAddress ()) {
238
241
addReverseDependency (Op, &I);
239
242
} else {
240
- markValueLive (&I);
243
+ markInstructionLive (&I);
241
244
}
242
245
break ;
243
246
}
@@ -255,7 +258,7 @@ void DCE::markLive() {
255
258
}
256
259
default :
257
260
if (seemsUseful (&I))
258
- markValueLive (&I);
261
+ markInstructionLive (&I);
259
262
}
260
263
}
261
264
}
@@ -284,7 +287,7 @@ void DCE::markTerminatorArgsLive(SILBasicBlock *Pred,
284
287
285
288
// If the arguments are live, we need to keep the terminator that
286
289
// delivers those arguments.
287
- markValueLive (Term);
290
+ markInstructionLive (Term);
288
291
289
292
switch (Term->getTermKind ()) {
290
293
case TermKind::ReturnInst:
@@ -345,11 +348,11 @@ void DCE::propagateLiveBlockArgument(SILArgument *Arg) {
345
348
// is in reverse direction: Only if its definition (the Arg) is alive, also
346
349
// the debug_value instruction is alive.
347
350
for (Operand *DU : getDebugUses (Arg))
348
- markValueLive (DU->getUser ());
351
+ markInstructionLive (DU->getUser ());
349
352
350
353
// Mark all reverse dependencies on the Arg live
351
354
for (auto *depInst : ReverseDependencies.lookup (Arg)) {
352
- markValueLive (depInst);
355
+ markInstructionLive (depInst);
353
356
}
354
357
355
358
auto *Block = Arg->getParent ();
@@ -371,14 +374,14 @@ void DCE::propagateLiveness(SILInstruction *I) {
371
374
// debug_value instruction is alive.
372
375
for (auto result : I->getResults ())
373
376
for (Operand *DU : getDebugUses (result))
374
- markValueLive (DU->getUser ());
377
+ markInstructionLive (DU->getUser ());
375
378
376
379
// Handle all other reverse-dependency instructions, like cond_fail,
377
380
// fix_lifetime, destroy_value, etc. Only if the definition is alive, the
378
381
// user itself is alive.
379
382
for (auto res : I->getResults ()) {
380
383
for (auto *depInst : ReverseDependencies.lookup (res)) {
381
- markValueLive (depInst);
384
+ markInstructionLive (depInst);
382
385
}
383
386
}
384
387
return ;
@@ -448,7 +451,7 @@ void DCE::replaceBranchWithJump(SILInstruction *Inst, SILBasicBlock *Block) {
448
451
std::vector<SILValue> Args;
449
452
auto E = Block->args_end ();
450
453
for (auto A = Block->args_begin (); A != E; ++A) {
451
- assert (!LiveValues .count (*A) && " Unexpected live block argument!" );
454
+ assert (!LiveArguments .count (*A) && " Unexpected live block argument!" );
452
455
Args.push_back (SILUndef::get ((*A)->getType (), *(*A)->getFunction ()));
453
456
}
454
457
Branch =
@@ -462,8 +465,12 @@ void DCE::replaceBranchWithJump(SILInstruction *Inst, SILBasicBlock *Block) {
462
465
}
463
466
464
467
void DCE::endLifetimeOfLiveValue (SILValue value, SILInstruction *insertPt) {
465
- if (!LiveValues.count (value->getRepresentativeSILNodeInObject ())) {
466
- return ;
468
+ if (SILInstruction *inst = value->getDefiningInstruction ()) {
469
+ if (!LiveInstructions.count (inst))
470
+ return ;
471
+ } else if (auto *arg = dyn_cast<SILArgument>(value)) {
472
+ if (!LiveArguments.count (arg))
473
+ return ;
467
474
}
468
475
SILBuilderWithScope builder (insertPt);
469
476
if (value.getOwnershipKind () == OwnershipKind::Owned) {
@@ -483,7 +490,7 @@ bool DCE::removeDead() {
483
490
for (auto &BB : *F) {
484
491
for (unsigned i = 0 ; i < BB.getArguments ().size ();) {
485
492
auto *arg = BB.getArgument (i);
486
- if (LiveValues .count (arg)) {
493
+ if (LiveArguments .count (arg)) {
487
494
i++;
488
495
continue ;
489
496
}
@@ -509,7 +516,7 @@ bool DCE::removeDead() {
509
516
auto insertPt = getInsertAfterPoint (arg).getValue ();
510
517
SILBuilderWithScope builder (insertPt);
511
518
auto *destroy = builder.createDestroyValue (insertPt->getLoc (), arg);
512
- LiveValues .insert (destroy-> getRepresentativeSILNodeInObject () );
519
+ LiveInstructions .insert (destroy);
513
520
}
514
521
i++;
515
522
Changed = true ;
@@ -534,7 +541,7 @@ bool DCE::removeDead() {
534
541
for (auto I = BB.begin (), E = BB.end (); I != E; ) {
535
542
auto *Inst = &*I;
536
543
++I;
537
- if (LiveValues .count (Inst) || isa<BranchInst>(Inst))
544
+ if (LiveInstructions .count (Inst) || isa<BranchInst>(Inst))
538
545
continue ;
539
546
540
547
// We want to replace dead terminators with unconditional branches to
@@ -770,7 +777,7 @@ void DCE::markControllingTerminatorsLive(SILBasicBlock *Block) {
770
777
collectControllingBlocks (Block, ControllingBlocks);
771
778
772
779
for (auto BB : ControllingBlocks)
773
- markValueLive (BB->getTerminator ());
780
+ markInstructionLive (BB->getTerminator ());
774
781
}
775
782
776
783
class DCEPass : public SILFunctionTransform {
0 commit comments