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