forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SILValue.cpp
253 lines (213 loc) · 6.97 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
//===--- SILValue.cpp - Implementation for SILValue -----------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/Dominance.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBasicBlock.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// 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 SILValue::replaceAllUsesWith(SILValue V) {
assert(*this != V && "Cannot RAUW a value with itself");
assert(getType() == V.getType() && "Invalid type");
while (!use_empty())
(**use_begin()).set(V);
}
static bool isRCIdentityPreservingCast(ValueKind Kind) {
switch (Kind) {
case ValueKind::UpcastInst:
case ValueKind::UncheckedRefCastInst:
case ValueKind::UncheckedRefCastAddrInst:
case ValueKind::UnconditionalCheckedCastInst:
case ValueKind::RefToBridgeObjectInst:
case ValueKind::BridgeObjectToRefInst:
return true;
default:
return false;
}
}
/// Return the underlying SILValue after stripping off identity SILArguments if
/// we belong to a BB with one predecessor.
static SILValue stripSinglePredecessorArgs(SILValue V) {
while (true) {
auto *A = dyn_cast<SILArgument>(V);
if (!A)
return V;
SILBasicBlock *BB = A->getParent();
// First try and grab the single predecessor of our parent BB. If we don't
// have one, bail.
SILBasicBlock *Pred = BB->getSinglePredecessor();
if (!Pred)
return V;
// Then grab the terminator of Pred...
TermInst *PredTI = Pred->getTerminator();
// And attempt to find our matching argument.
if (auto *BI = dyn_cast<BranchInst>(PredTI)) {
V = BI->getArg(A->getIndex());
continue;
}
if (auto *CBI = dyn_cast<CondBranchInst>(PredTI)) {
if (SILValue Arg = CBI->getArgForDestBB(BB, A)) {
V = Arg;
continue;
}
}
return V;
}
}
SILValue SILValue::stripCasts() {
SILValue V = *this;
while (true) {
V = stripSinglePredecessorArgs(V);
auto K = V->getKind();
if (isRCIdentityPreservingCast(K)
|| K == ValueKind::UncheckedTrivialBitCastInst
|| K == ValueKind::MarkDependenceInst) {
V = cast<SILInstruction>(V.getDef())->getOperand(0);
continue;
}
return V;
}
}
SILValue SILValue::stripUpCasts() {
assert(getType().isClassOrClassMetatype() &&
"Expected class or class metatype!");
SILValue V = stripSinglePredecessorArgs(*this);
while (isa<UpcastInst>(V))
V = stripSinglePredecessorArgs(cast<UpcastInst>(V)->getOperand());
return V;
}
SILValue SILValue::stripClassCasts() {
SILValue V = *this;
while (true) {
if (auto *UI = dyn_cast<UpcastInst>(V)) {
V = UI->getOperand();
continue;
}
if (auto *UCCI = dyn_cast<UnconditionalCheckedCastInst>(V)) {
V = UCCI->getOperand();
continue;
}
return V;
}
}
SILValue SILValue::stripAddressProjections() {
SILValue V = *this;
while (true) {
V = stripSinglePredecessorArgs(V);
switch (V->getKind()) {
case ValueKind::StructElementAddrInst:
case ValueKind::TupleElementAddrInst:
case ValueKind::RefElementAddrInst:
case ValueKind::UncheckedTakeEnumDataAddrInst:
V = cast<SILInstruction>(V.getDef())->getOperand(0);
continue;
default:
return V;
}
}
}
SILValue SILValue::stripValueProjections() {
SILValue V = *this;
while (true) {
V = stripSinglePredecessorArgs(V);
switch (V->getKind()) {
case ValueKind::StructExtractInst:
case ValueKind::TupleExtractInst:
V = cast<SILInstruction>(V.getDef())->getOperand(0);
continue;
default:
return V;
}
}
}
SILValue SILValue::stripIndexingInsts() {
SILValue V = *this;
while (true) {
if (!isa<IndexingInst>(V.getDef()))
return V;
V = cast<IndexingInst>(V)->getBase();
}
}
SILValue SILValue::stripExpectIntrinsic() {
SILValue V = *this;
auto *BI = dyn_cast<BuiltinInst>(V);
if (!BI)
return V;
if (BI->getIntrinsicInfo().ID != llvm::Intrinsic::expect)
return V;
return BI->getArguments()[0];
}
SILBasicBlock *ValueBase::getParentBB() {
if (auto Inst = dyn_cast<SILInstruction>(this))
return Inst->getParent();
if (auto Arg = dyn_cast<SILArgument>(this))
return Arg->getParent();
return nullptr;
}
void Operand::hoistAddressProjections(SILInstruction *InsertBefore,
DominanceInfo *DomTree) {
SILValue V = get();
SILInstruction *Prev = nullptr;
auto *InsertPt = InsertBefore;
while (true) {
SILValue Incoming = stripSinglePredecessorArgs(V);
// Forward the incoming arg from a single predeccessor.
if (V != Incoming) {
if (V == get()) {
// If we are the operand itself set the operand to the incoming
// arugment.
set(Incoming);
V = Incoming;
} else {
// Otherwise, set the previous projections operand to the incoming
// argument.
assert(Prev && "Must have seen a projection");
Prev->setOperand(0, Incoming);
V = Incoming;
}
}
switch (V->getKind()) {
case ValueKind::StructElementAddrInst:
case ValueKind::TupleElementAddrInst:
case ValueKind::RefElementAddrInst:
case ValueKind::UncheckedTakeEnumDataAddrInst: {
auto *Inst = cast<SILInstruction>(V);
// We are done once the current projection dominates the insert point.
if (DomTree->dominates(Inst->getParent(), InsertBefore->getParent()))
return;
// Move the current projection and memorize it for the next iteration.
Prev = Inst;
Inst->moveBefore(InsertPt);
InsertPt = Inst;
V = Inst->getOperand(0);
continue;
}
default:
assert(DomTree->dominates(V->getParentBB(), InsertBefore->getParent()) &&
"The projected value must dominate the insertion point");
return;
}
}
}