-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathReleaseDevirtualizer.cpp
239 lines (192 loc) · 8.27 KB
/
ReleaseDevirtualizer.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
//===--- ReleaseDevirtualizer.cpp - Devirtualizes release-instructions ----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "release-devirtualizer"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
#include "swift/SIL/SILBuilder.h"
#include "llvm/ADT/Statistic.h"
STATISTIC(NumReleasesDevirtualized, "Number of devirtualized releases");
using namespace swift;
namespace {
/// Devirtualizes release instructions which are known to destruct the object.
/// This means, it replaces a sequence of
/// %x = alloc_ref [stack] $X
/// ...
/// strong_release %x
/// dealloc_ref [stack] %x
/// with
/// %x = alloc_ref [stack] $X
/// ...
/// set_deallocating %x
/// %d = function_ref @dealloc_of_X
/// %a = apply %d(%x)
/// dealloc_ref [stack] %x
///
/// It also works for array buffers, where the allocation/deallocation is done
/// by calls to the swift_bufferAllocateOnStack/swift_bufferDeallocateFromStack
/// functions.
///
/// The optimization is only done for stack promoted objects because they are
/// known to have no associated objects (which are not explicitly released
/// in the deinit method).
class ReleaseDevirtualizer : public SILFunctionTransform {
public:
ReleaseDevirtualizer() {}
private:
/// The entry point to the transformation.
void run() override;
/// Devirtualize releases of array buffers.
bool devirtualizeReleaseOfObject(SILInstruction *ReleaseInst,
DeallocRefInst *DeallocInst);
/// Devirtualize releases of swift objects.
bool devirtualizeReleaseOfBuffer(SILInstruction *ReleaseInst,
ApplyInst *DeallocCall);
/// Replace the release-instruction \p ReleaseInst with an explicit call to
/// the deallocating destructor of \p AllocType for \p object.
bool createDeallocCall(SILType AllocType, SILInstruction *ReleaseInst,
SILValue object);
StringRef getName() override { return "Release Devirtualizer"; }
RCIdentityFunctionInfo *RCIA = nullptr;
};
void ReleaseDevirtualizer::run() {
DEBUG(llvm::dbgs() << "** ReleaseDevirtualizer **\n");
SILFunction *F = getFunction();
RCIA = PM->getAnalysis<RCIdentityAnalysis>()->get(F);
bool Changed = false;
for (SILBasicBlock &BB : *F) {
// The last release_value or strong_release instruction before the
// deallocation.
SILInstruction *LastRelease = nullptr;
for (SILInstruction &I : BB) {
if (LastRelease) {
if (auto *DRI = dyn_cast<DeallocRefInst>(&I)) {
Changed |= devirtualizeReleaseOfObject(LastRelease, DRI);
LastRelease = nullptr;
continue;
}
if (auto *AI = dyn_cast<ApplyInst>(&I)) {
Changed |= devirtualizeReleaseOfBuffer(LastRelease, AI);
LastRelease = nullptr;
continue;
}
}
if (isa<ReleaseValueInst>(&I) ||
isa<StrongReleaseInst>(&I)) {
LastRelease = &I;
} else if (I.mayReleaseOrReadRefCount()) {
LastRelease = nullptr;
}
}
}
if (Changed) {
invalidateAnalysis(SILAnalysis::InvalidationKind::CallsAndInstructions);
}
}
bool ReleaseDevirtualizer::
devirtualizeReleaseOfObject(SILInstruction *ReleaseInst,
DeallocRefInst *DeallocInst) {
DEBUG(llvm::dbgs() << " try to devirtualize " << *ReleaseInst);
// We only do the optimization for stack promoted object, because for these
// we know that they don't have associated objects, which are _not_ released
// by the deinit method.
// This restriction is no problem because only stack promotion result in this
// alloc-release-dealloc pattern.
if (!DeallocInst->canAllocOnStack())
return false;
// Is the dealloc_ref paired with an alloc_ref?
AllocRefInst *ARI = dyn_cast<AllocRefInst>(DeallocInst->getOperand());
if (!ARI)
return false;
// Does the last release really release the allocated object?
SILValue rcRoot = RCIA->getRCIdentityRoot(ReleaseInst->getOperand(0));
if (rcRoot != ARI)
return false;
SILType AllocType = ARI->getType();
return createDeallocCall(AllocType, ReleaseInst, ARI);
}
bool ReleaseDevirtualizer::
devirtualizeReleaseOfBuffer(SILInstruction *ReleaseInst,
ApplyInst *DeallocCall) {
DEBUG(llvm::dbgs() << " try to devirtualize " << *ReleaseInst);
// Is this a deallocation of a buffer?
SILFunction *DeallocFn = DeallocCall->getReferencedFunction();
if (!DeallocFn || DeallocFn->getName() != "swift_bufferDeallocateFromStack")
return false;
// Is the deallocation call paired with an allocation call?
ApplyInst *AllocAI = dyn_cast<ApplyInst>(DeallocCall->getArgument(0));
if (!AllocAI || AllocAI->getNumArguments() < 1)
return false;
SILFunction *AllocFunc = AllocAI->getReferencedFunction();
if (!AllocFunc || AllocFunc->getName() != "swift_bufferAllocateOnStack")
return false;
// Can we find the buffer type which is allocated? It's metatype is passed
// as first argument to the allocation function.
auto *IEMTI = dyn_cast<InitExistentialMetatypeInst>(AllocAI->getArgument(0));
if (!IEMTI)
return false;
SILType MType = IEMTI->getOperand()->getType();
auto *MetaType = MType.getSwiftRValueType()->getAs<AnyMetatypeType>();
if (!MetaType)
return false;
// Is the allocated buffer a class type? This should always be the case.
auto *ClType = MetaType->getInstanceType()->getAs<BoundGenericClassType>();
if (!ClType)
return false;
// Does the last release really release the allocated buffer?
SILValue rcRoot = RCIA->getRCIdentityRoot(ReleaseInst->getOperand(0));
if (rcRoot != AllocAI)
return false;
SILType SILClType = SILType::getPrimitiveObjectType(CanType(ClType));
return createDeallocCall(SILClType, ReleaseInst, AllocAI);
}
bool ReleaseDevirtualizer::createDeallocCall(SILType AllocType,
SILInstruction *ReleaseInst,
SILValue object) {
DEBUG(llvm::dbgs() << " create dealloc call\n");
ClassDecl *Cl = AllocType.getClassOrBoundGenericClass();
assert(Cl && "no class type allocated with alloc_ref");
// Find the destructor of the type.
DestructorDecl *Destructor = Cl->getDestructor();
SILDeclRef DeallocRef(Destructor, SILDeclRef::Kind::Deallocator);
SILModule &M = ReleaseInst->getFunction()->getModule();
SILFunction *Dealloc = M.lookUpFunction(DeallocRef);
if (!Dealloc)
return false;
CanSILFunctionType DeallocType = Dealloc->getLoweredFunctionType();
ArrayRef<Substitution> AllocSubsts = AllocType.gatherAllSubstitutions(M);
assert(!AllocSubsts.empty() == DeallocType->isPolymorphic() &&
"dealloc of generic class is not polymorphic or vice versa");
if (DeallocType->isPolymorphic())
DeallocType = DeallocType->substGenericArgs(M, M.getSwiftModule(),
AllocSubsts);
SILType ReturnType = DeallocType->getSILResult();
SILType DeallocSILType = SILType::getPrimitiveObjectType(DeallocType);
SILBuilder B(ReleaseInst);
if (object->getType() != AllocType)
object = B.createUncheckedRefCast(ReleaseInst->getLoc(), object, AllocType);
// Do what a release would do before calling the deallocator: set the object
// in deallocating state, which means set the RC_DEALLOCATING_FLAG flag.
B.createSetDeallocating(ReleaseInst->getLoc(), object, Atomicity::Atomic);
// Create the call to the destructor with the allocated object as self
// argument.
auto *MI = B.createFunctionRef(ReleaseInst->getLoc(), Dealloc);
B.createApply(ReleaseInst->getLoc(), MI, DeallocSILType, ReturnType,
AllocSubsts, { object }, false);
NumReleasesDevirtualized++;
ReleaseInst->eraseFromParent();
return true;
}
} // end anonymous namespace
SILTransform *swift::createReleaseDevirtualizer() {
return new ReleaseDevirtualizer();
}