1 //===- Attributor.cpp - Module-wide attribute deduction -------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements an interprocedural pass that deduces and/or propagates 10 // attributes. This is done in an abstract interpretation style fixpoint 11 // iteration. See the Attributor.h file comment and the class descriptions in 12 // that file for more information. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/Transforms/IPO/Attributor.h" 17 18 #include "llvm/ADT/GraphTraits.h" 19 #include "llvm/ADT/PointerIntPair.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/ADT/Statistic.h" 22 #include "llvm/ADT/TinyPtrVector.h" 23 #include "llvm/Analysis/CallGraph.h" 24 #include "llvm/Analysis/InlineCost.h" 25 #include "llvm/Analysis/LazyValueInfo.h" 26 #include "llvm/Analysis/MemoryBuiltins.h" 27 #include "llvm/Analysis/MemorySSAUpdater.h" 28 #include "llvm/Analysis/MustExecute.h" 29 #include "llvm/Analysis/ValueTracking.h" 30 #include "llvm/IR/Attributes.h" 31 #include "llvm/IR/Constant.h" 32 #include "llvm/IR/Constants.h" 33 #include "llvm/IR/GlobalValue.h" 34 #include "llvm/IR/GlobalVariable.h" 35 #include "llvm/IR/IRBuilder.h" 36 #include "llvm/IR/Instruction.h" 37 #include "llvm/IR/Instructions.h" 38 #include "llvm/IR/IntrinsicInst.h" 39 #include "llvm/IR/NoFolder.h" 40 #include "llvm/IR/ValueHandle.h" 41 #include "llvm/IR/Verifier.h" 42 #include "llvm/InitializePasses.h" 43 #include "llvm/Support/Casting.h" 44 #include "llvm/Support/CommandLine.h" 45 #include "llvm/Support/Debug.h" 46 #include "llvm/Support/DebugCounter.h" 47 #include "llvm/Support/FileSystem.h" 48 #include "llvm/Support/GraphWriter.h" 49 #include "llvm/Support/raw_ostream.h" 50 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 51 #include "llvm/Transforms/Utils/Cloning.h" 52 #include "llvm/Transforms/Utils/Local.h" 53 54 #include <cassert> 55 #include <string> 56 57 using namespace llvm; 58 59 #define DEBUG_TYPE "attributor" 60 61 DEBUG_COUNTER(ManifestDBGCounter, "attributor-manifest", 62 "Determine what attributes are manifested in the IR"); 63 64 STATISTIC(NumFnDeleted, "Number of function deleted"); 65 STATISTIC(NumFnWithExactDefinition, 66 "Number of functions with exact definitions"); 67 STATISTIC(NumFnWithoutExactDefinition, 68 "Number of functions without exact definitions"); 69 STATISTIC(NumFnShallowWrappersCreated, "Number of shallow wrappers created"); 70 STATISTIC(NumAttributesTimedOut, 71 "Number of abstract attributes timed out before fixpoint"); 72 STATISTIC(NumAttributesValidFixpoint, 73 "Number of abstract attributes in a valid fixpoint state"); 74 STATISTIC(NumAttributesManifested, 75 "Number of abstract attributes manifested in IR"); 76 77 // TODO: Determine a good default value. 78 // 79 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads 80 // (when run with the first 5 abstract attributes). The results also indicate 81 // that we never reach 32 iterations but always find a fixpoint sooner. 82 // 83 // This will become more evolved once we perform two interleaved fixpoint 84 // iterations: bottom-up and top-down. 85 static cl::opt<unsigned> 86 SetFixpointIterations("attributor-max-iterations", cl::Hidden, 87 cl::desc("Maximal number of fixpoint iterations."), 88 cl::init(32)); 89 90 static cl::opt<unsigned, true> MaxInitializationChainLengthX( 91 "attributor-max-initialization-chain-length", cl::Hidden, 92 cl::desc( 93 "Maximal number of chained initializations (to avoid stack overflows)"), 94 cl::location(MaxInitializationChainLength), cl::init(1024)); 95 unsigned llvm::MaxInitializationChainLength; 96 97 static cl::opt<bool> VerifyMaxFixpointIterations( 98 "attributor-max-iterations-verify", cl::Hidden, 99 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"), 100 cl::init(false)); 101 102 static cl::opt<bool> AnnotateDeclarationCallSites( 103 "attributor-annotate-decl-cs", cl::Hidden, 104 cl::desc("Annotate call sites of function declarations."), cl::init(false)); 105 106 static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion", 107 cl::init(true), cl::Hidden); 108 109 static cl::opt<bool> 110 AllowShallowWrappers("attributor-allow-shallow-wrappers", cl::Hidden, 111 cl::desc("Allow the Attributor to create shallow " 112 "wrappers for non-exact definitions."), 113 cl::init(false)); 114 115 static cl::opt<bool> 116 AllowDeepWrapper("attributor-allow-deep-wrappers", cl::Hidden, 117 cl::desc("Allow the Attributor to use IP information " 118 "derived from non-exact functions via cloning"), 119 cl::init(false)); 120 121 // These options can only used for debug builds. 122 #ifndef NDEBUG 123 static cl::list<std::string> 124 SeedAllowList("attributor-seed-allow-list", cl::Hidden, 125 cl::desc("Comma seperated list of attribute names that are " 126 "allowed to be seeded."), 127 cl::ZeroOrMore, cl::CommaSeparated); 128 129 static cl::list<std::string> FunctionSeedAllowList( 130 "attributor-function-seed-allow-list", cl::Hidden, 131 cl::desc("Comma seperated list of function names that are " 132 "allowed to be seeded."), 133 cl::ZeroOrMore, cl::CommaSeparated); 134 #endif 135 136 static cl::opt<bool> 137 DumpDepGraph("attributor-dump-dep-graph", cl::Hidden, 138 cl::desc("Dump the dependency graph to dot files."), 139 cl::init(false)); 140 141 static cl::opt<std::string> DepGraphDotFileNamePrefix( 142 "attributor-depgraph-dot-filename-prefix", cl::Hidden, 143 cl::desc("The prefix used for the CallGraph dot file names.")); 144 145 static cl::opt<bool> ViewDepGraph("attributor-view-dep-graph", cl::Hidden, 146 cl::desc("View the dependency graph."), 147 cl::init(false)); 148 149 static cl::opt<bool> PrintDependencies("attributor-print-dep", cl::Hidden, 150 cl::desc("Print attribute dependencies"), 151 cl::init(false)); 152 153 static cl::opt<bool> EnableCallSiteSpecific( 154 "attributor-enable-call-site-specific-deduction", cl::Hidden, 155 cl::desc("Allow the Attributor to do call site specific analysis"), 156 cl::init(false)); 157 158 static cl::opt<bool> 159 PrintCallGraph("attributor-print-call-graph", cl::Hidden, 160 cl::desc("Print Attributor's internal call graph"), 161 cl::init(false)); 162 163 static cl::opt<bool> SimplifyAllLoads("attributor-simplify-all-loads", 164 cl::Hidden, 165 cl::desc("Try to simplify all loads."), 166 cl::init(true)); 167 168 /// Logic operators for the change status enum class. 169 /// 170 ///{ 171 ChangeStatus llvm::operator|(ChangeStatus L, ChangeStatus R) { 172 return L == ChangeStatus::CHANGED ? L : R; 173 } 174 ChangeStatus &llvm::operator|=(ChangeStatus &L, ChangeStatus R) { 175 L = L | R; 176 return L; 177 } 178 ChangeStatus llvm::operator&(ChangeStatus L, ChangeStatus R) { 179 return L == ChangeStatus::UNCHANGED ? L : R; 180 } 181 ChangeStatus &llvm::operator&=(ChangeStatus &L, ChangeStatus R) { 182 L = L & R; 183 return L; 184 } 185 ///} 186 187 bool AA::isNoSyncInst(Attributor &A, const Instruction &I, 188 const AbstractAttribute &QueryingAA) { 189 // We are looking for volatile instructions or non-relaxed atomics. 190 if (const auto *CB = dyn_cast<CallBase>(&I)) { 191 if (CB->hasFnAttr(Attribute::NoSync)) 192 return true; 193 194 // Non-convergent and readnone imply nosync. 195 if (!CB->isConvergent() && !CB->mayReadOrWriteMemory()) 196 return true; 197 198 if (AANoSync::isNoSyncIntrinsic(&I)) 199 return true; 200 201 const auto &NoSyncAA = A.getAAFor<AANoSync>( 202 QueryingAA, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL); 203 return NoSyncAA.isAssumedNoSync(); 204 } 205 206 if (!I.mayReadOrWriteMemory()) 207 return true; 208 209 return !I.isVolatile() && !AANoSync::isNonRelaxedAtomic(&I); 210 } 211 212 bool AA::isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA, 213 const Value &V) { 214 if (auto *C = dyn_cast<Constant>(&V)) 215 return !C->isThreadDependent(); 216 // TODO: Inspect and cache more complex instructions. 217 if (auto *CB = dyn_cast<CallBase>(&V)) 218 return CB->getNumOperands() == 0 && !CB->mayHaveSideEffects() && 219 !CB->mayReadFromMemory(); 220 const Function *Scope = nullptr; 221 if (auto *I = dyn_cast<Instruction>(&V)) 222 Scope = I->getFunction(); 223 if (auto *A = dyn_cast<Argument>(&V)) 224 Scope = A->getParent(); 225 if (!Scope) 226 return false; 227 auto &NoRecurseAA = A.getAAFor<AANoRecurse>( 228 QueryingAA, IRPosition::function(*Scope), DepClassTy::OPTIONAL); 229 return NoRecurseAA.isAssumedNoRecurse(); 230 } 231 232 Constant *AA::getInitialValueForObj(Value &Obj, Type &Ty, 233 const TargetLibraryInfo *TLI) { 234 if (isa<AllocaInst>(Obj)) 235 return UndefValue::get(&Ty); 236 if (isAllocationFn(&Obj, TLI)) 237 return getInitialValueOfAllocation(&cast<CallBase>(Obj), TLI, &Ty); 238 auto *GV = dyn_cast<GlobalVariable>(&Obj); 239 if (!GV || !GV->hasLocalLinkage()) 240 return nullptr; 241 if (!GV->hasInitializer()) 242 return UndefValue::get(&Ty); 243 return dyn_cast_or_null<Constant>(getWithType(*GV->getInitializer(), Ty)); 244 } 245 246 bool AA::isValidInScope(const Value &V, const Function *Scope) { 247 if (isa<Constant>(V)) 248 return true; 249 if (auto *I = dyn_cast<Instruction>(&V)) 250 return I->getFunction() == Scope; 251 if (auto *A = dyn_cast<Argument>(&V)) 252 return A->getParent() == Scope; 253 return false; 254 } 255 256 bool AA::isValidAtPosition(const Value &V, const Instruction &CtxI, 257 InformationCache &InfoCache) { 258 if (isa<Constant>(V) || &V == &CtxI) 259 return true; 260 const Function *Scope = CtxI.getFunction(); 261 if (auto *A = dyn_cast<Argument>(&V)) 262 return A->getParent() == Scope; 263 if (auto *I = dyn_cast<Instruction>(&V)) { 264 if (I->getFunction() == Scope) { 265 if (const DominatorTree *DT = 266 InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>( 267 *Scope)) 268 return DT->dominates(I, &CtxI); 269 // Local dominance check mostly for the old PM passes. 270 if (I->getParent() == CtxI.getParent()) 271 return llvm::any_of( 272 make_range(I->getIterator(), I->getParent()->end()), 273 [&](const Instruction &AfterI) { return &AfterI == &CtxI; }); 274 } 275 } 276 return false; 277 } 278 279 Value *AA::getWithType(Value &V, Type &Ty) { 280 if (V.getType() == &Ty) 281 return &V; 282 if (isa<PoisonValue>(V)) 283 return PoisonValue::get(&Ty); 284 if (isa<UndefValue>(V)) 285 return UndefValue::get(&Ty); 286 if (auto *C = dyn_cast<Constant>(&V)) { 287 if (C->isNullValue()) 288 return Constant::getNullValue(&Ty); 289 if (C->getType()->isPointerTy() && Ty.isPointerTy()) 290 return ConstantExpr::getPointerCast(C, &Ty); 291 if (C->getType()->getPrimitiveSizeInBits() >= Ty.getPrimitiveSizeInBits()) { 292 if (C->getType()->isIntegerTy() && Ty.isIntegerTy()) 293 return ConstantExpr::getTrunc(C, &Ty, /* OnlyIfReduced */ true); 294 if (C->getType()->isFloatingPointTy() && Ty.isFloatingPointTy()) 295 return ConstantExpr::getFPTrunc(C, &Ty, /* OnlyIfReduced */ true); 296 } 297 } 298 return nullptr; 299 } 300 301 Optional<Value *> 302 AA::combineOptionalValuesInAAValueLatice(const Optional<Value *> &A, 303 const Optional<Value *> &B, Type *Ty) { 304 if (A == B) 305 return A; 306 if (!B.hasValue()) 307 return A; 308 if (*B == nullptr) 309 return nullptr; 310 if (!A.hasValue()) 311 return Ty ? getWithType(**B, *Ty) : nullptr; 312 if (*A == nullptr) 313 return nullptr; 314 if (!Ty) 315 Ty = (*A)->getType(); 316 if (isa_and_nonnull<UndefValue>(*A)) 317 return getWithType(**B, *Ty); 318 if (isa<UndefValue>(*B)) 319 return A; 320 if (*A && *B && *A == getWithType(**B, *Ty)) 321 return A; 322 return nullptr; 323 } 324 325 template <bool IsLoad, typename Ty> 326 static bool 327 getPotentialCopiesOfMemoryValue(Attributor &A, Ty &I, 328 SmallSetVector<Value *, 4> &PotentialCopies, 329 const AbstractAttribute &QueryingAA, 330 bool &UsedAssumedInformation, bool OnlyExact) { 331 LLVM_DEBUG(dbgs() << "Trying to determine the potential copies of " << I 332 << " (only exact: " << OnlyExact << ")\n";); 333 334 Value &Ptr = *I.getPointerOperand(); 335 SmallVector<Value *, 8> Objects; 336 if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, QueryingAA, &I, 337 UsedAssumedInformation)) { 338 LLVM_DEBUG( 339 dbgs() << "Underlying objects stored into could not be determined\n";); 340 return false; 341 } 342 343 // Containers to remember the pointer infos and new copies while we are not 344 // sure that we can find all of them. If we abort we want to avoid spurious 345 // dependences and potential copies in the provided container. 346 SmallVector<const AAPointerInfo *> PIs; 347 SmallVector<Value *> NewCopies; 348 349 const auto *TLI = 350 A.getInfoCache().getTargetLibraryInfoForFunction(*I.getFunction()); 351 for (Value *Obj : Objects) { 352 LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n"); 353 if (isa<UndefValue>(Obj)) 354 continue; 355 if (isa<ConstantPointerNull>(Obj)) { 356 // A null pointer access can be undefined but any offset from null may 357 // be OK. We do not try to optimize the latter. 358 if (!NullPointerIsDefined(I.getFunction(), 359 Ptr.getType()->getPointerAddressSpace()) && 360 A.getAssumedSimplified(Ptr, QueryingAA, UsedAssumedInformation) == 361 Obj) 362 continue; 363 LLVM_DEBUG( 364 dbgs() << "Underlying object is a valid nullptr, giving up.\n";); 365 return false; 366 } 367 // TODO: Use assumed noalias return. 368 if (!isa<AllocaInst>(Obj) && !isa<GlobalVariable>(Obj) && 369 !(IsLoad ? isAllocationFn(Obj, TLI) : isNoAliasCall(Obj))) { 370 LLVM_DEBUG(dbgs() << "Underlying object is not supported yet: " << *Obj 371 << "\n";); 372 return false; 373 } 374 if (auto *GV = dyn_cast<GlobalVariable>(Obj)) 375 if (!GV->hasLocalLinkage()) { 376 LLVM_DEBUG(dbgs() << "Underlying object is global with external " 377 "linkage, not supported yet: " 378 << *Obj << "\n";); 379 return false; 380 } 381 382 if (IsLoad) { 383 Value *InitialValue = AA::getInitialValueForObj(*Obj, *I.getType(), TLI); 384 if (!InitialValue) 385 return false; 386 NewCopies.push_back(InitialValue); 387 } 388 389 auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) { 390 if ((IsLoad && !Acc.isWrite()) || (!IsLoad && !Acc.isRead())) 391 return true; 392 if (OnlyExact && !IsExact) { 393 LLVM_DEBUG(dbgs() << "Non exact access " << *Acc.getRemoteInst() 394 << ", abort!\n"); 395 return false; 396 } 397 if (IsLoad) { 398 assert(isa<LoadInst>(I) && "Expected load or store instruction only!"); 399 if (Acc.isWrittenValueYetUndetermined()) 400 return true; 401 if (!Acc.isWrittenValueUnknown()) { 402 NewCopies.push_back(Acc.getWrittenValue()); 403 return true; 404 } 405 auto *SI = dyn_cast<StoreInst>(Acc.getRemoteInst()); 406 if (!SI) { 407 LLVM_DEBUG(dbgs() << "Underlying object written through a non-store " 408 "instruction not supported yet: " 409 << *Acc.getRemoteInst() << "\n";); 410 return false; 411 } 412 NewCopies.push_back(SI->getValueOperand()); 413 } else { 414 assert(isa<StoreInst>(I) && "Expected load or store instruction only!"); 415 auto *LI = dyn_cast<LoadInst>(Acc.getRemoteInst()); 416 if (!LI && OnlyExact) { 417 LLVM_DEBUG(dbgs() << "Underlying object read through a non-load " 418 "instruction not supported yet: " 419 << *Acc.getRemoteInst() << "\n";); 420 return false; 421 } 422 NewCopies.push_back(Acc.getRemoteInst()); 423 } 424 return true; 425 }; 426 427 auto &PI = A.getAAFor<AAPointerInfo>(QueryingAA, IRPosition::value(*Obj), 428 DepClassTy::NONE); 429 if (!PI.forallInterferingAccesses(A, QueryingAA, I, CheckAccess)) { 430 LLVM_DEBUG( 431 dbgs() 432 << "Failed to verify all interfering accesses for underlying object: " 433 << *Obj << "\n"); 434 return false; 435 } 436 PIs.push_back(&PI); 437 } 438 439 // Only if we were successful collection all potential copies we record 440 // dependences (on non-fix AAPointerInfo AAs). We also only then modify the 441 // given PotentialCopies container. 442 for (auto *PI : PIs) { 443 if (!PI->getState().isAtFixpoint()) 444 UsedAssumedInformation = true; 445 A.recordDependence(*PI, QueryingAA, DepClassTy::OPTIONAL); 446 } 447 PotentialCopies.insert(NewCopies.begin(), NewCopies.end()); 448 449 return true; 450 } 451 452 bool AA::getPotentiallyLoadedValues(Attributor &A, LoadInst &LI, 453 SmallSetVector<Value *, 4> &PotentialValues, 454 const AbstractAttribute &QueryingAA, 455 bool &UsedAssumedInformation, 456 bool OnlyExact) { 457 return getPotentialCopiesOfMemoryValue</* IsLoad */ true>( 458 A, LI, PotentialValues, QueryingAA, UsedAssumedInformation, OnlyExact); 459 } 460 461 bool AA::getPotentialCopiesOfStoredValue( 462 Attributor &A, StoreInst &SI, SmallSetVector<Value *, 4> &PotentialCopies, 463 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation, 464 bool OnlyExact) { 465 return getPotentialCopiesOfMemoryValue</* IsLoad */ false>( 466 A, SI, PotentialCopies, QueryingAA, UsedAssumedInformation, OnlyExact); 467 } 468 469 static bool isAssumedReadOnlyOrReadNone(Attributor &A, const IRPosition &IRP, 470 const AbstractAttribute &QueryingAA, 471 bool RequireReadNone, bool &IsKnown) { 472 473 IRPosition::Kind Kind = IRP.getPositionKind(); 474 if (Kind == IRPosition::IRP_FUNCTION || Kind == IRPosition::IRP_CALL_SITE) { 475 const auto &MemLocAA = 476 A.getAAFor<AAMemoryLocation>(QueryingAA, IRP, DepClassTy::NONE); 477 if (MemLocAA.isAssumedReadNone()) { 478 IsKnown = MemLocAA.isKnownReadNone(); 479 if (!IsKnown) 480 A.recordDependence(MemLocAA, QueryingAA, DepClassTy::OPTIONAL); 481 return true; 482 } 483 } 484 485 const auto &MemBehaviorAA = 486 A.getAAFor<AAMemoryBehavior>(QueryingAA, IRP, DepClassTy::NONE); 487 if (MemBehaviorAA.isAssumedReadNone() || 488 (!RequireReadNone && MemBehaviorAA.isAssumedReadOnly())) { 489 IsKnown = RequireReadNone ? MemBehaviorAA.isKnownReadNone() 490 : MemBehaviorAA.isKnownReadOnly(); 491 if (!IsKnown) 492 A.recordDependence(MemBehaviorAA, QueryingAA, DepClassTy::OPTIONAL); 493 return true; 494 } 495 496 return false; 497 } 498 499 bool AA::isAssumedReadOnly(Attributor &A, const IRPosition &IRP, 500 const AbstractAttribute &QueryingAA, bool &IsKnown) { 501 return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA, 502 /* RequireReadNone */ false, IsKnown); 503 } 504 bool AA::isAssumedReadNone(Attributor &A, const IRPosition &IRP, 505 const AbstractAttribute &QueryingAA, bool &IsKnown) { 506 return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA, 507 /* RequireReadNone */ true, IsKnown); 508 } 509 510 static bool 511 isPotentiallyReachable(Attributor &A, const Instruction &FromI, 512 const Instruction *ToI, const Function &ToFn, 513 const AbstractAttribute &QueryingAA, 514 std::function<bool(const Function &F)> GoBackwardsCB) { 515 LLVM_DEBUG(dbgs() << "[AA] isPotentiallyReachable @" << ToFn.getName() 516 << " from " << FromI << " [GBCB: " << bool(GoBackwardsCB) 517 << "]\n"); 518 519 SmallPtrSet<const Instruction *, 8> Visited; 520 SmallVector<const Instruction *> Worklist; 521 Worklist.push_back(&FromI); 522 523 while (!Worklist.empty()) { 524 const Instruction *CurFromI = Worklist.pop_back_val(); 525 if (!Visited.insert(CurFromI).second) 526 continue; 527 528 const Function *FromFn = CurFromI->getFunction(); 529 if (FromFn == &ToFn) { 530 if (!ToI) 531 return true; 532 LLVM_DEBUG(dbgs() << "[AA] check " << *ToI << " from " << *CurFromI 533 << " intraprocedurally\n"); 534 const auto &ReachabilityAA = A.getAAFor<AAReachability>( 535 QueryingAA, IRPosition::function(ToFn), DepClassTy::OPTIONAL); 536 bool Result = ReachabilityAA.isAssumedReachable(A, *CurFromI, *ToI); 537 LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " " 538 << (Result ? "can potentially " : "cannot ") << "reach " 539 << *ToI << " [Intra]\n"); 540 if (Result) 541 return true; 542 continue; 543 } 544 545 // TODO: If we can go arbitrarily backwards we will eventually reach an 546 // entry point that can reach ToI. Only once this takes a set of blocks 547 // through which we cannot go, or once we track internal functions not 548 // accessible from the outside, it makes sense to perform backwards analysis 549 // in the absence of a GoBackwardsCB. 550 if (!GoBackwardsCB) { 551 LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn.getName() << " from " 552 << *CurFromI << " is not checked backwards, abort\n"); 553 return true; 554 } 555 556 // Check if the current instruction is already known to reach the ToFn. 557 const auto &FnReachabilityAA = A.getAAFor<AAFunctionReachability>( 558 QueryingAA, IRPosition::function(*FromFn), DepClassTy::OPTIONAL); 559 bool Result = FnReachabilityAA.instructionCanReach( 560 A, *CurFromI, ToFn, /* UseBackwards */ false); 561 LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " in @" << FromFn->getName() 562 << " " << (Result ? "can potentially " : "cannot ") 563 << "reach @" << ToFn.getName() << " [FromFn]\n"); 564 if (Result) 565 return true; 566 567 // If we do not go backwards from the FromFn we are done here and so far we 568 // could not find a way to reach ToFn/ToI. 569 if (!GoBackwardsCB(*FromFn)) 570 continue; 571 572 LLVM_DEBUG(dbgs() << "Stepping backwards to the call sites of @" 573 << FromFn->getName() << "\n"); 574 575 auto CheckCallSite = [&](AbstractCallSite ACS) { 576 CallBase *CB = ACS.getInstruction(); 577 if (!CB) 578 return false; 579 580 if (isa<InvokeInst>(CB)) 581 return false; 582 583 Instruction *Inst = CB->getNextNonDebugInstruction(); 584 Worklist.push_back(Inst); 585 return true; 586 }; 587 588 bool UsedAssumedInformation = false; 589 Result = !A.checkForAllCallSites(CheckCallSite, *FromFn, 590 /* RequireAllCallSites */ true, 591 &QueryingAA, UsedAssumedInformation); 592 if (Result) { 593 LLVM_DEBUG(dbgs() << "[AA] stepping back to call sites from " << *CurFromI 594 << " in @" << FromFn->getName() 595 << " failed, give up\n"); 596 return true; 597 } 598 599 LLVM_DEBUG(dbgs() << "[AA] stepped back to call sites from " << *CurFromI 600 << " in @" << FromFn->getName() 601 << " worklist size is: " << Worklist.size() << "\n"); 602 } 603 return false; 604 } 605 606 bool AA::isPotentiallyReachable( 607 Attributor &A, const Instruction &FromI, const Instruction &ToI, 608 const AbstractAttribute &QueryingAA, 609 std::function<bool(const Function &F)> GoBackwardsCB) { 610 LLVM_DEBUG(dbgs() << "[AA] isPotentiallyReachable " << ToI << " from " 611 << FromI << " [GBCB: " << bool(GoBackwardsCB) << "]\n"); 612 const Function *ToFn = ToI.getFunction(); 613 return ::isPotentiallyReachable(A, FromI, &ToI, *ToFn, QueryingAA, 614 GoBackwardsCB); 615 } 616 617 bool AA::isPotentiallyReachable( 618 Attributor &A, const Instruction &FromI, const Function &ToFn, 619 const AbstractAttribute &QueryingAA, 620 std::function<bool(const Function &F)> GoBackwardsCB) { 621 return ::isPotentiallyReachable(A, FromI, /* ToI */ nullptr, ToFn, QueryingAA, 622 GoBackwardsCB); 623 } 624 625 /// Return true if \p New is equal or worse than \p Old. 626 static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { 627 if (!Old.isIntAttribute()) 628 return true; 629 630 return Old.getValueAsInt() >= New.getValueAsInt(); 631 } 632 633 /// Return true if the information provided by \p Attr was added to the 634 /// attribute list \p Attrs. This is only the case if it was not already present 635 /// in \p Attrs at the position describe by \p PK and \p AttrIdx. 636 static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, 637 AttributeList &Attrs, int AttrIdx, 638 bool ForceReplace = false) { 639 640 if (Attr.isEnumAttribute()) { 641 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 642 if (Attrs.hasAttributeAtIndex(AttrIdx, Kind)) 643 if (!ForceReplace && 644 isEqualOrWorse(Attr, Attrs.getAttributeAtIndex(AttrIdx, Kind))) 645 return false; 646 Attrs = Attrs.addAttributeAtIndex(Ctx, AttrIdx, Attr); 647 return true; 648 } 649 if (Attr.isStringAttribute()) { 650 StringRef Kind = Attr.getKindAsString(); 651 if (Attrs.hasAttributeAtIndex(AttrIdx, Kind)) 652 if (!ForceReplace && 653 isEqualOrWorse(Attr, Attrs.getAttributeAtIndex(AttrIdx, Kind))) 654 return false; 655 Attrs = Attrs.addAttributeAtIndex(Ctx, AttrIdx, Attr); 656 return true; 657 } 658 if (Attr.isIntAttribute()) { 659 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 660 if (Attrs.hasAttributeAtIndex(AttrIdx, Kind)) 661 if (!ForceReplace && 662 isEqualOrWorse(Attr, Attrs.getAttributeAtIndex(AttrIdx, Kind))) 663 return false; 664 Attrs = Attrs.removeAttributeAtIndex(Ctx, AttrIdx, Kind); 665 Attrs = Attrs.addAttributeAtIndex(Ctx, AttrIdx, Attr); 666 return true; 667 } 668 669 llvm_unreachable("Expected enum or string attribute!"); 670 } 671 672 Argument *IRPosition::getAssociatedArgument() const { 673 if (getPositionKind() == IRP_ARGUMENT) 674 return cast<Argument>(&getAnchorValue()); 675 676 // Not an Argument and no argument number means this is not a call site 677 // argument, thus we cannot find a callback argument to return. 678 int ArgNo = getCallSiteArgNo(); 679 if (ArgNo < 0) 680 return nullptr; 681 682 // Use abstract call sites to make the connection between the call site 683 // values and the ones in callbacks. If a callback was found that makes use 684 // of the underlying call site operand, we want the corresponding callback 685 // callee argument and not the direct callee argument. 686 Optional<Argument *> CBCandidateArg; 687 SmallVector<const Use *, 4> CallbackUses; 688 const auto &CB = cast<CallBase>(getAnchorValue()); 689 AbstractCallSite::getCallbackUses(CB, CallbackUses); 690 for (const Use *U : CallbackUses) { 691 AbstractCallSite ACS(U); 692 assert(ACS && ACS.isCallbackCall()); 693 if (!ACS.getCalledFunction()) 694 continue; 695 696 for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) { 697 698 // Test if the underlying call site operand is argument number u of the 699 // callback callee. 700 if (ACS.getCallArgOperandNo(u) != ArgNo) 701 continue; 702 703 assert(ACS.getCalledFunction()->arg_size() > u && 704 "ACS mapped into var-args arguments!"); 705 if (CBCandidateArg.hasValue()) { 706 CBCandidateArg = nullptr; 707 break; 708 } 709 CBCandidateArg = ACS.getCalledFunction()->getArg(u); 710 } 711 } 712 713 // If we found a unique callback candidate argument, return it. 714 if (CBCandidateArg.hasValue() && CBCandidateArg.getValue()) 715 return CBCandidateArg.getValue(); 716 717 // If no callbacks were found, or none used the underlying call site operand 718 // exclusively, use the direct callee argument if available. 719 const Function *Callee = CB.getCalledFunction(); 720 if (Callee && Callee->arg_size() > unsigned(ArgNo)) 721 return Callee->getArg(ArgNo); 722 723 return nullptr; 724 } 725 726 ChangeStatus AbstractAttribute::update(Attributor &A) { 727 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 728 if (getState().isAtFixpoint()) 729 return HasChanged; 730 731 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n"); 732 733 HasChanged = updateImpl(A); 734 735 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this 736 << "\n"); 737 738 return HasChanged; 739 } 740 741 ChangeStatus 742 IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP, 743 const ArrayRef<Attribute> &DeducedAttrs, 744 bool ForceReplace) { 745 Function *ScopeFn = IRP.getAnchorScope(); 746 IRPosition::Kind PK = IRP.getPositionKind(); 747 748 // In the following some generic code that will manifest attributes in 749 // DeducedAttrs if they improve the current IR. Due to the different 750 // annotation positions we use the underlying AttributeList interface. 751 752 AttributeList Attrs; 753 switch (PK) { 754 case IRPosition::IRP_INVALID: 755 case IRPosition::IRP_FLOAT: 756 return ChangeStatus::UNCHANGED; 757 case IRPosition::IRP_ARGUMENT: 758 case IRPosition::IRP_FUNCTION: 759 case IRPosition::IRP_RETURNED: 760 Attrs = ScopeFn->getAttributes(); 761 break; 762 case IRPosition::IRP_CALL_SITE: 763 case IRPosition::IRP_CALL_SITE_RETURNED: 764 case IRPosition::IRP_CALL_SITE_ARGUMENT: 765 Attrs = cast<CallBase>(IRP.getAnchorValue()).getAttributes(); 766 break; 767 } 768 769 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 770 LLVMContext &Ctx = IRP.getAnchorValue().getContext(); 771 for (const Attribute &Attr : DeducedAttrs) { 772 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx(), ForceReplace)) 773 continue; 774 775 HasChanged = ChangeStatus::CHANGED; 776 } 777 778 if (HasChanged == ChangeStatus::UNCHANGED) 779 return HasChanged; 780 781 switch (PK) { 782 case IRPosition::IRP_ARGUMENT: 783 case IRPosition::IRP_FUNCTION: 784 case IRPosition::IRP_RETURNED: 785 ScopeFn->setAttributes(Attrs); 786 break; 787 case IRPosition::IRP_CALL_SITE: 788 case IRPosition::IRP_CALL_SITE_RETURNED: 789 case IRPosition::IRP_CALL_SITE_ARGUMENT: 790 cast<CallBase>(IRP.getAnchorValue()).setAttributes(Attrs); 791 break; 792 case IRPosition::IRP_INVALID: 793 case IRPosition::IRP_FLOAT: 794 break; 795 } 796 797 return HasChanged; 798 } 799 800 const IRPosition IRPosition::EmptyKey(DenseMapInfo<void *>::getEmptyKey()); 801 const IRPosition 802 IRPosition::TombstoneKey(DenseMapInfo<void *>::getTombstoneKey()); 803 804 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) { 805 IRPositions.emplace_back(IRP); 806 807 // Helper to determine if operand bundles on a call site are benin or 808 // potentially problematic. We handle only llvm.assume for now. 809 auto CanIgnoreOperandBundles = [](const CallBase &CB) { 810 return (isa<IntrinsicInst>(CB) && 811 cast<IntrinsicInst>(CB).getIntrinsicID() == Intrinsic ::assume); 812 }; 813 814 const auto *CB = dyn_cast<CallBase>(&IRP.getAnchorValue()); 815 switch (IRP.getPositionKind()) { 816 case IRPosition::IRP_INVALID: 817 case IRPosition::IRP_FLOAT: 818 case IRPosition::IRP_FUNCTION: 819 return; 820 case IRPosition::IRP_ARGUMENT: 821 case IRPosition::IRP_RETURNED: 822 IRPositions.emplace_back(IRPosition::function(*IRP.getAnchorScope())); 823 return; 824 case IRPosition::IRP_CALL_SITE: 825 assert(CB && "Expected call site!"); 826 // TODO: We need to look at the operand bundles similar to the redirection 827 // in CallBase. 828 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) 829 if (const Function *Callee = CB->getCalledFunction()) 830 IRPositions.emplace_back(IRPosition::function(*Callee)); 831 return; 832 case IRPosition::IRP_CALL_SITE_RETURNED: 833 assert(CB && "Expected call site!"); 834 // TODO: We need to look at the operand bundles similar to the redirection 835 // in CallBase. 836 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) { 837 if (const Function *Callee = CB->getCalledFunction()) { 838 IRPositions.emplace_back(IRPosition::returned(*Callee)); 839 IRPositions.emplace_back(IRPosition::function(*Callee)); 840 for (const Argument &Arg : Callee->args()) 841 if (Arg.hasReturnedAttr()) { 842 IRPositions.emplace_back( 843 IRPosition::callsite_argument(*CB, Arg.getArgNo())); 844 IRPositions.emplace_back( 845 IRPosition::value(*CB->getArgOperand(Arg.getArgNo()))); 846 IRPositions.emplace_back(IRPosition::argument(Arg)); 847 } 848 } 849 } 850 IRPositions.emplace_back(IRPosition::callsite_function(*CB)); 851 return; 852 case IRPosition::IRP_CALL_SITE_ARGUMENT: { 853 assert(CB && "Expected call site!"); 854 // TODO: We need to look at the operand bundles similar to the redirection 855 // in CallBase. 856 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) { 857 const Function *Callee = CB->getCalledFunction(); 858 if (Callee) { 859 if (Argument *Arg = IRP.getAssociatedArgument()) 860 IRPositions.emplace_back(IRPosition::argument(*Arg)); 861 IRPositions.emplace_back(IRPosition::function(*Callee)); 862 } 863 } 864 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue())); 865 return; 866 } 867 } 868 } 869 870 bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs, 871 bool IgnoreSubsumingPositions, Attributor *A) const { 872 SmallVector<Attribute, 4> Attrs; 873 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { 874 for (Attribute::AttrKind AK : AKs) 875 if (EquivIRP.getAttrsFromIRAttr(AK, Attrs)) 876 return true; 877 // The first position returned by the SubsumingPositionIterator is 878 // always the position itself. If we ignore subsuming positions we 879 // are done after the first iteration. 880 if (IgnoreSubsumingPositions) 881 break; 882 } 883 if (A) 884 for (Attribute::AttrKind AK : AKs) 885 if (getAttrsFromAssumes(AK, Attrs, *A)) 886 return true; 887 return false; 888 } 889 890 void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs, 891 SmallVectorImpl<Attribute> &Attrs, 892 bool IgnoreSubsumingPositions, Attributor *A) const { 893 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { 894 for (Attribute::AttrKind AK : AKs) 895 EquivIRP.getAttrsFromIRAttr(AK, Attrs); 896 // The first position returned by the SubsumingPositionIterator is 897 // always the position itself. If we ignore subsuming positions we 898 // are done after the first iteration. 899 if (IgnoreSubsumingPositions) 900 break; 901 } 902 if (A) 903 for (Attribute::AttrKind AK : AKs) 904 getAttrsFromAssumes(AK, Attrs, *A); 905 } 906 907 bool IRPosition::getAttrsFromIRAttr(Attribute::AttrKind AK, 908 SmallVectorImpl<Attribute> &Attrs) const { 909 if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT) 910 return false; 911 912 AttributeList AttrList; 913 if (const auto *CB = dyn_cast<CallBase>(&getAnchorValue())) 914 AttrList = CB->getAttributes(); 915 else 916 AttrList = getAssociatedFunction()->getAttributes(); 917 918 bool HasAttr = AttrList.hasAttributeAtIndex(getAttrIdx(), AK); 919 if (HasAttr) 920 Attrs.push_back(AttrList.getAttributeAtIndex(getAttrIdx(), AK)); 921 return HasAttr; 922 } 923 924 bool IRPosition::getAttrsFromAssumes(Attribute::AttrKind AK, 925 SmallVectorImpl<Attribute> &Attrs, 926 Attributor &A) const { 927 assert(getPositionKind() != IRP_INVALID && "Did expect a valid position!"); 928 Value &AssociatedValue = getAssociatedValue(); 929 930 const Assume2KnowledgeMap &A2K = 931 A.getInfoCache().getKnowledgeMap().lookup({&AssociatedValue, AK}); 932 933 // Check if we found any potential assume use, if not we don't need to create 934 // explorer iterators. 935 if (A2K.empty()) 936 return false; 937 938 LLVMContext &Ctx = AssociatedValue.getContext(); 939 unsigned AttrsSize = Attrs.size(); 940 MustBeExecutedContextExplorer &Explorer = 941 A.getInfoCache().getMustBeExecutedContextExplorer(); 942 auto EIt = Explorer.begin(getCtxI()), EEnd = Explorer.end(getCtxI()); 943 for (auto &It : A2K) 944 if (Explorer.findInContextOf(It.first, EIt, EEnd)) 945 Attrs.push_back(Attribute::get(Ctx, AK, It.second.Max)); 946 return AttrsSize != Attrs.size(); 947 } 948 949 void IRPosition::verify() { 950 #ifdef EXPENSIVE_CHECKS 951 switch (getPositionKind()) { 952 case IRP_INVALID: 953 assert((CBContext == nullptr) && 954 "Invalid position must not have CallBaseContext!"); 955 assert(!Enc.getOpaqueValue() && 956 "Expected a nullptr for an invalid position!"); 957 return; 958 case IRP_FLOAT: 959 assert((!isa<Argument>(&getAssociatedValue())) && 960 "Expected specialized kind for argument values!"); 961 return; 962 case IRP_RETURNED: 963 assert(isa<Function>(getAsValuePtr()) && 964 "Expected function for a 'returned' position!"); 965 assert(getAsValuePtr() == &getAssociatedValue() && 966 "Associated value mismatch!"); 967 return; 968 case IRP_CALL_SITE_RETURNED: 969 assert((CBContext == nullptr) && 970 "'call site returned' position must not have CallBaseContext!"); 971 assert((isa<CallBase>(getAsValuePtr())) && 972 "Expected call base for 'call site returned' position!"); 973 assert(getAsValuePtr() == &getAssociatedValue() && 974 "Associated value mismatch!"); 975 return; 976 case IRP_CALL_SITE: 977 assert((CBContext == nullptr) && 978 "'call site function' position must not have CallBaseContext!"); 979 assert((isa<CallBase>(getAsValuePtr())) && 980 "Expected call base for 'call site function' position!"); 981 assert(getAsValuePtr() == &getAssociatedValue() && 982 "Associated value mismatch!"); 983 return; 984 case IRP_FUNCTION: 985 assert(isa<Function>(getAsValuePtr()) && 986 "Expected function for a 'function' position!"); 987 assert(getAsValuePtr() == &getAssociatedValue() && 988 "Associated value mismatch!"); 989 return; 990 case IRP_ARGUMENT: 991 assert(isa<Argument>(getAsValuePtr()) && 992 "Expected argument for a 'argument' position!"); 993 assert(getAsValuePtr() == &getAssociatedValue() && 994 "Associated value mismatch!"); 995 return; 996 case IRP_CALL_SITE_ARGUMENT: { 997 assert((CBContext == nullptr) && 998 "'call site argument' position must not have CallBaseContext!"); 999 Use *U = getAsUsePtr(); 1000 (void)U; // Silence unused variable warning. 1001 assert(U && "Expected use for a 'call site argument' position!"); 1002 assert(isa<CallBase>(U->getUser()) && 1003 "Expected call base user for a 'call site argument' position!"); 1004 assert(cast<CallBase>(U->getUser())->isArgOperand(U) && 1005 "Expected call base argument operand for a 'call site argument' " 1006 "position"); 1007 assert(cast<CallBase>(U->getUser())->getArgOperandNo(U) == 1008 unsigned(getCallSiteArgNo()) && 1009 "Argument number mismatch!"); 1010 assert(U->get() == &getAssociatedValue() && "Associated value mismatch!"); 1011 return; 1012 } 1013 } 1014 #endif 1015 } 1016 1017 Optional<Constant *> 1018 Attributor::getAssumedConstant(const IRPosition &IRP, 1019 const AbstractAttribute &AA, 1020 bool &UsedAssumedInformation) { 1021 // First check all callbacks provided by outside AAs. If any of them returns 1022 // a non-null value that is different from the associated value, or None, we 1023 // assume it's simpliied. 1024 for (auto &CB : SimplificationCallbacks.lookup(IRP)) { 1025 Optional<Value *> SimplifiedV = CB(IRP, &AA, UsedAssumedInformation); 1026 if (!SimplifiedV.hasValue()) 1027 return llvm::None; 1028 if (isa_and_nonnull<Constant>(*SimplifiedV)) 1029 return cast<Constant>(*SimplifiedV); 1030 return nullptr; 1031 } 1032 const auto &ValueSimplifyAA = 1033 getAAFor<AAValueSimplify>(AA, IRP, DepClassTy::NONE); 1034 Optional<Value *> SimplifiedV = 1035 ValueSimplifyAA.getAssumedSimplifiedValue(*this); 1036 bool IsKnown = ValueSimplifyAA.isAtFixpoint(); 1037 UsedAssumedInformation |= !IsKnown; 1038 if (!SimplifiedV.hasValue()) { 1039 recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); 1040 return llvm::None; 1041 } 1042 if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) { 1043 recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); 1044 return UndefValue::get(IRP.getAssociatedType()); 1045 } 1046 Constant *CI = dyn_cast_or_null<Constant>(SimplifiedV.getValue()); 1047 if (CI) 1048 CI = dyn_cast_or_null<Constant>( 1049 AA::getWithType(*CI, *IRP.getAssociatedType())); 1050 if (CI) 1051 recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); 1052 return CI; 1053 } 1054 1055 Optional<Value *> 1056 Attributor::getAssumedSimplified(const IRPosition &IRP, 1057 const AbstractAttribute *AA, 1058 bool &UsedAssumedInformation) { 1059 // First check all callbacks provided by outside AAs. If any of them returns 1060 // a non-null value that is different from the associated value, or None, we 1061 // assume it's simpliied. 1062 for (auto &CB : SimplificationCallbacks.lookup(IRP)) 1063 return CB(IRP, AA, UsedAssumedInformation); 1064 1065 // If no high-level/outside simplification occurred, use AAValueSimplify. 1066 const auto &ValueSimplifyAA = 1067 getOrCreateAAFor<AAValueSimplify>(IRP, AA, DepClassTy::NONE); 1068 Optional<Value *> SimplifiedV = 1069 ValueSimplifyAA.getAssumedSimplifiedValue(*this); 1070 bool IsKnown = ValueSimplifyAA.isAtFixpoint(); 1071 UsedAssumedInformation |= !IsKnown; 1072 if (!SimplifiedV.hasValue()) { 1073 if (AA) 1074 recordDependence(ValueSimplifyAA, *AA, DepClassTy::OPTIONAL); 1075 return llvm::None; 1076 } 1077 if (*SimplifiedV == nullptr) 1078 return const_cast<Value *>(&IRP.getAssociatedValue()); 1079 if (Value *SimpleV = 1080 AA::getWithType(**SimplifiedV, *IRP.getAssociatedType())) { 1081 if (AA) 1082 recordDependence(ValueSimplifyAA, *AA, DepClassTy::OPTIONAL); 1083 return SimpleV; 1084 } 1085 return const_cast<Value *>(&IRP.getAssociatedValue()); 1086 } 1087 1088 Optional<Value *> Attributor::translateArgumentToCallSiteContent( 1089 Optional<Value *> V, CallBase &CB, const AbstractAttribute &AA, 1090 bool &UsedAssumedInformation) { 1091 if (!V.hasValue()) 1092 return V; 1093 if (*V == nullptr || isa<Constant>(*V)) 1094 return V; 1095 if (auto *Arg = dyn_cast<Argument>(*V)) 1096 if (CB.getCalledFunction() == Arg->getParent()) 1097 if (!Arg->hasPointeeInMemoryValueAttr()) 1098 return getAssumedSimplified( 1099 IRPosition::callsite_argument(CB, Arg->getArgNo()), AA, 1100 UsedAssumedInformation); 1101 return nullptr; 1102 } 1103 1104 Attributor::~Attributor() { 1105 // The abstract attributes are allocated via the BumpPtrAllocator Allocator, 1106 // thus we cannot delete them. We can, and want to, destruct them though. 1107 for (auto &DepAA : DG.SyntheticRoot.Deps) { 1108 AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer()); 1109 AA->~AbstractAttribute(); 1110 } 1111 } 1112 1113 bool Attributor::isAssumedDead(const AbstractAttribute &AA, 1114 const AAIsDead *FnLivenessAA, 1115 bool &UsedAssumedInformation, 1116 bool CheckBBLivenessOnly, DepClassTy DepClass) { 1117 const IRPosition &IRP = AA.getIRPosition(); 1118 if (!Functions.count(IRP.getAnchorScope())) 1119 return false; 1120 return isAssumedDead(IRP, &AA, FnLivenessAA, UsedAssumedInformation, 1121 CheckBBLivenessOnly, DepClass); 1122 } 1123 1124 bool Attributor::isAssumedDead(const Use &U, 1125 const AbstractAttribute *QueryingAA, 1126 const AAIsDead *FnLivenessAA, 1127 bool &UsedAssumedInformation, 1128 bool CheckBBLivenessOnly, DepClassTy DepClass) { 1129 Instruction *UserI = dyn_cast<Instruction>(U.getUser()); 1130 if (!UserI) 1131 return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA, 1132 UsedAssumedInformation, CheckBBLivenessOnly, DepClass); 1133 1134 if (auto *CB = dyn_cast<CallBase>(UserI)) { 1135 // For call site argument uses we can check if the argument is 1136 // unused/dead. 1137 if (CB->isArgOperand(&U)) { 1138 const IRPosition &CSArgPos = 1139 IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U)); 1140 return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA, 1141 UsedAssumedInformation, CheckBBLivenessOnly, 1142 DepClass); 1143 } 1144 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) { 1145 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction()); 1146 return isAssumedDead(RetPos, QueryingAA, FnLivenessAA, 1147 UsedAssumedInformation, CheckBBLivenessOnly, DepClass); 1148 } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) { 1149 BasicBlock *IncomingBB = PHI->getIncomingBlock(U); 1150 return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA, 1151 UsedAssumedInformation, CheckBBLivenessOnly, DepClass); 1152 } 1153 1154 return isAssumedDead(IRPosition::inst(*UserI), QueryingAA, FnLivenessAA, 1155 UsedAssumedInformation, CheckBBLivenessOnly, DepClass); 1156 } 1157 1158 bool Attributor::isAssumedDead(const Instruction &I, 1159 const AbstractAttribute *QueryingAA, 1160 const AAIsDead *FnLivenessAA, 1161 bool &UsedAssumedInformation, 1162 bool CheckBBLivenessOnly, DepClassTy DepClass) { 1163 const IRPosition::CallBaseContext *CBCtx = 1164 QueryingAA ? QueryingAA->getCallBaseContext() : nullptr; 1165 1166 if (ManifestAddedBlocks.contains(I.getParent())) 1167 return false; 1168 1169 if (!FnLivenessAA) 1170 FnLivenessAA = 1171 lookupAAFor<AAIsDead>(IRPosition::function(*I.getFunction(), CBCtx), 1172 QueryingAA, DepClassTy::NONE); 1173 1174 // If we have a context instruction and a liveness AA we use it. 1175 if (FnLivenessAA && 1176 FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() && 1177 (CheckBBLivenessOnly ? FnLivenessAA->isAssumedDead(I.getParent()) 1178 : FnLivenessAA->isAssumedDead(&I))) { 1179 if (QueryingAA) 1180 recordDependence(*FnLivenessAA, *QueryingAA, DepClass); 1181 if (!FnLivenessAA->isKnownDead(&I)) 1182 UsedAssumedInformation = true; 1183 return true; 1184 } 1185 1186 if (CheckBBLivenessOnly) 1187 return false; 1188 1189 const IRPosition IRP = IRPosition::inst(I, CBCtx); 1190 const AAIsDead &IsDeadAA = 1191 getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE); 1192 // Don't check liveness for AAIsDead. 1193 if (QueryingAA == &IsDeadAA) 1194 return false; 1195 1196 if (IsDeadAA.isAssumedDead()) { 1197 if (QueryingAA) 1198 recordDependence(IsDeadAA, *QueryingAA, DepClass); 1199 if (!IsDeadAA.isKnownDead()) 1200 UsedAssumedInformation = true; 1201 return true; 1202 } 1203 1204 return false; 1205 } 1206 1207 bool Attributor::isAssumedDead(const IRPosition &IRP, 1208 const AbstractAttribute *QueryingAA, 1209 const AAIsDead *FnLivenessAA, 1210 bool &UsedAssumedInformation, 1211 bool CheckBBLivenessOnly, DepClassTy DepClass) { 1212 Instruction *CtxI = IRP.getCtxI(); 1213 if (CtxI && 1214 isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, UsedAssumedInformation, 1215 /* CheckBBLivenessOnly */ true, 1216 CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL)) 1217 return true; 1218 1219 if (CheckBBLivenessOnly) 1220 return false; 1221 1222 // If we haven't succeeded we query the specific liveness info for the IRP. 1223 const AAIsDead *IsDeadAA; 1224 if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE) 1225 IsDeadAA = &getOrCreateAAFor<AAIsDead>( 1226 IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())), 1227 QueryingAA, DepClassTy::NONE); 1228 else 1229 IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE); 1230 // Don't check liveness for AAIsDead. 1231 if (QueryingAA == IsDeadAA) 1232 return false; 1233 1234 if (IsDeadAA->isAssumedDead()) { 1235 if (QueryingAA) 1236 recordDependence(*IsDeadAA, *QueryingAA, DepClass); 1237 if (!IsDeadAA->isKnownDead()) 1238 UsedAssumedInformation = true; 1239 return true; 1240 } 1241 1242 return false; 1243 } 1244 1245 bool Attributor::isAssumedDead(const BasicBlock &BB, 1246 const AbstractAttribute *QueryingAA, 1247 const AAIsDead *FnLivenessAA, 1248 DepClassTy DepClass) { 1249 if (!FnLivenessAA) 1250 FnLivenessAA = lookupAAFor<AAIsDead>(IRPosition::function(*BB.getParent()), 1251 QueryingAA, DepClassTy::NONE); 1252 if (FnLivenessAA->isAssumedDead(&BB)) { 1253 if (QueryingAA) 1254 recordDependence(*FnLivenessAA, *QueryingAA, DepClass); 1255 return true; 1256 } 1257 1258 return false; 1259 } 1260 1261 bool Attributor::checkForAllUses( 1262 function_ref<bool(const Use &, bool &)> Pred, 1263 const AbstractAttribute &QueryingAA, const Value &V, 1264 bool CheckBBLivenessOnly, DepClassTy LivenessDepClass, 1265 function_ref<bool(const Use &OldU, const Use &NewU)> EquivalentUseCB) { 1266 1267 // Check the trivial case first as it catches void values. 1268 if (V.use_empty()) 1269 return true; 1270 1271 const IRPosition &IRP = QueryingAA.getIRPosition(); 1272 SmallVector<const Use *, 16> Worklist; 1273 SmallPtrSet<const Use *, 16> Visited; 1274 1275 for (const Use &U : V.uses()) 1276 Worklist.push_back(&U); 1277 1278 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size() 1279 << " initial uses to check\n"); 1280 1281 const Function *ScopeFn = IRP.getAnchorScope(); 1282 const auto *LivenessAA = 1283 ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn), 1284 DepClassTy::NONE) 1285 : nullptr; 1286 1287 while (!Worklist.empty()) { 1288 const Use *U = Worklist.pop_back_val(); 1289 if (isa<PHINode>(U->getUser()) && !Visited.insert(U).second) 1290 continue; 1291 LLVM_DEBUG({ 1292 if (auto *Fn = dyn_cast<Function>(U->getUser())) 1293 dbgs() << "[Attributor] Check use: " << **U << " in " << Fn->getName() 1294 << "\n"; 1295 else 1296 dbgs() << "[Attributor] Check use: " << **U << " in " << *U->getUser() 1297 << "\n"; 1298 }); 1299 bool UsedAssumedInformation = false; 1300 if (isAssumedDead(*U, &QueryingAA, LivenessAA, UsedAssumedInformation, 1301 CheckBBLivenessOnly, LivenessDepClass)) { 1302 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n"); 1303 continue; 1304 } 1305 if (U->getUser()->isDroppable()) { 1306 LLVM_DEBUG(dbgs() << "[Attributor] Droppable user, skip!\n"); 1307 continue; 1308 } 1309 1310 if (auto *SI = dyn_cast<StoreInst>(U->getUser())) { 1311 if (&SI->getOperandUse(0) == U) { 1312 if (!Visited.insert(U).second) 1313 continue; 1314 SmallSetVector<Value *, 4> PotentialCopies; 1315 if (AA::getPotentialCopiesOfStoredValue(*this, *SI, PotentialCopies, 1316 QueryingAA, 1317 UsedAssumedInformation)) { 1318 LLVM_DEBUG(dbgs() << "[Attributor] Value is stored, continue with " 1319 << PotentialCopies.size() 1320 << " potential copies instead!\n"); 1321 for (Value *PotentialCopy : PotentialCopies) 1322 for (const Use &CopyUse : PotentialCopy->uses()) { 1323 if (EquivalentUseCB && !EquivalentUseCB(*U, CopyUse)) { 1324 LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was " 1325 "rejected by the equivalence call back: " 1326 << *CopyUse << "!\n"); 1327 return false; 1328 } 1329 Worklist.push_back(&CopyUse); 1330 } 1331 continue; 1332 } 1333 } 1334 } 1335 1336 bool Follow = false; 1337 if (!Pred(*U, Follow)) 1338 return false; 1339 if (!Follow) 1340 continue; 1341 for (const Use &UU : U->getUser()->uses()) 1342 Worklist.push_back(&UU); 1343 } 1344 1345 return true; 1346 } 1347 1348 bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred, 1349 const AbstractAttribute &QueryingAA, 1350 bool RequireAllCallSites, 1351 bool &UsedAssumedInformation) { 1352 // We can try to determine information from 1353 // the call sites. However, this is only possible all call sites are known, 1354 // hence the function has internal linkage. 1355 const IRPosition &IRP = QueryingAA.getIRPosition(); 1356 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 1357 if (!AssociatedFunction) { 1358 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP 1359 << "\n"); 1360 return false; 1361 } 1362 1363 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites, 1364 &QueryingAA, UsedAssumedInformation); 1365 } 1366 1367 bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred, 1368 const Function &Fn, 1369 bool RequireAllCallSites, 1370 const AbstractAttribute *QueryingAA, 1371 bool &UsedAssumedInformation) { 1372 if (RequireAllCallSites && !Fn.hasLocalLinkage()) { 1373 LLVM_DEBUG( 1374 dbgs() 1375 << "[Attributor] Function " << Fn.getName() 1376 << " has no internal linkage, hence not all call sites are known\n"); 1377 return false; 1378 } 1379 1380 SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses())); 1381 for (unsigned u = 0; u < Uses.size(); ++u) { 1382 const Use &U = *Uses[u]; 1383 LLVM_DEBUG({ 1384 if (auto *Fn = dyn_cast<Function>(U)) 1385 dbgs() << "[Attributor] Check use: " << Fn->getName() << " in " 1386 << *U.getUser() << "\n"; 1387 else 1388 dbgs() << "[Attributor] Check use: " << *U << " in " << *U.getUser() 1389 << "\n"; 1390 }); 1391 if (isAssumedDead(U, QueryingAA, nullptr, UsedAssumedInformation, 1392 /* CheckBBLivenessOnly */ true)) { 1393 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n"); 1394 continue; 1395 } 1396 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) { 1397 if (CE->isCast() && CE->getType()->isPointerTy()) { 1398 LLVM_DEBUG( 1399 dbgs() << "[Attributor] Use, is constant cast expression, add " 1400 << CE->getNumUses() 1401 << " uses of that expression instead!\n"); 1402 for (const Use &CEU : CE->uses()) 1403 Uses.push_back(&CEU); 1404 continue; 1405 } 1406 } 1407 1408 AbstractCallSite ACS(&U); 1409 if (!ACS) { 1410 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName() 1411 << " has non call site use " << *U.get() << " in " 1412 << *U.getUser() << "\n"); 1413 // BlockAddress users are allowed. 1414 if (isa<BlockAddress>(U.getUser())) 1415 continue; 1416 return false; 1417 } 1418 1419 const Use *EffectiveUse = 1420 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U; 1421 if (!ACS.isCallee(EffectiveUse)) { 1422 if (!RequireAllCallSites) { 1423 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser() 1424 << " is not a call of " << Fn.getName() 1425 << ", skip use\n"); 1426 continue; 1427 } 1428 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser() 1429 << " is an invalid use of " << Fn.getName() << "\n"); 1430 return false; 1431 } 1432 1433 // Make sure the arguments that can be matched between the call site and the 1434 // callee argee on their type. It is unlikely they do not and it doesn't 1435 // make sense for all attributes to know/care about this. 1436 assert(&Fn == ACS.getCalledFunction() && "Expected known callee"); 1437 unsigned MinArgsParams = 1438 std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size()); 1439 for (unsigned u = 0; u < MinArgsParams; ++u) { 1440 Value *CSArgOp = ACS.getCallArgOperand(u); 1441 if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) { 1442 LLVM_DEBUG( 1443 dbgs() << "[Attributor] Call site / callee argument type mismatch [" 1444 << u << "@" << Fn.getName() << ": " 1445 << *Fn.getArg(u)->getType() << " vs. " 1446 << *ACS.getCallArgOperand(u)->getType() << "\n"); 1447 return false; 1448 } 1449 } 1450 1451 if (Pred(ACS)) 1452 continue; 1453 1454 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " 1455 << *ACS.getInstruction() << "\n"); 1456 return false; 1457 } 1458 1459 return true; 1460 } 1461 1462 bool Attributor::shouldPropagateCallBaseContext(const IRPosition &IRP) { 1463 // TODO: Maintain a cache of Values that are 1464 // on the pathway from a Argument to a Instruction that would effect the 1465 // liveness/return state etc. 1466 return EnableCallSiteSpecific; 1467 } 1468 1469 bool Attributor::checkForAllReturnedValuesAndReturnInsts( 1470 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred, 1471 const AbstractAttribute &QueryingAA) { 1472 1473 const IRPosition &IRP = QueryingAA.getIRPosition(); 1474 // Since we need to provide return instructions we have to have an exact 1475 // definition. 1476 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 1477 if (!AssociatedFunction) 1478 return false; 1479 1480 // If this is a call site query we use the call site specific return values 1481 // and liveness information. 1482 // TODO: use the function scope once we have call site AAReturnedValues. 1483 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 1484 const auto &AARetVal = 1485 getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED); 1486 if (!AARetVal.getState().isValidState()) 1487 return false; 1488 1489 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); 1490 } 1491 1492 bool Attributor::checkForAllReturnedValues( 1493 function_ref<bool(Value &)> Pred, const AbstractAttribute &QueryingAA) { 1494 1495 const IRPosition &IRP = QueryingAA.getIRPosition(); 1496 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 1497 if (!AssociatedFunction) 1498 return false; 1499 1500 // TODO: use the function scope once we have call site AAReturnedValues. 1501 const IRPosition &QueryIRP = IRPosition::function( 1502 *AssociatedFunction, QueryingAA.getCallBaseContext()); 1503 const auto &AARetVal = 1504 getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED); 1505 if (!AARetVal.getState().isValidState()) 1506 return false; 1507 1508 return AARetVal.checkForAllReturnedValuesAndReturnInsts( 1509 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { 1510 return Pred(RV); 1511 }); 1512 } 1513 1514 static bool checkForAllInstructionsImpl( 1515 Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap, 1516 function_ref<bool(Instruction &)> Pred, const AbstractAttribute *QueryingAA, 1517 const AAIsDead *LivenessAA, const ArrayRef<unsigned> &Opcodes, 1518 bool &UsedAssumedInformation, bool CheckBBLivenessOnly = false, 1519 bool CheckPotentiallyDead = false) { 1520 for (unsigned Opcode : Opcodes) { 1521 // Check if we have instructions with this opcode at all first. 1522 auto *Insts = OpcodeInstMap.lookup(Opcode); 1523 if (!Insts) 1524 continue; 1525 1526 for (Instruction *I : *Insts) { 1527 // Skip dead instructions. 1528 if (A && !CheckPotentiallyDead && 1529 A->isAssumedDead(IRPosition::inst(*I), QueryingAA, LivenessAA, 1530 UsedAssumedInformation, CheckBBLivenessOnly)) { 1531 LLVM_DEBUG(dbgs() << "[Attributor] Instruction " << *I 1532 << " is potentially dead, skip!\n";); 1533 continue; 1534 } 1535 1536 if (!Pred(*I)) 1537 return false; 1538 } 1539 } 1540 return true; 1541 } 1542 1543 bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred, 1544 const Function *Fn, 1545 const AbstractAttribute &QueryingAA, 1546 const ArrayRef<unsigned> &Opcodes, 1547 bool &UsedAssumedInformation, 1548 bool CheckBBLivenessOnly, 1549 bool CheckPotentiallyDead) { 1550 // Since we need to provide instructions we have to have an exact definition. 1551 if (!Fn || Fn->isDeclaration()) 1552 return false; 1553 1554 // TODO: use the function scope once we have call site AAReturnedValues. 1555 const IRPosition &QueryIRP = IRPosition::function(*Fn); 1556 const auto *LivenessAA = 1557 (CheckBBLivenessOnly || CheckPotentiallyDead) 1558 ? nullptr 1559 : &(getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE)); 1560 1561 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn); 1562 if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA, 1563 LivenessAA, Opcodes, UsedAssumedInformation, 1564 CheckBBLivenessOnly, CheckPotentiallyDead)) 1565 return false; 1566 1567 return true; 1568 } 1569 1570 bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred, 1571 const AbstractAttribute &QueryingAA, 1572 const ArrayRef<unsigned> &Opcodes, 1573 bool &UsedAssumedInformation, 1574 bool CheckBBLivenessOnly, 1575 bool CheckPotentiallyDead) { 1576 const IRPosition &IRP = QueryingAA.getIRPosition(); 1577 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 1578 return checkForAllInstructions(Pred, AssociatedFunction, QueryingAA, Opcodes, 1579 UsedAssumedInformation, CheckBBLivenessOnly, 1580 CheckPotentiallyDead); 1581 } 1582 1583 bool Attributor::checkForAllReadWriteInstructions( 1584 function_ref<bool(Instruction &)> Pred, AbstractAttribute &QueryingAA, 1585 bool &UsedAssumedInformation) { 1586 1587 const Function *AssociatedFunction = 1588 QueryingAA.getIRPosition().getAssociatedFunction(); 1589 if (!AssociatedFunction) 1590 return false; 1591 1592 // TODO: use the function scope once we have call site AAReturnedValues. 1593 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 1594 const auto &LivenessAA = 1595 getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE); 1596 1597 for (Instruction *I : 1598 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { 1599 // Skip dead instructions. 1600 if (isAssumedDead(IRPosition::inst(*I), &QueryingAA, &LivenessAA, 1601 UsedAssumedInformation)) 1602 continue; 1603 1604 if (!Pred(*I)) 1605 return false; 1606 } 1607 1608 return true; 1609 } 1610 1611 void Attributor::runTillFixpoint() { 1612 TimeTraceScope TimeScope("Attributor::runTillFixpoint"); 1613 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " 1614 << DG.SyntheticRoot.Deps.size() 1615 << " abstract attributes.\n"); 1616 1617 // Now that all abstract attributes are collected and initialized we start 1618 // the abstract analysis. 1619 1620 unsigned IterationCounter = 1; 1621 unsigned MaxFixedPointIterations; 1622 if (MaxFixpointIterations) 1623 MaxFixedPointIterations = MaxFixpointIterations.getValue(); 1624 else 1625 MaxFixedPointIterations = SetFixpointIterations; 1626 1627 SmallVector<AbstractAttribute *, 32> ChangedAAs; 1628 SetVector<AbstractAttribute *> Worklist, InvalidAAs; 1629 Worklist.insert(DG.SyntheticRoot.begin(), DG.SyntheticRoot.end()); 1630 1631 do { 1632 // Remember the size to determine new attributes. 1633 size_t NumAAs = DG.SyntheticRoot.Deps.size(); 1634 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter 1635 << ", Worklist size: " << Worklist.size() << "\n"); 1636 1637 // For invalid AAs we can fix dependent AAs that have a required dependence, 1638 // thereby folding long dependence chains in a single step without the need 1639 // to run updates. 1640 for (unsigned u = 0; u < InvalidAAs.size(); ++u) { 1641 AbstractAttribute *InvalidAA = InvalidAAs[u]; 1642 1643 // Check the dependences to fast track invalidation. 1644 LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has " 1645 << InvalidAA->Deps.size() 1646 << " required & optional dependences\n"); 1647 while (!InvalidAA->Deps.empty()) { 1648 const auto &Dep = InvalidAA->Deps.back(); 1649 InvalidAA->Deps.pop_back(); 1650 AbstractAttribute *DepAA = cast<AbstractAttribute>(Dep.getPointer()); 1651 if (Dep.getInt() == unsigned(DepClassTy::OPTIONAL)) { 1652 LLVM_DEBUG(dbgs() << " - recompute: " << *DepAA); 1653 Worklist.insert(DepAA); 1654 continue; 1655 } 1656 LLVM_DEBUG(dbgs() << " - invalidate: " << *DepAA); 1657 DepAA->getState().indicatePessimisticFixpoint(); 1658 assert(DepAA->getState().isAtFixpoint() && "Expected fixpoint state!"); 1659 if (!DepAA->getState().isValidState()) 1660 InvalidAAs.insert(DepAA); 1661 else 1662 ChangedAAs.push_back(DepAA); 1663 } 1664 } 1665 1666 // Add all abstract attributes that are potentially dependent on one that 1667 // changed to the work list. 1668 for (AbstractAttribute *ChangedAA : ChangedAAs) 1669 while (!ChangedAA->Deps.empty()) { 1670 Worklist.insert( 1671 cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer())); 1672 ChangedAA->Deps.pop_back(); 1673 } 1674 1675 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter 1676 << ", Worklist+Dependent size: " << Worklist.size() 1677 << "\n"); 1678 1679 // Reset the changed and invalid set. 1680 ChangedAAs.clear(); 1681 InvalidAAs.clear(); 1682 1683 // Update all abstract attribute in the work list and record the ones that 1684 // changed. 1685 for (AbstractAttribute *AA : Worklist) { 1686 const auto &AAState = AA->getState(); 1687 if (!AAState.isAtFixpoint()) 1688 if (updateAA(*AA) == ChangeStatus::CHANGED) 1689 ChangedAAs.push_back(AA); 1690 1691 // Use the InvalidAAs vector to propagate invalid states fast transitively 1692 // without requiring updates. 1693 if (!AAState.isValidState()) 1694 InvalidAAs.insert(AA); 1695 } 1696 1697 // Add attributes to the changed set if they have been created in the last 1698 // iteration. 1699 ChangedAAs.append(DG.SyntheticRoot.begin() + NumAAs, 1700 DG.SyntheticRoot.end()); 1701 1702 // Reset the work list and repopulate with the changed abstract attributes. 1703 // Note that dependent ones are added above. 1704 Worklist.clear(); 1705 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); 1706 Worklist.insert(QueryAAsAwaitingUpdate.begin(), 1707 QueryAAsAwaitingUpdate.end()); 1708 QueryAAsAwaitingUpdate.clear(); 1709 1710 } while (!Worklist.empty() && (IterationCounter++ < MaxFixedPointIterations || 1711 VerifyMaxFixpointIterations)); 1712 1713 if (IterationCounter > MaxFixedPointIterations && !Functions.empty()) { 1714 auto Remark = [&](OptimizationRemarkMissed ORM) { 1715 return ORM << "Attributor did not reach a fixpoint after " 1716 << ore::NV("Iterations", MaxFixedPointIterations) 1717 << " iterations."; 1718 }; 1719 Function *F = Functions.front(); 1720 emitRemark<OptimizationRemarkMissed>(F, "FixedPoint", Remark); 1721 } 1722 1723 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " 1724 << IterationCounter << "/" << MaxFixpointIterations 1725 << " iterations\n"); 1726 1727 // Reset abstract arguments not settled in a sound fixpoint by now. This 1728 // happens when we stopped the fixpoint iteration early. Note that only the 1729 // ones marked as "changed" *and* the ones transitively depending on them 1730 // need to be reverted to a pessimistic state. Others might not be in a 1731 // fixpoint state but we can use the optimistic results for them anyway. 1732 SmallPtrSet<AbstractAttribute *, 32> Visited; 1733 for (unsigned u = 0; u < ChangedAAs.size(); u++) { 1734 AbstractAttribute *ChangedAA = ChangedAAs[u]; 1735 if (!Visited.insert(ChangedAA).second) 1736 continue; 1737 1738 AbstractState &State = ChangedAA->getState(); 1739 if (!State.isAtFixpoint()) { 1740 State.indicatePessimisticFixpoint(); 1741 1742 NumAttributesTimedOut++; 1743 } 1744 1745 while (!ChangedAA->Deps.empty()) { 1746 ChangedAAs.push_back( 1747 cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer())); 1748 ChangedAA->Deps.pop_back(); 1749 } 1750 } 1751 1752 LLVM_DEBUG({ 1753 if (!Visited.empty()) 1754 dbgs() << "\n[Attributor] Finalized " << Visited.size() 1755 << " abstract attributes.\n"; 1756 }); 1757 1758 if (VerifyMaxFixpointIterations && 1759 IterationCounter != MaxFixedPointIterations) { 1760 errs() << "\n[Attributor] Fixpoint iteration done after: " 1761 << IterationCounter << "/" << MaxFixedPointIterations 1762 << " iterations\n"; 1763 llvm_unreachable("The fixpoint was not reached with exactly the number of " 1764 "specified iterations!"); 1765 } 1766 } 1767 1768 void Attributor::registerForUpdate(AbstractAttribute &AA) { 1769 assert(AA.isQueryAA() && 1770 "Non-query AAs should not be required to register for updates!"); 1771 QueryAAsAwaitingUpdate.insert(&AA); 1772 } 1773 1774 ChangeStatus Attributor::manifestAttributes() { 1775 TimeTraceScope TimeScope("Attributor::manifestAttributes"); 1776 size_t NumFinalAAs = DG.SyntheticRoot.Deps.size(); 1777 1778 unsigned NumManifested = 0; 1779 unsigned NumAtFixpoint = 0; 1780 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; 1781 for (auto &DepAA : DG.SyntheticRoot.Deps) { 1782 AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer()); 1783 AbstractState &State = AA->getState(); 1784 1785 // If there is not already a fixpoint reached, we can now take the 1786 // optimistic state. This is correct because we enforced a pessimistic one 1787 // on abstract attributes that were transitively dependent on a changed one 1788 // already above. 1789 if (!State.isAtFixpoint()) 1790 State.indicateOptimisticFixpoint(); 1791 1792 // We must not manifest Attributes that use Callbase info. 1793 if (AA->hasCallBaseContext()) 1794 continue; 1795 // If the state is invalid, we do not try to manifest it. 1796 if (!State.isValidState()) 1797 continue; 1798 1799 // Skip dead code. 1800 bool UsedAssumedInformation = false; 1801 if (isAssumedDead(*AA, nullptr, UsedAssumedInformation, 1802 /* CheckBBLivenessOnly */ true)) 1803 continue; 1804 // Check if the manifest debug counter that allows skipping manifestation of 1805 // AAs 1806 if (!DebugCounter::shouldExecute(ManifestDBGCounter)) 1807 continue; 1808 // Manifest the state and record if we changed the IR. 1809 ChangeStatus LocalChange = AA->manifest(*this); 1810 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) 1811 AA->trackStatistics(); 1812 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA 1813 << "\n"); 1814 1815 ManifestChange = ManifestChange | LocalChange; 1816 1817 NumAtFixpoint++; 1818 NumManifested += (LocalChange == ChangeStatus::CHANGED); 1819 } 1820 1821 (void)NumManifested; 1822 (void)NumAtFixpoint; 1823 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested 1824 << " arguments while " << NumAtFixpoint 1825 << " were in a valid fixpoint state\n"); 1826 1827 NumAttributesManifested += NumManifested; 1828 NumAttributesValidFixpoint += NumAtFixpoint; 1829 1830 (void)NumFinalAAs; 1831 if (NumFinalAAs != DG.SyntheticRoot.Deps.size()) { 1832 for (unsigned u = NumFinalAAs; u < DG.SyntheticRoot.Deps.size(); ++u) 1833 errs() << "Unexpected abstract attribute: " 1834 << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer()) 1835 << " :: " 1836 << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer()) 1837 ->getIRPosition() 1838 .getAssociatedValue() 1839 << "\n"; 1840 llvm_unreachable("Expected the final number of abstract attributes to " 1841 "remain unchanged!"); 1842 } 1843 return ManifestChange; 1844 } 1845 1846 void Attributor::identifyDeadInternalFunctions() { 1847 // Early exit if we don't intend to delete functions. 1848 if (!DeleteFns) 1849 return; 1850 1851 // Identify dead internal functions and delete them. This happens outside 1852 // the other fixpoint analysis as we might treat potentially dead functions 1853 // as live to lower the number of iterations. If they happen to be dead, the 1854 // below fixpoint loop will identify and eliminate them. 1855 SmallVector<Function *, 8> InternalFns; 1856 for (Function *F : Functions) 1857 if (F->hasLocalLinkage()) 1858 InternalFns.push_back(F); 1859 1860 SmallPtrSet<Function *, 8> LiveInternalFns; 1861 bool FoundLiveInternal = true; 1862 while (FoundLiveInternal) { 1863 FoundLiveInternal = false; 1864 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { 1865 Function *F = InternalFns[u]; 1866 if (!F) 1867 continue; 1868 1869 bool UsedAssumedInformation = false; 1870 if (checkForAllCallSites( 1871 [&](AbstractCallSite ACS) { 1872 Function *Callee = ACS.getInstruction()->getFunction(); 1873 return ToBeDeletedFunctions.count(Callee) || 1874 (Functions.count(Callee) && Callee->hasLocalLinkage() && 1875 !LiveInternalFns.count(Callee)); 1876 }, 1877 *F, true, nullptr, UsedAssumedInformation)) { 1878 continue; 1879 } 1880 1881 LiveInternalFns.insert(F); 1882 InternalFns[u] = nullptr; 1883 FoundLiveInternal = true; 1884 } 1885 } 1886 1887 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) 1888 if (Function *F = InternalFns[u]) 1889 ToBeDeletedFunctions.insert(F); 1890 } 1891 1892 ChangeStatus Attributor::cleanupIR() { 1893 TimeTraceScope TimeScope("Attributor::cleanupIR"); 1894 // Delete stuff at the end to avoid invalid references and a nice order. 1895 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least " 1896 << ToBeDeletedFunctions.size() << " functions and " 1897 << ToBeDeletedBlocks.size() << " blocks and " 1898 << ToBeDeletedInsts.size() << " instructions and " 1899 << ToBeChangedValues.size() << " values and " 1900 << ToBeChangedUses.size() << " uses. " 1901 << "Preserve manifest added " << ManifestAddedBlocks.size() 1902 << " blocks\n"); 1903 1904 SmallVector<WeakTrackingVH, 32> DeadInsts; 1905 SmallVector<Instruction *, 32> TerminatorsToFold; 1906 1907 auto ReplaceUse = [&](Use *U, Value *NewV) { 1908 Value *OldV = U->get(); 1909 1910 // If we plan to replace NewV we need to update it at this point. 1911 do { 1912 const auto &Entry = ToBeChangedValues.lookup(NewV); 1913 if (!Entry.first) 1914 break; 1915 NewV = Entry.first; 1916 } while (true); 1917 1918 // Do not replace uses in returns if the value is a must-tail call we will 1919 // not delete. 1920 if (auto *RI = dyn_cast<ReturnInst>(U->getUser())) { 1921 if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts())) 1922 if (CI->isMustTailCall() && 1923 (!ToBeDeletedInsts.count(CI) || !isRunOn(*CI->getCaller()))) 1924 return; 1925 // If we rewrite a return and the new value is not an argument, strip the 1926 // `returned` attribute as it is wrong now. 1927 if (!isa<Argument>(NewV)) 1928 for (auto &Arg : RI->getFunction()->args()) 1929 Arg.removeAttr(Attribute::Returned); 1930 } 1931 1932 // Do not perform call graph altering changes outside the SCC. 1933 if (auto *CB = dyn_cast<CallBase>(U->getUser())) 1934 if (CB->isCallee(U) && !isRunOn(*CB->getCaller())) 1935 return; 1936 1937 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() 1938 << " instead of " << *OldV << "\n"); 1939 U->set(NewV); 1940 1941 if (Instruction *I = dyn_cast<Instruction>(OldV)) { 1942 CGModifiedFunctions.insert(I->getFunction()); 1943 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) && 1944 isInstructionTriviallyDead(I)) 1945 DeadInsts.push_back(I); 1946 } 1947 if (isa<UndefValue>(NewV) && isa<CallBase>(U->getUser())) { 1948 auto *CB = cast<CallBase>(U->getUser()); 1949 if (CB->isArgOperand(U)) { 1950 unsigned Idx = CB->getArgOperandNo(U); 1951 CB->removeParamAttr(Idx, Attribute::NoUndef); 1952 Function *Fn = CB->getCalledFunction(); 1953 if (Fn && Fn->arg_size() > Idx) 1954 Fn->removeParamAttr(Idx, Attribute::NoUndef); 1955 } 1956 } 1957 if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) { 1958 Instruction *UserI = cast<Instruction>(U->getUser()); 1959 if (isa<UndefValue>(NewV)) { 1960 ToBeChangedToUnreachableInsts.insert(UserI); 1961 } else { 1962 TerminatorsToFold.push_back(UserI); 1963 } 1964 } 1965 }; 1966 1967 for (auto &It : ToBeChangedUses) { 1968 Use *U = It.first; 1969 Value *NewV = It.second; 1970 ReplaceUse(U, NewV); 1971 } 1972 1973 SmallVector<Use *, 4> Uses; 1974 for (auto &It : ToBeChangedValues) { 1975 Value *OldV = It.first; 1976 auto &Entry = It.second; 1977 Value *NewV = Entry.first; 1978 Uses.clear(); 1979 for (auto &U : OldV->uses()) 1980 if (Entry.second || !U.getUser()->isDroppable()) 1981 Uses.push_back(&U); 1982 for (Use *U : Uses) 1983 ReplaceUse(U, NewV); 1984 } 1985 1986 for (auto &V : InvokeWithDeadSuccessor) 1987 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) { 1988 assert(isRunOn(*II->getFunction()) && 1989 "Cannot replace an invoke outside the current SCC!"); 1990 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind); 1991 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn); 1992 bool Invoke2CallAllowed = 1993 !AAIsDead::mayCatchAsynchronousExceptions(*II->getFunction()); 1994 assert((UnwindBBIsDead || NormalBBIsDead) && 1995 "Invoke does not have dead successors!"); 1996 BasicBlock *BB = II->getParent(); 1997 BasicBlock *NormalDestBB = II->getNormalDest(); 1998 if (UnwindBBIsDead) { 1999 Instruction *NormalNextIP = &NormalDestBB->front(); 2000 if (Invoke2CallAllowed) { 2001 changeToCall(II); 2002 NormalNextIP = BB->getTerminator(); 2003 } 2004 if (NormalBBIsDead) 2005 ToBeChangedToUnreachableInsts.insert(NormalNextIP); 2006 } else { 2007 assert(NormalBBIsDead && "Broken invariant!"); 2008 if (!NormalDestBB->getUniquePredecessor()) 2009 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); 2010 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front()); 2011 } 2012 } 2013 for (Instruction *I : TerminatorsToFold) { 2014 if (!isRunOn(*I->getFunction())) 2015 continue; 2016 CGModifiedFunctions.insert(I->getFunction()); 2017 ConstantFoldTerminator(I->getParent()); 2018 } 2019 for (auto &V : ToBeChangedToUnreachableInsts) 2020 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) { 2021 if (!isRunOn(*I->getFunction())) 2022 continue; 2023 CGModifiedFunctions.insert(I->getFunction()); 2024 changeToUnreachable(I); 2025 } 2026 2027 for (auto &V : ToBeDeletedInsts) { 2028 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) { 2029 if (auto *CB = dyn_cast<CallBase>(I)) { 2030 if (!isRunOn(*I->getFunction())) 2031 continue; 2032 if (!isa<IntrinsicInst>(CB)) 2033 CGUpdater.removeCallSite(*CB); 2034 } 2035 I->dropDroppableUses(); 2036 CGModifiedFunctions.insert(I->getFunction()); 2037 if (!I->getType()->isVoidTy()) 2038 I->replaceAllUsesWith(UndefValue::get(I->getType())); 2039 if (!isa<PHINode>(I) && isInstructionTriviallyDead(I)) 2040 DeadInsts.push_back(I); 2041 else 2042 I->eraseFromParent(); 2043 } 2044 } 2045 2046 llvm::erase_if(DeadInsts, [&](WeakTrackingVH I) { 2047 return !I || !isRunOn(*cast<Instruction>(I)->getFunction()); 2048 }); 2049 2050 LLVM_DEBUG({ 2051 dbgs() << "[Attributor] DeadInsts size: " << DeadInsts.size() << "\n"; 2052 for (auto &I : DeadInsts) 2053 if (I) 2054 dbgs() << " - " << *I << "\n"; 2055 }); 2056 2057 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); 2058 2059 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { 2060 SmallVector<BasicBlock *, 8> ToBeDeletedBBs; 2061 ToBeDeletedBBs.reserve(NumDeadBlocks); 2062 for (BasicBlock *BB : ToBeDeletedBlocks) { 2063 assert(isRunOn(*BB->getParent()) && 2064 "Cannot delete a block outside the current SCC!"); 2065 CGModifiedFunctions.insert(BB->getParent()); 2066 // Do not delete BBs added during manifests of AAs. 2067 if (ManifestAddedBlocks.contains(BB)) 2068 continue; 2069 ToBeDeletedBBs.push_back(BB); 2070 } 2071 // Actually we do not delete the blocks but squash them into a single 2072 // unreachable but untangling branches that jump here is something we need 2073 // to do in a more generic way. 2074 detachDeadBlocks(ToBeDeletedBBs, nullptr); 2075 } 2076 2077 identifyDeadInternalFunctions(); 2078 2079 // Rewrite the functions as requested during manifest. 2080 ChangeStatus ManifestChange = rewriteFunctionSignatures(CGModifiedFunctions); 2081 2082 for (Function *Fn : CGModifiedFunctions) 2083 if (!ToBeDeletedFunctions.count(Fn) && Functions.count(Fn)) 2084 CGUpdater.reanalyzeFunction(*Fn); 2085 2086 for (Function *Fn : ToBeDeletedFunctions) { 2087 if (!Functions.count(Fn)) 2088 continue; 2089 CGUpdater.removeFunction(*Fn); 2090 } 2091 2092 if (!ToBeChangedUses.empty()) 2093 ManifestChange = ChangeStatus::CHANGED; 2094 2095 if (!ToBeChangedToUnreachableInsts.empty()) 2096 ManifestChange = ChangeStatus::CHANGED; 2097 2098 if (!ToBeDeletedFunctions.empty()) 2099 ManifestChange = ChangeStatus::CHANGED; 2100 2101 if (!ToBeDeletedBlocks.empty()) 2102 ManifestChange = ChangeStatus::CHANGED; 2103 2104 if (!ToBeDeletedInsts.empty()) 2105 ManifestChange = ChangeStatus::CHANGED; 2106 2107 if (!InvokeWithDeadSuccessor.empty()) 2108 ManifestChange = ChangeStatus::CHANGED; 2109 2110 if (!DeadInsts.empty()) 2111 ManifestChange = ChangeStatus::CHANGED; 2112 2113 NumFnDeleted += ToBeDeletedFunctions.size(); 2114 2115 LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions.size() 2116 << " functions after manifest.\n"); 2117 2118 #ifdef EXPENSIVE_CHECKS 2119 for (Function *F : Functions) { 2120 if (ToBeDeletedFunctions.count(F)) 2121 continue; 2122 assert(!verifyFunction(*F, &errs()) && "Module verification failed!"); 2123 } 2124 #endif 2125 2126 return ManifestChange; 2127 } 2128 2129 ChangeStatus Attributor::run() { 2130 TimeTraceScope TimeScope("Attributor::run"); 2131 AttributorCallGraph ACallGraph(*this); 2132 2133 if (PrintCallGraph) 2134 ACallGraph.populateAll(); 2135 2136 Phase = AttributorPhase::UPDATE; 2137 runTillFixpoint(); 2138 2139 // dump graphs on demand 2140 if (DumpDepGraph) 2141 DG.dumpGraph(); 2142 2143 if (ViewDepGraph) 2144 DG.viewGraph(); 2145 2146 if (PrintDependencies) 2147 DG.print(); 2148 2149 Phase = AttributorPhase::MANIFEST; 2150 ChangeStatus ManifestChange = manifestAttributes(); 2151 2152 Phase = AttributorPhase::CLEANUP; 2153 ChangeStatus CleanupChange = cleanupIR(); 2154 2155 if (PrintCallGraph) 2156 ACallGraph.print(); 2157 2158 return ManifestChange | CleanupChange; 2159 } 2160 2161 ChangeStatus Attributor::updateAA(AbstractAttribute &AA) { 2162 TimeTraceScope TimeScope( 2163 AA.getName() + std::to_string(AA.getIRPosition().getPositionKind()) + 2164 "::updateAA"); 2165 assert(Phase == AttributorPhase::UPDATE && 2166 "We can update AA only in the update stage!"); 2167 2168 // Use a new dependence vector for this update. 2169 DependenceVector DV; 2170 DependenceStack.push_back(&DV); 2171 2172 auto &AAState = AA.getState(); 2173 ChangeStatus CS = ChangeStatus::UNCHANGED; 2174 bool UsedAssumedInformation = false; 2175 if (!isAssumedDead(AA, nullptr, UsedAssumedInformation, 2176 /* CheckBBLivenessOnly */ true)) 2177 CS = AA.update(*this); 2178 2179 if (!AA.isQueryAA() && DV.empty()) { 2180 // If the attribute did not query any non-fix information, the state 2181 // will not change and we can indicate that right away. 2182 AAState.indicateOptimisticFixpoint(); 2183 } 2184 2185 if (!AAState.isAtFixpoint()) 2186 rememberDependences(); 2187 2188 // Verify the stack was used properly, that is we pop the dependence vector we 2189 // put there earlier. 2190 DependenceVector *PoppedDV = DependenceStack.pop_back_val(); 2191 (void)PoppedDV; 2192 assert(PoppedDV == &DV && "Inconsistent usage of the dependence stack!"); 2193 2194 return CS; 2195 } 2196 2197 void Attributor::createShallowWrapper(Function &F) { 2198 assert(!F.isDeclaration() && "Cannot create a wrapper around a declaration!"); 2199 2200 Module &M = *F.getParent(); 2201 LLVMContext &Ctx = M.getContext(); 2202 FunctionType *FnTy = F.getFunctionType(); 2203 2204 Function *Wrapper = 2205 Function::Create(FnTy, F.getLinkage(), F.getAddressSpace(), F.getName()); 2206 F.setName(""); // set the inside function anonymous 2207 M.getFunctionList().insert(F.getIterator(), Wrapper); 2208 2209 F.setLinkage(GlobalValue::InternalLinkage); 2210 2211 F.replaceAllUsesWith(Wrapper); 2212 assert(F.use_empty() && "Uses remained after wrapper was created!"); 2213 2214 // Move the COMDAT section to the wrapper. 2215 // TODO: Check if we need to keep it for F as well. 2216 Wrapper->setComdat(F.getComdat()); 2217 F.setComdat(nullptr); 2218 2219 // Copy all metadata and attributes but keep them on F as well. 2220 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs; 2221 F.getAllMetadata(MDs); 2222 for (auto MDIt : MDs) 2223 Wrapper->addMetadata(MDIt.first, *MDIt.second); 2224 Wrapper->setAttributes(F.getAttributes()); 2225 2226 // Create the call in the wrapper. 2227 BasicBlock *EntryBB = BasicBlock::Create(Ctx, "entry", Wrapper); 2228 2229 SmallVector<Value *, 8> Args; 2230 Argument *FArgIt = F.arg_begin(); 2231 for (Argument &Arg : Wrapper->args()) { 2232 Args.push_back(&Arg); 2233 Arg.setName((FArgIt++)->getName()); 2234 } 2235 2236 CallInst *CI = CallInst::Create(&F, Args, "", EntryBB); 2237 CI->setTailCall(true); 2238 CI->addFnAttr(Attribute::NoInline); 2239 ReturnInst::Create(Ctx, CI->getType()->isVoidTy() ? nullptr : CI, EntryBB); 2240 2241 NumFnShallowWrappersCreated++; 2242 } 2243 2244 bool Attributor::isInternalizable(Function &F) { 2245 if (F.isDeclaration() || F.hasLocalLinkage() || 2246 GlobalValue::isInterposableLinkage(F.getLinkage())) 2247 return false; 2248 return true; 2249 } 2250 2251 Function *Attributor::internalizeFunction(Function &F, bool Force) { 2252 if (!AllowDeepWrapper && !Force) 2253 return nullptr; 2254 if (!isInternalizable(F)) 2255 return nullptr; 2256 2257 SmallPtrSet<Function *, 2> FnSet = {&F}; 2258 DenseMap<Function *, Function *> InternalizedFns; 2259 internalizeFunctions(FnSet, InternalizedFns); 2260 2261 return InternalizedFns[&F]; 2262 } 2263 2264 bool Attributor::internalizeFunctions(SmallPtrSetImpl<Function *> &FnSet, 2265 DenseMap<Function *, Function *> &FnMap) { 2266 for (Function *F : FnSet) 2267 if (!Attributor::isInternalizable(*F)) 2268 return false; 2269 2270 FnMap.clear(); 2271 // Generate the internalized version of each function. 2272 for (Function *F : FnSet) { 2273 Module &M = *F->getParent(); 2274 FunctionType *FnTy = F->getFunctionType(); 2275 2276 // Create a copy of the current function 2277 Function *Copied = 2278 Function::Create(FnTy, F->getLinkage(), F->getAddressSpace(), 2279 F->getName() + ".internalized"); 2280 ValueToValueMapTy VMap; 2281 auto *NewFArgIt = Copied->arg_begin(); 2282 for (auto &Arg : F->args()) { 2283 auto ArgName = Arg.getName(); 2284 NewFArgIt->setName(ArgName); 2285 VMap[&Arg] = &(*NewFArgIt++); 2286 } 2287 SmallVector<ReturnInst *, 8> Returns; 2288 2289 // Copy the body of the original function to the new one 2290 CloneFunctionInto(Copied, F, VMap, 2291 CloneFunctionChangeType::LocalChangesOnly, Returns); 2292 2293 // Set the linakage and visibility late as CloneFunctionInto has some 2294 // implicit requirements. 2295 Copied->setVisibility(GlobalValue::DefaultVisibility); 2296 Copied->setLinkage(GlobalValue::PrivateLinkage); 2297 2298 // Copy metadata 2299 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs; 2300 F->getAllMetadata(MDs); 2301 for (auto MDIt : MDs) 2302 if (!Copied->hasMetadata()) 2303 Copied->addMetadata(MDIt.first, *MDIt.second); 2304 2305 M.getFunctionList().insert(F->getIterator(), Copied); 2306 Copied->setDSOLocal(true); 2307 FnMap[F] = Copied; 2308 } 2309 2310 // Replace all uses of the old function with the new internalized function 2311 // unless the caller is a function that was just internalized. 2312 for (Function *F : FnSet) { 2313 auto &InternalizedFn = FnMap[F]; 2314 auto IsNotInternalized = [&](Use &U) -> bool { 2315 if (auto *CB = dyn_cast<CallBase>(U.getUser())) 2316 return !FnMap.lookup(CB->getCaller()); 2317 return false; 2318 }; 2319 F->replaceUsesWithIf(InternalizedFn, IsNotInternalized); 2320 } 2321 2322 return true; 2323 } 2324 2325 bool Attributor::isValidFunctionSignatureRewrite( 2326 Argument &Arg, ArrayRef<Type *> ReplacementTypes) { 2327 2328 if (!RewriteSignatures) 2329 return false; 2330 2331 Function *Fn = Arg.getParent(); 2332 auto CallSiteCanBeChanged = [Fn](AbstractCallSite ACS) { 2333 // Forbid the call site to cast the function return type. If we need to 2334 // rewrite these functions we need to re-create a cast for the new call site 2335 // (if the old had uses). 2336 if (!ACS.getCalledFunction() || 2337 ACS.getInstruction()->getType() != 2338 ACS.getCalledFunction()->getReturnType()) 2339 return false; 2340 if (ACS.getCalledOperand()->getType() != Fn->getType()) 2341 return false; 2342 // Forbid must-tail calls for now. 2343 return !ACS.isCallbackCall() && !ACS.getInstruction()->isMustTailCall(); 2344 }; 2345 2346 // Avoid var-arg functions for now. 2347 if (Fn->isVarArg()) { 2348 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n"); 2349 return false; 2350 } 2351 2352 // Avoid functions with complicated argument passing semantics. 2353 AttributeList FnAttributeList = Fn->getAttributes(); 2354 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) || 2355 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) || 2356 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca) || 2357 FnAttributeList.hasAttrSomewhere(Attribute::Preallocated)) { 2358 LLVM_DEBUG( 2359 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n"); 2360 return false; 2361 } 2362 2363 // Avoid callbacks for now. 2364 bool UsedAssumedInformation = false; 2365 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr, 2366 UsedAssumedInformation)) { 2367 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n"); 2368 return false; 2369 } 2370 2371 auto InstPred = [](Instruction &I) { 2372 if (auto *CI = dyn_cast<CallInst>(&I)) 2373 return !CI->isMustTailCall(); 2374 return true; 2375 }; 2376 2377 // Forbid must-tail calls for now. 2378 // TODO: 2379 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn); 2380 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr, 2381 nullptr, {Instruction::Call}, 2382 UsedAssumedInformation)) { 2383 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n"); 2384 return false; 2385 } 2386 2387 return true; 2388 } 2389 2390 bool Attributor::registerFunctionSignatureRewrite( 2391 Argument &Arg, ArrayRef<Type *> ReplacementTypes, 2392 ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB, 2393 ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) { 2394 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " 2395 << Arg.getParent()->getName() << " with " 2396 << ReplacementTypes.size() << " replacements\n"); 2397 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) && 2398 "Cannot register an invalid rewrite"); 2399 2400 Function *Fn = Arg.getParent(); 2401 SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs = 2402 ArgumentReplacementMap[Fn]; 2403 if (ARIs.empty()) 2404 ARIs.resize(Fn->arg_size()); 2405 2406 // If we have a replacement already with less than or equal new arguments, 2407 // ignore this request. 2408 std::unique_ptr<ArgumentReplacementInfo> &ARI = ARIs[Arg.getArgNo()]; 2409 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) { 2410 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n"); 2411 return false; 2412 } 2413 2414 // If we have a replacement already but we like the new one better, delete 2415 // the old. 2416 ARI.reset(); 2417 2418 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " 2419 << Arg.getParent()->getName() << " with " 2420 << ReplacementTypes.size() << " replacements\n"); 2421 2422 // Remember the replacement. 2423 ARI.reset(new ArgumentReplacementInfo(*this, Arg, ReplacementTypes, 2424 std::move(CalleeRepairCB), 2425 std::move(ACSRepairCB))); 2426 2427 return true; 2428 } 2429 2430 bool Attributor::shouldSeedAttribute(AbstractAttribute &AA) { 2431 bool Result = true; 2432 #ifndef NDEBUG 2433 if (SeedAllowList.size() != 0) 2434 Result = llvm::is_contained(SeedAllowList, AA.getName()); 2435 Function *Fn = AA.getAnchorScope(); 2436 if (FunctionSeedAllowList.size() != 0 && Fn) 2437 Result &= llvm::is_contained(FunctionSeedAllowList, Fn->getName()); 2438 #endif 2439 return Result; 2440 } 2441 2442 ChangeStatus Attributor::rewriteFunctionSignatures( 2443 SmallPtrSetImpl<Function *> &ModifiedFns) { 2444 ChangeStatus Changed = ChangeStatus::UNCHANGED; 2445 2446 for (auto &It : ArgumentReplacementMap) { 2447 Function *OldFn = It.getFirst(); 2448 2449 // Deleted functions do not require rewrites. 2450 if (!Functions.count(OldFn) || ToBeDeletedFunctions.count(OldFn)) 2451 continue; 2452 2453 const SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs = 2454 It.getSecond(); 2455 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!"); 2456 2457 SmallVector<Type *, 16> NewArgumentTypes; 2458 SmallVector<AttributeSet, 16> NewArgumentAttributes; 2459 2460 // Collect replacement argument types and copy over existing attributes. 2461 AttributeList OldFnAttributeList = OldFn->getAttributes(); 2462 for (Argument &Arg : OldFn->args()) { 2463 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI = 2464 ARIs[Arg.getArgNo()]) { 2465 NewArgumentTypes.append(ARI->ReplacementTypes.begin(), 2466 ARI->ReplacementTypes.end()); 2467 NewArgumentAttributes.append(ARI->getNumReplacementArgs(), 2468 AttributeSet()); 2469 } else { 2470 NewArgumentTypes.push_back(Arg.getType()); 2471 NewArgumentAttributes.push_back( 2472 OldFnAttributeList.getParamAttrs(Arg.getArgNo())); 2473 } 2474 } 2475 2476 FunctionType *OldFnTy = OldFn->getFunctionType(); 2477 Type *RetTy = OldFnTy->getReturnType(); 2478 2479 // Construct the new function type using the new arguments types. 2480 FunctionType *NewFnTy = 2481 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg()); 2482 2483 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName() 2484 << "' from " << *OldFn->getFunctionType() << " to " 2485 << *NewFnTy << "\n"); 2486 2487 // Create the new function body and insert it into the module. 2488 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(), 2489 OldFn->getAddressSpace(), ""); 2490 Functions.insert(NewFn); 2491 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn); 2492 NewFn->takeName(OldFn); 2493 NewFn->copyAttributesFrom(OldFn); 2494 2495 // Patch the pointer to LLVM function in debug info descriptor. 2496 NewFn->setSubprogram(OldFn->getSubprogram()); 2497 OldFn->setSubprogram(nullptr); 2498 2499 // Recompute the parameter attributes list based on the new arguments for 2500 // the function. 2501 LLVMContext &Ctx = OldFn->getContext(); 2502 NewFn->setAttributes(AttributeList::get( 2503 Ctx, OldFnAttributeList.getFnAttrs(), OldFnAttributeList.getRetAttrs(), 2504 NewArgumentAttributes)); 2505 2506 // Since we have now created the new function, splice the body of the old 2507 // function right into the new function, leaving the old rotting hulk of the 2508 // function empty. 2509 NewFn->getBasicBlockList().splice(NewFn->begin(), 2510 OldFn->getBasicBlockList()); 2511 2512 // Fixup block addresses to reference new function. 2513 SmallVector<BlockAddress *, 8u> BlockAddresses; 2514 for (User *U : OldFn->users()) 2515 if (auto *BA = dyn_cast<BlockAddress>(U)) 2516 BlockAddresses.push_back(BA); 2517 for (auto *BA : BlockAddresses) 2518 BA->replaceAllUsesWith(BlockAddress::get(NewFn, BA->getBasicBlock())); 2519 2520 // Set of all "call-like" instructions that invoke the old function mapped 2521 // to their new replacements. 2522 SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs; 2523 2524 // Callback to create a new "call-like" instruction for a given one. 2525 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) { 2526 CallBase *OldCB = cast<CallBase>(ACS.getInstruction()); 2527 const AttributeList &OldCallAttributeList = OldCB->getAttributes(); 2528 2529 // Collect the new argument operands for the replacement call site. 2530 SmallVector<Value *, 16> NewArgOperands; 2531 SmallVector<AttributeSet, 16> NewArgOperandAttributes; 2532 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) { 2533 unsigned NewFirstArgNum = NewArgOperands.size(); 2534 (void)NewFirstArgNum; // only used inside assert. 2535 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI = 2536 ARIs[OldArgNum]) { 2537 if (ARI->ACSRepairCB) 2538 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands); 2539 assert(ARI->getNumReplacementArgs() + NewFirstArgNum == 2540 NewArgOperands.size() && 2541 "ACS repair callback did not provide as many operand as new " 2542 "types were registered!"); 2543 // TODO: Exose the attribute set to the ACS repair callback 2544 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(), 2545 AttributeSet()); 2546 } else { 2547 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum)); 2548 NewArgOperandAttributes.push_back( 2549 OldCallAttributeList.getParamAttrs(OldArgNum)); 2550 } 2551 } 2552 2553 assert(NewArgOperands.size() == NewArgOperandAttributes.size() && 2554 "Mismatch # argument operands vs. # argument operand attributes!"); 2555 assert(NewArgOperands.size() == NewFn->arg_size() && 2556 "Mismatch # argument operands vs. # function arguments!"); 2557 2558 SmallVector<OperandBundleDef, 4> OperandBundleDefs; 2559 OldCB->getOperandBundlesAsDefs(OperandBundleDefs); 2560 2561 // Create a new call or invoke instruction to replace the old one. 2562 CallBase *NewCB; 2563 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) { 2564 NewCB = 2565 InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(), 2566 NewArgOperands, OperandBundleDefs, "", OldCB); 2567 } else { 2568 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs, 2569 "", OldCB); 2570 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind()); 2571 NewCB = NewCI; 2572 } 2573 2574 // Copy over various properties and the new attributes. 2575 NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg}); 2576 NewCB->setCallingConv(OldCB->getCallingConv()); 2577 NewCB->takeName(OldCB); 2578 NewCB->setAttributes(AttributeList::get( 2579 Ctx, OldCallAttributeList.getFnAttrs(), 2580 OldCallAttributeList.getRetAttrs(), NewArgOperandAttributes)); 2581 2582 CallSitePairs.push_back({OldCB, NewCB}); 2583 return true; 2584 }; 2585 2586 // Use the CallSiteReplacementCreator to create replacement call sites. 2587 bool UsedAssumedInformation = false; 2588 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn, 2589 true, nullptr, UsedAssumedInformation); 2590 (void)Success; 2591 assert(Success && "Assumed call site replacement to succeed!"); 2592 2593 // Rewire the arguments. 2594 Argument *OldFnArgIt = OldFn->arg_begin(); 2595 Argument *NewFnArgIt = NewFn->arg_begin(); 2596 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); 2597 ++OldArgNum, ++OldFnArgIt) { 2598 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI = 2599 ARIs[OldArgNum]) { 2600 if (ARI->CalleeRepairCB) 2601 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt); 2602 NewFnArgIt += ARI->ReplacementTypes.size(); 2603 } else { 2604 NewFnArgIt->takeName(&*OldFnArgIt); 2605 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt); 2606 ++NewFnArgIt; 2607 } 2608 } 2609 2610 // Eliminate the instructions *after* we visited all of them. 2611 for (auto &CallSitePair : CallSitePairs) { 2612 CallBase &OldCB = *CallSitePair.first; 2613 CallBase &NewCB = *CallSitePair.second; 2614 assert(OldCB.getType() == NewCB.getType() && 2615 "Cannot handle call sites with different types!"); 2616 ModifiedFns.insert(OldCB.getFunction()); 2617 CGUpdater.replaceCallSite(OldCB, NewCB); 2618 OldCB.replaceAllUsesWith(&NewCB); 2619 OldCB.eraseFromParent(); 2620 } 2621 2622 // Replace the function in the call graph (if any). 2623 CGUpdater.replaceFunctionWith(*OldFn, *NewFn); 2624 2625 // If the old function was modified and needed to be reanalyzed, the new one 2626 // does now. 2627 if (ModifiedFns.erase(OldFn)) 2628 ModifiedFns.insert(NewFn); 2629 2630 Changed = ChangeStatus::CHANGED; 2631 } 2632 2633 return Changed; 2634 } 2635 2636 void InformationCache::initializeInformationCache(const Function &CF, 2637 FunctionInfo &FI) { 2638 // As we do not modify the function here we can remove the const 2639 // withouth breaking implicit assumptions. At the end of the day, we could 2640 // initialize the cache eagerly which would look the same to the users. 2641 Function &F = const_cast<Function &>(CF); 2642 2643 // Walk all instructions to find interesting instructions that might be 2644 // queried by abstract attributes during their initialization or update. 2645 // This has to happen before we create attributes. 2646 2647 for (Instruction &I : instructions(&F)) { 2648 bool IsInterestingOpcode = false; 2649 2650 // To allow easy access to all instructions in a function with a given 2651 // opcode we store them in the InfoCache. As not all opcodes are interesting 2652 // to concrete attributes we only cache the ones that are as identified in 2653 // the following switch. 2654 // Note: There are no concrete attributes now so this is initially empty. 2655 switch (I.getOpcode()) { 2656 default: 2657 assert(!isa<CallBase>(&I) && 2658 "New call base instruction type needs to be known in the " 2659 "Attributor."); 2660 break; 2661 case Instruction::Call: 2662 // Calls are interesting on their own, additionally: 2663 // For `llvm.assume` calls we also fill the KnowledgeMap as we find them. 2664 // For `must-tail` calls we remember the caller and callee. 2665 if (auto *Assume = dyn_cast<AssumeInst>(&I)) { 2666 fillMapFromAssume(*Assume, KnowledgeMap); 2667 } else if (cast<CallInst>(I).isMustTailCall()) { 2668 FI.ContainsMustTailCall = true; 2669 if (const Function *Callee = cast<CallInst>(I).getCalledFunction()) 2670 getFunctionInfo(*Callee).CalledViaMustTail = true; 2671 } 2672 LLVM_FALLTHROUGH; 2673 case Instruction::CallBr: 2674 case Instruction::Invoke: 2675 case Instruction::CleanupRet: 2676 case Instruction::CatchSwitch: 2677 case Instruction::AtomicRMW: 2678 case Instruction::AtomicCmpXchg: 2679 case Instruction::Br: 2680 case Instruction::Resume: 2681 case Instruction::Ret: 2682 case Instruction::Load: 2683 // The alignment of a pointer is interesting for loads. 2684 case Instruction::Store: 2685 // The alignment of a pointer is interesting for stores. 2686 case Instruction::Alloca: 2687 case Instruction::AddrSpaceCast: 2688 IsInterestingOpcode = true; 2689 } 2690 if (IsInterestingOpcode) { 2691 auto *&Insts = FI.OpcodeInstMap[I.getOpcode()]; 2692 if (!Insts) 2693 Insts = new (Allocator) InstructionVectorTy(); 2694 Insts->push_back(&I); 2695 } 2696 if (I.mayReadOrWriteMemory()) 2697 FI.RWInsts.push_back(&I); 2698 } 2699 2700 if (F.hasFnAttribute(Attribute::AlwaysInline) && 2701 isInlineViable(F).isSuccess()) 2702 InlineableFunctions.insert(&F); 2703 } 2704 2705 AAResults *InformationCache::getAAResultsForFunction(const Function &F) { 2706 return AG.getAnalysis<AAManager>(F); 2707 } 2708 2709 InformationCache::FunctionInfo::~FunctionInfo() { 2710 // The instruction vectors are allocated using a BumpPtrAllocator, we need to 2711 // manually destroy them. 2712 for (auto &It : OpcodeInstMap) 2713 It.getSecond()->~InstructionVectorTy(); 2714 } 2715 2716 void Attributor::recordDependence(const AbstractAttribute &FromAA, 2717 const AbstractAttribute &ToAA, 2718 DepClassTy DepClass) { 2719 if (DepClass == DepClassTy::NONE) 2720 return; 2721 // If we are outside of an update, thus before the actual fixpoint iteration 2722 // started (= when we create AAs), we do not track dependences because we will 2723 // put all AAs into the initial worklist anyway. 2724 if (DependenceStack.empty()) 2725 return; 2726 if (FromAA.getState().isAtFixpoint()) 2727 return; 2728 DependenceStack.back()->push_back({&FromAA, &ToAA, DepClass}); 2729 } 2730 2731 void Attributor::rememberDependences() { 2732 assert(!DependenceStack.empty() && "No dependences to remember!"); 2733 2734 for (DepInfo &DI : *DependenceStack.back()) { 2735 assert((DI.DepClass == DepClassTy::REQUIRED || 2736 DI.DepClass == DepClassTy::OPTIONAL) && 2737 "Expected required or optional dependence (1 bit)!"); 2738 auto &DepAAs = const_cast<AbstractAttribute &>(*DI.FromAA).Deps; 2739 DepAAs.push_back(AbstractAttribute::DepTy( 2740 const_cast<AbstractAttribute *>(DI.ToAA), unsigned(DI.DepClass))); 2741 } 2742 } 2743 2744 void Attributor::identifyDefaultAbstractAttributes(Function &F) { 2745 if (!VisitedFunctions.insert(&F).second) 2746 return; 2747 if (F.isDeclaration()) 2748 return; 2749 2750 // In non-module runs we need to look at the call sites of a function to 2751 // determine if it is part of a must-tail call edge. This will influence what 2752 // attributes we can derive. 2753 InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F); 2754 if (!isModulePass() && !FI.CalledViaMustTail) { 2755 for (const Use &U : F.uses()) 2756 if (const auto *CB = dyn_cast<CallBase>(U.getUser())) 2757 if (CB->isCallee(&U) && CB->isMustTailCall()) 2758 FI.CalledViaMustTail = true; 2759 } 2760 2761 IRPosition FPos = IRPosition::function(F); 2762 2763 // Check for dead BasicBlocks in every function. 2764 // We need dead instruction detection because we do not want to deal with 2765 // broken IR in which SSA rules do not apply. 2766 getOrCreateAAFor<AAIsDead>(FPos); 2767 2768 // Every function might be "will-return". 2769 getOrCreateAAFor<AAWillReturn>(FPos); 2770 2771 // Every function might contain instructions that cause "undefined behavior". 2772 getOrCreateAAFor<AAUndefinedBehavior>(FPos); 2773 2774 // Every function can be nounwind. 2775 getOrCreateAAFor<AANoUnwind>(FPos); 2776 2777 // Every function might be marked "nosync" 2778 getOrCreateAAFor<AANoSync>(FPos); 2779 2780 // Every function might be "no-free". 2781 getOrCreateAAFor<AANoFree>(FPos); 2782 2783 // Every function might be "no-return". 2784 getOrCreateAAFor<AANoReturn>(FPos); 2785 2786 // Every function might be "no-recurse". 2787 getOrCreateAAFor<AANoRecurse>(FPos); 2788 2789 // Every function might be "readnone/readonly/writeonly/...". 2790 getOrCreateAAFor<AAMemoryBehavior>(FPos); 2791 2792 // Every function can be "readnone/argmemonly/inaccessiblememonly/...". 2793 getOrCreateAAFor<AAMemoryLocation>(FPos); 2794 2795 // Every function can track active assumptions. 2796 getOrCreateAAFor<AAAssumptionInfo>(FPos); 2797 2798 // Every function might be applicable for Heap-To-Stack conversion. 2799 if (EnableHeapToStack) 2800 getOrCreateAAFor<AAHeapToStack>(FPos); 2801 2802 // Return attributes are only appropriate if the return type is non void. 2803 Type *ReturnType = F.getReturnType(); 2804 if (!ReturnType->isVoidTy()) { 2805 // Argument attribute "returned" --- Create only one per function even 2806 // though it is an argument attribute. 2807 getOrCreateAAFor<AAReturnedValues>(FPos); 2808 2809 IRPosition RetPos = IRPosition::returned(F); 2810 2811 // Every returned value might be dead. 2812 getOrCreateAAFor<AAIsDead>(RetPos); 2813 2814 // Every function might be simplified. 2815 getOrCreateAAFor<AAValueSimplify>(RetPos); 2816 2817 // Every returned value might be marked noundef. 2818 getOrCreateAAFor<AANoUndef>(RetPos); 2819 2820 if (ReturnType->isPointerTy()) { 2821 2822 // Every function with pointer return type might be marked align. 2823 getOrCreateAAFor<AAAlign>(RetPos); 2824 2825 // Every function with pointer return type might be marked nonnull. 2826 getOrCreateAAFor<AANonNull>(RetPos); 2827 2828 // Every function with pointer return type might be marked noalias. 2829 getOrCreateAAFor<AANoAlias>(RetPos); 2830 2831 // Every function with pointer return type might be marked 2832 // dereferenceable. 2833 getOrCreateAAFor<AADereferenceable>(RetPos); 2834 } 2835 } 2836 2837 for (Argument &Arg : F.args()) { 2838 IRPosition ArgPos = IRPosition::argument(Arg); 2839 2840 // Every argument might be simplified. We have to go through the Attributor 2841 // interface though as outside AAs can register custom simplification 2842 // callbacks. 2843 bool UsedAssumedInformation = false; 2844 getAssumedSimplified(ArgPos, /* AA */ nullptr, UsedAssumedInformation); 2845 2846 // Every argument might be dead. 2847 getOrCreateAAFor<AAIsDead>(ArgPos); 2848 2849 // Every argument might be marked noundef. 2850 getOrCreateAAFor<AANoUndef>(ArgPos); 2851 2852 if (Arg.getType()->isPointerTy()) { 2853 // Every argument with pointer type might be marked nonnull. 2854 getOrCreateAAFor<AANonNull>(ArgPos); 2855 2856 // Every argument with pointer type might be marked noalias. 2857 getOrCreateAAFor<AANoAlias>(ArgPos); 2858 2859 // Every argument with pointer type might be marked dereferenceable. 2860 getOrCreateAAFor<AADereferenceable>(ArgPos); 2861 2862 // Every argument with pointer type might be marked align. 2863 getOrCreateAAFor<AAAlign>(ArgPos); 2864 2865 // Every argument with pointer type might be marked nocapture. 2866 getOrCreateAAFor<AANoCapture>(ArgPos); 2867 2868 // Every argument with pointer type might be marked 2869 // "readnone/readonly/writeonly/..." 2870 getOrCreateAAFor<AAMemoryBehavior>(ArgPos); 2871 2872 // Every argument with pointer type might be marked nofree. 2873 getOrCreateAAFor<AANoFree>(ArgPos); 2874 2875 // Every argument with pointer type might be privatizable (or promotable) 2876 getOrCreateAAFor<AAPrivatizablePtr>(ArgPos); 2877 } 2878 } 2879 2880 auto CallSitePred = [&](Instruction &I) -> bool { 2881 auto &CB = cast<CallBase>(I); 2882 IRPosition CBInstPos = IRPosition::inst(CB); 2883 IRPosition CBFnPos = IRPosition::callsite_function(CB); 2884 2885 // Call sites might be dead if they do not have side effects and no live 2886 // users. The return value might be dead if there are no live users. 2887 getOrCreateAAFor<AAIsDead>(CBInstPos); 2888 2889 Function *Callee = CB.getCalledFunction(); 2890 // TODO: Even if the callee is not known now we might be able to simplify 2891 // the call/callee. 2892 if (!Callee) 2893 return true; 2894 2895 // Every call site can track active assumptions. 2896 getOrCreateAAFor<AAAssumptionInfo>(CBFnPos); 2897 2898 // Skip declarations except if annotations on their call sites were 2899 // explicitly requested. 2900 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() && 2901 !Callee->hasMetadata(LLVMContext::MD_callback)) 2902 return true; 2903 2904 if (!Callee->getReturnType()->isVoidTy() && !CB.use_empty()) { 2905 2906 IRPosition CBRetPos = IRPosition::callsite_returned(CB); 2907 getOrCreateAAFor<AAValueSimplify>(CBRetPos); 2908 } 2909 2910 for (int I = 0, E = CB.arg_size(); I < E; ++I) { 2911 2912 IRPosition CBArgPos = IRPosition::callsite_argument(CB, I); 2913 2914 // Every call site argument might be dead. 2915 getOrCreateAAFor<AAIsDead>(CBArgPos); 2916 2917 // Call site argument might be simplified. We have to go through the 2918 // Attributor interface though as outside AAs can register custom 2919 // simplification callbacks. 2920 bool UsedAssumedInformation = false; 2921 getAssumedSimplified(CBArgPos, /* AA */ nullptr, UsedAssumedInformation); 2922 2923 // Every call site argument might be marked "noundef". 2924 getOrCreateAAFor<AANoUndef>(CBArgPos); 2925 2926 if (!CB.getArgOperand(I)->getType()->isPointerTy()) 2927 continue; 2928 2929 // Call site argument attribute "non-null". 2930 getOrCreateAAFor<AANonNull>(CBArgPos); 2931 2932 // Call site argument attribute "nocapture". 2933 getOrCreateAAFor<AANoCapture>(CBArgPos); 2934 2935 // Call site argument attribute "no-alias". 2936 getOrCreateAAFor<AANoAlias>(CBArgPos); 2937 2938 // Call site argument attribute "dereferenceable". 2939 getOrCreateAAFor<AADereferenceable>(CBArgPos); 2940 2941 // Call site argument attribute "align". 2942 getOrCreateAAFor<AAAlign>(CBArgPos); 2943 2944 // Call site argument attribute 2945 // "readnone/readonly/writeonly/..." 2946 getOrCreateAAFor<AAMemoryBehavior>(CBArgPos); 2947 2948 // Call site argument attribute "nofree". 2949 getOrCreateAAFor<AANoFree>(CBArgPos); 2950 } 2951 return true; 2952 }; 2953 2954 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); 2955 bool Success; 2956 bool UsedAssumedInformation = false; 2957 Success = checkForAllInstructionsImpl( 2958 nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr, 2959 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, 2960 (unsigned)Instruction::Call}, 2961 UsedAssumedInformation); 2962 (void)Success; 2963 assert(Success && "Expected the check call to be successful!"); 2964 2965 auto LoadStorePred = [&](Instruction &I) -> bool { 2966 if (isa<LoadInst>(I)) { 2967 getOrCreateAAFor<AAAlign>( 2968 IRPosition::value(*cast<LoadInst>(I).getPointerOperand())); 2969 if (SimplifyAllLoads) 2970 getOrCreateAAFor<AAValueSimplify>(IRPosition::value(I)); 2971 } else 2972 getOrCreateAAFor<AAAlign>( 2973 IRPosition::value(*cast<StoreInst>(I).getPointerOperand())); 2974 return true; 2975 }; 2976 Success = checkForAllInstructionsImpl( 2977 nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr, 2978 {(unsigned)Instruction::Load, (unsigned)Instruction::Store}, 2979 UsedAssumedInformation); 2980 (void)Success; 2981 assert(Success && "Expected the check call to be successful!"); 2982 } 2983 2984 /// Helpers to ease debugging through output streams and print calls. 2985 /// 2986 ///{ 2987 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { 2988 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); 2989 } 2990 2991 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { 2992 switch (AP) { 2993 case IRPosition::IRP_INVALID: 2994 return OS << "inv"; 2995 case IRPosition::IRP_FLOAT: 2996 return OS << "flt"; 2997 case IRPosition::IRP_RETURNED: 2998 return OS << "fn_ret"; 2999 case IRPosition::IRP_CALL_SITE_RETURNED: 3000 return OS << "cs_ret"; 3001 case IRPosition::IRP_FUNCTION: 3002 return OS << "fn"; 3003 case IRPosition::IRP_CALL_SITE: 3004 return OS << "cs"; 3005 case IRPosition::IRP_ARGUMENT: 3006 return OS << "arg"; 3007 case IRPosition::IRP_CALL_SITE_ARGUMENT: 3008 return OS << "cs_arg"; 3009 } 3010 llvm_unreachable("Unknown attribute position!"); 3011 } 3012 3013 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { 3014 const Value &AV = Pos.getAssociatedValue(); 3015 OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" 3016 << Pos.getAnchorValue().getName() << "@" << Pos.getCallSiteArgNo() << "]"; 3017 3018 if (Pos.hasCallBaseContext()) 3019 OS << "[cb_context:" << *Pos.getCallBaseContext() << "]"; 3020 return OS << "}"; 3021 } 3022 3023 raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) { 3024 OS << "range-state(" << S.getBitWidth() << ")<"; 3025 S.getKnown().print(OS); 3026 OS << " / "; 3027 S.getAssumed().print(OS); 3028 OS << ">"; 3029 3030 return OS << static_cast<const AbstractState &>(S); 3031 } 3032 3033 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { 3034 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); 3035 } 3036 3037 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { 3038 AA.print(OS); 3039 return OS; 3040 } 3041 3042 raw_ostream &llvm::operator<<(raw_ostream &OS, 3043 const PotentialConstantIntValuesState &S) { 3044 OS << "set-state(< {"; 3045 if (!S.isValidState()) 3046 OS << "full-set"; 3047 else { 3048 for (auto &it : S.getAssumedSet()) 3049 OS << it << ", "; 3050 if (S.undefIsContained()) 3051 OS << "undef "; 3052 } 3053 OS << "} >)"; 3054 3055 return OS; 3056 } 3057 3058 void AbstractAttribute::print(raw_ostream &OS) const { 3059 OS << "["; 3060 OS << getName(); 3061 OS << "] for CtxI "; 3062 3063 if (auto *I = getCtxI()) { 3064 OS << "'"; 3065 I->print(OS); 3066 OS << "'"; 3067 } else 3068 OS << "<<null inst>>"; 3069 3070 OS << " at position " << getIRPosition() << " with state " << getAsStr() 3071 << '\n'; 3072 } 3073 3074 void AbstractAttribute::printWithDeps(raw_ostream &OS) const { 3075 print(OS); 3076 3077 for (const auto &DepAA : Deps) { 3078 auto *AA = DepAA.getPointer(); 3079 OS << " updates "; 3080 AA->print(OS); 3081 } 3082 3083 OS << '\n'; 3084 } 3085 3086 raw_ostream &llvm::operator<<(raw_ostream &OS, 3087 const AAPointerInfo::Access &Acc) { 3088 OS << " [" << Acc.getKind() << "] " << *Acc.getRemoteInst(); 3089 if (Acc.getLocalInst() != Acc.getRemoteInst()) 3090 OS << " via " << *Acc.getLocalInst(); 3091 if (Acc.getContent().hasValue()) 3092 OS << " [" << *Acc.getContent() << "]"; 3093 return OS; 3094 } 3095 ///} 3096 3097 /// ---------------------------------------------------------------------------- 3098 /// Pass (Manager) Boilerplate 3099 /// ---------------------------------------------------------------------------- 3100 3101 static bool runAttributorOnFunctions(InformationCache &InfoCache, 3102 SetVector<Function *> &Functions, 3103 AnalysisGetter &AG, 3104 CallGraphUpdater &CGUpdater, 3105 bool DeleteFns) { 3106 if (Functions.empty()) 3107 return false; 3108 3109 LLVM_DEBUG({ 3110 dbgs() << "[Attributor] Run on module with " << Functions.size() 3111 << " functions:\n"; 3112 for (Function *Fn : Functions) 3113 dbgs() << " - " << Fn->getName() << "\n"; 3114 }); 3115 3116 // Create an Attributor and initially empty information cache that is filled 3117 // while we identify default attribute opportunities. 3118 Attributor A(Functions, InfoCache, CGUpdater, /* Allowed */ nullptr, 3119 DeleteFns); 3120 3121 // Create shallow wrappers for all functions that are not IPO amendable 3122 if (AllowShallowWrappers) 3123 for (Function *F : Functions) 3124 if (!A.isFunctionIPOAmendable(*F)) 3125 Attributor::createShallowWrapper(*F); 3126 3127 // Internalize non-exact functions 3128 // TODO: for now we eagerly internalize functions without calculating the 3129 // cost, we need a cost interface to determine whether internalizing 3130 // a function is "benefitial" 3131 if (AllowDeepWrapper) { 3132 unsigned FunSize = Functions.size(); 3133 for (unsigned u = 0; u < FunSize; u++) { 3134 Function *F = Functions[u]; 3135 if (!F->isDeclaration() && !F->isDefinitionExact() && F->getNumUses() && 3136 !GlobalValue::isInterposableLinkage(F->getLinkage())) { 3137 Function *NewF = Attributor::internalizeFunction(*F); 3138 assert(NewF && "Could not internalize function."); 3139 Functions.insert(NewF); 3140 3141 // Update call graph 3142 CGUpdater.replaceFunctionWith(*F, *NewF); 3143 for (const Use &U : NewF->uses()) 3144 if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) { 3145 auto *CallerF = CB->getCaller(); 3146 CGUpdater.reanalyzeFunction(*CallerF); 3147 } 3148 } 3149 } 3150 } 3151 3152 for (Function *F : Functions) { 3153 if (F->hasExactDefinition()) 3154 NumFnWithExactDefinition++; 3155 else 3156 NumFnWithoutExactDefinition++; 3157 3158 // We look at internal functions only on-demand but if any use is not a 3159 // direct call or outside the current set of analyzed functions, we have 3160 // to do it eagerly. 3161 if (F->hasLocalLinkage()) { 3162 if (llvm::all_of(F->uses(), [&Functions](const Use &U) { 3163 const auto *CB = dyn_cast<CallBase>(U.getUser()); 3164 return CB && CB->isCallee(&U) && 3165 Functions.count(const_cast<Function *>(CB->getCaller())); 3166 })) 3167 continue; 3168 } 3169 3170 // Populate the Attributor with abstract attribute opportunities in the 3171 // function and the information cache with IR information. 3172 A.identifyDefaultAbstractAttributes(*F); 3173 } 3174 3175 ChangeStatus Changed = A.run(); 3176 3177 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size() 3178 << " functions, result: " << Changed << ".\n"); 3179 return Changed == ChangeStatus::CHANGED; 3180 } 3181 3182 void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); } 3183 3184 void AADepGraph::dumpGraph() { 3185 static std::atomic<int> CallTimes; 3186 std::string Prefix; 3187 3188 if (!DepGraphDotFileNamePrefix.empty()) 3189 Prefix = DepGraphDotFileNamePrefix; 3190 else 3191 Prefix = "dep_graph"; 3192 std::string Filename = 3193 Prefix + "_" + std::to_string(CallTimes.load()) + ".dot"; 3194 3195 outs() << "Dependency graph dump to " << Filename << ".\n"; 3196 3197 std::error_code EC; 3198 3199 raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF); 3200 if (!EC) 3201 llvm::WriteGraph(File, this); 3202 3203 CallTimes++; 3204 } 3205 3206 void AADepGraph::print() { 3207 for (auto DepAA : SyntheticRoot.Deps) 3208 cast<AbstractAttribute>(DepAA.getPointer())->printWithDeps(outs()); 3209 } 3210 3211 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { 3212 FunctionAnalysisManager &FAM = 3213 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 3214 AnalysisGetter AG(FAM); 3215 3216 SetVector<Function *> Functions; 3217 for (Function &F : M) 3218 Functions.insert(&F); 3219 3220 CallGraphUpdater CGUpdater; 3221 BumpPtrAllocator Allocator; 3222 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr); 3223 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater, 3224 /* DeleteFns */ true)) { 3225 // FIXME: Think about passes we will preserve and add them here. 3226 return PreservedAnalyses::none(); 3227 } 3228 return PreservedAnalyses::all(); 3229 } 3230 3231 PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C, 3232 CGSCCAnalysisManager &AM, 3233 LazyCallGraph &CG, 3234 CGSCCUpdateResult &UR) { 3235 FunctionAnalysisManager &FAM = 3236 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager(); 3237 AnalysisGetter AG(FAM); 3238 3239 SetVector<Function *> Functions; 3240 for (LazyCallGraph::Node &N : C) 3241 Functions.insert(&N.getFunction()); 3242 3243 if (Functions.empty()) 3244 return PreservedAnalyses::all(); 3245 3246 Module &M = *Functions.back()->getParent(); 3247 CallGraphUpdater CGUpdater; 3248 CGUpdater.initialize(CG, C, AM, UR); 3249 BumpPtrAllocator Allocator; 3250 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions); 3251 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater, 3252 /* DeleteFns */ false)) { 3253 // FIXME: Think about passes we will preserve and add them here. 3254 PreservedAnalyses PA; 3255 PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 3256 return PA; 3257 } 3258 return PreservedAnalyses::all(); 3259 } 3260 3261 namespace llvm { 3262 3263 template <> struct GraphTraits<AADepGraphNode *> { 3264 using NodeRef = AADepGraphNode *; 3265 using DepTy = PointerIntPair<AADepGraphNode *, 1>; 3266 using EdgeRef = PointerIntPair<AADepGraphNode *, 1>; 3267 3268 static NodeRef getEntryNode(AADepGraphNode *DGN) { return DGN; } 3269 static NodeRef DepGetVal(DepTy &DT) { return DT.getPointer(); } 3270 3271 using ChildIteratorType = 3272 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>; 3273 using ChildEdgeIteratorType = TinyPtrVector<DepTy>::iterator; 3274 3275 static ChildIteratorType child_begin(NodeRef N) { return N->child_begin(); } 3276 3277 static ChildIteratorType child_end(NodeRef N) { return N->child_end(); } 3278 }; 3279 3280 template <> 3281 struct GraphTraits<AADepGraph *> : public GraphTraits<AADepGraphNode *> { 3282 static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); } 3283 3284 using nodes_iterator = 3285 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>; 3286 3287 static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); } 3288 3289 static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); } 3290 }; 3291 3292 template <> struct DOTGraphTraits<AADepGraph *> : public DefaultDOTGraphTraits { 3293 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} 3294 3295 static std::string getNodeLabel(const AADepGraphNode *Node, 3296 const AADepGraph *DG) { 3297 std::string AAString; 3298 raw_string_ostream O(AAString); 3299 Node->print(O); 3300 return AAString; 3301 } 3302 }; 3303 3304 } // end namespace llvm 3305 3306 namespace { 3307 3308 struct AttributorLegacyPass : public ModulePass { 3309 static char ID; 3310 3311 AttributorLegacyPass() : ModulePass(ID) { 3312 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); 3313 } 3314 3315 bool runOnModule(Module &M) override { 3316 if (skipModule(M)) 3317 return false; 3318 3319 AnalysisGetter AG; 3320 SetVector<Function *> Functions; 3321 for (Function &F : M) 3322 Functions.insert(&F); 3323 3324 CallGraphUpdater CGUpdater; 3325 BumpPtrAllocator Allocator; 3326 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr); 3327 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater, 3328 /* DeleteFns*/ true); 3329 } 3330 3331 void getAnalysisUsage(AnalysisUsage &AU) const override { 3332 // FIXME: Think about passes we will preserve and add them here. 3333 AU.addRequired<TargetLibraryInfoWrapperPass>(); 3334 } 3335 }; 3336 3337 struct AttributorCGSCCLegacyPass : public CallGraphSCCPass { 3338 static char ID; 3339 3340 AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) { 3341 initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry()); 3342 } 3343 3344 bool runOnSCC(CallGraphSCC &SCC) override { 3345 if (skipSCC(SCC)) 3346 return false; 3347 3348 SetVector<Function *> Functions; 3349 for (CallGraphNode *CGN : SCC) 3350 if (Function *Fn = CGN->getFunction()) 3351 if (!Fn->isDeclaration()) 3352 Functions.insert(Fn); 3353 3354 if (Functions.empty()) 3355 return false; 3356 3357 AnalysisGetter AG; 3358 CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph()); 3359 CallGraphUpdater CGUpdater; 3360 CGUpdater.initialize(CG, SCC); 3361 Module &M = *Functions.back()->getParent(); 3362 BumpPtrAllocator Allocator; 3363 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions); 3364 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater, 3365 /* DeleteFns */ false); 3366 } 3367 3368 void getAnalysisUsage(AnalysisUsage &AU) const override { 3369 // FIXME: Think about passes we will preserve and add them here. 3370 AU.addRequired<TargetLibraryInfoWrapperPass>(); 3371 CallGraphSCCPass::getAnalysisUsage(AU); 3372 } 3373 }; 3374 3375 } // end anonymous namespace 3376 3377 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } 3378 Pass *llvm::createAttributorCGSCCLegacyPass() { 3379 return new AttributorCGSCCLegacyPass(); 3380 } 3381 3382 char AttributorLegacyPass::ID = 0; 3383 char AttributorCGSCCLegacyPass::ID = 0; 3384 3385 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", 3386 "Deduce and propagate attributes", false, false) 3387 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 3388 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", 3389 "Deduce and propagate attributes", false, false) 3390 INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc", 3391 "Deduce and propagate attributes (CGSCC pass)", false, 3392 false) 3393 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 3394 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 3395 INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc", 3396 "Deduce and propagate attributes (CGSCC pass)", false, 3397 false) 3398