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
|
//===--- ARCMatchingSet.cpp - ARC Matching Set Builder --------------------===//
//
// 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 "ARCMatchingSet.h"
#include "RefCountState.h"
#include "swift/Basic/BlotMapVector.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/PostOrderAnalysis.h"
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.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;
//===----------------------------------------------------------------------===//
// ARC Matching Set Builder
//===----------------------------------------------------------------------===//
/// Match retains to releases and return whether or not all of the releases
/// were known safe.
std::optional<MatchingSetFlags>
ARCMatchingSetBuilder::matchIncrementsToDecrements() {
MatchingSetFlags Flags = {true, true};
// For each increment in our list of new increments.
//
// FIXME: Refactor this into its own function.
for (SILInstruction *Increment : NewIncrements) {
LLVM_DEBUG(llvm::dbgs() << " Looking up state for increment: "
<< *Increment);
auto BURefCountState = BUMap.find(Increment);
assert(BURefCountState != BUMap.end() && "Catch this on debug builds.");
if (BURefCountState == BUMap.end()) {
LLVM_DEBUG(llvm::dbgs() << " FAILURE! Could not find state for "
"increment!\n");
return std::nullopt;
}
LLVM_DEBUG(llvm::dbgs() << " SUCCESS! Found state for increment.\n");
// If we are not tracking a ref count inst for this increment, there is
// nothing we can pair it with implying we should skip it.
if (!(*BURefCountState)->second.isTrackingRefCountInst()) {
LLVM_DEBUG(llvm::dbgs() << " SKIPPING INCREMENT! State not tracking "
"any instruction.\n");
continue;
}
bool BUIsKnownSafe = (*BURefCountState)->second.isKnownSafe();
LLVM_DEBUG(llvm::dbgs() << " BOTTOM UP KNOWNSAFE: "
<< (BUIsKnownSafe ? "true" : "false") << "\n");
Flags.KnownSafe &= BUIsKnownSafe;
bool BUCodeMotionSafe = (*BURefCountState)->second.isCodeMotionSafe();
LLVM_DEBUG(llvm::dbgs() << " BOTTOM UP CODEMOTIONSAFE: "
<< (BUCodeMotionSafe ? "true" : "false") << "\n");
Flags.CodeMotionSafe &= BUCodeMotionSafe;
// Now that we know we have an inst, grab the decrement.
for (auto DecIter : (*BURefCountState)->second.getInstructions()) {
SILInstruction *Decrement = DecIter;
LLVM_DEBUG(llvm::dbgs() << " Decrement: " << *Decrement);
// Now grab the increment matched up with the decrement from the bottom up
// map.
// If we can't find it, bail we can't match this increment up with
// anything.
auto TDRefCountState = TDMap.find(Decrement);
if (TDRefCountState == TDMap.end()) {
LLVM_DEBUG(llvm::dbgs() << " FAILURE! Could not find state "
"for decrement.\n");
return std::nullopt;
}
LLVM_DEBUG(llvm::dbgs() << " SUCCESS! Found state for "
"decrement.\n");
// Make sure the increment we are looking at is also matched to our
// decrement. Otherwise bail.
if (!(*TDRefCountState)->second.isTrackingRefCountInst() ||
!(*TDRefCountState)->second.containsInstruction(Increment)) {
LLVM_DEBUG(
llvm::dbgs() << " FAILURE! Not tracking instruction or "
"found increment that did not match.\n");
return std::nullopt;
}
// Add the decrement to the decrement to move set. If we don't insert
// anything, just continue.
if (!MatchSet.Decrements.insert(Decrement)) {
LLVM_DEBUG(llvm::dbgs()
<< " SKIPPING! Already processed this decrement\n");
continue;
}
NewDecrements.push_back(Decrement);
}
}
return Flags;
}
std::optional<MatchingSetFlags>
ARCMatchingSetBuilder::matchDecrementsToIncrements() {
MatchingSetFlags Flags = {true, true};
// For each increment in our list of new increments.
//
// FIXME: Refactor this into its own function.
for (SILInstruction *Decrement : NewDecrements) {
LLVM_DEBUG(llvm::dbgs() << " Looking up state for decrement: "
<< *Decrement);
auto TDRefCountState = TDMap.find(Decrement);
assert(TDRefCountState != TDMap.end() && "Catch this on debug builds.");
if (TDRefCountState == TDMap.end()) {
LLVM_DEBUG(llvm::dbgs() << " FAILURE! Could not find state for "
"increment!\n");
return std::nullopt;
}
LLVM_DEBUG(llvm::dbgs() << " SUCCESS! Found state for decrement.\n");
// If we are not tracking a ref count inst for this increment, there is
// nothing we can pair it with implying we should skip it.
if (!(*TDRefCountState)->second.isTrackingRefCountInst()) {
LLVM_DEBUG(llvm::dbgs() << " SKIPPING DECREMENT! State not tracking "
"any instruction.\n");
continue;
}
bool TDIsKnownSafe = (*TDRefCountState)->second.isKnownSafe();
LLVM_DEBUG(llvm::dbgs() << " TOP DOWN KNOWNSAFE: "
<< (TDIsKnownSafe ? "true" : "false") << "\n");
Flags.KnownSafe &= TDIsKnownSafe;
bool TDCodeMotionSafe = (*TDRefCountState)->second.isCodeMotionSafe();
LLVM_DEBUG(llvm::dbgs() << " TOP DOWN CODEMOTIONSAFE: "
<< (TDCodeMotionSafe ? "true" : "false") << "\n");
Flags.CodeMotionSafe &= TDCodeMotionSafe;
// Now that we know we have an inst, grab the decrement.
for (auto IncIter : (*TDRefCountState)->second.getInstructions()) {
SILInstruction *Increment = IncIter;
LLVM_DEBUG(llvm::dbgs() << " Increment: " << *Increment);
// Now grab the increment matched up with the decrement from the bottom up
// map.
// If we can't find it, bail we can't match this increment up with
// anything.
auto BURefCountState = BUMap.find(Increment);
if (BURefCountState == BUMap.end()) {
LLVM_DEBUG(llvm::dbgs() << " FAILURE! Could not find state "
"for increment.\n");
return std::nullopt;
}
LLVM_DEBUG(
llvm::dbgs() << " SUCCESS! Found state for increment.\n");
// Make sure the increment we are looking at is also matched to our
// decrement.
// Otherwise bail.
if (!(*BURefCountState)->second.isTrackingRefCountInst() ||
!(*BURefCountState)->second.containsInstruction(Decrement)) {
LLVM_DEBUG(
llvm::dbgs() << " FAILURE! Not tracking instruction or "
"found increment that did not match.\n");
return std::nullopt;
}
// Add the decrement to the decrement to move set. If we don't insert
// anything, just continue.
if (!MatchSet.Increments.insert(Increment)) {
LLVM_DEBUG(llvm::dbgs()
<< " SKIPPING! Already processed this increment.\n");
continue;
}
NewIncrements.push_back(Increment);
}
}
return Flags;
}
/// Visit each retain/release that is matched up to our operand over and over
/// again until we converge by not adding any more to the set which we can move.
/// If we find a situation that we cannot handle, we bail and return false. If
/// we succeed and it is safe to move increment/releases, we return true.
bool ARCMatchingSetBuilder::matchUpIncDecSetsForPtr() {
bool KnownSafeTD = true;
bool KnownSafeBU = true;
bool CodeMotionSafeTD = true;
bool CodeMotionSafeBU = true;
while (true) {
LLVM_DEBUG(llvm::dbgs() << "Attempting to match up increments -> "
"decrements:\n");
// For each increment in our list of new increments, attempt to match them
// up with decrements.
auto Result = matchIncrementsToDecrements();
if (!Result) {
LLVM_DEBUG(llvm::dbgs() << " FAILED TO MATCH INCREMENTS -> "
"DECREMENTS!\n");
return false;
}
if (!Result->KnownSafe) {
LLVM_DEBUG(llvm::dbgs() << " NOT KNOWN SAFE!\n");
KnownSafeTD = false;
}
if (!Result->CodeMotionSafe) {
LLVM_DEBUG(llvm::dbgs() << " NOT CODE MOTION SAFE!\n");
CodeMotionSafeTD = false;
}
NewIncrements.clear();
// If we do not have any decrements to attempt to match up with, bail.
if (NewDecrements.empty())
break;
LLVM_DEBUG(llvm::dbgs() << "Attempting to match up decrements -> "
"increments:\n");
Result = matchDecrementsToIncrements();
if (!Result) {
LLVM_DEBUG(llvm::dbgs() << " FAILED TO MATCH DECREMENTS -> "
"INCREMENTS!\n");
return false;
}
if (!Result->KnownSafe) {
LLVM_DEBUG(llvm::dbgs() << " NOT KNOWN SAFE!\n");
KnownSafeBU = false;
}
if (!Result->CodeMotionSafe) {
LLVM_DEBUG(llvm::dbgs() << " NOT CODE MOTION SAFE!\n");
CodeMotionSafeBU = false;
}
NewDecrements.clear();
// If we do not have any increment to attempt to match up with again, bail.
if (NewIncrements.empty())
break;
}
// There is no way we can get a top-down code motion but not a bottom-up, or vice
// versa.
assert(CodeMotionSafeTD == CodeMotionSafeBU && "Asymmetric code motion safety");
bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU);
bool CodeMotionSafe = (CodeMotionSafeTD && CodeMotionSafeBU);
if (UnconditionallySafe || CodeMotionSafe) {
LLVM_DEBUG(llvm::dbgs() << "UNCONDITIONALLY OR CODE MOTION SAFE! "
"DELETING INSTS.\n");
} else {
LLVM_DEBUG(llvm::dbgs() << "NOT UNCONDITIONALLY SAFE AND CODE MOTION "
"BLOCKED!\n");
return false;
}
// Make sure we always have increments and decrements in the match set.
assert(MatchSet.Increments.empty() == MatchSet.Decrements.empty() &&
"Match set without increments or decrements");
if (!MatchSet.Increments.empty())
MatchedPair = true;
// Success!
LLVM_DEBUG(llvm::dbgs() << "SUCCESS! We can remove things.\n");
return true;
}
|