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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
//===--- CallerAnalysis.cpp - Determine callsites to a function ----------===//
//
// 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 "sil-caller-analysis"
#include "swift/SILOptimizer/Analysis/CallerAnalysis.h"
#include "swift/Basic/Assertions.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILVisitor.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/YAMLTraits.h"
using namespace swift;
namespace {
using FunctionInfo = CallerAnalysis::FunctionInfo;
} // end anonymous namespace
//===----------------------------------------------------------------------===//
// CallerAnalysis::FunctionInfo
//===----------------------------------------------------------------------===//
CallerAnalysis::FunctionInfo::FunctionInfo(SILFunction *f)
: callerStates(),
// TODO: Make this more aggressive by considering
// final/visibility/etc.
mayHaveIndirectCallers(
f->getDynamicallyReplacedFunction() ||
f->getReferencedAdHocRequirementWitnessFunction() ||
canBeCalledIndirectly(f->getRepresentation())),
mayHaveExternalCallers(f->isPossiblyUsedExternally() ||
f->isAvailableExternally()) {}
//===----------------------------------------------------------------------===//
// CallerAnalysis::ApplySiteFinderVisitor
//===----------------------------------------------------------------------===//
struct CallerAnalysis::ApplySiteFinderVisitor
: SILInstructionVisitor<ApplySiteFinderVisitor, bool> {
CallerAnalysis *analysis;
SILFunction *callerFn;
#ifndef NDEBUG
SmallPtrSet<SILInstruction *, 8> visitedCallSites;
llvm::SmallSetVector<SILInstruction *, 8> callSitesThatMustBeVisited;
#endif
ApplySiteFinderVisitor(CallerAnalysis *analysis, SILFunction *callerFn)
: analysis(analysis), callerFn(callerFn) {}
~ApplySiteFinderVisitor();
bool visitSILInstruction(SILInstruction *) { return false; }
bool visitFunctionRefInst(FunctionRefInst *fri) {
return visitFunctionRefBaseInst(fri);
}
bool visitDynamicFunctionRefInst(DynamicFunctionRefInst *fri) {
return visitFunctionRefBaseInst(fri);
}
bool
visitPreviousDynamicFunctionRefInst(PreviousDynamicFunctionRefInst *fri) {
return visitFunctionRefBaseInst(fri);
}
bool visitFunctionRefBaseInst(FunctionRefBaseInst *fri);
void process();
void processApplySites(ArrayRef<ApplySite> applySites);
void processApplySites(ArrayRef<FullApplySite> applySites);
void checkCallSiteInvariants(SILInstruction &i);
};
void CallerAnalysis::ApplySiteFinderVisitor::processApplySites(
ArrayRef<ApplySite> applySites) {
// For now we just verify our invariants. If we need to update other
// non-NDEBUG state related to apply sites, this should be updated.
#ifndef NDEBUG
for (auto applySite : applySites) {
visitedCallSites.insert(applySite.getInstruction());
callSitesThatMustBeVisited.remove(applySite.getInstruction());
}
#endif
}
void CallerAnalysis::ApplySiteFinderVisitor::processApplySites(
ArrayRef<FullApplySite> applySites) {
// For now we just verify our invariants. If we need to update other
// non-NDEBUG state related to apply sites, this should be updated.
#ifndef NDEBUG
for (auto applySite : applySites) {
visitedCallSites.insert(applySite.getInstruction());
callSitesThatMustBeVisited.remove(applySite.getInstruction());
}
#endif
}
CallerAnalysis::ApplySiteFinderVisitor::~ApplySiteFinderVisitor() {
#ifndef NDEBUG
if (callSitesThatMustBeVisited.empty())
return;
llvm::errs() << "Found unhandled call sites!\n";
while (callSitesThatMustBeVisited.size()) {
auto *i = callSitesThatMustBeVisited.pop_back_val();
llvm::errs() << "Inst: " << *i;
}
assert(false && "Unhandled call site?!");
#endif
}
bool CallerAnalysis::ApplySiteFinderVisitor::visitFunctionRefBaseInst(
FunctionRefBaseInst *fri) {
auto optResult = findLocalApplySites(fri);
auto *calleeFn = fri->getInitiallyReferencedFunction();
// First make an edge from our callerInfo to our calleeState for invalidation
// purposes.
analysis->getOrInsertFunctionInfo(callerFn).calleeStates.insert(calleeFn);
// Then grab our callee state and update it with state for this caller.
auto iter = analysis->getOrInsertFunctionInfo(calleeFn).callerStates.
insert({callerFn, {}});
// If we succeeded in inserting a new value, put in an optimistic
// value for escaping.
if (iter.second) {
iter.first->second.isDirectCallerSetComplete = true;
}
// Then check if we found any information at all from our result. If we
// didn't, then mark this as escaping and bail.
if (!optResult.has_value()) {
iter.first->second.isDirectCallerSetComplete = false;
return true;
}
auto &result = optResult.value();
// Ok. We know that we have some sort of information. Merge that information
// into our information.
iter.first->second.isDirectCallerSetComplete &= !result.isEscaping();
if (result.fullApplySites.size()) {
iter.first->second.hasFullApply = true;
processApplySites(llvm::ArrayRef(result.fullApplySites));
}
if (result.partialApplySites.size()) {
auto optMin = iter.first->second.getNumPartiallyAppliedArguments();
unsigned min = optMin.value_or(UINT_MAX);
for (ApplySite partialSite : result.partialApplySites) {
min = std::min(min, partialSite.getNumArguments());
}
iter.first->second.setNumPartiallyAppliedArguments(min);
processApplySites(result.partialApplySites);
}
return true;
}
void CallerAnalysis::ApplySiteFinderVisitor::checkCallSiteInvariants(
SILInstruction &i) {
#ifndef NDEBUG
if (auto apply = FullApplySite::isa(&i)) {
if (apply.getCalleeFunction() && !visitedCallSites.count(&i)) {
callSitesThatMustBeVisited.insert(&i);
}
return;
}
// Make sure that we are in sync with looking for partial apply callees.
if (auto *pai = dyn_cast<PartialApplyInst>(&i)) {
if (pai->getCalleeFunction() && !visitedCallSites.count(&i)) {
callSitesThatMustBeVisited.insert(pai);
}
return;
}
#endif
}
void CallerAnalysis::ApplySiteFinderVisitor::process() {
for (auto &block : *callerFn) {
for (auto &i : block) {
#ifndef NDEBUG
// If this is a call site that we visited as part of seeing a different
// function_ref, skip it. We know that it has been processed correctly.
//
// NOTE: This is only used in NDEBUG builds since we only use this as part
// of the verification that we can find all callees going forward along
// def-use edges that FullApplySite is able to track backwards along
// def-use edges.
if (visitedCallSites.count(&i))
continue;
#endif
// Try to find the apply sites for this specific FRI.
if (visit(&i))
continue;
#ifndef NDEBUG
checkCallSiteInvariants(i);
#endif
}
}
}
//===----------------------------------------------------------------------===//
// CallerAnalysis
//===----------------------------------------------------------------------===//
// NOTE: This is only meant to be used by external users of CallerAnalysis since
// it recomputes our invalidated results. For internal uses, please instead use
// getOrInsertFunctionInfo or unsafeGetFunctionInfo.
const FunctionInfo &CallerAnalysis::getFunctionInfo(SILFunction *f) const {
// Recompute every function in the invalidated function list and empty the
// list.
auto &self = const_cast<CallerAnalysis &>(*this);
if (funcInfos.find(f) == funcInfos.end()) {
(void)self.getOrInsertFunctionInfo(f);
self.recomputeFunctionList.insert(f);
}
self.processRecomputeFunctionList();
return self.unsafeGetFunctionInfo(f);
}
// Private only version of this function for mutable callers that tries to
// initialize a new f.
FunctionInfo &CallerAnalysis::getOrInsertFunctionInfo(SILFunction *f) {
LLVM_DEBUG(llvm::dbgs() << "CallerAnalysis: Creating caller info for: "
<< f->getName() << "\n");
return funcInfos.try_emplace(f, f).first->second;
}
FunctionInfo &CallerAnalysis::unsafeGetFunctionInfo(SILFunction *f) {
auto r = funcInfos.find(f);
assert(r != funcInfos.end() && "Function does not have functionInfo!");
return r->second;
}
const FunctionInfo &
CallerAnalysis::unsafeGetFunctionInfo(SILFunction *f) const {
auto r = funcInfos.find(f);
assert(r != funcInfos.end() && "Function does not have functionInfo!");
return r->second;
}
CallerAnalysis::CallerAnalysis(SILModule *m)
: SILAnalysis(SILAnalysisKind::Caller), mod(*m) {
// When we start we create a function info for each f and add all f to the
// recompute function list.
for (auto &f : mod) {
getOrInsertFunctionInfo(&f);
recomputeFunctionList.insert(&f);
}
}
void CallerAnalysis::processFunctionCallSites(SILFunction *callerFn) {
ApplySiteFinderVisitor visitor(this, callerFn);
visitor.process();
}
void CallerAnalysis::invalidateAllInfo(SILFunction *f, FunctionInfo &fInfo) {
// Then we first eliminate any callees that we point at.
invalidateKnownCallees(f, fInfo);
// And then eliminate any caller edges that we need.
while (fInfo.callerStates.size()) {
auto back = fInfo.callerStates.back();
SILFunction *caller = back.first;
auto &callerInfo = unsafeGetFunctionInfo(caller);
LLVM_DEBUG(llvm::dbgs()
<< " caller-backedge: " << caller->getName() << "\n");
bool foundF = callerInfo.calleeStates.remove(f);
(void)foundF;
assert(foundF && "Bad caller edge pointing at f?");
fInfo.callerStates.pop_back();
}
}
void CallerAnalysis::invalidateKnownCallees(SILFunction *caller,
FunctionInfo &callerInfo) {
LLVM_DEBUG(llvm::dbgs() << "Invalidating caller: " << caller->getName()
<< "\n");
while (callerInfo.calleeStates.size()) {
auto *callee = callerInfo.calleeStates.pop_back_val();
FunctionInfo &calleeInfo = unsafeGetFunctionInfo(callee);
LLVM_DEBUG(llvm::dbgs() << " callee: " << callee->getName() << "\n");
assert(calleeInfo.callerStates.count(caller) &&
"Referenced callee is not fully/partially applied in the caller?!");
// Then remove the caller from this specific callee's info struct
// and to be conservative mark the callee as potentially having an
// escaping use that we do not understand.
calleeInfo.callerStates.erase(caller);
}
}
void CallerAnalysis::invalidateKnownCallees(SILFunction *caller) {
auto iter = funcInfos.find(caller);
if (iter != funcInfos.end()) {
// Look up the callees that our caller refers to and invalidate any
// values that point back at the caller.
invalidateKnownCallees(caller, iter->second);
}
}
void CallerAnalysis::verify(SILFunction *caller) const {
#ifndef NDEBUG
const FunctionInfo &callerInfo = unsafeGetFunctionInfo(caller);
verify(caller, callerInfo);
#endif
}
void CallerAnalysis::verify(SILFunction *function,
const FunctionInfo &functionInfo) const {
#ifndef NDEBUG
LLVM_DEBUG(llvm::dbgs() << "Validating function: " << function->getName()
<< "\n");
for (auto *callee : functionInfo.calleeStates) {
LLVM_DEBUG(llvm::dbgs() << " callee: " << callee->getName() << "\n");
const FunctionInfo &calleeInfo = unsafeGetFunctionInfo(callee);
assert(calleeInfo.callerStates.count(function) &&
"Referenced callee is not fully/partially applied in the caller");
}
// Make sure all caller edges are valid.
for (auto callerPair : functionInfo.callerStates) {
auto *caller = callerPair.first;
LLVM_DEBUG(llvm::dbgs() << " caller: " << caller->getName() << "\n");
const FunctionInfo &callerInfo = unsafeGetFunctionInfo(caller);
assert(callerInfo.calleeStates.count(function) &&
"Referencing caller does not have a callee edge for function");
}
#endif
}
void CallerAnalysis::verify() const {
#ifndef NDEBUG
std::vector<SILFunction *> seenFunctions;
for (auto &fn : mod) {
bool found = funcInfos.count(&fn);
if (!found) {
llvm::errs() << "Missing notification for added function: '"
<< fn.getName() << "'\n";
llvm_unreachable("standard error assertion");
}
seenFunctions.push_back(&fn);
}
sortUnique(seenFunctions);
for (auto &pair : funcInfos) {
bool found = std::binary_search(seenFunctions.begin(), seenFunctions.end(),
pair.first);
if (!found) {
llvm::errs() << "Notification not sent for deleted function: '"
<< pair.first->getName() << "'.";
llvm_unreachable("standard error assertion");
}
verify(pair.first, pair.second);
}
#endif
}
void CallerAnalysis::invalidate() {
for (auto &f : mod) {
// Since we are going over all functions in the module
// invalidateKnownCallees should be sufficient.
invalidateKnownCallees(&f);
// We do not need to clear recompute function list since we know that it can
// at most contain a subset of the functions in the module so the SetVector
// will unique for us.
recomputeFunctionList.insert(&f);
}
}
void CallerAnalysis::notifyWillDeleteFunction(SILFunction *f) {
auto iter = funcInfos.find(f);
if (iter == funcInfos.end())
return;
invalidateAllInfo(f, iter->second);
recomputeFunctionList.remove(f);
// Now that we have invalidated all references to the function, delete it.
funcInfos.erase(iter);
}
//===----------------------------------------------------------------------===//
// CallerAnalysis YAML Dumper
//===----------------------------------------------------------------------===//
namespace {
using llvm::yaml::IO;
using llvm::yaml::MappingTraits;
using llvm::yaml::Output;
using llvm::yaml::ScalarEnumerationTraits;
using llvm::yaml::SequenceTraits;
/// A special struct that marshals call graph state into a form that
/// is easy for llvm's yaml i/o to dump. Its structure is meant to
/// correspond to how the data should be shown by the printer, so
/// naturally it is slightly redundant.
struct YAMLCallGraphNode {
StringRef calleeName;
bool hasCaller;
unsigned minPartialAppliedArgs;
bool hasOnlyCompleteDirectCallerSets;
bool hasAllCallers;
std::vector<StringRef> partialAppliers;
std::vector<StringRef> fullAppliers;
YAMLCallGraphNode() = delete;
~YAMLCallGraphNode() = default;
YAMLCallGraphNode(const YAMLCallGraphNode &) = delete;
YAMLCallGraphNode(YAMLCallGraphNode &&) = delete;
YAMLCallGraphNode &operator=(const YAMLCallGraphNode &) = delete;
YAMLCallGraphNode &operator=(YAMLCallGraphNode &&) = delete;
YAMLCallGraphNode(StringRef calleeName, bool hasCaller,
unsigned minPartialAppliedArgs,
bool hasOnlyCompleteDirectCallerSets, bool hasAllCallers,
std::vector<StringRef> &&partialAppliers,
std::vector<StringRef> &&fullAppliers)
: calleeName(calleeName), hasCaller(hasCaller),
minPartialAppliedArgs(minPartialAppliedArgs),
hasOnlyCompleteDirectCallerSets(hasOnlyCompleteDirectCallerSets),
hasAllCallers(hasAllCallers),
partialAppliers(std::move(partialAppliers)),
fullAppliers(std::move(fullAppliers)) {}
};
} // end anonymous namespace
namespace llvm {
namespace yaml {
template <> struct MappingTraits<YAMLCallGraphNode> {
static void mapping(IO &io, YAMLCallGraphNode &func) {
io.mapRequired("calleeName", func.calleeName);
io.mapRequired("hasCaller", func.hasCaller);
io.mapRequired("minPartialAppliedArgs", func.minPartialAppliedArgs);
io.mapRequired("hasOnlyCompleteDirectCallerSets",
func.hasOnlyCompleteDirectCallerSets);
io.mapRequired("hasAllCallers", func.hasAllCallers);
io.mapRequired("partialAppliers", func.partialAppliers);
io.mapRequired("fullAppliers", func.fullAppliers);
}
};
} // namespace yaml
} // namespace llvm
void CallerAnalysis::dump() const { print(llvm::errs()); }
void CallerAnalysis::print(const char *filePath) const {
using namespace llvm::sys;
std::error_code error;
llvm::raw_fd_ostream fileOutputStream(filePath, error, fs::OF_Text);
if (error) {
llvm::errs() << "Failed to open path \"" << filePath << "\" for writing.!";
llvm_unreachable("default error handler");
}
print(fileOutputStream);
}
void CallerAnalysis::print(llvm::raw_ostream &os) const {
llvm::yaml::Output yout(os);
// NOTE: We purposely do not iterate over our internal state here to ensure
// that we dump for all functions and that we dump the state we have stored
// with the functions in module order.
for (auto &f : mod) {
const auto &fi = getFunctionInfo(&f);
std::vector<StringRef> partialAppliers;
std::vector<StringRef> fullAppliers;
for (auto &apply : fi.getAllReferencingCallers()) {
if (apply.second.hasFullApply) {
fullAppliers.push_back(apply.first->getName());
}
if (apply.second.getNumPartiallyAppliedArguments().has_value()) {
partialAppliers.push_back(apply.first->getName());
}
}
YAMLCallGraphNode node(
f.getName(), fi.hasDirectCaller(), fi.getMinPartialAppliedArgs(),
fi.hasOnlyCompleteDirectCallerSets(), fi.foundAllCallers(),
std::move(partialAppliers), std::move(fullAppliers));
yout << node;
}
}
//===----------------------------------------------------------------------===//
// Main Entry Point
//===----------------------------------------------------------------------===//
SILAnalysis *swift::createCallerAnalysis(SILModule *mod) {
return new CallerAnalysis(mod);
}
|