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 402 403 404 405 406 407 408 409 410 411 412 413
|
//===--- LLVMARCContract.cpp ----------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "swift-arc-contract"
#include "swift/LLVMPasses/Passes.h"
#include "ARCEntryPointBuilder.h"
#include "LLVMARCOpts.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Transforms/Utils/SSAUpdater.h"
using namespace llvm;
using namespace swift;
using swift::SwiftARCContract;
STATISTIC(NumNoopDeleted,
"Number of no-op swift calls eliminated");
STATISTIC(NumRetainReleasesEliminatedByMergingIntoRetainReleaseN,
"Number of retain/release eliminated by merging into "
"retain_n/release_n");
STATISTIC(NumUnknownObjectRetainReleasesEliminatedByMergingIntoRetainReleaseN,
"Number of retain/release eliminated by merging into "
"unknownObjectRetain_n/unknownObjectRelease_n");
STATISTIC(NumBridgeRetainReleasesEliminatedByMergingIntoRetainReleaseN,
"Number of bridge retain/release eliminated by merging into "
"bridgeRetain_n/bridgeRelease_n");
/// Pimpl implementation of SwiftARCContractPass.
namespace {
struct LocalState {
TinyPtrVector<CallInst *> RetainList;
TinyPtrVector<CallInst *> ReleaseList;
TinyPtrVector<CallInst *> UnknownObjectRetainList;
TinyPtrVector<CallInst *> UnknownObjectReleaseList;
TinyPtrVector<CallInst *> BridgeRetainList;
TinyPtrVector<CallInst *> BridgeReleaseList;
};
/// This implements the very late (just before code generation) lowering
/// processes that we do to expose low level performance optimizations and take
/// advantage of special features of the ABI. These expansion steps can foil
/// the general mid-level optimizer, so they are done very, very, late.
///
/// Optimizations include:
///
/// - Merging together retain and release calls into retain_n, release_n
/// - calls.
///
/// Coming into this function, we assume that the code is in canonical form:
/// none of these calls have any uses of their return values.
class SwiftARCContractImpl {
/// Was a change made while running the optimization.
bool Changed;
/// Swift RC Identity.
SwiftRCIdentity RC;
/// The function that we are processing.
Function &F;
/// The entry point builder that is used to construct ARC entry points.
ARCEntryPointBuilder B;
public:
SwiftARCContractImpl(Function &InF) : Changed(false), F(InF), B(F) {}
// The top level run routine of the pass.
bool run();
private:
/// Perform the RRN Optimization given the current state that we are
/// tracking. This is called at the end of BBs and if we run into an unknown
/// call.
void
performRRNOptimization(DenseMap<Value *, LocalState> &PtrToLocalStateMap);
};
} // end anonymous namespace
// FIXME: This method is pretty long since it is actually several smaller
// optimizations that have been copied/pasted over time. This should be split
// into those smaller (currently inline) functions.
void SwiftARCContractImpl::
performRRNOptimization(DenseMap<Value *, LocalState> &PtrToLocalStateMap) {
// Go through all of our pointers and merge all of the retains with the
// first retain we saw and all of the releases with the last release we saw.
llvm::Value *O = nullptr;
for (auto &P : PtrToLocalStateMap) {
auto &RetainList = P.second.RetainList;
if (RetainList.size() > 1) {
// Create the retainN call right by the first retain.
B.setInsertPoint(RetainList[0]);
O = RetainList[0]->getArgOperand(0);
auto *RI = RetainList[0];
for (auto R : RetainList) {
if (B.isAtomic(R)) {
RI = R;
break;
}
}
B.createRetainN(RC.getSwiftRCIdentityRoot(O), RetainList.size(), RI);
// Replace all uses of the retain instructions with our new retainN and
// then delete them.
for (auto *Inst : RetainList) {
Inst->eraseFromParent();
++NumRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
--NumRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
RetainList.clear();
auto &ReleaseList = P.second.ReleaseList;
if (ReleaseList.size() > 1) {
// Create the releaseN call right by the last release.
auto *OldCI = ReleaseList[ReleaseList.size() - 1];
B.setInsertPoint(OldCI);
O = OldCI->getArgOperand(0);
auto *RI = OldCI;
for (auto R : ReleaseList) {
if (B.isAtomic(R)) {
RI = R;
break;
}
}
B.createReleaseN(RC.getSwiftRCIdentityRoot(O), ReleaseList.size(), RI);
// Remove all old release instructions.
for (auto *Inst : ReleaseList) {
Inst->eraseFromParent();
++NumRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
--NumRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
ReleaseList.clear();
auto &UnknownObjectRetainList = P.second.UnknownObjectRetainList;
if (UnknownObjectRetainList.size() > 1) {
// Create the retainN call right by the first retain.
B.setInsertPoint(UnknownObjectRetainList[0]);
O = UnknownObjectRetainList[0]->getArgOperand(0);
auto *RI = UnknownObjectRetainList[0];
for (auto R : UnknownObjectRetainList) {
if (B.isAtomic(R)) {
RI = R;
break;
}
}
B.createUnknownObjectRetainN(RC.getSwiftRCIdentityRoot(O),
UnknownObjectRetainList.size(), RI);
// Replace all uses of the retain instructions with our new retainN and
// then delete them.
for (auto *Inst : UnknownObjectRetainList) {
Inst->eraseFromParent();
++NumUnknownObjectRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
--NumUnknownObjectRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
UnknownObjectRetainList.clear();
auto &UnknownObjectReleaseList = P.second.UnknownObjectReleaseList;
if (UnknownObjectReleaseList.size() > 1) {
// Create the releaseN call right by the last release.
auto *OldCI =
UnknownObjectReleaseList[UnknownObjectReleaseList.size() - 1];
B.setInsertPoint(OldCI);
O = OldCI->getArgOperand(0);
auto *RI = OldCI;
for (auto R : UnknownObjectReleaseList) {
if (B.isAtomic(R)) {
RI = R;
break;
}
}
B.createUnknownObjectReleaseN(RC.getSwiftRCIdentityRoot(O),
UnknownObjectReleaseList.size(), RI);
// Remove all old release instructions.
for (auto *Inst : UnknownObjectReleaseList) {
Inst->eraseFromParent();
++NumUnknownObjectRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
--NumUnknownObjectRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
UnknownObjectReleaseList.clear();
auto &BridgeRetainList = P.second.BridgeRetainList;
if (BridgeRetainList.size() > 1) {
// Create the releaseN call right by the first retain.
auto *OldCI = BridgeRetainList[0];
B.setInsertPoint(OldCI);
O = OldCI->getArgOperand(0);
auto *RI = OldCI;
for (auto R : BridgeRetainList) {
if (B.isAtomic(R)) {
RI = R;
break;
}
}
// Bridge retain may modify the input reference before forwarding it.
auto *I = B.createBridgeRetainN(RC.getSwiftRCIdentityRoot(O),
BridgeRetainList.size(), RI);
// Remove all old retain instructions.
for (auto *Inst : BridgeRetainList) {
// We may need to perform a pointer cast here to ensure that the output
// type of the retainN matches the output type. This can come up in
// cases where types have been obfuscated in some way. In such a case,
// we need the inert point to be at the retain location.
B.setInsertPoint(Inst);
Inst->replaceAllUsesWith(B.maybeCast(I, Inst->getType()));
Inst->eraseFromParent();
++NumBridgeRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
--NumBridgeRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
BridgeRetainList.clear();
auto &BridgeReleaseList = P.second.BridgeReleaseList;
if (BridgeReleaseList.size() > 1) {
// Create the releaseN call right by the last release.
auto *OldCI = BridgeReleaseList[BridgeReleaseList.size() - 1];
B.setInsertPoint(OldCI);
O = OldCI->getArgOperand(0);
auto *RI = OldCI;
for (auto R : BridgeReleaseList) {
if (B.isAtomic(R)) {
RI = R;
break;
}
}
B.createBridgeReleaseN(RC.getSwiftRCIdentityRoot(O),
BridgeReleaseList.size(), RI);
// Remove all old release instructions.
for (auto *Inst : BridgeReleaseList) {
Inst->eraseFromParent();
++NumBridgeRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
--NumBridgeRetainReleasesEliminatedByMergingIntoRetainReleaseN;
}
BridgeReleaseList.clear();
}
}
bool SwiftARCContractImpl::run() {
// intra-BB retain/release merging.
DenseMap<Value *, LocalState> PtrToLocalStateMap;
for (BasicBlock &BB : F) {
for (auto II = BB.begin(), IE = BB.end(); II != IE; ) {
// Preincrement iterator to avoid iteration issues in the loop.
Instruction &Inst = *II++;
auto Kind = classifyInstruction(Inst);
switch (Kind) {
// These instructions should not reach here based on the pass ordering.
// i.e. LLVMARCOpt -> LLVMContractOpt.
case RT_RetainN:
case RT_UnknownObjectRetainN:
case RT_BridgeRetainN:
case RT_ReleaseN:
case RT_UnknownObjectReleaseN:
case RT_BridgeReleaseN:
llvm_unreachable("These are only created by LLVMARCContract !");
// Delete all fix lifetime and end borrow instructions. After llvm-ir they
// have no use and show up as calls in the final binary.
case RT_FixLifetime:
case RT_EndBorrow:
Inst.eraseFromParent();
++NumNoopDeleted;
continue;
case RT_Retain: {
auto *CI = cast<CallInst>(&Inst);
auto *ArgVal = RC.getSwiftRCIdentityRoot(CI->getArgOperand(0));
LocalState &LocalEntry = PtrToLocalStateMap[ArgVal];
LocalEntry.RetainList.push_back(CI);
continue;
}
case RT_UnknownObjectRetain: {
auto *CI = cast<CallInst>(&Inst);
auto *ArgVal = RC.getSwiftRCIdentityRoot(CI->getArgOperand(0));
LocalState &LocalEntry = PtrToLocalStateMap[ArgVal];
LocalEntry.UnknownObjectRetainList.push_back(CI);
continue;
}
case RT_Release: {
// Stash any releases that we see.
auto *CI = cast<CallInst>(&Inst);
auto *ArgVal = RC.getSwiftRCIdentityRoot(CI->getArgOperand(0));
LocalState &LocalEntry = PtrToLocalStateMap[ArgVal];
LocalEntry.ReleaseList.push_back(CI);
continue;
}
case RT_UnknownObjectRelease: {
// Stash any releases that we see.
auto *CI = cast<CallInst>(&Inst);
auto *ArgVal = RC.getSwiftRCIdentityRoot(CI->getArgOperand(0));
LocalState &LocalEntry = PtrToLocalStateMap[ArgVal];
LocalEntry.UnknownObjectReleaseList.push_back(CI);
continue;
}
case RT_BridgeRetain: {
auto *CI = cast<CallInst>(&Inst);
auto *ArgVal = RC.getSwiftRCIdentityRoot(CI->getArgOperand(0));
LocalState &LocalEntry = PtrToLocalStateMap[ArgVal];
LocalEntry.BridgeRetainList.push_back(CI);
continue;
}
case RT_BridgeRelease: {
auto *CI = cast<CallInst>(&Inst);
auto *ArgVal = RC.getSwiftRCIdentityRoot(CI->getArgOperand(0));
LocalState &LocalEntry = PtrToLocalStateMap[ArgVal];
LocalEntry.BridgeReleaseList.push_back(CI);
continue;
}
case RT_Unknown:
case RT_AllocObject:
case RT_NoMemoryAccessed:
case RT_RetainUnowned:
case RT_CheckUnowned:
case RT_ObjCRelease:
case RT_ObjCRetain:
break;
}
if (Kind != RT_Unknown)
continue;
// If we have an unknown call, we need to create any retainN calls we
// have seen. The reason why is that we do not want to move retains,
// releases over isUniquelyReferenced calls. Specifically imagine this:
//
// retain(x); unknown(x); release(x); isUniquelyReferenced(x); retain(x);
//
// In this case we would with this optimization merge the last retain
// with the first. This would then create an additional copy. The
// release side of this is:
//
// retain(x); unknown(x); release(x); isUniquelyReferenced(x); release(x);
//
// Again in such a case by merging the first release with the second
// release, we would be introducing an additional copy.
//
// Thus if we see an unknown call we merge together all retains and
// releases before. This could be made more aggressive through
// appropriate alias analysis and usage of LLVM's function attributes to
// determine that a function does not touch globals.
performRRNOptimization(PtrToLocalStateMap);
}
// Perform the RRNOptimization.
performRRNOptimization(PtrToLocalStateMap);
PtrToLocalStateMap.clear();
}
return Changed;
}
bool SwiftARCContract::runOnFunction(Function &F) {
return SwiftARCContractImpl(F).run();
}
char SwiftARCContract::ID = 0;
INITIALIZE_PASS_BEGIN(SwiftARCContract, "swift-arc-contract",
"Swift ARC contraction", false, false)
INITIALIZE_PASS_END(SwiftARCContract,
"swift-arc-contract", "Swift ARC contraction",
false, false)
llvm::FunctionPass *swift::createSwiftARCContractPass() {
initializeSwiftARCContractPass(*llvm::PassRegistry::getPassRegistry());
return new SwiftARCContract();
}
void SwiftARCContract::getAnalysisUsage(llvm::AnalysisUsage &AU) const {
AU.setPreservesCFG();
}
llvm::PreservedAnalyses
SwiftARCContractPass::run(llvm::Function &F,
llvm::FunctionAnalysisManager &AM) {
bool changed = SwiftARCContractImpl(F).run();
if (!changed)
return PreservedAnalyses::all();
PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
return PA;
}
|