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 (isAllocationFn(&Obj, TLI)) 226 return getInitialValueOfAllocation(&cast<CallBase>(Obj), TLI, &Ty); 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.hasValue()) 301 return A; 302 if (*B == nullptr) 303 return nullptr; 304 if (!A.hasValue()) 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.hasValue()) { 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.hasValue() && 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.hasValue()) 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.hasValue()) { 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.hasValue()) { 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.hasValue()) 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 || FnLivenessAA->getAnchorScope() != I.getFunction()) 1192 FnLivenessAA = &getOrCreateAAFor<AAIsDead>( 1193 IRPosition::function(*I.getFunction(), CBCtx), QueryingAA, 1194 DepClassTy::NONE); 1195 1196 // If we have a context instruction and a liveness AA we use it. 1197 if (CheckBBLivenessOnly ? FnLivenessAA->isAssumedDead(I.getParent()) 1198 : FnLivenessAA->isAssumedDead(&I)) { 1199 if (QueryingAA) 1200 recordDependence(*FnLivenessAA, *QueryingAA, DepClass); 1201 if (!FnLivenessAA->isKnownDead(&I)) 1202 UsedAssumedInformation = true; 1203 return true; 1204 } 1205 1206 if (CheckBBLivenessOnly) 1207 return false; 1208 1209 const IRPosition IRP = IRPosition::inst(I, CBCtx); 1210 const AAIsDead &IsDeadAA = 1211 getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE); 1212 // Don't check liveness for AAIsDead. 1213 if (QueryingAA == &IsDeadAA) 1214 return false; 1215 1216 if (IsDeadAA.isAssumedDead()) { 1217 if (QueryingAA) 1218 recordDependence(IsDeadAA, *QueryingAA, DepClass); 1219 if (!IsDeadAA.isKnownDead()) 1220 UsedAssumedInformation = true; 1221 return true; 1222 } 1223 1224 return false; 1225 } 1226 1227 bool Attributor::isAssumedDead(const IRPosition &IRP, 1228 const AbstractAttribute *QueryingAA, 1229 const AAIsDead *FnLivenessAA, 1230 bool &UsedAssumedInformation, 1231 bool CheckBBLivenessOnly, DepClassTy DepClass) { 1232 Instruction *CtxI = IRP.getCtxI(); 1233 if (CtxI && 1234 isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, UsedAssumedInformation, 1235 /* CheckBBLivenessOnly */ true, 1236 CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL)) 1237 return true; 1238 1239 if (CheckBBLivenessOnly) 1240 return false; 1241 1242 // If we haven't succeeded we query the specific liveness info for the IRP. 1243 const AAIsDead *IsDeadAA; 1244 if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE) 1245 IsDeadAA = &getOrCreateAAFor<AAIsDead>( 1246 IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())), 1247 QueryingAA, DepClassTy::NONE); 1248 else 1249 IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE); 1250 // Don't check liveness for AAIsDead. 1251 if (QueryingAA == IsDeadAA) 1252 return false; 1253 1254 if (IsDeadAA->isAssumedDead()) { 1255 if (QueryingAA) 1256 recordDependence(*IsDeadAA, *QueryingAA, DepClass); 1257 if (!IsDeadAA->isKnownDead()) 1258 UsedAssumedInformation = true; 1259 return true; 1260 } 1261 1262 return false; 1263 } 1264 1265 bool Attributor::isAssumedDead(const BasicBlock &BB, 1266 const AbstractAttribute *QueryingAA, 1267 const AAIsDead *FnLivenessAA, 1268 DepClassTy DepClass) { 1269 if (!FnLivenessAA || FnLivenessAA->getAnchorScope() != BB.getParent()) 1270 FnLivenessAA = &getOrCreateAAFor<AAIsDead>( 1271 IRPosition::function(*BB.getParent()), QueryingAA, DepClassTy::NONE); 1272 if (FnLivenessAA->isAssumedDead(&BB)) { 1273 if (QueryingAA) 1274 recordDependence(*FnLivenessAA, *QueryingAA, DepClass); 1275 return true; 1276 } 1277 1278 return false; 1279 } 1280 1281 bool Attributor::checkForAllUses( 1282 function_ref<bool(const Use &, bool &)> Pred, 1283 const AbstractAttribute &QueryingAA, const Value &V, 1284 bool CheckBBLivenessOnly, DepClassTy LivenessDepClass, 1285 bool IgnoreDroppableUses, 1286 function_ref<bool(const Use &OldU, const Use &NewU)> EquivalentUseCB) { 1287 1288 // Check the trivial case first as it catches void values. 1289 if (V.use_empty()) 1290 return true; 1291 1292 const IRPosition &IRP = QueryingAA.getIRPosition(); 1293 SmallVector<const Use *, 16> Worklist; 1294 SmallPtrSet<const Use *, 16> Visited; 1295 1296 for (const Use &U : V.uses()) 1297 Worklist.push_back(&U); 1298 1299 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size() 1300 << " initial uses to check\n"); 1301 1302 const Function *ScopeFn = IRP.getAnchorScope(); 1303 const auto *LivenessAA = 1304 ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn), 1305 DepClassTy::NONE) 1306 : nullptr; 1307 1308 while (!Worklist.empty()) { 1309 const Use *U = Worklist.pop_back_val(); 1310 if (isa<PHINode>(U->getUser()) && !Visited.insert(U).second) 1311 continue; 1312 LLVM_DEBUG({ 1313 if (auto *Fn = dyn_cast<Function>(U->getUser())) 1314 dbgs() << "[Attributor] Check use: " << **U << " in " << Fn->getName() 1315 << "\n"; 1316 else 1317 dbgs() << "[Attributor] Check use: " << **U << " in " << *U->getUser() 1318 << "\n"; 1319 }); 1320 bool UsedAssumedInformation = false; 1321 if (isAssumedDead(*U, &QueryingAA, LivenessAA, UsedAssumedInformation, 1322 CheckBBLivenessOnly, LivenessDepClass)) { 1323 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n"); 1324 continue; 1325 } 1326 if (IgnoreDroppableUses && U->getUser()->isDroppable()) { 1327 LLVM_DEBUG(dbgs() << "[Attributor] Droppable user, skip!\n"); 1328 continue; 1329 } 1330 1331 if (auto *SI = dyn_cast<StoreInst>(U->getUser())) { 1332 if (&SI->getOperandUse(0) == U) { 1333 if (!Visited.insert(U).second) 1334 continue; 1335 SmallSetVector<Value *, 4> PotentialCopies; 1336 if (AA::getPotentialCopiesOfStoredValue( 1337 *this, *SI, PotentialCopies, QueryingAA, UsedAssumedInformation, 1338 /* OnlyExact */ true)) { 1339 LLVM_DEBUG(dbgs() << "[Attributor] Value is stored, continue with " 1340 << PotentialCopies.size() 1341 << " potential copies instead!\n"); 1342 for (Value *PotentialCopy : PotentialCopies) 1343 for (const Use &CopyUse : PotentialCopy->uses()) { 1344 if (EquivalentUseCB && !EquivalentUseCB(*U, CopyUse)) { 1345 LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was " 1346 "rejected by the equivalence call back: " 1347 << *CopyUse << "!\n"); 1348 return false; 1349 } 1350 Worklist.push_back(&CopyUse); 1351 } 1352 continue; 1353 } 1354 } 1355 } 1356 1357 bool Follow = false; 1358 if (!Pred(*U, Follow)) 1359 return false; 1360 if (!Follow) 1361 continue; 1362 for (const Use &UU : U->getUser()->uses()) 1363 Worklist.push_back(&UU); 1364 } 1365 1366 return true; 1367 } 1368 1369 bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred, 1370 const AbstractAttribute &QueryingAA, 1371 bool RequireAllCallSites, 1372 bool &UsedAssumedInformation) { 1373 // We can try to determine information from 1374 // the call sites. However, this is only possible all call sites are known, 1375 // hence the function has internal linkage. 1376 const IRPosition &IRP = QueryingAA.getIRPosition(); 1377 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 1378 if (!AssociatedFunction) { 1379 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP 1380 << "\n"); 1381 return false; 1382 } 1383 1384 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites, 1385 &QueryingAA, UsedAssumedInformation); 1386 } 1387 1388 bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred, 1389 const Function &Fn, 1390 bool RequireAllCallSites, 1391 const AbstractAttribute *QueryingAA, 1392 bool &UsedAssumedInformation) { 1393 if (RequireAllCallSites && !Fn.hasLocalLinkage()) { 1394 LLVM_DEBUG( 1395 dbgs() 1396 << "[Attributor] Function " << Fn.getName() 1397 << " has no internal linkage, hence not all call sites are known\n"); 1398 return false; 1399 } 1400 1401 SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses())); 1402 for (unsigned u = 0; u < Uses.size(); ++u) { 1403 const Use &U = *Uses[u]; 1404 LLVM_DEBUG({ 1405 if (auto *Fn = dyn_cast<Function>(U)) 1406 dbgs() << "[Attributor] Check use: " << Fn->getName() << " in " 1407 << *U.getUser() << "\n"; 1408 else 1409 dbgs() << "[Attributor] Check use: " << *U << " in " << *U.getUser() 1410 << "\n"; 1411 }); 1412 if (isAssumedDead(U, QueryingAA, nullptr, UsedAssumedInformation, 1413 /* CheckBBLivenessOnly */ true)) { 1414 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n"); 1415 continue; 1416 } 1417 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) { 1418 if (CE->isCast() && CE->getType()->isPointerTy()) { 1419 LLVM_DEBUG( 1420 dbgs() << "[Attributor] Use, is constant cast expression, add " 1421 << CE->getNumUses() 1422 << " uses of that expression instead!\n"); 1423 for (const Use &CEU : CE->uses()) 1424 Uses.push_back(&CEU); 1425 continue; 1426 } 1427 } 1428 1429 AbstractCallSite ACS(&U); 1430 if (!ACS) { 1431 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName() 1432 << " has non call site use " << *U.get() << " in " 1433 << *U.getUser() << "\n"); 1434 // BlockAddress users are allowed. 1435 if (isa<BlockAddress>(U.getUser())) 1436 continue; 1437 return false; 1438 } 1439 1440 const Use *EffectiveUse = 1441 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U; 1442 if (!ACS.isCallee(EffectiveUse)) { 1443 if (!RequireAllCallSites) { 1444 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser() 1445 << " is not a call of " << Fn.getName() 1446 << ", skip use\n"); 1447 continue; 1448 } 1449 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser() 1450 << " is an invalid use of " << Fn.getName() << "\n"); 1451 return false; 1452 } 1453 1454 // Make sure the arguments that can be matched between the call site and the 1455 // callee argee on their type. It is unlikely they do not and it doesn't 1456 // make sense for all attributes to know/care about this. 1457 assert(&Fn == ACS.getCalledFunction() && "Expected known callee"); 1458 unsigned MinArgsParams = 1459 std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size()); 1460 for (unsigned u = 0; u < MinArgsParams; ++u) { 1461 Value *CSArgOp = ACS.getCallArgOperand(u); 1462 if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) { 1463 LLVM_DEBUG( 1464 dbgs() << "[Attributor] Call site / callee argument type mismatch [" 1465 << u << "@" << Fn.getName() << ": " 1466 << *Fn.getArg(u)->getType() << " vs. " 1467 << *ACS.getCallArgOperand(u)->getType() << "\n"); 1468 return false; 1469 } 1470 } 1471 1472 if (Pred(ACS)) 1473 continue; 1474 1475 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " 1476 << *ACS.getInstruction() << "\n"); 1477 return false; 1478 } 1479 1480 return true; 1481 } 1482 1483 bool Attributor::shouldPropagateCallBaseContext(const IRPosition &IRP) { 1484 // TODO: Maintain a cache of Values that are 1485 // on the pathway from a Argument to a Instruction that would effect the 1486 // liveness/return state etc. 1487 return EnableCallSiteSpecific; 1488 } 1489 1490 bool Attributor::checkForAllReturnedValuesAndReturnInsts( 1491 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred, 1492 const AbstractAttribute &QueryingAA) { 1493 1494 const IRPosition &IRP = QueryingAA.getIRPosition(); 1495 // Since we need to provide return instructions we have to have an exact 1496 // definition. 1497 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 1498 if (!AssociatedFunction) 1499 return false; 1500 1501 // If this is a call site query we use the call site specific return values 1502 // and liveness information. 1503 // TODO: use the function scope once we have call site AAReturnedValues. 1504 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 1505 const auto &AARetVal = 1506 getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED); 1507 if (!AARetVal.getState().isValidState()) 1508 return false; 1509 1510 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); 1511 } 1512 1513 bool Attributor::checkForAllReturnedValues( 1514 function_ref<bool(Value &)> Pred, const AbstractAttribute &QueryingAA) { 1515 1516 const IRPosition &IRP = QueryingAA.getIRPosition(); 1517 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 1518 if (!AssociatedFunction) 1519 return false; 1520 1521 // TODO: use the function scope once we have call site AAReturnedValues. 1522 const IRPosition &QueryIRP = IRPosition::function( 1523 *AssociatedFunction, QueryingAA.getCallBaseContext()); 1524 const auto &AARetVal = 1525 getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED); 1526 if (!AARetVal.getState().isValidState()) 1527 return false; 1528 1529 return AARetVal.checkForAllReturnedValuesAndReturnInsts( 1530 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { 1531 return Pred(RV); 1532 }); 1533 } 1534 1535 static bool checkForAllInstructionsImpl( 1536 Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap, 1537 function_ref<bool(Instruction &)> Pred, const AbstractAttribute *QueryingAA, 1538 const AAIsDead *LivenessAA, const ArrayRef<unsigned> &Opcodes, 1539 bool &UsedAssumedInformation, bool CheckBBLivenessOnly = false, 1540 bool CheckPotentiallyDead = false) { 1541 for (unsigned Opcode : Opcodes) { 1542 // Check if we have instructions with this opcode at all first. 1543 auto *Insts = OpcodeInstMap.lookup(Opcode); 1544 if (!Insts) 1545 continue; 1546 1547 for (Instruction *I : *Insts) { 1548 // Skip dead instructions. 1549 if (A && !CheckPotentiallyDead && 1550 A->isAssumedDead(IRPosition::inst(*I), QueryingAA, LivenessAA, 1551 UsedAssumedInformation, CheckBBLivenessOnly)) { 1552 LLVM_DEBUG(dbgs() << "[Attributor] Instruction " << *I 1553 << " is potentially dead, skip!\n";); 1554 continue; 1555 } 1556 1557 if (!Pred(*I)) 1558 return false; 1559 } 1560 } 1561 return true; 1562 } 1563 1564 bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred, 1565 const Function *Fn, 1566 const AbstractAttribute &QueryingAA, 1567 const ArrayRef<unsigned> &Opcodes, 1568 bool &UsedAssumedInformation, 1569 bool CheckBBLivenessOnly, 1570 bool CheckPotentiallyDead) { 1571 // Since we need to provide instructions we have to have an exact definition. 1572 if (!Fn || Fn->isDeclaration()) 1573 return false; 1574 1575 // TODO: use the function scope once we have call site AAReturnedValues. 1576 const IRPosition &QueryIRP = IRPosition::function(*Fn); 1577 const auto *LivenessAA = 1578 (CheckBBLivenessOnly || CheckPotentiallyDead) 1579 ? nullptr 1580 : &(getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE)); 1581 1582 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn); 1583 if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA, 1584 LivenessAA, Opcodes, UsedAssumedInformation, 1585 CheckBBLivenessOnly, CheckPotentiallyDead)) 1586 return false; 1587 1588 return true; 1589 } 1590 1591 bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred, 1592 const AbstractAttribute &QueryingAA, 1593 const ArrayRef<unsigned> &Opcodes, 1594 bool &UsedAssumedInformation, 1595 bool CheckBBLivenessOnly, 1596 bool CheckPotentiallyDead) { 1597 const IRPosition &IRP = QueryingAA.getIRPosition(); 1598 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 1599 return checkForAllInstructions(Pred, AssociatedFunction, QueryingAA, Opcodes, 1600 UsedAssumedInformation, CheckBBLivenessOnly, 1601 CheckPotentiallyDead); 1602 } 1603 1604 bool Attributor::checkForAllReadWriteInstructions( 1605 function_ref<bool(Instruction &)> Pred, AbstractAttribute &QueryingAA, 1606 bool &UsedAssumedInformation) { 1607 1608 const Function *AssociatedFunction = 1609 QueryingAA.getIRPosition().getAssociatedFunction(); 1610 if (!AssociatedFunction) 1611 return false; 1612 1613 // TODO: use the function scope once we have call site AAReturnedValues. 1614 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 1615 const auto &LivenessAA = 1616 getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE); 1617 1618 for (Instruction *I : 1619 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { 1620 // Skip dead instructions. 1621 if (isAssumedDead(IRPosition::inst(*I), &QueryingAA, &LivenessAA, 1622 UsedAssumedInformation)) 1623 continue; 1624 1625 if (!Pred(*I)) 1626 return false; 1627 } 1628 1629 return true; 1630 } 1631 1632 void Attributor::runTillFixpoint() { 1633 TimeTraceScope TimeScope("Attributor::runTillFixpoint"); 1634 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " 1635 << DG.SyntheticRoot.Deps.size() 1636 << " abstract attributes.\n"); 1637 1638 // Now that all abstract attributes are collected and initialized we start 1639 // the abstract analysis. 1640 1641 unsigned IterationCounter = 1; 1642 unsigned MaxIterations = 1643 Configuration.MaxFixpointIterations.getValueOr(SetFixpointIterations); 1644 1645 SmallVector<AbstractAttribute *, 32> ChangedAAs; 1646 SetVector<AbstractAttribute *> Worklist, InvalidAAs; 1647 Worklist.insert(DG.SyntheticRoot.begin(), DG.SyntheticRoot.end()); 1648 1649 do { 1650 // Remember the size to determine new attributes. 1651 size_t NumAAs = DG.SyntheticRoot.Deps.size(); 1652 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter 1653 << ", Worklist size: " << Worklist.size() << "\n"); 1654 1655 // For invalid AAs we can fix dependent AAs that have a required dependence, 1656 // thereby folding long dependence chains in a single step without the need 1657 // to run updates. 1658 for (unsigned u = 0; u < InvalidAAs.size(); ++u) { 1659 AbstractAttribute *InvalidAA = InvalidAAs[u]; 1660 1661 // Check the dependences to fast track invalidation. 1662 LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has " 1663 << InvalidAA->Deps.size() 1664 << " required & optional dependences\n"); 1665 while (!InvalidAA->Deps.empty()) { 1666 const auto &Dep = InvalidAA->Deps.back(); 1667 InvalidAA->Deps.pop_back(); 1668 AbstractAttribute *DepAA = cast<AbstractAttribute>(Dep.getPointer()); 1669 if (Dep.getInt() == unsigned(DepClassTy::OPTIONAL)) { 1670 LLVM_DEBUG(dbgs() << " - recompute: " << *DepAA); 1671 Worklist.insert(DepAA); 1672 continue; 1673 } 1674 LLVM_DEBUG(dbgs() << " - invalidate: " << *DepAA); 1675 DepAA->getState().indicatePessimisticFixpoint(); 1676 assert(DepAA->getState().isAtFixpoint() && "Expected fixpoint state!"); 1677 if (!DepAA->getState().isValidState()) 1678 InvalidAAs.insert(DepAA); 1679 else 1680 ChangedAAs.push_back(DepAA); 1681 } 1682 } 1683 1684 // Add all abstract attributes that are potentially dependent on one that 1685 // changed to the work list. 1686 for (AbstractAttribute *ChangedAA : ChangedAAs) 1687 while (!ChangedAA->Deps.empty()) { 1688 Worklist.insert( 1689 cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer())); 1690 ChangedAA->Deps.pop_back(); 1691 } 1692 1693 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter 1694 << ", Worklist+Dependent size: " << Worklist.size() 1695 << "\n"); 1696 1697 // Reset the changed and invalid set. 1698 ChangedAAs.clear(); 1699 InvalidAAs.clear(); 1700 1701 // Update all abstract attribute in the work list and record the ones that 1702 // changed. 1703 for (AbstractAttribute *AA : Worklist) { 1704 const auto &AAState = AA->getState(); 1705 if (!AAState.isAtFixpoint()) 1706 if (updateAA(*AA) == ChangeStatus::CHANGED) 1707 ChangedAAs.push_back(AA); 1708 1709 // Use the InvalidAAs vector to propagate invalid states fast transitively 1710 // without requiring updates. 1711 if (!AAState.isValidState()) 1712 InvalidAAs.insert(AA); 1713 } 1714 1715 // Add attributes to the changed set if they have been created in the last 1716 // iteration. 1717 ChangedAAs.append(DG.SyntheticRoot.begin() + NumAAs, 1718 DG.SyntheticRoot.end()); 1719 1720 // Reset the work list and repopulate with the changed abstract attributes. 1721 // Note that dependent ones are added above. 1722 Worklist.clear(); 1723 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); 1724 Worklist.insert(QueryAAsAwaitingUpdate.begin(), 1725 QueryAAsAwaitingUpdate.end()); 1726 QueryAAsAwaitingUpdate.clear(); 1727 1728 } while (!Worklist.empty() && 1729 (IterationCounter++ < MaxIterations || VerifyMaxFixpointIterations)); 1730 1731 if (IterationCounter > MaxIterations && !Functions.empty()) { 1732 auto Remark = [&](OptimizationRemarkMissed ORM) { 1733 return ORM << "Attributor did not reach a fixpoint after " 1734 << ore::NV("Iterations", MaxIterations) << " iterations."; 1735 }; 1736 Function *F = Functions.front(); 1737 emitRemark<OptimizationRemarkMissed>(F, "FixedPoint", Remark); 1738 } 1739 1740 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " 1741 << IterationCounter << "/" << MaxIterations 1742 << " iterations\n"); 1743 1744 // Reset abstract arguments not settled in a sound fixpoint by now. This 1745 // happens when we stopped the fixpoint iteration early. Note that only the 1746 // ones marked as "changed" *and* the ones transitively depending on them 1747 // need to be reverted to a pessimistic state. Others might not be in a 1748 // fixpoint state but we can use the optimistic results for them anyway. 1749 SmallPtrSet<AbstractAttribute *, 32> Visited; 1750 for (unsigned u = 0; u < ChangedAAs.size(); u++) { 1751 AbstractAttribute *ChangedAA = ChangedAAs[u]; 1752 if (!Visited.insert(ChangedAA).second) 1753 continue; 1754 1755 AbstractState &State = ChangedAA->getState(); 1756 if (!State.isAtFixpoint()) { 1757 State.indicatePessimisticFixpoint(); 1758 1759 NumAttributesTimedOut++; 1760 } 1761 1762 while (!ChangedAA->Deps.empty()) { 1763 ChangedAAs.push_back( 1764 cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer())); 1765 ChangedAA->Deps.pop_back(); 1766 } 1767 } 1768 1769 LLVM_DEBUG({ 1770 if (!Visited.empty()) 1771 dbgs() << "\n[Attributor] Finalized " << Visited.size() 1772 << " abstract attributes.\n"; 1773 }); 1774 1775 if (VerifyMaxFixpointIterations && IterationCounter != MaxIterations) { 1776 errs() << "\n[Attributor] Fixpoint iteration done after: " 1777 << IterationCounter << "/" << MaxIterations << " iterations\n"; 1778 llvm_unreachable("The fixpoint was not reached with exactly the number of " 1779 "specified iterations!"); 1780 } 1781 } 1782 1783 void Attributor::registerForUpdate(AbstractAttribute &AA) { 1784 assert(AA.isQueryAA() && 1785 "Non-query AAs should not be required to register for updates!"); 1786 QueryAAsAwaitingUpdate.insert(&AA); 1787 } 1788 1789 ChangeStatus Attributor::manifestAttributes() { 1790 TimeTraceScope TimeScope("Attributor::manifestAttributes"); 1791 size_t NumFinalAAs = DG.SyntheticRoot.Deps.size(); 1792 1793 unsigned NumManifested = 0; 1794 unsigned NumAtFixpoint = 0; 1795 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; 1796 for (auto &DepAA : DG.SyntheticRoot.Deps) { 1797 AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer()); 1798 AbstractState &State = AA->getState(); 1799 1800 // If there is not already a fixpoint reached, we can now take the 1801 // optimistic state. This is correct because we enforced a pessimistic one 1802 // on abstract attributes that were transitively dependent on a changed one 1803 // already above. 1804 if (!State.isAtFixpoint()) 1805 State.indicateOptimisticFixpoint(); 1806 1807 // We must not manifest Attributes that use Callbase info. 1808 if (AA->hasCallBaseContext()) 1809 continue; 1810 // If the state is invalid, we do not try to manifest it. 1811 if (!State.isValidState()) 1812 continue; 1813 1814 if (AA->getCtxI() && !isRunOn(*AA->getAnchorScope())) 1815 continue; 1816 1817 // Skip dead code. 1818 bool UsedAssumedInformation = false; 1819 if (isAssumedDead(*AA, nullptr, UsedAssumedInformation, 1820 /* CheckBBLivenessOnly */ true)) 1821 continue; 1822 // Check if the manifest debug counter that allows skipping manifestation of 1823 // AAs 1824 if (!DebugCounter::shouldExecute(ManifestDBGCounter)) 1825 continue; 1826 // Manifest the state and record if we changed the IR. 1827 ChangeStatus LocalChange = AA->manifest(*this); 1828 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) 1829 AA->trackStatistics(); 1830 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA 1831 << "\n"); 1832 1833 ManifestChange = ManifestChange | LocalChange; 1834 1835 NumAtFixpoint++; 1836 NumManifested += (LocalChange == ChangeStatus::CHANGED); 1837 } 1838 1839 (void)NumManifested; 1840 (void)NumAtFixpoint; 1841 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested 1842 << " arguments while " << NumAtFixpoint 1843 << " were in a valid fixpoint state\n"); 1844 1845 NumAttributesManifested += NumManifested; 1846 NumAttributesValidFixpoint += NumAtFixpoint; 1847 1848 (void)NumFinalAAs; 1849 if (NumFinalAAs != DG.SyntheticRoot.Deps.size()) { 1850 for (unsigned u = NumFinalAAs; u < DG.SyntheticRoot.Deps.size(); ++u) 1851 errs() << "Unexpected abstract attribute: " 1852 << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer()) 1853 << " :: " 1854 << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer()) 1855 ->getIRPosition() 1856 .getAssociatedValue() 1857 << "\n"; 1858 llvm_unreachable("Expected the final number of abstract attributes to " 1859 "remain unchanged!"); 1860 } 1861 return ManifestChange; 1862 } 1863 1864 void Attributor::identifyDeadInternalFunctions() { 1865 // Early exit if we don't intend to delete functions. 1866 if (!Configuration.DeleteFns) 1867 return; 1868 1869 // Identify dead internal functions and delete them. This happens outside 1870 // the other fixpoint analysis as we might treat potentially dead functions 1871 // as live to lower the number of iterations. If they happen to be dead, the 1872 // below fixpoint loop will identify and eliminate them. 1873 SmallVector<Function *, 8> InternalFns; 1874 for (Function *F : Functions) 1875 if (F->hasLocalLinkage()) 1876 InternalFns.push_back(F); 1877 1878 SmallPtrSet<Function *, 8> LiveInternalFns; 1879 bool FoundLiveInternal = true; 1880 while (FoundLiveInternal) { 1881 FoundLiveInternal = false; 1882 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { 1883 Function *F = InternalFns[u]; 1884 if (!F) 1885 continue; 1886 1887 bool UsedAssumedInformation = false; 1888 if (checkForAllCallSites( 1889 [&](AbstractCallSite ACS) { 1890 Function *Callee = ACS.getInstruction()->getFunction(); 1891 return ToBeDeletedFunctions.count(Callee) || 1892 (Functions.count(Callee) && Callee->hasLocalLinkage() && 1893 !LiveInternalFns.count(Callee)); 1894 }, 1895 *F, true, nullptr, UsedAssumedInformation)) { 1896 continue; 1897 } 1898 1899 LiveInternalFns.insert(F); 1900 InternalFns[u] = nullptr; 1901 FoundLiveInternal = true; 1902 } 1903 } 1904 1905 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) 1906 if (Function *F = InternalFns[u]) 1907 ToBeDeletedFunctions.insert(F); 1908 } 1909 1910 ChangeStatus Attributor::cleanupIR() { 1911 TimeTraceScope TimeScope("Attributor::cleanupIR"); 1912 // Delete stuff at the end to avoid invalid references and a nice order. 1913 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least " 1914 << ToBeDeletedFunctions.size() << " functions and " 1915 << ToBeDeletedBlocks.size() << " blocks and " 1916 << ToBeDeletedInsts.size() << " instructions and " 1917 << ToBeChangedValues.size() << " values and " 1918 << ToBeChangedUses.size() << " uses. To insert " 1919 << ToBeChangedToUnreachableInsts.size() << " unreachables." 1920 << "Preserve manifest added " << ManifestAddedBlocks.size() 1921 << " blocks\n"); 1922 1923 SmallVector<WeakTrackingVH, 32> DeadInsts; 1924 SmallVector<Instruction *, 32> TerminatorsToFold; 1925 1926 auto ReplaceUse = [&](Use *U, Value *NewV) { 1927 Value *OldV = U->get(); 1928 1929 // If we plan to replace NewV we need to update it at this point. 1930 do { 1931 const auto &Entry = ToBeChangedValues.lookup(NewV); 1932 if (!Entry.first) 1933 break; 1934 NewV = Entry.first; 1935 } while (true); 1936 1937 Instruction *I = dyn_cast<Instruction>(U->getUser()); 1938 assert((!I || isRunOn(*I->getFunction())) && 1939 "Cannot replace an instruction outside the current SCC!"); 1940 1941 // Do not replace uses in returns if the value is a must-tail call we will 1942 // not delete. 1943 if (auto *RI = dyn_cast_or_null<ReturnInst>(I)) { 1944 if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts())) 1945 if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI)) 1946 return; 1947 // If we rewrite a return and the new value is not an argument, strip the 1948 // `returned` attribute as it is wrong now. 1949 if (!isa<Argument>(NewV)) 1950 for (auto &Arg : RI->getFunction()->args()) 1951 Arg.removeAttr(Attribute::Returned); 1952 } 1953 1954 // Do not perform call graph altering changes outside the SCC. 1955 if (auto *CB = dyn_cast_or_null<CallBase>(I)) 1956 if (CB->isCallee(U)) 1957 return; 1958 1959 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() 1960 << " instead of " << *OldV << "\n"); 1961 U->set(NewV); 1962 1963 if (Instruction *I = dyn_cast<Instruction>(OldV)) { 1964 CGModifiedFunctions.insert(I->getFunction()); 1965 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) && 1966 isInstructionTriviallyDead(I)) 1967 DeadInsts.push_back(I); 1968 } 1969 if (isa<UndefValue>(NewV) && isa<CallBase>(U->getUser())) { 1970 auto *CB = cast<CallBase>(U->getUser()); 1971 if (CB->isArgOperand(U)) { 1972 unsigned Idx = CB->getArgOperandNo(U); 1973 CB->removeParamAttr(Idx, Attribute::NoUndef); 1974 Function *Fn = CB->getCalledFunction(); 1975 if (Fn && Fn->arg_size() > Idx) 1976 Fn->removeParamAttr(Idx, Attribute::NoUndef); 1977 } 1978 } 1979 if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) { 1980 Instruction *UserI = cast<Instruction>(U->getUser()); 1981 if (isa<UndefValue>(NewV)) { 1982 ToBeChangedToUnreachableInsts.insert(UserI); 1983 } else { 1984 TerminatorsToFold.push_back(UserI); 1985 } 1986 } 1987 }; 1988 1989 for (auto &It : ToBeChangedUses) { 1990 Use *U = It.first; 1991 Value *NewV = It.second; 1992 ReplaceUse(U, NewV); 1993 } 1994 1995 SmallVector<Use *, 4> Uses; 1996 for (auto &It : ToBeChangedValues) { 1997 Value *OldV = It.first; 1998 auto &Entry = It.second; 1999 Value *NewV = Entry.first; 2000 Uses.clear(); 2001 for (auto &U : OldV->uses()) 2002 if (Entry.second || !U.getUser()->isDroppable()) 2003 Uses.push_back(&U); 2004 for (Use *U : Uses) { 2005 if (auto *I = dyn_cast<Instruction>(U->getUser())) 2006 if (!isRunOn(*I->getFunction())) 2007 continue; 2008 ReplaceUse(U, NewV); 2009 } 2010 } 2011 2012 for (auto &V : InvokeWithDeadSuccessor) 2013 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) { 2014 assert(isRunOn(*II->getFunction()) && 2015 "Cannot replace an invoke outside the current SCC!"); 2016 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind); 2017 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn); 2018 bool Invoke2CallAllowed = 2019 !AAIsDead::mayCatchAsynchronousExceptions(*II->getFunction()); 2020 assert((UnwindBBIsDead || NormalBBIsDead) && 2021 "Invoke does not have dead successors!"); 2022 BasicBlock *BB = II->getParent(); 2023 BasicBlock *NormalDestBB = II->getNormalDest(); 2024 if (UnwindBBIsDead) { 2025 Instruction *NormalNextIP = &NormalDestBB->front(); 2026 if (Invoke2CallAllowed) { 2027 changeToCall(II); 2028 NormalNextIP = BB->getTerminator(); 2029 } 2030 if (NormalBBIsDead) 2031 ToBeChangedToUnreachableInsts.insert(NormalNextIP); 2032 } else { 2033 assert(NormalBBIsDead && "Broken invariant!"); 2034 if (!NormalDestBB->getUniquePredecessor()) 2035 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); 2036 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front()); 2037 } 2038 } 2039 for (Instruction *I : TerminatorsToFold) { 2040 assert(isRunOn(*I->getFunction()) && 2041 "Cannot replace a terminator outside the current SCC!"); 2042 CGModifiedFunctions.insert(I->getFunction()); 2043 ConstantFoldTerminator(I->getParent()); 2044 } 2045 for (auto &V : ToBeChangedToUnreachableInsts) 2046 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) { 2047 assert(isRunOn(*I->getFunction()) && 2048 "Cannot replace an instruction outside the current SCC!"); 2049 CGModifiedFunctions.insert(I->getFunction()); 2050 changeToUnreachable(I); 2051 } 2052 2053 for (auto &V : ToBeDeletedInsts) { 2054 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) { 2055 if (auto *CB = dyn_cast<CallBase>(I)) { 2056 assert(isRunOn(*I->getFunction()) && 2057 "Cannot delete an instruction outside the current SCC!"); 2058 if (!isa<IntrinsicInst>(CB)) 2059 Configuration.CGUpdater.removeCallSite(*CB); 2060 } 2061 I->dropDroppableUses(); 2062 CGModifiedFunctions.insert(I->getFunction()); 2063 if (!I->getType()->isVoidTy()) 2064 I->replaceAllUsesWith(UndefValue::get(I->getType())); 2065 if (!isa<PHINode>(I) && isInstructionTriviallyDead(I)) 2066 DeadInsts.push_back(I); 2067 else 2068 I->eraseFromParent(); 2069 } 2070 } 2071 2072 llvm::erase_if(DeadInsts, [&](WeakTrackingVH I) { return !I; }); 2073 2074 LLVM_DEBUG({ 2075 dbgs() << "[Attributor] DeadInsts size: " << DeadInsts.size() << "\n"; 2076 for (auto &I : DeadInsts) 2077 if (I) 2078 dbgs() << " - " << *I << "\n"; 2079 }); 2080 2081 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); 2082 2083 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { 2084 SmallVector<BasicBlock *, 8> ToBeDeletedBBs; 2085 ToBeDeletedBBs.reserve(NumDeadBlocks); 2086 for (BasicBlock *BB : ToBeDeletedBlocks) { 2087 assert(isRunOn(*BB->getParent()) && 2088 "Cannot delete a block outside the current SCC!"); 2089 CGModifiedFunctions.insert(BB->getParent()); 2090 // Do not delete BBs added during manifests of AAs. 2091 if (ManifestAddedBlocks.contains(BB)) 2092 continue; 2093 ToBeDeletedBBs.push_back(BB); 2094 } 2095 // Actually we do not delete the blocks but squash them into a single 2096 // unreachable but untangling branches that jump here is something we need 2097 // to do in a more generic way. 2098 detachDeadBlocks(ToBeDeletedBBs, nullptr); 2099 } 2100 2101 identifyDeadInternalFunctions(); 2102 2103 // Rewrite the functions as requested during manifest. 2104 ChangeStatus ManifestChange = rewriteFunctionSignatures(CGModifiedFunctions); 2105 2106 for (Function *Fn : CGModifiedFunctions) 2107 if (!ToBeDeletedFunctions.count(Fn) && Functions.count(Fn)) 2108 Configuration.CGUpdater.reanalyzeFunction(*Fn); 2109 2110 for (Function *Fn : ToBeDeletedFunctions) { 2111 if (!Functions.count(Fn)) 2112 continue; 2113 Configuration.CGUpdater.removeFunction(*Fn); 2114 } 2115 2116 if (!ToBeChangedUses.empty()) 2117 ManifestChange = ChangeStatus::CHANGED; 2118 2119 if (!ToBeChangedToUnreachableInsts.empty()) 2120 ManifestChange = ChangeStatus::CHANGED; 2121 2122 if (!ToBeDeletedFunctions.empty()) 2123 ManifestChange = ChangeStatus::CHANGED; 2124 2125 if (!ToBeDeletedBlocks.empty()) 2126 ManifestChange = ChangeStatus::CHANGED; 2127 2128 if (!ToBeDeletedInsts.empty()) 2129 ManifestChange = ChangeStatus::CHANGED; 2130 2131 if (!InvokeWithDeadSuccessor.empty()) 2132 ManifestChange = ChangeStatus::CHANGED; 2133 2134 if (!DeadInsts.empty()) 2135 ManifestChange = ChangeStatus::CHANGED; 2136 2137 NumFnDeleted += ToBeDeletedFunctions.size(); 2138 2139 LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions.size() 2140 << " functions after manifest.\n"); 2141 2142 #ifdef EXPENSIVE_CHECKS 2143 for (Function *F : Functions) { 2144 if (ToBeDeletedFunctions.count(F)) 2145 continue; 2146 assert(!verifyFunction(*F, &errs()) && "Module verification failed!"); 2147 } 2148 #endif 2149 2150 return ManifestChange; 2151 } 2152 2153 ChangeStatus Attributor::run() { 2154 TimeTraceScope TimeScope("Attributor::run"); 2155 AttributorCallGraph ACallGraph(*this); 2156 2157 if (PrintCallGraph) 2158 ACallGraph.populateAll(); 2159 2160 Phase = AttributorPhase::UPDATE; 2161 runTillFixpoint(); 2162 2163 // dump graphs on demand 2164 if (DumpDepGraph) 2165 DG.dumpGraph(); 2166 2167 if (ViewDepGraph) 2168 DG.viewGraph(); 2169 2170 if (PrintDependencies) 2171 DG.print(); 2172 2173 Phase = AttributorPhase::MANIFEST; 2174 ChangeStatus ManifestChange = manifestAttributes(); 2175 2176 Phase = AttributorPhase::CLEANUP; 2177 ChangeStatus CleanupChange = cleanupIR(); 2178 2179 if (PrintCallGraph) 2180 ACallGraph.print(); 2181 2182 return ManifestChange | CleanupChange; 2183 } 2184 2185 ChangeStatus Attributor::updateAA(AbstractAttribute &AA) { 2186 TimeTraceScope TimeScope( 2187 AA.getName() + std::to_string(AA.getIRPosition().getPositionKind()) + 2188 "::updateAA"); 2189 assert(Phase == AttributorPhase::UPDATE && 2190 "We can update AA only in the update stage!"); 2191 2192 // Use a new dependence vector for this update. 2193 DependenceVector DV; 2194 DependenceStack.push_back(&DV); 2195 2196 auto &AAState = AA.getState(); 2197 ChangeStatus CS = ChangeStatus::UNCHANGED; 2198 bool UsedAssumedInformation = false; 2199 if (!isAssumedDead(AA, nullptr, UsedAssumedInformation, 2200 /* CheckBBLivenessOnly */ true)) 2201 CS = AA.update(*this); 2202 2203 if (!AA.isQueryAA() && DV.empty()) { 2204 // If the attribute did not query any non-fix information, the state 2205 // will not change and we can indicate that right away. 2206 AAState.indicateOptimisticFixpoint(); 2207 } 2208 2209 if (!AAState.isAtFixpoint()) 2210 rememberDependences(); 2211 2212 // Verify the stack was used properly, that is we pop the dependence vector we 2213 // put there earlier. 2214 DependenceVector *PoppedDV = DependenceStack.pop_back_val(); 2215 (void)PoppedDV; 2216 assert(PoppedDV == &DV && "Inconsistent usage of the dependence stack!"); 2217 2218 return CS; 2219 } 2220 2221 void Attributor::createShallowWrapper(Function &F) { 2222 assert(!F.isDeclaration() && "Cannot create a wrapper around a declaration!"); 2223 2224 Module &M = *F.getParent(); 2225 LLVMContext &Ctx = M.getContext(); 2226 FunctionType *FnTy = F.getFunctionType(); 2227 2228 Function *Wrapper = 2229 Function::Create(FnTy, F.getLinkage(), F.getAddressSpace(), F.getName()); 2230 F.setName(""); // set the inside function anonymous 2231 M.getFunctionList().insert(F.getIterator(), Wrapper); 2232 2233 F.setLinkage(GlobalValue::InternalLinkage); 2234 2235 F.replaceAllUsesWith(Wrapper); 2236 assert(F.use_empty() && "Uses remained after wrapper was created!"); 2237 2238 // Move the COMDAT section to the wrapper. 2239 // TODO: Check if we need to keep it for F as well. 2240 Wrapper->setComdat(F.getComdat()); 2241 F.setComdat(nullptr); 2242 2243 // Copy all metadata and attributes but keep them on F as well. 2244 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs; 2245 F.getAllMetadata(MDs); 2246 for (auto MDIt : MDs) 2247 Wrapper->addMetadata(MDIt.first, *MDIt.second); 2248 Wrapper->setAttributes(F.getAttributes()); 2249 2250 // Create the call in the wrapper. 2251 BasicBlock *EntryBB = BasicBlock::Create(Ctx, "entry", Wrapper); 2252 2253 SmallVector<Value *, 8> Args; 2254 Argument *FArgIt = F.arg_begin(); 2255 for (Argument &Arg : Wrapper->args()) { 2256 Args.push_back(&Arg); 2257 Arg.setName((FArgIt++)->getName()); 2258 } 2259 2260 CallInst *CI = CallInst::Create(&F, Args, "", EntryBB); 2261 CI->setTailCall(true); 2262 CI->addFnAttr(Attribute::NoInline); 2263 ReturnInst::Create(Ctx, CI->getType()->isVoidTy() ? nullptr : CI, EntryBB); 2264 2265 NumFnShallowWrappersCreated++; 2266 } 2267 2268 bool Attributor::isInternalizable(Function &F) { 2269 if (F.isDeclaration() || F.hasLocalLinkage() || 2270 GlobalValue::isInterposableLinkage(F.getLinkage())) 2271 return false; 2272 return true; 2273 } 2274 2275 Function *Attributor::internalizeFunction(Function &F, bool Force) { 2276 if (!AllowDeepWrapper && !Force) 2277 return nullptr; 2278 if (!isInternalizable(F)) 2279 return nullptr; 2280 2281 SmallPtrSet<Function *, 2> FnSet = {&F}; 2282 DenseMap<Function *, Function *> InternalizedFns; 2283 internalizeFunctions(FnSet, InternalizedFns); 2284 2285 return InternalizedFns[&F]; 2286 } 2287 2288 bool Attributor::internalizeFunctions(SmallPtrSetImpl<Function *> &FnSet, 2289 DenseMap<Function *, Function *> &FnMap) { 2290 for (Function *F : FnSet) 2291 if (!Attributor::isInternalizable(*F)) 2292 return false; 2293 2294 FnMap.clear(); 2295 // Generate the internalized version of each function. 2296 for (Function *F : FnSet) { 2297 Module &M = *F->getParent(); 2298 FunctionType *FnTy = F->getFunctionType(); 2299 2300 // Create a copy of the current function 2301 Function *Copied = 2302 Function::Create(FnTy, F->getLinkage(), F->getAddressSpace(), 2303 F->getName() + ".internalized"); 2304 ValueToValueMapTy VMap; 2305 auto *NewFArgIt = Copied->arg_begin(); 2306 for (auto &Arg : F->args()) { 2307 auto ArgName = Arg.getName(); 2308 NewFArgIt->setName(ArgName); 2309 VMap[&Arg] = &(*NewFArgIt++); 2310 } 2311 SmallVector<ReturnInst *, 8> Returns; 2312 2313 // Copy the body of the original function to the new one 2314 CloneFunctionInto(Copied, F, VMap, 2315 CloneFunctionChangeType::LocalChangesOnly, Returns); 2316 2317 // Set the linakage and visibility late as CloneFunctionInto has some 2318 // implicit requirements. 2319 Copied->setVisibility(GlobalValue::DefaultVisibility); 2320 Copied->setLinkage(GlobalValue::PrivateLinkage); 2321 2322 // Copy metadata 2323 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs; 2324 F->getAllMetadata(MDs); 2325 for (auto MDIt : MDs) 2326 if (!Copied->hasMetadata()) 2327 Copied->addMetadata(MDIt.first, *MDIt.second); 2328 2329 M.getFunctionList().insert(F->getIterator(), Copied); 2330 Copied->setDSOLocal(true); 2331 FnMap[F] = Copied; 2332 } 2333 2334 // Replace all uses of the old function with the new internalized function 2335 // unless the caller is a function that was just internalized. 2336 for (Function *F : FnSet) { 2337 auto &InternalizedFn = FnMap[F]; 2338 auto IsNotInternalized = [&](Use &U) -> bool { 2339 if (auto *CB = dyn_cast<CallBase>(U.getUser())) 2340 return !FnMap.lookup(CB->getCaller()); 2341 return false; 2342 }; 2343 F->replaceUsesWithIf(InternalizedFn, IsNotInternalized); 2344 } 2345 2346 return true; 2347 } 2348 2349 bool Attributor::isValidFunctionSignatureRewrite( 2350 Argument &Arg, ArrayRef<Type *> ReplacementTypes) { 2351 2352 if (!Configuration.RewriteSignatures) 2353 return false; 2354 2355 Function *Fn = Arg.getParent(); 2356 auto CallSiteCanBeChanged = [Fn](AbstractCallSite ACS) { 2357 // Forbid the call site to cast the function return type. If we need to 2358 // rewrite these functions we need to re-create a cast for the new call site 2359 // (if the old had uses). 2360 if (!ACS.getCalledFunction() || 2361 ACS.getInstruction()->getType() != 2362 ACS.getCalledFunction()->getReturnType()) 2363 return false; 2364 if (ACS.getCalledOperand()->getType() != Fn->getType()) 2365 return false; 2366 // Forbid must-tail calls for now. 2367 return !ACS.isCallbackCall() && !ACS.getInstruction()->isMustTailCall(); 2368 }; 2369 2370 // Avoid var-arg functions for now. 2371 if (Fn->isVarArg()) { 2372 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n"); 2373 return false; 2374 } 2375 2376 // Avoid functions with complicated argument passing semantics. 2377 AttributeList FnAttributeList = Fn->getAttributes(); 2378 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) || 2379 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) || 2380 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca) || 2381 FnAttributeList.hasAttrSomewhere(Attribute::Preallocated)) { 2382 LLVM_DEBUG( 2383 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n"); 2384 return false; 2385 } 2386 2387 // Avoid callbacks for now. 2388 bool UsedAssumedInformation = false; 2389 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr, 2390 UsedAssumedInformation)) { 2391 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n"); 2392 return false; 2393 } 2394 2395 auto InstPred = [](Instruction &I) { 2396 if (auto *CI = dyn_cast<CallInst>(&I)) 2397 return !CI->isMustTailCall(); 2398 return true; 2399 }; 2400 2401 // Forbid must-tail calls for now. 2402 // TODO: 2403 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn); 2404 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr, 2405 nullptr, {Instruction::Call}, 2406 UsedAssumedInformation)) { 2407 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n"); 2408 return false; 2409 } 2410 2411 return true; 2412 } 2413 2414 bool Attributor::registerFunctionSignatureRewrite( 2415 Argument &Arg, ArrayRef<Type *> ReplacementTypes, 2416 ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB, 2417 ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) { 2418 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " 2419 << Arg.getParent()->getName() << " with " 2420 << ReplacementTypes.size() << " replacements\n"); 2421 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) && 2422 "Cannot register an invalid rewrite"); 2423 2424 Function *Fn = Arg.getParent(); 2425 SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs = 2426 ArgumentReplacementMap[Fn]; 2427 if (ARIs.empty()) 2428 ARIs.resize(Fn->arg_size()); 2429 2430 // If we have a replacement already with less than or equal new arguments, 2431 // ignore this request. 2432 std::unique_ptr<ArgumentReplacementInfo> &ARI = ARIs[Arg.getArgNo()]; 2433 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) { 2434 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n"); 2435 return false; 2436 } 2437 2438 // If we have a replacement already but we like the new one better, delete 2439 // the old. 2440 ARI.reset(); 2441 2442 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " 2443 << Arg.getParent()->getName() << " with " 2444 << ReplacementTypes.size() << " replacements\n"); 2445 2446 // Remember the replacement. 2447 ARI.reset(new ArgumentReplacementInfo(*this, Arg, ReplacementTypes, 2448 std::move(CalleeRepairCB), 2449 std::move(ACSRepairCB))); 2450 2451 return true; 2452 } 2453 2454 bool Attributor::shouldSeedAttribute(AbstractAttribute &AA) { 2455 bool Result = true; 2456 #ifndef NDEBUG 2457 if (SeedAllowList.size() != 0) 2458 Result = llvm::is_contained(SeedAllowList, AA.getName()); 2459 Function *Fn = AA.getAnchorScope(); 2460 if (FunctionSeedAllowList.size() != 0 && Fn) 2461 Result &= llvm::is_contained(FunctionSeedAllowList, Fn->getName()); 2462 #endif 2463 return Result; 2464 } 2465 2466 ChangeStatus Attributor::rewriteFunctionSignatures( 2467 SmallSetVector<Function *, 8> &ModifiedFns) { 2468 ChangeStatus Changed = ChangeStatus::UNCHANGED; 2469 2470 for (auto &It : ArgumentReplacementMap) { 2471 Function *OldFn = It.getFirst(); 2472 2473 // Deleted functions do not require rewrites. 2474 if (!Functions.count(OldFn) || ToBeDeletedFunctions.count(OldFn)) 2475 continue; 2476 2477 const SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs = 2478 It.getSecond(); 2479 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!"); 2480 2481 SmallVector<Type *, 16> NewArgumentTypes; 2482 SmallVector<AttributeSet, 16> NewArgumentAttributes; 2483 2484 // Collect replacement argument types and copy over existing attributes. 2485 AttributeList OldFnAttributeList = OldFn->getAttributes(); 2486 for (Argument &Arg : OldFn->args()) { 2487 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI = 2488 ARIs[Arg.getArgNo()]) { 2489 NewArgumentTypes.append(ARI->ReplacementTypes.begin(), 2490 ARI->ReplacementTypes.end()); 2491 NewArgumentAttributes.append(ARI->getNumReplacementArgs(), 2492 AttributeSet()); 2493 } else { 2494 NewArgumentTypes.push_back(Arg.getType()); 2495 NewArgumentAttributes.push_back( 2496 OldFnAttributeList.getParamAttrs(Arg.getArgNo())); 2497 } 2498 } 2499 2500 uint64_t LargestVectorWidth = 0; 2501 for (auto *I : NewArgumentTypes) 2502 if (auto *VT = dyn_cast<llvm::VectorType>(I)) 2503 LargestVectorWidth = std::max( 2504 LargestVectorWidth, VT->getPrimitiveSizeInBits().getKnownMinSize()); 2505 2506 FunctionType *OldFnTy = OldFn->getFunctionType(); 2507 Type *RetTy = OldFnTy->getReturnType(); 2508 2509 // Construct the new function type using the new arguments types. 2510 FunctionType *NewFnTy = 2511 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg()); 2512 2513 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName() 2514 << "' from " << *OldFn->getFunctionType() << " to " 2515 << *NewFnTy << "\n"); 2516 2517 // Create the new function body and insert it into the module. 2518 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(), 2519 OldFn->getAddressSpace(), ""); 2520 Functions.insert(NewFn); 2521 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn); 2522 NewFn->takeName(OldFn); 2523 NewFn->copyAttributesFrom(OldFn); 2524 2525 // Patch the pointer to LLVM function in debug info descriptor. 2526 NewFn->setSubprogram(OldFn->getSubprogram()); 2527 OldFn->setSubprogram(nullptr); 2528 2529 // Recompute the parameter attributes list based on the new arguments for 2530 // the function. 2531 LLVMContext &Ctx = OldFn->getContext(); 2532 NewFn->setAttributes(AttributeList::get( 2533 Ctx, OldFnAttributeList.getFnAttrs(), OldFnAttributeList.getRetAttrs(), 2534 NewArgumentAttributes)); 2535 AttributeFuncs::updateMinLegalVectorWidthAttr(*NewFn, LargestVectorWidth); 2536 2537 // Since we have now created the new function, splice the body of the old 2538 // function right into the new function, leaving the old rotting hulk of the 2539 // function empty. 2540 NewFn->getBasicBlockList().splice(NewFn->begin(), 2541 OldFn->getBasicBlockList()); 2542 2543 // Fixup block addresses to reference new function. 2544 SmallVector<BlockAddress *, 8u> BlockAddresses; 2545 for (User *U : OldFn->users()) 2546 if (auto *BA = dyn_cast<BlockAddress>(U)) 2547 BlockAddresses.push_back(BA); 2548 for (auto *BA : BlockAddresses) 2549 BA->replaceAllUsesWith(BlockAddress::get(NewFn, BA->getBasicBlock())); 2550 2551 // Set of all "call-like" instructions that invoke the old function mapped 2552 // to their new replacements. 2553 SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs; 2554 2555 // Callback to create a new "call-like" instruction for a given one. 2556 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) { 2557 CallBase *OldCB = cast<CallBase>(ACS.getInstruction()); 2558 const AttributeList &OldCallAttributeList = OldCB->getAttributes(); 2559 2560 // Collect the new argument operands for the replacement call site. 2561 SmallVector<Value *, 16> NewArgOperands; 2562 SmallVector<AttributeSet, 16> NewArgOperandAttributes; 2563 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) { 2564 unsigned NewFirstArgNum = NewArgOperands.size(); 2565 (void)NewFirstArgNum; // only used inside assert. 2566 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI = 2567 ARIs[OldArgNum]) { 2568 if (ARI->ACSRepairCB) 2569 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands); 2570 assert(ARI->getNumReplacementArgs() + NewFirstArgNum == 2571 NewArgOperands.size() && 2572 "ACS repair callback did not provide as many operand as new " 2573 "types were registered!"); 2574 // TODO: Exose the attribute set to the ACS repair callback 2575 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(), 2576 AttributeSet()); 2577 } else { 2578 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum)); 2579 NewArgOperandAttributes.push_back( 2580 OldCallAttributeList.getParamAttrs(OldArgNum)); 2581 } 2582 } 2583 2584 assert(NewArgOperands.size() == NewArgOperandAttributes.size() && 2585 "Mismatch # argument operands vs. # argument operand attributes!"); 2586 assert(NewArgOperands.size() == NewFn->arg_size() && 2587 "Mismatch # argument operands vs. # function arguments!"); 2588 2589 SmallVector<OperandBundleDef, 4> OperandBundleDefs; 2590 OldCB->getOperandBundlesAsDefs(OperandBundleDefs); 2591 2592 // Create a new call or invoke instruction to replace the old one. 2593 CallBase *NewCB; 2594 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) { 2595 NewCB = 2596 InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(), 2597 NewArgOperands, OperandBundleDefs, "", OldCB); 2598 } else { 2599 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs, 2600 "", OldCB); 2601 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind()); 2602 NewCB = NewCI; 2603 } 2604 2605 // Copy over various properties and the new attributes. 2606 NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg}); 2607 NewCB->setCallingConv(OldCB->getCallingConv()); 2608 NewCB->takeName(OldCB); 2609 NewCB->setAttributes(AttributeList::get( 2610 Ctx, OldCallAttributeList.getFnAttrs(), 2611 OldCallAttributeList.getRetAttrs(), NewArgOperandAttributes)); 2612 2613 AttributeFuncs::updateMinLegalVectorWidthAttr(*NewCB->getCaller(), 2614 LargestVectorWidth); 2615 2616 CallSitePairs.push_back({OldCB, NewCB}); 2617 return true; 2618 }; 2619 2620 // Use the CallSiteReplacementCreator to create replacement call sites. 2621 bool UsedAssumedInformation = false; 2622 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn, 2623 true, nullptr, UsedAssumedInformation); 2624 (void)Success; 2625 assert(Success && "Assumed call site replacement to succeed!"); 2626 2627 // Rewire the arguments. 2628 Argument *OldFnArgIt = OldFn->arg_begin(); 2629 Argument *NewFnArgIt = NewFn->arg_begin(); 2630 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); 2631 ++OldArgNum, ++OldFnArgIt) { 2632 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI = 2633 ARIs[OldArgNum]) { 2634 if (ARI->CalleeRepairCB) 2635 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt); 2636 if (ARI->ReplacementTypes.empty()) 2637 OldFnArgIt->replaceAllUsesWith( 2638 PoisonValue::get(OldFnArgIt->getType())); 2639 NewFnArgIt += ARI->ReplacementTypes.size(); 2640 } else { 2641 NewFnArgIt->takeName(&*OldFnArgIt); 2642 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt); 2643 ++NewFnArgIt; 2644 } 2645 } 2646 2647 // Eliminate the instructions *after* we visited all of them. 2648 for (auto &CallSitePair : CallSitePairs) { 2649 CallBase &OldCB = *CallSitePair.first; 2650 CallBase &NewCB = *CallSitePair.second; 2651 assert(OldCB.getType() == NewCB.getType() && 2652 "Cannot handle call sites with different types!"); 2653 ModifiedFns.insert(OldCB.getFunction()); 2654 Configuration.CGUpdater.replaceCallSite(OldCB, NewCB); 2655 OldCB.replaceAllUsesWith(&NewCB); 2656 OldCB.eraseFromParent(); 2657 } 2658 2659 // Replace the function in the call graph (if any). 2660 Configuration.CGUpdater.replaceFunctionWith(*OldFn, *NewFn); 2661 2662 // If the old function was modified and needed to be reanalyzed, the new one 2663 // does now. 2664 if (ModifiedFns.remove(OldFn)) 2665 ModifiedFns.insert(NewFn); 2666 2667 Changed = ChangeStatus::CHANGED; 2668 } 2669 2670 return Changed; 2671 } 2672 2673 void InformationCache::initializeInformationCache(const Function &CF, 2674 FunctionInfo &FI) { 2675 // As we do not modify the function here we can remove the const 2676 // withouth breaking implicit assumptions. At the end of the day, we could 2677 // initialize the cache eagerly which would look the same to the users. 2678 Function &F = const_cast<Function &>(CF); 2679 2680 // Walk all instructions to find interesting instructions that might be 2681 // queried by abstract attributes during their initialization or update. 2682 // This has to happen before we create attributes. 2683 2684 DenseMap<const Value *, Optional<short>> AssumeUsesMap; 2685 2686 // Add \p V to the assume uses map which track the number of uses outside of 2687 // "visited" assumes. If no outside uses are left the value is added to the 2688 // assume only use vector. 2689 auto AddToAssumeUsesMap = [&](const Value &V) -> void { 2690 SmallVector<const Instruction *> Worklist; 2691 if (auto *I = dyn_cast<Instruction>(&V)) 2692 Worklist.push_back(I); 2693 while (!Worklist.empty()) { 2694 const Instruction *I = Worklist.pop_back_val(); 2695 Optional<short> &NumUses = AssumeUsesMap[I]; 2696 if (!NumUses.hasValue()) 2697 NumUses = I->getNumUses(); 2698 NumUses = NumUses.getValue() - /* this assume */ 1; 2699 if (NumUses.getValue() != 0) 2700 continue; 2701 AssumeOnlyValues.insert(I); 2702 for (const Value *Op : I->operands()) 2703 if (auto *OpI = dyn_cast<Instruction>(Op)) 2704 Worklist.push_back(OpI); 2705 } 2706 }; 2707 2708 for (Instruction &I : instructions(&F)) { 2709 bool IsInterestingOpcode = false; 2710 2711 // To allow easy access to all instructions in a function with a given 2712 // opcode we store them in the InfoCache. As not all opcodes are interesting 2713 // to concrete attributes we only cache the ones that are as identified in 2714 // the following switch. 2715 // Note: There are no concrete attributes now so this is initially empty. 2716 switch (I.getOpcode()) { 2717 default: 2718 assert(!isa<CallBase>(&I) && 2719 "New call base instruction type needs to be known in the " 2720 "Attributor."); 2721 break; 2722 case Instruction::Call: 2723 // Calls are interesting on their own, additionally: 2724 // For `llvm.assume` calls we also fill the KnowledgeMap as we find them. 2725 // For `must-tail` calls we remember the caller and callee. 2726 if (auto *Assume = dyn_cast<AssumeInst>(&I)) { 2727 fillMapFromAssume(*Assume, KnowledgeMap); 2728 AddToAssumeUsesMap(*Assume->getArgOperand(0)); 2729 } else if (cast<CallInst>(I).isMustTailCall()) { 2730 FI.ContainsMustTailCall = true; 2731 if (const Function *Callee = cast<CallInst>(I).getCalledFunction()) 2732 getFunctionInfo(*Callee).CalledViaMustTail = true; 2733 } 2734 LLVM_FALLTHROUGH; 2735 case Instruction::CallBr: 2736 case Instruction::Invoke: 2737 case Instruction::CleanupRet: 2738 case Instruction::CatchSwitch: 2739 case Instruction::AtomicRMW: 2740 case Instruction::AtomicCmpXchg: 2741 case Instruction::Br: 2742 case Instruction::Resume: 2743 case Instruction::Ret: 2744 case Instruction::Load: 2745 // The alignment of a pointer is interesting for loads. 2746 case Instruction::Store: 2747 // The alignment of a pointer is interesting for stores. 2748 case Instruction::Alloca: 2749 case Instruction::AddrSpaceCast: 2750 IsInterestingOpcode = true; 2751 } 2752 if (IsInterestingOpcode) { 2753 auto *&Insts = FI.OpcodeInstMap[I.getOpcode()]; 2754 if (!Insts) 2755 Insts = new (Allocator) InstructionVectorTy(); 2756 Insts->push_back(&I); 2757 } 2758 if (I.mayReadOrWriteMemory()) 2759 FI.RWInsts.push_back(&I); 2760 } 2761 2762 if (F.hasFnAttribute(Attribute::AlwaysInline) && 2763 isInlineViable(F).isSuccess()) 2764 InlineableFunctions.insert(&F); 2765 } 2766 2767 AAResults *InformationCache::getAAResultsForFunction(const Function &F) { 2768 return AG.getAnalysis<AAManager>(F); 2769 } 2770 2771 InformationCache::FunctionInfo::~FunctionInfo() { 2772 // The instruction vectors are allocated using a BumpPtrAllocator, we need to 2773 // manually destroy them. 2774 for (auto &It : OpcodeInstMap) 2775 It.getSecond()->~InstructionVectorTy(); 2776 } 2777 2778 void Attributor::recordDependence(const AbstractAttribute &FromAA, 2779 const AbstractAttribute &ToAA, 2780 DepClassTy DepClass) { 2781 if (DepClass == DepClassTy::NONE) 2782 return; 2783 // If we are outside of an update, thus before the actual fixpoint iteration 2784 // started (= when we create AAs), we do not track dependences because we will 2785 // put all AAs into the initial worklist anyway. 2786 if (DependenceStack.empty()) 2787 return; 2788 if (FromAA.getState().isAtFixpoint()) 2789 return; 2790 DependenceStack.back()->push_back({&FromAA, &ToAA, DepClass}); 2791 } 2792 2793 void Attributor::rememberDependences() { 2794 assert(!DependenceStack.empty() && "No dependences to remember!"); 2795 2796 for (DepInfo &DI : *DependenceStack.back()) { 2797 assert((DI.DepClass == DepClassTy::REQUIRED || 2798 DI.DepClass == DepClassTy::OPTIONAL) && 2799 "Expected required or optional dependence (1 bit)!"); 2800 auto &DepAAs = const_cast<AbstractAttribute &>(*DI.FromAA).Deps; 2801 DepAAs.push_back(AbstractAttribute::DepTy( 2802 const_cast<AbstractAttribute *>(DI.ToAA), unsigned(DI.DepClass))); 2803 } 2804 } 2805 2806 void Attributor::identifyDefaultAbstractAttributes(Function &F) { 2807 if (!VisitedFunctions.insert(&F).second) 2808 return; 2809 if (F.isDeclaration()) 2810 return; 2811 2812 // In non-module runs we need to look at the call sites of a function to 2813 // determine if it is part of a must-tail call edge. This will influence what 2814 // attributes we can derive. 2815 InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F); 2816 if (!isModulePass() && !FI.CalledViaMustTail) { 2817 for (const Use &U : F.uses()) 2818 if (const auto *CB = dyn_cast<CallBase>(U.getUser())) 2819 if (CB->isCallee(&U) && CB->isMustTailCall()) 2820 FI.CalledViaMustTail = true; 2821 } 2822 2823 IRPosition FPos = IRPosition::function(F); 2824 2825 // Check for dead BasicBlocks in every function. 2826 // We need dead instruction detection because we do not want to deal with 2827 // broken IR in which SSA rules do not apply. 2828 getOrCreateAAFor<AAIsDead>(FPos); 2829 2830 // Every function might be "will-return". 2831 getOrCreateAAFor<AAWillReturn>(FPos); 2832 2833 // Every function might contain instructions that cause "undefined behavior". 2834 getOrCreateAAFor<AAUndefinedBehavior>(FPos); 2835 2836 // Every function can be nounwind. 2837 getOrCreateAAFor<AANoUnwind>(FPos); 2838 2839 // Every function might be marked "nosync" 2840 getOrCreateAAFor<AANoSync>(FPos); 2841 2842 // Every function might be "no-free". 2843 getOrCreateAAFor<AANoFree>(FPos); 2844 2845 // Every function might be "no-return". 2846 getOrCreateAAFor<AANoReturn>(FPos); 2847 2848 // Every function might be "no-recurse". 2849 getOrCreateAAFor<AANoRecurse>(FPos); 2850 2851 // Every function might be "readnone/readonly/writeonly/...". 2852 getOrCreateAAFor<AAMemoryBehavior>(FPos); 2853 2854 // Every function can be "readnone/argmemonly/inaccessiblememonly/...". 2855 getOrCreateAAFor<AAMemoryLocation>(FPos); 2856 2857 // Every function can track active assumptions. 2858 getOrCreateAAFor<AAAssumptionInfo>(FPos); 2859 2860 // Every function might be applicable for Heap-To-Stack conversion. 2861 if (EnableHeapToStack) 2862 getOrCreateAAFor<AAHeapToStack>(FPos); 2863 2864 // Return attributes are only appropriate if the return type is non void. 2865 Type *ReturnType = F.getReturnType(); 2866 if (!ReturnType->isVoidTy()) { 2867 // Argument attribute "returned" --- Create only one per function even 2868 // though it is an argument attribute. 2869 getOrCreateAAFor<AAReturnedValues>(FPos); 2870 2871 IRPosition RetPos = IRPosition::returned(F); 2872 2873 // Every returned value might be dead. 2874 getOrCreateAAFor<AAIsDead>(RetPos); 2875 2876 // Every function might be simplified. 2877 bool UsedAssumedInformation = false; 2878 getAssumedSimplified(RetPos, nullptr, UsedAssumedInformation); 2879 2880 // Every returned value might be marked noundef. 2881 getOrCreateAAFor<AANoUndef>(RetPos); 2882 2883 if (ReturnType->isPointerTy()) { 2884 2885 // Every function with pointer return type might be marked align. 2886 getOrCreateAAFor<AAAlign>(RetPos); 2887 2888 // Every function with pointer return type might be marked nonnull. 2889 getOrCreateAAFor<AANonNull>(RetPos); 2890 2891 // Every function with pointer return type might be marked noalias. 2892 getOrCreateAAFor<AANoAlias>(RetPos); 2893 2894 // Every function with pointer return type might be marked 2895 // dereferenceable. 2896 getOrCreateAAFor<AADereferenceable>(RetPos); 2897 } 2898 } 2899 2900 for (Argument &Arg : F.args()) { 2901 IRPosition ArgPos = IRPosition::argument(Arg); 2902 2903 // Every argument might be simplified. We have to go through the Attributor 2904 // interface though as outside AAs can register custom simplification 2905 // callbacks. 2906 bool UsedAssumedInformation = false; 2907 getAssumedSimplified(ArgPos, /* AA */ nullptr, UsedAssumedInformation); 2908 2909 // Every argument might be dead. 2910 getOrCreateAAFor<AAIsDead>(ArgPos); 2911 2912 // Every argument might be marked noundef. 2913 getOrCreateAAFor<AANoUndef>(ArgPos); 2914 2915 if (Arg.getType()->isPointerTy()) { 2916 // Every argument with pointer type might be marked nonnull. 2917 getOrCreateAAFor<AANonNull>(ArgPos); 2918 2919 // Every argument with pointer type might be marked noalias. 2920 getOrCreateAAFor<AANoAlias>(ArgPos); 2921 2922 // Every argument with pointer type might be marked dereferenceable. 2923 getOrCreateAAFor<AADereferenceable>(ArgPos); 2924 2925 // Every argument with pointer type might be marked align. 2926 getOrCreateAAFor<AAAlign>(ArgPos); 2927 2928 // Every argument with pointer type might be marked nocapture. 2929 getOrCreateAAFor<AANoCapture>(ArgPos); 2930 2931 // Every argument with pointer type might be marked 2932 // "readnone/readonly/writeonly/..." 2933 getOrCreateAAFor<AAMemoryBehavior>(ArgPos); 2934 2935 // Every argument with pointer type might be marked nofree. 2936 getOrCreateAAFor<AANoFree>(ArgPos); 2937 2938 // Every argument with pointer type might be privatizable (or promotable) 2939 getOrCreateAAFor<AAPrivatizablePtr>(ArgPos); 2940 } 2941 } 2942 2943 auto CallSitePred = [&](Instruction &I) -> bool { 2944 auto &CB = cast<CallBase>(I); 2945 IRPosition CBInstPos = IRPosition::inst(CB); 2946 IRPosition CBFnPos = IRPosition::callsite_function(CB); 2947 2948 // Call sites might be dead if they do not have side effects and no live 2949 // users. The return value might be dead if there are no live users. 2950 getOrCreateAAFor<AAIsDead>(CBInstPos); 2951 2952 Function *Callee = CB.getCalledFunction(); 2953 // TODO: Even if the callee is not known now we might be able to simplify 2954 // the call/callee. 2955 if (!Callee) 2956 return true; 2957 2958 // Every call site can track active assumptions. 2959 getOrCreateAAFor<AAAssumptionInfo>(CBFnPos); 2960 2961 // Skip declarations except if annotations on their call sites were 2962 // explicitly requested. 2963 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() && 2964 !Callee->hasMetadata(LLVMContext::MD_callback)) 2965 return true; 2966 2967 if (!Callee->getReturnType()->isVoidTy() && !CB.use_empty()) { 2968 2969 IRPosition CBRetPos = IRPosition::callsite_returned(CB); 2970 bool UsedAssumedInformation = false; 2971 getAssumedSimplified(CBRetPos, nullptr, UsedAssumedInformation); 2972 } 2973 2974 for (int I = 0, E = CB.arg_size(); I < E; ++I) { 2975 2976 IRPosition CBArgPos = IRPosition::callsite_argument(CB, I); 2977 2978 // Every call site argument might be dead. 2979 getOrCreateAAFor<AAIsDead>(CBArgPos); 2980 2981 // Call site argument might be simplified. We have to go through the 2982 // Attributor interface though as outside AAs can register custom 2983 // simplification callbacks. 2984 bool UsedAssumedInformation = false; 2985 getAssumedSimplified(CBArgPos, /* AA */ nullptr, UsedAssumedInformation); 2986 2987 // Every call site argument might be marked "noundef". 2988 getOrCreateAAFor<AANoUndef>(CBArgPos); 2989 2990 if (!CB.getArgOperand(I)->getType()->isPointerTy()) 2991 continue; 2992 2993 // Call site argument attribute "non-null". 2994 getOrCreateAAFor<AANonNull>(CBArgPos); 2995 2996 // Call site argument attribute "nocapture". 2997 getOrCreateAAFor<AANoCapture>(CBArgPos); 2998 2999 // Call site argument attribute "no-alias". 3000 getOrCreateAAFor<AANoAlias>(CBArgPos); 3001 3002 // Call site argument attribute "dereferenceable". 3003 getOrCreateAAFor<AADereferenceable>(CBArgPos); 3004 3005 // Call site argument attribute "align". 3006 getOrCreateAAFor<AAAlign>(CBArgPos); 3007 3008 // Call site argument attribute 3009 // "readnone/readonly/writeonly/..." 3010 getOrCreateAAFor<AAMemoryBehavior>(CBArgPos); 3011 3012 // Call site argument attribute "nofree". 3013 getOrCreateAAFor<AANoFree>(CBArgPos); 3014 } 3015 return true; 3016 }; 3017 3018 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); 3019 bool Success; 3020 bool UsedAssumedInformation = false; 3021 Success = checkForAllInstructionsImpl( 3022 nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr, 3023 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, 3024 (unsigned)Instruction::Call}, 3025 UsedAssumedInformation); 3026 (void)Success; 3027 assert(Success && "Expected the check call to be successful!"); 3028 3029 auto LoadStorePred = [&](Instruction &I) -> bool { 3030 if (isa<LoadInst>(I)) { 3031 getOrCreateAAFor<AAAlign>( 3032 IRPosition::value(*cast<LoadInst>(I).getPointerOperand())); 3033 if (SimplifyAllLoads) 3034 getAssumedSimplified(IRPosition::value(I), nullptr, 3035 UsedAssumedInformation); 3036 } else { 3037 auto &SI = cast<StoreInst>(I); 3038 getOrCreateAAFor<AAIsDead>(IRPosition::inst(I)); 3039 getAssumedSimplified(IRPosition::value(*SI.getValueOperand()), nullptr, 3040 UsedAssumedInformation); 3041 getOrCreateAAFor<AAAlign>(IRPosition::value(*SI.getPointerOperand())); 3042 } 3043 return true; 3044 }; 3045 Success = checkForAllInstructionsImpl( 3046 nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr, 3047 {(unsigned)Instruction::Load, (unsigned)Instruction::Store}, 3048 UsedAssumedInformation); 3049 (void)Success; 3050 assert(Success && "Expected the check call to be successful!"); 3051 } 3052 3053 /// Helpers to ease debugging through output streams and print calls. 3054 /// 3055 ///{ 3056 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { 3057 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); 3058 } 3059 3060 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { 3061 switch (AP) { 3062 case IRPosition::IRP_INVALID: 3063 return OS << "inv"; 3064 case IRPosition::IRP_FLOAT: 3065 return OS << "flt"; 3066 case IRPosition::IRP_RETURNED: 3067 return OS << "fn_ret"; 3068 case IRPosition::IRP_CALL_SITE_RETURNED: 3069 return OS << "cs_ret"; 3070 case IRPosition::IRP_FUNCTION: 3071 return OS << "fn"; 3072 case IRPosition::IRP_CALL_SITE: 3073 return OS << "cs"; 3074 case IRPosition::IRP_ARGUMENT: 3075 return OS << "arg"; 3076 case IRPosition::IRP_CALL_SITE_ARGUMENT: 3077 return OS << "cs_arg"; 3078 } 3079 llvm_unreachable("Unknown attribute position!"); 3080 } 3081 3082 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { 3083 const Value &AV = Pos.getAssociatedValue(); 3084 OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" 3085 << Pos.getAnchorValue().getName() << "@" << Pos.getCallSiteArgNo() << "]"; 3086 3087 if (Pos.hasCallBaseContext()) 3088 OS << "[cb_context:" << *Pos.getCallBaseContext() << "]"; 3089 return OS << "}"; 3090 } 3091 3092 raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) { 3093 OS << "range-state(" << S.getBitWidth() << ")<"; 3094 S.getKnown().print(OS); 3095 OS << " / "; 3096 S.getAssumed().print(OS); 3097 OS << ">"; 3098 3099 return OS << static_cast<const AbstractState &>(S); 3100 } 3101 3102 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { 3103 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); 3104 } 3105 3106 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { 3107 AA.print(OS); 3108 return OS; 3109 } 3110 3111 raw_ostream &llvm::operator<<(raw_ostream &OS, 3112 const PotentialConstantIntValuesState &S) { 3113 OS << "set-state(< {"; 3114 if (!S.isValidState()) 3115 OS << "full-set"; 3116 else { 3117 for (auto &It : S.getAssumedSet()) 3118 OS << It << ", "; 3119 if (S.undefIsContained()) 3120 OS << "undef "; 3121 } 3122 OS << "} >)"; 3123 3124 return OS; 3125 } 3126 3127 void AbstractAttribute::print(raw_ostream &OS) const { 3128 OS << "["; 3129 OS << getName(); 3130 OS << "] for CtxI "; 3131 3132 if (auto *I = getCtxI()) { 3133 OS << "'"; 3134 I->print(OS); 3135 OS << "'"; 3136 } else 3137 OS << "<<null inst>>"; 3138 3139 OS << " at position " << getIRPosition() << " with state " << getAsStr() 3140 << '\n'; 3141 } 3142 3143 void AbstractAttribute::printWithDeps(raw_ostream &OS) const { 3144 print(OS); 3145 3146 for (const auto &DepAA : Deps) { 3147 auto *AA = DepAA.getPointer(); 3148 OS << " updates "; 3149 AA->print(OS); 3150 } 3151 3152 OS << '\n'; 3153 } 3154 3155 raw_ostream &llvm::operator<<(raw_ostream &OS, 3156 const AAPointerInfo::Access &Acc) { 3157 OS << " [" << Acc.getKind() << "] " << *Acc.getRemoteInst(); 3158 if (Acc.getLocalInst() != Acc.getRemoteInst()) 3159 OS << " via " << *Acc.getLocalInst(); 3160 if (Acc.getContent().hasValue()) { 3161 if (*Acc.getContent()) 3162 OS << " [" << **Acc.getContent() << "]"; 3163 else 3164 OS << " [ <unknown> ]"; 3165 } 3166 return OS; 3167 } 3168 ///} 3169 3170 /// ---------------------------------------------------------------------------- 3171 /// Pass (Manager) Boilerplate 3172 /// ---------------------------------------------------------------------------- 3173 3174 static bool runAttributorOnFunctions(InformationCache &InfoCache, 3175 SetVector<Function *> &Functions, 3176 AnalysisGetter &AG, 3177 CallGraphUpdater &CGUpdater, 3178 bool DeleteFns, bool IsModulePass) { 3179 if (Functions.empty()) 3180 return false; 3181 3182 LLVM_DEBUG({ 3183 dbgs() << "[Attributor] Run on module with " << Functions.size() 3184 << " functions:\n"; 3185 for (Function *Fn : Functions) 3186 dbgs() << " - " << Fn->getName() << "\n"; 3187 }); 3188 3189 // Create an Attributor and initially empty information cache that is filled 3190 // while we identify default attribute opportunities. 3191 AttributorConfig AC(CGUpdater); 3192 AC.IsModulePass = IsModulePass; 3193 AC.DeleteFns = DeleteFns; 3194 Attributor A(Functions, InfoCache, AC); 3195 3196 // Create shallow wrappers for all functions that are not IPO amendable 3197 if (AllowShallowWrappers) 3198 for (Function *F : Functions) 3199 if (!A.isFunctionIPOAmendable(*F)) 3200 Attributor::createShallowWrapper(*F); 3201 3202 // Internalize non-exact functions 3203 // TODO: for now we eagerly internalize functions without calculating the 3204 // cost, we need a cost interface to determine whether internalizing 3205 // a function is "benefitial" 3206 if (AllowDeepWrapper) { 3207 unsigned FunSize = Functions.size(); 3208 for (unsigned u = 0; u < FunSize; u++) { 3209 Function *F = Functions[u]; 3210 if (!F->isDeclaration() && !F->isDefinitionExact() && F->getNumUses() && 3211 !GlobalValue::isInterposableLinkage(F->getLinkage())) { 3212 Function *NewF = Attributor::internalizeFunction(*F); 3213 assert(NewF && "Could not internalize function."); 3214 Functions.insert(NewF); 3215 3216 // Update call graph 3217 CGUpdater.replaceFunctionWith(*F, *NewF); 3218 for (const Use &U : NewF->uses()) 3219 if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) { 3220 auto *CallerF = CB->getCaller(); 3221 CGUpdater.reanalyzeFunction(*CallerF); 3222 } 3223 } 3224 } 3225 } 3226 3227 for (Function *F : Functions) { 3228 if (F->hasExactDefinition()) 3229 NumFnWithExactDefinition++; 3230 else 3231 NumFnWithoutExactDefinition++; 3232 3233 // We look at internal functions only on-demand but if any use is not a 3234 // direct call or outside the current set of analyzed functions, we have 3235 // to do it eagerly. 3236 if (F->hasLocalLinkage()) { 3237 if (llvm::all_of(F->uses(), [&Functions](const Use &U) { 3238 const auto *CB = dyn_cast<CallBase>(U.getUser()); 3239 return CB && CB->isCallee(&U) && 3240 Functions.count(const_cast<Function *>(CB->getCaller())); 3241 })) 3242 continue; 3243 } 3244 3245 // Populate the Attributor with abstract attribute opportunities in the 3246 // function and the information cache with IR information. 3247 A.identifyDefaultAbstractAttributes(*F); 3248 } 3249 3250 ChangeStatus Changed = A.run(); 3251 3252 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size() 3253 << " functions, result: " << Changed << ".\n"); 3254 return Changed == ChangeStatus::CHANGED; 3255 } 3256 3257 void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); } 3258 3259 void AADepGraph::dumpGraph() { 3260 static std::atomic<int> CallTimes; 3261 std::string Prefix; 3262 3263 if (!DepGraphDotFileNamePrefix.empty()) 3264 Prefix = DepGraphDotFileNamePrefix; 3265 else 3266 Prefix = "dep_graph"; 3267 std::string Filename = 3268 Prefix + "_" + std::to_string(CallTimes.load()) + ".dot"; 3269 3270 outs() << "Dependency graph dump to " << Filename << ".\n"; 3271 3272 std::error_code EC; 3273 3274 raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF); 3275 if (!EC) 3276 llvm::WriteGraph(File, this); 3277 3278 CallTimes++; 3279 } 3280 3281 void AADepGraph::print() { 3282 for (auto DepAA : SyntheticRoot.Deps) 3283 cast<AbstractAttribute>(DepAA.getPointer())->printWithDeps(outs()); 3284 } 3285 3286 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { 3287 FunctionAnalysisManager &FAM = 3288 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 3289 AnalysisGetter AG(FAM); 3290 3291 SetVector<Function *> Functions; 3292 for (Function &F : M) 3293 Functions.insert(&F); 3294 3295 CallGraphUpdater CGUpdater; 3296 BumpPtrAllocator Allocator; 3297 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr); 3298 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater, 3299 /* DeleteFns */ true, /* IsModulePass */ true)) { 3300 // FIXME: Think about passes we will preserve and add them here. 3301 return PreservedAnalyses::none(); 3302 } 3303 return PreservedAnalyses::all(); 3304 } 3305 3306 PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C, 3307 CGSCCAnalysisManager &AM, 3308 LazyCallGraph &CG, 3309 CGSCCUpdateResult &UR) { 3310 FunctionAnalysisManager &FAM = 3311 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager(); 3312 AnalysisGetter AG(FAM); 3313 3314 SetVector<Function *> Functions; 3315 for (LazyCallGraph::Node &N : C) 3316 Functions.insert(&N.getFunction()); 3317 3318 if (Functions.empty()) 3319 return PreservedAnalyses::all(); 3320 3321 Module &M = *Functions.back()->getParent(); 3322 CallGraphUpdater CGUpdater; 3323 CGUpdater.initialize(CG, C, AM, UR); 3324 BumpPtrAllocator Allocator; 3325 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions); 3326 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater, 3327 /* DeleteFns */ false, 3328 /* IsModulePass */ false)) { 3329 // FIXME: Think about passes we will preserve and add them here. 3330 PreservedAnalyses PA; 3331 PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 3332 return PA; 3333 } 3334 return PreservedAnalyses::all(); 3335 } 3336 3337 namespace llvm { 3338 3339 template <> struct GraphTraits<AADepGraphNode *> { 3340 using NodeRef = AADepGraphNode *; 3341 using DepTy = PointerIntPair<AADepGraphNode *, 1>; 3342 using EdgeRef = PointerIntPair<AADepGraphNode *, 1>; 3343 3344 static NodeRef getEntryNode(AADepGraphNode *DGN) { return DGN; } 3345 static NodeRef DepGetVal(DepTy &DT) { return DT.getPointer(); } 3346 3347 using ChildIteratorType = 3348 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>; 3349 using ChildEdgeIteratorType = TinyPtrVector<DepTy>::iterator; 3350 3351 static ChildIteratorType child_begin(NodeRef N) { return N->child_begin(); } 3352 3353 static ChildIteratorType child_end(NodeRef N) { return N->child_end(); } 3354 }; 3355 3356 template <> 3357 struct GraphTraits<AADepGraph *> : public GraphTraits<AADepGraphNode *> { 3358 static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); } 3359 3360 using nodes_iterator = 3361 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>; 3362 3363 static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); } 3364 3365 static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); } 3366 }; 3367 3368 template <> struct DOTGraphTraits<AADepGraph *> : public DefaultDOTGraphTraits { 3369 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} 3370 3371 static std::string getNodeLabel(const AADepGraphNode *Node, 3372 const AADepGraph *DG) { 3373 std::string AAString; 3374 raw_string_ostream O(AAString); 3375 Node->print(O); 3376 return AAString; 3377 } 3378 }; 3379 3380 } // end namespace llvm 3381 3382 namespace { 3383 3384 struct AttributorLegacyPass : public ModulePass { 3385 static char ID; 3386 3387 AttributorLegacyPass() : ModulePass(ID) { 3388 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); 3389 } 3390 3391 bool runOnModule(Module &M) override { 3392 if (skipModule(M)) 3393 return false; 3394 3395 AnalysisGetter AG; 3396 SetVector<Function *> Functions; 3397 for (Function &F : M) 3398 Functions.insert(&F); 3399 3400 CallGraphUpdater CGUpdater; 3401 BumpPtrAllocator Allocator; 3402 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr); 3403 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater, 3404 /* DeleteFns*/ true, 3405 /* IsModulePass */ true); 3406 } 3407 3408 void getAnalysisUsage(AnalysisUsage &AU) const override { 3409 // FIXME: Think about passes we will preserve and add them here. 3410 AU.addRequired<TargetLibraryInfoWrapperPass>(); 3411 } 3412 }; 3413 3414 struct AttributorCGSCCLegacyPass : public CallGraphSCCPass { 3415 static char ID; 3416 3417 AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) { 3418 initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry()); 3419 } 3420 3421 bool runOnSCC(CallGraphSCC &SCC) override { 3422 if (skipSCC(SCC)) 3423 return false; 3424 3425 SetVector<Function *> Functions; 3426 for (CallGraphNode *CGN : SCC) 3427 if (Function *Fn = CGN->getFunction()) 3428 if (!Fn->isDeclaration()) 3429 Functions.insert(Fn); 3430 3431 if (Functions.empty()) 3432 return false; 3433 3434 AnalysisGetter AG; 3435 CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph()); 3436 CallGraphUpdater CGUpdater; 3437 CGUpdater.initialize(CG, SCC); 3438 Module &M = *Functions.back()->getParent(); 3439 BumpPtrAllocator Allocator; 3440 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions); 3441 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater, 3442 /* DeleteFns */ false, 3443 /* IsModulePass */ false); 3444 } 3445 3446 void getAnalysisUsage(AnalysisUsage &AU) const override { 3447 // FIXME: Think about passes we will preserve and add them here. 3448 AU.addRequired<TargetLibraryInfoWrapperPass>(); 3449 CallGraphSCCPass::getAnalysisUsage(AU); 3450 } 3451 }; 3452 3453 } // end anonymous namespace 3454 3455 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } 3456 Pass *llvm::createAttributorCGSCCLegacyPass() { 3457 return new AttributorCGSCCLegacyPass(); 3458 } 3459 3460 char AttributorLegacyPass::ID = 0; 3461 char AttributorCGSCCLegacyPass::ID = 0; 3462 3463 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", 3464 "Deduce and propagate attributes", false, false) 3465 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 3466 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", 3467 "Deduce and propagate attributes", false, false) 3468 INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc", 3469 "Deduce and propagate attributes (CGSCC pass)", false, 3470 false) 3471 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 3472 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 3473 INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc", 3474 "Deduce and propagate attributes (CGSCC pass)", false, 3475 false) 3476