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