-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDeadObjectElimination.cpp
1307 lines (1136 loc) · 47.3 KB
/
DeadObjectElimination.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
//===--- DeadObjectElimination.cpp - Remove unused objects ---------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This pass eliminates store only alloc_ref objects that have destructors
// without side effects.
//
// The high level overview of the algorithm is that first it visits the
// destructor and attempts to prove that the destructor is well behaved, i.e. it
// does not have any side effects outside of the destructor itself. If the
// destructor can be proven to be well behaved, it then goes through the use
// list of the alloc_ref and attempts to prove that the alloc_ref does not
// escape or is used in a way that could cause side effects. If both of those
// conditions apply, the alloc_ref and its entire use graph is eliminated.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "dead-object-elim"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/IndexTrie.h"
#include "swift/AST/ResilienceExpansion.h"
#include "swift/SIL/BasicBlockUtils.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/Projection.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILDeclRef.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILUndef.h"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/SILOptimizer/Analysis/ArraySemantic.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
#include "swift/SILOptimizer/Utils/ValueLifetime.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Debug.h"
using namespace swift;
STATISTIC(DeadAllocRefEliminated,
"number of AllocRef instructions removed");
STATISTIC(DeadAllocStackEliminated,
"number of AllocStack instructions removed");
STATISTIC(DeadKeyPathEliminated,
"number of keypath instructions removed");
STATISTIC(DeadAllocApplyEliminated,
"number of allocating Apply instructions removed");
using UserList = llvm::SmallSetVector<SILInstruction *, 16>;
namespace {
/// Side effects of a destructor.
enum class DestructorEffects {
None,
/// The destructor contains a "destroyArray" builtin which destroys the tail
/// elements of the object - like in Array.
DestroysTailElems,
Unknown
};
// Analyzing the body of this class destructor is valid because the object is
// dead. This means that the object is never passed to objc_setAssociatedObject,
// so its destructor cannot be extended at runtime.
static SILFunction *getDestructor(AllocRefInstBase *ARI) {
// We can't know the destructor for an alloc_ref_dynamic instruction in
// general.
auto *dynamicAllocRef = dyn_cast<AllocRefDynamicInst>(ARI);
if (dynamicAllocRef &&
!dynamicAllocRef->isDynamicTypeDeinitAndSizeKnownEquivalentToBaseType())
return nullptr;
// We only support classes.
ClassDecl *ClsDecl = ARI->getType().getClassOrBoundGenericClass();
if (!ClsDecl)
return nullptr;
// Look up the destructor of ClsDecl.
DestructorDecl *Destructor = ClsDecl->getDestructor();
assert(Destructor && "getDestructor() should never return a nullptr.");
// Find the destructor name via SILDeclRef.
// FIXME: When destructors get moved into vtables, update this to use the
// vtable for the class.
SILDeclRef Ref(Destructor);
SILFunction *Fn = ARI->getModule().lookUpFunction(Ref);
if (!Fn || Fn->empty()) {
LLVM_DEBUG(llvm::dbgs() << " Could not find destructor.\n");
return nullptr;
}
LLVM_DEBUG(llvm::dbgs() << " Found destructor!\n");
// If the destructor has an objc_method calling convention, we cannot
// analyze it since it could be swapped out from under us at runtime.
if (Fn->getRepresentation() == SILFunctionTypeRepresentation::ObjCMethod) {
LLVM_DEBUG(llvm::dbgs() << " Found Objective-C destructor. Can't "
"analyze!\n");
return nullptr;
}
return Fn;
}
static bool isDestroyArray(SILInstruction *inst) {
BuiltinInst *bi = dyn_cast<BuiltinInst>(inst);
return bi && bi->getBuiltinInfo().ID == BuiltinValueKind::DestroyArray;
}
/// Analyze the destructor for the class of ARI to see if any instructions in it
/// could have side effects on the program outside the destructor. If it does
/// not, then we can eliminate the destructor.
/// TODO: Most default destructors with non-trivial elements will have a
/// destroy_addr of the non-trivial element in the destructor, this analysis
/// will return as having side-effects in such cases, leading to conservative
/// results. Check if we can do better here.
static DestructorEffects doesDestructorHaveSideEffects(AllocRefInstBase *ARI) {
SILFunction *Fn = getDestructor(ARI);
// If we can't find a constructor then assume it has side effects.
if (!Fn)
return DestructorEffects::Unknown;
DestructorEffects effects = DestructorEffects::None;
// A destructor only has one argument, self.
assert(Fn->begin()->getNumArguments() == 1 &&
"Destructor should have only one argument, self.");
SILArgument *Self = Fn->begin()->getArgument(0);
LLVM_DEBUG(llvm::dbgs() << " Analyzing destructor.\n");
// For each BB in the destructor...
for (auto &BB : *Fn) {
// For each instruction I in BB...
for (auto &I : BB) {
LLVM_DEBUG(llvm::dbgs() << " Visiting: " << I);
// If I has no side effects, we can ignore it.
if (!I.mayHaveSideEffects()) {
LLVM_DEBUG(llvm::dbgs() << " SAFE! Instruction has no side "
"effects.\n");
continue;
}
if (auto *fl = dyn_cast<FixLifetimeInst>(&I)) {
// A fix_lifetime of self does cannot have a side effect, because in the
// destructor, Self is deleted.
if (stripCasts(fl->getOperand()) == Self)
continue;
return DestructorEffects::Unknown;
}
// RefCounting operations on Self are ok since we are already in the
// destructor. RefCountingOperations on other instructions could have side
// effects though.
if (auto *RefInst = dyn_cast<RefCountingInst>(&I)) {
if (stripCasts(RefInst->getOperand(0)) == Self) {
// For now all ref counting insts have 1 operand. Put in an assert
// just in case.
assert(RefInst->getNumOperands() == 1 &&
"Make sure RefInst only has one argument.");
LLVM_DEBUG(llvm::dbgs() << " SAFE! Ref count operation on "
"Self.\n");
continue;
}
LLVM_DEBUG(llvm::dbgs() << " UNSAFE! Ref count operation "
"not on self.\n");
return DestructorEffects::Unknown;
}
if (auto *destroy = dyn_cast<DestroyValueInst>(&I)) {
if (stripCasts(destroy->getOperand()) == Self) {
LLVM_DEBUG(llvm::dbgs() << " SAFE! Ref count operation on "
"Self.\n");
continue;
}
LLVM_DEBUG(llvm::dbgs() << " UNSAFE! Ref count operation "
"not on self.\n");
return DestructorEffects::Unknown;
}
// dealloc_stack can be ignored.
if (isa<DeallocStackInst>(I)) {
LLVM_DEBUG(llvm::dbgs() << " SAFE! dealloc_stack can be "
"ignored.\n");
continue;
}
if (isa<BeginBorrowInst>(I) || isa<EndBorrowInst>(I) || isa<EndLifetimeInst>(I)) {
continue;
}
// dealloc_ref on self can be ignored, but dealloc_ref on anything else
// cannot be eliminated.
if (auto *DeallocRef = dyn_cast<DeallocRefInst>(&I)) {
if (stripCasts(DeallocRef->getOperand()) == Self) {
LLVM_DEBUG(llvm::dbgs() <<" SAFE! dealloc_ref on self.\n");
continue;
} else {
LLVM_DEBUG(llvm::dbgs() << " UNSAFE! dealloc_ref on value "
"besides self.\n");
return DestructorEffects::Unknown;
}
}
// Storing into the object can be ignored.
if (auto *SI = dyn_cast<StoreInst>(&I))
if (stripAddressProjections(SI->getDest()) == Self) {
LLVM_DEBUG(llvm::dbgs() << " SAFE! Instruction is a store "
"into self.\n");
continue;
}
if (isDestroyArray(&I)) {
// Check if the "destroyArray" destroys the tail elements of the object,
// like in Array.
SILValue addr = I.getOperand(1);
auto *atp = dyn_cast<AddressToPointerInst>(addr);
if (!atp)
return DestructorEffects::Unknown;
auto *rta = dyn_cast<RefTailAddrInst>(atp->getOperand());
if (!rta)
return DestructorEffects::Unknown;
effects = DestructorEffects::DestroysTailElems;
if (rta->getOperand() == Self)
continue;
}
LLVM_DEBUG(llvm::dbgs() << " UNSAFE! Unknown instruction.\n");
// Otherwise, we can't remove the deallocation completely.
return DestructorEffects::Unknown;
}
}
// We didn't find any side effects.
return effects;
}
//===----------------------------------------------------------------------===//
// Use Graph Analysis
//===----------------------------------------------------------------------===//
/// Returns false if Inst is an instruction that would require us to keep the
/// alloc_ref alive.
static bool canZapInstruction(SILInstruction *Inst, bool acceptRefCountInsts,
bool onlyAcceptTrivialStores) {
if (isa<DestroyValueInst>(Inst)) {
return acceptRefCountInsts;
}
if (isa<CopyValueInst>(Inst) || isa<BeginBorrowInst>(Inst) ||
isa<MoveValueInst>(Inst)) {
return true;
}
if (isa<EndInitLetRefInst>(Inst) || isa<BeginDeallocRefInst>(Inst) ||
isa<FixLifetimeInst>(Inst) || isa<EndBorrowInst>(Inst) ||
isa<UpcastInst>(Inst) || isa<UncheckedRefCastInst>(Inst))
return true;
// It is ok to eliminate various retains/releases. We are either removing
// everything or nothing.
if (isa<RefCountingInst>(Inst) ||
// dealloc_partial_ref invokes releases implicitly
isa<DeallocPartialRefInst>(Inst))
return acceptRefCountInsts;
if (isa<InjectEnumAddrInst>(Inst))
return true;
if (isa<KeyPathInst>(Inst))
return true;
// We know that the destructor has no side effects so we can remove the
// deallocation instruction too.
if (isa<DeallocationInst>(Inst) || isa<AllocationInst>(Inst))
return true;
// Much like deallocation, destroy addr is safe.
if (isa<DestroyAddrInst>(Inst))
return true;
// We have already checked that we are storing into the pointer before we
// added it to the worklist. Here, in the case we are allowing non-trivial
// stores, check if the store's source is lexical, if so return false.
// Deleting a dead object with non-trivial stores, will need compensating
// destroys at the store for it's source, which will shorten the lifetime of
// the store's source.
if (auto *store = dyn_cast<StoreInst>(Inst)) {
auto storeSrc = store->getSrc();
return storeSrc->getType().isTrivial(*store->getFunction()) ||
(!onlyAcceptTrivialStores &&
(!store->getFunction()->hasOwnership() || !storeSrc->isLexical()));
}
// Conceptually this instruction has no side-effects.
if (isa<InitExistentialAddrInst>(Inst))
return true;
if (isa<BeginAccessInst>(Inst) || isa<EndAccessInst>(Inst))
return true;
// The value form of zero init is not a user of any operand. The address
// form however is easily zappable because it's always a trivial store.
if (auto bi = dyn_cast<BuiltinInst>(Inst)) {
if (bi->getBuiltinKind() == BuiltinValueKind::ZeroInitializer) {
return true;
}
}
// If Inst does not read or write to memory, have side effects, and is not a
// terminator, we can zap it.
if (!Inst->mayHaveSideEffects() && !Inst->mayReadFromMemory() &&
!isa<TermInst>(Inst))
return true;
// Otherwise we do not know how to handle this instruction. Be conservative
// and don't zap it.
return false;
}
/// Returns true if all stores in \p users store to the tail elements of
/// \p allocRef, which are destroyed by the \p destroyArray builtin.
static bool onlyStoresToTailObjects(BuiltinInst *destroyArray,
const UserList &users,
AllocRefInstBase *allocRef) {
// Get the number of destroyed elements.
auto *literal = dyn_cast<IntegerLiteralInst>(destroyArray->getArguments()[2]);
if (!literal || literal->getValue().getSignificantBits() > 32)
return false;
int numDestroyed = literal->getValue().getSExtValue();
SILFunction *func = destroyArray->getFunction();
SILBasicBlock *storesBlock = nullptr;
// Check if the destroyArray destroys the tail elements of allocRef.
auto destroyPath = AccessPath::compute(destroyArray->getArguments()[1]);
AccessStorage storage = destroyPath.getStorage();
if (auto *beginDealloc = dyn_cast<BeginDeallocRefInst>(storage.getRoot())) {
destroyPath = AccessPath(
storage.transformReference(beginDealloc->getAllocation()),
destroyPath.getPathNode(),
destroyPath.getOffset());
}
if (destroyPath != AccessPath::forTailStorage(allocRef))
return false;
SmallVector<AccessPath, 32> pathsToCheck;
// Check all stores to the tail elements.
for (SILInstruction *user : users) {
auto *store = dyn_cast<StoreInst>(user);
if (!store)
continue;
assert(users.count(store->getSrc()->getDefiningInstruction()) == 0 &&
"Storing a use of an array (that would mean the array escapes)?");
// All stores must be in the same block. This ensure that the stores
// dominate the destroyArray (which may be in a different block).
if (storesBlock && store->getParent() != storesBlock)
return false;
storesBlock = store->getParent();
AccessPath storePath = AccessPath::compute(store->getDest());
if (!storePath.isValid())
return false;
// We don't care about trivial stores.
if (store->getSrc()->getType().isTrivial(*func))
continue;
// Check if it's a store to the tail elements.
if (!destroyPath.contains(storePath.withOffset(0)))
return false;
// Check if the store is within the range of the destroyed array. In OSSA
// we would not need this check. Otherwise it would be a memory lifetime
// failure.
if (storePath.getOffset() < 0 || storePath.getOffset() >= numDestroyed)
return false;
pathsToCheck.push_back(storePath);
}
// In non-OSSA we have to check if two paths overlap, because we could end up
// over-releasing the stored objects.
// Group the paths by tail-element index, so that we only have to check within
// a tail-element group.
std::sort(pathsToCheck.begin(), pathsToCheck.end(), [](AccessPath p1, AccessPath p2) {
return p1.getOffset() < p2.getOffset();
});
for (unsigned i = 0, n = pathsToCheck.size(); i < n; ++i) {
for (unsigned j = i + 1;
j < n && pathsToCheck[i].getOffset() == pathsToCheck[j].getOffset(); ++j) {
if (pathsToCheck[i].mayOverlap(pathsToCheck[j]))
return false;
// Limit the number of checks to avoid quadratic complexity.
if (j > i + 8)
return false;
}
}
return true;
}
/// Analyze the use graph of AllocRef for any uses that would prevent us from
/// zapping it completely.
static bool
hasUnremovableUsers(SILInstruction *allocation, UserList *Users,
bool acceptRefCountInsts, bool onlyAcceptTrivialStores) {
SmallVector<SILInstruction *, 16> Worklist;
Worklist.push_back(allocation);
LLVM_DEBUG(llvm::dbgs() << " Analyzing Use Graph.");
SmallVector<RefElementAddrInst *, 8> refElementAddrs;
BuiltinInst *destroyArray = nullptr;
auto *allocRef = dyn_cast<AllocRefInstBase>(allocation);
while (!Worklist.empty()) {
SILInstruction *I = Worklist.pop_back_val();
LLVM_DEBUG(llvm::dbgs() << " Visiting: " << *I);
// Insert the instruction into our InvolvedInstructions set. If we have
// already seen it, then don't reprocess all of the uses.
if (Users && !Users->insert(I)) {
LLVM_DEBUG(llvm::dbgs() << " Already seen skipping...\n");
continue;
} else if (auto *rea = dyn_cast<RefElementAddrInst>(I)) {
if (rea != allocation && !rea->getType().isTrivial(*rea->getFunction()))
refElementAddrs.push_back(rea);
} else if (allocRef && isDestroyArray(I)) {
if (destroyArray)
return true;
destroyArray = cast<BuiltinInst>(I);
} else if (!canZapInstruction(I, acceptRefCountInsts,
onlyAcceptTrivialStores)) {
LLVM_DEBUG(llvm::dbgs() << " Found instruction we can't zap...\n");
return true;
}
// At this point, we can remove the instruction as long as all of its users
// can be removed as well. Scan its users and add them to the worklist for
// recursive processing.
for (auto result : I->getResults()) {
for (auto *Op : result->getUses()) {
auto *User = Op->getUser();
// Make sure that we are only storing into our users, not storing our
// users which would be an escape.
if (auto *SI = dyn_cast<StoreInst>(User))
if (Op->get() == SI->getSrc()) {
LLVM_DEBUG(llvm::dbgs() << " Found store of pointer. "
"Failure: "
<< *SI);
return true;
}
// Otherwise, add normal instructions to the worklist for processing.
Worklist.push_back(User);
}
}
}
if (!allocation->getFunction()->hasOwnership()) {
// In non-ossa, if we found a destroy array builtin that destroys the tail
// elements, ensure all stores are to the taile elems.
if (destroyArray) {
return !onlyStoresToTailObjects(destroyArray, *Users, allocRef);
}
// In non-OSSA we cannot reliably track the lifetime of non-trivial stored
// properties. Removing the dead alloc_ref might leak a property value.
for (RefElementAddrInst *rea : refElementAddrs) {
// Re-run the check with not accepting non-trivial stores.
if (hasUnremovableUsers(rea, nullptr, acceptRefCountInsts,
/*onlyAcceptTrivialStores*/ true))
return true;
}
}
return false;
}
//===----------------------------------------------------------------------===//
// NonTrivial DeadObject Elimination
//===----------------------------------------------------------------------===//
/// Determine if an object is dead. Compute its original lifetime. Find the
/// lifetime endpoints reached by each store of a refcounted object into the
/// object.
///
/// TODO: Use this to remove nontrivial dead alloc_ref/alloc_stack, not just
/// dead arrays. We just need a slightly better destructor analysis to prove
/// that it only releases elements.
class DeadObjectAnalysis {
// Map each address projection of this object to a list of stores.
// Do not iterate over this map's entries.
using AddressToStoreMap =
llvm::DenseMap<IndexTrieNode*, llvm::SmallVector<StoreInst*, 4> >;
// The value of the object's address at the point of allocation.
SILValue NewAddrValue;
// Track all users that extend the lifetime of the object.
UserList AllUsers;
// Trie of stored locations.
std::unique_ptr<IndexTrieNode> AddressProjectionTrie;
// Track all stores of refcounted elements per address projection.
AddressToStoreMap StoredLocations;
// Are any uses behind a PointerToAddressInst?
bool SeenPtrToAddr;
public:
explicit DeadObjectAnalysis(SILValue V):
NewAddrValue(V), AddressProjectionTrie(nullptr), SeenPtrToAddr(false) {}
bool analyze();
ArrayRef<SILInstruction*> getAllUsers() const {
return ArrayRef<SILInstruction*>(AllUsers.begin(), AllUsers.end());
}
template<typename Visitor>
void visitStoreLocations(Visitor visitor) {
visitStoreLocations(visitor, AddressProjectionTrie.get());
}
private:
void addStore(StoreInst *Store, IndexTrieNode *AddressNode);
bool recursivelyCollectInteriorUses(ValueBase *DefInst,
IndexTrieNode *AddressNode,
bool IsInteriorAddress);
template<typename Visitor>
void visitStoreLocations(Visitor visitor, IndexTrieNode *AddressNode);
};
} // end anonymous namespace
// Record a store into this object.
void DeadObjectAnalysis::
addStore(StoreInst *Store, IndexTrieNode *AddressNode) {
if (Store->getSrc()->getType().isTrivial(*Store->getFunction()))
return;
// SSAUpdater cannot handle multiple defs in the same blocks. Therefore, we
// ensure that only one store per block is present in the StoredLocations.
auto &StoredLocs = StoredLocations[AddressNode];
for (auto &OtherSt : StoredLocs) {
// In case the object's address is stored in itself.
if (OtherSt == Store)
return;
if (OtherSt->getParent() == Store->getParent()) {
for (auto II = std::next(Store->getIterator()),
IE = Store->getParent()->end();
II != IE; ++II) {
if (&*II == OtherSt)
return; // Keep the other store.
}
// Replace OtherSt with this store.
OtherSt = Store;
return;
}
}
StoredLocations[AddressNode].push_back(Store);
}
// Collect instructions that either initialize or release any values at the
// object defined by defInst.
//
// Populates AllUsers, AddressProjectionTrie, and StoredLocations.
//
// If a use is visited that potentially causes defInst's address to
// escape, then return false without fully populating the data structures.
//
// `InteriorAddress` is true if the current address projection already includes
// a struct/ref/tuple element address. index_addr is only expected at the top
// level. The first non-index element address encountered pushes an "zero index"
// address node to represent the implicit index_addr #0. We do not support
// nested indexed data types in native SIL.
bool DeadObjectAnalysis::
recursivelyCollectInteriorUses(ValueBase *DefInst,
IndexTrieNode* AddressNode,
bool IsInteriorAddress) {
for (auto Op : DefInst->getUses()) {
auto User = Op->getUser();
// Lifetime endpoints that don't allow the address to escape.
if (isa<RefCountingInst>(User) || isa<DebugValueInst>(User) ||
isa<FixLifetimeInst>(User) || isa<DestroyValueInst>(User) ||
isa<EndBorrowInst>(User)) {
AllUsers.insert(User);
continue;
}
// Initialization points.
if (auto *Store = dyn_cast<StoreInst>(User)) {
// Bail if this address is stored to another object.
if (Store->getDest() != DefInst) {
LLVM_DEBUG(llvm::dbgs() <<" Found an escaping store: " << *User);
return false;
}
IndexTrieNode *StoreAddrNode = AddressNode;
// Push an extra zero index node for a store to noninterior address.
if (!IsInteriorAddress)
StoreAddrNode = AddressNode->getChild(0);
addStore(Store, StoreAddrNode);
AllUsers.insert(User);
continue;
}
if (auto *MDI = dyn_cast<MarkDependenceInst>(User)) {
if (!recursivelyCollectInteriorUses(MDI, AddressNode,
IsInteriorAddress)) {
return false;
}
continue;
}
if (auto *bb = dyn_cast<BeginBorrowInst>(User)) {
if (!recursivelyCollectInteriorUses(bb, AddressNode,
IsInteriorAddress)) {
return false;
}
continue;
}
if (auto PTAI = dyn_cast<PointerToAddressInst>(User)) {
// Only one pointer-to-address is allowed for safety.
if (SeenPtrToAddr)
return false;
SeenPtrToAddr = true;
if (!recursivelyCollectInteriorUses(PTAI, AddressNode, IsInteriorAddress))
return false;
continue;
}
// Recursively follow projections.
if (auto *svi = dyn_cast<SingleValueInstruction>(User)) {
ProjectionIndex PI(svi);
if (PI.isValid()) {
IndexTrieNode *ProjAddrNode = AddressNode;
bool ProjInteriorAddr = IsInteriorAddress;
if (Projection::isAddressProjection(svi)) {
if (isa<IndexAddrInst>(svi)) {
// Don't support indexing within an interior address.
if (IsInteriorAddress)
return false;
}
else if (!IsInteriorAddress) {
// Push an extra zero index node for the first interior address.
ProjAddrNode = AddressNode->getChild(0);
ProjInteriorAddr = true;
}
}
else if (IsInteriorAddress) {
// Don't expect to extract values once we've taken an address.
return false;
}
if (!recursivelyCollectInteriorUses(svi,
ProjAddrNode->getChild(PI.Index),
ProjInteriorAddr)) {
return false;
}
continue;
}
ArraySemanticsCall AS(svi);
if (AS.getKind() == swift::ArrayCallKind::kArrayFinalizeIntrinsic) {
if (!recursivelyCollectInteriorUses(svi, AddressNode, IsInteriorAddress))
return false;
continue;
}
}
// Otherwise bail.
LLVM_DEBUG(llvm::dbgs() << " Found an escaping use: " << *User);
return false;
}
return true;
}
// Track the lifetime, release points, and released values referenced by a
// newly allocated object.
bool DeadObjectAnalysis::analyze() {
LLVM_DEBUG(llvm::dbgs() << " Analyzing nontrivial dead object: "
<< NewAddrValue);
// Populate AllValues, AddressProjectionTrie, and StoredLocations.
AddressProjectionTrie.reset(new IndexTrieNode());
if (!recursivelyCollectInteriorUses(NewAddrValue,
AddressProjectionTrie.get(), false)) {
return false;
}
// If all stores are leaves in the AddressProjectionTrie, then we can analyze
// the stores that reach the end of the object lifetime. Otherwise bail.
// This iteration order is nondeterministic but has no impact.
for (auto &AddressToStoresPair : StoredLocations) {
IndexTrieNode *Location = AddressToStoresPair.first;
if (!Location->isLeaf())
return false;
}
return true;
}
template<typename Visitor>
void DeadObjectAnalysis::
visitStoreLocations(Visitor visitor, IndexTrieNode *AddressNode) {
if (AddressNode->isLeaf()) {
auto LocI = StoredLocations.find(AddressNode);
if (LocI != StoredLocations.end())
visitor(LocI->second);
return;
}
for (auto *SubAddressNode : AddressNode->getChildren())
visitStoreLocations(visitor, SubAddressNode);
}
// At each release point, release the reaching values that have been stored to
// this address.
//
// The caller has already determined that all Stores are to the same element
// within an otherwise dead object.
static void insertReleases(ArrayRef<StoreInst*> Stores,
ArrayRef<SILInstruction*> ReleasePoints,
SILSSAUpdater &SSAUp) {
assert(!Stores.empty());
SILValue StVal = Stores.front()->getSrc();
SSAUp.initialize(StVal->getFunction(), StVal->getType(),
StVal->getOwnershipKind());
for (auto *Store : Stores)
SSAUp.addAvailableValue(Store->getParent(), Store->getSrc());
SILLocation Loc = Stores[0]->getLoc();
for (auto *RelPoint : ReleasePoints) {
SILBuilder B(RelPoint);
// This does not use the SSAUpdater::RewriteUse API because it does not do
// the right thing for local uses. We have already ensured a single store
// per block, and all release points occur after all stores. Therefore we
// can simply ask SSAUpdater for the reaching store.
SILValue RelVal = SSAUp.getValueAtEndOfBlock(RelPoint->getParent());
B.emitDestroyValueOperation(Loc, RelVal);
}
}
//===----------------------------------------------------------------------===//
// Function Processing
//===----------------------------------------------------------------------===//
/// Does this instruction perform object allocation with no other observable
/// side effect?
static bool isAllocatingApply(SILInstruction *Inst) {
ArraySemanticsCall ArrayAlloc(Inst);
return ArrayAlloc.getKind() == ArrayCallKind::kArrayUninitialized ||
ArrayAlloc.getKind() == ArrayCallKind::kArrayUninitializedIntrinsic;
}
namespace {
class DeadObjectElimination : public SILFunctionTransform {
llvm::DenseMap<SILType, DestructorEffects> DestructorAnalysisCache;
InstructionDeleter deleter;
DominanceInfo *domInfo = nullptr;
void removeInstructions(ArrayRef<SILInstruction*> toRemove);
/// Try to salvage the debug info for a dead instruction removed by
/// DeadObjectElimination.
///
/// Dead stores will be replaced by a debug value for the object variable,
/// using a fragment expression. By walking from the store to the allocation,
/// we can know which member of the object is being assigned, and create
/// fragments for each member. Other instructions are not salvaged.
/// Currently only supports dead stack-allocated objects.
void salvageDebugInfo(SILInstruction *toBeRemoved);
std::optional<SILDebugVariable> buildDIExpression(SILInstruction *current);
bool processAllocRef(AllocRefInstBase *ARI);
bool processAllocStack(AllocStackInst *ASI);
bool processKeyPath(KeyPathInst *KPI);
bool processAllocBox(AllocBoxInst *ABI){ return false;}
bool processAllocApply(ApplyInst *AI, DeadEndBlocks &DEBlocks);
bool insertCompensatingReleases(SILInstruction *before,
const UserList &users);
bool getDeadInstsAfterInitializerRemoved(
ApplyInst *AI, llvm::SmallVectorImpl<SILInstruction *> &ToDestroy);
bool removeAndReleaseArray(
SingleValueInstruction *NewArrayValue, DeadEndBlocks &DEBlocks);
bool processFunction(SILFunction &Fn) {
DeadEndBlocks DEBlocks(&Fn);
DestructorAnalysisCache.clear();
LLVM_DEBUG(llvm::dbgs() << "Processing " << Fn.getName() << "\n");
bool Changed = false;
for (auto &BB : Fn) {
for (SILInstruction &inst : BB.deletableInstructions()) {
if (auto *A = dyn_cast<AllocRefInstBase>(&inst))
Changed |= processAllocRef(A);
else if (auto *A = dyn_cast<AllocStackInst>(&inst))
Changed |= processAllocStack(A);
else if (auto *KPI = dyn_cast<KeyPathInst>(&inst))
Changed |= processKeyPath(KPI);
else if (auto *A = dyn_cast<AllocBoxInst>(&inst))
Changed |= processAllocBox(A);
else if (auto *A = dyn_cast<ApplyInst>(&inst))
Changed |= processAllocApply(A, DEBlocks);
}
deleter.cleanupDeadInstructions();
}
return Changed;
}
void run() override {
assert(!domInfo);
if (processFunction(*getFunction())) {
invalidateAnalysis(SILAnalysis::InvalidationKind::CallsAndInstructions);
}
domInfo = nullptr;
}
};
} // end anonymous namespace
void
DeadObjectElimination::removeInstructions(ArrayRef<SILInstruction*> toRemove) {
for (auto *I : toRemove) {
I->replaceAllUsesOfAllResultsWithUndef();
// Now we know that I should not have any uses... erase it from its parent.
deleter.forceDelete(I);
}
}
void DeadObjectElimination::salvageDebugInfo(SILInstruction *toBeRemoved) {
auto *SI = dyn_cast<StoreInst>(toBeRemoved);
if (!SI)
return;
auto *parent = SI->getDest()->getDefiningInstruction();
auto varInfo = buildDIExpression(parent);
if (!varInfo)
return;
// Note: The instruction should logically be in SI's scope.
// However, LLVM does not support variables and stores in different scopes,
// so we use the variable's scope.
SILBuilder Builder(SI, varInfo->Scope);
Builder.createDebugValue(SI->getLoc(), SI->getSrc(), *varInfo);
}
std::optional<SILDebugVariable>
DeadObjectElimination::buildDIExpression(SILInstruction *current) {
if (!current)
return {};
if (auto dvci = dyn_cast<AllocStackInst>(current)) {
auto var = dvci->getVarInfo();
if (!var)
return {};
if (!var->Type)
var->Type = dvci->getElementType();
return var;
}
if (auto *tupleAddr = dyn_cast<TupleElementAddrInst>(current)) {
auto *definer = tupleAddr->getOperand().getDefiningInstruction();
auto path = buildDIExpression(definer);
if (!path)
return {};
path->DIExpr.append(SILDebugInfoExpression::createTupleFragment(
tupleAddr->getTupleType(), tupleAddr->getFieldIndex()));
return path;
}
if (auto *structAddr = dyn_cast<StructElementAddrInst>(current)) {
auto *definer = structAddr->getOperand().getDefiningInstruction();
auto path = buildDIExpression(definer);
if (!path)
return {};
path->DIExpr.append(SILDebugInfoExpression::createFragment(
structAddr->getField()));
return path;
}
return {};
}
bool DeadObjectElimination::processAllocRef(AllocRefInstBase *ARI) {
// Ok, we have an alloc_ref. Check the cache to see if we have already
// computed the destructor behavior for its SILType.
DestructorEffects destructorEffects;
SILType Type = ARI->getType();
auto CacheSearchResult = DestructorAnalysisCache.find(Type);
if (CacheSearchResult != DestructorAnalysisCache.end()) {
// Ok we found a value in the cache.
destructorEffects = CacheSearchResult->second;
} else {
// We did not find a value in the cache for our destructor. Analyze the
// destructor to make sure it has no side effects. For now this only
// supports alloc_ref of classes so any alloc_ref with a reference type
// that is not a class this will return false for. Once we have analyzed
// it, set Behavior to that value and insert the value into the Cache.
//
// TODO: We should be able to handle destructors that do nothing but release
// members of the object.
destructorEffects = doesDestructorHaveSideEffects(ARI);
DestructorAnalysisCache[Type] = destructorEffects;
}
// Our destructor has no side effects, so if we can prove that no loads
// escape, then we can completely remove the use graph of this alloc_ref.
UserList UsersToRemove;
if (hasUnremovableUsers(ARI, &UsersToRemove,
/*acceptRefCountInsts=*/ destructorEffects != DestructorEffects::Unknown,
/*onlyAcceptTrivialStores*/false)) {
LLVM_DEBUG(llvm::dbgs() << " Found a use that cannot be zapped...\n");
return false;
}
if (!ARI->getFunction()->hasOwnership()) {
// Find the instruction which releases the object's tail elements.
SILInstruction *releaseOfTailElems = nullptr;
for (SILInstruction *user : UsersToRemove) {
if (isDestroyArray(user) ||
(destructorEffects == DestructorEffects::DestroysTailElems &&
isa<RefCountingInst>(user) && user->mayRelease())) {
// Bail if we find multiple such instructions.
if (releaseOfTailElems)
return false;
releaseOfTailElems = user;
}
}
if (releaseOfTailElems) {
if (!insertCompensatingReleases(releaseOfTailElems, UsersToRemove)) {
return false;
}
}
}
if (ARI->getFunction()->hasOwnership()) {
// In ossa, we are going to delete the dead element store and insert a
// destroy_value of the store's source. This is shortening the store's
// source lifetime. Check if there was a pointer escape of the store's
// source, if so bail out.
for (auto *user : UsersToRemove) {
auto *store = dyn_cast<StoreInst>(user);
if (!store ||
store->getOwnershipQualifier() == StoreOwnershipQualifier::Trivial)
continue;
if (findPointerEscape(store->getSrc())) {
return false;
}
}
for (auto *user : UsersToRemove) {
auto *store = dyn_cast<StoreInst>(user);
if (!store ||
store->getOwnershipQualifier() == StoreOwnershipQualifier::Trivial) {
continue;
}
SILBuilderWithScope(store).createDestroyValue(store->getLoc(),
store->getSrc());
}
}
// Remove the AllocRef and all of its users.
removeInstructions(
ArrayRef<SILInstruction*>(UsersToRemove.begin(), UsersToRemove.end()));
LLVM_DEBUG(llvm::dbgs() << " Success! Eliminating alloc_ref.\n");
++DeadAllocRefEliminated;
return true;
}
bool DeadObjectElimination::processAllocStack(AllocStackInst *ASI) {
// Trivial types don't have destructors.
bool isTrivialType = ASI->getElementType().isTrivial(*ASI->getFunction());
// In non-ossa, only accept trivial stores if we have a non-trivial
// alloc_stack