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