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