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