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