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