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