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