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