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