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