forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSILInstruction.cpp
940 lines (790 loc) · 29.5 KB
/
SILInstruction.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
//===--- SILInstruction.cpp - Instructions for SIL code -------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the high-level SILInstruction classes used for SIL code.
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/SILInstruction.h"
#include "swift/Basic/type_traits.h"
#include "swift/Basic/Unicode.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILCloner.h"
#include "swift/SIL/SILDebugScope.h"
#include "swift/SIL/SILVisitor.h"
#include "swift/AST/AST.h"
#include "swift/Basic/AssertImplements.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/SIL/SILModule.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/ErrorHandling.h"
using namespace swift;
using namespace Lowering;
//===----------------------------------------------------------------------===//
// Instruction-specific properties on SILValue
//===----------------------------------------------------------------------===//
Optional<SILLocation> SILValue::getLoc() const {
if (auto I = dyn_cast<SILInstruction>(*this)) {
return I->getLoc();
}
return None;
}
SILLocation SILInstruction::getLoc() const { return Location.getLocation(); }
const SILDebugScope *SILInstruction::getDebugScope() const {
return Location.getScope();
}
void SILInstruction::setDebugScope(SILBuilder &B, const SILDebugScope *DS) {
if (getDebugScope() && getDebugScope()->InlinedCallSite)
assert(DS->InlinedCallSite && "throwing away inlined scope info");
assert(DS->InlinedCallSite || DS->SILFn == getFunction() &&
"scope of a non-inlined instruction points to different function");
Location = *B.getOrCreateDebugLocation(getLoc(), DS);
}
//===----------------------------------------------------------------------===//
// ilist_traits<SILInstruction> Implementation
//===----------------------------------------------------------------------===//
// The trait object is embedded into a basic block. Use dirty hacks to
// reconstruct the BB from the 'self' pointer of the trait.
SILBasicBlock *llvm::ilist_traits<SILInstruction>::getContainingBlock() {
size_t Offset(
size_t(&((SILBasicBlock *)0->*SILBasicBlock::getSublistAccess())));
iplist<SILInstruction> *Anchor(static_cast<iplist<SILInstruction> *>(this));
return reinterpret_cast<SILBasicBlock *>(reinterpret_cast<char *>(Anchor) -
Offset);
}
void llvm::ilist_traits<SILInstruction>::addNodeToList(SILInstruction *I) {
assert(I->ParentBB == 0 && "Already in a list!");
I->ParentBB = getContainingBlock();
}
void llvm::ilist_traits<SILInstruction>::removeNodeFromList(SILInstruction *I) {
// When an instruction is removed from a BB, clear the parent pointer.
assert(I->ParentBB && "Not in a list!");
I->ParentBB = 0;
}
void llvm::ilist_traits<SILInstruction>::
transferNodesFromList(llvm::ilist_traits<SILInstruction> &L2,
llvm::ilist_iterator<SILInstruction> first,
llvm::ilist_iterator<SILInstruction> last) {
// If transferring instructions within the same basic block, no reason to
// update their parent pointers.
SILBasicBlock *ThisParent = getContainingBlock();
if (ThisParent == L2.getContainingBlock()) return;
// Update the parent fields in the instructions.
for (; first != last; ++first)
first->ParentBB = ThisParent;
}
//===----------------------------------------------------------------------===//
// SILInstruction Implementation
//===----------------------------------------------------------------------===//
// Assert that all subclasses of ValueBase implement classof.
#define VALUE(CLASS, PARENT) \
ASSERT_IMPLEMENTS_STATIC(CLASS, PARENT, classof, bool(const ValueBase*));
#include "swift/SIL/SILNodes.def"
SILFunction *SILInstruction::getFunction() {
return getParent()->getParent();
}
const SILFunction *SILInstruction::getFunction() const {
return getParent()->getParent();
}
SILModule &SILInstruction::getModule() const {
return getFunction()->getModule();
}
/// removeFromParent - This method unlinks 'self' from the containing basic
/// block, but does not delete it.
///
void SILInstruction::removeFromParent() {
getParent()->remove(this);
}
/// eraseFromParent - This method unlinks 'self' from the containing basic
/// block and deletes it.
///
void SILInstruction::eraseFromParent() {
assert(use_empty() && "There are uses of instruction being deleted.");
getParent()->erase(this);
}
/// Unlink this instruction from its current basic block and insert it into
/// the basic block that Later lives in, right before Later.
void SILInstruction::moveBefore(SILInstruction *Later) {
if (this == Later)
return;
getParent()->remove(this);
Later->getParent()->insert(Later, this);
}
/// Unlink this instruction from its current basic block and insert it into
/// the basic block that Earlier lives in, right after Earlier.
void SILInstruction::moveAfter(SILInstruction *Earlier) {
// Since MovePos is an instruction, we know that there is always a valid
// iterator after it.
auto Later = std::next(SILBasicBlock::iterator(Earlier));
moveBefore(&*Later);
}
void SILInstruction::dropAllReferences() {
MutableArrayRef<Operand> PossiblyDeadOps = getAllOperands();
for (auto OpI = PossiblyDeadOps.begin(),
OpE = PossiblyDeadOps.end(); OpI != OpE; ++OpI) {
OpI->drop();
}
// If we have a function ref inst, we need to especially drop its function
// argument so that it gets a proper ref decrement.
auto *FRI = dyn_cast<FunctionRefInst>(this);
if (!FRI || !FRI->getReferencedFunction())
return;
FRI->dropReferencedFunction();
}
void SILInstruction::replaceAllUsesWithUndef() {
SILModule &Mod = getModule();
while (!use_empty()) {
Operand *Op = *use_begin();
Op->set(SILUndef::get(Op->get().getType(), Mod));
}
}
namespace {
class InstructionDestroyer : public SILVisitor<InstructionDestroyer> {
public:
#define VALUE(CLASS, PARENT) void visit##CLASS(CLASS *I) { I->~CLASS(); }
#include "swift/SIL/SILNodes.def"
};
} // end anonymous namespace
void SILInstruction::destroy(SILInstruction *I) {
InstructionDestroyer().visit(I);
}
namespace {
/// Given a pair of instructions that are already known to have the same kind,
/// type, and operands check any special state in the two instructions that
/// could disrupt equality.
class InstructionIdentityComparer :
public SILInstructionVisitor<InstructionIdentityComparer, bool> {
public:
InstructionIdentityComparer(const SILInstruction *L) : LHS(L) { }
/// Make sure we only process instructions we know how to process.
bool visitValueBase(const ValueBase *RHS) {
return false;
}
bool visitInjectEnumAddrInst(const InjectEnumAddrInst *RHS) {
auto *X = cast<InjectEnumAddrInst>(LHS);
return X->getElement() == RHS->getElement();
}
bool visitDestroyAddrInst(const DestroyAddrInst *RHS) {
return true;
}
bool visitReleaseValueInst(const ReleaseValueInst *RHS) {
return true;
}
bool visitRetainValueInst(const RetainValueInst *RHS) {
return true;
}
bool visitDeallocStackInst(const DeallocStackInst *RHS) {
return true;
}
bool visitAllocStackInst(const AllocStackInst *RHS) {
return true;
}
bool visitDeallocBoxInst(const DeallocBoxInst *RHS) {
return true;
}
bool visitAllocBoxInst(const AllocBoxInst *RHS) {
return true;
}
bool visitDeallocRefInst(const DeallocRefInst *RHS) {
return true;
}
bool visitDeallocPartialRefInst(const DeallocPartialRefInst *RHS) {
return true;
}
bool visitAllocRefInst(const AllocRefInst *RHS) {
return true;
}
bool visitAllocRefDynamicInst(const AllocRefDynamicInst *RHS) {
return true;
}
bool visitProjectValueBufferInst(const ProjectValueBufferInst *RHS) {
auto *X = cast<ProjectValueBufferInst>(LHS);
return X->getValueType() == RHS->getValueType();
}
bool visitProjectBoxInst(const ProjectBoxInst *RHS) {
return true;
}
bool visitStrongReleaseInst(const StrongReleaseInst *RHS) {
return true;
}
bool visitStrongRetainInst(const StrongRetainInst *RHS) {
return true;
}
bool visitStrongRetainUnownedInst(const StrongRetainUnownedInst *RHS) {
return true;
}
bool visitLoadInst(const LoadInst *RHS) {
return true;
}
bool visitStoreInst(const StoreInst *RHS) {
auto *X = cast<StoreInst>(LHS);
return (X->getSrc() == RHS->getSrc() && X->getDest() == RHS->getDest());
}
bool visitFunctionRefInst(const FunctionRefInst *RHS) {
auto *X = cast<FunctionRefInst>(LHS);
return X->getReferencedFunction() == RHS->getReferencedFunction();
}
bool visitGlobalAddrInst(const GlobalAddrInst *RHS) {
auto *X = cast<GlobalAddrInst>(LHS);
return X->getReferencedGlobal() == RHS->getReferencedGlobal();
}
bool visitIntegerLiteralInst(const IntegerLiteralInst *RHS) {
APInt X = cast<IntegerLiteralInst>(LHS)->getValue();
APInt Y = RHS->getValue();
return X.getBitWidth() == Y.getBitWidth() &&
X == Y;
}
bool visitFloatLiteralInst(const FloatLiteralInst *RHS) {
// Avoid floating point comparison issues by doing a bitwise comparison.
APInt X = cast<FloatLiteralInst>(LHS)->getBits();
APInt Y = RHS->getBits();
return X.getBitWidth() == Y.getBitWidth() &&
X == Y;
}
bool visitStringLiteralInst(const StringLiteralInst *RHS) {
auto LHS_ = cast<StringLiteralInst>(LHS);
return LHS_->getEncoding() == RHS->getEncoding()
&& LHS_->getValue().equals(RHS->getValue());
}
bool visitStructInst(const StructInst *RHS) {
// We have already checked the operands. Make sure that the StructDecls
// match up.
StructDecl *S1 = cast<StructInst>(LHS)->getStructDecl();
return S1 == RHS->getStructDecl();
}
bool visitStructExtractInst(const StructExtractInst *RHS) {
// We have already checked that the operands of our struct_extracts
// match. Thus we need to check the field/struct decl which are not
// operands.
auto *X = cast<StructExtractInst>(LHS);
if (X->getStructDecl() != RHS->getStructDecl())
return false;
if (X->getField() != RHS->getField())
return false;
return true;
}
bool visitRefElementAddrInst(RefElementAddrInst *RHS) {
auto *X = cast<RefElementAddrInst>(LHS);
if (X->getField() != RHS->getField())
return false;
if (X->getOperand() != RHS->getOperand())
return false;
return true;
}
bool visitStructElementAddrInst(const StructElementAddrInst *RHS) {
// We have already checked that the operands of our struct_element_addrs
// match. Thus we only need to check the field/struct decl which are not
// operands.
auto *X = cast<StructElementAddrInst>(LHS);
if (X->getStructDecl() != RHS->getStructDecl())
return false;
if (X->getField() != RHS->getField())
return false;
return true;
}
bool visitTupleInst(const TupleInst *RHS) {
// We have already checked the operands. Make sure that the tuple types
// match up.
TupleType *TT1 = cast<TupleInst>(LHS)->getTupleType();
return TT1 == RHS->getTupleType();
}
bool visitTupleExtractInst(const TupleExtractInst *RHS) {
// We have already checked that the operands match. Thus we only need to
// check the field no and tuple type which are not represented as operands.
auto *X = cast<TupleExtractInst>(LHS);
if (X->getTupleType() != RHS->getTupleType())
return false;
if (X->getFieldNo() != RHS->getFieldNo())
return false;
return true;
}
bool visitTupleElementAddrInst(const TupleElementAddrInst *RHS) {
// We have already checked that the operands match. Thus we only need to
// check the field no and tuple type which are not represented as operands.
auto *X = cast<TupleElementAddrInst>(LHS);
if (X->getTupleType() != RHS->getTupleType())
return false;
if (X->getFieldNo() != RHS->getFieldNo())
return false;
return true;
}
bool visitMetatypeInst(const MetatypeInst *RHS) {
// We have already compared the operands/types, so we should have equality
// at this point.
return true;
}
bool visitValueMetatypeInst(const ValueMetatypeInst *RHS) {
// We have already compared the operands/types, so we should have equality
// at this point.
return true;
}
bool visitExistentialMetatypeInst(const ExistentialMetatypeInst *RHS) {
// We have already compared the operands/types, so we should have equality
// at this point.
return true;
}
bool visitIndexRawPointerInst(IndexRawPointerInst *RHS) {
// We have already compared the operands/types, so we should have equality
// at this point.
return true;
}
bool visitIndexAddrInst(IndexAddrInst *RHS) {
// We have already compared the operands/types, so we should have equality
// at this point.
return true;
}
bool visitCondFailInst(CondFailInst *RHS) {
// We have already compared the operands/types, so we should have equality
// at this point.
return true;
}
bool visitApplyInst(ApplyInst *RHS) {
auto *X = cast<ApplyInst>(LHS);
return X->getSubstitutions() == RHS->getSubstitutions();
}
bool visitBuiltinInst(BuiltinInst *RHS) {
auto *X = cast<BuiltinInst>(LHS);
if (X->getName() != RHS->getName())
return false;
return X->getSubstitutions() == RHS->getSubstitutions();
}
bool visitEnumInst(EnumInst *RHS) {
// We already checked operands and types. Only thing we need to check is
// that the element is the same.
auto *X = cast<EnumInst>(LHS);
return X->getElement() == RHS->getElement();
}
bool visitUncheckedEnumDataInst(UncheckedEnumDataInst *RHS) {
// We already checked operands and types. Only thing we need to check is
// that the element is the same.
auto *X = cast<UncheckedEnumDataInst>(LHS);
return X->getElement() == RHS->getElement();
}
bool visitSelectEnumInstBase(const SelectEnumInstBase *RHS) {
// Check that the instructions match cases in the same order.
auto *X = cast<SelectEnumInstBase>(LHS);
if (X->getNumCases() != RHS->getNumCases())
return false;
if (X->hasDefault() != RHS->hasDefault())
return false;
for (unsigned i = 0, e = X->getNumCases(); i < e; ++i) {
if (X->getCase(i).first != RHS->getCase(i).first)
return false;
}
return true;
}
bool visitSelectEnumInst(const SelectEnumInst *RHS) {
return visitSelectEnumInstBase(RHS);
}
bool visitSelectEnumAddrInst(const SelectEnumAddrInst *RHS) {
return visitSelectEnumInstBase(RHS);
}
bool visitSelectValueInst(const SelectValueInst *RHS) {
// Check that the instructions match cases in the same order.
auto *X = cast<SelectValueInst>(LHS);
if (X->getNumCases() != RHS->getNumCases())
return false;
if (X->hasDefault() != RHS->hasDefault())
return false;
for (unsigned i = 0, e = X->getNumCases(); i < e; ++i) {
if (X->getCase(i).first != RHS->getCase(i).first)
return false;
if (X->getCase(i).second != RHS->getCase(i).second)
return false;
}
return true;
}
// Conversion instructions.
// All of these just return true as they have already had their
// operands and types checked
bool visitUncheckedRefCastInst(UncheckedRefCastInst *RHS) {
return true;
}
bool visitUncheckedAddrCastInst(UncheckedAddrCastInst *RHS) {
return true;
}
bool visitUncheckedTrivialBitCastInst(UncheckedTrivialBitCastInst *RHS) {
return true;
}
bool visitUncheckedBitwiseCastInst(UncheckedBitwiseCastInst *RHS) {
return true;
}
bool visitUpcastInst(UpcastInst *RHS) {
return true;
}
bool visitAddressToPointerInst(AddressToPointerInst *RHS) {
return true;
}
bool visitPointerToAddressInst(PointerToAddressInst *RHS) {
return true;
}
bool visitRefToRawPointerInst(RefToRawPointerInst *RHS) {
return true;
}
bool visitRawPointerToRefInst(RawPointerToRefInst *RHS) {
return true;
}
bool visitRefToUnownedInst(RefToUnownedInst *RHS) {
return true;
}
bool visitUnownedToRefInst(UnownedToRefInst *RHS) {
return true;
}
bool visitRefToUnmanagedInst(RefToUnmanagedInst *RHS) {
return true;
}
bool visitUnmanagedToRefInst(UnmanagedToRefInst *RHS) {
return true;
}
bool visitThinToThickFunctionInst(ThinToThickFunctionInst *RHS) {
return true;
}
bool visitThickToObjCMetatypeInst(ThickToObjCMetatypeInst *RHS) {
return true;
}
bool visitObjCToThickMetatypeInst(ObjCToThickMetatypeInst *RHS) {
return true;
}
bool visitConvertFunctionInst(ConvertFunctionInst *RHS) {
return true;
}
bool visitObjCMetatypeToObjectInst(ObjCMetatypeToObjectInst *RHS) {
return true;
}
bool visitObjCExistentialMetatypeToObjectInst(ObjCExistentialMetatypeToObjectInst *RHS) {
return true;
}
bool visitProjectBlockStorageInst(ProjectBlockStorageInst *RHS) {
return true;
}
bool visitIsNonnullInst(IsNonnullInst *RHS) {
return true;
}
bool visitBridgeObjectToRefInst(BridgeObjectToRefInst *X) {
return true;
}
bool visitBridgeObjectToWordInst(BridgeObjectToWordInst *X) {
return true;
}
bool visitRefToBridgeObjectInst(RefToBridgeObjectInst *X) {
return true;
}
bool visitThinFunctionToPointerInst(ThinFunctionToPointerInst *X) {
return true;
}
bool visitPointerToThinFunctionInst(PointerToThinFunctionInst *X) {
return true;
}
bool visitObjCProtocolInst(ObjCProtocolInst *RHS) {
auto *X = cast<ObjCProtocolInst>(LHS);
return X->getProtocol() == RHS->getProtocol();
}
bool visitClassMethodInst(ClassMethodInst *RHS) {
auto *X = cast<ClassMethodInst>(LHS);
return X->getMember() == RHS->getMember() &&
X->getOperand() == RHS->getOperand() &&
X->getType() == RHS->getType();
}
bool visitWitnessMethodInst(const WitnessMethodInst *RHS) {
auto *X = cast<WitnessMethodInst>(LHS);
if (X->isVolatile() != RHS->isVolatile())
return false;
if (X->getMember() != RHS->getMember())
return false;
if (X->getLookupType() != RHS->getLookupType())
return false;
if (X->getConformance() != RHS->getConformance())
return false;
return true;
}
private:
const SILInstruction *LHS;
};
}
bool SILInstruction::hasIdenticalState(const SILInstruction *RHS) const {
SILInstruction *UnconstRHS = const_cast<SILInstruction *>(RHS);
return InstructionIdentityComparer(this).visit(UnconstRHS);
}
namespace {
class AllOperandsAccessor : public SILVisitor<AllOperandsAccessor,
ArrayRef<Operand> > {
public:
#define VALUE(CLASS, PARENT) \
ArrayRef<Operand> visit##CLASS(const CLASS *I) { \
llvm_unreachable("accessing non-instruction " #CLASS); \
}
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
ArrayRef<Operand> visit##CLASS(const CLASS *I) { \
ASSERT_IMPLEMENTS(CLASS, SILInstruction, getAllOperands, \
ArrayRef<Operand>() const); \
return I->getAllOperands(); \
}
#include "swift/SIL/SILNodes.def"
};
class AllOperandsMutableAccessor
: public SILVisitor<AllOperandsMutableAccessor,
MutableArrayRef<Operand> > {
public:
#define VALUE(CLASS, PARENT) \
MutableArrayRef<Operand> visit##CLASS(const CLASS *I) { \
llvm_unreachable("accessing non-instruction " #CLASS); \
}
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
MutableArrayRef<Operand> visit##CLASS(CLASS *I) { \
ASSERT_IMPLEMENTS(CLASS, SILInstruction, getAllOperands, \
MutableArrayRef<Operand>()); \
return I->getAllOperands(); \
}
#include "swift/SIL/SILNodes.def"
};
} // end anonymous namespace
ArrayRef<Operand> SILInstruction::getAllOperands() const {
return AllOperandsAccessor().visit(const_cast<SILInstruction*>(this));
}
MutableArrayRef<Operand> SILInstruction::getAllOperands() {
return AllOperandsMutableAccessor().visit(this);
}
/// getOperandNumber - Return which operand this is in the operand list of the
/// using instruction.
unsigned Operand::getOperandNumber() const {
return this - &cast<SILInstruction>(getUser())->getAllOperands()[0];
}
SILInstruction::MemoryBehavior SILInstruction::getMemoryBehavior() const {
if (auto *BI = dyn_cast<BuiltinInst>(this)) {
// Handle Swift builtin functions.
const BuiltinInfo &BInfo = BI->getBuiltinInfo();
if (BInfo.ID != BuiltinValueKind::None)
return BInfo.isReadNone() ? MemoryBehavior::None
: MemoryBehavior::MayHaveSideEffects;
// Handle LLVM intrinsic functions.
const IntrinsicInfo & IInfo = BI->getIntrinsicInfo();
if (IInfo.ID != llvm::Intrinsic::not_intrinsic) {
// Read-only.
if (IInfo.hasAttribute(llvm::Attribute::ReadOnly) &&
IInfo.hasAttribute(llvm::Attribute::NoUnwind))
return MemoryBehavior::MayRead;
// Read-none?
return IInfo.hasAttribute(llvm::Attribute::ReadNone) &&
IInfo.hasAttribute(llvm::Attribute::NoUnwind)
? MemoryBehavior::None
: MemoryBehavior::MayHaveSideEffects;
}
}
// Handle functions that have an effects attribute.
if (auto *AI = dyn_cast<ApplyInst>(this))
if (auto *F = AI->getCalleeFunction())
return F->getEffectsKind() == EffectsKind::ReadNone
? MemoryBehavior::None
: MemoryBehavior::MayHaveSideEffects;
switch (getKind()) {
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
case ValueKind::CLASS: return MemoryBehavior::MEMBEHAVIOR;
#include "swift/SIL/SILNodes.def"
case ValueKind::SILArgument:
case ValueKind::SILUndef:
llvm_unreachable("Non-instructions are unreachable.");
}
llvm_unreachable("We've just exhausted the switch.");
}
SILInstruction::ReleasingBehavior SILInstruction::getReleasingBehavior() const {
switch (getKind()) {
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
case ValueKind::CLASS: return ReleasingBehavior::RELEASINGBEHAVIOR;
#include "swift/SIL/SILNodes.def"
case ValueKind::SILArgument:
case ValueKind::SILUndef:
llvm_unreachable("Non-instructions are unreachable.");
}
llvm_unreachable("We've just exhausted the switch.");
}
bool SILInstruction::mayHaveSideEffects() const {
// If this instruction traps then it must have side effects.
if (mayTrap())
return true;
MemoryBehavior B = getMemoryBehavior();
return B == MemoryBehavior::MayWrite ||
B == MemoryBehavior::MayReadWrite ||
B == MemoryBehavior::MayHaveSideEffects;
}
bool SILInstruction::mayRelease() const {
if (getReleasingBehavior() ==
SILInstruction::ReleasingBehavior::DoesNotRelease)
return false;
switch (getKind()) {
default:
llvm_unreachable("Unhandled releasing instruction!");
case ValueKind::ApplyInst:
case ValueKind::TryApplyInst:
case ValueKind::DestroyAddrInst:
case ValueKind::StrongReleaseInst:
case ValueKind::UnownedReleaseInst:
case ValueKind::ReleaseValueInst:
return true;
case ValueKind::UnconditionalCheckedCastAddrInst: {
// Failing casts with take_always can release.
auto *Cast = cast<UnconditionalCheckedCastAddrInst>(this);
return Cast->getConsumptionKind() == CastConsumptionKind::TakeAlways;
}
case ValueKind::CheckedCastAddrBranchInst: {
// Failing casts with take_always can release.
auto *Cast = cast<CheckedCastAddrBranchInst>(this);
return Cast->getConsumptionKind() == CastConsumptionKind::TakeAlways;
}
case ValueKind::CopyAddrInst: {
auto *CopyAddr = cast<CopyAddrInst>(this);
// copy_addr without initialization can cause a release.
return CopyAddr->isInitializationOfDest() ==
IsInitialization_t::IsNotInitialization;
}
case ValueKind::BuiltinInst: {
auto *BI = cast<BuiltinInst>(this);
// Builtins without side effects also do not release.
if (!BI->mayHaveSideEffects())
return false;
// If this is a builtin which might have side effect, but its side
// effects do not cause reference counts to be decremented, return false.
if (auto Kind = BI->getBuiltinKind()) {
switch (Kind.getValue()) {
case BuiltinValueKind::CopyArray:
return false;
default:
break;
}
}
if (auto ID = BI->getIntrinsicID()) {
switch (ID.getValue()) {
case llvm::Intrinsic::memcpy:
case llvm::Intrinsic::memmove:
case llvm::Intrinsic::memset:
return false;
default:
break;
}
}
return true;
}
}
}
bool SILInstruction::mayReleaseOrReadRefCount() const {
switch (getKind()) {
case ValueKind::IsUniqueInst:
case ValueKind::IsUniqueOrPinnedInst:
return true;
default:
return mayRelease();
}
}
namespace {
class TrivialCloner : public SILCloner<TrivialCloner> {
friend class SILCloner<TrivialCloner>;
friend class SILVisitor<TrivialCloner>;
SILInstruction *Result = nullptr;
TrivialCloner(SILFunction *F) : SILCloner(*F) {}
public:
static SILInstruction *doIt(SILInstruction *I) {
TrivialCloner TC(I->getFunction());
TC.visit(I);
return TC.Result;
}
void postProcess(SILInstruction *Orig, SILInstruction *Cloned) {
assert(Orig->getFunction() == &getBuilder().getFunction() &&
"cloning between functions is not supported");
Result = Cloned;
SILCloner<TrivialCloner>::postProcess(Orig, Cloned);
}
SILValue remapValue(SILValue Value) {
return Value;
}
SILBasicBlock *remapBasicBlock(SILBasicBlock *BB) { return BB; }
};
}
bool SILInstruction::isAllocatingStack() const {
if (isa<AllocStackInst>(this))
return true;
if (auto *ARI = dyn_cast<AllocRefInst>(this)) {
if (ARI->canAllocOnStack())
return true;
}
return false;
}
bool SILInstruction::isDeallocatingStack() const {
if (isa<DeallocStackInst>(this))
return true;
if (auto *DRI = dyn_cast<DeallocRefInst>(this)) {
if (DRI->canAllocOnStack())
return true;
}
return false;
}
/// Create a new copy of this instruction, which retains all of the operands
/// and other information of this one. If an insertion point is specified,
/// then the new instruction is inserted before the specified point, otherwise
/// the new instruction is returned without a parent.
SILInstruction *SILInstruction::clone(SILInstruction *InsertPt) {
SILInstruction *NewInst = TrivialCloner::doIt(this);
if (NewInst && InsertPt)
InsertPt->getParent()->insert(InsertPt, NewInst);
return NewInst;
}
/// Returns true if the instruction can be duplicated without any special
/// additional handling. It is important to know this information when
/// you perform such optimizations like e.g. jump-threading.
bool SILInstruction::isTriviallyDuplicatable() const {
if (isa<AllocStackInst>(this) || isa<DeallocStackInst>(this)) {
return false;
}
if (isa<OpenExistentialAddrInst>(this) ||
isa<OpenExistentialRefInst>(this) ||
isa<OpenExistentialMetatypeInst>(this)) {
// Don't know how to duplicate these properly yet. Inst.clone() per
// instruction does not work. Because the follow-up instructions need to
// reuse the same archetype uuid which would only work if we used a
// cloner.
return false;
}
if (auto *MI = dyn_cast<MethodInst>(this)) {
// We can't build SSA for method values that lower to objc methods.
if (MI->getMember().isForeign)
return false;
}
return true;
}
bool SILInstruction::mayTrap() const {
switch(getKind()) {
case ValueKind::CondFailInst:
case ValueKind::UnconditionalCheckedCastInst:
case ValueKind::UnconditionalCheckedCastAddrInst:
return true;
default:
return false;
}
}
//===----------------------------------------------------------------------===//
// Utilities
//===----------------------------------------------------------------------===//
#ifndef NDEBUG
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &OS,
SILInstruction::MemoryBehavior B) {
switch (B) {
case SILInstruction::MemoryBehavior::None:
return OS << "None";
case SILInstruction::MemoryBehavior::MayRead:
return OS << "MayRead";
case SILInstruction::MemoryBehavior::MayWrite:
return OS << "MayWrite";
case SILInstruction::MemoryBehavior::MayReadWrite:
return OS << "MayReadWrite";
case SILInstruction::MemoryBehavior::MayHaveSideEffects:
return OS << "MayHaveSideEffects";
}
}
#endif