-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathBasicBlockOptUtils.cpp
401 lines (354 loc) · 13.8 KB
/
BasicBlockOptUtils.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
//===--- BasicBlockOptUtils.cpp - SILOptimizer basic block utilities ------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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/SILOptimizer/Utils/BasicBlockOptUtils.h"
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
using namespace swift;
/// Invoke \p visitor for each reachable block in \p f in worklist order (at
/// least one predecessor has been visited).
bool ReachableBlocks::visit(function_ref<bool(SILBasicBlock *)> visitor) {
// Walk over the CFG, starting at the entry block, until all reachable blocks
// are visited.
SILBasicBlock *entryBB = visited.getFunction()->getEntryBlock();
SmallVector<SILBasicBlock *, 8> worklist = {entryBB};
visited.insert(entryBB);
while (!worklist.empty()) {
SILBasicBlock *bb = worklist.pop_back_val();
if (!visitor(bb))
return false;
for (auto &succ : bb->getSuccessors()) {
if (visited.insert(succ))
worklist.push_back(succ);
}
}
return true;
}
/// Remove all instructions in the body of \p bb in safe manner by using
/// undef.
void swift::clearBlockBody(SILBasicBlock *bb) {
for (SILArgument *arg : bb->getArguments()) {
arg->replaceAllUsesWithUndef();
// To appease the ownership verifier, just set to None.
arg->setOwnershipKind(OwnershipKind::None);
}
// Instructions in the dead block may be used by other dead blocks. Replace
// any uses of them with undef values.
while (!bb->empty()) {
// Grab the last instruction in the bb.
auto *inst = &bb->back();
// Replace any still-remaining uses with undef values and erase.
inst->replaceAllUsesOfAllResultsWithUndef();
inst->eraseFromParent();
}
}
// Handle the mechanical aspects of removing an unreachable block.
void swift::removeDeadBlock(SILBasicBlock *bb) {
// Clear the body of bb.
clearBlockBody(bb);
// Now that the bb is empty, eliminate it.
bb->eraseFromParent();
}
bool swift::removeUnreachableBlocks(SILFunction &f) {
ReachableBlocks reachable(&f);
// Visit all the blocks without doing any extra work.
reachable.visit([](SILBasicBlock *) { return true; });
// Remove the blocks we never reached. Assume the entry block is visited.
// Reachable's visited set contains dangling pointers during this loop.
bool changed = false;
for (auto ii = std::next(f.begin()), end = f.end(); ii != end;) {
auto *bb = &*ii++;
if (!reachable.isVisited(bb)) {
removeDeadBlock(bb);
changed = true;
}
}
return changed;
}
//===----------------------------------------------------------------------===//
// BasicBlock Cloning
//===----------------------------------------------------------------------===//
// Return true if a guaranteed terminator result can be borrowed such that the
// nested borrow scope covers all its uses.
static bool canBorrowGuaranteedResult(SILValue guaranteedResult) {
if (guaranteedResult.getOwnershipKind() != OwnershipKind::Guaranteed) {
// Either this terminator forwards an owned value, or it is some legal
// conversion to a non-guaranteed value. Either way, not interesting.
return true;
}
SmallVector<Operand *, 16> usePoints;
return findInnerTransitiveGuaranteedUses(guaranteedResult, usePoints);
}
bool swift::canCloneTerminator(TermInst *termInst) {
// TODO: this is an awkward way to check for guaranteed terminator results.
for (Operand &oper : termInst->getAllOperands()) {
if (oper.getOperandOwnership() != OperandOwnership::ForwardingBorrow)
continue;
if (!ForwardingOperand(&oper).visitForwardedValues(
[&](SILValue termResult) {
return canBorrowGuaranteedResult(termResult);
})) {
return false;
}
}
return true;
}
/// Given a terminator result, either from the original or the cloned block,
/// update OSSA for any phis created for the result during edge splitting.
void BasicBlockCloner::updateOSSATerminatorResult(SILPhiArgument *termResult) {
assert(termResult->isTerminatorResult() && "precondition");
// If the terminator result is used by a phi, then it is invalid OSSA
// which was created by edge splitting.
for (Operand *termUse : termResult->getUses()) {
if (auto phiOper = PhiOperand(termUse)) {
createBorrowScopeForPhiOperands(phiOper.getValue());
}
}
}
// Cloning does not invalidate ownership lifetime. When it clones values, it
// also either clones the consumes, or creates the necessary phis that consume
// the new values on all paths. However, cloning may create new phis of
// inner guaranteed values. Since phis are reborrows, they are only allowed to
// use BorrowedValues. Therefore, we must create nested borrow scopes for any
// new phis whose arguments aren't BorrowedValues. Note that other newly created
// phis are themselves BorrowedValues, so only one level of nested borrow is
// needed per value, per new phi that the value reaches.
void BasicBlockCloner::updateOSSAAfterCloning() {
SmallVector<SILPhiArgument *, 4> updateSSAPhis;
if (!origBB->getParent()->hasOwnership()) {
updateSSAAfterCloning(updateSSAPhis);
return;
}
// If the original basic block has terminator results, then all phis in the
// exit blocks are new phis that used to be terminator results.
//
// Create nested borrow scopes for terminator results that were converted to
// phis during edge splitting. This is simpler to check before SSA update.
//
// This assumes that the phis introduced by update-SSA below cannot be users
// of the phis that were created in exitBBs during block cloning. Otherwise
// borrowPhiArguments would handle them twice.
auto *termInst = origBB->getTerminator();
// FIXME: cond_br args should not exist in OSSA
if (!isa<BranchInst>(termInst) && !isa<CondBranchInst>(termInst)) {
// Update all of the terminator results.
for (auto *succBB : origBB->getSuccessorBlocks()) {
for (SILArgument *termResult : succBB->getArguments()) {
updateOSSATerminatorResult(cast<SILPhiArgument>(termResult));
}
}
}
// Update SSA form before calling OSSA update utilities to maintain a layering
// of SIL invariants.
updateSSAAfterCloning(updateSSAPhis);
// Create nested borrow scopes for phis created during SSA update.
for (auto *phi : updateSSAPhis) {
createBorrowScopeForPhiOperands(phi);
}
}
void BasicBlockCloner::updateSSAAfterCloning(
SmallVectorImpl<SILPhiArgument *> &newPhis) {
// All instructions should have been checked by canCloneInstruction. But we
// still need to check the arguments.
for (auto arg : origBB->getArguments()) {
if ((needsSSAUpdate |= isUsedOutsideOfBlock(arg))) {
break;
}
}
if (!needsSSAUpdate)
return;
SILSSAUpdater ssaUpdater(&newPhis);
for (auto availValPair : availVals) {
auto inst = availValPair.first;
if (inst->use_empty())
continue;
SILValue newResult(availValPair.second);
SmallVector<UseWrapper, 16> useList;
// Collect the uses of the value.
for (auto *use : inst->getUses())
useList.push_back(UseWrapper(use));
ssaUpdater.initialize(inst->getType(), inst.getOwnershipKind());
ssaUpdater.addAvailableValue(origBB, inst);
ssaUpdater.addAvailableValue(getNewBB(), newResult);
if (useList.empty())
continue;
// Update all the uses.
for (auto useWrapper : useList) {
Operand *use = useWrapper; // unwrap
SILInstruction *user = use->getUser();
assert(user && "Missing user");
// Ignore uses in the same basic block.
if (user->getParent() == origBB)
continue;
ssaUpdater.rewriteUse(*use);
}
}
}
void BasicBlockCloner::sinkAddressProjections() {
// Because the address projections chains will be disjoint (an instruction
// in one chain cannot use the result of an instruction in another chain),
// the order they are sunk does not matter.
InstructionDeleter deleter;
for (auto ii = origBB->begin(), ie = origBB->end(); ii != ie;) {
bool canSink = sinkProj.analyzeAddressProjections(&*ii);
(void)canSink;
assert(canSink && "canCloneInstruction should catch this.");
sinkProj.cloneProjections();
assert((sinkProj.getInBlockDefs().empty() || needsSSAUpdate)
&& "canCloneInstruction should catch this.");
auto nextII = std::next(ii);
deleter.trackIfDead(&*ii);
ii = nextII;
}
deleter.cleanUpDeadInstructions();
}
// Populate 'projections' with the chain of address projections leading
// to and including 'inst'.
//
// Populate 'inBlockDefs' with all the non-address value definitions in
// the block that will be used outside this block after projection sinking.
//
// Return true on success, even if projections is empty.
bool SinkAddressProjections::analyzeAddressProjections(SILInstruction *inst) {
projections.clear();
inBlockDefs.clear();
SILBasicBlock *bb = inst->getParent();
auto pushOperandVal = [&](SILValue def) {
if (def->getParentBlock() != bb)
return true;
if (!def->getType().isAddress()) {
inBlockDefs.insert(def);
return true;
}
if (auto *addressProj = dyn_cast<SingleValueInstruction>(def)) {
if (addressProj->isPure()) {
projections.push_back(addressProj);
return true;
}
}
// Can't handle a multi-value or unclonable address producer.
return false;
};
// Check the given instruction for any address-type results.
for (auto result : inst->getResults()) {
if (!isUsedOutsideOfBlock(result))
continue;
if (!pushOperandVal(result))
return false;
}
// Recurse upward through address projections.
for (unsigned idx = 0; idx < projections.size(); ++idx) {
// Only one address result/operand can be handled per instruction.
if (projections.size() != idx + 1)
return false;
for (SILValue operandVal : projections[idx]->getOperandValues())
if (!pushOperandVal(operandVal))
return false;
}
return true;
}
// Clone the projections gathered by 'analyzeAddressProjections' at
// their use site outside this block.
bool SinkAddressProjections::cloneProjections() {
if (projections.empty())
return false;
SILBasicBlock *bb = projections.front()->getParent();
// Clone projections in last-to-first order.
for (unsigned idx = 0; idx < projections.size(); ++idx) {
auto *oldProj = projections[idx];
assert(oldProj->getParent() == bb);
// Reset transient per-projection sets.
usesToReplace.clear();
firstBlockUse.clear();
// Gather uses.
for (Operand *use : oldProj->getUses()) {
auto *useBB = use->getUser()->getParent();
if (useBB != bb) {
firstBlockUse.try_emplace(useBB, use);
usesToReplace.push_back(use);
}
}
// Replace uses. Uses must be handled in the same order they were discovered
// above.
//
// Avoid cloning a projection multiple times per block. This avoids extra
// projections, but also prevents the removal of DebugValue. If a
// projection's only remaining is DebugValue, then it is deleted along with
// the DebugValue.
for (Operand *use : usesToReplace) {
auto *useBB = use->getUser()->getParent();
auto *firstUse = firstBlockUse.lookup(useBB);
SingleValueInstruction *newProj;
if (use == firstUse)
newProj = cast<SingleValueInstruction>(oldProj->clone(use->getUser()));
else {
newProj = cast<SingleValueInstruction>(firstUse->get());
assert(newProj->getParent() == useBB);
newProj->moveFront(useBB);
}
use->set(newProj);
}
}
return true;
}
bool StaticInitCloner::add(SILInstruction *initVal) {
// Don't schedule an instruction twice for cloning.
if (numOpsToClone.count(initVal) != 0)
return true;
if (auto *funcRef = dyn_cast<FunctionRefInst>(initVal)) {
// We cannot inline non-public functions into functions which are serialized.
if (!getBuilder().isInsertingIntoGlobal() &&
getBuilder().getFunction().isSerialized() &&
!funcRef->getReferencedFunction()->hasValidLinkageForFragileRef()) {
return false;
}
}
ArrayRef<Operand> operands = initVal->getAllOperands();
numOpsToClone[initVal] = operands.size();
if (operands.empty()) {
// It's an instruction without operands, e.g. a literal. It's ready to be
// cloned first.
readyToClone.push_back(initVal);
} else {
// Recursively add all operands.
for (const Operand &operand : operands) {
if (!add(cast<SingleValueInstruction>(operand.get())))
return false;
}
}
return true;
}
SingleValueInstruction *
StaticInitCloner::clone(SingleValueInstruction *initVal) {
assert(numOpsToClone.count(initVal) != 0 && "initVal was not added");
if (!isValueCloned(initVal)) {
// Find the right order to clone: all operands of an instruction must be
// cloned before the instruction itself.
while (!readyToClone.empty()) {
SILInstruction *inst = readyToClone.pop_back_val();
// Clone the instruction into the SILGlobalVariable
visit(inst);
// Check if users of I can now be cloned.
for (SILValue result : inst->getResults()) {
for (Operand *use : result->getUses()) {
SILInstruction *user = use->getUser();
if (numOpsToClone.count(user) != 0 && --numOpsToClone[user] == 0)
readyToClone.push_back(user);
}
}
if (inst == initVal)
break;
}
}
return cast<SingleValueInstruction>(getMappedValue(initVal));
}