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
|
//===--- ARCSequenceOpts.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 "arc-sequence-opts"
#include "ARCSequenceOpts.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILVisitor.h"
#include "swift/SILOptimizer/Analysis/ARCAnalysis.h"
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
#include "swift/SILOptimizer/Analysis/EpilogueARCAnalysis.h"
#include "swift/SILOptimizer/Analysis/LoopAnalysis.h"
#include "swift/SILOptimizer/Analysis/LoopRegionAnalysis.h"
#include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
#include "swift/SILOptimizer/Analysis/ProgramTerminationAnalysis.h"
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "swift/SILOptimizer/Utils/LoopUtils.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
using namespace swift;
STATISTIC(NumRefCountOpsRemoved, "Total number of increments removed");
llvm::cl::opt<bool> EnableLoopARC("enable-loop-arc", llvm::cl::init(true));
//===----------------------------------------------------------------------===//
// Code Motion
//===----------------------------------------------------------------------===//
// This routine takes in the ARCMatchingSet \p MatchSet and adds the increments
// and decrements to the delete list.
void ARCPairingContext::optimizeMatchingSet(
ARCMatchingSet &MatchSet,
llvm::SmallVectorImpl<SILInstruction *> &DeadInsts) {
LLVM_DEBUG(llvm::dbgs() << "**** Optimizing Matching Set ****\n");
// Add the old increments to the delete list.
for (SILInstruction *Increment : MatchSet.Increments) {
MadeChange = true;
LLVM_DEBUG(llvm::dbgs() << " Deleting increment: " << *Increment);
DeadInsts.push_back(Increment);
++NumRefCountOpsRemoved;
}
// Add the old decrements to the delete list.
for (SILInstruction *Decrement : MatchSet.Decrements) {
MadeChange = true;
LLVM_DEBUG(llvm::dbgs() << " Deleting decrement: " << *Decrement);
DeadInsts.push_back(Decrement);
++NumRefCountOpsRemoved;
}
}
bool ARCPairingContext::performMatching(
llvm::SmallVectorImpl<SILInstruction *> &DeadInsts) {
bool MatchedPair = false;
LLVM_DEBUG(llvm::dbgs() << "**** Computing ARC Matching Sets for "
<< F.getName() << " ****\n");
/// For each increment that we matched to a decrement, try to match it to a
/// decrement -> increment pair.
for (auto Pair : IncToDecStateMap) {
if (!Pair.has_value())
continue;
SILInstruction *Increment = Pair->first;
if (!Increment)
continue; // blotted
LLVM_DEBUG(llvm::dbgs() << "Constructing Matching Set For: " << *Increment);
ARCMatchingSetBuilder Builder(DecToIncStateMap, IncToDecStateMap, RCIA);
Builder.init(Increment);
if (Builder.matchUpIncDecSetsForPtr()) {
MatchedPair |= Builder.matchedPair();
auto &Set = Builder.getResult();
for (auto *I : Set.Increments)
IncToDecStateMap.erase(I);
for (auto *I : Set.Decrements)
DecToIncStateMap.erase(I);
optimizeMatchingSet(Set, DeadInsts);
}
}
return MatchedPair;
}
//===----------------------------------------------------------------------===//
// Loop ARC
//===----------------------------------------------------------------------===//
void LoopARCPairingContext::runOnLoop(SILLoop *L) {
auto *Region = LRFI->getRegion(L);
if (processRegion(Region, false, false)) {
// We do not recompute for now since we only look at the top function level
// for post dominating releases.
processRegion(Region, true, false);
}
// Now that we have finished processing the loop, summarize the loop.
Evaluator.summarizeLoop(Region);
}
void LoopARCPairingContext::runOnFunction(SILFunction *F) {
if (processRegion(LRFI->getTopLevelRegion(), false, false)) {
// We recompute the final post dom release since we may have moved the final
// post dominated releases.
processRegion(LRFI->getTopLevelRegion(), true, true);
}
}
bool LoopARCPairingContext::processRegion(const LoopRegion *Region,
bool FreezePostDomReleases,
bool RecomputePostDomReleases) {
llvm::SmallVector<SILInstruction *, 8> DeadInsts;
// We have already summarized all subloops of this loop. Now summarize our
// blocks so that we only visit interesting instructions.
Evaluator.summarizeSubregionBlocks(Region);
bool MadeChange = false;
bool NestingDetected = false;
bool MatchedPair = false;
do {
NestingDetected = Evaluator.runOnLoop(Region, FreezePostDomReleases,
RecomputePostDomReleases);
MatchedPair = Context.performMatching(DeadInsts);
if (!DeadInsts.empty()) {
LLVM_DEBUG(llvm::dbgs() << "Removing dead interesting insts!\n");
do {
SILInstruction *I = DeadInsts.pop_back_val();
LLVM_DEBUG(llvm::dbgs() << " " << *I);
Evaluator.removeInterestingInst(I);
I->eraseFromParent();
} while (!DeadInsts.empty());
}
MadeChange |= MatchedPair;
Evaluator.saveMatchingInfo(Region);
Evaluator.clearLoopState(Region);
Context.DecToIncStateMap.clear();
Context.IncToDecStateMap.clear();
Evaluator.clearSetFactory();
// This ensures we only ever recompute post dominating releases on the first
// iteration.
RecomputePostDomReleases = false;
} while (NestingDetected && MatchedPair);
return MadeChange;
}
//===----------------------------------------------------------------------===//
// Non Loop Optimizer
//===----------------------------------------------------------------------===//
static bool
processFunctionWithoutLoopSupport(SILFunction &F, bool FreezePostDomReleases,
AliasAnalysis *AA, PostOrderAnalysis *POTA,
RCIdentityFunctionInfo *RCIA,
EpilogueARCFunctionInfo *EAFI,
ProgramTerminationFunctionInfo *PTFI) {
// GlobalARCOpts seems to be taking up a lot of compile time when running on
// globalinit_func. Since that is not *that* interesting from an ARC
// perspective (i.e. no ref count operations in a loop), disable it on such
// functions temporarily in order to unblock others. This should be removed.
if (F.isGlobalInitOnceFunction())
return false;
LLVM_DEBUG(llvm::dbgs() << "***** Processing " << F.getName() << " *****\n");
bool Changed = false;
BlockARCPairingContext Context(F, AA, POTA, RCIA, EAFI, PTFI);
// Until we do not remove any instructions or have nested increments,
// decrements...
while (true) {
// Compute matching sets of increments, decrements, and their insertion
// points.
//
// We need to blot pointers we remove after processing an individual pointer
// so we don't process pairs after we have paired them up. Thus we pass in a
// lambda that performs the work for us.
bool ShouldRunAgain = Context.run(FreezePostDomReleases);
Changed |= Context.madeChange();
// If we did not remove any instructions or have any nested increments, do
// not perform another iteration.
if (!ShouldRunAgain)
break;
// Otherwise, perform another iteration.
LLVM_DEBUG(llvm::dbgs() << "\n<<< Made a Change! "
"Reprocessing Function! >>>\n");
}
LLVM_DEBUG(llvm::dbgs() << "\n");
// Return true if we moved or deleted any instructions.
return Changed;
}
//===----------------------------------------------------------------------===//
// Loop Optimizer
//===----------------------------------------------------------------------===//
static bool processFunctionWithLoopSupport(
SILFunction &F, AliasAnalysis *AA, PostOrderAnalysis *POTA,
LoopRegionFunctionInfo *LRFI, SILLoopInfo *LI, RCIdentityFunctionInfo *RCFI,
EpilogueARCFunctionInfo *EAFI, ProgramTerminationFunctionInfo *PTFI) {
// GlobalARCOpts seems to be taking up a lot of compile time when running on
// globalinit_func. Since that is not *that* interesting from an ARC
// perspective (i.e. no ref count operations in a loop), disable it on such
// functions temporarily in order to unblock others. This should be removed.
if (F.isGlobalInitOnceFunction())
return false;
LLVM_DEBUG(llvm::dbgs() << "***** Processing " << F.getName() << " *****\n");
LoopARCPairingContext Context(F, AA, LRFI, LI, RCFI, EAFI, PTFI);
return Context.process();
}
//===----------------------------------------------------------------------===//
// Top Level Driver
//===----------------------------------------------------------------------===//
namespace {
class ARCSequenceOpts : public SILFunctionTransform {
/// The entry point to the transformation.
void run() override {
auto *F = getFunction();
// If ARC optimizations are disabled, don't optimize anything and bail.
if (!getOptions().EnableARCOptimizations)
return;
// FIXME: We should support ownership.
if (F->hasOwnership())
return;
if (!EnableLoopARC) {
auto *AA = getAnalysis<AliasAnalysis>(F);
auto *POTA = getAnalysis<PostOrderAnalysis>();
auto *RCFI = getAnalysis<RCIdentityAnalysis>()->get(F);
auto *EAFI = getAnalysis<EpilogueARCAnalysis>()->get(F);
ProgramTerminationFunctionInfo PTFI(F);
if (processFunctionWithoutLoopSupport(*F, false, AA, POTA, RCFI, EAFI, &PTFI)) {
processFunctionWithoutLoopSupport(*F, true, AA, POTA, RCFI, EAFI, &PTFI);
invalidateAnalysis(SILAnalysis::InvalidationKind::CallsAndInstructions);
}
return;
}
auto *LA = getAnalysis<SILLoopAnalysis>();
auto *LI = LA->get(F);
auto *DA = getAnalysis<DominanceAnalysis>();
auto *DI = DA->get(F);
// Canonicalize the loops, invalidating if we need to.
if (canonicalizeAllLoops(DI, LI)) {
// We preserve loop info and the dominator tree.
DA->lockInvalidation();
LA->lockInvalidation();
PM->invalidateAnalysis(F, SILAnalysis::InvalidationKind::FunctionBody);
DA->unlockInvalidation();
LA->unlockInvalidation();
}
auto *AA = getAnalysis<AliasAnalysis>(F);
auto *POTA = getAnalysis<PostOrderAnalysis>();
auto *RCFI = getAnalysis<RCIdentityAnalysis>()->get(F);
auto *EAFI = getAnalysis<EpilogueARCAnalysis>()->get(F);
auto *LRFI = getAnalysis<LoopRegionAnalysis>()->get(F);
ProgramTerminationFunctionInfo PTFI(F);
if (processFunctionWithLoopSupport(*F, AA, POTA, LRFI, LI, RCFI, EAFI, &PTFI)) {
invalidateAnalysis(SILAnalysis::InvalidationKind::CallsAndInstructions);
}
}
};
} // end anonymous namespace
SILTransform *swift::createARCSequenceOpts() {
return new ARCSequenceOpts();
}
|