-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathCopyForwarding.cpp
1318 lines (1202 loc) · 46 KB
/
CopyForwarding.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
//===--- CopyForwarding.cpp - Forward local copies from caller to callee --===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Eliminate local copies of either address-only or reference types.
//
// This opportunity frequently results from a calling convention that transfers
// object ownership from caller to callee. In this convention, the caller
// creates a local copy before passing it to the callee. If the original object
// is immediately destroyed after passing off the copy, then the copy was
// unnecessary. Removing the useless copy can be thought of as forwarding the
// original object directly to the call argument in place of the copy. Hence
// "copy forwarding".
//
// There are two classifications of types that copy forwarding applies to:
// address-only types and references.
//
// Useless copies of address-only types look like this:
//
// %copy = alloc_stack $T
// copy_addr %arg to [init] %copy : $*T
// %ret = apply %callee<T>(%copy) : $@convention(thin) <τ_0_0> (@in τ_0_0) -> ()
// dealloc_stack %copy : $*T
// destroy_addr %arg : $*T
//
// Eliminating the address-only copies eliminates a very expensive call to
// getGenericMetadata.
//
// Useless copies of references look like this:
//
// strong_retain %arg : $A
// %ret = apply %callee(%arg) : $@convention(thin) (@owned A) -> ()
// strong_release %arg : $A
//
// Eliminating the reference copies, avoids artificially bumping the refcount
// which could save a copy of all elements in a COW container.
//
// The actual analysis and optimization do not depend on the copy being linked
// to call arguments. Any obviously useless copy will be eliminated.
//
// TODO: Currently we only handle the address-only case, not the retain/release
// case.
//
// TODO: We should run this at -Onone even though it's not diagnostic.
//
// TODO: Currently we only handle cases in which one side of the copy is block
// local. Either:
// (1) Forward propagate: copy src -> dest; deinit(dest)
// (2) Backward propagate: init(src); copy src -> dest
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "copy-forwarding"
#include "swift/Basic/Assertions.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILVisitor.h"
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
#include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
#include "swift/SILOptimizer/Utils/ValueLifetime.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
STATISTIC(NumCopyForward, "Number of copies removed via forward propagation");
STATISTIC(NumCopyBackward,
"Number of copies removed via backward propagation");
STATISTIC(NumDeadTemp, "Number of copies removed from unused temporaries");
using namespace swift;
// Temporary debugging flag until this pass is better tested.
static llvm::cl::opt<bool> EnableCopyForwarding("enable-copyforwarding",
llvm::cl::init(true));
/// \return true if the given copy source value can only be accessed via the
/// given def (this def uniquely identifies the object).
///
/// (1) An "in" argument.
/// (inouts are also nonaliased, but won't be destroyed in scope)
///
/// (2) A local alloc_stack variable.
static bool isIdentifiedSourceValue(SILValue Def) {
if (auto *Arg = dyn_cast<SILFunctionArgument>(Def)) {
// Check that the argument is passed as an in type. This means there are
// no aliases accessible within this function scope.
SILArgumentConvention Conv = Arg->getArgumentConvention();
switch (Conv) {
case SILArgumentConvention::Indirect_In:
case SILArgumentConvention::Indirect_In_Guaranteed:
return true;
default:
LLVM_DEBUG(llvm::dbgs() << " Skipping Def: Not an @in argument!\n");
return false;
}
}
if (isa<AllocStackInst>(Def))
return true;
return false;
}
/// \return true if the given copy dest value can only be accessed via the given
/// def (this def uniquely identifies the object).
///
/// (1) An "out" or inout argument.
///
/// (2) A local alloc_stack variable.
static bool isIdentifiedDestValue(SILValue Def) {
if (auto *Arg = dyn_cast<SILFunctionArgument>(Def)) {
// Check that the argument is passed as an out type. This means there are
// no aliases accessible within this function scope.
SILArgumentConvention Conv = Arg->getArgumentConvention();
switch (Conv) {
case SILArgumentConvention::Indirect_Inout:
case SILArgumentConvention::Indirect_Out:
return true;
default:
LLVM_DEBUG(llvm::dbgs() << " Skipping Def: Not an @in argument!\n");
return false;
}
}
if (isa<AllocStackInst>(Def))
return true;
return false;
}
/// Return the parameter convention used by Apply to pass an argument
/// indirectly via Address.
///
/// Set Oper to the Apply operand that passes Address.
static SILArgumentConvention getAddressArgConvention(ApplyInst *Apply,
SILValue Address,
Operand *&Oper) {
Oper = nullptr;
auto Args = Apply->getArgumentOperands();
for (auto ArgIdx : indices(Args)) {
if (Args[ArgIdx].get() != Address)
continue;
assert(!Oper && "Address can only be passed once as an indirection.");
Oper = &Args[ArgIdx];
#ifdef NDEBUG
break;
#endif
}
assert(Oper && "Address value not passed as an argument to this call.");
return ApplySite(Apply).getArgumentConvention(*Oper);
}
/// If the given instruction is a store, return the stored value.
static SILValue getStoredValue(SILInstruction *I) {
switch (I->getKind()) {
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Store##Name##Inst:
#include "swift/AST/ReferenceStorage.def"
case SILInstructionKind::StoreInst:
case SILInstructionKind::StoreBorrowInst:
return I->getOperand(0);
default:
return SILValue();
}
}
//===----------------------------------------------------------------------===//
// Forward and backward copy propagation
//===----------------------------------------------------------------------===//
// Visitor for visitAddressUsers.
namespace {
class AddressUserVisitor {
public:
virtual ~AddressUserVisitor() {}
virtual bool visitNormalUse(SILInstruction *user) = 0;
virtual bool visitTake(CopyAddrInst *copy) = 0;
virtual bool visitDestroy(DestroyAddrInst *destroy) = 0;
virtual bool visitDebugValue(DebugValueInst *debugValue) = 0;
};
} // namespace
/// Gather all instructions that use the given `address`
///
/// "Normal" uses are a allowlisted set of uses that guarantees the address is
/// only used as if it refers to a single value and all uses are accounted for
/// (no address projections).
///
/// Takes are "copy_addr [take]"
///
/// Destroys are "destroy_addr"
/// -
///
/// If we are unable to find all uses, for example, because we don't look
/// through struct_element_addr, then return false.
///
/// The collected use points will be consulted during forward and backward
/// copy propagation.
///
/// \param ignoredUser will be ignored if it is is non-null.
static bool visitAddressUsers(SILValue address, SILInstruction *ignoredUser,
AddressUserVisitor &visitor) {
for (Operand *use : address->getUses()) {
SILInstruction *UserInst = use->getUser();
if (UserInst == ignoredUser)
continue;
if (auto *Apply = dyn_cast<ApplyInst>(UserInst)) {
/// A call to materializeForSet exposes an address within the parent
/// object. However, we can rely on a subsequent mark_dependent
/// instruction to take that object as an operand, causing it to escape
/// for the purpose of this analysis.
assert(Apply->getSubstCalleeConv()
.getSILArgumentConvention(use->getOperandNumber()
- Apply->getArgumentOperandNumber())
.isIndirectConvention()
&& "copy_addr location should be passed indirect");
if (!visitor.visitNormalUse(UserInst))
return false;
continue;
}
if (auto *CopyInst = dyn_cast<CopyAddrInst>(UserInst)) {
if (CopyInst->getSrc() == use->get() && CopyInst->isTakeOfSrc()) {
if (!visitor.visitTake(CopyInst))
return false;
} else {
if (!visitor.visitNormalUse(CopyInst))
return false;
}
continue;
}
if (auto *Destroy = dyn_cast<DestroyAddrInst>(UserInst)) {
if (!visitor.visitDestroy(Destroy))
return false;
continue;
}
switch (UserInst->getKind()) {
case SILInstructionKind::LoadInst:
if (!visitor.visitNormalUse(UserInst))
return false;
break;
case SILInstructionKind::ExistentialMetatypeInst:
case SILInstructionKind::InjectEnumAddrInst:
case SILInstructionKind::StoreInst:
if (!visitor.visitNormalUse(UserInst))
return false;
break;
case SILInstructionKind::DebugValueInst:
if (auto *DV = DebugValueInst::hasAddrVal(UserInst)) {
if (!visitor.visitDebugValue(DV))
return false;
} else {
LLVM_DEBUG(llvm::dbgs() << " Skipping copy: use exposes def"
<< *UserInst);
return false;
}
break;
case SILInstructionKind::DeallocStackInst:
break;
default:
// Most likely one of:
// init_enum_data_addr
// open_existential_addr
// partial_apply
// struct_element_addr
// unchecked_take_enum_data_addr
//
// TODO: Peek through struct element users like COWArrayOpts.
//
// TODO: Attempt to analyze partial applies or run closure propagation
// first.
//
// TODO: assert that this list is consistent with
// isTransitiveEscapeInst().
LLVM_DEBUG(llvm::dbgs() << " Skipping copy: use exposes def"
<< *UserInst);
return false;
}
}
return true;
}
namespace {
/// Analyze an instruction that operates on the Address of a forward propagated
/// value.
///
/// Set Oper to the operand that may be safely replaced by an address
/// pointing to an equivalent value. If UserInst cannot be analyzed, Oper is set
/// to nullptr.
///
/// Return true if the instruction destroys the value at Address.
///
/// This checks for the following cases of deinit:
/// - 'in' argument
/// - copy_addr [take] src
/// - copy_addr [!init] dest
/// - destroy_addr
/// - unchecked_take_enum_data_addr
///
/// The copy_addr [!init] case is special because the operand cannot simply be
/// replaced with a new address without causing that location to be
/// reinitialized (after being deinitialized). The caller must check for and
/// handle this case.
///
/// This returns false and sets Oper to a valid operand if the instruction is a
/// projection of the value at the given address. The assumption is that we
/// cannot deinitialize memory via projections.
///
/// This returns true with Oper == nullptr for trivial stores (without a proper
/// deinit).
class AnalyzeForwardUse
: public SILInstructionVisitor<AnalyzeForwardUse, bool> {
public:
SILValue Address;
Operand *Oper;
AnalyzeForwardUse(SILValue Address): Address(Address), Oper(nullptr) {}
bool visitApplyInst(ApplyInst *Apply) {
switch (getAddressArgConvention(Apply, Address, Oper)) {
case SILArgumentConvention::Indirect_In:
return true;
case SILArgumentConvention::Indirect_In_Guaranteed:
case SILArgumentConvention::Indirect_In_CXX:
case SILArgumentConvention::Indirect_Inout:
case SILArgumentConvention::Indirect_InoutAliasable:
return false;
default:
llvm_unreachable("unexpected calling convention for copy_addr user");
}
}
bool visitCopyAddrInst(CopyAddrInst *CopyInst) {
if (CopyInst->getSrc() == Address) {
Oper = &CopyInst->getAllOperands()[CopyAddrInst::Src];
return CopyInst->isTakeOfSrc();
}
assert(!CopyInst->isInitializationOfDest() && "illegal reinitialization");
Oper = &CopyInst->getAllOperands()[CopyAddrInst::Dest];
return true;
}
bool visitStoreInst(StoreInst *Store) {
// Trivial values may be stored prior to the next deinit. A store is an
// implicit "deinit" with no operand to replace.
assert(Store->getOperand(0)->getType().isTrivial(*Store->getFunction()));
return true;
}
bool visitDestroyAddrInst(DestroyAddrInst *UserInst) {
Oper = &UserInst->getOperandRef();
return true;
}
bool visitUncheckedTakeEnumDataAddrInst(
UncheckedTakeEnumDataAddrInst *UserInst) {
Oper = &UserInst->getOperandRef();
return true;
}
bool visitExistentialMetatypeInst(ExistentialMetatypeInst *UserInst) {
Oper = &UserInst->getOperandRef();
return false;
}
bool visitLoadInst(LoadInst *UserInst) {
Oper = &UserInst->getOperandRef();
return false;
}
bool visitOpenExistentialAddrInst(OpenExistentialAddrInst *UserInst) {
Oper = &UserInst->getOperandRef();
return false;
}
bool visitStructElementAddrInst(StructElementAddrInst *UserInst) {
Oper = &UserInst->getOperandRef();
return false;
}
bool visitDebugValueInst(DebugValueInst *UserInst) {
if (UserInst->hasAddrVal()) {
Oper = &UserInst->getOperandRef();
return false;
}
return true;
}
bool visitInitEnumDataAddrInst(InitEnumDataAddrInst *UserInst) {
llvm_unreachable("illegal reinitialization");
}
bool visitInjectEnumAddrInst(InjectEnumAddrInst *UserInst) {
llvm_unreachable("illegal reinitialization");
}
bool visitSILInstruction(SILInstruction *UserInst) {
return false;
}
};
/// Analyze an instruction that operates on the Address of a backward propagated
/// value.
///
/// Set Oper to the operand that my be safely replaced by an address
/// pointing to an equivalent value. If UserInst cannot be analyzed, Oper is set
/// to nullptr.
///
/// Return true if the instruction initializes the value at Address.
///
/// We currently check for the following cases of init:
/// - 'out' argument
/// - copy_addr [init] dest
/// - copy_addr [!init] dest
/// - store
///
/// The copy_addr [!init] case is special because the operand cannot simply be
/// replaced with a new address without causing that location to be
/// deinitialized (before being initialized). The caller must check for and
/// handle this case.
///
/// This returns false and sets Oper to nullptr for projections of the value at
/// the given address. For example, init_enum_data_addr and struct_element_addr
/// may be part of a decoupled initialization sequence.
class AnalyzeBackwardUse
: public SILInstructionVisitor<AnalyzeBackwardUse, bool> {
public:
SILValue Address;
Operand *Oper;
AnalyzeBackwardUse(SILValue Address): Address(Address), Oper(nullptr) {}
bool visitApplyInst(ApplyInst *Apply) {
switch (getAddressArgConvention(Apply, Address, Oper)) {
case SILArgumentConvention::Indirect_Out:
return true;
case SILArgumentConvention::Indirect_Inout:
case SILArgumentConvention::Indirect_InoutAliasable:
case SILArgumentConvention::Indirect_In_Guaranteed:
return false;
case SILArgumentConvention::Indirect_In_CXX:
case SILArgumentConvention::Indirect_In:
llvm_unreachable("copy_addr src destroyed without reinitialization");
default:
llvm_unreachable("unexpected calling convention for copy_addr user");
}
}
bool visitCopyAddrInst(CopyAddrInst *CopyInst) {
if (CopyInst->getDest() == Address) {
Oper = &CopyInst->getAllOperands()[CopyAddrInst::Dest];
return true;
}
Oper = &CopyInst->getAllOperands()[CopyAddrInst::Src];
assert(!CopyInst->isTakeOfSrc() && "illegal deinitialization");
return false;
}
bool visitStoreInst(StoreInst *Store) {
Oper = &Store->getAllOperands()[StoreInst::Dest];
assert(Oper->get() == Address && "illegal store of an address");
return true;
}
bool visitExistentialMetatypeInst(ExistentialMetatypeInst *UserInst) {
Oper = &UserInst->getOperandRef();
return false;
}
bool visitInjectEnumAddrInst(InjectEnumAddrInst *UserInst) {
Oper = &UserInst->getOperandRef();
return false;
}
bool visitLoadInst(LoadInst *UserInst) {
Oper = &UserInst->getOperandRef();
return false;
}
bool visitOpenExistentialAddrInst(OpenExistentialAddrInst *UserInst) {
Oper = &UserInst->getOperandRef();
return false;
}
bool visitDestroyAddrInst(DestroyAddrInst *UserInst) {
llvm_unreachable("illegal deinitialization");
}
bool visitUncheckedTakeEnumDataAddrInst(
UncheckedTakeEnumDataAddrInst *UserInst) {
llvm_unreachable("illegal deinitialization");
}
bool visitUncheckedRefCastAddrInst(
UncheckedRefCastAddrInst *UserInst) {
if (UserInst->getDest() == Address) {
Oper = &UserInst->getAllOperands()[UncheckedRefCastAddrInst::Dest];
}
return true;
}
bool visitDebugValueInst(DebugValueInst *UserInst) {
if (UserInst->hasAddrVal()) {
Oper = &UserInst->getOperandRef();
return false;
}
return true;
}
bool visitSILInstruction(SILInstruction *UserInst) {
return false;
}
};
class CopyForwarding {
// Per-function state.
PostOrderAnalysis *PostOrder;
DominanceAnalysis *DomAnalysis;
RCIdentityAnalysis *RCIAnalysis;
bool HasChanged;
bool HasChangedCFG;
// --- Per copied-def state ---
// Transient state for the current Def valid during forwardCopiesOf.
SILValue CurrentDef;
// Is the addressed defined by CurrentDef ever loaded from?
// This indicates that lifetime of any transitively referenced objects lives
// beyond the value's immediate uses.
bool IsSrcLoadedFrom;
// Does the address defined by CurrentDef have unrecognized uses of a
// nontrivial value stored at its address?
bool HasUnknownStoredValue;
bool HasForwardedToCopy;
SmallPtrSet<SILInstruction*, 16> SrcUserInsts;
SmallPtrSet<SILInstruction*, 4> SrcDebugValueInsts;
SmallVector<CopyAddrInst*, 4> TakePoints;
SmallVector<DestroyAddrInst*, 4> DestroyPoints;
SmallPtrSet<SILBasicBlock*, 32> DeadInBlocks;
// --- Per copy_addr state ---
CopyAddrInst *CurrentCopy = nullptr;
class CopySrcUserVisitor : public AddressUserVisitor {
CopyForwarding &CPF;
public:
CopySrcUserVisitor(CopyForwarding &CPF) : CPF(CPF) {}
virtual bool visitNormalUse(SILInstruction *user) override {
if (isa<LoadInst>(user))
CPF.IsSrcLoadedFrom = true;
if (SILValue storedValue = getStoredValue(user)) {
if (!CPF.markStoredValueUsers(storedValue))
CPF.HasUnknownStoredValue = true;
}
// Bail on multiple uses in the same instruction to avoid complexity.
return CPF.SrcUserInsts.insert(user).second;
}
virtual bool visitTake(CopyAddrInst *take) override {
if (take->getSrc() == take->getDest())
return false;
CPF.TakePoints.push_back(take);
return true;
}
virtual bool visitDestroy(DestroyAddrInst *destroy) override {
CPF.DestroyPoints.push_back(destroy);
return true;
}
virtual bool visitDebugValue(DebugValueInst *debugValue) override {
return CPF.SrcDebugValueInsts.insert(debugValue).second;
}
};
public:
CopyForwarding(PostOrderAnalysis *PO, DominanceAnalysis *DA,
RCIdentityAnalysis *RCIAnalysis)
: PostOrder(PO), DomAnalysis(DA), RCIAnalysis(RCIAnalysis),
HasChanged(false), HasChangedCFG(false),
IsSrcLoadedFrom(false), HasUnknownStoredValue(false),
HasForwardedToCopy(false), CurrentCopy(nullptr) {}
void reset(SILFunction *F) {
// Don't hoist destroy_addr globally in transparent functions. Avoid cloning
// destroy_addr instructions and splitting critical edges before mandatory
// diagnostic passes. For example, PredictableMemOps can no longer remove
// some alloc_stack cases after global destroy hoisting. CopyForwarding will
// be reapplied after the transparent function is inlined at which point
// global hoisting will be done.
if (HasChangedCFG) {
// We are only invalidating the analysis that we use internally.
// We'll invalidate the analysis that are used by other passes at the end.
DomAnalysis->invalidate(F, SILAnalysis::InvalidationKind::FunctionBody);
PostOrder->invalidate(F, SILAnalysis::InvalidationKind::FunctionBody);
RCIAnalysis->invalidate(F, SILAnalysis::InvalidationKind::FunctionBody);
}
CurrentDef = SILValue();
IsSrcLoadedFrom = false;
HasUnknownStoredValue = false;
HasForwardedToCopy = false;
SrcUserInsts.clear();
SrcDebugValueInsts.clear();
TakePoints.clear();
DestroyPoints.clear();
DeadInBlocks.clear();
CurrentCopy = nullptr;
}
bool hasChanged() const { return HasChanged; }
bool hasChangedCFG() const { return HasChangedCFG; }
/// Return true if CurrentDef has been forwarded through one copy into
/// another. This means we should iterate.
bool hasForwardedToCopy() const { return HasForwardedToCopy; }
void forwardCopiesOf(SILValue Def, SILFunction *F);
protected:
bool propagateCopy(CopyAddrInst *CopyInst);
CopyAddrInst *findCopyIntoDeadTemp(
CopyAddrInst *destCopy,
SmallVectorImpl<SILInstruction *> &debugInstsToDelete);
bool forwardDeadTempCopy(CopyAddrInst *destCopy);
bool forwardPropagateCopy();
bool backwardPropagateCopy();
bool isSourceDeadAtCopy();
typedef llvm::SmallSetVector<SILInstruction *, 16> UserVector;
bool doesCopyDominateDestUsers(const UserVector &DirectDestUses);
bool markStoredValueUsers(SILValue storedValue);
};
class CopyDestUserVisitor : public AddressUserVisitor {
SmallPtrSetImpl<SILInstruction *> &DestUsers;
public:
CopyDestUserVisitor(SmallPtrSetImpl<SILInstruction *> &DestUsers)
: DestUsers(DestUsers) {}
virtual bool visitNormalUse(SILInstruction *user) override {
// Bail on multiple uses in the same instruction to avoid complexity.
return DestUsers.insert(user).second;
}
virtual bool visitTake(CopyAddrInst *take) override {
return DestUsers.insert(take).second;
}
virtual bool visitDestroy(DestroyAddrInst *destroy) override {
return DestUsers.insert(destroy).second;
}
virtual bool visitDebugValue(DebugValueInst *debugValue) override {
return DestUsers.insert(debugValue).second;
}
};
} // end anonymous namespace
/// Attempt to forward, then backward propagate this copy.
///
/// The caller has already proven that lifetime of the value being copied ends
/// at the copy. (Either it is a [take] or is immediately destroyed).
///
///
/// If the forwarded copy is not an [init], then insert a destroy of the copy's
/// dest.
bool CopyForwarding::
propagateCopy(CopyAddrInst *CopyInst) {
if (!EnableCopyForwarding)
return false;
// CopyForwarding should be split into per-def-state vs. per-copy-state, but
// this hack is good enough for a pass that's going away "soon".
struct RAIISetCurrentCopy {
CopyAddrInst *&CurrentCopy;
RAIISetCurrentCopy(CopyAddrInst *&CurrentCopy, CopyAddrInst *CopyInst)
: CurrentCopy(CurrentCopy) {
assert(!CurrentCopy);
CurrentCopy = CopyInst;
}
~RAIISetCurrentCopy() {
CurrentCopy = nullptr;
}
};
RAIISetCurrentCopy setCurrentCopy(CurrentCopy, CopyInst);
// Handle copy-of-copy without analyzing uses.
// Assumes that CurrentCopy->getSrc() is dead after CurrentCopy.
assert(CurrentCopy->isTakeOfSrc());
if (forwardDeadTempCopy(CurrentCopy)) {
HasChanged = true;
++NumDeadTemp;
return true;
}
if (forwardPropagateCopy()) {
LLVM_DEBUG(llvm::dbgs() << " Forwarding Copy:" << *CurrentCopy);
if (!CurrentCopy->isInitializationOfDest()) {
// Replace the original copy with a destroy.
SILBuilderWithScope(CurrentCopy)
.createDestroyAddr(CurrentCopy->getLoc(), CurrentCopy->getDest());
}
swift::salvageStoreDebugInfo(CurrentCopy, CurrentCopy->getSrc(),
CurrentCopy->getDest());
CurrentCopy->eraseFromParent();
HasChanged = true;
++NumCopyForward;
return true;
}
// Forward propagation failed. Attempt to backward propagate.
if (CurrentCopy->isInitializationOfDest() && backwardPropagateCopy()) {
LLVM_DEBUG(llvm::dbgs() << " Reversing Copy:" << *CurrentCopy);
swift::salvageStoreDebugInfo(CurrentCopy, CurrentCopy->getDest(),
CurrentCopy->getSrc());
CurrentCopy->eraseFromParent();
HasChanged = true;
++NumCopyBackward;
return true;
}
return false;
}
/// Find a copy into an otherwise dead temporary:
///
/// The given copy is copying out of the temporary
/// copy_addr %temp, %dest
///
/// Precondition: The lifetime of %temp ends at `destCopy`
/// (%temp is CurrentDef).
///
/// Find a previous copy:
/// copy_addr %src, %temp
///
/// Such that it is safe to forward its source into the source of
/// `destCopy`. i.e. `destCopy` can be safely rewritten as:
/// copy_addr %src, %dest
///
/// Otherwise return nullptr. No instructions are harmed in this analysis.
///
/// This can be checked with a simple instruction walk that ends at:
/// - an intervening instruction that may write to memory
/// - a use of the temporary, %temp
///
/// Unlike the forward and backward propagation that finds all use points, this
/// handles copies of address projections. By conservatively checking all
/// intervening instructions, it avoids the need to analyze projection paths.
CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp(
CopyAddrInst *destCopy,
SmallVectorImpl<SILInstruction *> &debugInstsToDelete) {
auto tmpVal = destCopy->getSrc();
assert(tmpVal == CurrentDef);
assert(isIdentifiedSourceValue(tmpVal));
for (auto II = destCopy->getIterator(), IB = destCopy->getParent()->begin();
II != IB;) {
--II;
SILInstruction *UserInst = &*II;
if (auto *srcCopy = dyn_cast<CopyAddrInst>(UserInst)) {
if (srcCopy->getDest() == tmpVal)
return srcCopy;
}
// 'SrcUserInsts' consists of all users of the 'temp'
if (SrcUserInsts.count(UserInst))
return nullptr;
// Collect all debug_value w/ address value instructions between temp to
// dest copy and src to temp copy.
// On success, these debug_value instructions should be deleted.
if (isa<DebugValueInst>(UserInst)) {
// 'SrcDebugValueInsts' consists of all the debug users of 'temp'
if (SrcDebugValueInsts.count(UserInst))
debugInstsToDelete.push_back(UserInst);
continue;
}
if (UserInst->mayWriteToMemory())
return nullptr;
}
return nullptr;
}
/// Forward a copy into a dead temporary as identified by
/// `findCopyIntoDeadTemp`.
///
/// Returns true if the copy was successfully forwarded.
///
/// Old SIL:
/// copy_addr %src, %temp
/// copy_addr %temp, %dest
///
/// New SIL:
/// copy_addr %src, %dest
///
/// Precondition: `srcCopy->getDest()` == `destCopy->getSrc()`
/// Precondition: %src is unused between srcCopy and destCopy.
/// Precondition: The lifetime of %temp ends immediate after `destCopy`.
///
/// Postcondition:
/// - `srcCopy` is erased.
/// - Any initial value in %temp is destroyed at `srcCopy` position.
/// - %temp is uninitialized following `srcCopy` and subsequent instruction
/// attempts to destroy this uninitialized value.
bool CopyForwarding::
forwardDeadTempCopy(CopyAddrInst *destCopy) {
SmallVector<SILInstruction*, 2> debugInstsToDelete;
auto *srcCopy = findCopyIntoDeadTemp(CurrentCopy, debugInstsToDelete);
if (!srcCopy)
return false;
LLVM_DEBUG(llvm::dbgs() << " Temp Copy:" << *srcCopy
<< " to " << *destCopy);
assert(srcCopy->getDest() == destCopy->getSrc());
// This pattern can be trivially folded without affecting %temp destroys:
// copy_addr [...] %src, [init] %temp
// copy_addr [take] %temp, [...] %dest
// If copy into temp is not initializing, add a destroy:
// - copy_addr %src, %temp
// + destroy %temp
if (!srcCopy->isInitializationOfDest()) {
SILBuilderWithScope(srcCopy)
.createDestroyAddr(srcCopy->getLoc(), srcCopy->getDest());
}
// Salvage debug values before deleting them.
swift::salvageStoreDebugInfo(srcCopy, srcCopy->getSrc(), srcCopy->getDest());
// Delete all dead debug_value instructions
for (auto *deadDebugUser : debugInstsToDelete) {
deadDebugUser->eraseFromParent();
}
// `destCopy` is a take. It's safe to simply rewrite destCopy.
destCopy->setSrc(srcCopy->getSrc());
destCopy->setIsTakeOfSrc(srcCopy->isTakeOfSrc());
srcCopy->eraseFromParent();
return true;
}
/// Check that the lifetime of %src ends at the copy and is not reinitialized
/// thereafter with a new value.
bool CopyForwarding::isSourceDeadAtCopy() {
// A single copy_addr [take] %Src.
if (TakePoints.size() == 1 && DestroyPoints.empty() && SrcUserInsts.empty())
return true;
if (TakePoints.empty() && DestroyPoints.size() == 1 &&
SrcUserInsts.size() == 1) {
assert(*SrcUserInsts.begin() == CurrentCopy);
return true;
}
// For now just check for a single copy_addr that destroys its source.
return false;
}
/// Check that all immediate users of the destination address of the copy are
/// dominated by the copy. There is no path around copy that could initialize
/// %dest with a different value.
bool CopyForwarding::doesCopyDominateDestUsers(
const UserVector &DirectDestUsers) {
DominanceInfo *DT = DomAnalysis->get(CurrentCopy->getFunction());
for (auto *user : DirectDestUsers) {
// Check dominance of the parent blocks.
if (!DT->properlyDominates(CurrentCopy, user))
return false;
}
return true;
}
// Return true if all users were recognized.
//
// To find all SSA users of storedValue, we first find the RC root, then search
// past any instructions that may propagate the reference.
bool CopyForwarding::markStoredValueUsers(SILValue storedValue) {
auto *F = storedValue->getFunction();
if (storedValue->getType().isTrivial(*F))
return true;
// Find the RC root, peeking past things like struct_extract.
RCIdentityFunctionInfo *RCI = RCIAnalysis->get(F);
SILValue root = RCI->getRCIdentityRoot(storedValue);
SmallVector<SILInstruction *, 8> users;
RCI->getRCUsers(root, users);
for (SILInstruction *user : users) {
// Recognize any uses that have no results as normal uses. They cannot
// transitively propagate a reference.
if (user->getResults().empty()) {
continue;
}
// Recognize full applies as normal uses. They may transitively retain, but
// the caller cannot rely on that.
if (FullApplySite::isa(user)) {
continue;
}
// A single-valued use is nontransitive if its result is trivial.
if (auto *SVI = dyn_cast<SingleValueInstruction>(user)) {
if (SVI->getType().isTrivial(*F)) {
continue;
}
}
// Conservatively treat everything else as potentially transitively
// retaining the stored value.
LLVM_DEBUG(llvm::dbgs() << " Cannot reduce lifetime. May retain "
<< storedValue
<< " at: " << *user << "\n");
return false;
}
return true;
}
/// Returns the associated dealloc_stack if \p ASI has a single dealloc_stack.
/// Usually this is the case, but the optimizations may generate something like:
/// %1 = alloc_stack
/// if (...) {
/// dealloc_stack %1
/// } else {
/// dealloc_stack %1
/// }
static DeallocStackInst *getSingleDealloc(AllocStackInst *ASI) {
return ASI->getSingleDeallocStack();
}
/// Perform forward copy-propagation. Find a set of uses that the given copy can
/// forward to and replace them with the copy's source.
///
/// We must only replace uses of this copy's value. To do this, we search
/// forward in the current block from the copy that initializes the value to the
/// point of deinitialization. Typically, this will be a point at which the
/// value is passed as an 'in' argument:
/// \code
/// %copy = alloc_stack $T
/// ...
/// CurrentBlock:
/// copy_addr %arg to [init] %copy : $*T
/// ...
/// %ret = apply %callee<T>(%copy) : $@convention(thin) <τ_0_0> (@in τ_0_0) -> ()
/// \endcode
///
/// If the last use (deinit) is a copy, replace it with a destroy+copy[init].
///
/// The caller has already guaranteed that the lifetime of the copy's source
/// ends at this copy. The copy is a [take].
bool CopyForwarding::forwardPropagateCopy() {
SILValue CopyDest = CurrentCopy->getDest();
// Require the copy dest to be a simple alloc_stack. This ensures that all
// instructions that may read from the destination address depend on CopyDest.
if (!isa<AllocStackInst>(CopyDest))
return false;
// Record all direct dest uses. Forward propagation doesn't care if they are
// projections or propagate the address in any way--their operand only needs
// to be substituted with the copy's source.
UserVector DirectDestUsers;
for (auto *Use : CopyDest->getUses()) {
auto *UserInst = Use->getUser();
if (UserInst == CurrentCopy)
continue;
if (isa<DeallocStackInst>(UserInst))
continue;
// Bail on multiple uses in the same instruction so that AnalyzeForwardUse
// does not need to deal with it.
if (!DirectDestUsers.insert(UserInst))
return false;
}
// Looking at
//
// copy_addr %Src, [init] %Dst
//
// We can reuse %Src if it is dead after the copy and not reinitialized. To
// know that we can safely replace all uses of %Dst with source we must know
// that it is uniquely named and cannot be accessed outside of the function
// (an alloc_stack instruction qualifies for this, an inout parameter does
// not). Additionally, we must know that all accesses to %Dst further on must
// have had this copy on their path (there might be reinitialization of %Dst
// later, but there must not be a path around this copy that reads from %Dst).
if (isSourceDeadAtCopy() && doesCopyDominateDestUsers(DirectDestUsers)) {
SILValue CopySrc = CurrentCopy->getSrc();
// Replace all uses of Dest with a use of Src.
for (SILInstruction *user : DirectDestUsers) {
for (Operand &oper : user->getAllOperands()) {
if (oper.get() != CopyDest)
continue;
// Rewrite both read and writes of CopyDest as CopySrc.
oper.set(CopySrc);
}
if (isa<CopyAddrInst>(user))
HasForwardedToCopy = true;
}
// The caller will Remove the destroy_addr of %src.
assert((DestroyPoints.empty() ||