@@ -92,10 +92,17 @@ static llvm::cl::opt<bool> EnableLocalStoreDSE("enable-local-store-dse",
92
92
STATISTIC (NumDeadStores, " Number of dead stores removed" );
93
93
STATISTIC (NumPartialDeadStores, " Number of partial dead stores removed" );
94
94
95
+ // / Are we building the gen/kill sets or actually performing the DSE.
96
+ enum class DSEComputeKind {BuildGenKillSet, PerformDSE};
97
+
95
98
// ===----------------------------------------------------------------------===//
96
99
// Utility Functions
97
100
// ===----------------------------------------------------------------------===//
98
101
102
+ static inline bool isBuildingGenKillSet (DSEComputeKind Kind) {
103
+ return Kind == DSEComputeKind::BuildGenKillSet;
104
+ }
105
+
99
106
// / Returns true if this is an instruction that may have side effects in a
100
107
// / general sense but are inert from a load store perspective.
101
108
static bool isDeadStoreInertInstruction (SILInstruction *Inst) {
@@ -323,26 +330,26 @@ class DSEContext {
323
330
// / There is a read to a location, expand the location into individual fields
324
331
// / before processing them.
325
332
void processRead (SILInstruction *Inst, BlockState *State, SILValue Mem,
326
- bool BuildGenKillSet );
333
+ DSEComputeKind Kind );
327
334
328
335
// / There is a write to a location, expand the location into individual fields
329
336
// / before processing them.
330
337
void processWrite (SILInstruction *Inst, BlockState *State, SILValue Val,
331
- SILValue Mem, bool BuildGenKillSet );
338
+ SILValue Mem, DSEComputeKind Kind );
332
339
333
340
// / Process Instructions. Extract MemLocations from SIL LoadInst.
334
- void processLoadInst (SILInstruction *Inst, bool BuildGenKillSet );
341
+ void processLoadInst (SILInstruction *Inst, DSEComputeKind Kind );
335
342
336
343
// / Process Instructions. Extract MemLocations from SIL StoreInst.
337
- void processStoreInst (SILInstruction *Inst, bool BuildGenKillSet );
344
+ void processStoreInst (SILInstruction *Inst, DSEComputeKind Kind );
338
345
339
346
// / Process Instructions. Extract MemLocations from SIL DebugValueAddrInst.
340
347
// / DebugValueAddrInst operand maybe promoted to DebugValue, when this is
341
348
// / done, DebugValueAddrInst is effectively a read on the MemLocation.
342
349
void processDebugValueAddrInst (SILInstruction *I);
343
350
344
351
// / Process Instructions. Extract MemLocations from SIL Unknown Memory Inst.
345
- void processUnknownReadMemInst (SILInstruction *Inst, bool BuildGenKillSet );
352
+ void processUnknownReadMemInst (SILInstruction *Inst, DSEComputeKind Kind );
346
353
347
354
// / Check whether the instruction invalidate any MemLocations due to change in
348
355
// / its MemLocation Base.
@@ -367,7 +374,7 @@ class DSEContext {
367
374
// / the loop basic block will have store to x.a and therefore x.a = 13 can now
368
375
// / be considered dead.
369
376
// /
370
- void invalidateMemLocationBase (SILInstruction *Inst, bool BuildGenKillSet );
377
+ void invalidateMemLocationBase (SILInstruction *Inst, DSEComputeKind Kind );
371
378
372
379
// / Get the bit representing the location in the MemLocationVault.
373
380
// /
@@ -400,7 +407,7 @@ class DSEContext {
400
407
void mergeSuccessorStates (SILBasicBlock *BB);
401
408
402
409
// / Update the BlockState based on the given instruction.
403
- void processInstruction (SILInstruction *I, bool BuildGenKillSet );
410
+ void processInstruction (SILInstruction *I, DSEComputeKind Kind );
404
411
405
412
// / Entry point for global dead store elimination.
406
413
void run ();
@@ -423,7 +430,7 @@ unsigned DSEContext::getMemLocationBit(const MemLocation &Loc) {
423
430
424
431
void DSEContext::processBasicBlockForGenKillSet (SILBasicBlock *BB) {
425
432
for (auto I = BB->rbegin (), E = BB->rend (); I != E; ++I) {
426
- processInstruction (&(*I), true );
433
+ processInstruction (&(*I), DSEComputeKind::BuildGenKillSet );
427
434
}
428
435
}
429
436
@@ -449,7 +456,7 @@ bool DSEContext::processBasicBlock(SILBasicBlock *BB) {
449
456
450
457
// Process instructions in post-order fashion.
451
458
for (auto I = BB->rbegin (), E = BB->rend (); I != E; ++I) {
452
- processInstruction (&(*I), false );
459
+ processInstruction (&(*I), DSEComputeKind::PerformDSE );
453
460
}
454
461
455
462
// If WriteSetIn changes, then keep iterating until reached a fixed
@@ -487,11 +494,11 @@ void DSEContext::mergeSuccessorStates(SILBasicBlock *BB) {
487
494
}
488
495
489
496
void DSEContext::invalidateMemLocationBase (SILInstruction *I,
490
- bool BuildGenKillSet ) {
497
+ DSEComputeKind Kind ) {
491
498
// If this instruction defines the base of a location, then we need to
492
499
// invalidate any locations with the same base.
493
500
BlockState *S = getBlockState (I);
494
- if (BuildGenKillSet ) {
501
+ if (isBuildingGenKillSet (Kind) ) {
495
502
for (unsigned i = 0 ; i < S->MemLocationNum ; ++i) {
496
503
if (MemLocationVault[i].getBase ().getDef () != I)
497
504
continue ;
@@ -574,7 +581,7 @@ void DSEContext::updateGenKillSetForWrite(BlockState *S, unsigned bit) {
574
581
}
575
582
576
583
void DSEContext::processRead (SILInstruction *I, BlockState *S, SILValue Mem,
577
- bool BuildGenKillSet ) {
584
+ DSEComputeKind Kind ) {
578
585
// Construct a MemLocation to represent the memory read by this instruction.
579
586
// NOTE: The base will point to the actual object this inst is accessing,
580
587
// not this particular field.
@@ -593,7 +600,7 @@ void DSEContext::processRead(SILInstruction *I, BlockState *S, SILValue Mem,
593
600
// If we cant figure out the Base or Projection Path for the read instruction,
594
601
// process it as an unknown memory instruction for now.
595
602
if (!L.isValid ()) {
596
- processUnknownReadMemInst (I, BuildGenKillSet );
603
+ processUnknownReadMemInst (I, Kind );
597
604
return ;
598
605
}
599
606
@@ -610,7 +617,7 @@ void DSEContext::processRead(SILInstruction *I, BlockState *S, SILValue Mem,
610
617
// separate reads.
611
618
MemLocationList Locs;
612
619
MemLocation::expand (L, &I->getModule (), Locs, TypeExpansionVault);
613
- if (BuildGenKillSet ) {
620
+ if (isBuildingGenKillSet (Kind) ) {
614
621
for (auto &E : Locs) {
615
622
// Only building the gen and kill sets for now.
616
623
updateGenKillSetForRead (S, getMemLocationBit (E));
@@ -625,7 +632,7 @@ void DSEContext::processRead(SILInstruction *I, BlockState *S, SILValue Mem,
625
632
}
626
633
627
634
void DSEContext::processWrite (SILInstruction *I, BlockState *S, SILValue Val,
628
- SILValue Mem, bool BuildGenKillSet ) {
635
+ SILValue Mem, DSEComputeKind Kind ) {
629
636
// Construct a MemLocation to represent the memory read by this instruction.
630
637
// NOTE: The base will point to the actual object this inst is accessing,
631
638
// not this particular field.
@@ -662,7 +669,7 @@ void DSEContext::processWrite(SILInstruction *I, BlockState *S, SILValue Val,
662
669
MemLocationList Locs;
663
670
MemLocation::expand (L, Mod, Locs, TypeExpansionVault);
664
671
llvm::BitVector V (Locs.size ());
665
- if (BuildGenKillSet ) {
672
+ if (isBuildingGenKillSet (Kind) ) {
666
673
for (auto &E : Locs) {
667
674
// Only building the gen and kill sets here.
668
675
updateGenKillSetForWrite (S, getMemLocationBit (E));
@@ -680,7 +687,7 @@ void DSEContext::processWrite(SILInstruction *I, BlockState *S, SILValue Val,
680
687
}
681
688
682
689
// Data flow has not stablized, do not perform the DSE just yet.
683
- if (BuildGenKillSet )
690
+ if (isBuildingGenKillSet (Kind) )
684
691
return ;
685
692
686
693
// Fully dead store - stores to all the components are dead, therefore this
@@ -739,15 +746,15 @@ void DSEContext::processWrite(SILInstruction *I, BlockState *S, SILValue Val,
739
746
}
740
747
}
741
748
742
- void DSEContext::processLoadInst (SILInstruction *I, bool BuildGenKillSet ) {
749
+ void DSEContext::processLoadInst (SILInstruction *I, DSEComputeKind Kind ) {
743
750
SILValue Mem = cast<LoadInst>(I)->getOperand ();
744
- processRead (I, getBlockState (I), Mem, BuildGenKillSet );
751
+ processRead (I, getBlockState (I), Mem, Kind );
745
752
}
746
753
747
- void DSEContext::processStoreInst (SILInstruction *I, bool BuildGenKillSet ) {
754
+ void DSEContext::processStoreInst (SILInstruction *I, DSEComputeKind Kind ) {
748
755
SILValue Val = cast<StoreInst>(I)->getSrc ();
749
756
SILValue Mem = cast<StoreInst>(I)->getDest ();
750
- processWrite (I, getBlockState (I), Val, Mem, BuildGenKillSet );
757
+ processWrite (I, getBlockState (I), Val, Mem, Kind );
751
758
}
752
759
753
760
void DSEContext::processDebugValueAddrInst (SILInstruction *I) {
@@ -763,10 +770,10 @@ void DSEContext::processDebugValueAddrInst(SILInstruction *I) {
763
770
}
764
771
765
772
void DSEContext::processUnknownReadMemInst (SILInstruction *I,
766
- bool BuildGenKillSet ) {
773
+ DSEComputeKind Kind ) {
767
774
BlockState *S = getBlockState (I);
768
775
// Update the gen kill set.
769
- if (BuildGenKillSet ) {
776
+ if (isBuildingGenKillSet (Kind) ) {
770
777
for (unsigned i = 0 ; i < S->MemLocationNum ; ++i) {
771
778
if (!AA->mayReadFromMemory (I, MemLocationVault[i].getBase ()))
772
779
continue ;
@@ -788,7 +795,7 @@ void DSEContext::processUnknownReadMemInst(SILInstruction *I,
788
795
}
789
796
}
790
797
791
- void DSEContext::processInstruction (SILInstruction *I, bool BuildGenKillSet ) {
798
+ void DSEContext::processInstruction (SILInstruction *I, DSEComputeKind Kind ) {
792
799
// If this instruction has side effects, but is inert from a store
793
800
// perspective, skip it.
794
801
if (isDeadStoreInertInstruction (I))
@@ -799,17 +806,17 @@ void DSEContext::processInstruction(SILInstruction *I, bool BuildGenKillSet) {
799
806
// TODO: process more instructions.
800
807
//
801
808
if (isa<LoadInst>(I)) {
802
- processLoadInst (I, BuildGenKillSet );
809
+ processLoadInst (I, Kind );
803
810
} else if (isa<StoreInst>(I)) {
804
- processStoreInst (I, BuildGenKillSet );
811
+ processStoreInst (I, Kind );
805
812
} else if (isa<DebugValueAddrInst>(I)) {
806
813
processDebugValueAddrInst (I);
807
814
} else if (I->mayReadFromMemory ()) {
808
- processUnknownReadMemInst (I, BuildGenKillSet );
815
+ processUnknownReadMemInst (I, Kind );
809
816
}
810
817
811
818
// Check whether this instruction will invalidate any other MemLocations.
812
- invalidateMemLocationBase (I, BuildGenKillSet );
819
+ invalidateMemLocationBase (I, Kind );
813
820
}
814
821
815
822
SILValue DSEContext::createExtract (SILValue Base,
0 commit comments