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