forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSILValue.cpp
288 lines (249 loc) · 9.93 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
//===--- 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/SIL/SILValue.h"
#include "ValueOwnershipKindClassifier.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 "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() {
SILModule *Mod = getModule();
if (!Mod) {
llvm_unreachable("replaceAllUsesWithUndef can only be used on ValueBase "
"that have access to the parent module.");
}
while (!use_empty()) {
Operand *Op = *use_begin();
Op->set(SILUndef::get(Op->get()->getType(), Mod));
}
}
SILInstruction *ValueBase::getDefiningInstruction() {
if (auto *inst = dyn_cast<SingleValueInstruction>(this))
return inst;
if (auto *result = dyn_cast<MultipleValueInstructionResult>(this))
return result->getParent();
return nullptr;
}
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 None;
}
SILBasicBlock *SILNode::getParentBlock() const {
auto *CanonicalNode =
const_cast<SILNode *>(this)->getRepresentativeSILNodeInObject();
if (auto *Inst = dyn_cast<SILInstruction>(CanonicalNode))
return Inst->getParent();
if (auto *Arg = dyn_cast<SILArgument>(CanonicalNode))
return Arg->getParent();
return nullptr;
}
SILFunction *SILNode::getFunction() const {
auto *CanonicalNode =
const_cast<SILNode *>(this)->getRepresentativeSILNodeInObject();
if (auto *Inst = dyn_cast<SILInstruction>(CanonicalNode))
return Inst->getFunction();
if (auto *Arg = dyn_cast<SILArgument>(CanonicalNode))
return Arg->getFunction();
return nullptr;
}
SILModule *SILNode::getModule() const {
auto *CanonicalNode =
const_cast<SILNode *>(this)->getRepresentativeSILNodeInObject();
if (auto *Inst = dyn_cast<SILInstruction>(CanonicalNode))
return &Inst->getModule();
if (auto *Arg = dyn_cast<SILArgument>(CanonicalNode))
return &Arg->getModule();
return nullptr;
}
const SILNode *SILNode::getRepresentativeSILNodeSlowPath() const {
assert(getStorageLoc() != SILNodeStorageLocation::Instruction);
if (isa<SingleValueInstruction>(this)) {
assert(hasMultipleSILNodeBases(getKind()));
return &static_cast<const SILInstruction &>(
static_cast<const SingleValueInstruction &>(
static_cast<const ValueBase &>(*this)));
}
if (auto *MVR = dyn_cast<MultipleValueInstructionResult>(this)) {
return MVR->getParent();
}
llvm_unreachable("Invalid value for slow path");
}
/// 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();
}
//===----------------------------------------------------------------------===//
// ValueOwnershipKind
//===----------------------------------------------------------------------===//
ValueOwnershipKind::ValueOwnershipKind(SILModule &M, SILType Type,
SILArgumentConvention Convention)
: Value() {
// Trivial types can be passed using a variety of conventions. They always
// have trivial ownership.
if (Type.isTrivial(M)) {
Value = ValueOwnershipKind::Trivial;
return;
}
switch (Convention) {
case SILArgumentConvention::Indirect_In:
case SILArgumentConvention::Indirect_In_Constant:
Value = SILModuleConventions(M).useLoweredAddresses()
? ValueOwnershipKind::Trivial
: ValueOwnershipKind::Owned;
break;
case SILArgumentConvention::Indirect_In_Guaranteed:
Value = SILModuleConventions(M).useLoweredAddresses()
? ValueOwnershipKind::Trivial
: ValueOwnershipKind::Guaranteed;
break;
case SILArgumentConvention::Indirect_Inout:
case SILArgumentConvention::Indirect_InoutAliasable:
case SILArgumentConvention::Indirect_Out:
Value = ValueOwnershipKind::Trivial;
return;
case SILArgumentConvention::Direct_Owned:
Value = ValueOwnershipKind::Owned;
return;
case SILArgumentConvention::Direct_Unowned:
Value = ValueOwnershipKind::Unowned;
return;
case SILArgumentConvention::Direct_Guaranteed:
Value = ValueOwnershipKind::Guaranteed;
return;
case SILArgumentConvention::Direct_Deallocating:
llvm_unreachable("Not handled");
}
}
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
ValueOwnershipKind Kind) {
switch (Kind) {
case ValueOwnershipKind::Trivial:
return os << "trivial";
case ValueOwnershipKind::Unowned:
return os << "unowned";
case ValueOwnershipKind::Owned:
return os << "owned";
case ValueOwnershipKind::Guaranteed:
return os << "guaranteed";
case ValueOwnershipKind::Any:
return os << "any";
}
llvm_unreachable("Unhandled ValueOwnershipKind in switch.");
}
Optional<ValueOwnershipKind>
ValueOwnershipKind::merge(ValueOwnershipKind RHS) const {
auto LHSVal = Value;
auto RHSVal = RHS.Value;
// Any merges with anything.
if (LHSVal == ValueOwnershipKind::Any) {
return ValueOwnershipKind(RHSVal);
}
// Any merges with anything.
if (RHSVal == ValueOwnershipKind::Any) {
return ValueOwnershipKind(LHSVal);
}
return (LHSVal == RHSVal) ? Optional<ValueOwnershipKind>(*this) : None;
}
ValueOwnershipKind::ValueOwnershipKind(StringRef S) {
auto Result = llvm::StringSwitch<Optional<ValueOwnershipKind::innerty>>(S)
.Case("trivial", ValueOwnershipKind::Trivial)
.Case("unowned", ValueOwnershipKind::Unowned)
.Case("owned", ValueOwnershipKind::Owned)
.Case("guaranteed", ValueOwnershipKind::Guaranteed)
.Case("any", ValueOwnershipKind::Any)
.Default(None);
if (!Result.hasValue())
llvm_unreachable("Invalid string representation of ValueOwnershipKind");
Value = Result.getValue();
}
ValueOwnershipKind
ValueOwnershipKind::getProjectedOwnershipKind(SILModule &M,
SILType Proj) const {
if (Proj.isTrivial(M))
return ValueOwnershipKind::Trivial;
return *this;
}
ValueOwnershipKind SILValue::getOwnershipKind() const {
// Once we have multiple return values, this must be changed.
sil::ValueOwnershipKindClassifier Classifier;
return Classifier.visit(const_cast<ValueBase *>(Value));
}
#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