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