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