-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathSILValue.cpp
598 lines (517 loc) · 19.9 KB
/
SILValue.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
//===--- SILValue.cpp - Implementation for SILValue -----------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/Assertions.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBuiltinVisitor.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILVisitor.h"
#include "swift/SIL/Test.h"
#include "llvm/ADT/StringSwitch.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// Check SILNode Type Properties
//===----------------------------------------------------------------------===//
/// These are just for performance and verification. If one needs to make
/// changes that cause the asserts the fire, please update them. The purpose is
/// to prevent these predicates from changing values by mistake.
//===----------------------------------------------------------------------===//
// Check SILValue Type Properties
//===----------------------------------------------------------------------===//
/// These are just for performance and verification. If one needs to make
/// changes that cause the asserts the fire, please update them. The purpose is
/// to prevent these predicates from changing values by mistake.
static_assert(std::is_standard_layout<SILValue>::value,
"Expected SILValue to be standard layout");
static_assert(sizeof(SILValue) == sizeof(uintptr_t),
"SILValue should be pointer sized");
//===----------------------------------------------------------------------===//
// Utility Methods
//===----------------------------------------------------------------------===//
void ValueBase::replaceAllUsesWith(ValueBase *RHS) {
assert(this != RHS && "Cannot RAUW a value with itself");
while (!use_empty()) {
Operand *Op = *use_begin();
Op->set(RHS);
}
}
void ValueBase::replaceAllUsesWithUndef() {
auto *F = getFunction();
if (!F) {
llvm_unreachable("replaceAllUsesWithUndef can only be used on ValueBase "
"that have access to the parent function.");
}
while (!use_empty()) {
Operand *Op = *use_begin();
Op->set(SILUndef::get(F, Op->get()->getType()));
}
}
void ValueBase::replaceAllTypeDependentUsesWith(ValueBase *RHS) {
SmallVector<Operand *, 4> typeUses(getTypeDependentUses());
for (Operand *use : typeUses) {
use->set(RHS);
}
}
SILInstruction *ValueBase::getDefiningInstruction() {
if (auto *inst = dyn_cast<SingleValueInstruction>(this))
return inst;
if (auto *result = dyn_cast<MultipleValueInstructionResult>(this))
return result->getParent();
return nullptr;
}
SILInstruction *ValueBase::getDefiningInstructionOrTerminator() {
if (auto *inst = dyn_cast<SingleValueInstruction>(this))
return inst;
if (auto *result = dyn_cast<MultipleValueInstructionResult>(this))
return result->getParent();
if (auto *result = SILArgument::isTerminatorResult(this))
return result->getSingleTerminator();
return nullptr;
}
SILInstruction *ValueBase::getDefiningInsertionPoint() {
if (auto *inst = getDefiningInstruction())
return inst;
if (auto *arg = dyn_cast<SILArgument>(this))
return &*arg->getParentBlock()->begin();
return nullptr;
}
SILInstruction *ValueBase::getNextInstruction() {
if (auto *inst = getDefiningInstruction())
return &*std::next(inst->getIterator());
if (auto *arg = dyn_cast<SILArgument>(this))
return &*arg->getParentBlock()->begin();
return nullptr;
}
std::optional<ValueBase::DefiningInstructionResult>
ValueBase::getDefiningInstructionResult() {
if (auto *inst = dyn_cast<SingleValueInstruction>(this))
return DefiningInstructionResult{inst, 0};
if (auto *result = dyn_cast<MultipleValueInstructionResult>(this))
return DefiningInstructionResult{result->getParent(), result->getIndex()};
return std::nullopt;
}
bool SILPhiArgument::isLexical() const {
if (!isPhi())
return false;
// FIXME: Cache this on the node.
// Does there exist an incoming value which is lexical?
//
// Invert the condition to "is every incoming value non-lexical?" in order to
// stop visiting incoming values once one lexical value is
// found--visitTransitiveIncomingPhiOperands stops once false is returned
// from it.
auto isEveryIncomingValueNonLexical =
visitTransitiveIncomingPhiOperands([&](auto *, auto *operand) {
auto value = operand->get();
SILPhiArgument *phi = dyn_cast<SILPhiArgument>(value);
if (phi && phi->isPhi()) {
return true;
}
// If this non-phi incoming value is lexical, then there is one at least
// one lexical value incoming to this phi, to it's lexical.
return !value->isLexical();
});
return !isEveryIncomingValueNonLexical;
}
bool ValueBase::isLexical() const {
if (auto *argument = dyn_cast<SILFunctionArgument>(this))
return argument->getLifetime().isLexical();
auto *phi = dyn_cast<SILPhiArgument>(this);
if (phi && phi->isPhi())
return phi->isLexical();
if (auto *bbi = dyn_cast<BeginBorrowInst>(this))
return bbi->isLexical();
if (auto *mvi = dyn_cast<MoveValueInst>(this))
return mvi->isLexical();
// TODO: This is only a workaround. Optimizations should look through such instructions to
// get the isLexical state, instead of doing it here.
// rdar://143577158
if (auto *eilr = dyn_cast<EndInitLetRefInst>(this))
return eilr->getOperand()->isLexical();
return false;
}
namespace swift::test {
// Arguments:
// - value
// Dumps:
// - value
// - whether it's lexical
static FunctionTest IsLexicalTest("is_lexical", [](auto &function,
auto &arguments,
auto &test) {
auto value = arguments.takeValue();
auto isLexical = value->isLexical();
value->print(llvm::outs());
auto *boolString = isLexical ? "true" : "false";
llvm::outs() << boolString << "\n";
});
} // end namespace swift::test
bool ValueBase::isGuaranteedForwarding() const {
if (getOwnershipKind() != OwnershipKind::Guaranteed) {
return false;
}
// NOTE: canOpcodeForwardInnerGuaranteedValues returns true for transformation
// terminator results.
if (canOpcodeForwardInnerGuaranteedValues(this) ||
isa<SILFunctionArgument>(this)) {
return true;
}
// If not a phi, return false
auto *phi = dyn_cast<SILPhiArgument>(this);
if (!phi || !phi->isPhi()) {
return false;
}
return phi->isGuaranteedForwarding();
}
bool ValueBase::isBeginApplyToken() const {
auto *result = isaResultOf<BeginApplyInst>(this);
if (!result)
return false;
return result->isBeginApplyToken();
}
bool ValueBase::hasDebugTrace() const {
for (auto *op : getUses()) {
if (auto *debugValue = dyn_cast<DebugValueInst>(op->getUser())) {
if (debugValue->hasTrace())
return true;
}
}
return false;
}
bool ValueBase::isFromVarDecl() {
if (auto *mvi = dyn_cast<MoveValueInst>(this)) {
return mvi->isFromVarDecl();
}
if (auto *bbi = dyn_cast<BeginBorrowInst>(this)) {
return bbi->isFromVarDecl();
}
return false;
}
SILBasicBlock *SILNode::getParentBlock() const {
if (auto *Inst = dyn_cast<SILInstruction>(this))
return Inst->getParent();
if (auto *Arg = dyn_cast<SILArgument>(this))
return Arg->getParent();
if (auto *MVR = dyn_cast<MultipleValueInstructionResult>(this)) {
return MVR->getParent()->getParent();
}
if (auto *undef = dyn_cast<SILUndef>(this)) {
// By convention, undefs are considered to be defined at the entry of the function.
return undef->getParent()->getEntryBlock();
}
return nullptr;
}
SILFunction *SILNode::getFunction() const {
if (auto *parentBlock = getParentBlock()) {
// This can return nullptr if the block's parent is a global variable
// initializer.
if (auto *parentFunction = parentBlock->getParent()) {
return parentFunction;
}
}
if (auto *undef = dyn_cast<SILUndef>(this))
return undef->getParent();
if (auto *placeHolder = dyn_cast<PlaceholderValue>(this))
return placeHolder->getParent();
return nullptr;
}
SILModule *SILNode::getModule() const { return &getFunction()->getModule(); }
/// Get a location for this value.
SILLocation SILValue::getLoc() const {
if (auto *instr = Value->getDefiningInstruction())
return instr->getLoc();
if (auto *arg = dyn_cast<SILArgument>(*this)) {
if (arg->getDecl())
return RegularLocation(const_cast<ValueDecl *>(arg->getDecl()));
}
// TODO: bbargs should probably use one of their operand locations.
return Value->getFunction()->getLocation();
}
void SILValue::dump() const {
Value->dump();
}
//===----------------------------------------------------------------------===//
// OwnershipKind
//===----------------------------------------------------------------------===//
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
const OwnershipKind &kind) {
return os << kind.asString();
}
StringRef OwnershipKind::asString() const {
switch (value) {
case OwnershipKind::Any:
return "any";
case OwnershipKind::Unowned:
return "unowned";
case OwnershipKind::Owned:
return "owned";
case OwnershipKind::Guaranteed:
return "guaranteed";
case OwnershipKind::None:
return "none";
}
llvm_unreachable("covered switch");
}
//===----------------------------------------------------------------------===//
// ValueOwnershipKind
//===----------------------------------------------------------------------===//
ValueOwnershipKind::ValueOwnershipKind(const SILFunction &F, SILType Type,
SILArgumentConvention Convention)
: ValueOwnershipKind(F, Type, Convention,
SILModuleConventions(F.getModule())) {}
ValueOwnershipKind::ValueOwnershipKind(const SILFunction &F, SILType Type,
SILArgumentConvention Convention,
SILModuleConventions moduleConventions)
: value(OwnershipKind::Any) {
// Trivial types can be passed using a variety of conventions. They always
// have trivial ownership.
if (Type.isTrivial(F)) {
value = OwnershipKind::None;
return;
}
switch (Convention) {
case SILArgumentConvention::Indirect_In_CXX:
case SILArgumentConvention::Indirect_In_Guaranteed:
value = moduleConventions.isTypeIndirectForIndirectParamConvention(
Type.getASTType())
? OwnershipKind::None
: OwnershipKind::Guaranteed;
break;
case SILArgumentConvention::Indirect_In:
value = moduleConventions.isTypeIndirectForIndirectParamConvention(
Type.getASTType())
? OwnershipKind::None
: OwnershipKind::Owned;
break;
case SILArgumentConvention::Indirect_Inout:
case SILArgumentConvention::Indirect_InoutAliasable:
case SILArgumentConvention::Indirect_Out:
case SILArgumentConvention::Pack_Inout:
case SILArgumentConvention::Pack_Out:
case SILArgumentConvention::Pack_Owned:
case SILArgumentConvention::Pack_Guaranteed:
value = OwnershipKind::None;
return;
case SILArgumentConvention::Direct_Owned:
value = OwnershipKind::Owned;
return;
case SILArgumentConvention::Direct_Unowned:
value = OwnershipKind::Unowned;
return;
case SILArgumentConvention::Direct_Guaranteed:
value = OwnershipKind::Guaranteed;
return;
}
}
StringRef ValueOwnershipKind::asString() const {
return value.asString();
}
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
ValueOwnershipKind kind) {
return os << kind.asString();
}
ValueOwnershipKind::ValueOwnershipKind(StringRef S)
: value(OwnershipKind::Any) {
auto Result = llvm::StringSwitch<std::optional<OwnershipKind::innerty>>(S)
.Case("unowned", OwnershipKind::Unowned)
.Case("owned", OwnershipKind::Owned)
.Case("guaranteed", OwnershipKind::Guaranteed)
.Case("none", OwnershipKind::None)
.Default(std::nullopt);
if (!Result.has_value())
llvm_unreachable("Invalid string representation of ValueOwnershipKind");
value = Result.value();
}
ValueOwnershipKind
ValueOwnershipKind::getProjectedOwnershipKind(const SILFunction &F,
SILType Proj) const {
if (Proj.isTrivial(F))
return OwnershipKind::None;
return *this;
}
#if 0
/// Map a SILValue mnemonic name to its ValueKind.
ValueKind swift::getSILValueKind(StringRef Name) {
#define SINGLE_VALUE_INST(Id, TextualName, Parent, MemoryBehavior, \
ReleasingBehavior) \
if (Name == #TextualName) \
return ValueKind::Id;
#define VALUE(Id, Parent) \
if (Name == #Id) \
return ValueKind::Id;
#include "swift/SIL/SILNodes.def"
#ifdef NDEBUG
llvm::errs()
<< "Unknown SILValue name\n";
abort();
#endif
llvm_unreachable("Unknown SILValue name");
}
/// Map ValueKind to a corresponding mnemonic name.
StringRef swift::getSILValueName(ValueKind Kind) {
switch (Kind) {
#define SINGLE_VALUE_INST(Id, TextualName, Parent, MemoryBehavior, \
ReleasingBehavior) \
case ValueKind::Id: \
return #TextualName;
#define VALUE(Id, Parent) \
case ValueKind::Id: \
return #Id;
#include "swift/SIL/SILNodes.def"
}
}
#endif
//===----------------------------------------------------------------------===//
// UseLifetimeConstraint
//===----------------------------------------------------------------------===//
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
UseLifetimeConstraint constraint) {
switch (constraint) {
case UseLifetimeConstraint::NonLifetimeEnding:
os << "NonLifetimeEnding";
break;
case UseLifetimeConstraint::LifetimeEnding:
os << "LifetimeEnding";
break;
}
return os;
}
//===----------------------------------------------------------------------===//
// Operand
//===----------------------------------------------------------------------===//
void Operand::updateReborrowFlags() {
if (isa<EndBorrowInst>(getUser())) {
swift::updateReborrowFlags(get());
}
}
void Operand::verify() const {
if (isa<BorrowedFromInst>(getUser()) && getOperandNumber() == 0) {
assert(isa<SILArgument>(get()) || isa<SILUndef>(get()));
}
}
SILBasicBlock *Operand::getParentBlock() const {
auto *self = const_cast<Operand *>(this);
return self->getUser()->getParent();
}
SILFunction *Operand::getParentFunction() const {
auto *self = const_cast<Operand *>(this);
return self->getUser()->getFunction();
}
bool Operand::canAcceptKind(ValueOwnershipKind kind,
SILModuleConventions *silConv) const {
auto operandOwnership = getOperandOwnership(silConv);
auto constraint = operandOwnership.getOwnershipConstraint();
if (constraint.satisfiesConstraint(kind)) {
// Constraints aren't precise enough to enforce Unowned value uses.
if (kind == OwnershipKind::Unowned) {
return canAcceptUnownedValue(operandOwnership);
}
return true;
}
return false;
}
bool Operand::satisfiesConstraints(SILModuleConventions *silConv) const {
return canAcceptKind(get()->getOwnershipKind(), silConv);
}
bool Operand::isLifetimeEnding() const {
auto constraint = getOwnershipConstraint();
// If our use lifetime constraint is NonLifetimeEnding, just return false.
if (!constraint.isLifetimeEnding())
return false;
// Otherwise, we may have a lifetime ending use. We consider two cases here:
// the case where our value has OwnershipKind::None and one where it has some
// other OwnershipKind. Note that values with OwnershipKind::None ownership
// can not have their lifetime ended since they are outside of the ownership
// system. Given such a case, if we have such a value we return
// isLifetimeEnding() as false even if the constraint itself has a constraint
// that says a value is LifetimeEnding. If we have a value that has a
// non-OwnershipKind::None ownership then we just return true as expected.
return get()->getOwnershipKind() != OwnershipKind::None;
}
bool Operand::isConsuming() const {
if (!getOwnershipConstraint().isConsuming())
return false;
return get()->getOwnershipKind() != OwnershipKind::None;
}
void Operand::dump() const { print(llvm::dbgs()); }
void Operand::print(llvm::raw_ostream &os) const {
os << "Operand.\n"
"Owner: "
<< *Owner << "Value: " << get() << "Operand Number: " << getOperandNumber()
<< '\n'
<< "Is Type Dependent: " << (isTypeDependent() ? "yes" : "no") << '\n';
}
SILFunction *Operand::getFunction() const {
return getUser()->getFunction();
}
//===----------------------------------------------------------------------===//
// OperandConstraint
//===----------------------------------------------------------------------===//
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
OwnershipConstraint constraint) {
return os << "<Constraint "
"Kind:" << constraint.getPreferredKind()
<< " LifetimeConstraint:" << constraint.getLifetimeConstraint()
<< ">";
}
StringRef OperandOwnership::asString() const {
switch (value) {
case OperandOwnership::NonUse:
return "non-use";
case OperandOwnership::TrivialUse:
return "trivial-use";
case OperandOwnership::InstantaneousUse:
return "instantaneous";
case OperandOwnership::UnownedInstantaneousUse:
return "unowned-instantaneous";
case OperandOwnership::ForwardingUnowned:
return "forwarding-unowned";
case OperandOwnership::PointerEscape:
return "pointer-escape";
case OperandOwnership::BitwiseEscape:
return "bitwise-escape";
case OperandOwnership::Borrow:
return "borrow";
case OperandOwnership::DestroyingConsume:
return "destroying-consume";
case OperandOwnership::ForwardingConsume:
return "forwarding-consume";
case OperandOwnership::InteriorPointer:
return "interior-pointer";
case OperandOwnership::AnyInteriorPointer:
return "any-interior-pointer";
case OperandOwnership::GuaranteedForwarding:
return "guaranteed-forwarding";
case OperandOwnership::EndBorrow:
return "end-borrow";
case OperandOwnership::Reborrow:
return "reborrow";
}
llvm_unreachable("covered switch");
}
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
const OperandOwnership &operandOwnership) {
return os << operandOwnership.asString();
}
//===----------------------------------------------------------------------===//
// PlaceholderValue
//===----------------------------------------------------------------------===//
int PlaceholderValue::numPlaceholderValuesAlive = 0;
PlaceholderValue::PlaceholderValue(SILFunction *fn, SILType type)
: ValueBase(ValueKind::PlaceholderValue, type), parentFunction(fn) {
numPlaceholderValuesAlive++;
}
PlaceholderValue::~PlaceholderValue() {
numPlaceholderValuesAlive--;
}