blob: 9be3af2cc3620445bc6fbeb49089c51c54c1ad12 [file] [log] [blame]
//===--- ARCSequenceOpts.h --------------------------------------*- C++ -*-===//
// This source file is part of the 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 for license information
// See for the list of Swift project authors
#include "GlobalARCSequenceDataflow.h"
#include "GlobalLoopARCSequenceDataflow.h"
#include "ARCMatchingSet.h"
#include "swift/SIL/SILValue.h"
#include "swift/SILOptimizer/Utils/LoopUtils.h"
#include "llvm/ADT/SetVector.h"
namespace swift {
class SILInstruction;
class SILFunction;
class AliasAnalysis;
class PostOrderAnalysis;
class LoopRegionFunctionInfo;
class SILLoopInfo;
class RCIdentityFunctionInfo;
struct ARCPairingContext {
SILFunction &F;
BlotMapVector<SILInstruction *, TopDownRefCountState> DecToIncStateMap;
BlotMapVector<SILInstruction *, BottomUpRefCountState> IncToDecStateMap;
RCIdentityFunctionInfo *RCIA;
bool MadeChange = false;
ARCPairingContext(SILFunction &F, RCIdentityFunctionInfo *RCIA)
: F(F), DecToIncStateMap(), IncToDecStateMap(), RCIA(RCIA) {}
bool performMatching(llvm::SmallVectorImpl<SILInstruction *> &DeadInsts);
void optimizeMatchingSet(ARCMatchingSet &MatchSet,
llvm::SmallVectorImpl<SILInstruction *> &DeadInsts);
/// A composition of an ARCSequenceDataflowEvaluator and an
/// ARCPairingContext. The evaluator performs top down/bottom up dataflows
/// clearing the dataflow at loop boundaries. Then the results of the evaluator
/// are placed into the ARCPairingContext and then the ARCPairingContext is used
/// to pair retains/releases.
struct BlockARCPairingContext {
ARCPairingContext Context;
ARCSequenceDataflowEvaluator Evaluator;
BlockARCPairingContext(SILFunction &F, AliasAnalysis *AA,
PostOrderAnalysis *POTA, RCIdentityFunctionInfo *RCFI,
EpilogueARCFunctionInfo *EAFI,
ProgramTerminationFunctionInfo *PTFI)
: Context(F, RCFI),
Evaluator(F, AA, POTA, RCFI, EAFI, PTFI, Context.DecToIncStateMap,
Context.IncToDecStateMap) {}
bool run(bool FreezePostDomReleases) {
bool NestingDetected =;
llvm::SmallVector<SILInstruction *, 8> DeadInsts;
bool MatchedPair = Context.performMatching(DeadInsts);
while (!DeadInsts.empty())
return NestingDetected && MatchedPair;
bool madeChange() const { return Context.MadeChange; }
/// A composition of a LoopARCSequenceDataflowEvaluator and an
/// ARCPairingContext. The loop nest is processed bottom up. For each loop, we
/// run the evaluator on the loop and then use the ARCPairingContext to pair
/// retains/releases and eliminate them.
struct LoopARCPairingContext : SILLoopVisitor {
ARCPairingContext Context;
LoopARCSequenceDataflowEvaluator Evaluator;
LoopRegionFunctionInfo *LRFI;
SILLoopInfo *SLI;
LoopARCPairingContext(SILFunction &F, AliasAnalysis *AA,
LoopRegionFunctionInfo *LRFI, SILLoopInfo *SLI,
RCIdentityFunctionInfo *RCFI,
EpilogueARCFunctionInfo *EAFI,
ProgramTerminationFunctionInfo *PTFI)
: SILLoopVisitor(&F, SLI), Context(F, RCFI),
Evaluator(F, AA, LRFI, SLI, RCFI, EAFI, PTFI, Context.DecToIncStateMap,
bool process() {
if (!madeChange())
return false;
return true;
bool madeChange() const { return Context.MadeChange; }
void runOnLoop(SILLoop *L) override;
void runOnFunction(SILFunction *F) override;
bool processRegion(const LoopRegion *R, bool FreezePostDomReleases,
bool RecomputePostDomReleases);
} // end swift namespace