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