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