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