-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSink.cpp
169 lines (138 loc) · 5.46 KB
/
Sink.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
//===--- Sink.cpp ----- Code Sinking --------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Many SIL instructions that don't have side effects at the SIL level are
/// lowered to a sequence of LLVM instructions that does have side effects that
/// LLVM can't sink. This pass sinks instructions close to their users.
///
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sink-instructions"
#include "swift/Basic/Assertions.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/Dominance.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILDebugScope.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
#include "swift/SILOptimizer/Analysis/LoopAnalysis.h"
#include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Debug.h"
using namespace swift;
STATISTIC(NumInstrSunk, "Number of instructions sunk");
namespace {
class CodeSinkingPass : public SILFunctionTransform {
public:
CodeSinkingPass() = default;
DominanceInfo *DT;
PostOrderFunctionInfo *PO;
SILLoopInfo *LoopInfo;
/// returns True if were able to sink the instruction \p II
/// closer to it's users.
bool sinkInstruction(SILInstruction *II) {
// We can't sink instructions that may read or write to memory
// or side effects because it can change the semantics of the program.
if (II->mayHaveSideEffects() || II->mayReadOrWriteMemory()) return false;
// Some instructions don't have direct memory side effects but can't be sunk
// because of other reasons.
if (isa<MarkUninitializedInst>(II) ||
isa<MarkFunctionEscapeInst>(II) ||
isa<MarkDependenceInst>(II)) return false;
// We don't sink stack allocations to not destroy the proper nesting of
// stack allocations.
if (II->isAllocatingStack() || II->isDeallocatingStack())
return false;
SILBasicBlock *CurrentBlock = II->getParent();
SILBasicBlock *Dest = nullptr;
unsigned InitialLoopDepth = LoopInfo->getLoopDepth(CurrentBlock);
// TODO: We may want to delete debug instructions to allow us to sink more
// instructions.
for (auto result : II->getResults()) {
for (auto *Operand : result->getUses()) {
SILInstruction *User = Operand->getUser();
// Check if the instruction is already in the user's block.
if (User->getParent() == CurrentBlock) return false;
// Record the block of the first user and move on to
// other users.
if (!Dest) {
Dest = User->getParent();
continue;
}
// Find a location that dominates all users. If we did not find such
// a block or if it is the current block then bail out.
Dest = DT->findNearestCommonDominator(Dest, User->getParent());
if (!Dest || Dest == CurrentBlock)
return false;
}
}
if (!Dest) return false;
// We don't want to sink instructions into loops. Walk up the dom tree
// until we reach the same loop nest level.
while (LoopInfo->getLoopDepth(Dest) != InitialLoopDepth) {
auto Node = DT->getNode(Dest);
assert(Node && "Invalid dom tree");
auto IDom = Node->getIDom();
assert(IDom && "Can't find the idom");
Dest = IDom->getBlock();
if (!Dest || Dest == CurrentBlock)
return false;
}
II->moveBefore(&*Dest->begin());
++NumInstrSunk;
return true;
}
void run() override {
bool Changed = false;
auto *F = getFunction();
DT = PM->getAnalysis<DominanceAnalysis>()->get(F);
PO = getAnalysis<PostOrderAnalysis>()->get(F);
SILLoopAnalysis *LA = PM->getAnalysis<SILLoopAnalysis>();
LoopInfo = LA->get(F);
auto postOrder = PO->getPostOrder();
// Scan the blocks in a post-order direction to make sure that we sink the
// users of each instruction before visiting the instruction itself to allow
// us to scan the function just once.
for (auto &BB : postOrder) {
auto Inst = BB->end();
auto Begin = BB->begin();
// Skip empty blocks.
if (Inst == Begin) continue;
// Point to the first real instruction.
--Inst;
while (true) {
if (Inst == Begin) {
// This is the first instruction in the block. Try to sink it and
// move on to the next block.
Changed |= sinkInstruction(&*Inst);
break;
} else {
// Move the iterator to the next instruction because we may sink the
// current instruction.
SILInstruction *II = &*Inst;
--Inst;
Changed |= sinkInstruction(II);
}
}
}
if (Changed) PM->invalidateAnalysis(F,
SILAnalysis::InvalidationKind::Instructions);
}
};
} // end anonymous namespace
SILTransform *swift::createCodeSinking() {
return new CodeSinkingPass();
}