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