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