forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeadStoreElimination.cpp
1271 lines (1102 loc) · 45 KB
/
DeadStoreElimination.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- DeadStoreElimination.cpp - SIL Dead Store Elimination ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// This pass eliminates dead stores across basic blocks.
///
/// A store is dead if after the store has occurred:
///
/// 1. The store to pointer is not used along any path to program exit.
/// 2. The store to pointer is overwritten by another store before any
/// potential use of the pointer.
///
/// Dead store elimination (DSE) eliminates such stores by:
///
/// 1. Introducing a notion of a LSLocation that is used to model objects
/// fields. (See below for more details).
///
/// 2. Performing a post-order walk over the control flow graph, tracking any
/// LSLocations that are read from or stored into in each basic block. After
/// eliminating any dead stores in single blocks, it computes a genset and
/// killset for each block. The genset keeps a list of upward visible stores
/// and the killset keeps a list of LSLocation this basic block reads (kills).
///
/// 3. An optimistic iterative dataflow is performed on the genset and killset
/// until convergence.
///
/// At the core of DSE, there is the LSLocation class. a LSLocation is an
/// abstraction of an object field in program. It consists of a base and a
/// projection path to the field accessed.
///
/// When a load or store instruction is encountered, the memory is broken down
/// to the indivisible components, i.e aggregates are broken down to their
/// individual fields using the expand function. This gives the flexibility to
/// find exactly which part of the store is alive and which part is dead.
///
/// After the live parts of the store are determined, they are merged into the
/// minimum number of stores possible using the reduce function. This is done
/// so that we do not bloat SIL IR.
///
/// Another way to implement the DSE optimization is to keep the instructions
/// that read and/or write memory without breaking the memory read/written
/// using the ProjectionPath. However, this can easily lead to loss of
/// opportunities, e.g. a read that only kills part of a store may need to be
/// treated as killing the entire store. However, using ProjectionPath does
/// lead to more memory uses.
///
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-dead-store-elim"
#include "swift/SIL/Projection.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
#include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/CFG.h"
#include "swift/SILOptimizer/Utils/LoadStoreOptUtils.h"
#include "swift/SILOptimizer/Utils/Local.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
using namespace swift;
STATISTIC(NumDeadStores, "Number of dead stores removed");
STATISTIC(NumPartialDeadStores, "Number of partial dead stores removed");
/// If a large store is broken down to too many smaller stores, bail out.
/// Currently, we only do partial dead store if we can form a single contiguous
/// live store.
static llvm::cl::opt<unsigned>
MaxPartialStoreCount("max-partial-store-count", llvm::cl::init(1), llvm::cl::Hidden);
/// ComputeMaxStoreSet - If we ignore all reads, what is the max store set that
/// can reach a particular point in a basic block. This helps in generating
/// the genset and killset. i.e. if there is no upward visible store that can
/// reach the beginning of a basic block, then we know that the genset and
/// killset for the stored location need not be set for the basic block.
///
/// BuildGenKillSet - Build the genset and killset of the basic block.
///
/// PerformDSE - Perform the actual dead store elimination.
enum class DSEKind : unsigned {
ComputeMaxStoreSet = 0,
BuildGenKillSet = 1,
PerformDSE = 2,
};
//===----------------------------------------------------------------------===//
// Utility Functions
//===----------------------------------------------------------------------===//
/// Return the deallocate stack instructions corresponding to the given
/// AllocStackInst.
static llvm::SmallVector<SILInstruction *, 1>
findDeallocStackInst(AllocStackInst *ASI) {
llvm::SmallVector<SILInstruction *, 1> DSIs;
for (auto UI = ASI->use_begin(), E = ASI->use_end(); UI != E; ++UI) {
if (auto *D = dyn_cast<DeallocStackInst>(UI->getUser())) {
DSIs.push_back(D);
}
}
return DSIs;
}
/// Return the deallocate ref instructions corresponding to the given
/// AllocRefInst.
static llvm::SmallVector<SILInstruction *, 1>
findDeallocRefInst(AllocRefInst *ARI) {
llvm::SmallVector<SILInstruction *, 1> DSIs;
for (auto UI = ARI->use_begin(), E = ARI->use_end(); UI != E; ++UI) {
if (auto *D = dyn_cast<DeallocRefInst>(UI->getUser())) {
if (D->isDeallocatingStack())
DSIs.push_back(D);
}
}
return DSIs;
}
static inline bool isComputeMaxStoreSet(DSEKind Kind) {
return Kind == DSEKind::ComputeMaxStoreSet;
}
static inline bool isBuildingGenKillSet(DSEKind Kind) {
return Kind == DSEKind::BuildGenKillSet;
}
static inline bool isPerformingDSE(DSEKind Kind) {
return Kind == DSEKind::PerformDSE;
}
/// Returns true if this is an instruction that may have side effects in a
/// general sense but are inert from a load store perspective.
static bool isDeadStoreInertInstruction(SILInstruction *Inst) {
switch (Inst->getKind()) {
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Name##RetainInst: \
case SILInstructionKind::StrongRetain##Name##Inst: \
case SILInstructionKind::Copy##Name##ValueInst:
#include "swift/AST/ReferenceStorage.def"
case SILInstructionKind::StrongRetainInst:
case SILInstructionKind::RetainValueInst:
case SILInstructionKind::DeallocStackInst:
case SILInstructionKind::DeallocRefInst:
case SILInstructionKind::CondFailInst:
case SILInstructionKind::FixLifetimeInst:
return true;
default:
return false;
}
}
//===----------------------------------------------------------------------===//
// Basic Block Location State
//===----------------------------------------------------------------------===//
namespace {
/// If this function has too many basic blocks or too many locations, it may
/// take a long time to compute the genset and killset. The number of memory
/// behavior or alias query we need to do in worst case is roughly linear to
/// # of BBs x(times) # of locations.
///
/// we could run DSE on functions with 256 basic blocks and 256 locations,
/// which is a large function.
constexpr unsigned MaxLSLocationBBMultiplicationNone = 256*256;
/// we could run optimistic DSE on functions with less than 64 basic blocks
/// and 64 locations which is a sizable function.
constexpr unsigned MaxLSLocationBBMultiplicationPessimistic = 64*64;
/// forward declaration.
class DSEContext;
/// BlockState summarizes how LSLocations are used in a basic block.
///
/// Initially the BBWriteSetOut is empty. Before a basic block is processed, it
/// is initialized to the intersection of BBWriteSetIns of all successors of the
/// basic block.
///
/// BBWriteSetMid is initialized to BBWriteSetOut of the current basic block
/// before instructions in the basic block is processed.
///
/// Initially BBWriteSetIn is set to true. After the basic block is processed,
/// if its BBWriteSetMid is different from BBWriteSetIn, BBWriteSetIn is
/// assigned the value of BBWriteSetMid and the data flow is rerun on the
/// current basic block's predecessors.
///
/// Instructions in each basic block are processed in post-order as follows:
///
/// 1. When a store instruction is encountered, the stored location is tracked.
///
/// 2. When a load instruction is encountered, remove the loaded location and
/// any location it may alias with from the BBWriteSetMid.
///
/// 3. When an instruction reads from memory in an unknown way, the BBWriteSet
/// bit is cleared if the instruction can read the corresponding LSLocation.
class BlockState {
public:
/// The basic block this BlockState represents.
SILBasicBlock *BB;
/// Keep the number of LSLocations in the LocationVault.
unsigned LocationNum;
/// A bit vector for which the ith bit represents the ith LSLocation in
/// LocationVault. If the bit is set, then the location currently has an
/// upward visible store at the end of the basic block.
SmallBitVector BBWriteSetOut;
/// A bit vector for which the ith bit represents the ith LSLocation in
/// LocationVault. If the bit is set, then the location currently has an
/// upward visible store in middle of the basic block.
SmallBitVector BBWriteSetMid;
/// A bit vector for which the ith bit represents the ith LSLocation in
/// LocationVault. If a bit in the vector is set, then the location has an
/// upward visible store at the beginning of the basic block.
SmallBitVector BBWriteSetIn;
/// A bit vector for which the ith bit represents the ith LSLocation in
/// LocationVault. If the bit is set, then the current basic block
/// generates an upward visible store.
SmallBitVector BBGenSet;
/// A bit vector for which the ith bit represents the ith LSLocation in
/// LocationVault. If the bit is set, then the current basic block
/// kills an upward visible store.
SmallBitVector BBKillSet;
/// A bit vector to keep the maximum number of stores that can reach a
/// certain point of the basic block. If a bit is set, that means there is
/// potentially an upward visible store to the location at the particular
/// point of the basic block.
SmallBitVector BBMaxStoreSet;
/// If a bit in the vector is set, then the location is dead at the end of
/// this basic block.
SmallBitVector BBDeallocateLocation;
/// The dead stores in the current basic block.
llvm::SmallVector<SILInstruction *, 2> DeadStores;
/// Keeps track of what stores to generate after the data flow stabilizes.
/// these stores come from partial dead stores.
///
/// The first SILValue keeps the address of the live store and the second
/// SILValue keeps the value of the store.
llvm::SetVector<SILValue> LiveAddr;
llvm::DenseMap<SILValue, SILValue> LiveStores;
/// Constructors.
BlockState(SILBasicBlock *B, unsigned LocationNum, bool Optimistic)
: BB(B), LocationNum(LocationNum) {
init(LocationNum, Optimistic);
}
/// Initialize the bitvectors for the current basic block.
void init(unsigned LocationNum, bool Optimistic);
/// Check whether the BBWriteSetIn has changed. If it does, we need to rerun
/// the data flow on this block's predecessors to reach fixed point.
bool updateBBWriteSetIn(SmallBitVector &X);
/// Functions to manipulate the write set.
void startTrackingLocation(SmallBitVector &BV, unsigned bit);
void stopTrackingLocation(SmallBitVector &BV, unsigned bit);
bool isTrackingLocation(SmallBitVector &BV, unsigned bit);
/// Set the store bit for stack slot deallocated in this basic block.
void initStoreSetAtEndOfBlock(DSEContext &Ctx);
};
} // end anonymous namespace
bool BlockState::updateBBWriteSetIn(SmallBitVector &X) {
if (BBWriteSetIn == X)
return false;
BBWriteSetIn = X;
return true;
}
void BlockState::startTrackingLocation(SmallBitVector &BV, unsigned i) {
BV.set(i);
}
void BlockState::stopTrackingLocation(SmallBitVector &BV, unsigned i) {
BV.reset(i);
}
bool BlockState::isTrackingLocation(SmallBitVector &BV, unsigned i) {
return BV.test(i);
}
//===----------------------------------------------------------------------===//
// Top Level Implementation
//===----------------------------------------------------------------------===//
namespace {
/// The dead store elimination context, keep information about stores in a basic
/// block granularity.
class DSEContext {
/// How to process the current function.
enum class ProcessKind {
ProcessOptimistic = 0,
ProcessPessimistic = 1,
ProcessNone = 2,
};
private:
/// The module we are currently processing.
SILModule *Mod;
/// The function we are currently processing.
SILFunction *F;
/// Pass manager, used to get various analysis.
SILPassManager *PM;
/// Alias Analysis.
AliasAnalysis *AA;
/// Type Expansion Analysis.
TypeExpansionAnalysis *TE;
/// Epilogue release analysis.
EpilogueARCFunctionInfo *EAFI;
/// The allocator we are using.
llvm::SpecificBumpPtrAllocator<BlockState> &BPA;
/// Map every basic block to its location state.
llvm::SmallDenseMap<SILBasicBlock *, BlockState *> BBToLocState;
/// Keeps all the locations for the current function. The BitVector in each
/// BlockState is then laid on top of it to keep track of which LSLocation
/// has an upward visible store.
std::vector<LSLocation> LocationVault;
/// Keeps a list of basic blocks that have StoreInsts. If a basic block does
/// not have StoreInst, we do not actually perform the last iteration where
/// DSE is actually performed on the basic block.
///
/// NOTE: This is never populated for functions which will only require 1
/// data flow iteration. For function that requires more than 1 iteration of
/// the data flow this is populated when the first time the functions is
/// walked, i.e. when the we generate the genset and killset.
llvm::DenseSet<SILBasicBlock *> BBWithStores;
/// Contains a map between location to their index in the LocationVault.
/// used to facilitate fast location to index lookup.
LSLocationIndexMap LocToBitIndex;
/// Keeps a map between the accessed SILValue and the location.
LSLocationBaseMap BaseToLocIndex;
/// Return the BlockState for the basic block this basic block belongs to.
BlockState *getBlockState(SILBasicBlock *B) { return BBToLocState[B]; }
/// Return the BlockState for the basic block this instruction belongs to.
BlockState *getBlockState(SILInstruction *I) {
return getBlockState(I->getParent());
}
/// LSLocation written has been extracted, expanded and mapped to the bit
/// position in the bitvector. update the max store set using the bit
/// position.
void processWriteForMaxStoreSet(BlockState *S, unsigned bit);
/// There is a read to a location, expand the location into individual fields
/// before processing them.
void processRead(SILInstruction *Inst, SILValue M, DSEKind K);
void processReadForGenKillSet(BlockState *S, unsigned bit);
void processReadForDSE(BlockState *S, unsigned Bit);
/// There is a write to a location, expand the location into individual fields
/// before processing them.
void processWrite(SILInstruction *Inst, SILValue V, SILValue M, DSEKind K);
void processWriteForGenKillSet(BlockState *S, unsigned bit);
bool processWriteForDSE(BlockState *S, unsigned bit);
/// Process instructions. Extract locations from SIL LoadInst.
void processLoadInst(SILInstruction *Inst, DSEKind Kind);
/// Process instructions. Extract locations from SIL StoreInst.
void processStoreInst(SILInstruction *Inst, DSEKind Kind);
/// Process instructions. Extract locations from SIL DebugValueAddrInst.
/// DebugValueAddrInst maybe promoted to DebugValue, when this is done,
/// DebugValueAddrInst is effectively a read on the location.
void processDebugValueAddrInst(SILInstruction *I, DSEKind Kind);
void processDebugValueAddrInstForGenKillSet(SILInstruction *I);
void processDebugValueAddrInstForDSE(SILInstruction *I);
/// Process unknown read instructions. Extract locations from unknown memory
/// inst.
void processUnknownReadInst(SILInstruction *Inst, DSEKind Kind);
void processUnknownReadInstForGenKillSet(SILInstruction *Inst);
void processUnknownReadInstForDSE(SILInstruction *Inst);
/// Check whether the instruction invalidate any locations due to change in
/// its location Base.
///
/// This is to handle a case like this.
///
/// class Foo { var a : Int = 12 }
/// for _ in 0 ...x {
/// x = Foo();
/// x.a = 13
/// }
/// x.a = 12
///
/// In this case, DSE cannot remove the x.a = 13 inside the loop.
///
/// To do this, when the algorithm reaches the beginning of the basic block in
/// the loop it will need to invalidate the location in the BBWriteSetMid.
/// i.e. the base of the location is changed.
///
/// If not, on the second iteration, the intersection of the successors of
/// the loop basic block will have store to x.a and therefore x.a = 13 can now
/// be considered dead.
void invalidateBase(SILValue B, BlockState *S, DSEKind Kind);
void invalidateBaseForGenKillSet(SILValue B, BlockState *S);
void invalidateBaseForDSE(SILValue B, BlockState *S);
/// Get the bit representing the location in the LocationVault.
unsigned getLocationBit(const LSLocation &L);
public:
/// Constructor.
DSEContext(SILFunction *F, SILModule *M, SILPassManager *PM,
AliasAnalysis *AA, TypeExpansionAnalysis *TE,
EpilogueARCFunctionInfo *EAFI,
llvm::SpecificBumpPtrAllocator<BlockState> &BPA)
: Mod(M), F(F), PM(PM), AA(AA), TE(TE), EAFI(EAFI), BPA(BPA) {}
/// Entry point for dead store elimination.
bool run();
/// Run the iterative DF to converge the BBWriteSetIn.
void runIterativeDSE();
/// Returns the location vault of the current function.
std::vector<LSLocation> &getLocationVault() { return LocationVault; }
/// Use a set of ad hoc rules to tell whether we should run a pessimistic
/// one iteration data flow on the function.
ProcessKind getProcessFunctionKind(unsigned StoreCount);
/// Compute the kill set for the basic block. return true if the store set
/// changes.
void processBasicBlockForDSE(SILBasicBlock *BB, bool Optimistic);
/// Compute the genset and killset for the current basic block.
void processBasicBlockForGenKillSet(SILBasicBlock *BB);
/// Compute the BBWriteSetOut and BBWriteSetIn for the current basic
/// block with the generated gen and kill set.
bool processBasicBlockWithGenKillSet(SILBasicBlock *BB);
/// Intersect the successors' BBWriteSetIns.
void mergeSuccessorLiveIns(SILBasicBlock *BB);
/// Update the BlockState based on the given instruction.
void processInstruction(SILInstruction *I, DSEKind Kind);
};
} // end anonymous namespace
void BlockState::init(unsigned LocationNum, bool Optimistic) {
// For function that requires just 1 iteration of the data flow to converge
// we set the initial state of BBWriteSetIn to 0.
//
// For other functions, the initial state of BBWriteSetIn should be all 1's.
// Otherwise the dataflow solution could be too conservative.
//
// Consider this case, the dead store by var a = 10 before the loop will not
// be eliminated if the BBWriteSetIn is set to 0 initially.
//
// var a = 10
// for _ in 0...1024 {}
// a = 10
//
// However, by doing so, we can only eliminate the dead stores after the
// data flow stabilizes.
//
BBWriteSetIn.resize(LocationNum, Optimistic);
BBWriteSetOut.resize(LocationNum, false);
BBWriteSetMid.resize(LocationNum, false);
// GenSet and KillSet initially empty.
BBGenSet.resize(LocationNum, false);
BBKillSet.resize(LocationNum, false);
// MaxStoreSet is optimistically set to true initially.
BBMaxStoreSet.resize(LocationNum, true);
// DeallocateLocation initially empty.
BBDeallocateLocation.resize(LocationNum, false);
}
unsigned DSEContext::getLocationBit(const LSLocation &Loc) {
// Return the bit position of the given Loc in the LocationVault. The bit
// position is then used to set/reset the bitvector kept by each BlockState.
//
// We should have the location populated by the enumerateLSLocation at this
// point.
auto Iter = LocToBitIndex.find(Loc);
assert(Iter != LocToBitIndex.end() && "LSLocation should have been enum'ed");
return Iter->second;
}
DSEContext::ProcessKind DSEContext::getProcessFunctionKind(unsigned StoreCount) {
// Don't optimize function that are marked as 'no.optimize'.
if (!F->shouldOptimize())
return ProcessKind::ProcessNone;
// Really no point optimizing here as there is no dead stores.
if (StoreCount < 1)
return ProcessKind::ProcessNone;
bool RunOneIteration = true;
unsigned BBCount = 0;
unsigned LocationCount = LocationVault.size();
// If all basic blocks will have their successors processed if
// the basic blocks in the functions are iterated in post order.
// Then this function can be processed in one iteration, i.e. no
// need to generate the genset and killset.
auto *PO = PM->getAnalysis<PostOrderAnalysis>()->get(F);
llvm::DenseSet<SILBasicBlock *> HandledBBs;
for (SILBasicBlock *B : PO->getPostOrder()) {
++BBCount;
for (auto &X : B->getSuccessors()) {
if (HandledBBs.find(X) == HandledBBs.end()) {
RunOneIteration = false;
break;
}
}
HandledBBs.insert(B);
}
// Data flow may take too long to run.
if (BBCount * LocationCount > MaxLSLocationBBMultiplicationNone)
return ProcessKind::ProcessNone;
// This function's data flow would converge in 1 iteration.
if (RunOneIteration)
return ProcessKind::ProcessPessimistic;
// We run one pessimistic data flow to do dead store elimination on
// the function.
if (BBCount * LocationCount > MaxLSLocationBBMultiplicationPessimistic)
return ProcessKind::ProcessPessimistic;
return ProcessKind::ProcessOptimistic;
}
void DSEContext::processBasicBlockForGenKillSet(SILBasicBlock *BB) {
// Compute the MaxStoreSet at the end of the basic block.
auto *BBState = getBlockState(BB);
if (BB->succ_empty()) {
BBState->BBMaxStoreSet |= BBState->BBDeallocateLocation;
} else {
auto Iter = BB->succ_begin();
BBState->BBMaxStoreSet = getBlockState(*Iter)->BBMaxStoreSet;
Iter = std::next(Iter);
for (auto EndIter = BB->succ_end(); Iter != EndIter; ++Iter) {
BBState->BBMaxStoreSet &= getBlockState(*Iter)->BBMaxStoreSet;
}
}
// Compute the genset and killset.
//
// Also compute the MaxStoreSet at the current position of the basic block.
//
// This helps generating the genset and killset. If there is no way a
// location can have an upward visible store at a particular point in the
// basic block, we do not need to turn on the genset and killset for the
// location.
//
// Turning on the genset and killset can be costly as it involves querying
// the AA interface.
for (auto I = BB->rbegin(), E = BB->rend(); I != E; ++I) {
// Only process store insts.
if (isa<StoreInst>(*I)) {
if (BBWithStores.find(BB) == BBWithStores.end())
BBWithStores.insert(BB);
processStoreInst(&(*I), DSEKind::ComputeMaxStoreSet);
}
// Compute the genset and killset for this instruction.
processInstruction(&(*I), DSEKind::BuildGenKillSet);
}
// Handle SILArgument for base invalidation.
ArrayRef<SILArgument *> Args = BB->getArguments();
for (auto &X : Args) {
invalidateBase(X, BBState, DSEKind::BuildGenKillSet);
}
}
bool DSEContext::processBasicBlockWithGenKillSet(SILBasicBlock *BB) {
// Compute the BBWriteSetOut at the end of the basic block.
mergeSuccessorLiveIns(BB);
// Compute the BBWriteSet at the beginning of the basic block.
BlockState *S = getBlockState(BB);
S->BBWriteSetMid = S->BBWriteSetOut;
S->BBWriteSetMid.reset(S->BBKillSet);
S->BBWriteSetMid |= S->BBGenSet;
// If BBWriteSetIn changes, then keep iterating until reached a fixed point.
return S->updateBBWriteSetIn(S->BBWriteSetMid);
}
void DSEContext::processBasicBlockForDSE(SILBasicBlock *BB, bool Optimistic) {
// If we know this is not a one iteration function which means its
// its BBWriteSetIn and BBWriteSetOut have been computed and converged,
// and this basic block does not even have StoreInsts, there is no point
// in processing every instruction in the basic block again as no store
// will be eliminated.
if (Optimistic && BBWithStores.find(BB) == BBWithStores.end())
return;
// Intersect in the successor WriteSetIns. A store is dead if it is not read
// from any path to the end of the program. Thus an intersection.
mergeSuccessorLiveIns(BB);
// Initialize the BBWriteSetMid to BBWriteSetOut to get started.
BlockState *S = getBlockState(BB);
S->BBWriteSetMid = S->BBWriteSetOut;
// Process instructions in post-order fashion.
for (auto I = BB->rbegin(), E = BB->rend(); I != E; ++I) {
processInstruction(&(*I), DSEKind::PerformDSE);
}
// Handle SILArgument for base invalidation.
ArrayRef<SILArgument *> Args = BB->getArguments();
for (auto &X : Args) {
invalidateBase(X, S, DSEKind::BuildGenKillSet);
}
S->BBWriteSetIn = S->BBWriteSetMid;
}
void BlockState::initStoreSetAtEndOfBlock(DSEContext &Ctx) {
std::vector<LSLocation> &LocationVault = Ctx.getLocationVault();
// We set the store bit at the end of the basic block in which a stack
// allocated location is deallocated.
for (unsigned i = 0; i < LocationVault.size(); ++i) {
// Turn on the store bit at the block which the stack slot is deallocated.
if (auto *ASI = dyn_cast<AllocStackInst>(LocationVault[i].getBase())) {
for (auto X : findDeallocStackInst(ASI)) {
SILBasicBlock *DSIBB = X->getParent();
if (DSIBB != BB)
continue;
startTrackingLocation(BBDeallocateLocation, i);
}
}
if (auto *ARI = dyn_cast<AllocRefInst>(LocationVault[i].getBase())) {
if (!ARI->isAllocatingStack())
continue;
for (auto X : findDeallocRefInst(ARI)) {
SILBasicBlock *DSIBB = X->getParent();
if (DSIBB != BB)
continue;
startTrackingLocation(BBDeallocateLocation, i);
}
}
}
}
void DSEContext::mergeSuccessorLiveIns(SILBasicBlock *BB) {
// If basic block has no successor, then all local writes can be considered
// dead for block with no successor.
BlockState *C = getBlockState(BB);
if (BB->succ_empty()) {
C->BBWriteSetOut |= C->BBDeallocateLocation;
return;
}
// Use the first successor as the base condition.
auto Iter = BB->succ_begin();
C->BBWriteSetOut = getBlockState(*Iter)->BBWriteSetIn;
/// Merge/intersection is very frequently performed, so it is important to
/// make it as cheap as possible.
///
/// To do so, we canonicalize LSLocations, i.e. traced back to the underlying
/// object. Therefore, no need to do a O(N^2) comparison to figure out what is
/// dead along all successors.
///
/// NOTE: Canonicalization does not solve the problem entirely. i.e. it is
/// still possible that 2 LSLocations with different bases that happen to be
/// the same object and field. In such case, we would miss a dead store
/// opportunity. But this happens less often with canonicalization.
Iter = std::next(Iter);
for (auto EndIter = BB->succ_end(); Iter != EndIter; ++Iter) {
C->BBWriteSetOut &= getBlockState(*Iter)->BBWriteSetIn;
}
// We set the store bit at the end of the basic block in which a stack
// allocated location is deallocated.
C->BBWriteSetOut |= C->BBDeallocateLocation;
}
void DSEContext::invalidateBaseForGenKillSet(SILValue B, BlockState *S) {
for (unsigned i = 0; i < S->LocationNum; ++i) {
if (LocationVault[i].getBase() != B)
continue;
S->startTrackingLocation(S->BBKillSet, i);
S->stopTrackingLocation(S->BBGenSet, i);
}
}
void DSEContext::invalidateBaseForDSE(SILValue B, BlockState *S) {
for (unsigned i = 0; i < S->LocationNum; ++i) {
if (!S->BBWriteSetMid.test(i))
continue;
if (LocationVault[i].getBase() != B)
continue;
S->stopTrackingLocation(S->BBWriteSetMid, i);
}
}
void DSEContext::invalidateBase(SILValue B, BlockState *S, DSEKind Kind) {
// If this instruction defines the base of a location, then we need to
// invalidate any locations with the same base.
//
// Are we building genset and killset.
if (isBuildingGenKillSet(Kind)) {
invalidateBaseForGenKillSet(B, S);
return;
}
// Are we performing dead store elimination.
if (isPerformingDSE(Kind)) {
invalidateBaseForDSE(B, S);
return;
}
llvm_unreachable("Unknown DSE compute kind");
}
void DSEContext::processReadForDSE(BlockState *S, unsigned bit) {
// Remove any may/must-aliasing stores to the LSLocation, as they can't be
// used to kill any upward visible stores due to the interfering load.
LSLocation &R = LocationVault[bit];
for (unsigned i = 0; i < S->LocationNum; ++i) {
if (!S->isTrackingLocation(S->BBWriteSetMid, i))
continue;
LSLocation &L = LocationVault[i];
if (!L.isMayAliasLSLocation(R, AA))
continue;
S->stopTrackingLocation(S->BBWriteSetMid, i);
}
}
void DSEContext::processReadForGenKillSet(BlockState *S, unsigned bit) {
// Start tracking the read to this LSLocation in the killset and update
// the genset accordingly.
//
// Even though, LSLocations are canonicalized, we still need to consult
// alias analysis to determine whether 2 LSLocations are disjointed.
LSLocation &R = LocationVault[bit];
for (unsigned i = 0; i < S->LocationNum; ++i) {
if (!S->BBMaxStoreSet.test(i))
continue;
// Do nothing if the read location NoAlias with the current location.
LSLocation &L = LocationVault[i];
if (!L.isMayAliasLSLocation(R, AA))
continue;
// Update the genset and kill set.
S->startTrackingLocation(S->BBKillSet, i);
S->stopTrackingLocation(S->BBGenSet, i);
}
}
void DSEContext::processRead(SILInstruction *I, SILValue Mem, DSEKind Kind) {
auto *S = getBlockState(I);
// Construct a LSLocation to represent the memory read by this instruction.
// NOTE: The base will point to the actual object this inst is accessing,
// not this particular field.
//
// e.g. %1 = alloc_stack $S
// %2 = struct_element_addr %1, #a
// %3 = load %2 : $*Int
//
// Base will point to %1, but not %2. Projection path will indicate which
// field is accessed.
//
// This will make comparison between locations easier. This eases the
// implementation of intersection operator in the data flow.
LSLocation L;
if (BaseToLocIndex.find(Mem) != BaseToLocIndex.end()) {
L = BaseToLocIndex[Mem];
} else {
SILValue UO = getUnderlyingObject(Mem);
L = LSLocation(UO, ProjectionPath::getProjectionPath(UO, Mem));
}
// If we can't figure out the Base or Projection Path for the read instruction,
// process it as an unknown memory instruction for now.
if (!L.isValid()) {
processUnknownReadInst(I, Kind);
return;
}
// Expand the given Mem into individual fields and process them as separate
// reads.
LSLocationList Locs;
LSLocation::expand(L, &I->getModule(), Locs, TE);
// Are we building the genset and killset.
if (isBuildingGenKillSet(Kind)) {
for (auto &E : Locs) {
// Only building the gen and kill sets for now.
processReadForGenKillSet(S, getLocationBit(E));
}
return;
}
// Are we performing the actual DSE.
if (isPerformingDSE(Kind)) {
for (auto &E : Locs) {
// This is the last iteration, compute BBWriteSetOut and perform DSE.
processReadForDSE(S, getLocationBit(E));
}
return;
}
llvm_unreachable("Unknown DSE compute kind");
}
bool DSEContext::processWriteForDSE(BlockState *S, unsigned bit) {
// If a tracked store must aliases with this store, then this store is dead.
bool StoreDead = false;
LSLocation &R = LocationVault[bit];
for (unsigned i = 0; i < S->LocationNum; ++i) {
if (!S->isTrackingLocation(S->BBWriteSetMid, i))
continue;
// If 2 locations may alias, we can still keep both stores.
LSLocation &L = LocationVault[i];
if (!L.isMustAliasLSLocation(R, AA))
continue;
// There is a must alias store. No need to check further.
StoreDead = true;
break;
}
// Track this new store.
S->startTrackingLocation(S->BBWriteSetMid, bit);
return StoreDead;
}
void DSEContext::processWriteForGenKillSet(BlockState *S, unsigned bit) {
S->startTrackingLocation(S->BBGenSet, bit);
}
void DSEContext::processWriteForMaxStoreSet(BlockState *S, unsigned bit) {
S->startTrackingLocation(S->BBMaxStoreSet, bit);
}
void DSEContext::processWrite(SILInstruction *I, SILValue Val, SILValue Mem,
DSEKind Kind) {
auto *S = getBlockState(I);
// Construct a LSLocation to represent the memory read by this instruction.
// NOTE: The base will point to the actual object this inst is accessing,
// not this particular field.
//
// e.g. %1 = alloc_stack $S
// %2 = struct_element_addr %1, #a
// store %3 to %2 : $*Int
//
// Base will point to %1, but not %2. Projection path will indicate which
// field is accessed.
//
// This will make comparison between locations easier. This eases the
// implementation of intersection operator in the data flow.
LSLocation L;
if (BaseToLocIndex.find(Mem) != BaseToLocIndex.end()) {
L = BaseToLocIndex[Mem];
} else {
SILValue UO = getUnderlyingObject(Mem);
L = LSLocation(UO, ProjectionPath::getProjectionPath(UO, Mem));
}
// If we can't figure out the Base or Projection Path for the store
// instruction, simply ignore it.
if (!L.isValid())
return;
// Expand the given Mem into individual fields and process them as separate
// writes.
bool Dead = true;
LSLocationList Locs;
LSLocation::expand(L, Mod, Locs, TE);
SmallBitVector V(Locs.size());
// Are we computing max store set.
if (isComputeMaxStoreSet(Kind)) {
for (auto &E : Locs) {
// Update the max store set for the basic block.
processWriteForMaxStoreSet(S, getLocationBit(E));
}
return;
}
// Are we computing genset and killset.
if (isBuildingGenKillSet(Kind)) {
for (auto &E : Locs) {
// Only building the gen and kill sets here.
processWriteForGenKillSet(S, getLocationBit(E));
}
// Data flow has not stabilized, do not perform the DSE just yet.
return;
}
// We are doing the actual DSE.
assert(isPerformingDSE(Kind) && "Invalid computation kind");
unsigned idx = 0;
for (auto &E : Locs) {
// This is the last iteration, compute BBWriteSetOut and perform the dead
// store elimination.
if (processWriteForDSE(S, getLocationBit(E)))
V.set(idx);
Dead &= V.test(idx);
++idx;
}
// Fully dead store - stores to all the components are dead, therefore this
// instruction is dead.
if (Dead) {
LLVM_DEBUG(llvm::dbgs() << "Instruction Dead: " << *I << "\n");
S->DeadStores.push_back(I);
++NumDeadStores;
return;
}
// Partial dead store - stores to some locations are dead, but not all. This
// is a partially dead store. Also at this point we know what locations are
// dead.
LSLocationList Alives;
if (V.any()) {
// Take out locations that are dead.
for (unsigned i = 0; i < V.size(); ++i) {
if (V.test(i))
continue;
// This location is alive.
Alives.push_back(Locs[i]);
}
// Try to create as few aggregated stores as possible out of the locations.
LSLocation::reduce(L, Mod, Alives);
// Oops, we have too many smaller stores generated, bail out.
if (Alives.size() > MaxPartialStoreCount)
return;
// At this point, we are performing a partial dead store elimination.
//
// Locations here have a projection path from their Base, but this
// particular instruction may not be accessing the base, so we need to
// *rebase* the locations w.r.t. to the current instruction.
SILValue B = Locs[0].getBase();
Optional<ProjectionPath> BP = ProjectionPath::getProjectionPath(B, Mem);
// Strip off the projection path from base to the accessed field.
for (auto &X : Alives) {
X.removePathPrefix(BP);
}
// We merely setup the remaining live stores, but do not materialize in IR
// yet, These stores will be materialized before the algorithm exits.
for (auto &X : Alives) {
SILValue Value = X.getPath()->createExtract(Val, I, true);
SILValue Addr = X.getPath()->createExtract(Mem, I, false);
S->LiveAddr.insert(Addr);
S->LiveStores[Addr] = Value;
}
// Lastly, mark the old store as dead.
LLVM_DEBUG(llvm::dbgs() << "Instruction Partially Dead: " << *I << "\n");
S->DeadStores.push_back(I);
++NumPartialDeadStores;
}
}