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