-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathManagedValue.h
580 lines (492 loc) · 22.7 KB
/
ManagedValue.h
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
//===--- ManagedValue.h - Exploded RValue Representation --------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// A storage structure for holding a destructured rvalue with an optional
// cleanup(s).
// Ownership of the rvalue can be "forwarded" to disable the associated
// cleanup(s).
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_LOWERING_MANAGEDVALUE_H
#define SWIFT_LOWERING_MANAGEDVALUE_H
#include "Cleanup.h"
#include "llvm/ADT/PointerIntPair.h"
#include "swift/Basic/Assertions.h"
#include "swift/SIL/Consumption.h"
#include "swift/SIL/SILValue.h"
namespace swift {
enum class CastConsumptionKind : unsigned char;
namespace Lowering {
class Initialization;
class SILGenFunction;
enum class SGFAccessKind : uint8_t;
/// ManagedValue - represents a singular SIL value and an optional cleanup.
/// Ownership of the ManagedValue can be "forwarded" to disable its cleanup when
/// the rvalue is consumed. A ManagedValue can also represent an LValue used as
/// a value, such as an inout function argument, and can be null.
///
/// Interesting relevant cases include:
/// LValue: the SILValue will always have an isAddress() SILType. LValues
/// never have an associated cleanup.
/// RValue, isAddress() type: an address-only RValue.
/// RValue, !isAddress() type: a loadable RValue.
/// "InContext": Represented with the lvalue flag set but with no SILValue,
/// this represents a value that was emitted directly into an
/// initialization stored by an SGFContext.
///
/// The RValue cases may or may not have a cleanup associated with the value. A
/// cleanup is associated with +1 values of non-trivial type and +0 values of
/// non-trivial type.
///
class ManagedValue {
/// The value (or address of an address-only value) being managed, and
/// whether it represents an lvalue. InContext is represented with the lvalue
/// flag set but with a null SILValue.
llvm::PointerIntPair<SILValue, 1, bool> valueAndFlag;
/// A handle to the cleanup that destroys this value, or
/// CleanupHandle::invalid() if the value has no cleanup.
CleanupHandle cleanup;
explicit ManagedValue(SILValue value, bool isLValue, CleanupHandle cleanup)
: valueAndFlag(value, isLValue), cleanup(cleanup) {
}
/// Create a managed value for a +0 rvalue.
///
/// Please do not introduce new uses of this method! Instead use one of the
/// static constructors below!
static ManagedValue forUnmanaged(SILValue value) {
assert(value && "No value specified");
return ManagedValue(value, false, CleanupHandle::invalid());
}
/// Create a managed value for a +1 rvalue.
///
/// Please do not introduce new uses of this method! Instead use one of the
/// static constructors below.
explicit ManagedValue(SILValue value,
CleanupHandle cleanup = CleanupHandle::invalid())
: valueAndFlag(value, false), cleanup(cleanup) {
assert(value && "No value specified?!");
assert((!getType().isObject() ||
value->getOwnershipKind() != OwnershipKind::None ||
!hasCleanup()) &&
"Objects with trivial ownership should never have a cleanup");
}
public:
/// Constructs an invalid ManagedValue.
ManagedValue() = default;
/// Sometimes SILGen wants to represent an owned value or owned address
/// without a cleanup as a +0 value that must be copied to be consumed.
///
/// Please do not introduce new uses of this.
///
/// DISCUSSION: We purposely provide a specific API for code paths that use
/// owned values (and assert the values are owned) so that users do not
/// attempt to use this for borrowed values. All borrowed values need to use
/// the borrowed value APIs.
static ManagedValue forUnmanagedOwnedValue(SILValue value) {
assert(value);
assert(!value->getType().isObject() ||
value->getOwnershipKind().isCompatibleWith(OwnershipKind::Owned));
return ManagedValue(value);
}
/// Wrap a value with OwnershipKind::Unowned in a ManagedValue. This must be
/// copied before it is used.
static ManagedValue forUnownedObjectValue(SILValue value) {
assert(value);
assert(value->getType().isObject());
assert(value->getOwnershipKind().isCompatibleWith(OwnershipKind::Unowned));
return ManagedValue(value);
}
enum class ScopeKind {
Lexical,
FormalAccess,
};
/// Create a managed value for a SILValue whose ownership is
/// forwarded. Creates a new cleanup for +1 values. Forwarded +0 values
/// require no cleanup.
///
/// Use this for values that do not introduce a new borrow scope. This is
/// correct for casts and terminator results, not for phis.
static ManagedValue forForwardedRValue(SILGenFunction &SGF, SILValue value);
/// Create a managed value for a +1 rvalue object.
static ManagedValue forOwnedObjectRValue(SILValue value,
CleanupHandle cleanup) {
assert(value && "No value specified");
assert(value->getType().isObject() &&
"Expected borrowed rvalues to be objects");
assert(value->getOwnershipKind() != OwnershipKind::None);
return ManagedValue(value, false, cleanup);
}
/// Create a managed value for a +1 rvalue address.
///
/// From a high level perspective, this consists of a temporary buffer.
static ManagedValue forOwnedAddressRValue(SILValue value,
CleanupHandle cleanup) {
assert(value && "No value specified");
assert(value->getType().isAddress() && "Expected value to be an address");
assert(value->getOwnershipKind() == OwnershipKind::None &&
"Addresses always have any ownership");
return ManagedValue(value, false, cleanup);
}
/// Create a managed value for a +1 non-trivial rvalue.
static ManagedValue forOwnedRValue(SILValue value, CleanupHandle cleanup) {
if (value->getType().isAddress())
return ManagedValue::forOwnedAddressRValue(value, cleanup);
return ManagedValue::forOwnedObjectRValue(value, cleanup);
}
static ManagedValue
forExclusivelyBorrowedOwnedObjectRValue(SILValue value,
CleanupHandle cleanup) {
assert(value->getType().isObject());
return ManagedValue::forOwnedObjectRValue(value, cleanup);
}
/// Create a managed value for a +0 borrowed non-trivial rvalue object.
static ManagedValue
forBorrowedObjectRValue(SILValue value) {
assert(value && "No value specified");
assert(value->getType().isObject() &&
"Expected borrowed rvalues to be objects");
if (value->getOwnershipKind() == OwnershipKind::None) {
return forObjectRValueWithoutOwnership(value);
}
assert(value->getOwnershipKind() == OwnershipKind::Guaranteed);
return ManagedValue(value, false, CleanupHandle::invalid());
}
/// Create a managed value for a +0 borrowed non-trivial rvalue address.
static ManagedValue
forBorrowedAddressRValue(SILValue value) {
assert(value && "No value specified");
assert(value->getType().isAddress() && "Expected value to be an address");
// We check for value->getFunction() here since we /could/ be passed
// SILUndef here.
if (auto *f = value->getFunction()) {
if (value->getType().isTrivial(f)) {
return forTrivialAddressRValue(value);
}
}
assert(value->getOwnershipKind() == OwnershipKind::None &&
"Addresses always have trivial ownership");
return ManagedValue(value, false, CleanupHandle::invalid());
}
/// Create a managed value for a +0 guaranteed rvalue.
static ManagedValue
forBorrowedRValue(SILValue value) {
if (value->getType().isAddress())
return ManagedValue::forBorrowedAddressRValue(value);
return ManagedValue::forBorrowedObjectRValue(value);
}
/// Create a managed value for a +0 trivial object rvalue.
static ManagedValue forObjectRValueWithoutOwnership(SILValue value) {
assert(value->getType().isObject() && "Expected an object");
assert(value->getOwnershipKind() == OwnershipKind::None);
return ManagedValue(value, false, CleanupHandle::invalid());
}
/// Create a managed value for a +0 trivial address rvalue.
static ManagedValue forTrivialAddressRValue(SILValue value) {
assert(value->getType().isAddress() && "Expected an address");
assert(value->getOwnershipKind() == OwnershipKind::None);
// TODO: Add an assert that we have a trivial type here.
//
// DISCUSSION: We cannot do this today since we have problems along certain
// materialization paths where we want to emit a borrow operation. To handle
// those cases, we have loosened the rules of OSSA by allowing for store
// [trivial] to take non-trivial .none parameters. This has hidden bugs
// where SILGen emits a borrow to materialize a parameter for an @in
// parameter. It just coincidently works since when we emit the
// store_borrow, we use the store [trivial] instead. This should be fixed.
return ManagedValue(value, false, CleanupHandle::invalid());
}
/// Create a managed value for a trivial address rvalue or an object rvalue
/// that has .none ownership.
static ManagedValue forRValueWithoutOwnership(SILValue value) {
if (value->getType().isObject())
return ManagedValue::forObjectRValueWithoutOwnership(value);
return ManagedValue::forTrivialAddressRValue(value);
}
/// Create a managed value for an l-value.
static ManagedValue forLValue(SILValue value) {
assert(value && "No value specified");
assert(value->getType().isAddress() &&
"lvalues always have isAddress() type");
return ManagedValue(value, true, CleanupHandle::invalid());
}
/// Create a managed value that indicates that the value you're looking for
/// got stored into an initialization specified by an SGFContext, instead of
/// being represented by this ManagedValue.
static ManagedValue forInContext() {
return ManagedValue(SILValue(), true, CleanupHandle::invalid());
}
/// Creates a managed value for an address that is undergoing a formal
/// access. This will be `forLValue` if the `accessKind` is a mutating
/// (exclusive) access or `forBorrowedRValueAddress` if the
/// `accessKind` is borrowing (shared).
static ManagedValue forFormalAccessedAddress(SILValue address,
SGFAccessKind accessKind);
bool isValid() const {
return valueAndFlag.getInt() || valueAndFlag.getPointer();
}
bool isLValue() const {
return valueAndFlag.getInt() && valueAndFlag.getPointer();
}
bool isInContext() const {
return valueAndFlag.getInt() && !valueAndFlag.getPointer();
}
/// Return true if this is an +0 rvalue, or has trivial type.
bool isPlusZeroRValueOrTrivial() const {
// If this is an lvalue or isInContext() then it is not an RValue.
if (isLValue() || isInContext()) return false;
// If this has a cleanup attached, then it is +1 rvalue. If not, it is
// either +0 or trivial (in which case +0 vs +1 doesn't matter).
return !hasCleanup();
}
/// Returns true if this managed value can be consumed.
///
/// This is true if either this value has a cleanup or if it is a trivial
/// object value. For address values, this returns true only if the value has
/// a cleanup regardless of whether the type is trivial.
///
/// When an object value is trivial, it can be passed to a consuming operation
/// without destroying it. Consuming a value by address, however, always
/// deinitializes the memory regardless of whether or not it is trivial.
///
/// Use this before emitting an operation that "takes" this value or passing
/// this value to a call that consumes the argument.
bool isPlusOne(SILGenFunction &SGF) const;
/// Returns true if this managed value can be forwarded without necessarilly
/// destroying the original.
///
/// This is true if either isPlusOne is true or the value is trivial. Unlike
/// isPlusOne(), this returns true for trivial address values regardless of
/// whether the value has a cleanup. A +1 value can be created from a trivial
/// value without consuming the original.
///
/// Use this when storing this value into a new location simply by forwarding
/// the cleanup without destroying the original value. If it's necessary to
/// "take" or otherwise immediately consume the original value, then use
/// isPlusOne() instead.
bool isPlusOneOrTrivial(SILGenFunction &SGF) const;
/// Returns true if this is an ManagedValue that can be used safely as a +0
/// ManagedValue.
///
/// Specifically, we return true if:
///
/// 1. All sub-values are trivially typed.
/// 2. At least 1 subvalue is non-trivial and all such non-trivial values do
/// not have a cleanup.
///
/// *NOTE* Due to 1. isPlusOne and isPlusZero both return true for
/// ManagedValues consisting of only trivial values.
bool isPlusZero() const;
SILValue getLValueAddress() const {
assert(isLValue() && "This isn't an lvalue");
return getValue();
}
SILValue getUnmanagedValue() const {
assert(!hasCleanup());
return getValue();
}
/// Treat getValue() as used so that we can access the value directly when
/// debugging without needing to interpret the flag field.
LLVM_ATTRIBUTE_USED
SILValue getValue() const { return valueAndFlag.getPointer(); }
SILType getType() const { return getValue()->getType(); }
ValueOwnershipKind getOwnershipKind() const {
return getValue()->getOwnershipKind();
}
/// Transform the given ManagedValue, replacing the underlying value, but
/// keeping the same cleanup.
///
/// For owned values, this is equivalent to forwarding the cleanup and
/// creating a new cleanup of the same type on the new value. This is useful
/// for forwarding sequences.
///
/// For all other values, it is a move.
ManagedValue transform(SILValue newValue) && {
assert(getValue()->getOwnershipKind() == newValue->getOwnershipKind() &&
"New value and old value must have the same ownership kind");
ManagedValue M(newValue, isLValue(), getCleanup());
*this = ManagedValue();
return M;
}
/// Emit a copy of this value with independent ownership.
ManagedValue copy(SILGenFunction &SGF, SILLocation loc) const;
/// Returns an unmanaged copy of this value.
/// WARNING: Callers of this API should manage the cleanup of this value!
SILValue unmanagedCopy(SILGenFunction &SGF, SILLocation loc) const;
/// Emit a copy of this value with independent ownership into the current
/// formal evaluation scope.
ManagedValue formalAccessCopy(SILGenFunction &SGF, SILLocation loc);
/// This is the same operation as 'copy', but works on +0 values that don't
/// have cleanups. It returns a +1 value with one.
ManagedValue copyUnmanaged(SILGenFunction &SGF, SILLocation loc);
/// This is the same operation as 'formalAccessCopy', but works on +0 values
/// that don't have cleanups. It returns a +1 value with one.
ManagedValue formalAccessCopyUnmanaged(SILGenFunction &SGF, SILLocation loc);
bool hasCleanup() const { return cleanup.isValid(); }
CleanupHandle getCleanup() const { return cleanup; }
/// Return a "borrowed" version of this value.
///
/// An l-value is borrowed as itself. A +1 r-value is borrowed as a
/// +0 r-value, with the assumption that the original ManagedValue
/// will not be forwarded until the borrowed value is fully used.
ManagedValue borrow(SILGenFunction &SGF, SILLocation loc) const;
/// Return a formally evaluated "borrowed" version of this value.
ManagedValue formalAccessBorrow(SILGenFunction &SGF, SILLocation loc) const;
ManagedValue unmanagedBorrow() const {
return isLValue() ? *this : ManagedValue::forUnmanaged(getValue());
}
/// If this managed value is a plus one value, return *this. If this is a plus
/// zero value, return a copy instead.
ManagedValue ensurePlusOne(SILGenFunction &SGF, SILLocation loc) const;
/// Given a scalar value, materialize it into memory with the
/// exact same level of cleanup it had before.
ManagedValue materialize(SILGenFunction &SGF, SILLocation loc) const;
ManagedValue formallyMaterialize(SILGenFunction &SGF, SILLocation loc) const;
/// Disable the cleanup for this value.
void forwardCleanup(SILGenFunction &SGF) const;
/// Forward this value, deactivating the cleanup and returning the
/// underlying value.
SILValue forward(SILGenFunction &SGF) const;
/// Forward this value into memory by storing it to the given address.
///
/// \param SGF - The SILGenFunction.
/// \param loc - the AST location to associate with emitted instructions.
/// \param address - the address to assign to.
void forwardInto(SILGenFunction &SGF, SILLocation loc, SILValue address);
/// Forward this value into the given initialization.
///
/// \param SGF - The SILGenFunction.
/// \param loc - the AST location to associate with emitted instructions.
/// \param dest - the destination to forward into
void forwardInto(SILGenFunction &SGF, SILLocation loc, Initialization *dest);
/// Assign this value into memory, destroying the existing
/// value at the destination address.
///
/// \param SGF - The SILGenFunction.
/// \param loc - the AST location to associate with emitted instructions.
/// \param address - the address to assign to.
void assignInto(SILGenFunction &SGF, SILLocation loc, SILValue address);
/// Store a copy of this value with independent ownership into the given
/// uninitialized address.
void copyInto(SILGenFunction &SGF, SILLocation loc, SILValue dest);
/// Store a copy of this value with independent ownership into the given
/// initialization \p dest.
void copyInto(SILGenFunction &SGF, SILLocation loc, Initialization *dest);
explicit operator bool() const {
// "InContext" is not considered false.
return bool(getValue()) || valueAndFlag.getInt();
}
SILFunction *getFunction() const {
assert(getValue());
return getValue()->getFunction();
}
void dump() const;
void dump(raw_ostream &os, unsigned indent = 0) const;
void print(raw_ostream &os) const;
};
/// A ManagedValue which may not be intended to be consumed.
///
/// The invariant is that the cleanup on a ManagedValue that's not
/// meant to be consumed should be free to clear.
///
/// Code which gets a ManagedValue from a ConsumableManagedValue
/// must be careful before handing the MV off to an API. Many
/// SILGen APIs expect that an MV is +1, but ConsumableManagedValue
/// often traffics in borrowed values. A value is only +1 if
/// the associated consumption is TakeAlways, but conditional
/// operation should turn TakeOnSuccess consumptions into TakeAlways
/// consumptions on their success path.
class ConsumableManagedValue {
ManagedValue Value;
CastConsumptionKind FinalConsumption;
public:
/// Create an invalid CMV.
ConsumableManagedValue() = default;
/// Create a CMV with a specific value and consumption rule.
/*implicit*/ ConsumableManagedValue(ManagedValue value,
CastConsumptionKind finalConsumption)
: Value(value), FinalConsumption(finalConsumption) {
assert((value.getType().isAddress() ||
finalConsumption != CastConsumptionKind::CopyOnSuccess) &&
"Can not copy on success a value.");
}
/// Create a CMV for a value of trivial type.
static ConsumableManagedValue forUnmanaged(SILValue value) {
return {ManagedValue::forObjectRValueWithoutOwnership(value),
CastConsumptionKind::TakeAlways};
}
/// Create a CMV for an owned value.
static ConsumableManagedValue forOwned(ManagedValue value) {
return { value, CastConsumptionKind::TakeAlways };
}
/// Has this been filled in with meaningful data?
bool isValid() const { return (bool) Value; }
bool isOwned() const {
assert(isValid());
return FinalConsumption == CastConsumptionKind::TakeAlways;
}
/// Return true if there's a cleanup associated with this value.
bool hasCleanup() const { return Value.hasCleanup(); }
CleanupHandle getCleanup() const { return Value.getCleanup(); }
SILType getType() const { return Value.getType(); }
SILValue getValue() const { return Value.getValue(); }
ValueOwnershipKind getOwnershipKind() const {
return Value.getOwnershipKind();
}
/// Return a managed value appropriate for the final use of this CMV.
ManagedValue getFinalManagedValue() const { return Value; }
/// Get the value as an unmanaged ManagedValue.
///
/// You probably should not be using this; it's here to make it easy
/// to find code that is probably wrong.
ManagedValue asUnmanagedOwnedValue() const {
return ManagedValue::forUnmanagedOwnedValue(Value.getValue());
}
/// Return the consumption rules appropriate for the final use of
/// this CMV.
CastConsumptionKind getFinalConsumption() const { return FinalConsumption; }
/// Return a managed value that's appropriate for borrowing this
/// value and promising not to consume it.
///
/// TODO: Should be superseded by `asBorrowedOperand2` once existing code is
/// updated to tolerate address-only values being borrowed.
ConsumableManagedValue asBorrowedOperand(SILGenFunction &SGF,
SILLocation loc) const {
if (getType().isAddress())
return {asUnmanagedOwnedValue(), CastConsumptionKind::CopyOnSuccess};
if (Value.getOwnershipKind() == OwnershipKind::Guaranteed)
return {Value, CastConsumptionKind::BorrowAlways};
return {asUnmanagedOwnedValue().borrow(SGF, loc),
CastConsumptionKind::BorrowAlways};
}
ConsumableManagedValue asBorrowedOperand2(SILGenFunction &SGF,
SILLocation loc) const {
if (getType().isAddress())
return {asUnmanagedOwnedValue(), CastConsumptionKind::BorrowAlways};
if (Value.getOwnershipKind() == OwnershipKind::Guaranteed)
return {Value, CastConsumptionKind::BorrowAlways};
return {asUnmanagedOwnedValue().borrow(SGF, loc),
CastConsumptionKind::BorrowAlways};
}
/// Return a managed value that's appropriate for copying this value and
/// always consuming it.
ConsumableManagedValue copy(SILGenFunction &SGF, SILLocation loc) const {
return ConsumableManagedValue::forOwned(Value.copy(SGF, loc));
}
};
} // namespace Lowering
} // namespace swift
namespace swift {
template <typename To> inline bool isa(const Lowering::ManagedValue &M) {
return isa<To>(M.getValue());
}
} // end namespace swift
#endif