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