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 inter procedural pass that deduces and/or propagating 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/DepthFirstIterator.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/SmallPtrSet.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/Analysis/CallGraph.h" 24 #include "llvm/Analysis/CallGraphSCCPass.h" 25 #include "llvm/Analysis/CaptureTracking.h" 26 #include "llvm/Analysis/EHPersonalities.h" 27 #include "llvm/Analysis/GlobalsModRef.h" 28 #include "llvm/Analysis/LazyValueInfo.h" 29 #include "llvm/Analysis/Loads.h" 30 #include "llvm/Analysis/MemoryBuiltins.h" 31 #include "llvm/Analysis/ScalarEvolution.h" 32 #include "llvm/Analysis/ValueTracking.h" 33 #include "llvm/IR/Argument.h" 34 #include "llvm/IR/Attributes.h" 35 #include "llvm/IR/CFG.h" 36 #include "llvm/IR/IRBuilder.h" 37 #include "llvm/IR/InstIterator.h" 38 #include "llvm/IR/IntrinsicInst.h" 39 #include "llvm/IR/Verifier.h" 40 #include "llvm/InitializePasses.h" 41 #include "llvm/IR/NoFolder.h" 42 #include "llvm/Support/CommandLine.h" 43 #include "llvm/Support/Debug.h" 44 #include "llvm/Support/raw_ostream.h" 45 #include "llvm/Transforms/IPO/ArgumentPromotion.h" 46 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 47 #include "llvm/Transforms/Utils/Local.h" 48 49 #include <cassert> 50 51 using namespace llvm; 52 53 #define DEBUG_TYPE "attributor" 54 55 STATISTIC(NumFnWithExactDefinition, 56 "Number of function with exact definitions"); 57 STATISTIC(NumFnWithoutExactDefinition, 58 "Number of function without exact definitions"); 59 STATISTIC(NumAttributesTimedOut, 60 "Number of abstract attributes timed out before fixpoint"); 61 STATISTIC(NumAttributesValidFixpoint, 62 "Number of abstract attributes in a valid fixpoint state"); 63 STATISTIC(NumAttributesManifested, 64 "Number of abstract attributes manifested in IR"); 65 STATISTIC(NumAttributesFixedDueToRequiredDependences, 66 "Number of abstract attributes fixed due to required dependences"); 67 68 // Some helper macros to deal with statistics tracking. 69 // 70 // Usage: 71 // For simple IR attribute tracking overload trackStatistics in the abstract 72 // attribute and choose the right STATS_DECLTRACK_********* macro, 73 // e.g.,: 74 // void trackStatistics() const override { 75 // STATS_DECLTRACK_ARG_ATTR(returned) 76 // } 77 // If there is a single "increment" side one can use the macro 78 // STATS_DECLTRACK with a custom message. If there are multiple increment 79 // sides, STATS_DECL and STATS_TRACK can also be used separatly. 80 // 81 #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \ 82 ("Number of " #TYPE " marked '" #NAME "'") 83 #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME 84 #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG); 85 #define STATS_DECL(NAME, TYPE, MSG) \ 86 STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG); 87 #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE)); 88 #define STATS_DECLTRACK(NAME, TYPE, MSG) \ 89 { \ 90 STATS_DECL(NAME, TYPE, MSG) \ 91 STATS_TRACK(NAME, TYPE) \ 92 } 93 #define STATS_DECLTRACK_ARG_ATTR(NAME) \ 94 STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME)) 95 #define STATS_DECLTRACK_CSARG_ATTR(NAME) \ 96 STATS_DECLTRACK(NAME, CSArguments, \ 97 BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME)) 98 #define STATS_DECLTRACK_FN_ATTR(NAME) \ 99 STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME)) 100 #define STATS_DECLTRACK_CS_ATTR(NAME) \ 101 STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME)) 102 #define STATS_DECLTRACK_FNRET_ATTR(NAME) \ 103 STATS_DECLTRACK(NAME, FunctionReturn, \ 104 BUILD_STAT_MSG_IR_ATTR(function returns, NAME)) 105 #define STATS_DECLTRACK_CSRET_ATTR(NAME) \ 106 STATS_DECLTRACK(NAME, CSReturn, \ 107 BUILD_STAT_MSG_IR_ATTR(call site returns, NAME)) 108 #define STATS_DECLTRACK_FLOATING_ATTR(NAME) \ 109 STATS_DECLTRACK(NAME, Floating, \ 110 ("Number of floating values known to be '" #NAME "'")) 111 112 // Specialization of the operator<< for abstract attributes subclasses. This 113 // disambiguates situations where multiple operators are applicable. 114 namespace llvm { 115 #define PIPE_OPERATOR(CLASS) \ 116 raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) { \ 117 return OS << static_cast<const AbstractAttribute &>(AA); \ 118 } 119 120 PIPE_OPERATOR(AAIsDead) 121 PIPE_OPERATOR(AANoUnwind) 122 PIPE_OPERATOR(AANoSync) 123 PIPE_OPERATOR(AANoRecurse) 124 PIPE_OPERATOR(AAWillReturn) 125 PIPE_OPERATOR(AANoReturn) 126 PIPE_OPERATOR(AAReturnedValues) 127 PIPE_OPERATOR(AANonNull) 128 PIPE_OPERATOR(AANoAlias) 129 PIPE_OPERATOR(AADereferenceable) 130 PIPE_OPERATOR(AAAlign) 131 PIPE_OPERATOR(AANoCapture) 132 PIPE_OPERATOR(AAValueSimplify) 133 PIPE_OPERATOR(AANoFree) 134 PIPE_OPERATOR(AAHeapToStack) 135 PIPE_OPERATOR(AAReachability) 136 PIPE_OPERATOR(AAMemoryBehavior) 137 PIPE_OPERATOR(AAMemoryLocation) 138 PIPE_OPERATOR(AAValueConstantRange) 139 PIPE_OPERATOR(AAPrivatizablePtr) 140 141 #undef PIPE_OPERATOR 142 } // namespace llvm 143 144 // TODO: Determine a good default value. 145 // 146 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads 147 // (when run with the first 5 abstract attributes). The results also indicate 148 // that we never reach 32 iterations but always find a fixpoint sooner. 149 // 150 // This will become more evolved once we perform two interleaved fixpoint 151 // iterations: bottom-up and top-down. 152 static cl::opt<unsigned> 153 MaxFixpointIterations("attributor-max-iterations", cl::Hidden, 154 cl::desc("Maximal number of fixpoint iterations."), 155 cl::init(32)); 156 static cl::opt<bool> VerifyMaxFixpointIterations( 157 "attributor-max-iterations-verify", cl::Hidden, 158 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"), 159 cl::init(false)); 160 161 static cl::opt<bool> DisableAttributor( 162 "attributor-disable", cl::Hidden, 163 cl::desc("Disable the attributor inter-procedural deduction pass."), 164 cl::init(true)); 165 166 static cl::opt<bool> AnnotateDeclarationCallSites( 167 "attributor-annotate-decl-cs", cl::Hidden, 168 cl::desc("Annotate call sites of function declarations."), cl::init(false)); 169 170 static cl::opt<bool> ManifestInternal( 171 "attributor-manifest-internal", cl::Hidden, 172 cl::desc("Manifest Attributor internal string attributes."), 173 cl::init(false)); 174 175 static cl::opt<unsigned> DepRecInterval( 176 "attributor-dependence-recompute-interval", cl::Hidden, 177 cl::desc("Number of iterations until dependences are recomputed."), 178 cl::init(4)); 179 180 static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion", 181 cl::init(true), cl::Hidden); 182 183 static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128), 184 cl::Hidden); 185 186 /// Logic operators for the change status enum class. 187 /// 188 ///{ 189 ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) { 190 return l == ChangeStatus::CHANGED ? l : r; 191 } 192 ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) { 193 return l == ChangeStatus::UNCHANGED ? l : r; 194 } 195 ///} 196 197 Argument *IRPosition::getAssociatedArgument() const { 198 if (getPositionKind() == IRP_ARGUMENT) 199 return cast<Argument>(&getAnchorValue()); 200 201 // Not an Argument and no argument number means this is not a call site 202 // argument, thus we cannot find a callback argument to return. 203 int ArgNo = getArgNo(); 204 if (ArgNo < 0) 205 return nullptr; 206 207 // Use abstract call sites to make the connection between the call site 208 // values and the ones in callbacks. If a callback was found that makes use 209 // of the underlying call site operand, we want the corresponding callback 210 // callee argument and not the direct callee argument. 211 Optional<Argument *> CBCandidateArg; 212 SmallVector<const Use *, 4> CBUses; 213 ImmutableCallSite ICS(&getAnchorValue()); 214 AbstractCallSite::getCallbackUses(ICS, CBUses); 215 for (const Use *U : CBUses) { 216 AbstractCallSite ACS(U); 217 assert(ACS && ACS.isCallbackCall()); 218 if (!ACS.getCalledFunction()) 219 continue; 220 221 for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) { 222 223 // Test if the underlying call site operand is argument number u of the 224 // callback callee. 225 if (ACS.getCallArgOperandNo(u) != ArgNo) 226 continue; 227 228 assert(ACS.getCalledFunction()->arg_size() > u && 229 "ACS mapped into var-args arguments!"); 230 if (CBCandidateArg.hasValue()) { 231 CBCandidateArg = nullptr; 232 break; 233 } 234 CBCandidateArg = ACS.getCalledFunction()->getArg(u); 235 } 236 } 237 238 // If we found a unique callback candidate argument, return it. 239 if (CBCandidateArg.hasValue() && CBCandidateArg.getValue()) 240 return CBCandidateArg.getValue(); 241 242 // If no callbacks were found, or none used the underlying call site operand 243 // exclusively, use the direct callee argument if available. 244 const Function *Callee = ICS.getCalledFunction(); 245 if (Callee && Callee->arg_size() > unsigned(ArgNo)) 246 return Callee->getArg(ArgNo); 247 248 return nullptr; 249 } 250 251 static Optional<Constant *> getAssumedConstant(Attributor &A, const Value &V, 252 const AbstractAttribute &AA, 253 bool &UsedAssumedInformation) { 254 const auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>( 255 AA, IRPosition::value(V), /* TrackDependence */ false); 256 Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A); 257 bool IsKnown = ValueSimplifyAA.isKnown(); 258 UsedAssumedInformation |= !IsKnown; 259 if (!SimplifiedV.hasValue()) { 260 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); 261 return llvm::None; 262 } 263 if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) { 264 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); 265 return llvm::None; 266 } 267 Constant *CI = dyn_cast_or_null<Constant>(SimplifiedV.getValue()); 268 if (CI && CI->getType() != V.getType()) { 269 // TODO: Check for a save conversion. 270 return nullptr; 271 } 272 if (CI) 273 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL); 274 return CI; 275 } 276 277 static Optional<ConstantInt *> 278 getAssumedConstantInt(Attributor &A, const Value &V, 279 const AbstractAttribute &AA, 280 bool &UsedAssumedInformation) { 281 Optional<Constant *> C = getAssumedConstant(A, V, AA, UsedAssumedInformation); 282 if (C.hasValue()) 283 return dyn_cast_or_null<ConstantInt>(C.getValue()); 284 return llvm::None; 285 } 286 287 /// Get pointer operand of memory accessing instruction. If \p I is 288 /// not a memory accessing instruction, return nullptr. If \p AllowVolatile, 289 /// is set to false and the instruction is volatile, return nullptr. 290 static const Value *getPointerOperand(const Instruction *I, 291 bool AllowVolatile) { 292 if (auto *LI = dyn_cast<LoadInst>(I)) { 293 if (!AllowVolatile && LI->isVolatile()) 294 return nullptr; 295 return LI->getPointerOperand(); 296 } 297 298 if (auto *SI = dyn_cast<StoreInst>(I)) { 299 if (!AllowVolatile && SI->isVolatile()) 300 return nullptr; 301 return SI->getPointerOperand(); 302 } 303 304 if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) { 305 if (!AllowVolatile && CXI->isVolatile()) 306 return nullptr; 307 return CXI->getPointerOperand(); 308 } 309 310 if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) { 311 if (!AllowVolatile && RMWI->isVolatile()) 312 return nullptr; 313 return RMWI->getPointerOperand(); 314 } 315 316 return nullptr; 317 } 318 319 /// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and 320 /// advanced by \p Offset bytes. To aid later analysis the method tries to build 321 /// getelement pointer instructions that traverse the natural type of \p Ptr if 322 /// possible. If that fails, the remaining offset is adjusted byte-wise, hence 323 /// through a cast to i8*. 324 /// 325 /// TODO: This could probably live somewhere more prominantly if it doesn't 326 /// already exist. 327 static Value *constructPointer(Type *ResTy, Value *Ptr, int64_t Offset, 328 IRBuilder<NoFolder> &IRB, const DataLayout &DL) { 329 assert(Offset >= 0 && "Negative offset not supported yet!"); 330 LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset 331 << "-bytes as " << *ResTy << "\n"); 332 333 // The initial type we are trying to traverse to get nice GEPs. 334 Type *Ty = Ptr->getType(); 335 336 SmallVector<Value *, 4> Indices; 337 std::string GEPName = Ptr->getName().str(); 338 while (Offset) { 339 uint64_t Idx, Rem; 340 341 if (auto *STy = dyn_cast<StructType>(Ty)) { 342 const StructLayout *SL = DL.getStructLayout(STy); 343 if (int64_t(SL->getSizeInBytes()) < Offset) 344 break; 345 Idx = SL->getElementContainingOffset(Offset); 346 assert(Idx < STy->getNumElements() && "Offset calculation error!"); 347 Rem = Offset - SL->getElementOffset(Idx); 348 Ty = STy->getElementType(Idx); 349 } else if (auto *PTy = dyn_cast<PointerType>(Ty)) { 350 Ty = PTy->getElementType(); 351 if (!Ty->isSized()) 352 break; 353 uint64_t ElementSize = DL.getTypeAllocSize(Ty); 354 assert(ElementSize && "Expected type with size!"); 355 Idx = Offset / ElementSize; 356 Rem = Offset % ElementSize; 357 } else { 358 // Non-aggregate type, we cast and make byte-wise progress now. 359 break; 360 } 361 362 LLVM_DEBUG(errs() << "Ty: " << *Ty << " Offset: " << Offset 363 << " Idx: " << Idx << " Rem: " << Rem << "\n"); 364 365 GEPName += "." + std::to_string(Idx); 366 Indices.push_back(ConstantInt::get(IRB.getInt32Ty(), Idx)); 367 Offset = Rem; 368 } 369 370 // Create a GEP if we collected indices above. 371 if (Indices.size()) 372 Ptr = IRB.CreateGEP(Ptr, Indices, GEPName); 373 374 // If an offset is left we use byte-wise adjustment. 375 if (Offset) { 376 Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy()); 377 Ptr = IRB.CreateGEP(Ptr, IRB.getInt32(Offset), 378 GEPName + ".b" + Twine(Offset)); 379 } 380 381 // Ensure the result has the requested type. 382 Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast"); 383 384 LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n"); 385 return Ptr; 386 } 387 388 /// Recursively visit all values that might become \p IRP at some point. This 389 /// will be done by looking through cast instructions, selects, phis, and calls 390 /// with the "returned" attribute. Once we cannot look through the value any 391 /// further, the callback \p VisitValueCB is invoked and passed the current 392 /// value, the \p State, and a flag to indicate if we stripped anything. 393 /// Stripped means that we unpacked the value associated with \p IRP at least 394 /// once. Note that the value used for the callback may still be the value 395 /// associated with \p IRP (due to PHIs). To limit how much effort is invested, 396 /// we will never visit more values than specified by \p MaxValues. 397 template <typename AAType, typename StateTy> 398 static bool genericValueTraversal( 399 Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State, 400 const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB, 401 int MaxValues = 8, const function_ref<Value *(Value *)> StripCB = nullptr) { 402 403 const AAIsDead *LivenessAA = nullptr; 404 if (IRP.getAnchorScope()) 405 LivenessAA = &A.getAAFor<AAIsDead>( 406 QueryingAA, IRPosition::function(*IRP.getAnchorScope()), 407 /* TrackDependence */ false); 408 bool AnyDead = false; 409 410 // TODO: Use Positions here to allow context sensitivity in VisitValueCB 411 SmallPtrSet<Value *, 16> Visited; 412 SmallVector<Value *, 16> Worklist; 413 Worklist.push_back(&IRP.getAssociatedValue()); 414 415 int Iteration = 0; 416 do { 417 Value *V = Worklist.pop_back_val(); 418 if (StripCB) 419 V = StripCB(V); 420 421 // Check if we should process the current value. To prevent endless 422 // recursion keep a record of the values we followed! 423 if (!Visited.insert(V).second) 424 continue; 425 426 // Make sure we limit the compile time for complex expressions. 427 if (Iteration++ >= MaxValues) 428 return false; 429 430 // Explicitly look through calls with a "returned" attribute if we do 431 // not have a pointer as stripPointerCasts only works on them. 432 Value *NewV = nullptr; 433 if (V->getType()->isPointerTy()) { 434 NewV = V->stripPointerCasts(); 435 } else { 436 CallSite CS(V); 437 if (CS && CS.getCalledFunction()) { 438 for (Argument &Arg : CS.getCalledFunction()->args()) 439 if (Arg.hasReturnedAttr()) { 440 NewV = CS.getArgOperand(Arg.getArgNo()); 441 break; 442 } 443 } 444 } 445 if (NewV && NewV != V) { 446 Worklist.push_back(NewV); 447 continue; 448 } 449 450 // Look through select instructions, visit both potential values. 451 if (auto *SI = dyn_cast<SelectInst>(V)) { 452 Worklist.push_back(SI->getTrueValue()); 453 Worklist.push_back(SI->getFalseValue()); 454 continue; 455 } 456 457 // Look through phi nodes, visit all live operands. 458 if (auto *PHI = dyn_cast<PHINode>(V)) { 459 assert(LivenessAA && 460 "Expected liveness in the presence of instructions!"); 461 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { 462 const BasicBlock *IncomingBB = PHI->getIncomingBlock(u); 463 if (A.isAssumedDead(*IncomingBB->getTerminator(), &QueryingAA, 464 LivenessAA, 465 /* CheckBBLivenessOnly */ true)) { 466 AnyDead = true; 467 continue; 468 } 469 Worklist.push_back(PHI->getIncomingValue(u)); 470 } 471 continue; 472 } 473 474 // Once a leaf is reached we inform the user through the callback. 475 if (!VisitValueCB(*V, State, Iteration > 1)) 476 return false; 477 } while (!Worklist.empty()); 478 479 // If we actually used liveness information so we have to record a dependence. 480 if (AnyDead) 481 A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL); 482 483 // All values have been visited. 484 return true; 485 } 486 487 /// Return true if \p New is equal or worse than \p Old. 488 static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { 489 if (!Old.isIntAttribute()) 490 return true; 491 492 return Old.getValueAsInt() >= New.getValueAsInt(); 493 } 494 495 /// Return true if the information provided by \p Attr was added to the 496 /// attribute list \p Attrs. This is only the case if it was not already present 497 /// in \p Attrs at the position describe by \p PK and \p AttrIdx. 498 static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, 499 AttributeList &Attrs, int AttrIdx) { 500 501 if (Attr.isEnumAttribute()) { 502 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 503 if (Attrs.hasAttribute(AttrIdx, Kind)) 504 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) 505 return false; 506 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); 507 return true; 508 } 509 if (Attr.isStringAttribute()) { 510 StringRef Kind = Attr.getKindAsString(); 511 if (Attrs.hasAttribute(AttrIdx, Kind)) 512 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) 513 return false; 514 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); 515 return true; 516 } 517 if (Attr.isIntAttribute()) { 518 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 519 if (Attrs.hasAttribute(AttrIdx, Kind)) 520 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) 521 return false; 522 Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind); 523 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); 524 return true; 525 } 526 527 llvm_unreachable("Expected enum or string attribute!"); 528 } 529 530 static const Value * 531 getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset, 532 const DataLayout &DL, 533 bool AllowNonInbounds = false) { 534 const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false); 535 if (!Ptr) 536 return nullptr; 537 538 return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL, 539 AllowNonInbounds); 540 } 541 542 ChangeStatus AbstractAttribute::update(Attributor &A) { 543 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 544 if (getState().isAtFixpoint()) 545 return HasChanged; 546 547 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n"); 548 549 HasChanged = updateImpl(A); 550 551 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this 552 << "\n"); 553 554 return HasChanged; 555 } 556 557 ChangeStatus 558 IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP, 559 const ArrayRef<Attribute> &DeducedAttrs) { 560 Function *ScopeFn = IRP.getAssociatedFunction(); 561 IRPosition::Kind PK = IRP.getPositionKind(); 562 563 // In the following some generic code that will manifest attributes in 564 // DeducedAttrs if they improve the current IR. Due to the different 565 // annotation positions we use the underlying AttributeList interface. 566 567 AttributeList Attrs; 568 switch (PK) { 569 case IRPosition::IRP_INVALID: 570 case IRPosition::IRP_FLOAT: 571 return ChangeStatus::UNCHANGED; 572 case IRPosition::IRP_ARGUMENT: 573 case IRPosition::IRP_FUNCTION: 574 case IRPosition::IRP_RETURNED: 575 Attrs = ScopeFn->getAttributes(); 576 break; 577 case IRPosition::IRP_CALL_SITE: 578 case IRPosition::IRP_CALL_SITE_RETURNED: 579 case IRPosition::IRP_CALL_SITE_ARGUMENT: 580 Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes(); 581 break; 582 } 583 584 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 585 LLVMContext &Ctx = IRP.getAnchorValue().getContext(); 586 for (const Attribute &Attr : DeducedAttrs) { 587 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx())) 588 continue; 589 590 HasChanged = ChangeStatus::CHANGED; 591 } 592 593 if (HasChanged == ChangeStatus::UNCHANGED) 594 return HasChanged; 595 596 switch (PK) { 597 case IRPosition::IRP_ARGUMENT: 598 case IRPosition::IRP_FUNCTION: 599 case IRPosition::IRP_RETURNED: 600 ScopeFn->setAttributes(Attrs); 601 break; 602 case IRPosition::IRP_CALL_SITE: 603 case IRPosition::IRP_CALL_SITE_RETURNED: 604 case IRPosition::IRP_CALL_SITE_ARGUMENT: 605 CallSite(&IRP.getAnchorValue()).setAttributes(Attrs); 606 break; 607 case IRPosition::IRP_INVALID: 608 case IRPosition::IRP_FLOAT: 609 break; 610 } 611 612 return HasChanged; 613 } 614 615 const IRPosition IRPosition::EmptyKey(255); 616 const IRPosition IRPosition::TombstoneKey(256); 617 618 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) { 619 IRPositions.emplace_back(IRP); 620 621 ImmutableCallSite ICS(&IRP.getAnchorValue()); 622 switch (IRP.getPositionKind()) { 623 case IRPosition::IRP_INVALID: 624 case IRPosition::IRP_FLOAT: 625 case IRPosition::IRP_FUNCTION: 626 return; 627 case IRPosition::IRP_ARGUMENT: 628 case IRPosition::IRP_RETURNED: 629 IRPositions.emplace_back( 630 IRPosition::function(*IRP.getAssociatedFunction())); 631 return; 632 case IRPosition::IRP_CALL_SITE: 633 assert(ICS && "Expected call site!"); 634 // TODO: We need to look at the operand bundles similar to the redirection 635 // in CallBase. 636 if (!ICS.hasOperandBundles()) 637 if (const Function *Callee = ICS.getCalledFunction()) 638 IRPositions.emplace_back(IRPosition::function(*Callee)); 639 return; 640 case IRPosition::IRP_CALL_SITE_RETURNED: 641 assert(ICS && "Expected call site!"); 642 // TODO: We need to look at the operand bundles similar to the redirection 643 // in CallBase. 644 if (!ICS.hasOperandBundles()) { 645 if (const Function *Callee = ICS.getCalledFunction()) { 646 IRPositions.emplace_back(IRPosition::returned(*Callee)); 647 IRPositions.emplace_back(IRPosition::function(*Callee)); 648 for (const Argument &Arg : Callee->args()) 649 if (Arg.hasReturnedAttr()) { 650 IRPositions.emplace_back( 651 IRPosition::callsite_argument(ICS, Arg.getArgNo())); 652 IRPositions.emplace_back( 653 IRPosition::value(*ICS.getArgOperand(Arg.getArgNo()))); 654 IRPositions.emplace_back(IRPosition::argument(Arg)); 655 } 656 } 657 } 658 IRPositions.emplace_back( 659 IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction()))); 660 return; 661 case IRPosition::IRP_CALL_SITE_ARGUMENT: { 662 int ArgNo = IRP.getArgNo(); 663 assert(ICS && ArgNo >= 0 && "Expected call site!"); 664 // TODO: We need to look at the operand bundles similar to the redirection 665 // in CallBase. 666 if (!ICS.hasOperandBundles()) { 667 const Function *Callee = ICS.getCalledFunction(); 668 if (Callee && Callee->arg_size() > unsigned(ArgNo)) 669 IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo))); 670 if (Callee) 671 IRPositions.emplace_back(IRPosition::function(*Callee)); 672 } 673 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue())); 674 return; 675 } 676 } 677 } 678 679 bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs, 680 bool IgnoreSubsumingPositions) const { 681 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { 682 for (Attribute::AttrKind AK : AKs) 683 if (EquivIRP.getAttr(AK).getKindAsEnum() == AK) 684 return true; 685 // The first position returned by the SubsumingPositionIterator is 686 // always the position itself. If we ignore subsuming positions we 687 // are done after the first iteration. 688 if (IgnoreSubsumingPositions) 689 break; 690 } 691 return false; 692 } 693 694 void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs, 695 SmallVectorImpl<Attribute> &Attrs, 696 bool IgnoreSubsumingPositions) const { 697 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { 698 for (Attribute::AttrKind AK : AKs) { 699 const Attribute &Attr = EquivIRP.getAttr(AK); 700 if (Attr.getKindAsEnum() == AK) 701 Attrs.push_back(Attr); 702 } 703 // The first position returned by the SubsumingPositionIterator is 704 // always the position itself. If we ignore subsuming positions we 705 // are done after the first iteration. 706 if (IgnoreSubsumingPositions) 707 break; 708 } 709 } 710 711 void IRPosition::verify() { 712 switch (KindOrArgNo) { 713 default: 714 assert(KindOrArgNo >= 0 && "Expected argument or call site argument!"); 715 assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) && 716 "Expected call base or argument for positive attribute index!"); 717 if (isa<Argument>(AnchorVal)) { 718 assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) && 719 "Argument number mismatch!"); 720 assert(cast<Argument>(AnchorVal) == &getAssociatedValue() && 721 "Associated value mismatch!"); 722 } else { 723 assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) && 724 "Call site argument number mismatch!"); 725 assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) == 726 &getAssociatedValue() && 727 "Associated value mismatch!"); 728 } 729 break; 730 case IRP_INVALID: 731 assert(!AnchorVal && "Expected no value for an invalid position!"); 732 break; 733 case IRP_FLOAT: 734 assert((!isa<CallBase>(&getAssociatedValue()) && 735 !isa<Argument>(&getAssociatedValue())) && 736 "Expected specialized kind for call base and argument values!"); 737 break; 738 case IRP_RETURNED: 739 assert(isa<Function>(AnchorVal) && 740 "Expected function for a 'returned' position!"); 741 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 742 break; 743 case IRP_CALL_SITE_RETURNED: 744 assert((isa<CallBase>(AnchorVal)) && 745 "Expected call base for 'call site returned' position!"); 746 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 747 break; 748 case IRP_CALL_SITE: 749 assert((isa<CallBase>(AnchorVal)) && 750 "Expected call base for 'call site function' position!"); 751 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 752 break; 753 case IRP_FUNCTION: 754 assert(isa<Function>(AnchorVal) && 755 "Expected function for a 'function' position!"); 756 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 757 break; 758 } 759 } 760 761 namespace { 762 763 /// Helper function to clamp a state \p S of type \p StateType with the 764 /// information in \p R and indicate/return if \p S did change (as-in update is 765 /// required to be run again). 766 template <typename StateType> 767 ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) { 768 auto Assumed = S.getAssumed(); 769 S ^= R; 770 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED 771 : ChangeStatus::CHANGED; 772 } 773 774 /// Clamp the information known for all returned values of a function 775 /// (identified by \p QueryingAA) into \p S. 776 template <typename AAType, typename StateType = typename AAType::StateType> 777 static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA, 778 StateType &S) { 779 LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for " 780 << QueryingAA << " into " << S << "\n"); 781 782 assert((QueryingAA.getIRPosition().getPositionKind() == 783 IRPosition::IRP_RETURNED || 784 QueryingAA.getIRPosition().getPositionKind() == 785 IRPosition::IRP_CALL_SITE_RETURNED) && 786 "Can only clamp returned value states for a function returned or call " 787 "site returned position!"); 788 789 // Use an optional state as there might not be any return values and we want 790 // to join (IntegerState::operator&) the state of all there are. 791 Optional<StateType> T; 792 793 // Callback for each possibly returned value. 794 auto CheckReturnValue = [&](Value &RV) -> bool { 795 const IRPosition &RVPos = IRPosition::value(RV); 796 const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos); 797 LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr() 798 << " @ " << RVPos << "\n"); 799 const StateType &AAS = static_cast<const StateType &>(AA.getState()); 800 if (T.hasValue()) 801 *T &= AAS; 802 else 803 T = AAS; 804 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T 805 << "\n"); 806 return T->isValidState(); 807 }; 808 809 if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA)) 810 S.indicatePessimisticFixpoint(); 811 else if (T.hasValue()) 812 S ^= *T; 813 } 814 815 /// Helper class to compose two generic deduction 816 template <typename AAType, typename Base, typename StateType, 817 template <typename...> class F, template <typename...> class G> 818 struct AAComposeTwoGenericDeduction 819 : public F<AAType, G<AAType, Base, StateType>, StateType> { 820 AAComposeTwoGenericDeduction(const IRPosition &IRP) 821 : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {} 822 823 /// See AbstractAttribute::updateImpl(...). 824 ChangeStatus updateImpl(Attributor &A) override { 825 ChangeStatus ChangedF = 826 F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A); 827 ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A); 828 return ChangedF | ChangedG; 829 } 830 }; 831 832 /// Helper class for generic deduction: return value -> returned position. 833 template <typename AAType, typename Base, 834 typename StateType = typename Base::StateType> 835 struct AAReturnedFromReturnedValues : public Base { 836 AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {} 837 838 /// See AbstractAttribute::updateImpl(...). 839 ChangeStatus updateImpl(Attributor &A) override { 840 StateType S(StateType::getBestState(this->getState())); 841 clampReturnedValueStates<AAType, StateType>(A, *this, S); 842 // TODO: If we know we visited all returned values, thus no are assumed 843 // dead, we can take the known information from the state T. 844 return clampStateAndIndicateChange<StateType>(this->getState(), S); 845 } 846 }; 847 848 /// Clamp the information known at all call sites for a given argument 849 /// (identified by \p QueryingAA) into \p S. 850 template <typename AAType, typename StateType = typename AAType::StateType> 851 static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA, 852 StateType &S) { 853 LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for " 854 << QueryingAA << " into " << S << "\n"); 855 856 assert(QueryingAA.getIRPosition().getPositionKind() == 857 IRPosition::IRP_ARGUMENT && 858 "Can only clamp call site argument states for an argument position!"); 859 860 // Use an optional state as there might not be any return values and we want 861 // to join (IntegerState::operator&) the state of all there are. 862 Optional<StateType> T; 863 864 // The argument number which is also the call site argument number. 865 unsigned ArgNo = QueryingAA.getIRPosition().getArgNo(); 866 867 auto CallSiteCheck = [&](AbstractCallSite ACS) { 868 const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); 869 // Check if a coresponding argument was found or if it is on not associated 870 // (which can happen for callback calls). 871 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) 872 return false; 873 874 const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos); 875 LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction() 876 << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n"); 877 const StateType &AAS = static_cast<const StateType &>(AA.getState()); 878 if (T.hasValue()) 879 *T &= AAS; 880 else 881 T = AAS; 882 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T 883 << "\n"); 884 return T->isValidState(); 885 }; 886 887 bool AllCallSitesKnown; 888 if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true, 889 AllCallSitesKnown)) 890 S.indicatePessimisticFixpoint(); 891 else if (T.hasValue()) 892 S ^= *T; 893 } 894 895 /// Helper class for generic deduction: call site argument -> argument position. 896 template <typename AAType, typename Base, 897 typename StateType = typename AAType::StateType> 898 struct AAArgumentFromCallSiteArguments : public Base { 899 AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {} 900 901 /// See AbstractAttribute::updateImpl(...). 902 ChangeStatus updateImpl(Attributor &A) override { 903 StateType S(StateType::getBestState(this->getState())); 904 clampCallSiteArgumentStates<AAType, StateType>(A, *this, S); 905 // TODO: If we know we visited all incoming values, thus no are assumed 906 // dead, we can take the known information from the state T. 907 return clampStateAndIndicateChange<StateType>(this->getState(), S); 908 } 909 }; 910 911 /// Helper class for generic replication: function returned -> cs returned. 912 template <typename AAType, typename Base, 913 typename StateType = typename Base::StateType> 914 struct AACallSiteReturnedFromReturned : public Base { 915 AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {} 916 917 /// See AbstractAttribute::updateImpl(...). 918 ChangeStatus updateImpl(Attributor &A) override { 919 assert(this->getIRPosition().getPositionKind() == 920 IRPosition::IRP_CALL_SITE_RETURNED && 921 "Can only wrap function returned positions for call site returned " 922 "positions!"); 923 auto &S = this->getState(); 924 925 const Function *AssociatedFunction = 926 this->getIRPosition().getAssociatedFunction(); 927 if (!AssociatedFunction) 928 return S.indicatePessimisticFixpoint(); 929 930 IRPosition FnPos = IRPosition::returned(*AssociatedFunction); 931 const AAType &AA = A.getAAFor<AAType>(*this, FnPos); 932 return clampStateAndIndicateChange( 933 S, static_cast<const StateType &>(AA.getState())); 934 } 935 }; 936 937 /// Helper class for generic deduction using must-be-executed-context 938 /// Base class is required to have `followUse` method. 939 940 /// bool followUse(Attributor &A, const Use *U, const Instruction *I) 941 /// U - Underlying use. 942 /// I - The user of the \p U. 943 /// `followUse` returns true if the value should be tracked transitively. 944 945 template <typename AAType, typename Base, 946 typename StateType = typename AAType::StateType> 947 struct AAFromMustBeExecutedContext : public Base { 948 AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {} 949 950 void initialize(Attributor &A) override { 951 Base::initialize(A); 952 const IRPosition &IRP = this->getIRPosition(); 953 Instruction *CtxI = IRP.getCtxI(); 954 955 if (!CtxI) 956 return; 957 958 for (const Use &U : IRP.getAssociatedValue().uses()) 959 Uses.insert(&U); 960 } 961 962 /// See AbstractAttribute::updateImpl(...). 963 ChangeStatus updateImpl(Attributor &A) override { 964 auto BeforeState = this->getState(); 965 auto &S = this->getState(); 966 Instruction *CtxI = this->getIRPosition().getCtxI(); 967 if (!CtxI) 968 return ChangeStatus::UNCHANGED; 969 970 MustBeExecutedContextExplorer &Explorer = 971 A.getInfoCache().getMustBeExecutedContextExplorer(); 972 973 auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI); 974 for (unsigned u = 0; u < Uses.size(); ++u) { 975 const Use *U = Uses[u]; 976 if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) { 977 bool Found = Explorer.findInContextOf(UserI, EIt, EEnd); 978 if (Found && Base::followUse(A, U, UserI)) 979 for (const Use &Us : UserI->uses()) 980 Uses.insert(&Us); 981 } 982 } 983 984 return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED; 985 } 986 987 private: 988 /// Container for (transitive) uses of the associated value. 989 SetVector<const Use *> Uses; 990 }; 991 992 template <typename AAType, typename Base, 993 typename StateType = typename AAType::StateType> 994 using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext = 995 AAComposeTwoGenericDeduction<AAType, Base, StateType, 996 AAFromMustBeExecutedContext, 997 AAArgumentFromCallSiteArguments>; 998 999 template <typename AAType, typename Base, 1000 typename StateType = typename AAType::StateType> 1001 using AACallSiteReturnedFromReturnedAndMustBeExecutedContext = 1002 AAComposeTwoGenericDeduction<AAType, Base, StateType, 1003 AAFromMustBeExecutedContext, 1004 AACallSiteReturnedFromReturned>; 1005 1006 /// -----------------------NoUnwind Function Attribute-------------------------- 1007 1008 struct AANoUnwindImpl : AANoUnwind { 1009 AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {} 1010 1011 const std::string getAsStr() const override { 1012 return getAssumed() ? "nounwind" : "may-unwind"; 1013 } 1014 1015 /// See AbstractAttribute::updateImpl(...). 1016 ChangeStatus updateImpl(Attributor &A) override { 1017 auto Opcodes = { 1018 (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, 1019 (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet, 1020 (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; 1021 1022 auto CheckForNoUnwind = [&](Instruction &I) { 1023 if (!I.mayThrow()) 1024 return true; 1025 1026 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { 1027 const auto &NoUnwindAA = 1028 A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS)); 1029 return NoUnwindAA.isAssumedNoUnwind(); 1030 } 1031 return false; 1032 }; 1033 1034 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes)) 1035 return indicatePessimisticFixpoint(); 1036 1037 return ChangeStatus::UNCHANGED; 1038 } 1039 }; 1040 1041 struct AANoUnwindFunction final : public AANoUnwindImpl { 1042 AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} 1043 1044 /// See AbstractAttribute::trackStatistics() 1045 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) } 1046 }; 1047 1048 /// NoUnwind attribute deduction for a call sites. 1049 struct AANoUnwindCallSite final : AANoUnwindImpl { 1050 AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} 1051 1052 /// See AbstractAttribute::initialize(...). 1053 void initialize(Attributor &A) override { 1054 AANoUnwindImpl::initialize(A); 1055 Function *F = getAssociatedFunction(); 1056 if (!F) 1057 indicatePessimisticFixpoint(); 1058 } 1059 1060 /// See AbstractAttribute::updateImpl(...). 1061 ChangeStatus updateImpl(Attributor &A) override { 1062 // TODO: Once we have call site specific value information we can provide 1063 // call site specific liveness information and then it makes 1064 // sense to specialize attributes for call sites arguments instead of 1065 // redirecting requests to the callee argument. 1066 Function *F = getAssociatedFunction(); 1067 const IRPosition &FnPos = IRPosition::function(*F); 1068 auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos); 1069 return clampStateAndIndicateChange( 1070 getState(), 1071 static_cast<const AANoUnwind::StateType &>(FnAA.getState())); 1072 } 1073 1074 /// See AbstractAttribute::trackStatistics() 1075 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); } 1076 }; 1077 1078 /// --------------------- Function Return Values ------------------------------- 1079 1080 /// "Attribute" that collects all potential returned values and the return 1081 /// instructions that they arise from. 1082 /// 1083 /// If there is a unique returned value R, the manifest method will: 1084 /// - mark R with the "returned" attribute, if R is an argument. 1085 class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { 1086 1087 /// Mapping of values potentially returned by the associated function to the 1088 /// return instructions that might return them. 1089 MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues; 1090 1091 /// Mapping to remember the number of returned values for a call site such 1092 /// that we can avoid updates if nothing changed. 1093 DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA; 1094 1095 /// Set of unresolved calls returned by the associated function. 1096 SmallSetVector<CallBase *, 4> UnresolvedCalls; 1097 1098 /// State flags 1099 /// 1100 ///{ 1101 bool IsFixed = false; 1102 bool IsValidState = true; 1103 ///} 1104 1105 public: 1106 AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {} 1107 1108 /// See AbstractAttribute::initialize(...). 1109 void initialize(Attributor &A) override { 1110 // Reset the state. 1111 IsFixed = false; 1112 IsValidState = true; 1113 ReturnedValues.clear(); 1114 1115 Function *F = getAssociatedFunction(); 1116 if (!F) { 1117 indicatePessimisticFixpoint(); 1118 return; 1119 } 1120 assert(!F->getReturnType()->isVoidTy() && 1121 "Did not expect a void return type!"); 1122 1123 // The map from instruction opcodes to those instructions in the function. 1124 auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); 1125 1126 // Look through all arguments, if one is marked as returned we are done. 1127 for (Argument &Arg : F->args()) { 1128 if (Arg.hasReturnedAttr()) { 1129 auto &ReturnInstSet = ReturnedValues[&Arg]; 1130 for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) 1131 ReturnInstSet.insert(cast<ReturnInst>(RI)); 1132 1133 indicateOptimisticFixpoint(); 1134 return; 1135 } 1136 } 1137 1138 if (!F->hasExactDefinition()) 1139 indicatePessimisticFixpoint(); 1140 } 1141 1142 /// See AbstractAttribute::manifest(...). 1143 ChangeStatus manifest(Attributor &A) override; 1144 1145 /// See AbstractAttribute::getState(...). 1146 AbstractState &getState() override { return *this; } 1147 1148 /// See AbstractAttribute::getState(...). 1149 const AbstractState &getState() const override { return *this; } 1150 1151 /// See AbstractAttribute::updateImpl(Attributor &A). 1152 ChangeStatus updateImpl(Attributor &A) override; 1153 1154 llvm::iterator_range<iterator> returned_values() override { 1155 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); 1156 } 1157 1158 llvm::iterator_range<const_iterator> returned_values() const override { 1159 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); 1160 } 1161 1162 const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override { 1163 return UnresolvedCalls; 1164 } 1165 1166 /// Return the number of potential return values, -1 if unknown. 1167 size_t getNumReturnValues() const override { 1168 return isValidState() ? ReturnedValues.size() : -1; 1169 } 1170 1171 /// Return an assumed unique return value if a single candidate is found. If 1172 /// there cannot be one, return a nullptr. If it is not clear yet, return the 1173 /// Optional::NoneType. 1174 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; 1175 1176 /// See AbstractState::checkForAllReturnedValues(...). 1177 bool checkForAllReturnedValuesAndReturnInsts( 1178 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> 1179 &Pred) const override; 1180 1181 /// Pretty print the attribute similar to the IR representation. 1182 const std::string getAsStr() const override; 1183 1184 /// See AbstractState::isAtFixpoint(). 1185 bool isAtFixpoint() const override { return IsFixed; } 1186 1187 /// See AbstractState::isValidState(). 1188 bool isValidState() const override { return IsValidState; } 1189 1190 /// See AbstractState::indicateOptimisticFixpoint(...). 1191 ChangeStatus indicateOptimisticFixpoint() override { 1192 IsFixed = true; 1193 return ChangeStatus::UNCHANGED; 1194 } 1195 1196 ChangeStatus indicatePessimisticFixpoint() override { 1197 IsFixed = true; 1198 IsValidState = false; 1199 return ChangeStatus::CHANGED; 1200 } 1201 }; 1202 1203 ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { 1204 ChangeStatus Changed = ChangeStatus::UNCHANGED; 1205 1206 // Bookkeeping. 1207 assert(isValidState()); 1208 STATS_DECLTRACK(KnownReturnValues, FunctionReturn, 1209 "Number of function with known return values"); 1210 1211 // Check if we have an assumed unique return value that we could manifest. 1212 Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); 1213 1214 if (!UniqueRV.hasValue() || !UniqueRV.getValue()) 1215 return Changed; 1216 1217 // Bookkeeping. 1218 STATS_DECLTRACK(UniqueReturnValue, FunctionReturn, 1219 "Number of function with unique return"); 1220 1221 // Callback to replace the uses of CB with the constant C. 1222 auto ReplaceCallSiteUsersWith = [&A](CallBase &CB, Constant &C) { 1223 if (CB.getNumUses() == 0 || CB.isMustTailCall()) 1224 return ChangeStatus::UNCHANGED; 1225 if (A.changeValueAfterManifest(CB, C)) 1226 return ChangeStatus::CHANGED; 1227 return ChangeStatus::UNCHANGED; 1228 }; 1229 1230 // If the assumed unique return value is an argument, annotate it. 1231 if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { 1232 // TODO: This should be handled differently! 1233 this->AnchorVal = UniqueRVArg; 1234 this->KindOrArgNo = UniqueRVArg->getArgNo(); 1235 Changed = IRAttribute::manifest(A); 1236 } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) { 1237 // We can replace the returned value with the unique returned constant. 1238 Value &AnchorValue = getAnchorValue(); 1239 if (Function *F = dyn_cast<Function>(&AnchorValue)) { 1240 for (const Use &U : F->uses()) 1241 if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) 1242 if (CB->isCallee(&U)) { 1243 Constant *RVCCast = 1244 CB->getType() == RVC->getType() 1245 ? RVC 1246 : ConstantExpr::getTruncOrBitCast(RVC, CB->getType()); 1247 Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed; 1248 } 1249 } else { 1250 assert(isa<CallBase>(AnchorValue) && 1251 "Expcected a function or call base anchor!"); 1252 Constant *RVCCast = 1253 AnchorValue.getType() == RVC->getType() 1254 ? RVC 1255 : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType()); 1256 Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast); 1257 } 1258 if (Changed == ChangeStatus::CHANGED) 1259 STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn, 1260 "Number of function returns replaced by constant return"); 1261 } 1262 1263 return Changed; 1264 } 1265 1266 const std::string AAReturnedValuesImpl::getAsStr() const { 1267 return (isAtFixpoint() ? "returns(#" : "may-return(#") + 1268 (isValidState() ? std::to_string(getNumReturnValues()) : "?") + 1269 ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]"; 1270 } 1271 1272 Optional<Value *> 1273 AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { 1274 // If checkForAllReturnedValues provides a unique value, ignoring potential 1275 // undef values that can also be present, it is assumed to be the actual 1276 // return value and forwarded to the caller of this method. If there are 1277 // multiple, a nullptr is returned indicating there cannot be a unique 1278 // returned value. 1279 Optional<Value *> UniqueRV; 1280 1281 auto Pred = [&](Value &RV) -> bool { 1282 // If we found a second returned value and neither the current nor the saved 1283 // one is an undef, there is no unique returned value. Undefs are special 1284 // since we can pretend they have any value. 1285 if (UniqueRV.hasValue() && UniqueRV != &RV && 1286 !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) { 1287 UniqueRV = nullptr; 1288 return false; 1289 } 1290 1291 // Do not overwrite a value with an undef. 1292 if (!UniqueRV.hasValue() || !isa<UndefValue>(RV)) 1293 UniqueRV = &RV; 1294 1295 return true; 1296 }; 1297 1298 if (!A.checkForAllReturnedValues(Pred, *this)) 1299 UniqueRV = nullptr; 1300 1301 return UniqueRV; 1302 } 1303 1304 bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( 1305 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> 1306 &Pred) const { 1307 if (!isValidState()) 1308 return false; 1309 1310 // Check all returned values but ignore call sites as long as we have not 1311 // encountered an overdefined one during an update. 1312 for (auto &It : ReturnedValues) { 1313 Value *RV = It.first; 1314 1315 CallBase *CB = dyn_cast<CallBase>(RV); 1316 if (CB && !UnresolvedCalls.count(CB)) 1317 continue; 1318 1319 if (!Pred(*RV, It.second)) 1320 return false; 1321 } 1322 1323 return true; 1324 } 1325 1326 ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { 1327 size_t NumUnresolvedCalls = UnresolvedCalls.size(); 1328 bool Changed = false; 1329 1330 // State used in the value traversals starting in returned values. 1331 struct RVState { 1332 // The map in which we collect return values -> return instrs. 1333 decltype(ReturnedValues) &RetValsMap; 1334 // The flag to indicate a change. 1335 bool &Changed; 1336 // The return instrs we come from. 1337 SmallSetVector<ReturnInst *, 4> RetInsts; 1338 }; 1339 1340 // Callback for a leaf value returned by the associated function. 1341 auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool { 1342 auto Size = RVS.RetValsMap[&Val].size(); 1343 RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end()); 1344 bool Inserted = RVS.RetValsMap[&Val].size() != Size; 1345 RVS.Changed |= Inserted; 1346 LLVM_DEBUG({ 1347 if (Inserted) 1348 dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val 1349 << " => " << RVS.RetInsts.size() << "\n"; 1350 }); 1351 return true; 1352 }; 1353 1354 // Helper method to invoke the generic value traversal. 1355 auto VisitReturnedValue = [&](Value &RV, RVState &RVS) { 1356 IRPosition RetValPos = IRPosition::value(RV); 1357 return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this, 1358 RVS, VisitValueCB); 1359 }; 1360 1361 // Callback for all "return intructions" live in the associated function. 1362 auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) { 1363 ReturnInst &Ret = cast<ReturnInst>(I); 1364 RVState RVS({ReturnedValues, Changed, {}}); 1365 RVS.RetInsts.insert(&Ret); 1366 return VisitReturnedValue(*Ret.getReturnValue(), RVS); 1367 }; 1368 1369 // Start by discovering returned values from all live returned instructions in 1370 // the associated function. 1371 if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret})) 1372 return indicatePessimisticFixpoint(); 1373 1374 // Once returned values "directly" present in the code are handled we try to 1375 // resolve returned calls. 1376 decltype(ReturnedValues) NewRVsMap; 1377 for (auto &It : ReturnedValues) { 1378 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first 1379 << " by #" << It.second.size() << " RIs\n"); 1380 CallBase *CB = dyn_cast<CallBase>(It.first); 1381 if (!CB || UnresolvedCalls.count(CB)) 1382 continue; 1383 1384 if (!CB->getCalledFunction()) { 1385 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB 1386 << "\n"); 1387 UnresolvedCalls.insert(CB); 1388 continue; 1389 } 1390 1391 // TODO: use the function scope once we have call site AAReturnedValues. 1392 const auto &RetValAA = A.getAAFor<AAReturnedValues>( 1393 *this, IRPosition::function(*CB->getCalledFunction())); 1394 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: " 1395 << RetValAA << "\n"); 1396 1397 // Skip dead ends, thus if we do not know anything about the returned 1398 // call we mark it as unresolved and it will stay that way. 1399 if (!RetValAA.getState().isValidState()) { 1400 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB 1401 << "\n"); 1402 UnresolvedCalls.insert(CB); 1403 continue; 1404 } 1405 1406 // Do not try to learn partial information. If the callee has unresolved 1407 // return values we will treat the call as unresolved/opaque. 1408 auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls(); 1409 if (!RetValAAUnresolvedCalls.empty()) { 1410 UnresolvedCalls.insert(CB); 1411 continue; 1412 } 1413 1414 // Now check if we can track transitively returned values. If possible, thus 1415 // if all return value can be represented in the current scope, do so. 1416 bool Unresolved = false; 1417 for (auto &RetValAAIt : RetValAA.returned_values()) { 1418 Value *RetVal = RetValAAIt.first; 1419 if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) || 1420 isa<Constant>(RetVal)) 1421 continue; 1422 // Anything that did not fit in the above categories cannot be resolved, 1423 // mark the call as unresolved. 1424 LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value " 1425 "cannot be translated: " 1426 << *RetVal << "\n"); 1427 UnresolvedCalls.insert(CB); 1428 Unresolved = true; 1429 break; 1430 } 1431 1432 if (Unresolved) 1433 continue; 1434 1435 // Now track transitively returned values. 1436 unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB]; 1437 if (NumRetAA == RetValAA.getNumReturnValues()) { 1438 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not " 1439 "changed since it was seen last\n"); 1440 continue; 1441 } 1442 NumRetAA = RetValAA.getNumReturnValues(); 1443 1444 for (auto &RetValAAIt : RetValAA.returned_values()) { 1445 Value *RetVal = RetValAAIt.first; 1446 if (Argument *Arg = dyn_cast<Argument>(RetVal)) { 1447 // Arguments are mapped to call site operands and we begin the traversal 1448 // again. 1449 bool Unused = false; 1450 RVState RVS({NewRVsMap, Unused, RetValAAIt.second}); 1451 VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS); 1452 continue; 1453 } else if (isa<CallBase>(RetVal)) { 1454 // Call sites are resolved by the callee attribute over time, no need to 1455 // do anything for us. 1456 continue; 1457 } else if (isa<Constant>(RetVal)) { 1458 // Constants are valid everywhere, we can simply take them. 1459 NewRVsMap[RetVal].insert(It.second.begin(), It.second.end()); 1460 continue; 1461 } 1462 } 1463 } 1464 1465 // To avoid modifications to the ReturnedValues map while we iterate over it 1466 // we kept record of potential new entries in a copy map, NewRVsMap. 1467 for (auto &It : NewRVsMap) { 1468 assert(!It.second.empty() && "Entry does not add anything."); 1469 auto &ReturnInsts = ReturnedValues[It.first]; 1470 for (ReturnInst *RI : It.second) 1471 if (ReturnInsts.insert(RI)) { 1472 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value " 1473 << *It.first << " => " << *RI << "\n"); 1474 Changed = true; 1475 } 1476 } 1477 1478 Changed |= (NumUnresolvedCalls != UnresolvedCalls.size()); 1479 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 1480 } 1481 1482 struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { 1483 AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} 1484 1485 /// See AbstractAttribute::trackStatistics() 1486 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) } 1487 }; 1488 1489 /// Returned values information for a call sites. 1490 struct AAReturnedValuesCallSite final : AAReturnedValuesImpl { 1491 AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} 1492 1493 /// See AbstractAttribute::initialize(...). 1494 void initialize(Attributor &A) override { 1495 // TODO: Once we have call site specific value information we can provide 1496 // call site specific liveness information and then it makes 1497 // sense to specialize attributes for call sites instead of 1498 // redirecting requests to the callee. 1499 llvm_unreachable("Abstract attributes for returned values are not " 1500 "supported for call sites yet!"); 1501 } 1502 1503 /// See AbstractAttribute::updateImpl(...). 1504 ChangeStatus updateImpl(Attributor &A) override { 1505 return indicatePessimisticFixpoint(); 1506 } 1507 1508 /// See AbstractAttribute::trackStatistics() 1509 void trackStatistics() const override {} 1510 }; 1511 1512 /// ------------------------ NoSync Function Attribute ------------------------- 1513 1514 struct AANoSyncImpl : AANoSync { 1515 AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {} 1516 1517 const std::string getAsStr() const override { 1518 return getAssumed() ? "nosync" : "may-sync"; 1519 } 1520 1521 /// See AbstractAttribute::updateImpl(...). 1522 ChangeStatus updateImpl(Attributor &A) override; 1523 1524 /// Helper function used to determine whether an instruction is non-relaxed 1525 /// atomic. In other words, if an atomic instruction does not have unordered 1526 /// or monotonic ordering 1527 static bool isNonRelaxedAtomic(Instruction *I); 1528 1529 /// Helper function used to determine whether an instruction is volatile. 1530 static bool isVolatile(Instruction *I); 1531 1532 /// Helper function uset to check if intrinsic is volatile (memcpy, memmove, 1533 /// memset). 1534 static bool isNoSyncIntrinsic(Instruction *I); 1535 }; 1536 1537 bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) { 1538 if (!I->isAtomic()) 1539 return false; 1540 1541 AtomicOrdering Ordering; 1542 switch (I->getOpcode()) { 1543 case Instruction::AtomicRMW: 1544 Ordering = cast<AtomicRMWInst>(I)->getOrdering(); 1545 break; 1546 case Instruction::Store: 1547 Ordering = cast<StoreInst>(I)->getOrdering(); 1548 break; 1549 case Instruction::Load: 1550 Ordering = cast<LoadInst>(I)->getOrdering(); 1551 break; 1552 case Instruction::Fence: { 1553 auto *FI = cast<FenceInst>(I); 1554 if (FI->getSyncScopeID() == SyncScope::SingleThread) 1555 return false; 1556 Ordering = FI->getOrdering(); 1557 break; 1558 } 1559 case Instruction::AtomicCmpXchg: { 1560 AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering(); 1561 AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering(); 1562 // Only if both are relaxed, than it can be treated as relaxed. 1563 // Otherwise it is non-relaxed. 1564 if (Success != AtomicOrdering::Unordered && 1565 Success != AtomicOrdering::Monotonic) 1566 return true; 1567 if (Failure != AtomicOrdering::Unordered && 1568 Failure != AtomicOrdering::Monotonic) 1569 return true; 1570 return false; 1571 } 1572 default: 1573 llvm_unreachable( 1574 "New atomic operations need to be known in the attributor."); 1575 } 1576 1577 // Relaxed. 1578 if (Ordering == AtomicOrdering::Unordered || 1579 Ordering == AtomicOrdering::Monotonic) 1580 return false; 1581 return true; 1582 } 1583 1584 /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics. 1585 /// FIXME: We should ipmrove the handling of intrinsics. 1586 bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) { 1587 if (auto *II = dyn_cast<IntrinsicInst>(I)) { 1588 switch (II->getIntrinsicID()) { 1589 /// Element wise atomic memory intrinsics are can only be unordered, 1590 /// therefore nosync. 1591 case Intrinsic::memset_element_unordered_atomic: 1592 case Intrinsic::memmove_element_unordered_atomic: 1593 case Intrinsic::memcpy_element_unordered_atomic: 1594 return true; 1595 case Intrinsic::memset: 1596 case Intrinsic::memmove: 1597 case Intrinsic::memcpy: 1598 if (!cast<MemIntrinsic>(II)->isVolatile()) 1599 return true; 1600 return false; 1601 default: 1602 return false; 1603 } 1604 } 1605 return false; 1606 } 1607 1608 bool AANoSyncImpl::isVolatile(Instruction *I) { 1609 assert(!ImmutableCallSite(I) && !isa<CallBase>(I) && 1610 "Calls should not be checked here"); 1611 1612 switch (I->getOpcode()) { 1613 case Instruction::AtomicRMW: 1614 return cast<AtomicRMWInst>(I)->isVolatile(); 1615 case Instruction::Store: 1616 return cast<StoreInst>(I)->isVolatile(); 1617 case Instruction::Load: 1618 return cast<LoadInst>(I)->isVolatile(); 1619 case Instruction::AtomicCmpXchg: 1620 return cast<AtomicCmpXchgInst>(I)->isVolatile(); 1621 default: 1622 return false; 1623 } 1624 } 1625 1626 ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { 1627 1628 auto CheckRWInstForNoSync = [&](Instruction &I) { 1629 /// We are looking for volatile instructions or Non-Relaxed atomics. 1630 /// FIXME: We should improve the handling of intrinsics. 1631 1632 if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I)) 1633 return true; 1634 1635 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { 1636 if (ICS.hasFnAttr(Attribute::NoSync)) 1637 return true; 1638 1639 const auto &NoSyncAA = 1640 A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS)); 1641 if (NoSyncAA.isAssumedNoSync()) 1642 return true; 1643 return false; 1644 } 1645 1646 if (!isVolatile(&I) && !isNonRelaxedAtomic(&I)) 1647 return true; 1648 1649 return false; 1650 }; 1651 1652 auto CheckForNoSync = [&](Instruction &I) { 1653 // At this point we handled all read/write effects and they are all 1654 // nosync, so they can be skipped. 1655 if (I.mayReadOrWriteMemory()) 1656 return true; 1657 1658 // non-convergent and readnone imply nosync. 1659 return !ImmutableCallSite(&I).isConvergent(); 1660 }; 1661 1662 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) || 1663 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this)) 1664 return indicatePessimisticFixpoint(); 1665 1666 return ChangeStatus::UNCHANGED; 1667 } 1668 1669 struct AANoSyncFunction final : public AANoSyncImpl { 1670 AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {} 1671 1672 /// See AbstractAttribute::trackStatistics() 1673 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) } 1674 }; 1675 1676 /// NoSync attribute deduction for a call sites. 1677 struct AANoSyncCallSite final : AANoSyncImpl { 1678 AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {} 1679 1680 /// See AbstractAttribute::initialize(...). 1681 void initialize(Attributor &A) override { 1682 AANoSyncImpl::initialize(A); 1683 Function *F = getAssociatedFunction(); 1684 if (!F) 1685 indicatePessimisticFixpoint(); 1686 } 1687 1688 /// See AbstractAttribute::updateImpl(...). 1689 ChangeStatus updateImpl(Attributor &A) override { 1690 // TODO: Once we have call site specific value information we can provide 1691 // call site specific liveness information and then it makes 1692 // sense to specialize attributes for call sites arguments instead of 1693 // redirecting requests to the callee argument. 1694 Function *F = getAssociatedFunction(); 1695 const IRPosition &FnPos = IRPosition::function(*F); 1696 auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos); 1697 return clampStateAndIndicateChange( 1698 getState(), static_cast<const AANoSync::StateType &>(FnAA.getState())); 1699 } 1700 1701 /// See AbstractAttribute::trackStatistics() 1702 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); } 1703 }; 1704 1705 /// ------------------------ No-Free Attributes ---------------------------- 1706 1707 struct AANoFreeImpl : public AANoFree { 1708 AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {} 1709 1710 /// See AbstractAttribute::updateImpl(...). 1711 ChangeStatus updateImpl(Attributor &A) override { 1712 auto CheckForNoFree = [&](Instruction &I) { 1713 ImmutableCallSite ICS(&I); 1714 if (ICS.hasFnAttr(Attribute::NoFree)) 1715 return true; 1716 1717 const auto &NoFreeAA = 1718 A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS)); 1719 return NoFreeAA.isAssumedNoFree(); 1720 }; 1721 1722 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this)) 1723 return indicatePessimisticFixpoint(); 1724 return ChangeStatus::UNCHANGED; 1725 } 1726 1727 /// See AbstractAttribute::getAsStr(). 1728 const std::string getAsStr() const override { 1729 return getAssumed() ? "nofree" : "may-free"; 1730 } 1731 }; 1732 1733 struct AANoFreeFunction final : public AANoFreeImpl { 1734 AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {} 1735 1736 /// See AbstractAttribute::trackStatistics() 1737 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) } 1738 }; 1739 1740 /// NoFree attribute deduction for a call sites. 1741 struct AANoFreeCallSite final : AANoFreeImpl { 1742 AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {} 1743 1744 /// See AbstractAttribute::initialize(...). 1745 void initialize(Attributor &A) override { 1746 AANoFreeImpl::initialize(A); 1747 Function *F = getAssociatedFunction(); 1748 if (!F) 1749 indicatePessimisticFixpoint(); 1750 } 1751 1752 /// See AbstractAttribute::updateImpl(...). 1753 ChangeStatus updateImpl(Attributor &A) override { 1754 // TODO: Once we have call site specific value information we can provide 1755 // call site specific liveness information and then it makes 1756 // sense to specialize attributes for call sites arguments instead of 1757 // redirecting requests to the callee argument. 1758 Function *F = getAssociatedFunction(); 1759 const IRPosition &FnPos = IRPosition::function(*F); 1760 auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos); 1761 return clampStateAndIndicateChange( 1762 getState(), static_cast<const AANoFree::StateType &>(FnAA.getState())); 1763 } 1764 1765 /// See AbstractAttribute::trackStatistics() 1766 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); } 1767 }; 1768 1769 /// NoFree attribute for floating values. 1770 struct AANoFreeFloating : AANoFreeImpl { 1771 AANoFreeFloating(const IRPosition &IRP) : AANoFreeImpl(IRP) {} 1772 1773 /// See AbstractAttribute::trackStatistics() 1774 void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)} 1775 1776 /// See Abstract Attribute::updateImpl(...). 1777 ChangeStatus updateImpl(Attributor &A) override { 1778 const IRPosition &IRP = getIRPosition(); 1779 1780 const auto &NoFreeAA = 1781 A.getAAFor<AANoFree>(*this, IRPosition::function_scope(IRP)); 1782 if (NoFreeAA.isAssumedNoFree()) 1783 return ChangeStatus::UNCHANGED; 1784 1785 Value &AssociatedValue = getIRPosition().getAssociatedValue(); 1786 auto Pred = [&](const Use &U, bool &Follow) -> bool { 1787 Instruction *UserI = cast<Instruction>(U.getUser()); 1788 if (auto *CB = dyn_cast<CallBase>(UserI)) { 1789 if (CB->isBundleOperand(&U)) 1790 return false; 1791 if (!CB->isArgOperand(&U)) 1792 return true; 1793 unsigned ArgNo = CB->getArgOperandNo(&U); 1794 1795 const auto &NoFreeArg = A.getAAFor<AANoFree>( 1796 *this, IRPosition::callsite_argument(*CB, ArgNo)); 1797 return NoFreeArg.isAssumedNoFree(); 1798 } 1799 1800 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || 1801 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { 1802 Follow = true; 1803 return true; 1804 } 1805 if (isa<ReturnInst>(UserI)) 1806 return true; 1807 1808 // Unknown user. 1809 return false; 1810 }; 1811 if (!A.checkForAllUses(Pred, *this, AssociatedValue)) 1812 return indicatePessimisticFixpoint(); 1813 1814 return ChangeStatus::UNCHANGED; 1815 } 1816 }; 1817 1818 /// NoFree attribute for a call site argument. 1819 struct AANoFreeArgument final : AANoFreeFloating { 1820 AANoFreeArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {} 1821 1822 /// See AbstractAttribute::trackStatistics() 1823 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) } 1824 }; 1825 1826 /// NoFree attribute for call site arguments. 1827 struct AANoFreeCallSiteArgument final : AANoFreeFloating { 1828 AANoFreeCallSiteArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {} 1829 1830 /// See AbstractAttribute::updateImpl(...). 1831 ChangeStatus updateImpl(Attributor &A) override { 1832 // TODO: Once we have call site specific value information we can provide 1833 // call site specific liveness information and then it makes 1834 // sense to specialize attributes for call sites arguments instead of 1835 // redirecting requests to the callee argument. 1836 Argument *Arg = getAssociatedArgument(); 1837 if (!Arg) 1838 return indicatePessimisticFixpoint(); 1839 const IRPosition &ArgPos = IRPosition::argument(*Arg); 1840 auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos); 1841 return clampStateAndIndicateChange( 1842 getState(), static_cast<const AANoFree::StateType &>(ArgAA.getState())); 1843 } 1844 1845 /// See AbstractAttribute::trackStatistics() 1846 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)}; 1847 }; 1848 1849 /// NoFree attribute for function return value. 1850 struct AANoFreeReturned final : AANoFreeFloating { 1851 AANoFreeReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) { 1852 llvm_unreachable("NoFree is not applicable to function returns!"); 1853 } 1854 1855 /// See AbstractAttribute::initialize(...). 1856 void initialize(Attributor &A) override { 1857 llvm_unreachable("NoFree is not applicable to function returns!"); 1858 } 1859 1860 /// See AbstractAttribute::updateImpl(...). 1861 ChangeStatus updateImpl(Attributor &A) override { 1862 llvm_unreachable("NoFree is not applicable to function returns!"); 1863 } 1864 1865 /// See AbstractAttribute::trackStatistics() 1866 void trackStatistics() const override {} 1867 }; 1868 1869 /// NoFree attribute deduction for a call site return value. 1870 struct AANoFreeCallSiteReturned final : AANoFreeFloating { 1871 AANoFreeCallSiteReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {} 1872 1873 ChangeStatus manifest(Attributor &A) override { 1874 return ChangeStatus::UNCHANGED; 1875 } 1876 /// See AbstractAttribute::trackStatistics() 1877 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) } 1878 }; 1879 1880 /// ------------------------ NonNull Argument Attribute ------------------------ 1881 static int64_t getKnownNonNullAndDerefBytesForUse( 1882 Attributor &A, AbstractAttribute &QueryingAA, Value &AssociatedValue, 1883 const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) { 1884 TrackUse = false; 1885 1886 const Value *UseV = U->get(); 1887 if (!UseV->getType()->isPointerTy()) 1888 return 0; 1889 1890 Type *PtrTy = UseV->getType(); 1891 const Function *F = I->getFunction(); 1892 bool NullPointerIsDefined = 1893 F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true; 1894 const DataLayout &DL = A.getInfoCache().getDL(); 1895 if (ImmutableCallSite ICS = ImmutableCallSite(I)) { 1896 if (ICS.isBundleOperand(U)) 1897 return 0; 1898 1899 if (ICS.isCallee(U)) { 1900 IsNonNull |= !NullPointerIsDefined; 1901 return 0; 1902 } 1903 1904 unsigned ArgNo = ICS.getArgumentNo(U); 1905 IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); 1906 // As long as we only use known information there is no need to track 1907 // dependences here. 1908 auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP, 1909 /* TrackDependence */ false); 1910 IsNonNull |= DerefAA.isKnownNonNull(); 1911 return DerefAA.getKnownDereferenceableBytes(); 1912 } 1913 1914 // We need to follow common pointer manipulation uses to the accesses they 1915 // feed into. We can try to be smart to avoid looking through things we do not 1916 // like for now, e.g., non-inbounds GEPs. 1917 if (isa<CastInst>(I)) { 1918 TrackUse = true; 1919 return 0; 1920 } 1921 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) 1922 if (GEP->hasAllConstantIndices()) { 1923 TrackUse = true; 1924 return 0; 1925 } 1926 1927 int64_t Offset; 1928 if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) { 1929 if (Base == &AssociatedValue && 1930 getPointerOperand(I, /* AllowVolatile */ false) == UseV) { 1931 int64_t DerefBytes = 1932 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset; 1933 1934 IsNonNull |= !NullPointerIsDefined; 1935 return std::max(int64_t(0), DerefBytes); 1936 } 1937 } 1938 1939 /// Corner case when an offset is 0. 1940 if (const Value *Base = getBasePointerOfAccessPointerOperand( 1941 I, Offset, DL, /*AllowNonInbounds*/ true)) { 1942 if (Offset == 0 && Base == &AssociatedValue && 1943 getPointerOperand(I, /* AllowVolatile */ false) == UseV) { 1944 int64_t DerefBytes = 1945 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()); 1946 IsNonNull |= !NullPointerIsDefined; 1947 return std::max(int64_t(0), DerefBytes); 1948 } 1949 } 1950 1951 return 0; 1952 } 1953 1954 struct AANonNullImpl : AANonNull { 1955 AANonNullImpl(const IRPosition &IRP) 1956 : AANonNull(IRP), 1957 NullIsDefined(NullPointerIsDefined( 1958 getAnchorScope(), 1959 getAssociatedValue().getType()->getPointerAddressSpace())) {} 1960 1961 /// See AbstractAttribute::initialize(...). 1962 void initialize(Attributor &A) override { 1963 if (!NullIsDefined && 1964 hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) 1965 indicateOptimisticFixpoint(); 1966 else if (isa<ConstantPointerNull>(getAssociatedValue())) 1967 indicatePessimisticFixpoint(); 1968 else 1969 AANonNull::initialize(A); 1970 } 1971 1972 /// See AAFromMustBeExecutedContext 1973 bool followUse(Attributor &A, const Use *U, const Instruction *I) { 1974 bool IsNonNull = false; 1975 bool TrackUse = false; 1976 getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I, 1977 IsNonNull, TrackUse); 1978 setKnown(IsNonNull); 1979 return TrackUse; 1980 } 1981 1982 /// See AbstractAttribute::getAsStr(). 1983 const std::string getAsStr() const override { 1984 return getAssumed() ? "nonnull" : "may-null"; 1985 } 1986 1987 /// Flag to determine if the underlying value can be null and still allow 1988 /// valid accesses. 1989 const bool NullIsDefined; 1990 }; 1991 1992 /// NonNull attribute for a floating value. 1993 struct AANonNullFloating 1994 : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> { 1995 using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>; 1996 AANonNullFloating(const IRPosition &IRP) : Base(IRP) {} 1997 1998 /// See AbstractAttribute::updateImpl(...). 1999 ChangeStatus updateImpl(Attributor &A) override { 2000 ChangeStatus Change = Base::updateImpl(A); 2001 if (isKnownNonNull()) 2002 return Change; 2003 2004 if (!NullIsDefined) { 2005 const auto &DerefAA = 2006 A.getAAFor<AADereferenceable>(*this, getIRPosition()); 2007 if (DerefAA.getAssumedDereferenceableBytes()) 2008 return Change; 2009 } 2010 2011 const DataLayout &DL = A.getDataLayout(); 2012 2013 DominatorTree *DT = nullptr; 2014 InformationCache &InfoCache = A.getInfoCache(); 2015 if (const Function *Fn = getAnchorScope()) 2016 DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn); 2017 2018 auto VisitValueCB = [&](Value &V, AANonNull::StateType &T, 2019 bool Stripped) -> bool { 2020 const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); 2021 if (!Stripped && this == &AA) { 2022 if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, getCtxI(), DT)) 2023 T.indicatePessimisticFixpoint(); 2024 } else { 2025 // Use abstract attribute information. 2026 const AANonNull::StateType &NS = 2027 static_cast<const AANonNull::StateType &>(AA.getState()); 2028 T ^= NS; 2029 } 2030 return T.isValidState(); 2031 }; 2032 2033 StateType T; 2034 if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this, 2035 T, VisitValueCB)) 2036 return indicatePessimisticFixpoint(); 2037 2038 return clampStateAndIndicateChange(getState(), T); 2039 } 2040 2041 /// See AbstractAttribute::trackStatistics() 2042 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } 2043 }; 2044 2045 /// NonNull attribute for function return value. 2046 struct AANonNullReturned final 2047 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> { 2048 AANonNullReturned(const IRPosition &IRP) 2049 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {} 2050 2051 /// See AbstractAttribute::trackStatistics() 2052 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } 2053 }; 2054 2055 /// NonNull attribute for function argument. 2056 struct AANonNullArgument final 2057 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, 2058 AANonNullImpl> { 2059 AANonNullArgument(const IRPosition &IRP) 2060 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, 2061 AANonNullImpl>( 2062 IRP) {} 2063 2064 /// See AbstractAttribute::trackStatistics() 2065 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } 2066 }; 2067 2068 struct AANonNullCallSiteArgument final : AANonNullFloating { 2069 AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {} 2070 2071 /// See AbstractAttribute::trackStatistics() 2072 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } 2073 }; 2074 2075 /// NonNull attribute for a call site return position. 2076 struct AANonNullCallSiteReturned final 2077 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, 2078 AANonNullImpl> { 2079 AANonNullCallSiteReturned(const IRPosition &IRP) 2080 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, 2081 AANonNullImpl>( 2082 IRP) {} 2083 2084 /// See AbstractAttribute::trackStatistics() 2085 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) } 2086 }; 2087 2088 /// ------------------------ No-Recurse Attributes ---------------------------- 2089 2090 struct AANoRecurseImpl : public AANoRecurse { 2091 AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {} 2092 2093 /// See AbstractAttribute::getAsStr() 2094 const std::string getAsStr() const override { 2095 return getAssumed() ? "norecurse" : "may-recurse"; 2096 } 2097 }; 2098 2099 struct AANoRecurseFunction final : AANoRecurseImpl { 2100 AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} 2101 2102 /// See AbstractAttribute::initialize(...). 2103 void initialize(Attributor &A) override { 2104 AANoRecurseImpl::initialize(A); 2105 if (const Function *F = getAnchorScope()) 2106 if (A.getInfoCache().getSccSize(*F) != 1) 2107 indicatePessimisticFixpoint(); 2108 } 2109 2110 /// See AbstractAttribute::updateImpl(...). 2111 ChangeStatus updateImpl(Attributor &A) override { 2112 2113 // If all live call sites are known to be no-recurse, we are as well. 2114 auto CallSitePred = [&](AbstractCallSite ACS) { 2115 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>( 2116 *this, IRPosition::function(*ACS.getInstruction()->getFunction()), 2117 /* TrackDependence */ false, DepClassTy::OPTIONAL); 2118 return NoRecurseAA.isKnownNoRecurse(); 2119 }; 2120 bool AllCallSitesKnown; 2121 if (A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown)) { 2122 // If we know all call sites and all are known no-recurse, we are done. 2123 // If all known call sites, which might not be all that exist, are known 2124 // to be no-recurse, we are not done but we can continue to assume 2125 // no-recurse. If one of the call sites we have not visited will become 2126 // live, another update is triggered. 2127 if (AllCallSitesKnown) 2128 indicateOptimisticFixpoint(); 2129 return ChangeStatus::UNCHANGED; 2130 } 2131 2132 // If the above check does not hold anymore we look at the calls. 2133 auto CheckForNoRecurse = [&](Instruction &I) { 2134 ImmutableCallSite ICS(&I); 2135 if (ICS.hasFnAttr(Attribute::NoRecurse)) 2136 return true; 2137 2138 const auto &NoRecurseAA = 2139 A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS)); 2140 if (!NoRecurseAA.isAssumedNoRecurse()) 2141 return false; 2142 2143 // Recursion to the same function 2144 if (ICS.getCalledFunction() == getAnchorScope()) 2145 return false; 2146 2147 return true; 2148 }; 2149 2150 if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this)) 2151 return indicatePessimisticFixpoint(); 2152 return ChangeStatus::UNCHANGED; 2153 } 2154 2155 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } 2156 }; 2157 2158 /// NoRecurse attribute deduction for a call sites. 2159 struct AANoRecurseCallSite final : AANoRecurseImpl { 2160 AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} 2161 2162 /// See AbstractAttribute::initialize(...). 2163 void initialize(Attributor &A) override { 2164 AANoRecurseImpl::initialize(A); 2165 Function *F = getAssociatedFunction(); 2166 if (!F) 2167 indicatePessimisticFixpoint(); 2168 } 2169 2170 /// See AbstractAttribute::updateImpl(...). 2171 ChangeStatus updateImpl(Attributor &A) override { 2172 // TODO: Once we have call site specific value information we can provide 2173 // call site specific liveness information and then it makes 2174 // sense to specialize attributes for call sites arguments instead of 2175 // redirecting requests to the callee argument. 2176 Function *F = getAssociatedFunction(); 2177 const IRPosition &FnPos = IRPosition::function(*F); 2178 auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos); 2179 return clampStateAndIndicateChange( 2180 getState(), 2181 static_cast<const AANoRecurse::StateType &>(FnAA.getState())); 2182 } 2183 2184 /// See AbstractAttribute::trackStatistics() 2185 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); } 2186 }; 2187 2188 /// -------------------- Undefined-Behavior Attributes ------------------------ 2189 2190 struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior { 2191 AAUndefinedBehaviorImpl(const IRPosition &IRP) : AAUndefinedBehavior(IRP) {} 2192 2193 /// See AbstractAttribute::updateImpl(...). 2194 // through a pointer (i.e. also branches etc.) 2195 ChangeStatus updateImpl(Attributor &A) override { 2196 const size_t UBPrevSize = KnownUBInsts.size(); 2197 const size_t NoUBPrevSize = AssumedNoUBInsts.size(); 2198 2199 auto InspectMemAccessInstForUB = [&](Instruction &I) { 2200 // Skip instructions that are already saved. 2201 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) 2202 return true; 2203 2204 // If we reach here, we know we have an instruction 2205 // that accesses memory through a pointer operand, 2206 // for which getPointerOperand() should give it to us. 2207 const Value *PtrOp = getPointerOperand(&I, /* AllowVolatile */ true); 2208 assert(PtrOp && 2209 "Expected pointer operand of memory accessing instruction"); 2210 2211 // A memory access through a pointer is considered UB 2212 // only if the pointer has constant null value. 2213 // TODO: Expand it to not only check constant values. 2214 if (!isa<ConstantPointerNull>(PtrOp)) { 2215 AssumedNoUBInsts.insert(&I); 2216 return true; 2217 } 2218 const Type *PtrTy = PtrOp->getType(); 2219 2220 // Because we only consider instructions inside functions, 2221 // assume that a parent function exists. 2222 const Function *F = I.getFunction(); 2223 2224 // A memory access using constant null pointer is only considered UB 2225 // if null pointer is _not_ defined for the target platform. 2226 if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace())) 2227 AssumedNoUBInsts.insert(&I); 2228 else 2229 KnownUBInsts.insert(&I); 2230 return true; 2231 }; 2232 2233 auto InspectBrInstForUB = [&](Instruction &I) { 2234 // A conditional branch instruction is considered UB if it has `undef` 2235 // condition. 2236 2237 // Skip instructions that are already saved. 2238 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) 2239 return true; 2240 2241 // We know we have a branch instruction. 2242 auto BrInst = cast<BranchInst>(&I); 2243 2244 // Unconditional branches are never considered UB. 2245 if (BrInst->isUnconditional()) 2246 return true; 2247 2248 // Either we stopped and the appropriate action was taken, 2249 // or we got back a simplified value to continue. 2250 Optional<Value *> SimplifiedCond = 2251 stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst); 2252 if (!SimplifiedCond.hasValue()) 2253 return true; 2254 AssumedNoUBInsts.insert(&I); 2255 return true; 2256 }; 2257 2258 A.checkForAllInstructions(InspectMemAccessInstForUB, *this, 2259 {Instruction::Load, Instruction::Store, 2260 Instruction::AtomicCmpXchg, 2261 Instruction::AtomicRMW}, 2262 /* CheckBBLivenessOnly */ true); 2263 A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br}, 2264 /* CheckBBLivenessOnly */ true); 2265 if (NoUBPrevSize != AssumedNoUBInsts.size() || 2266 UBPrevSize != KnownUBInsts.size()) 2267 return ChangeStatus::CHANGED; 2268 return ChangeStatus::UNCHANGED; 2269 } 2270 2271 bool isKnownToCauseUB(Instruction *I) const override { 2272 return KnownUBInsts.count(I); 2273 } 2274 2275 bool isAssumedToCauseUB(Instruction *I) const override { 2276 // In simple words, if an instruction is not in the assumed to _not_ 2277 // cause UB, then it is assumed UB (that includes those 2278 // in the KnownUBInsts set). The rest is boilerplate 2279 // is to ensure that it is one of the instructions we test 2280 // for UB. 2281 2282 switch (I->getOpcode()) { 2283 case Instruction::Load: 2284 case Instruction::Store: 2285 case Instruction::AtomicCmpXchg: 2286 case Instruction::AtomicRMW: 2287 return !AssumedNoUBInsts.count(I); 2288 case Instruction::Br: { 2289 auto BrInst = cast<BranchInst>(I); 2290 if (BrInst->isUnconditional()) 2291 return false; 2292 return !AssumedNoUBInsts.count(I); 2293 } break; 2294 default: 2295 return false; 2296 } 2297 return false; 2298 } 2299 2300 ChangeStatus manifest(Attributor &A) override { 2301 if (KnownUBInsts.empty()) 2302 return ChangeStatus::UNCHANGED; 2303 for (Instruction *I : KnownUBInsts) 2304 A.changeToUnreachableAfterManifest(I); 2305 return ChangeStatus::CHANGED; 2306 } 2307 2308 /// See AbstractAttribute::getAsStr() 2309 const std::string getAsStr() const override { 2310 return getAssumed() ? "undefined-behavior" : "no-ub"; 2311 } 2312 2313 /// Note: The correctness of this analysis depends on the fact that the 2314 /// following 2 sets will stop changing after some point. 2315 /// "Change" here means that their size changes. 2316 /// The size of each set is monotonically increasing 2317 /// (we only add items to them) and it is upper bounded by the number of 2318 /// instructions in the processed function (we can never save more 2319 /// elements in either set than this number). Hence, at some point, 2320 /// they will stop increasing. 2321 /// Consequently, at some point, both sets will have stopped 2322 /// changing, effectively making the analysis reach a fixpoint. 2323 2324 /// Note: These 2 sets are disjoint and an instruction can be considered 2325 /// one of 3 things: 2326 /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in 2327 /// the KnownUBInsts set. 2328 /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior 2329 /// has a reason to assume it). 2330 /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior 2331 /// could not find a reason to assume or prove that it can cause UB, 2332 /// hence it assumes it doesn't. We have a set for these instructions 2333 /// so that we don't reprocess them in every update. 2334 /// Note however that instructions in this set may cause UB. 2335 2336 protected: 2337 /// A set of all live instructions _known_ to cause UB. 2338 SmallPtrSet<Instruction *, 8> KnownUBInsts; 2339 2340 private: 2341 /// A set of all the (live) instructions that are assumed to _not_ cause UB. 2342 SmallPtrSet<Instruction *, 8> AssumedNoUBInsts; 2343 2344 // Should be called on updates in which if we're processing an instruction 2345 // \p I that depends on a value \p V, one of the following has to happen: 2346 // - If the value is assumed, then stop. 2347 // - If the value is known but undef, then consider it UB. 2348 // - Otherwise, do specific processing with the simplified value. 2349 // We return None in the first 2 cases to signify that an appropriate 2350 // action was taken and the caller should stop. 2351 // Otherwise, we return the simplified value that the caller should 2352 // use for specific processing. 2353 Optional<Value *> stopOnUndefOrAssumed(Attributor &A, const Value *V, 2354 Instruction *I) { 2355 const auto &ValueSimplifyAA = 2356 A.getAAFor<AAValueSimplify>(*this, IRPosition::value(*V)); 2357 Optional<Value *> SimplifiedV = 2358 ValueSimplifyAA.getAssumedSimplifiedValue(A); 2359 if (!ValueSimplifyAA.isKnown()) { 2360 // Don't depend on assumed values. 2361 return llvm::None; 2362 } 2363 if (!SimplifiedV.hasValue()) { 2364 // If it is known (which we tested above) but it doesn't have a value, 2365 // then we can assume `undef` and hence the instruction is UB. 2366 KnownUBInsts.insert(I); 2367 return llvm::None; 2368 } 2369 Value *Val = SimplifiedV.getValue(); 2370 if (isa<UndefValue>(Val)) { 2371 KnownUBInsts.insert(I); 2372 return llvm::None; 2373 } 2374 return Val; 2375 } 2376 }; 2377 2378 struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl { 2379 AAUndefinedBehaviorFunction(const IRPosition &IRP) 2380 : AAUndefinedBehaviorImpl(IRP) {} 2381 2382 /// See AbstractAttribute::trackStatistics() 2383 void trackStatistics() const override { 2384 STATS_DECL(UndefinedBehaviorInstruction, Instruction, 2385 "Number of instructions known to have UB"); 2386 BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) += 2387 KnownUBInsts.size(); 2388 } 2389 }; 2390 2391 /// ------------------------ Will-Return Attributes ---------------------------- 2392 2393 // Helper function that checks whether a function has any cycle. 2394 // TODO: Replace with more efficent code 2395 static bool containsCycle(Function &F) { 2396 SmallPtrSet<BasicBlock *, 32> Visited; 2397 2398 // Traverse BB by dfs and check whether successor is already visited. 2399 for (BasicBlock *BB : depth_first(&F)) { 2400 Visited.insert(BB); 2401 for (auto *SuccBB : successors(BB)) { 2402 if (Visited.count(SuccBB)) 2403 return true; 2404 } 2405 } 2406 return false; 2407 } 2408 2409 // Helper function that checks the function have a loop which might become an 2410 // endless loop 2411 // FIXME: Any cycle is regarded as endless loop for now. 2412 // We have to allow some patterns. 2413 static bool containsPossiblyEndlessLoop(Function *F) { 2414 return !F || !F->hasExactDefinition() || containsCycle(*F); 2415 } 2416 2417 struct AAWillReturnImpl : public AAWillReturn { 2418 AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} 2419 2420 /// See AbstractAttribute::initialize(...). 2421 void initialize(Attributor &A) override { 2422 AAWillReturn::initialize(A); 2423 2424 Function *F = getAssociatedFunction(); 2425 if (containsPossiblyEndlessLoop(F)) 2426 indicatePessimisticFixpoint(); 2427 } 2428 2429 /// See AbstractAttribute::updateImpl(...). 2430 ChangeStatus updateImpl(Attributor &A) override { 2431 auto CheckForWillReturn = [&](Instruction &I) { 2432 IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I)); 2433 const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); 2434 if (WillReturnAA.isKnownWillReturn()) 2435 return true; 2436 if (!WillReturnAA.isAssumedWillReturn()) 2437 return false; 2438 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); 2439 return NoRecurseAA.isAssumedNoRecurse(); 2440 }; 2441 2442 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) 2443 return indicatePessimisticFixpoint(); 2444 2445 return ChangeStatus::UNCHANGED; 2446 } 2447 2448 /// See AbstractAttribute::getAsStr() 2449 const std::string getAsStr() const override { 2450 return getAssumed() ? "willreturn" : "may-noreturn"; 2451 } 2452 }; 2453 2454 struct AAWillReturnFunction final : AAWillReturnImpl { 2455 AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} 2456 2457 /// See AbstractAttribute::trackStatistics() 2458 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) } 2459 }; 2460 2461 /// WillReturn attribute deduction for a call sites. 2462 struct AAWillReturnCallSite final : AAWillReturnImpl { 2463 AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} 2464 2465 /// See AbstractAttribute::initialize(...). 2466 void initialize(Attributor &A) override { 2467 AAWillReturnImpl::initialize(A); 2468 Function *F = getAssociatedFunction(); 2469 if (!F) 2470 indicatePessimisticFixpoint(); 2471 } 2472 2473 /// See AbstractAttribute::updateImpl(...). 2474 ChangeStatus updateImpl(Attributor &A) override { 2475 // TODO: Once we have call site specific value information we can provide 2476 // call site specific liveness information and then it makes 2477 // sense to specialize attributes for call sites arguments instead of 2478 // redirecting requests to the callee argument. 2479 Function *F = getAssociatedFunction(); 2480 const IRPosition &FnPos = IRPosition::function(*F); 2481 auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos); 2482 return clampStateAndIndicateChange( 2483 getState(), 2484 static_cast<const AAWillReturn::StateType &>(FnAA.getState())); 2485 } 2486 2487 /// See AbstractAttribute::trackStatistics() 2488 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); } 2489 }; 2490 2491 /// -------------------AAReachability Attribute-------------------------- 2492 2493 struct AAReachabilityImpl : AAReachability { 2494 AAReachabilityImpl(const IRPosition &IRP) : AAReachability(IRP) {} 2495 2496 const std::string getAsStr() const override { 2497 // TODO: Return the number of reachable queries. 2498 return "reachable"; 2499 } 2500 2501 /// See AbstractAttribute::initialize(...). 2502 void initialize(Attributor &A) override { indicatePessimisticFixpoint(); } 2503 2504 /// See AbstractAttribute::updateImpl(...). 2505 ChangeStatus updateImpl(Attributor &A) override { 2506 return indicatePessimisticFixpoint(); 2507 } 2508 }; 2509 2510 struct AAReachabilityFunction final : public AAReachabilityImpl { 2511 AAReachabilityFunction(const IRPosition &IRP) : AAReachabilityImpl(IRP) {} 2512 2513 /// See AbstractAttribute::trackStatistics() 2514 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); } 2515 }; 2516 2517 /// ------------------------ NoAlias Argument Attribute ------------------------ 2518 2519 struct AANoAliasImpl : AANoAlias { 2520 AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) { 2521 assert(getAssociatedType()->isPointerTy() && 2522 "Noalias is a pointer attribute"); 2523 } 2524 2525 const std::string getAsStr() const override { 2526 return getAssumed() ? "noalias" : "may-alias"; 2527 } 2528 }; 2529 2530 /// NoAlias attribute for a floating value. 2531 struct AANoAliasFloating final : AANoAliasImpl { 2532 AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {} 2533 2534 /// See AbstractAttribute::initialize(...). 2535 void initialize(Attributor &A) override { 2536 AANoAliasImpl::initialize(A); 2537 Value *Val = &getAssociatedValue(); 2538 do { 2539 CastInst *CI = dyn_cast<CastInst>(Val); 2540 if (!CI) 2541 break; 2542 Value *Base = CI->getOperand(0); 2543 if (Base->getNumUses() != 1) 2544 break; 2545 Val = Base; 2546 } while (true); 2547 2548 if (!Val->getType()->isPointerTy()) { 2549 indicatePessimisticFixpoint(); 2550 return; 2551 } 2552 2553 if (isa<AllocaInst>(Val)) 2554 indicateOptimisticFixpoint(); 2555 else if (isa<ConstantPointerNull>(Val) && 2556 !NullPointerIsDefined(getAnchorScope(), 2557 Val->getType()->getPointerAddressSpace())) 2558 indicateOptimisticFixpoint(); 2559 else if (Val != &getAssociatedValue()) { 2560 const auto &ValNoAliasAA = 2561 A.getAAFor<AANoAlias>(*this, IRPosition::value(*Val)); 2562 if (ValNoAliasAA.isKnownNoAlias()) 2563 indicateOptimisticFixpoint(); 2564 } 2565 } 2566 2567 /// See AbstractAttribute::updateImpl(...). 2568 ChangeStatus updateImpl(Attributor &A) override { 2569 // TODO: Implement this. 2570 return indicatePessimisticFixpoint(); 2571 } 2572 2573 /// See AbstractAttribute::trackStatistics() 2574 void trackStatistics() const override { 2575 STATS_DECLTRACK_FLOATING_ATTR(noalias) 2576 } 2577 }; 2578 2579 /// NoAlias attribute for an argument. 2580 struct AANoAliasArgument final 2581 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> { 2582 using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>; 2583 AANoAliasArgument(const IRPosition &IRP) : Base(IRP) {} 2584 2585 /// See AbstractAttribute::initialize(...). 2586 void initialize(Attributor &A) override { 2587 Base::initialize(A); 2588 // See callsite argument attribute and callee argument attribute. 2589 if (hasAttr({Attribute::ByVal})) 2590 indicateOptimisticFixpoint(); 2591 } 2592 2593 /// See AbstractAttribute::update(...). 2594 ChangeStatus updateImpl(Attributor &A) override { 2595 // We have to make sure no-alias on the argument does not break 2596 // synchronization when this is a callback argument, see also [1] below. 2597 // If synchronization cannot be affected, we delegate to the base updateImpl 2598 // function, otherwise we give up for now. 2599 2600 // If the function is no-sync, no-alias cannot break synchronization. 2601 const auto &NoSyncAA = A.getAAFor<AANoSync>( 2602 *this, IRPosition::function_scope(getIRPosition())); 2603 if (NoSyncAA.isAssumedNoSync()) 2604 return Base::updateImpl(A); 2605 2606 // If the argument is read-only, no-alias cannot break synchronization. 2607 const auto &MemBehaviorAA = 2608 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); 2609 if (MemBehaviorAA.isAssumedReadOnly()) 2610 return Base::updateImpl(A); 2611 2612 // If the argument is never passed through callbacks, no-alias cannot break 2613 // synchronization. 2614 bool AllCallSitesKnown; 2615 if (A.checkForAllCallSites( 2616 [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this, 2617 true, AllCallSitesKnown)) 2618 return Base::updateImpl(A); 2619 2620 // TODO: add no-alias but make sure it doesn't break synchronization by 2621 // introducing fake uses. See: 2622 // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel, 2623 // International Workshop on OpenMP 2018, 2624 // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf 2625 2626 return indicatePessimisticFixpoint(); 2627 } 2628 2629 /// See AbstractAttribute::trackStatistics() 2630 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) } 2631 }; 2632 2633 struct AANoAliasCallSiteArgument final : AANoAliasImpl { 2634 AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {} 2635 2636 /// See AbstractAttribute::initialize(...). 2637 void initialize(Attributor &A) override { 2638 // See callsite argument attribute and callee argument attribute. 2639 ImmutableCallSite ICS(&getAnchorValue()); 2640 if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias)) 2641 indicateOptimisticFixpoint(); 2642 Value &Val = getAssociatedValue(); 2643 if (isa<ConstantPointerNull>(Val) && 2644 !NullPointerIsDefined(getAnchorScope(), 2645 Val.getType()->getPointerAddressSpace())) 2646 indicateOptimisticFixpoint(); 2647 } 2648 2649 /// Determine if the underlying value may alias with the call site argument 2650 /// \p OtherArgNo of \p ICS (= the underlying call site). 2651 bool mayAliasWithArgument(Attributor &A, AAResults *&AAR, 2652 const AAMemoryBehavior &MemBehaviorAA, 2653 ImmutableCallSite ICS, unsigned OtherArgNo) { 2654 // We do not need to worry about aliasing with the underlying IRP. 2655 if (this->getArgNo() == (int)OtherArgNo) 2656 return false; 2657 2658 // If it is not a pointer or pointer vector we do not alias. 2659 const Value *ArgOp = ICS.getArgOperand(OtherArgNo); 2660 if (!ArgOp->getType()->isPtrOrPtrVectorTy()) 2661 return false; 2662 2663 auto &ICSArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>( 2664 *this, IRPosition::callsite_argument(ICS, OtherArgNo), 2665 /* TrackDependence */ false); 2666 2667 // If the argument is readnone, there is no read-write aliasing. 2668 if (ICSArgMemBehaviorAA.isAssumedReadNone()) { 2669 A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL); 2670 return false; 2671 } 2672 2673 // If the argument is readonly and the underlying value is readonly, there 2674 // is no read-write aliasing. 2675 bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly(); 2676 if (ICSArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) { 2677 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); 2678 A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL); 2679 return false; 2680 } 2681 2682 // We have to utilize actual alias analysis queries so we need the object. 2683 if (!AAR) 2684 AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope()); 2685 2686 // Try to rule it out at the call site. 2687 bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp); 2688 LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between " 2689 "callsite arguments: " 2690 << getAssociatedValue() << " " << *ArgOp << " => " 2691 << (IsAliasing ? "" : "no-") << "alias \n"); 2692 2693 return IsAliasing; 2694 } 2695 2696 bool 2697 isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR, 2698 const AAMemoryBehavior &MemBehaviorAA, 2699 const AANoAlias &NoAliasAA) { 2700 // We can deduce "noalias" if the following conditions hold. 2701 // (i) Associated value is assumed to be noalias in the definition. 2702 // (ii) Associated value is assumed to be no-capture in all the uses 2703 // possibly executed before this callsite. 2704 // (iii) There is no other pointer argument which could alias with the 2705 // value. 2706 2707 bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias(); 2708 if (!AssociatedValueIsNoAliasAtDef) { 2709 LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue() 2710 << " is not no-alias at the definition\n"); 2711 return false; 2712 } 2713 2714 const IRPosition &VIRP = IRPosition::value(getAssociatedValue()); 2715 auto &NoCaptureAA = 2716 A.getAAFor<AANoCapture>(*this, VIRP, /* TrackDependence */ false); 2717 // Check whether the value is captured in the scope using AANoCapture. 2718 // FIXME: This is conservative though, it is better to look at CFG and 2719 // check only uses possibly executed before this callsite. 2720 if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { 2721 LLVM_DEBUG( 2722 dbgs() << "[AANoAliasCSArg] " << getAssociatedValue() 2723 << " cannot be noalias as it is potentially captured\n"); 2724 return false; 2725 } 2726 A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL); 2727 2728 // Check there is no other pointer argument which could alias with the 2729 // value passed at this call site. 2730 // TODO: AbstractCallSite 2731 ImmutableCallSite ICS(&getAnchorValue()); 2732 for (unsigned OtherArgNo = 0; OtherArgNo < ICS.getNumArgOperands(); 2733 OtherArgNo++) 2734 if (mayAliasWithArgument(A, AAR, MemBehaviorAA, ICS, OtherArgNo)) 2735 return false; 2736 2737 return true; 2738 } 2739 2740 /// See AbstractAttribute::updateImpl(...). 2741 ChangeStatus updateImpl(Attributor &A) override { 2742 // If the argument is readnone we are done as there are no accesses via the 2743 // argument. 2744 auto &MemBehaviorAA = 2745 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), 2746 /* TrackDependence */ false); 2747 if (MemBehaviorAA.isAssumedReadNone()) { 2748 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); 2749 return ChangeStatus::UNCHANGED; 2750 } 2751 2752 const IRPosition &VIRP = IRPosition::value(getAssociatedValue()); 2753 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, VIRP, 2754 /* TrackDependence */ false); 2755 2756 AAResults *AAR = nullptr; 2757 if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA, 2758 NoAliasAA)) { 2759 LLVM_DEBUG( 2760 dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n"); 2761 return ChangeStatus::UNCHANGED; 2762 } 2763 2764 return indicatePessimisticFixpoint(); 2765 } 2766 2767 /// See AbstractAttribute::trackStatistics() 2768 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) } 2769 }; 2770 2771 /// NoAlias attribute for function return value. 2772 struct AANoAliasReturned final : AANoAliasImpl { 2773 AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} 2774 2775 /// See AbstractAttribute::updateImpl(...). 2776 virtual ChangeStatus updateImpl(Attributor &A) override { 2777 2778 auto CheckReturnValue = [&](Value &RV) -> bool { 2779 if (Constant *C = dyn_cast<Constant>(&RV)) 2780 if (C->isNullValue() || isa<UndefValue>(C)) 2781 return true; 2782 2783 /// For now, we can only deduce noalias if we have call sites. 2784 /// FIXME: add more support. 2785 ImmutableCallSite ICS(&RV); 2786 if (!ICS) 2787 return false; 2788 2789 const IRPosition &RVPos = IRPosition::value(RV); 2790 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos); 2791 if (!NoAliasAA.isAssumedNoAlias()) 2792 return false; 2793 2794 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos); 2795 return NoCaptureAA.isAssumedNoCaptureMaybeReturned(); 2796 }; 2797 2798 if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) 2799 return indicatePessimisticFixpoint(); 2800 2801 return ChangeStatus::UNCHANGED; 2802 } 2803 2804 /// See AbstractAttribute::trackStatistics() 2805 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } 2806 }; 2807 2808 /// NoAlias attribute deduction for a call site return value. 2809 struct AANoAliasCallSiteReturned final : AANoAliasImpl { 2810 AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} 2811 2812 /// See AbstractAttribute::initialize(...). 2813 void initialize(Attributor &A) override { 2814 AANoAliasImpl::initialize(A); 2815 Function *F = getAssociatedFunction(); 2816 if (!F) 2817 indicatePessimisticFixpoint(); 2818 } 2819 2820 /// See AbstractAttribute::updateImpl(...). 2821 ChangeStatus updateImpl(Attributor &A) override { 2822 // TODO: Once we have call site specific value information we can provide 2823 // call site specific liveness information and then it makes 2824 // sense to specialize attributes for call sites arguments instead of 2825 // redirecting requests to the callee argument. 2826 Function *F = getAssociatedFunction(); 2827 const IRPosition &FnPos = IRPosition::returned(*F); 2828 auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos); 2829 return clampStateAndIndicateChange( 2830 getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState())); 2831 } 2832 2833 /// See AbstractAttribute::trackStatistics() 2834 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); } 2835 }; 2836 2837 /// -------------------AAIsDead Function Attribute----------------------- 2838 2839 struct AAIsDeadValueImpl : public AAIsDead { 2840 AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {} 2841 2842 /// See AAIsDead::isAssumedDead(). 2843 bool isAssumedDead() const override { return getAssumed(); } 2844 2845 /// See AAIsDead::isKnownDead(). 2846 bool isKnownDead() const override { return getKnown(); } 2847 2848 /// See AAIsDead::isAssumedDead(BasicBlock *). 2849 bool isAssumedDead(const BasicBlock *BB) const override { return false; } 2850 2851 /// See AAIsDead::isKnownDead(BasicBlock *). 2852 bool isKnownDead(const BasicBlock *BB) const override { return false; } 2853 2854 /// See AAIsDead::isAssumedDead(Instruction *I). 2855 bool isAssumedDead(const Instruction *I) const override { 2856 return I == getCtxI() && isAssumedDead(); 2857 } 2858 2859 /// See AAIsDead::isKnownDead(Instruction *I). 2860 bool isKnownDead(const Instruction *I) const override { 2861 return isAssumedDead(I) && getKnown(); 2862 } 2863 2864 /// See AbstractAttribute::getAsStr(). 2865 const std::string getAsStr() const override { 2866 return isAssumedDead() ? "assumed-dead" : "assumed-live"; 2867 } 2868 2869 /// Check if all uses are assumed dead. 2870 bool areAllUsesAssumedDead(Attributor &A, Value &V) { 2871 auto UsePred = [&](const Use &U, bool &Follow) { return false; }; 2872 // Explicitly set the dependence class to required because we want a long 2873 // chain of N dependent instructions to be considered live as soon as one is 2874 // without going through N update cycles. This is not required for 2875 // correctness. 2876 return A.checkForAllUses(UsePred, *this, V, DepClassTy::REQUIRED); 2877 } 2878 2879 /// Determine if \p I is assumed to be side-effect free. 2880 bool isAssumedSideEffectFree(Attributor &A, Instruction *I) { 2881 if (!I || wouldInstructionBeTriviallyDead(I)) 2882 return true; 2883 2884 auto *CB = dyn_cast<CallBase>(I); 2885 if (!CB || isa<IntrinsicInst>(CB)) 2886 return false; 2887 2888 const IRPosition &CallIRP = IRPosition::callsite_function(*CB); 2889 const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(*this, CallIRP); 2890 if (!NoUnwindAA.isAssumedNoUnwind()) 2891 return false; 2892 2893 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, CallIRP); 2894 if (!MemBehaviorAA.isAssumedReadOnly()) 2895 return false; 2896 2897 return true; 2898 } 2899 }; 2900 2901 struct AAIsDeadFloating : public AAIsDeadValueImpl { 2902 AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} 2903 2904 /// See AbstractAttribute::initialize(...). 2905 void initialize(Attributor &A) override { 2906 if (isa<UndefValue>(getAssociatedValue())) { 2907 indicatePessimisticFixpoint(); 2908 return; 2909 } 2910 2911 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()); 2912 if (!isAssumedSideEffectFree(A, I)) 2913 indicatePessimisticFixpoint(); 2914 } 2915 2916 /// See AbstractAttribute::updateImpl(...). 2917 ChangeStatus updateImpl(Attributor &A) override { 2918 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()); 2919 if (!isAssumedSideEffectFree(A, I)) 2920 return indicatePessimisticFixpoint(); 2921 2922 if (!areAllUsesAssumedDead(A, getAssociatedValue())) 2923 return indicatePessimisticFixpoint(); 2924 return ChangeStatus::UNCHANGED; 2925 } 2926 2927 /// See AbstractAttribute::manifest(...). 2928 ChangeStatus manifest(Attributor &A) override { 2929 Value &V = getAssociatedValue(); 2930 if (auto *I = dyn_cast<Instruction>(&V)) { 2931 // If we get here we basically know the users are all dead. We check if 2932 // isAssumedSideEffectFree returns true here again because it might not be 2933 // the case and only the users are dead but the instruction (=call) is 2934 // still needed. 2935 if (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I)) { 2936 A.deleteAfterManifest(*I); 2937 return ChangeStatus::CHANGED; 2938 } 2939 } 2940 if (V.use_empty()) 2941 return ChangeStatus::UNCHANGED; 2942 2943 bool UsedAssumedInformation = false; 2944 Optional<Constant *> C = 2945 getAssumedConstant(A, V, *this, UsedAssumedInformation); 2946 if (C.hasValue() && C.getValue()) 2947 return ChangeStatus::UNCHANGED; 2948 2949 UndefValue &UV = *UndefValue::get(V.getType()); 2950 bool AnyChange = A.changeValueAfterManifest(V, UV); 2951 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 2952 } 2953 2954 /// See AbstractAttribute::trackStatistics() 2955 void trackStatistics() const override { 2956 STATS_DECLTRACK_FLOATING_ATTR(IsDead) 2957 } 2958 }; 2959 2960 struct AAIsDeadArgument : public AAIsDeadFloating { 2961 AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} 2962 2963 /// See AbstractAttribute::initialize(...). 2964 void initialize(Attributor &A) override { 2965 if (!getAssociatedFunction()->hasExactDefinition()) 2966 indicatePessimisticFixpoint(); 2967 } 2968 2969 /// See AbstractAttribute::manifest(...). 2970 ChangeStatus manifest(Attributor &A) override { 2971 ChangeStatus Changed = AAIsDeadFloating::manifest(A); 2972 Argument &Arg = *getAssociatedArgument(); 2973 if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {})) 2974 if (A.registerFunctionSignatureRewrite( 2975 Arg, /* ReplacementTypes */ {}, 2976 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{}, 2977 Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) 2978 return ChangeStatus::CHANGED; 2979 return Changed; 2980 } 2981 2982 /// See AbstractAttribute::trackStatistics() 2983 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) } 2984 }; 2985 2986 struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl { 2987 AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} 2988 2989 /// See AbstractAttribute::initialize(...). 2990 void initialize(Attributor &A) override { 2991 if (isa<UndefValue>(getAssociatedValue())) 2992 indicatePessimisticFixpoint(); 2993 } 2994 2995 /// See AbstractAttribute::updateImpl(...). 2996 ChangeStatus updateImpl(Attributor &A) override { 2997 // TODO: Once we have call site specific value information we can provide 2998 // call site specific liveness information and then it makes 2999 // sense to specialize attributes for call sites arguments instead of 3000 // redirecting requests to the callee argument. 3001 Argument *Arg = getAssociatedArgument(); 3002 if (!Arg) 3003 return indicatePessimisticFixpoint(); 3004 const IRPosition &ArgPos = IRPosition::argument(*Arg); 3005 auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos); 3006 return clampStateAndIndicateChange( 3007 getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState())); 3008 } 3009 3010 /// See AbstractAttribute::manifest(...). 3011 ChangeStatus manifest(Attributor &A) override { 3012 CallBase &CB = cast<CallBase>(getAnchorValue()); 3013 Use &U = CB.getArgOperandUse(getArgNo()); 3014 assert(!isa<UndefValue>(U.get()) && 3015 "Expected undef values to be filtered out!"); 3016 UndefValue &UV = *UndefValue::get(U->getType()); 3017 if (A.changeUseAfterManifest(U, UV)) 3018 return ChangeStatus::CHANGED; 3019 return ChangeStatus::UNCHANGED; 3020 } 3021 3022 /// See AbstractAttribute::trackStatistics() 3023 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) } 3024 }; 3025 3026 struct AAIsDeadCallSiteReturned : public AAIsDeadFloating { 3027 AAIsDeadCallSiteReturned(const IRPosition &IRP) 3028 : AAIsDeadFloating(IRP), IsAssumedSideEffectFree(true) {} 3029 3030 /// See AAIsDead::isAssumedDead(). 3031 bool isAssumedDead() const override { 3032 return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree; 3033 } 3034 3035 /// See AbstractAttribute::initialize(...). 3036 void initialize(Attributor &A) override { 3037 if (isa<UndefValue>(getAssociatedValue())) { 3038 indicatePessimisticFixpoint(); 3039 return; 3040 } 3041 3042 // We track this separately as a secondary state. 3043 IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI()); 3044 } 3045 3046 /// See AbstractAttribute::updateImpl(...). 3047 ChangeStatus updateImpl(Attributor &A) override { 3048 ChangeStatus Changed = ChangeStatus::UNCHANGED; 3049 if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) { 3050 IsAssumedSideEffectFree = false; 3051 Changed = ChangeStatus::CHANGED; 3052 } 3053 3054 if (!areAllUsesAssumedDead(A, getAssociatedValue())) 3055 return indicatePessimisticFixpoint(); 3056 return Changed; 3057 } 3058 3059 /// See AbstractAttribute::manifest(...). 3060 ChangeStatus manifest(Attributor &A) override { 3061 if (auto *CI = dyn_cast<CallInst>(&getAssociatedValue())) 3062 if (CI->isMustTailCall()) 3063 return ChangeStatus::UNCHANGED; 3064 return AAIsDeadFloating::manifest(A); 3065 } 3066 3067 /// See AbstractAttribute::trackStatistics() 3068 void trackStatistics() const override { 3069 if (IsAssumedSideEffectFree) 3070 STATS_DECLTRACK_CSRET_ATTR(IsDead) 3071 else 3072 STATS_DECLTRACK_CSRET_ATTR(UnusedResult) 3073 } 3074 3075 /// See AbstractAttribute::getAsStr(). 3076 const std::string getAsStr() const override { 3077 return isAssumedDead() 3078 ? "assumed-dead" 3079 : (getAssumed() ? "assumed-dead-users" : "assumed-live"); 3080 } 3081 3082 private: 3083 bool IsAssumedSideEffectFree; 3084 }; 3085 3086 struct AAIsDeadReturned : public AAIsDeadValueImpl { 3087 AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} 3088 3089 /// See AbstractAttribute::updateImpl(...). 3090 ChangeStatus updateImpl(Attributor &A) override { 3091 3092 A.checkForAllInstructions([](Instruction &) { return true; }, *this, 3093 {Instruction::Ret}); 3094 3095 auto PredForCallSite = [&](AbstractCallSite ACS) { 3096 if (ACS.isCallbackCall() || !ACS.getInstruction()) 3097 return false; 3098 return areAllUsesAssumedDead(A, *ACS.getInstruction()); 3099 }; 3100 3101 bool AllCallSitesKnown; 3102 if (!A.checkForAllCallSites(PredForCallSite, *this, true, 3103 AllCallSitesKnown)) 3104 return indicatePessimisticFixpoint(); 3105 3106 return ChangeStatus::UNCHANGED; 3107 } 3108 3109 /// See AbstractAttribute::manifest(...). 3110 ChangeStatus manifest(Attributor &A) override { 3111 // TODO: Rewrite the signature to return void? 3112 bool AnyChange = false; 3113 UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType()); 3114 auto RetInstPred = [&](Instruction &I) { 3115 ReturnInst &RI = cast<ReturnInst>(I); 3116 if (auto *CI = dyn_cast<CallInst>(RI.getReturnValue())) 3117 if (CI->isMustTailCall()) 3118 return true; 3119 if (!isa<UndefValue>(RI.getReturnValue())) 3120 AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV); 3121 return true; 3122 }; 3123 A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret}); 3124 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 3125 } 3126 3127 /// See AbstractAttribute::trackStatistics() 3128 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) } 3129 }; 3130 3131 struct AAIsDeadFunction : public AAIsDead { 3132 AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {} 3133 3134 /// See AbstractAttribute::initialize(...). 3135 void initialize(Attributor &A) override { 3136 const Function *F = getAssociatedFunction(); 3137 if (F && !F->isDeclaration()) { 3138 ToBeExploredFrom.insert(&F->getEntryBlock().front()); 3139 assumeLive(A, F->getEntryBlock()); 3140 } 3141 } 3142 3143 /// See AbstractAttribute::getAsStr(). 3144 const std::string getAsStr() const override { 3145 return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + 3146 std::to_string(getAssociatedFunction()->size()) + "][#TBEP " + 3147 std::to_string(ToBeExploredFrom.size()) + "][#KDE " + 3148 std::to_string(KnownDeadEnds.size()) + "]"; 3149 } 3150 3151 /// See AbstractAttribute::manifest(...). 3152 ChangeStatus manifest(Attributor &A) override { 3153 assert(getState().isValidState() && 3154 "Attempted to manifest an invalid state!"); 3155 3156 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 3157 Function &F = *getAssociatedFunction(); 3158 3159 if (AssumedLiveBlocks.empty()) { 3160 A.deleteAfterManifest(F); 3161 return ChangeStatus::CHANGED; 3162 } 3163 3164 // Flag to determine if we can change an invoke to a call assuming the 3165 // callee is nounwind. This is not possible if the personality of the 3166 // function allows to catch asynchronous exceptions. 3167 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); 3168 3169 KnownDeadEnds.set_union(ToBeExploredFrom); 3170 for (const Instruction *DeadEndI : KnownDeadEnds) { 3171 auto *CB = dyn_cast<CallBase>(DeadEndI); 3172 if (!CB) 3173 continue; 3174 const auto &NoReturnAA = 3175 A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB)); 3176 bool MayReturn = !NoReturnAA.isAssumedNoReturn(); 3177 if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB))) 3178 continue; 3179 3180 if (auto *II = dyn_cast<InvokeInst>(DeadEndI)) 3181 A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II)); 3182 else 3183 A.changeToUnreachableAfterManifest( 3184 const_cast<Instruction *>(DeadEndI->getNextNode())); 3185 HasChanged = ChangeStatus::CHANGED; 3186 } 3187 3188 for (BasicBlock &BB : F) 3189 if (!AssumedLiveBlocks.count(&BB)) 3190 A.deleteAfterManifest(BB); 3191 3192 return HasChanged; 3193 } 3194 3195 /// See AbstractAttribute::updateImpl(...). 3196 ChangeStatus updateImpl(Attributor &A) override; 3197 3198 /// See AbstractAttribute::trackStatistics() 3199 void trackStatistics() const override {} 3200 3201 /// Returns true if the function is assumed dead. 3202 bool isAssumedDead() const override { return false; } 3203 3204 /// See AAIsDead::isKnownDead(). 3205 bool isKnownDead() const override { return false; } 3206 3207 /// See AAIsDead::isAssumedDead(BasicBlock *). 3208 bool isAssumedDead(const BasicBlock *BB) const override { 3209 assert(BB->getParent() == getAssociatedFunction() && 3210 "BB must be in the same anchor scope function."); 3211 3212 if (!getAssumed()) 3213 return false; 3214 return !AssumedLiveBlocks.count(BB); 3215 } 3216 3217 /// See AAIsDead::isKnownDead(BasicBlock *). 3218 bool isKnownDead(const BasicBlock *BB) const override { 3219 return getKnown() && isAssumedDead(BB); 3220 } 3221 3222 /// See AAIsDead::isAssumed(Instruction *I). 3223 bool isAssumedDead(const Instruction *I) const override { 3224 assert(I->getParent()->getParent() == getAssociatedFunction() && 3225 "Instruction must be in the same anchor scope function."); 3226 3227 if (!getAssumed()) 3228 return false; 3229 3230 // If it is not in AssumedLiveBlocks then it for sure dead. 3231 // Otherwise, it can still be after noreturn call in a live block. 3232 if (!AssumedLiveBlocks.count(I->getParent())) 3233 return true; 3234 3235 // If it is not after a liveness barrier it is live. 3236 const Instruction *PrevI = I->getPrevNode(); 3237 while (PrevI) { 3238 if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI)) 3239 return true; 3240 PrevI = PrevI->getPrevNode(); 3241 } 3242 return false; 3243 } 3244 3245 /// See AAIsDead::isKnownDead(Instruction *I). 3246 bool isKnownDead(const Instruction *I) const override { 3247 return getKnown() && isAssumedDead(I); 3248 } 3249 3250 /// Determine if \p F might catch asynchronous exceptions. 3251 static bool mayCatchAsynchronousExceptions(const Function &F) { 3252 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); 3253 } 3254 3255 /// Assume \p BB is (partially) live now and indicate to the Attributor \p A 3256 /// that internal function called from \p BB should now be looked at. 3257 bool assumeLive(Attributor &A, const BasicBlock &BB) { 3258 if (!AssumedLiveBlocks.insert(&BB).second) 3259 return false; 3260 3261 // We assume that all of BB is (probably) live now and if there are calls to 3262 // internal functions we will assume that those are now live as well. This 3263 // is a performance optimization for blocks with calls to a lot of internal 3264 // functions. It can however cause dead functions to be treated as live. 3265 for (const Instruction &I : BB) 3266 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) 3267 if (const Function *F = ICS.getCalledFunction()) 3268 if (F->hasLocalLinkage()) 3269 A.markLiveInternalFunction(*F); 3270 return true; 3271 } 3272 3273 /// Collection of instructions that need to be explored again, e.g., we 3274 /// did assume they do not transfer control to (one of their) successors. 3275 SmallSetVector<const Instruction *, 8> ToBeExploredFrom; 3276 3277 /// Collection of instructions that are known to not transfer control. 3278 SmallSetVector<const Instruction *, 8> KnownDeadEnds; 3279 3280 /// Collection of all assumed live BasicBlocks. 3281 DenseSet<const BasicBlock *> AssumedLiveBlocks; 3282 }; 3283 3284 static bool 3285 identifyAliveSuccessors(Attributor &A, const CallBase &CB, 3286 AbstractAttribute &AA, 3287 SmallVectorImpl<const Instruction *> &AliveSuccessors) { 3288 const IRPosition &IPos = IRPosition::callsite_function(CB); 3289 3290 const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos); 3291 if (NoReturnAA.isAssumedNoReturn()) 3292 return !NoReturnAA.isKnownNoReturn(); 3293 if (CB.isTerminator()) 3294 AliveSuccessors.push_back(&CB.getSuccessor(0)->front()); 3295 else 3296 AliveSuccessors.push_back(CB.getNextNode()); 3297 return false; 3298 } 3299 3300 static bool 3301 identifyAliveSuccessors(Attributor &A, const InvokeInst &II, 3302 AbstractAttribute &AA, 3303 SmallVectorImpl<const Instruction *> &AliveSuccessors) { 3304 bool UsedAssumedInformation = 3305 identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors); 3306 3307 // First, determine if we can change an invoke to a call assuming the 3308 // callee is nounwind. This is not possible if the personality of the 3309 // function allows to catch asynchronous exceptions. 3310 if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) { 3311 AliveSuccessors.push_back(&II.getUnwindDest()->front()); 3312 } else { 3313 const IRPosition &IPos = IRPosition::callsite_function(II); 3314 const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos); 3315 if (AANoUnw.isAssumedNoUnwind()) { 3316 UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind(); 3317 } else { 3318 AliveSuccessors.push_back(&II.getUnwindDest()->front()); 3319 } 3320 } 3321 return UsedAssumedInformation; 3322 } 3323 3324 static bool 3325 identifyAliveSuccessors(Attributor &A, const BranchInst &BI, 3326 AbstractAttribute &AA, 3327 SmallVectorImpl<const Instruction *> &AliveSuccessors) { 3328 bool UsedAssumedInformation = false; 3329 if (BI.getNumSuccessors() == 1) { 3330 AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); 3331 } else { 3332 Optional<ConstantInt *> CI = getAssumedConstantInt( 3333 A, *BI.getCondition(), AA, UsedAssumedInformation); 3334 if (!CI.hasValue()) { 3335 // No value yet, assume both edges are dead. 3336 } else if (CI.getValue()) { 3337 const BasicBlock *SuccBB = 3338 BI.getSuccessor(1 - CI.getValue()->getZExtValue()); 3339 AliveSuccessors.push_back(&SuccBB->front()); 3340 } else { 3341 AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); 3342 AliveSuccessors.push_back(&BI.getSuccessor(1)->front()); 3343 UsedAssumedInformation = false; 3344 } 3345 } 3346 return UsedAssumedInformation; 3347 } 3348 3349 static bool 3350 identifyAliveSuccessors(Attributor &A, const SwitchInst &SI, 3351 AbstractAttribute &AA, 3352 SmallVectorImpl<const Instruction *> &AliveSuccessors) { 3353 bool UsedAssumedInformation = false; 3354 Optional<ConstantInt *> CI = 3355 getAssumedConstantInt(A, *SI.getCondition(), AA, UsedAssumedInformation); 3356 if (!CI.hasValue()) { 3357 // No value yet, assume all edges are dead. 3358 } else if (CI.getValue()) { 3359 for (auto &CaseIt : SI.cases()) { 3360 if (CaseIt.getCaseValue() == CI.getValue()) { 3361 AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front()); 3362 return UsedAssumedInformation; 3363 } 3364 } 3365 AliveSuccessors.push_back(&SI.getDefaultDest()->front()); 3366 return UsedAssumedInformation; 3367 } else { 3368 for (const BasicBlock *SuccBB : successors(SI.getParent())) 3369 AliveSuccessors.push_back(&SuccBB->front()); 3370 } 3371 return UsedAssumedInformation; 3372 } 3373 3374 ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) { 3375 ChangeStatus Change = ChangeStatus::UNCHANGED; 3376 3377 LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" 3378 << getAssociatedFunction()->size() << "] BBs and " 3379 << ToBeExploredFrom.size() << " exploration points and " 3380 << KnownDeadEnds.size() << " known dead ends\n"); 3381 3382 // Copy and clear the list of instructions we need to explore from. It is 3383 // refilled with instructions the next update has to look at. 3384 SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(), 3385 ToBeExploredFrom.end()); 3386 decltype(ToBeExploredFrom) NewToBeExploredFrom; 3387 3388 SmallVector<const Instruction *, 8> AliveSuccessors; 3389 while (!Worklist.empty()) { 3390 const Instruction *I = Worklist.pop_back_val(); 3391 LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n"); 3392 3393 AliveSuccessors.clear(); 3394 3395 bool UsedAssumedInformation = false; 3396 switch (I->getOpcode()) { 3397 // TODO: look for (assumed) UB to backwards propagate "deadness". 3398 default: 3399 if (I->isTerminator()) { 3400 for (const BasicBlock *SuccBB : successors(I->getParent())) 3401 AliveSuccessors.push_back(&SuccBB->front()); 3402 } else { 3403 AliveSuccessors.push_back(I->getNextNode()); 3404 } 3405 break; 3406 case Instruction::Call: 3407 UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I), 3408 *this, AliveSuccessors); 3409 break; 3410 case Instruction::Invoke: 3411 UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I), 3412 *this, AliveSuccessors); 3413 break; 3414 case Instruction::Br: 3415 UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I), 3416 *this, AliveSuccessors); 3417 break; 3418 case Instruction::Switch: 3419 UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I), 3420 *this, AliveSuccessors); 3421 break; 3422 } 3423 3424 if (UsedAssumedInformation) { 3425 NewToBeExploredFrom.insert(I); 3426 } else { 3427 Change = ChangeStatus::CHANGED; 3428 if (AliveSuccessors.empty() || 3429 (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors())) 3430 KnownDeadEnds.insert(I); 3431 } 3432 3433 LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: " 3434 << AliveSuccessors.size() << " UsedAssumedInformation: " 3435 << UsedAssumedInformation << "\n"); 3436 3437 for (const Instruction *AliveSuccessor : AliveSuccessors) { 3438 if (!I->isTerminator()) { 3439 assert(AliveSuccessors.size() == 1 && 3440 "Non-terminator expected to have a single successor!"); 3441 Worklist.push_back(AliveSuccessor); 3442 } else { 3443 if (assumeLive(A, *AliveSuccessor->getParent())) 3444 Worklist.push_back(AliveSuccessor); 3445 } 3446 } 3447 } 3448 3449 ToBeExploredFrom = std::move(NewToBeExploredFrom); 3450 3451 // If we know everything is live there is no need to query for liveness. 3452 // Instead, indicating a pessimistic fixpoint will cause the state to be 3453 // "invalid" and all queries to be answered conservatively without lookups. 3454 // To be in this state we have to (1) finished the exploration and (3) not 3455 // discovered any non-trivial dead end and (2) not ruled unreachable code 3456 // dead. 3457 if (ToBeExploredFrom.empty() && 3458 getAssociatedFunction()->size() == AssumedLiveBlocks.size() && 3459 llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) { 3460 return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0; 3461 })) 3462 return indicatePessimisticFixpoint(); 3463 return Change; 3464 } 3465 3466 /// Liveness information for a call sites. 3467 struct AAIsDeadCallSite final : AAIsDeadFunction { 3468 AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {} 3469 3470 /// See AbstractAttribute::initialize(...). 3471 void initialize(Attributor &A) override { 3472 // TODO: Once we have call site specific value information we can provide 3473 // call site specific liveness information and then it makes 3474 // sense to specialize attributes for call sites instead of 3475 // redirecting requests to the callee. 3476 llvm_unreachable("Abstract attributes for liveness are not " 3477 "supported for call sites yet!"); 3478 } 3479 3480 /// See AbstractAttribute::updateImpl(...). 3481 ChangeStatus updateImpl(Attributor &A) override { 3482 return indicatePessimisticFixpoint(); 3483 } 3484 3485 /// See AbstractAttribute::trackStatistics() 3486 void trackStatistics() const override {} 3487 }; 3488 3489 /// -------------------- Dereferenceable Argument Attribute -------------------- 3490 3491 template <> 3492 ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S, 3493 const DerefState &R) { 3494 ChangeStatus CS0 = 3495 clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState); 3496 ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState); 3497 return CS0 | CS1; 3498 } 3499 3500 struct AADereferenceableImpl : AADereferenceable { 3501 AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} 3502 using StateType = DerefState; 3503 3504 void initialize(Attributor &A) override { 3505 SmallVector<Attribute, 4> Attrs; 3506 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, 3507 Attrs); 3508 for (const Attribute &Attr : Attrs) 3509 takeKnownDerefBytesMaximum(Attr.getValueAsInt()); 3510 3511 NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition(), 3512 /* TrackDependence */ false); 3513 3514 const IRPosition &IRP = this->getIRPosition(); 3515 bool IsFnInterface = IRP.isFnInterfaceKind(); 3516 const Function *FnScope = IRP.getAnchorScope(); 3517 if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition())) 3518 indicatePessimisticFixpoint(); 3519 } 3520 3521 /// See AbstractAttribute::getState() 3522 /// { 3523 StateType &getState() override { return *this; } 3524 const StateType &getState() const override { return *this; } 3525 /// } 3526 3527 /// Helper function for collecting accessed bytes in must-be-executed-context 3528 void addAccessedBytesForUse(Attributor &A, const Use *U, 3529 const Instruction *I) { 3530 const Value *UseV = U->get(); 3531 if (!UseV->getType()->isPointerTy()) 3532 return; 3533 3534 Type *PtrTy = UseV->getType(); 3535 const DataLayout &DL = A.getDataLayout(); 3536 int64_t Offset; 3537 if (const Value *Base = getBasePointerOfAccessPointerOperand( 3538 I, Offset, DL, /*AllowNonInbounds*/ true)) { 3539 if (Base == &getAssociatedValue() && 3540 getPointerOperand(I, /* AllowVolatile */ false) == UseV) { 3541 uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType()); 3542 addAccessedBytes(Offset, Size); 3543 } 3544 } 3545 return; 3546 } 3547 3548 /// See AAFromMustBeExecutedContext 3549 bool followUse(Attributor &A, const Use *U, const Instruction *I) { 3550 bool IsNonNull = false; 3551 bool TrackUse = false; 3552 int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse( 3553 A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse); 3554 3555 addAccessedBytesForUse(A, U, I); 3556 takeKnownDerefBytesMaximum(DerefBytes); 3557 return TrackUse; 3558 } 3559 3560 /// See AbstractAttribute::manifest(...). 3561 ChangeStatus manifest(Attributor &A) override { 3562 ChangeStatus Change = AADereferenceable::manifest(A); 3563 if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) { 3564 removeAttrs({Attribute::DereferenceableOrNull}); 3565 return ChangeStatus::CHANGED; 3566 } 3567 return Change; 3568 } 3569 3570 void getDeducedAttributes(LLVMContext &Ctx, 3571 SmallVectorImpl<Attribute> &Attrs) const override { 3572 // TODO: Add *_globally support 3573 if (isAssumedNonNull()) 3574 Attrs.emplace_back(Attribute::getWithDereferenceableBytes( 3575 Ctx, getAssumedDereferenceableBytes())); 3576 else 3577 Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( 3578 Ctx, getAssumedDereferenceableBytes())); 3579 } 3580 3581 /// See AbstractAttribute::getAsStr(). 3582 const std::string getAsStr() const override { 3583 if (!getAssumedDereferenceableBytes()) 3584 return "unknown-dereferenceable"; 3585 return std::string("dereferenceable") + 3586 (isAssumedNonNull() ? "" : "_or_null") + 3587 (isAssumedGlobal() ? "_globally" : "") + "<" + 3588 std::to_string(getKnownDereferenceableBytes()) + "-" + 3589 std::to_string(getAssumedDereferenceableBytes()) + ">"; 3590 } 3591 }; 3592 3593 /// Dereferenceable attribute for a floating value. 3594 struct AADereferenceableFloating 3595 : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> { 3596 using Base = 3597 AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>; 3598 AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {} 3599 3600 /// See AbstractAttribute::updateImpl(...). 3601 ChangeStatus updateImpl(Attributor &A) override { 3602 ChangeStatus Change = Base::updateImpl(A); 3603 3604 const DataLayout &DL = A.getDataLayout(); 3605 3606 auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool { 3607 unsigned IdxWidth = 3608 DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); 3609 APInt Offset(IdxWidth, 0); 3610 const Value *Base = 3611 V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); 3612 3613 const auto &AA = 3614 A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base)); 3615 int64_t DerefBytes = 0; 3616 if (!Stripped && this == &AA) { 3617 // Use IR information if we did not strip anything. 3618 // TODO: track globally. 3619 bool CanBeNull; 3620 DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull); 3621 T.GlobalState.indicatePessimisticFixpoint(); 3622 } else { 3623 const DerefState &DS = static_cast<const DerefState &>(AA.getState()); 3624 DerefBytes = DS.DerefBytesState.getAssumed(); 3625 T.GlobalState &= DS.GlobalState; 3626 } 3627 3628 // TODO: Use `AAConstantRange` to infer dereferenceable bytes. 3629 3630 // For now we do not try to "increase" dereferenceability due to negative 3631 // indices as we first have to come up with code to deal with loops and 3632 // for overflows of the dereferenceable bytes. 3633 int64_t OffsetSExt = Offset.getSExtValue(); 3634 if (OffsetSExt < 0) 3635 OffsetSExt = 0; 3636 3637 T.takeAssumedDerefBytesMinimum( 3638 std::max(int64_t(0), DerefBytes - OffsetSExt)); 3639 3640 if (this == &AA) { 3641 if (!Stripped) { 3642 // If nothing was stripped IR information is all we got. 3643 T.takeKnownDerefBytesMaximum( 3644 std::max(int64_t(0), DerefBytes - OffsetSExt)); 3645 T.indicatePessimisticFixpoint(); 3646 } else if (OffsetSExt > 0) { 3647 // If something was stripped but there is circular reasoning we look 3648 // for the offset. If it is positive we basically decrease the 3649 // dereferenceable bytes in a circluar loop now, which will simply 3650 // drive them down to the known value in a very slow way which we 3651 // can accelerate. 3652 T.indicatePessimisticFixpoint(); 3653 } 3654 } 3655 3656 return T.isValidState(); 3657 }; 3658 3659 DerefState T; 3660 if (!genericValueTraversal<AADereferenceable, DerefState>( 3661 A, getIRPosition(), *this, T, VisitValueCB)) 3662 return indicatePessimisticFixpoint(); 3663 3664 return Change | clampStateAndIndicateChange(getState(), T); 3665 } 3666 3667 /// See AbstractAttribute::trackStatistics() 3668 void trackStatistics() const override { 3669 STATS_DECLTRACK_FLOATING_ATTR(dereferenceable) 3670 } 3671 }; 3672 3673 /// Dereferenceable attribute for a return value. 3674 struct AADereferenceableReturned final 3675 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> { 3676 AADereferenceableReturned(const IRPosition &IRP) 3677 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>( 3678 IRP) {} 3679 3680 /// See AbstractAttribute::trackStatistics() 3681 void trackStatistics() const override { 3682 STATS_DECLTRACK_FNRET_ATTR(dereferenceable) 3683 } 3684 }; 3685 3686 /// Dereferenceable attribute for an argument 3687 struct AADereferenceableArgument final 3688 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< 3689 AADereferenceable, AADereferenceableImpl> { 3690 using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< 3691 AADereferenceable, AADereferenceableImpl>; 3692 AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {} 3693 3694 /// See AbstractAttribute::trackStatistics() 3695 void trackStatistics() const override { 3696 STATS_DECLTRACK_ARG_ATTR(dereferenceable) 3697 } 3698 }; 3699 3700 /// Dereferenceable attribute for a call site argument. 3701 struct AADereferenceableCallSiteArgument final : AADereferenceableFloating { 3702 AADereferenceableCallSiteArgument(const IRPosition &IRP) 3703 : AADereferenceableFloating(IRP) {} 3704 3705 /// See AbstractAttribute::trackStatistics() 3706 void trackStatistics() const override { 3707 STATS_DECLTRACK_CSARG_ATTR(dereferenceable) 3708 } 3709 }; 3710 3711 /// Dereferenceable attribute deduction for a call site return value. 3712 struct AADereferenceableCallSiteReturned final 3713 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext< 3714 AADereferenceable, AADereferenceableImpl> { 3715 using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext< 3716 AADereferenceable, AADereferenceableImpl>; 3717 AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} 3718 3719 /// See AbstractAttribute::trackStatistics() 3720 void trackStatistics() const override { 3721 STATS_DECLTRACK_CS_ATTR(dereferenceable); 3722 } 3723 }; 3724 3725 // ------------------------ Align Argument Attribute ------------------------ 3726 3727 static unsigned int getKnownAlignForUse(Attributor &A, 3728 AbstractAttribute &QueryingAA, 3729 Value &AssociatedValue, const Use *U, 3730 const Instruction *I, bool &TrackUse) { 3731 // We need to follow common pointer manipulation uses to the accesses they 3732 // feed into. 3733 if (isa<CastInst>(I)) { 3734 // Follow all but ptr2int casts. 3735 TrackUse = !isa<PtrToIntInst>(I); 3736 return 0; 3737 } 3738 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { 3739 if (GEP->hasAllConstantIndices()) { 3740 TrackUse = true; 3741 return 0; 3742 } 3743 } 3744 3745 unsigned Alignment = 0; 3746 if (ImmutableCallSite ICS = ImmutableCallSite(I)) { 3747 if (ICS.isBundleOperand(U) || ICS.isCallee(U)) 3748 return 0; 3749 3750 unsigned ArgNo = ICS.getArgumentNo(U); 3751 IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); 3752 // As long as we only use known information there is no need to track 3753 // dependences here. 3754 auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, 3755 /* TrackDependence */ false); 3756 Alignment = AlignAA.getKnownAlign(); 3757 } 3758 3759 const Value *UseV = U->get(); 3760 if (auto *SI = dyn_cast<StoreInst>(I)) { 3761 if (SI->getPointerOperand() == UseV) 3762 Alignment = SI->getAlignment(); 3763 } else if (auto *LI = dyn_cast<LoadInst>(I)) 3764 Alignment = LI->getAlignment(); 3765 3766 if (Alignment <= 1) 3767 return 0; 3768 3769 auto &DL = A.getDataLayout(); 3770 int64_t Offset; 3771 3772 if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) { 3773 if (Base == &AssociatedValue) { 3774 // BasePointerAddr + Offset = Alignment * Q for some integer Q. 3775 // So we can say that the maximum power of two which is a divisor of 3776 // gcd(Offset, Alignment) is an alignment. 3777 3778 uint32_t gcd = 3779 greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment); 3780 Alignment = llvm::PowerOf2Floor(gcd); 3781 } 3782 } 3783 3784 return Alignment; 3785 } 3786 struct AAAlignImpl : AAAlign { 3787 AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} 3788 3789 /// See AbstractAttribute::initialize(...). 3790 void initialize(Attributor &A) override { 3791 SmallVector<Attribute, 4> Attrs; 3792 getAttrs({Attribute::Alignment}, Attrs); 3793 for (const Attribute &Attr : Attrs) 3794 takeKnownMaximum(Attr.getValueAsInt()); 3795 3796 if (getIRPosition().isFnInterfaceKind() && 3797 (!getAssociatedFunction() || 3798 !getAssociatedFunction()->hasExactDefinition())) 3799 indicatePessimisticFixpoint(); 3800 } 3801 3802 /// See AbstractAttribute::manifest(...). 3803 ChangeStatus manifest(Attributor &A) override { 3804 ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED; 3805 3806 // Check for users that allow alignment annotations. 3807 Value &AssociatedValue = getAssociatedValue(); 3808 for (const Use &U : AssociatedValue.uses()) { 3809 if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { 3810 if (SI->getPointerOperand() == &AssociatedValue) 3811 if (SI->getAlignment() < getAssumedAlign()) { 3812 STATS_DECLTRACK(AAAlign, Store, 3813 "Number of times alignment added to a store"); 3814 SI->setAlignment(Align(getAssumedAlign())); 3815 LoadStoreChanged = ChangeStatus::CHANGED; 3816 } 3817 } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { 3818 if (LI->getPointerOperand() == &AssociatedValue) 3819 if (LI->getAlignment() < getAssumedAlign()) { 3820 LI->setAlignment(Align(getAssumedAlign())); 3821 STATS_DECLTRACK(AAAlign, Load, 3822 "Number of times alignment added to a load"); 3823 LoadStoreChanged = ChangeStatus::CHANGED; 3824 } 3825 } 3826 } 3827 3828 ChangeStatus Changed = AAAlign::manifest(A); 3829 3830 MaybeAlign InheritAlign = 3831 getAssociatedValue().getPointerAlignment(A.getDataLayout()); 3832 if (InheritAlign.valueOrOne() >= getAssumedAlign()) 3833 return LoadStoreChanged; 3834 return Changed | LoadStoreChanged; 3835 } 3836 3837 // TODO: Provide a helper to determine the implied ABI alignment and check in 3838 // the existing manifest method and a new one for AAAlignImpl that value 3839 // to avoid making the alignment explicit if it did not improve. 3840 3841 /// See AbstractAttribute::getDeducedAttributes 3842 virtual void 3843 getDeducedAttributes(LLVMContext &Ctx, 3844 SmallVectorImpl<Attribute> &Attrs) const override { 3845 if (getAssumedAlign() > 1) 3846 Attrs.emplace_back( 3847 Attribute::getWithAlignment(Ctx, Align(getAssumedAlign()))); 3848 } 3849 /// See AAFromMustBeExecutedContext 3850 bool followUse(Attributor &A, const Use *U, const Instruction *I) { 3851 bool TrackUse = false; 3852 3853 unsigned int KnownAlign = 3854 getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse); 3855 takeKnownMaximum(KnownAlign); 3856 3857 return TrackUse; 3858 } 3859 3860 /// See AbstractAttribute::getAsStr(). 3861 const std::string getAsStr() const override { 3862 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + 3863 "-" + std::to_string(getAssumedAlign()) + ">") 3864 : "unknown-align"; 3865 } 3866 }; 3867 3868 /// Align attribute for a floating value. 3869 struct AAAlignFloating : AAFromMustBeExecutedContext<AAAlign, AAAlignImpl> { 3870 using Base = AAFromMustBeExecutedContext<AAAlign, AAAlignImpl>; 3871 AAAlignFloating(const IRPosition &IRP) : Base(IRP) {} 3872 3873 /// See AbstractAttribute::updateImpl(...). 3874 ChangeStatus updateImpl(Attributor &A) override { 3875 Base::updateImpl(A); 3876 3877 const DataLayout &DL = A.getDataLayout(); 3878 3879 auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, 3880 bool Stripped) -> bool { 3881 const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); 3882 if (!Stripped && this == &AA) { 3883 // Use only IR information if we did not strip anything. 3884 const MaybeAlign PA = V.getPointerAlignment(DL); 3885 T.takeKnownMaximum(PA ? PA->value() : 0); 3886 T.indicatePessimisticFixpoint(); 3887 } else { 3888 // Use abstract attribute information. 3889 const AAAlign::StateType &DS = 3890 static_cast<const AAAlign::StateType &>(AA.getState()); 3891 T ^= DS; 3892 } 3893 return T.isValidState(); 3894 }; 3895 3896 StateType T; 3897 if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T, 3898 VisitValueCB)) 3899 return indicatePessimisticFixpoint(); 3900 3901 // TODO: If we know we visited all incoming values, thus no are assumed 3902 // dead, we can take the known information from the state T. 3903 return clampStateAndIndicateChange(getState(), T); 3904 } 3905 3906 /// See AbstractAttribute::trackStatistics() 3907 void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) } 3908 }; 3909 3910 /// Align attribute for function return value. 3911 struct AAAlignReturned final 3912 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> { 3913 AAAlignReturned(const IRPosition &IRP) 3914 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {} 3915 3916 /// See AbstractAttribute::trackStatistics() 3917 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } 3918 }; 3919 3920 /// Align attribute for function argument. 3921 struct AAAlignArgument final 3922 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, 3923 AAAlignImpl> { 3924 AAAlignArgument(const IRPosition &IRP) 3925 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, 3926 AAAlignImpl>( 3927 IRP) {} 3928 3929 /// See AbstractAttribute::trackStatistics() 3930 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) } 3931 }; 3932 3933 struct AAAlignCallSiteArgument final : AAAlignFloating { 3934 AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {} 3935 3936 /// See AbstractAttribute::manifest(...). 3937 ChangeStatus manifest(Attributor &A) override { 3938 ChangeStatus Changed = AAAlignImpl::manifest(A); 3939 MaybeAlign InheritAlign = 3940 getAssociatedValue().getPointerAlignment(A.getDataLayout()); 3941 if (InheritAlign.valueOrOne() >= getAssumedAlign()) 3942 Changed = ChangeStatus::UNCHANGED; 3943 return Changed; 3944 } 3945 3946 /// See AbstractAttribute::updateImpl(Attributor &A). 3947 ChangeStatus updateImpl(Attributor &A) override { 3948 ChangeStatus Changed = AAAlignFloating::updateImpl(A); 3949 if (Argument *Arg = getAssociatedArgument()) { 3950 // We only take known information from the argument 3951 // so we do not need to track a dependence. 3952 const auto &ArgAlignAA = A.getAAFor<AAAlign>( 3953 *this, IRPosition::argument(*Arg), /* TrackDependence */ false); 3954 takeKnownMaximum(ArgAlignAA.getKnownAlign()); 3955 } 3956 return Changed; 3957 } 3958 3959 /// See AbstractAttribute::trackStatistics() 3960 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } 3961 }; 3962 3963 /// Align attribute deduction for a call site return value. 3964 struct AAAlignCallSiteReturned final 3965 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, 3966 AAAlignImpl> { 3967 using Base = 3968 AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, 3969 AAAlignImpl>; 3970 AAAlignCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} 3971 3972 /// See AbstractAttribute::initialize(...). 3973 void initialize(Attributor &A) override { 3974 Base::initialize(A); 3975 Function *F = getAssociatedFunction(); 3976 if (!F) 3977 indicatePessimisticFixpoint(); 3978 } 3979 3980 /// See AbstractAttribute::trackStatistics() 3981 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); } 3982 }; 3983 3984 /// ------------------ Function No-Return Attribute ---------------------------- 3985 struct AANoReturnImpl : public AANoReturn { 3986 AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} 3987 3988 /// See AbstractAttribute::initialize(...). 3989 void initialize(Attributor &A) override { 3990 AANoReturn::initialize(A); 3991 Function *F = getAssociatedFunction(); 3992 if (!F) 3993 indicatePessimisticFixpoint(); 3994 } 3995 3996 /// See AbstractAttribute::getAsStr(). 3997 const std::string getAsStr() const override { 3998 return getAssumed() ? "noreturn" : "may-return"; 3999 } 4000 4001 /// See AbstractAttribute::updateImpl(Attributor &A). 4002 virtual ChangeStatus updateImpl(Attributor &A) override { 4003 auto CheckForNoReturn = [](Instruction &) { return false; }; 4004 if (!A.checkForAllInstructions(CheckForNoReturn, *this, 4005 {(unsigned)Instruction::Ret})) 4006 return indicatePessimisticFixpoint(); 4007 return ChangeStatus::UNCHANGED; 4008 } 4009 }; 4010 4011 struct AANoReturnFunction final : AANoReturnImpl { 4012 AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} 4013 4014 /// See AbstractAttribute::trackStatistics() 4015 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } 4016 }; 4017 4018 /// NoReturn attribute deduction for a call sites. 4019 struct AANoReturnCallSite final : AANoReturnImpl { 4020 AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {} 4021 4022 /// See AbstractAttribute::updateImpl(...). 4023 ChangeStatus updateImpl(Attributor &A) override { 4024 // TODO: Once we have call site specific value information we can provide 4025 // call site specific liveness information and then it makes 4026 // sense to specialize attributes for call sites arguments instead of 4027 // redirecting requests to the callee argument. 4028 Function *F = getAssociatedFunction(); 4029 const IRPosition &FnPos = IRPosition::function(*F); 4030 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos); 4031 return clampStateAndIndicateChange( 4032 getState(), 4033 static_cast<const AANoReturn::StateType &>(FnAA.getState())); 4034 } 4035 4036 /// See AbstractAttribute::trackStatistics() 4037 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); } 4038 }; 4039 4040 /// ----------------------- Variable Capturing --------------------------------- 4041 4042 /// A class to hold the state of for no-capture attributes. 4043 struct AANoCaptureImpl : public AANoCapture { 4044 AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {} 4045 4046 /// See AbstractAttribute::initialize(...). 4047 void initialize(Attributor &A) override { 4048 if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) { 4049 indicateOptimisticFixpoint(); 4050 return; 4051 } 4052 Function *AnchorScope = getAnchorScope(); 4053 if (isFnInterfaceKind() && 4054 (!AnchorScope || !AnchorScope->hasExactDefinition())) { 4055 indicatePessimisticFixpoint(); 4056 return; 4057 } 4058 4059 // You cannot "capture" null in the default address space. 4060 if (isa<ConstantPointerNull>(getAssociatedValue()) && 4061 getAssociatedValue().getType()->getPointerAddressSpace() == 0) { 4062 indicateOptimisticFixpoint(); 4063 return; 4064 } 4065 4066 const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope; 4067 4068 // Check what state the associated function can actually capture. 4069 if (F) 4070 determineFunctionCaptureCapabilities(getIRPosition(), *F, *this); 4071 else 4072 indicatePessimisticFixpoint(); 4073 } 4074 4075 /// See AbstractAttribute::updateImpl(...). 4076 ChangeStatus updateImpl(Attributor &A) override; 4077 4078 /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...). 4079 virtual void 4080 getDeducedAttributes(LLVMContext &Ctx, 4081 SmallVectorImpl<Attribute> &Attrs) const override { 4082 if (!isAssumedNoCaptureMaybeReturned()) 4083 return; 4084 4085 if (getArgNo() >= 0) { 4086 if (isAssumedNoCapture()) 4087 Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture)); 4088 else if (ManifestInternal) 4089 Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned")); 4090 } 4091 } 4092 4093 /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known 4094 /// depending on the ability of the function associated with \p IRP to capture 4095 /// state in memory and through "returning/throwing", respectively. 4096 static void determineFunctionCaptureCapabilities(const IRPosition &IRP, 4097 const Function &F, 4098 BitIntegerState &State) { 4099 // TODO: Once we have memory behavior attributes we should use them here. 4100 4101 // If we know we cannot communicate or write to memory, we do not care about 4102 // ptr2int anymore. 4103 if (F.onlyReadsMemory() && F.doesNotThrow() && 4104 F.getReturnType()->isVoidTy()) { 4105 State.addKnownBits(NO_CAPTURE); 4106 return; 4107 } 4108 4109 // A function cannot capture state in memory if it only reads memory, it can 4110 // however return/throw state and the state might be influenced by the 4111 // pointer value, e.g., loading from a returned pointer might reveal a bit. 4112 if (F.onlyReadsMemory()) 4113 State.addKnownBits(NOT_CAPTURED_IN_MEM); 4114 4115 // A function cannot communicate state back if it does not through 4116 // exceptions and doesn not return values. 4117 if (F.doesNotThrow() && F.getReturnType()->isVoidTy()) 4118 State.addKnownBits(NOT_CAPTURED_IN_RET); 4119 4120 // Check existing "returned" attributes. 4121 int ArgNo = IRP.getArgNo(); 4122 if (F.doesNotThrow() && ArgNo >= 0) { 4123 for (unsigned u = 0, e = F.arg_size(); u < e; ++u) 4124 if (F.hasParamAttribute(u, Attribute::Returned)) { 4125 if (u == unsigned(ArgNo)) 4126 State.removeAssumedBits(NOT_CAPTURED_IN_RET); 4127 else if (F.onlyReadsMemory()) 4128 State.addKnownBits(NO_CAPTURE); 4129 else 4130 State.addKnownBits(NOT_CAPTURED_IN_RET); 4131 break; 4132 } 4133 } 4134 } 4135 4136 /// See AbstractState::getAsStr(). 4137 const std::string getAsStr() const override { 4138 if (isKnownNoCapture()) 4139 return "known not-captured"; 4140 if (isAssumedNoCapture()) 4141 return "assumed not-captured"; 4142 if (isKnownNoCaptureMaybeReturned()) 4143 return "known not-captured-maybe-returned"; 4144 if (isAssumedNoCaptureMaybeReturned()) 4145 return "assumed not-captured-maybe-returned"; 4146 return "assumed-captured"; 4147 } 4148 }; 4149 4150 /// Attributor-aware capture tracker. 4151 struct AACaptureUseTracker final : public CaptureTracker { 4152 4153 /// Create a capture tracker that can lookup in-flight abstract attributes 4154 /// through the Attributor \p A. 4155 /// 4156 /// If a use leads to a potential capture, \p CapturedInMemory is set and the 4157 /// search is stopped. If a use leads to a return instruction, 4158 /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed. 4159 /// If a use leads to a ptr2int which may capture the value, 4160 /// \p CapturedInInteger is set. If a use is found that is currently assumed 4161 /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies 4162 /// set. All values in \p PotentialCopies are later tracked as well. For every 4163 /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0, 4164 /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger 4165 /// conservatively set to true. 4166 AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA, 4167 const AAIsDead &IsDeadAA, AANoCapture::StateType &State, 4168 SmallVectorImpl<const Value *> &PotentialCopies, 4169 unsigned &RemainingUsesToExplore) 4170 : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State), 4171 PotentialCopies(PotentialCopies), 4172 RemainingUsesToExplore(RemainingUsesToExplore) {} 4173 4174 /// Determine if \p V maybe captured. *Also updates the state!* 4175 bool valueMayBeCaptured(const Value *V) { 4176 if (V->getType()->isPointerTy()) { 4177 PointerMayBeCaptured(V, this); 4178 } else { 4179 State.indicatePessimisticFixpoint(); 4180 } 4181 return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); 4182 } 4183 4184 /// See CaptureTracker::tooManyUses(). 4185 void tooManyUses() override { 4186 State.removeAssumedBits(AANoCapture::NO_CAPTURE); 4187 } 4188 4189 bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override { 4190 if (CaptureTracker::isDereferenceableOrNull(O, DL)) 4191 return true; 4192 const auto &DerefAA = A.getAAFor<AADereferenceable>( 4193 NoCaptureAA, IRPosition::value(*O), /* TrackDependence */ true, 4194 DepClassTy::OPTIONAL); 4195 return DerefAA.getAssumedDereferenceableBytes(); 4196 } 4197 4198 /// See CaptureTracker::captured(...). 4199 bool captured(const Use *U) override { 4200 Instruction *UInst = cast<Instruction>(U->getUser()); 4201 LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst 4202 << "\n"); 4203 4204 // Because we may reuse the tracker multiple times we keep track of the 4205 // number of explored uses ourselves as well. 4206 if (RemainingUsesToExplore-- == 0) { 4207 LLVM_DEBUG(dbgs() << " - too many uses to explore!\n"); 4208 return isCapturedIn(/* Memory */ true, /* Integer */ true, 4209 /* Return */ true); 4210 } 4211 4212 // Deal with ptr2int by following uses. 4213 if (isa<PtrToIntInst>(UInst)) { 4214 LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n"); 4215 return valueMayBeCaptured(UInst); 4216 } 4217 4218 // Explicitly catch return instructions. 4219 if (isa<ReturnInst>(UInst)) 4220 return isCapturedIn(/* Memory */ false, /* Integer */ false, 4221 /* Return */ true); 4222 4223 // For now we only use special logic for call sites. However, the tracker 4224 // itself knows about a lot of other non-capturing cases already. 4225 CallSite CS(UInst); 4226 if (!CS || !CS.isArgOperand(U)) 4227 return isCapturedIn(/* Memory */ true, /* Integer */ true, 4228 /* Return */ true); 4229 4230 unsigned ArgNo = CS.getArgumentNo(U); 4231 const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo); 4232 // If we have a abstract no-capture attribute for the argument we can use 4233 // it to justify a non-capture attribute here. This allows recursion! 4234 auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos); 4235 if (ArgNoCaptureAA.isAssumedNoCapture()) 4236 return isCapturedIn(/* Memory */ false, /* Integer */ false, 4237 /* Return */ false); 4238 if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { 4239 addPotentialCopy(CS); 4240 return isCapturedIn(/* Memory */ false, /* Integer */ false, 4241 /* Return */ false); 4242 } 4243 4244 // Lastly, we could not find a reason no-capture can be assumed so we don't. 4245 return isCapturedIn(/* Memory */ true, /* Integer */ true, 4246 /* Return */ true); 4247 } 4248 4249 /// Register \p CS as potential copy of the value we are checking. 4250 void addPotentialCopy(CallSite CS) { 4251 PotentialCopies.push_back(CS.getInstruction()); 4252 } 4253 4254 /// See CaptureTracker::shouldExplore(...). 4255 bool shouldExplore(const Use *U) override { 4256 // Check liveness. 4257 return !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA); 4258 } 4259 4260 /// Update the state according to \p CapturedInMem, \p CapturedInInt, and 4261 /// \p CapturedInRet, then return the appropriate value for use in the 4262 /// CaptureTracker::captured() interface. 4263 bool isCapturedIn(bool CapturedInMem, bool CapturedInInt, 4264 bool CapturedInRet) { 4265 LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int " 4266 << CapturedInInt << "|Ret " << CapturedInRet << "]\n"); 4267 if (CapturedInMem) 4268 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM); 4269 if (CapturedInInt) 4270 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT); 4271 if (CapturedInRet) 4272 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET); 4273 return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); 4274 } 4275 4276 private: 4277 /// The attributor providing in-flight abstract attributes. 4278 Attributor &A; 4279 4280 /// The abstract attribute currently updated. 4281 AANoCapture &NoCaptureAA; 4282 4283 /// The abstract liveness state. 4284 const AAIsDead &IsDeadAA; 4285 4286 /// The state currently updated. 4287 AANoCapture::StateType &State; 4288 4289 /// Set of potential copies of the tracked value. 4290 SmallVectorImpl<const Value *> &PotentialCopies; 4291 4292 /// Global counter to limit the number of explored uses. 4293 unsigned &RemainingUsesToExplore; 4294 }; 4295 4296 ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) { 4297 const IRPosition &IRP = getIRPosition(); 4298 const Value *V = 4299 getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue(); 4300 if (!V) 4301 return indicatePessimisticFixpoint(); 4302 4303 const Function *F = 4304 getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope(); 4305 assert(F && "Expected a function!"); 4306 const IRPosition &FnPos = IRPosition::function(*F); 4307 const auto &IsDeadAA = 4308 A.getAAFor<AAIsDead>(*this, FnPos, /* TrackDependence */ false); 4309 4310 AANoCapture::StateType T; 4311 4312 // Readonly means we cannot capture through memory. 4313 const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>( 4314 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL); 4315 if (FnMemAA.isAssumedReadOnly()) { 4316 T.addKnownBits(NOT_CAPTURED_IN_MEM); 4317 if (FnMemAA.isKnownReadOnly()) 4318 addKnownBits(NOT_CAPTURED_IN_MEM); 4319 } 4320 4321 // Make sure all returned values are different than the underlying value. 4322 // TODO: we could do this in a more sophisticated way inside 4323 // AAReturnedValues, e.g., track all values that escape through returns 4324 // directly somehow. 4325 auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) { 4326 bool SeenConstant = false; 4327 for (auto &It : RVAA.returned_values()) { 4328 if (isa<Constant>(It.first)) { 4329 if (SeenConstant) 4330 return false; 4331 SeenConstant = true; 4332 } else if (!isa<Argument>(It.first) || 4333 It.first == getAssociatedArgument()) 4334 return false; 4335 } 4336 return true; 4337 }; 4338 4339 const auto &NoUnwindAA = A.getAAFor<AANoUnwind>( 4340 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL); 4341 if (NoUnwindAA.isAssumedNoUnwind()) { 4342 bool IsVoidTy = F->getReturnType()->isVoidTy(); 4343 const AAReturnedValues *RVAA = 4344 IsVoidTy ? nullptr 4345 : &A.getAAFor<AAReturnedValues>(*this, FnPos, 4346 /* TrackDependence */ true, 4347 DepClassTy::OPTIONAL); 4348 if (IsVoidTy || CheckReturnedArgs(*RVAA)) { 4349 T.addKnownBits(NOT_CAPTURED_IN_RET); 4350 if (T.isKnown(NOT_CAPTURED_IN_MEM)) 4351 return ChangeStatus::UNCHANGED; 4352 if (NoUnwindAA.isKnownNoUnwind() && 4353 (IsVoidTy || RVAA->getState().isAtFixpoint())) { 4354 addKnownBits(NOT_CAPTURED_IN_RET); 4355 if (isKnown(NOT_CAPTURED_IN_MEM)) 4356 return indicateOptimisticFixpoint(); 4357 } 4358 } 4359 } 4360 4361 // Use the CaptureTracker interface and logic with the specialized tracker, 4362 // defined in AACaptureUseTracker, that can look at in-flight abstract 4363 // attributes and directly updates the assumed state. 4364 SmallVector<const Value *, 4> PotentialCopies; 4365 unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore; 4366 AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies, 4367 RemainingUsesToExplore); 4368 4369 // Check all potential copies of the associated value until we can assume 4370 // none will be captured or we have to assume at least one might be. 4371 unsigned Idx = 0; 4372 PotentialCopies.push_back(V); 4373 while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size()) 4374 Tracker.valueMayBeCaptured(PotentialCopies[Idx++]); 4375 4376 AANoCapture::StateType &S = getState(); 4377 auto Assumed = S.getAssumed(); 4378 S.intersectAssumedBits(T.getAssumed()); 4379 if (!isAssumedNoCaptureMaybeReturned()) 4380 return indicatePessimisticFixpoint(); 4381 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED 4382 : ChangeStatus::CHANGED; 4383 } 4384 4385 /// NoCapture attribute for function arguments. 4386 struct AANoCaptureArgument final : AANoCaptureImpl { 4387 AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} 4388 4389 /// See AbstractAttribute::trackStatistics() 4390 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) } 4391 }; 4392 4393 /// NoCapture attribute for call site arguments. 4394 struct AANoCaptureCallSiteArgument final : AANoCaptureImpl { 4395 AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} 4396 4397 /// See AbstractAttribute::initialize(...). 4398 void initialize(Attributor &A) override { 4399 if (Argument *Arg = getAssociatedArgument()) 4400 if (Arg->hasByValAttr()) 4401 indicateOptimisticFixpoint(); 4402 AANoCaptureImpl::initialize(A); 4403 } 4404 4405 /// See AbstractAttribute::updateImpl(...). 4406 ChangeStatus updateImpl(Attributor &A) override { 4407 // TODO: Once we have call site specific value information we can provide 4408 // call site specific liveness information and then it makes 4409 // sense to specialize attributes for call sites arguments instead of 4410 // redirecting requests to the callee argument. 4411 Argument *Arg = getAssociatedArgument(); 4412 if (!Arg) 4413 return indicatePessimisticFixpoint(); 4414 const IRPosition &ArgPos = IRPosition::argument(*Arg); 4415 auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos); 4416 return clampStateAndIndicateChange( 4417 getState(), 4418 static_cast<const AANoCapture::StateType &>(ArgAA.getState())); 4419 } 4420 4421 /// See AbstractAttribute::trackStatistics() 4422 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)}; 4423 }; 4424 4425 /// NoCapture attribute for floating values. 4426 struct AANoCaptureFloating final : AANoCaptureImpl { 4427 AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} 4428 4429 /// See AbstractAttribute::trackStatistics() 4430 void trackStatistics() const override { 4431 STATS_DECLTRACK_FLOATING_ATTR(nocapture) 4432 } 4433 }; 4434 4435 /// NoCapture attribute for function return value. 4436 struct AANoCaptureReturned final : AANoCaptureImpl { 4437 AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) { 4438 llvm_unreachable("NoCapture is not applicable to function returns!"); 4439 } 4440 4441 /// See AbstractAttribute::initialize(...). 4442 void initialize(Attributor &A) override { 4443 llvm_unreachable("NoCapture is not applicable to function returns!"); 4444 } 4445 4446 /// See AbstractAttribute::updateImpl(...). 4447 ChangeStatus updateImpl(Attributor &A) override { 4448 llvm_unreachable("NoCapture is not applicable to function returns!"); 4449 } 4450 4451 /// See AbstractAttribute::trackStatistics() 4452 void trackStatistics() const override {} 4453 }; 4454 4455 /// NoCapture attribute deduction for a call site return value. 4456 struct AANoCaptureCallSiteReturned final : AANoCaptureImpl { 4457 AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} 4458 4459 /// See AbstractAttribute::trackStatistics() 4460 void trackStatistics() const override { 4461 STATS_DECLTRACK_CSRET_ATTR(nocapture) 4462 } 4463 }; 4464 4465 /// ------------------ Value Simplify Attribute ---------------------------- 4466 struct AAValueSimplifyImpl : AAValueSimplify { 4467 AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {} 4468 4469 /// See AbstractAttribute::initialize(...). 4470 void initialize(Attributor &A) override { 4471 if (getAssociatedValue().getType()->isVoidTy()) 4472 indicatePessimisticFixpoint(); 4473 } 4474 4475 /// See AbstractAttribute::getAsStr(). 4476 const std::string getAsStr() const override { 4477 return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple") 4478 : "not-simple"; 4479 } 4480 4481 /// See AbstractAttribute::trackStatistics() 4482 void trackStatistics() const override {} 4483 4484 /// See AAValueSimplify::getAssumedSimplifiedValue() 4485 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { 4486 if (!getAssumed()) 4487 return const_cast<Value *>(&getAssociatedValue()); 4488 return SimplifiedAssociatedValue; 4489 } 4490 4491 /// Helper function for querying AAValueSimplify and updating candicate. 4492 /// \param QueryingValue Value trying to unify with SimplifiedValue 4493 /// \param AccumulatedSimplifiedValue Current simplification result. 4494 static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA, 4495 Value &QueryingValue, 4496 Optional<Value *> &AccumulatedSimplifiedValue) { 4497 // FIXME: Add a typecast support. 4498 4499 auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>( 4500 QueryingAA, IRPosition::value(QueryingValue)); 4501 4502 Optional<Value *> QueryingValueSimplified = 4503 ValueSimplifyAA.getAssumedSimplifiedValue(A); 4504 4505 if (!QueryingValueSimplified.hasValue()) 4506 return true; 4507 4508 if (!QueryingValueSimplified.getValue()) 4509 return false; 4510 4511 Value &QueryingValueSimplifiedUnwrapped = 4512 *QueryingValueSimplified.getValue(); 4513 4514 if (AccumulatedSimplifiedValue.hasValue() && 4515 !isa<UndefValue>(AccumulatedSimplifiedValue.getValue()) && 4516 !isa<UndefValue>(QueryingValueSimplifiedUnwrapped)) 4517 return AccumulatedSimplifiedValue == QueryingValueSimplified; 4518 if (AccumulatedSimplifiedValue.hasValue() && 4519 isa<UndefValue>(QueryingValueSimplifiedUnwrapped)) 4520 return true; 4521 4522 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << QueryingValue 4523 << " is assumed to be " 4524 << QueryingValueSimplifiedUnwrapped << "\n"); 4525 4526 AccumulatedSimplifiedValue = QueryingValueSimplified; 4527 return true; 4528 } 4529 4530 bool askSimplifiedValueForAAValueConstantRange(Attributor &A) { 4531 if (!getAssociatedValue().getType()->isIntegerTy()) 4532 return false; 4533 4534 const auto &ValueConstantRangeAA = 4535 A.getAAFor<AAValueConstantRange>(*this, getIRPosition()); 4536 4537 Optional<ConstantInt *> COpt = 4538 ValueConstantRangeAA.getAssumedConstantInt(A); 4539 if (COpt.hasValue()) { 4540 if (auto *C = COpt.getValue()) 4541 SimplifiedAssociatedValue = C; 4542 else 4543 return false; 4544 } else { 4545 SimplifiedAssociatedValue = llvm::None; 4546 } 4547 return true; 4548 } 4549 4550 /// See AbstractAttribute::manifest(...). 4551 ChangeStatus manifest(Attributor &A) override { 4552 ChangeStatus Changed = ChangeStatus::UNCHANGED; 4553 4554 if (SimplifiedAssociatedValue.hasValue() && 4555 !SimplifiedAssociatedValue.getValue()) 4556 return Changed; 4557 4558 Value &V = getAssociatedValue(); 4559 auto *C = SimplifiedAssociatedValue.hasValue() 4560 ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue()) 4561 : UndefValue::get(V.getType()); 4562 if (C) { 4563 // We can replace the AssociatedValue with the constant. 4564 if (!V.user_empty() && &V != C && V.getType() == C->getType()) { 4565 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C 4566 << " :: " << *this << "\n"); 4567 if (A.changeValueAfterManifest(V, *C)) 4568 Changed = ChangeStatus::CHANGED; 4569 } 4570 } 4571 4572 return Changed | AAValueSimplify::manifest(A); 4573 } 4574 4575 /// See AbstractState::indicatePessimisticFixpoint(...). 4576 ChangeStatus indicatePessimisticFixpoint() override { 4577 // NOTE: Associated value will be returned in a pessimistic fixpoint and is 4578 // regarded as known. That's why`indicateOptimisticFixpoint` is called. 4579 SimplifiedAssociatedValue = &getAssociatedValue(); 4580 indicateOptimisticFixpoint(); 4581 return ChangeStatus::CHANGED; 4582 } 4583 4584 protected: 4585 // An assumed simplified value. Initially, it is set to Optional::None, which 4586 // means that the value is not clear under current assumption. If in the 4587 // pessimistic state, getAssumedSimplifiedValue doesn't return this value but 4588 // returns orignal associated value. 4589 Optional<Value *> SimplifiedAssociatedValue; 4590 }; 4591 4592 struct AAValueSimplifyArgument final : AAValueSimplifyImpl { 4593 AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} 4594 4595 void initialize(Attributor &A) override { 4596 AAValueSimplifyImpl::initialize(A); 4597 if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration()) 4598 indicatePessimisticFixpoint(); 4599 if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest}, 4600 /* IgnoreSubsumingPositions */ true)) 4601 indicatePessimisticFixpoint(); 4602 4603 // FIXME: This is a hack to prevent us from propagating function poiner in 4604 // the new pass manager CGSCC pass as it creates call edges the 4605 // CallGraphUpdater cannot handle yet. 4606 Value &V = getAssociatedValue(); 4607 if (V.getType()->isPointerTy() && 4608 V.getType()->getPointerElementType()->isFunctionTy() && 4609 !A.isModulePass() && 4610 A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>( 4611 *getAnchorScope())) 4612 indicatePessimisticFixpoint(); 4613 } 4614 4615 /// See AbstractAttribute::updateImpl(...). 4616 ChangeStatus updateImpl(Attributor &A) override { 4617 // Byval is only replacable if it is readonly otherwise we would write into 4618 // the replaced value and not the copy that byval creates implicitly. 4619 Argument *Arg = getAssociatedArgument(); 4620 if (Arg->hasByValAttr()) { 4621 // TODO: We probably need to verify synchronization is not an issue, e.g., 4622 // there is no race by not copying a constant byval. 4623 const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); 4624 if (!MemAA.isAssumedReadOnly()) 4625 return indicatePessimisticFixpoint(); 4626 } 4627 4628 bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); 4629 4630 auto PredForCallSite = [&](AbstractCallSite ACS) { 4631 const IRPosition &ACSArgPos = 4632 IRPosition::callsite_argument(ACS, getArgNo()); 4633 // Check if a coresponding argument was found or if it is on not 4634 // associated (which can happen for callback calls). 4635 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) 4636 return false; 4637 4638 // We can only propagate thread independent values through callbacks. 4639 // This is different to direct/indirect call sites because for them we 4640 // know the thread executing the caller and callee is the same. For 4641 // callbacks this is not guaranteed, thus a thread dependent value could 4642 // be different for the caller and callee, making it invalid to propagate. 4643 Value &ArgOp = ACSArgPos.getAssociatedValue(); 4644 if (ACS.isCallbackCall()) 4645 if (auto *C = dyn_cast<Constant>(&ArgOp)) 4646 if (C->isThreadDependent()) 4647 return false; 4648 return checkAndUpdate(A, *this, ArgOp, SimplifiedAssociatedValue); 4649 }; 4650 4651 bool AllCallSitesKnown; 4652 if (!A.checkForAllCallSites(PredForCallSite, *this, true, 4653 AllCallSitesKnown)) 4654 if (!askSimplifiedValueForAAValueConstantRange(A)) 4655 return indicatePessimisticFixpoint(); 4656 4657 // If a candicate was found in this update, return CHANGED. 4658 return HasValueBefore == SimplifiedAssociatedValue.hasValue() 4659 ? ChangeStatus::UNCHANGED 4660 : ChangeStatus ::CHANGED; 4661 } 4662 4663 /// See AbstractAttribute::trackStatistics() 4664 void trackStatistics() const override { 4665 STATS_DECLTRACK_ARG_ATTR(value_simplify) 4666 } 4667 }; 4668 4669 struct AAValueSimplifyReturned : AAValueSimplifyImpl { 4670 AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} 4671 4672 /// See AbstractAttribute::updateImpl(...). 4673 ChangeStatus updateImpl(Attributor &A) override { 4674 bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); 4675 4676 auto PredForReturned = [&](Value &V) { 4677 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); 4678 }; 4679 4680 if (!A.checkForAllReturnedValues(PredForReturned, *this)) 4681 if (!askSimplifiedValueForAAValueConstantRange(A)) 4682 return indicatePessimisticFixpoint(); 4683 4684 // If a candicate was found in this update, return CHANGED. 4685 return HasValueBefore == SimplifiedAssociatedValue.hasValue() 4686 ? ChangeStatus::UNCHANGED 4687 : ChangeStatus ::CHANGED; 4688 } 4689 4690 ChangeStatus manifest(Attributor &A) override { 4691 ChangeStatus Changed = ChangeStatus::UNCHANGED; 4692 4693 if (SimplifiedAssociatedValue.hasValue() && 4694 !SimplifiedAssociatedValue.getValue()) 4695 return Changed; 4696 4697 Value &V = getAssociatedValue(); 4698 auto *C = SimplifiedAssociatedValue.hasValue() 4699 ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue()) 4700 : UndefValue::get(V.getType()); 4701 if (C) { 4702 auto PredForReturned = 4703 [&](Value &V, const SmallSetVector<ReturnInst *, 4> &RetInsts) { 4704 // We can replace the AssociatedValue with the constant. 4705 if (&V == C || V.getType() != C->getType() || isa<UndefValue>(V)) 4706 return true; 4707 if (auto *CI = dyn_cast<CallInst>(&V)) 4708 if (CI->isMustTailCall()) 4709 return true; 4710 4711 for (ReturnInst *RI : RetInsts) { 4712 if (RI->getFunction() != getAnchorScope()) 4713 continue; 4714 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C 4715 << " in " << *RI << " :: " << *this << "\n"); 4716 if (A.changeUseAfterManifest(RI->getOperandUse(0), *C)) 4717 Changed = ChangeStatus::CHANGED; 4718 } 4719 return true; 4720 }; 4721 A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this); 4722 } 4723 4724 return Changed | AAValueSimplify::manifest(A); 4725 } 4726 4727 /// See AbstractAttribute::trackStatistics() 4728 void trackStatistics() const override { 4729 STATS_DECLTRACK_FNRET_ATTR(value_simplify) 4730 } 4731 }; 4732 4733 struct AAValueSimplifyFloating : AAValueSimplifyImpl { 4734 AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} 4735 4736 /// See AbstractAttribute::initialize(...). 4737 void initialize(Attributor &A) override { 4738 AAValueSimplifyImpl::initialize(A); 4739 Value &V = getAnchorValue(); 4740 4741 // TODO: add other stuffs 4742 if (isa<Constant>(V)) 4743 indicatePessimisticFixpoint(); 4744 } 4745 4746 /// See AbstractAttribute::updateImpl(...). 4747 ChangeStatus updateImpl(Attributor &A) override { 4748 bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); 4749 4750 auto VisitValueCB = [&](Value &V, bool &, bool Stripped) -> bool { 4751 auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V)); 4752 if (!Stripped && this == &AA) { 4753 // TODO: Look the instruction and check recursively. 4754 4755 LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V 4756 << "\n"); 4757 return false; 4758 } 4759 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); 4760 }; 4761 4762 bool Dummy = false; 4763 if (!genericValueTraversal<AAValueSimplify, bool>(A, getIRPosition(), *this, 4764 Dummy, VisitValueCB)) 4765 if (!askSimplifiedValueForAAValueConstantRange(A)) 4766 return indicatePessimisticFixpoint(); 4767 4768 // If a candicate was found in this update, return CHANGED. 4769 4770 return HasValueBefore == SimplifiedAssociatedValue.hasValue() 4771 ? ChangeStatus::UNCHANGED 4772 : ChangeStatus ::CHANGED; 4773 } 4774 4775 /// See AbstractAttribute::trackStatistics() 4776 void trackStatistics() const override { 4777 STATS_DECLTRACK_FLOATING_ATTR(value_simplify) 4778 } 4779 }; 4780 4781 struct AAValueSimplifyFunction : AAValueSimplifyImpl { 4782 AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} 4783 4784 /// See AbstractAttribute::initialize(...). 4785 void initialize(Attributor &A) override { 4786 SimplifiedAssociatedValue = &getAnchorValue(); 4787 indicateOptimisticFixpoint(); 4788 } 4789 /// See AbstractAttribute::initialize(...). 4790 ChangeStatus updateImpl(Attributor &A) override { 4791 llvm_unreachable( 4792 "AAValueSimplify(Function|CallSite)::updateImpl will not be called"); 4793 } 4794 /// See AbstractAttribute::trackStatistics() 4795 void trackStatistics() const override { 4796 STATS_DECLTRACK_FN_ATTR(value_simplify) 4797 } 4798 }; 4799 4800 struct AAValueSimplifyCallSite : AAValueSimplifyFunction { 4801 AAValueSimplifyCallSite(const IRPosition &IRP) 4802 : AAValueSimplifyFunction(IRP) {} 4803 /// See AbstractAttribute::trackStatistics() 4804 void trackStatistics() const override { 4805 STATS_DECLTRACK_CS_ATTR(value_simplify) 4806 } 4807 }; 4808 4809 struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned { 4810 AAValueSimplifyCallSiteReturned(const IRPosition &IRP) 4811 : AAValueSimplifyReturned(IRP) {} 4812 4813 /// See AbstractAttribute::manifest(...). 4814 ChangeStatus manifest(Attributor &A) override { 4815 if (auto *CI = dyn_cast<CallInst>(&getAssociatedValue())) 4816 if (CI->isMustTailCall()) 4817 return ChangeStatus::UNCHANGED; 4818 return AAValueSimplifyImpl::manifest(A); 4819 } 4820 4821 void trackStatistics() const override { 4822 STATS_DECLTRACK_CSRET_ATTR(value_simplify) 4823 } 4824 }; 4825 struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating { 4826 AAValueSimplifyCallSiteArgument(const IRPosition &IRP) 4827 : AAValueSimplifyFloating(IRP) {} 4828 4829 void trackStatistics() const override { 4830 STATS_DECLTRACK_CSARG_ATTR(value_simplify) 4831 } 4832 }; 4833 4834 /// ----------------------- Heap-To-Stack Conversion --------------------------- 4835 struct AAHeapToStackImpl : public AAHeapToStack { 4836 AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {} 4837 4838 const std::string getAsStr() const override { 4839 return "[H2S] Mallocs: " + std::to_string(MallocCalls.size()); 4840 } 4841 4842 ChangeStatus manifest(Attributor &A) override { 4843 assert(getState().isValidState() && 4844 "Attempted to manifest an invalid state!"); 4845 4846 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 4847 Function *F = getAssociatedFunction(); 4848 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); 4849 4850 for (Instruction *MallocCall : MallocCalls) { 4851 // This malloc cannot be replaced. 4852 if (BadMallocCalls.count(MallocCall)) 4853 continue; 4854 4855 for (Instruction *FreeCall : FreesForMalloc[MallocCall]) { 4856 LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n"); 4857 A.deleteAfterManifest(*FreeCall); 4858 HasChanged = ChangeStatus::CHANGED; 4859 } 4860 4861 LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall 4862 << "\n"); 4863 4864 Constant *Size; 4865 if (isCallocLikeFn(MallocCall, TLI)) { 4866 auto *Num = cast<ConstantInt>(MallocCall->getOperand(0)); 4867 auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1)); 4868 APInt TotalSize = SizeT->getValue() * Num->getValue(); 4869 Size = 4870 ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize); 4871 } else { 4872 Size = cast<ConstantInt>(MallocCall->getOperand(0)); 4873 } 4874 4875 unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace(); 4876 Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS, 4877 Size, "", MallocCall->getNextNode()); 4878 4879 if (AI->getType() != MallocCall->getType()) 4880 AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc", 4881 AI->getNextNode()); 4882 4883 A.changeValueAfterManifest(*MallocCall, *AI); 4884 4885 if (auto *II = dyn_cast<InvokeInst>(MallocCall)) { 4886 auto *NBB = II->getNormalDest(); 4887 BranchInst::Create(NBB, MallocCall->getParent()); 4888 A.deleteAfterManifest(*MallocCall); 4889 } else { 4890 A.deleteAfterManifest(*MallocCall); 4891 } 4892 4893 if (isCallocLikeFn(MallocCall, TLI)) { 4894 auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc", 4895 AI->getNextNode()); 4896 Value *Ops[] = { 4897 BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size, 4898 ConstantInt::get(Type::getInt1Ty(F->getContext()), false)}; 4899 4900 Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()}; 4901 Module *M = F->getParent(); 4902 Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); 4903 CallInst::Create(Fn, Ops, "", BI->getNextNode()); 4904 } 4905 HasChanged = ChangeStatus::CHANGED; 4906 } 4907 4908 return HasChanged; 4909 } 4910 4911 /// Collection of all malloc calls in a function. 4912 SmallSetVector<Instruction *, 4> MallocCalls; 4913 4914 /// Collection of malloc calls that cannot be converted. 4915 DenseSet<const Instruction *> BadMallocCalls; 4916 4917 /// A map for each malloc call to the set of associated free calls. 4918 DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc; 4919 4920 ChangeStatus updateImpl(Attributor &A) override; 4921 }; 4922 4923 ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) { 4924 const Function *F = getAssociatedFunction(); 4925 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); 4926 4927 MustBeExecutedContextExplorer &Explorer = 4928 A.getInfoCache().getMustBeExecutedContextExplorer(); 4929 4930 auto FreeCheck = [&](Instruction &I) { 4931 const auto &Frees = FreesForMalloc.lookup(&I); 4932 if (Frees.size() != 1) 4933 return false; 4934 Instruction *UniqueFree = *Frees.begin(); 4935 return Explorer.findInContextOf(UniqueFree, I.getNextNode()); 4936 }; 4937 4938 auto UsesCheck = [&](Instruction &I) { 4939 bool ValidUsesOnly = true; 4940 bool MustUse = true; 4941 auto Pred = [&](const Use &U, bool &Follow) -> bool { 4942 Instruction *UserI = cast<Instruction>(U.getUser()); 4943 if (isa<LoadInst>(UserI)) 4944 return true; 4945 if (auto *SI = dyn_cast<StoreInst>(UserI)) { 4946 if (SI->getValueOperand() == U.get()) { 4947 LLVM_DEBUG(dbgs() 4948 << "[H2S] escaping store to memory: " << *UserI << "\n"); 4949 ValidUsesOnly = false; 4950 } else { 4951 // A store into the malloc'ed memory is fine. 4952 } 4953 return true; 4954 } 4955 if (auto *CB = dyn_cast<CallBase>(UserI)) { 4956 if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd()) 4957 return true; 4958 // Record malloc. 4959 if (isFreeCall(UserI, TLI)) { 4960 if (MustUse) { 4961 FreesForMalloc[&I].insert(UserI); 4962 } else { 4963 LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: " 4964 << *UserI << "\n"); 4965 ValidUsesOnly = false; 4966 } 4967 return true; 4968 } 4969 4970 unsigned ArgNo = CB->getArgOperandNo(&U); 4971 4972 const auto &NoCaptureAA = A.getAAFor<AANoCapture>( 4973 *this, IRPosition::callsite_argument(*CB, ArgNo)); 4974 4975 // If a callsite argument use is nofree, we are fine. 4976 const auto &ArgNoFreeAA = A.getAAFor<AANoFree>( 4977 *this, IRPosition::callsite_argument(*CB, ArgNo)); 4978 4979 if (!NoCaptureAA.isAssumedNoCapture() || 4980 !ArgNoFreeAA.isAssumedNoFree()) { 4981 LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n"); 4982 ValidUsesOnly = false; 4983 } 4984 return true; 4985 } 4986 4987 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || 4988 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { 4989 MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI)); 4990 Follow = true; 4991 return true; 4992 } 4993 // Unknown user for which we can not track uses further (in a way that 4994 // makes sense). 4995 LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n"); 4996 ValidUsesOnly = false; 4997 return true; 4998 }; 4999 A.checkForAllUses(Pred, *this, I); 5000 return ValidUsesOnly; 5001 }; 5002 5003 auto MallocCallocCheck = [&](Instruction &I) { 5004 if (BadMallocCalls.count(&I)) 5005 return true; 5006 5007 bool IsMalloc = isMallocLikeFn(&I, TLI); 5008 bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI); 5009 if (!IsMalloc && !IsCalloc) { 5010 BadMallocCalls.insert(&I); 5011 return true; 5012 } 5013 5014 if (IsMalloc) { 5015 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0))) 5016 if (Size->getValue().ule(MaxHeapToStackSize)) 5017 if (UsesCheck(I) || FreeCheck(I)) { 5018 MallocCalls.insert(&I); 5019 return true; 5020 } 5021 } else if (IsCalloc) { 5022 bool Overflow = false; 5023 if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0))) 5024 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1))) 5025 if ((Size->getValue().umul_ov(Num->getValue(), Overflow)) 5026 .ule(MaxHeapToStackSize)) 5027 if (!Overflow && (UsesCheck(I) || FreeCheck(I))) { 5028 MallocCalls.insert(&I); 5029 return true; 5030 } 5031 } 5032 5033 BadMallocCalls.insert(&I); 5034 return true; 5035 }; 5036 5037 size_t NumBadMallocs = BadMallocCalls.size(); 5038 5039 A.checkForAllCallLikeInstructions(MallocCallocCheck, *this); 5040 5041 if (NumBadMallocs != BadMallocCalls.size()) 5042 return ChangeStatus::CHANGED; 5043 5044 return ChangeStatus::UNCHANGED; 5045 } 5046 5047 struct AAHeapToStackFunction final : public AAHeapToStackImpl { 5048 AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {} 5049 5050 /// See AbstractAttribute::trackStatistics() 5051 void trackStatistics() const override { 5052 STATS_DECL(MallocCalls, Function, 5053 "Number of malloc calls converted to allocas"); 5054 for (auto *C : MallocCalls) 5055 if (!BadMallocCalls.count(C)) 5056 ++BUILD_STAT_NAME(MallocCalls, Function); 5057 } 5058 }; 5059 5060 /// ----------------------- Privatizable Pointers ------------------------------ 5061 struct AAPrivatizablePtrImpl : public AAPrivatizablePtr { 5062 AAPrivatizablePtrImpl(const IRPosition &IRP) 5063 : AAPrivatizablePtr(IRP), PrivatizableType(llvm::None) {} 5064 5065 ChangeStatus indicatePessimisticFixpoint() override { 5066 AAPrivatizablePtr::indicatePessimisticFixpoint(); 5067 PrivatizableType = nullptr; 5068 return ChangeStatus::CHANGED; 5069 } 5070 5071 /// Identify the type we can chose for a private copy of the underlying 5072 /// argument. None means it is not clear yet, nullptr means there is none. 5073 virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0; 5074 5075 /// Return a privatizable type that encloses both T0 and T1. 5076 /// TODO: This is merely a stub for now as we should manage a mapping as well. 5077 Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) { 5078 if (!T0.hasValue()) 5079 return T1; 5080 if (!T1.hasValue()) 5081 return T0; 5082 if (T0 == T1) 5083 return T0; 5084 return nullptr; 5085 } 5086 5087 Optional<Type *> getPrivatizableType() const override { 5088 return PrivatizableType; 5089 } 5090 5091 const std::string getAsStr() const override { 5092 return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]"; 5093 } 5094 5095 protected: 5096 Optional<Type *> PrivatizableType; 5097 }; 5098 5099 // TODO: Do this for call site arguments (probably also other values) as well. 5100 5101 struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl { 5102 AAPrivatizablePtrArgument(const IRPosition &IRP) 5103 : AAPrivatizablePtrImpl(IRP) {} 5104 5105 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...) 5106 Optional<Type *> identifyPrivatizableType(Attributor &A) override { 5107 // If this is a byval argument and we know all the call sites (so we can 5108 // rewrite them), there is no need to check them explicitly. 5109 bool AllCallSitesKnown; 5110 if (getIRPosition().hasAttr(Attribute::ByVal) && 5111 A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this, 5112 true, AllCallSitesKnown)) 5113 return getAssociatedValue().getType()->getPointerElementType(); 5114 5115 Optional<Type *> Ty; 5116 unsigned ArgNo = getIRPosition().getArgNo(); 5117 5118 // Make sure the associated call site argument has the same type at all call 5119 // sites and it is an allocation we know is safe to privatize, for now that 5120 // means we only allow alloca instructions. 5121 // TODO: We can additionally analyze the accesses in the callee to create 5122 // the type from that information instead. That is a little more 5123 // involved and will be done in a follow up patch. 5124 auto CallSiteCheck = [&](AbstractCallSite ACS) { 5125 IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); 5126 // Check if a coresponding argument was found or if it is one not 5127 // associated (which can happen for callback calls). 5128 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) 5129 return false; 5130 5131 // Check that all call sites agree on a type. 5132 auto &PrivCSArgAA = A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos); 5133 Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType(); 5134 5135 LLVM_DEBUG({ 5136 dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; 5137 if (CSTy.hasValue() && CSTy.getValue()) 5138 CSTy.getValue()->print(dbgs()); 5139 else if (CSTy.hasValue()) 5140 dbgs() << "<nullptr>"; 5141 else 5142 dbgs() << "<none>"; 5143 }); 5144 5145 Ty = combineTypes(Ty, CSTy); 5146 5147 LLVM_DEBUG({ 5148 dbgs() << " : New Type: "; 5149 if (Ty.hasValue() && Ty.getValue()) 5150 Ty.getValue()->print(dbgs()); 5151 else if (Ty.hasValue()) 5152 dbgs() << "<nullptr>"; 5153 else 5154 dbgs() << "<none>"; 5155 dbgs() << "\n"; 5156 }); 5157 5158 return !Ty.hasValue() || Ty.getValue(); 5159 }; 5160 5161 if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown)) 5162 return nullptr; 5163 return Ty; 5164 } 5165 5166 /// See AbstractAttribute::updateImpl(...). 5167 ChangeStatus updateImpl(Attributor &A) override { 5168 PrivatizableType = identifyPrivatizableType(A); 5169 if (!PrivatizableType.hasValue()) 5170 return ChangeStatus::UNCHANGED; 5171 if (!PrivatizableType.getValue()) 5172 return indicatePessimisticFixpoint(); 5173 5174 // Avoid arguments with padding for now. 5175 if (!getIRPosition().hasAttr(Attribute::ByVal) && 5176 !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(), 5177 A.getInfoCache().getDL())) { 5178 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n"); 5179 return indicatePessimisticFixpoint(); 5180 } 5181 5182 // Verify callee and caller agree on how the promoted argument would be 5183 // passed. 5184 // TODO: The use of the ArgumentPromotion interface here is ugly, we need a 5185 // specialized form of TargetTransformInfo::areFunctionArgsABICompatible 5186 // which doesn't require the arguments ArgumentPromotion wanted to pass. 5187 Function &Fn = *getIRPosition().getAnchorScope(); 5188 SmallPtrSet<Argument *, 1> ArgsToPromote, Dummy; 5189 ArgsToPromote.insert(getAssociatedArgument()); 5190 const auto *TTI = 5191 A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn); 5192 if (!TTI || 5193 !ArgumentPromotionPass::areFunctionArgsABICompatible( 5194 Fn, *TTI, ArgsToPromote, Dummy) || 5195 ArgsToPromote.empty()) { 5196 LLVM_DEBUG( 5197 dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for " 5198 << Fn.getName() << "\n"); 5199 return indicatePessimisticFixpoint(); 5200 } 5201 5202 // Collect the types that will replace the privatizable type in the function 5203 // signature. 5204 SmallVector<Type *, 16> ReplacementTypes; 5205 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes); 5206 5207 // Register a rewrite of the argument. 5208 Argument *Arg = getAssociatedArgument(); 5209 if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) { 5210 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n"); 5211 return indicatePessimisticFixpoint(); 5212 } 5213 5214 unsigned ArgNo = Arg->getArgNo(); 5215 5216 // Helper to check if for the given call site the associated argument is 5217 // passed to a callback where the privatization would be different. 5218 auto IsCompatiblePrivArgOfCallback = [&](CallSite CS) { 5219 SmallVector<const Use *, 4> CBUses; 5220 AbstractCallSite::getCallbackUses(CS, CBUses); 5221 for (const Use *U : CBUses) { 5222 AbstractCallSite CBACS(U); 5223 assert(CBACS && CBACS.isCallbackCall()); 5224 for (Argument &CBArg : CBACS.getCalledFunction()->args()) { 5225 int CBArgNo = CBACS.getCallArgOperandNo(CBArg); 5226 5227 LLVM_DEBUG({ 5228 dbgs() 5229 << "[AAPrivatizablePtr] Argument " << *Arg 5230 << "check if can be privatized in the context of its parent (" 5231 << Arg->getParent()->getName() 5232 << ")\n[AAPrivatizablePtr] because it is an argument in a " 5233 "callback (" 5234 << CBArgNo << "@" << CBACS.getCalledFunction()->getName() 5235 << ")\n[AAPrivatizablePtr] " << CBArg << " : " 5236 << CBACS.getCallArgOperand(CBArg) << " vs " 5237 << CS.getArgOperand(ArgNo) << "\n" 5238 << "[AAPrivatizablePtr] " << CBArg << " : " 5239 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; 5240 }); 5241 5242 if (CBArgNo != int(ArgNo)) 5243 continue; 5244 const auto &CBArgPrivAA = 5245 A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(CBArg)); 5246 if (CBArgPrivAA.isValidState()) { 5247 auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType(); 5248 if (!CBArgPrivTy.hasValue()) 5249 continue; 5250 if (CBArgPrivTy.getValue() == PrivatizableType) 5251 continue; 5252 } 5253 5254 LLVM_DEBUG({ 5255 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg 5256 << " cannot be privatized in the context of its parent (" 5257 << Arg->getParent()->getName() 5258 << ")\n[AAPrivatizablePtr] because it is an argument in a " 5259 "callback (" 5260 << CBArgNo << "@" << CBACS.getCalledFunction()->getName() 5261 << ").\n[AAPrivatizablePtr] for which the argument " 5262 "privatization is not compatible.\n"; 5263 }); 5264 return false; 5265 } 5266 } 5267 return true; 5268 }; 5269 5270 // Helper to check if for the given call site the associated argument is 5271 // passed to a direct call where the privatization would be different. 5272 auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) { 5273 CallBase *DC = cast<CallBase>(ACS.getInstruction()); 5274 int DCArgNo = ACS.getCallArgOperandNo(ArgNo); 5275 assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->getNumArgOperands() && 5276 "Expected a direct call operand for callback call operand"); 5277 5278 LLVM_DEBUG({ 5279 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg 5280 << " check if be privatized in the context of its parent (" 5281 << Arg->getParent()->getName() 5282 << ")\n[AAPrivatizablePtr] because it is an argument in a " 5283 "direct call of (" 5284 << DCArgNo << "@" << DC->getCalledFunction()->getName() 5285 << ").\n"; 5286 }); 5287 5288 Function *DCCallee = DC->getCalledFunction(); 5289 if (unsigned(DCArgNo) < DCCallee->arg_size()) { 5290 const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>( 5291 *this, IRPosition::argument(*DCCallee->getArg(DCArgNo))); 5292 if (DCArgPrivAA.isValidState()) { 5293 auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType(); 5294 if (!DCArgPrivTy.hasValue()) 5295 return true; 5296 if (DCArgPrivTy.getValue() == PrivatizableType) 5297 return true; 5298 } 5299 } 5300 5301 LLVM_DEBUG({ 5302 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg 5303 << " cannot be privatized in the context of its parent (" 5304 << Arg->getParent()->getName() 5305 << ")\n[AAPrivatizablePtr] because it is an argument in a " 5306 "direct call of (" 5307 << ACS.getCallSite().getCalledFunction()->getName() 5308 << ").\n[AAPrivatizablePtr] for which the argument " 5309 "privatization is not compatible.\n"; 5310 }); 5311 return false; 5312 }; 5313 5314 // Helper to check if the associated argument is used at the given abstract 5315 // call site in a way that is incompatible with the privatization assumed 5316 // here. 5317 auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) { 5318 if (ACS.isDirectCall()) 5319 return IsCompatiblePrivArgOfCallback(ACS.getCallSite()); 5320 if (ACS.isCallbackCall()) 5321 return IsCompatiblePrivArgOfDirectCS(ACS); 5322 return false; 5323 }; 5324 5325 bool AllCallSitesKnown; 5326 if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true, 5327 AllCallSitesKnown)) 5328 return indicatePessimisticFixpoint(); 5329 5330 return ChangeStatus::UNCHANGED; 5331 } 5332 5333 /// Given a type to private \p PrivType, collect the constituates (which are 5334 /// used) in \p ReplacementTypes. 5335 static void 5336 identifyReplacementTypes(Type *PrivType, 5337 SmallVectorImpl<Type *> &ReplacementTypes) { 5338 // TODO: For now we expand the privatization type to the fullest which can 5339 // lead to dead arguments that need to be removed later. 5340 assert(PrivType && "Expected privatizable type!"); 5341 5342 // Traverse the type, extract constituate types on the outermost level. 5343 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { 5344 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) 5345 ReplacementTypes.push_back(PrivStructType->getElementType(u)); 5346 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { 5347 ReplacementTypes.append(PrivArrayType->getNumElements(), 5348 PrivArrayType->getElementType()); 5349 } else { 5350 ReplacementTypes.push_back(PrivType); 5351 } 5352 } 5353 5354 /// Initialize \p Base according to the type \p PrivType at position \p IP. 5355 /// The values needed are taken from the arguments of \p F starting at 5356 /// position \p ArgNo. 5357 static void createInitialization(Type *PrivType, Value &Base, Function &F, 5358 unsigned ArgNo, Instruction &IP) { 5359 assert(PrivType && "Expected privatizable type!"); 5360 5361 IRBuilder<NoFolder> IRB(&IP); 5362 const DataLayout &DL = F.getParent()->getDataLayout(); 5363 5364 // Traverse the type, build GEPs and stores. 5365 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { 5366 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType); 5367 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) { 5368 Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo(); 5369 Value *Ptr = constructPointer( 5370 PointeeTy, &Base, PrivStructLayout->getElementOffset(u), IRB, DL); 5371 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP); 5372 } 5373 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { 5374 Type *PointeePtrTy = PrivArrayType->getElementType()->getPointerTo(); 5375 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeePtrTy); 5376 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) { 5377 Value *Ptr = 5378 constructPointer(PointeePtrTy, &Base, u * PointeeTySize, IRB, DL); 5379 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP); 5380 } 5381 } else { 5382 new StoreInst(F.getArg(ArgNo), &Base, &IP); 5383 } 5384 } 5385 5386 /// Extract values from \p Base according to the type \p PrivType at the 5387 /// call position \p ACS. The values are appended to \p ReplacementValues. 5388 void createReplacementValues(Type *PrivType, AbstractCallSite ACS, 5389 Value *Base, 5390 SmallVectorImpl<Value *> &ReplacementValues) { 5391 assert(Base && "Expected base value!"); 5392 assert(PrivType && "Expected privatizable type!"); 5393 Instruction *IP = ACS.getInstruction(); 5394 5395 IRBuilder<NoFolder> IRB(IP); 5396 const DataLayout &DL = IP->getModule()->getDataLayout(); 5397 5398 if (Base->getType()->getPointerElementType() != PrivType) 5399 Base = BitCastInst::CreateBitOrPointerCast(Base, PrivType->getPointerTo(), 5400 "", ACS.getInstruction()); 5401 5402 // TODO: Improve the alignment of the loads. 5403 // Traverse the type, build GEPs and loads. 5404 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { 5405 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType); 5406 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) { 5407 Type *PointeeTy = PrivStructType->getElementType(u); 5408 Value *Ptr = 5409 constructPointer(PointeeTy->getPointerTo(), Base, 5410 PrivStructLayout->getElementOffset(u), IRB, DL); 5411 LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP); 5412 L->setAlignment(MaybeAlign(1)); 5413 ReplacementValues.push_back(L); 5414 } 5415 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { 5416 Type *PointeeTy = PrivArrayType->getElementType(); 5417 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy); 5418 Type *PointeePtrTy = PointeeTy->getPointerTo(); 5419 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) { 5420 Value *Ptr = 5421 constructPointer(PointeePtrTy, Base, u * PointeeTySize, IRB, DL); 5422 LoadInst *L = new LoadInst(PointeePtrTy, Ptr, "", IP); 5423 L->setAlignment(MaybeAlign(1)); 5424 ReplacementValues.push_back(L); 5425 } 5426 } else { 5427 LoadInst *L = new LoadInst(PrivType, Base, "", IP); 5428 L->setAlignment(MaybeAlign(1)); 5429 ReplacementValues.push_back(L); 5430 } 5431 } 5432 5433 /// See AbstractAttribute::manifest(...) 5434 ChangeStatus manifest(Attributor &A) override { 5435 if (!PrivatizableType.hasValue()) 5436 return ChangeStatus::UNCHANGED; 5437 assert(PrivatizableType.getValue() && "Expected privatizable type!"); 5438 5439 // Collect all tail calls in the function as we cannot allow new allocas to 5440 // escape into tail recursion. 5441 // TODO: Be smarter about new allocas escaping into tail calls. 5442 SmallVector<CallInst *, 16> TailCalls; 5443 if (!A.checkForAllInstructions( 5444 [&](Instruction &I) { 5445 CallInst &CI = cast<CallInst>(I); 5446 if (CI.isTailCall()) 5447 TailCalls.push_back(&CI); 5448 return true; 5449 }, 5450 *this, {Instruction::Call})) 5451 return ChangeStatus::UNCHANGED; 5452 5453 Argument *Arg = getAssociatedArgument(); 5454 5455 // Callback to repair the associated function. A new alloca is placed at the 5456 // beginning and initialized with the values passed through arguments. The 5457 // new alloca replaces the use of the old pointer argument. 5458 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB = 5459 [=](const Attributor::ArgumentReplacementInfo &ARI, 5460 Function &ReplacementFn, Function::arg_iterator ArgIt) { 5461 BasicBlock &EntryBB = ReplacementFn.getEntryBlock(); 5462 Instruction *IP = &*EntryBB.getFirstInsertionPt(); 5463 auto *AI = new AllocaInst(PrivatizableType.getValue(), 0, 5464 Arg->getName() + ".priv", IP); 5465 createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn, 5466 ArgIt->getArgNo(), *IP); 5467 Arg->replaceAllUsesWith(AI); 5468 5469 for (CallInst *CI : TailCalls) 5470 CI->setTailCall(false); 5471 }; 5472 5473 // Callback to repair a call site of the associated function. The elements 5474 // of the privatizable type are loaded prior to the call and passed to the 5475 // new function version. 5476 Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB = 5477 [=](const Attributor::ArgumentReplacementInfo &ARI, 5478 AbstractCallSite ACS, SmallVectorImpl<Value *> &NewArgOperands) { 5479 createReplacementValues( 5480 PrivatizableType.getValue(), ACS, 5481 ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()), 5482 NewArgOperands); 5483 }; 5484 5485 // Collect the types that will replace the privatizable type in the function 5486 // signature. 5487 SmallVector<Type *, 16> ReplacementTypes; 5488 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes); 5489 5490 // Register a rewrite of the argument. 5491 if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes, 5492 std::move(FnRepairCB), 5493 std::move(ACSRepairCB))) 5494 return ChangeStatus::CHANGED; 5495 return ChangeStatus::UNCHANGED; 5496 } 5497 5498 /// See AbstractAttribute::trackStatistics() 5499 void trackStatistics() const override { 5500 STATS_DECLTRACK_ARG_ATTR(privatizable_ptr); 5501 } 5502 }; 5503 5504 struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl { 5505 AAPrivatizablePtrFloating(const IRPosition &IRP) 5506 : AAPrivatizablePtrImpl(IRP) {} 5507 5508 /// See AbstractAttribute::initialize(...). 5509 virtual void initialize(Attributor &A) override { 5510 // TODO: We can privatize more than arguments. 5511 indicatePessimisticFixpoint(); 5512 } 5513 5514 ChangeStatus updateImpl(Attributor &A) override { 5515 llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::" 5516 "updateImpl will not be called"); 5517 } 5518 5519 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...) 5520 Optional<Type *> identifyPrivatizableType(Attributor &A) override { 5521 Value *Obj = 5522 GetUnderlyingObject(&getAssociatedValue(), A.getInfoCache().getDL()); 5523 if (!Obj) { 5524 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n"); 5525 return nullptr; 5526 } 5527 5528 if (auto *AI = dyn_cast<AllocaInst>(Obj)) 5529 if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) 5530 if (CI->isOne()) 5531 return Obj->getType()->getPointerElementType(); 5532 if (auto *Arg = dyn_cast<Argument>(Obj)) { 5533 auto &PrivArgAA = 5534 A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(*Arg)); 5535 if (PrivArgAA.isAssumedPrivatizablePtr()) 5536 return Obj->getType()->getPointerElementType(); 5537 } 5538 5539 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid " 5540 "alloca nor privatizable argument: " 5541 << *Obj << "!\n"); 5542 return nullptr; 5543 } 5544 5545 /// See AbstractAttribute::trackStatistics() 5546 void trackStatistics() const override { 5547 STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr); 5548 } 5549 }; 5550 5551 struct AAPrivatizablePtrCallSiteArgument final 5552 : public AAPrivatizablePtrFloating { 5553 AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP) 5554 : AAPrivatizablePtrFloating(IRP) {} 5555 5556 /// See AbstractAttribute::initialize(...). 5557 void initialize(Attributor &A) override { 5558 if (getIRPosition().hasAttr(Attribute::ByVal)) 5559 indicateOptimisticFixpoint(); 5560 } 5561 5562 /// See AbstractAttribute::updateImpl(...). 5563 ChangeStatus updateImpl(Attributor &A) override { 5564 PrivatizableType = identifyPrivatizableType(A); 5565 if (!PrivatizableType.hasValue()) 5566 return ChangeStatus::UNCHANGED; 5567 if (!PrivatizableType.getValue()) 5568 return indicatePessimisticFixpoint(); 5569 5570 const IRPosition &IRP = getIRPosition(); 5571 auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); 5572 if (!NoCaptureAA.isAssumedNoCapture()) { 5573 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n"); 5574 return indicatePessimisticFixpoint(); 5575 } 5576 5577 auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP); 5578 if (!NoAliasAA.isAssumedNoAlias()) { 5579 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n"); 5580 return indicatePessimisticFixpoint(); 5581 } 5582 5583 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, IRP); 5584 if (!MemBehaviorAA.isAssumedReadOnly()) { 5585 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n"); 5586 return indicatePessimisticFixpoint(); 5587 } 5588 5589 return ChangeStatus::UNCHANGED; 5590 } 5591 5592 /// See AbstractAttribute::trackStatistics() 5593 void trackStatistics() const override { 5594 STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr); 5595 } 5596 }; 5597 5598 struct AAPrivatizablePtrCallSiteReturned final 5599 : public AAPrivatizablePtrFloating { 5600 AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP) 5601 : AAPrivatizablePtrFloating(IRP) {} 5602 5603 /// See AbstractAttribute::initialize(...). 5604 void initialize(Attributor &A) override { 5605 // TODO: We can privatize more than arguments. 5606 indicatePessimisticFixpoint(); 5607 } 5608 5609 /// See AbstractAttribute::trackStatistics() 5610 void trackStatistics() const override { 5611 STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr); 5612 } 5613 }; 5614 5615 struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating { 5616 AAPrivatizablePtrReturned(const IRPosition &IRP) 5617 : AAPrivatizablePtrFloating(IRP) {} 5618 5619 /// See AbstractAttribute::initialize(...). 5620 void initialize(Attributor &A) override { 5621 // TODO: We can privatize more than arguments. 5622 indicatePessimisticFixpoint(); 5623 } 5624 5625 /// See AbstractAttribute::trackStatistics() 5626 void trackStatistics() const override { 5627 STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr); 5628 } 5629 }; 5630 5631 /// -------------------- Memory Behavior Attributes ---------------------------- 5632 /// Includes read-none, read-only, and write-only. 5633 /// ---------------------------------------------------------------------------- 5634 struct AAMemoryBehaviorImpl : public AAMemoryBehavior { 5635 AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {} 5636 5637 /// See AbstractAttribute::initialize(...). 5638 void initialize(Attributor &A) override { 5639 intersectAssumedBits(BEST_STATE); 5640 getKnownStateFromValue(getIRPosition(), getState()); 5641 IRAttribute::initialize(A); 5642 } 5643 5644 /// Return the memory behavior information encoded in the IR for \p IRP. 5645 static void getKnownStateFromValue(const IRPosition &IRP, 5646 BitIntegerState &State, 5647 bool IgnoreSubsumingPositions = false) { 5648 SmallVector<Attribute, 2> Attrs; 5649 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); 5650 for (const Attribute &Attr : Attrs) { 5651 switch (Attr.getKindAsEnum()) { 5652 case Attribute::ReadNone: 5653 State.addKnownBits(NO_ACCESSES); 5654 break; 5655 case Attribute::ReadOnly: 5656 State.addKnownBits(NO_WRITES); 5657 break; 5658 case Attribute::WriteOnly: 5659 State.addKnownBits(NO_READS); 5660 break; 5661 default: 5662 llvm_unreachable("Unexpected attribute!"); 5663 } 5664 } 5665 5666 if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) { 5667 if (!I->mayReadFromMemory()) 5668 State.addKnownBits(NO_READS); 5669 if (!I->mayWriteToMemory()) 5670 State.addKnownBits(NO_WRITES); 5671 } 5672 } 5673 5674 /// See AbstractAttribute::getDeducedAttributes(...). 5675 void getDeducedAttributes(LLVMContext &Ctx, 5676 SmallVectorImpl<Attribute> &Attrs) const override { 5677 assert(Attrs.size() == 0); 5678 if (isAssumedReadNone()) 5679 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); 5680 else if (isAssumedReadOnly()) 5681 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly)); 5682 else if (isAssumedWriteOnly()) 5683 Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly)); 5684 assert(Attrs.size() <= 1); 5685 } 5686 5687 /// See AbstractAttribute::manifest(...). 5688 ChangeStatus manifest(Attributor &A) override { 5689 if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true)) 5690 return ChangeStatus::UNCHANGED; 5691 5692 const IRPosition &IRP = getIRPosition(); 5693 5694 // Check if we would improve the existing attributes first. 5695 SmallVector<Attribute, 4> DeducedAttrs; 5696 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); 5697 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { 5698 return IRP.hasAttr(Attr.getKindAsEnum(), 5699 /* IgnoreSubsumingPositions */ true); 5700 })) 5701 return ChangeStatus::UNCHANGED; 5702 5703 // Clear existing attributes. 5704 IRP.removeAttrs(AttrKinds); 5705 5706 // Use the generic manifest method. 5707 return IRAttribute::manifest(A); 5708 } 5709 5710 /// See AbstractState::getAsStr(). 5711 const std::string getAsStr() const override { 5712 if (isAssumedReadNone()) 5713 return "readnone"; 5714 if (isAssumedReadOnly()) 5715 return "readonly"; 5716 if (isAssumedWriteOnly()) 5717 return "writeonly"; 5718 return "may-read/write"; 5719 } 5720 5721 /// The set of IR attributes AAMemoryBehavior deals with. 5722 static const Attribute::AttrKind AttrKinds[3]; 5723 }; 5724 5725 const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = { 5726 Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly}; 5727 5728 /// Memory behavior attribute for a floating value. 5729 struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl { 5730 AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} 5731 5732 /// See AbstractAttribute::initialize(...). 5733 void initialize(Attributor &A) override { 5734 AAMemoryBehaviorImpl::initialize(A); 5735 // Initialize the use vector with all direct uses of the associated value. 5736 for (const Use &U : getAssociatedValue().uses()) 5737 Uses.insert(&U); 5738 } 5739 5740 /// See AbstractAttribute::updateImpl(...). 5741 ChangeStatus updateImpl(Attributor &A) override; 5742 5743 /// See AbstractAttribute::trackStatistics() 5744 void trackStatistics() const override { 5745 if (isAssumedReadNone()) 5746 STATS_DECLTRACK_FLOATING_ATTR(readnone) 5747 else if (isAssumedReadOnly()) 5748 STATS_DECLTRACK_FLOATING_ATTR(readonly) 5749 else if (isAssumedWriteOnly()) 5750 STATS_DECLTRACK_FLOATING_ATTR(writeonly) 5751 } 5752 5753 private: 5754 /// Return true if users of \p UserI might access the underlying 5755 /// variable/location described by \p U and should therefore be analyzed. 5756 bool followUsersOfUseIn(Attributor &A, const Use *U, 5757 const Instruction *UserI); 5758 5759 /// Update the state according to the effect of use \p U in \p UserI. 5760 void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI); 5761 5762 protected: 5763 /// Container for (transitive) uses of the associated argument. 5764 SetVector<const Use *> Uses; 5765 }; 5766 5767 /// Memory behavior attribute for function argument. 5768 struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating { 5769 AAMemoryBehaviorArgument(const IRPosition &IRP) 5770 : AAMemoryBehaviorFloating(IRP) {} 5771 5772 /// See AbstractAttribute::initialize(...). 5773 void initialize(Attributor &A) override { 5774 intersectAssumedBits(BEST_STATE); 5775 const IRPosition &IRP = getIRPosition(); 5776 // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we 5777 // can query it when we use has/getAttr. That would allow us to reuse the 5778 // initialize of the base class here. 5779 bool HasByVal = 5780 IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true); 5781 getKnownStateFromValue(IRP, getState(), 5782 /* IgnoreSubsumingPositions */ HasByVal); 5783 5784 // Initialize the use vector with all direct uses of the associated value. 5785 Argument *Arg = getAssociatedArgument(); 5786 if (!Arg || !Arg->getParent()->hasExactDefinition()) { 5787 indicatePessimisticFixpoint(); 5788 } else { 5789 // Initialize the use vector with all direct uses of the associated value. 5790 for (const Use &U : Arg->uses()) 5791 Uses.insert(&U); 5792 } 5793 } 5794 5795 ChangeStatus manifest(Attributor &A) override { 5796 // TODO: Pointer arguments are not supported on vectors of pointers yet. 5797 if (!getAssociatedValue().getType()->isPointerTy()) 5798 return ChangeStatus::UNCHANGED; 5799 5800 // TODO: From readattrs.ll: "inalloca parameters are always 5801 // considered written" 5802 if (hasAttr({Attribute::InAlloca})) { 5803 removeKnownBits(NO_WRITES); 5804 removeAssumedBits(NO_WRITES); 5805 } 5806 return AAMemoryBehaviorFloating::manifest(A); 5807 } 5808 5809 /// See AbstractAttribute::trackStatistics() 5810 void trackStatistics() const override { 5811 if (isAssumedReadNone()) 5812 STATS_DECLTRACK_ARG_ATTR(readnone) 5813 else if (isAssumedReadOnly()) 5814 STATS_DECLTRACK_ARG_ATTR(readonly) 5815 else if (isAssumedWriteOnly()) 5816 STATS_DECLTRACK_ARG_ATTR(writeonly) 5817 } 5818 }; 5819 5820 struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument { 5821 AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP) 5822 : AAMemoryBehaviorArgument(IRP) {} 5823 5824 /// See AbstractAttribute::initialize(...). 5825 void initialize(Attributor &A) override { 5826 if (Argument *Arg = getAssociatedArgument()) { 5827 if (Arg->hasByValAttr()) { 5828 addKnownBits(NO_WRITES); 5829 removeKnownBits(NO_READS); 5830 removeAssumedBits(NO_READS); 5831 } 5832 } else { 5833 } 5834 AAMemoryBehaviorArgument::initialize(A); 5835 } 5836 5837 /// See AbstractAttribute::updateImpl(...). 5838 ChangeStatus updateImpl(Attributor &A) override { 5839 // TODO: Once we have call site specific value information we can provide 5840 // call site specific liveness liveness information and then it makes 5841 // sense to specialize attributes for call sites arguments instead of 5842 // redirecting requests to the callee argument. 5843 Argument *Arg = getAssociatedArgument(); 5844 const IRPosition &ArgPos = IRPosition::argument(*Arg); 5845 auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); 5846 return clampStateAndIndicateChange( 5847 getState(), 5848 static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState())); 5849 } 5850 5851 /// See AbstractAttribute::trackStatistics() 5852 void trackStatistics() const override { 5853 if (isAssumedReadNone()) 5854 STATS_DECLTRACK_CSARG_ATTR(readnone) 5855 else if (isAssumedReadOnly()) 5856 STATS_DECLTRACK_CSARG_ATTR(readonly) 5857 else if (isAssumedWriteOnly()) 5858 STATS_DECLTRACK_CSARG_ATTR(writeonly) 5859 } 5860 }; 5861 5862 /// Memory behavior attribute for a call site return position. 5863 struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating { 5864 AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP) 5865 : AAMemoryBehaviorFloating(IRP) {} 5866 5867 /// See AbstractAttribute::manifest(...). 5868 ChangeStatus manifest(Attributor &A) override { 5869 // We do not annotate returned values. 5870 return ChangeStatus::UNCHANGED; 5871 } 5872 5873 /// See AbstractAttribute::trackStatistics() 5874 void trackStatistics() const override {} 5875 }; 5876 5877 /// An AA to represent the memory behavior function attributes. 5878 struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl { 5879 AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} 5880 5881 /// See AbstractAttribute::updateImpl(Attributor &A). 5882 virtual ChangeStatus updateImpl(Attributor &A) override; 5883 5884 /// See AbstractAttribute::manifest(...). 5885 ChangeStatus manifest(Attributor &A) override { 5886 Function &F = cast<Function>(getAnchorValue()); 5887 if (isAssumedReadNone()) { 5888 F.removeFnAttr(Attribute::ArgMemOnly); 5889 F.removeFnAttr(Attribute::InaccessibleMemOnly); 5890 F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly); 5891 } 5892 return AAMemoryBehaviorImpl::manifest(A); 5893 } 5894 5895 /// See AbstractAttribute::trackStatistics() 5896 void trackStatistics() const override { 5897 if (isAssumedReadNone()) 5898 STATS_DECLTRACK_FN_ATTR(readnone) 5899 else if (isAssumedReadOnly()) 5900 STATS_DECLTRACK_FN_ATTR(readonly) 5901 else if (isAssumedWriteOnly()) 5902 STATS_DECLTRACK_FN_ATTR(writeonly) 5903 } 5904 }; 5905 5906 /// AAMemoryBehavior attribute for call sites. 5907 struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl { 5908 AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} 5909 5910 /// See AbstractAttribute::initialize(...). 5911 void initialize(Attributor &A) override { 5912 AAMemoryBehaviorImpl::initialize(A); 5913 Function *F = getAssociatedFunction(); 5914 if (!F || !F->hasExactDefinition()) 5915 indicatePessimisticFixpoint(); 5916 } 5917 5918 /// See AbstractAttribute::updateImpl(...). 5919 ChangeStatus updateImpl(Attributor &A) override { 5920 // TODO: Once we have call site specific value information we can provide 5921 // call site specific liveness liveness information and then it makes 5922 // sense to specialize attributes for call sites arguments instead of 5923 // redirecting requests to the callee argument. 5924 Function *F = getAssociatedFunction(); 5925 const IRPosition &FnPos = IRPosition::function(*F); 5926 auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); 5927 return clampStateAndIndicateChange( 5928 getState(), 5929 static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState())); 5930 } 5931 5932 /// See AbstractAttribute::trackStatistics() 5933 void trackStatistics() const override { 5934 if (isAssumedReadNone()) 5935 STATS_DECLTRACK_CS_ATTR(readnone) 5936 else if (isAssumedReadOnly()) 5937 STATS_DECLTRACK_CS_ATTR(readonly) 5938 else if (isAssumedWriteOnly()) 5939 STATS_DECLTRACK_CS_ATTR(writeonly) 5940 } 5941 }; 5942 5943 ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) { 5944 5945 // The current assumed state used to determine a change. 5946 auto AssumedState = getAssumed(); 5947 5948 auto CheckRWInst = [&](Instruction &I) { 5949 // If the instruction has an own memory behavior state, use it to restrict 5950 // the local state. No further analysis is required as the other memory 5951 // state is as optimistic as it gets. 5952 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { 5953 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( 5954 *this, IRPosition::callsite_function(ICS)); 5955 intersectAssumedBits(MemBehaviorAA.getAssumed()); 5956 return !isAtFixpoint(); 5957 } 5958 5959 // Remove access kind modifiers if necessary. 5960 if (I.mayReadFromMemory()) 5961 removeAssumedBits(NO_READS); 5962 if (I.mayWriteToMemory()) 5963 removeAssumedBits(NO_WRITES); 5964 return !isAtFixpoint(); 5965 }; 5966 5967 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this)) 5968 return indicatePessimisticFixpoint(); 5969 5970 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED 5971 : ChangeStatus::UNCHANGED; 5972 } 5973 5974 ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) { 5975 5976 const IRPosition &IRP = getIRPosition(); 5977 const IRPosition &FnPos = IRPosition::function_scope(IRP); 5978 AAMemoryBehavior::StateType &S = getState(); 5979 5980 // First, check the function scope. We take the known information and we avoid 5981 // work if the assumed information implies the current assumed information for 5982 // this attribute. This is a valid for all but byval arguments. 5983 Argument *Arg = IRP.getAssociatedArgument(); 5984 AAMemoryBehavior::base_t FnMemAssumedState = 5985 AAMemoryBehavior::StateType::getWorstState(); 5986 if (!Arg || !Arg->hasByValAttr()) { 5987 const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>( 5988 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL); 5989 FnMemAssumedState = FnMemAA.getAssumed(); 5990 S.addKnownBits(FnMemAA.getKnown()); 5991 if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed()) 5992 return ChangeStatus::UNCHANGED; 5993 } 5994 5995 // Make sure the value is not captured (except through "return"), if 5996 // it is, any information derived would be irrelevant anyway as we cannot 5997 // check the potential aliases introduced by the capture. However, no need 5998 // to fall back to anythign less optimistic than the function state. 5999 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>( 6000 *this, IRP, /* TrackDependence */ true, DepClassTy::OPTIONAL); 6001 if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { 6002 S.intersectAssumedBits(FnMemAssumedState); 6003 return ChangeStatus::CHANGED; 6004 } 6005 6006 // The current assumed state used to determine a change. 6007 auto AssumedState = S.getAssumed(); 6008 6009 // Liveness information to exclude dead users. 6010 // TODO: Take the FnPos once we have call site specific liveness information. 6011 const auto &LivenessAA = A.getAAFor<AAIsDead>( 6012 *this, IRPosition::function(*IRP.getAssociatedFunction()), 6013 /* TrackDependence */ false); 6014 6015 // Visit and expand uses until all are analyzed or a fixpoint is reached. 6016 for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) { 6017 const Use *U = Uses[i]; 6018 Instruction *UserI = cast<Instruction>(U->getUser()); 6019 LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI 6020 << " [Dead: " << (A.isAssumedDead(*U, this, &LivenessAA)) 6021 << "]\n"); 6022 if (A.isAssumedDead(*U, this, &LivenessAA)) 6023 continue; 6024 6025 // Check if the users of UserI should also be visited. 6026 if (followUsersOfUseIn(A, U, UserI)) 6027 for (const Use &UserIUse : UserI->uses()) 6028 Uses.insert(&UserIUse); 6029 6030 // If UserI might touch memory we analyze the use in detail. 6031 if (UserI->mayReadOrWriteMemory()) 6032 analyzeUseIn(A, U, UserI); 6033 } 6034 6035 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED 6036 : ChangeStatus::UNCHANGED; 6037 } 6038 6039 bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U, 6040 const Instruction *UserI) { 6041 // The loaded value is unrelated to the pointer argument, no need to 6042 // follow the users of the load. 6043 if (isa<LoadInst>(UserI)) 6044 return false; 6045 6046 // By default we follow all uses assuming UserI might leak information on U, 6047 // we have special handling for call sites operands though. 6048 ImmutableCallSite ICS(UserI); 6049 if (!ICS || !ICS.isArgOperand(U)) 6050 return true; 6051 6052 // If the use is a call argument known not to be captured, the users of 6053 // the call do not need to be visited because they have to be unrelated to 6054 // the input. Note that this check is not trivial even though we disallow 6055 // general capturing of the underlying argument. The reason is that the 6056 // call might the argument "through return", which we allow and for which we 6057 // need to check call users. 6058 if (U->get()->getType()->isPointerTy()) { 6059 unsigned ArgNo = ICS.getArgumentNo(U); 6060 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>( 6061 *this, IRPosition::callsite_argument(ICS, ArgNo), 6062 /* TrackDependence */ true, DepClassTy::OPTIONAL); 6063 return !ArgNoCaptureAA.isAssumedNoCapture(); 6064 } 6065 6066 return true; 6067 } 6068 6069 void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U, 6070 const Instruction *UserI) { 6071 assert(UserI->mayReadOrWriteMemory()); 6072 6073 switch (UserI->getOpcode()) { 6074 default: 6075 // TODO: Handle all atomics and other side-effect operations we know of. 6076 break; 6077 case Instruction::Load: 6078 // Loads cause the NO_READS property to disappear. 6079 removeAssumedBits(NO_READS); 6080 return; 6081 6082 case Instruction::Store: 6083 // Stores cause the NO_WRITES property to disappear if the use is the 6084 // pointer operand. Note that we do assume that capturing was taken care of 6085 // somewhere else. 6086 if (cast<StoreInst>(UserI)->getPointerOperand() == U->get()) 6087 removeAssumedBits(NO_WRITES); 6088 return; 6089 6090 case Instruction::Call: 6091 case Instruction::CallBr: 6092 case Instruction::Invoke: { 6093 // For call sites we look at the argument memory behavior attribute (this 6094 // could be recursive!) in order to restrict our own state. 6095 ImmutableCallSite ICS(UserI); 6096 6097 // Give up on operand bundles. 6098 if (ICS.isBundleOperand(U)) { 6099 indicatePessimisticFixpoint(); 6100 return; 6101 } 6102 6103 // Calling a function does read the function pointer, maybe write it if the 6104 // function is self-modifying. 6105 if (ICS.isCallee(U)) { 6106 removeAssumedBits(NO_READS); 6107 break; 6108 } 6109 6110 // Adjust the possible access behavior based on the information on the 6111 // argument. 6112 IRPosition Pos; 6113 if (U->get()->getType()->isPointerTy()) 6114 Pos = IRPosition::callsite_argument(ICS, ICS.getArgumentNo(U)); 6115 else 6116 Pos = IRPosition::callsite_function(ICS); 6117 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( 6118 *this, Pos, 6119 /* TrackDependence */ true, DepClassTy::OPTIONAL); 6120 // "assumed" has at most the same bits as the MemBehaviorAA assumed 6121 // and at least "known". 6122 intersectAssumedBits(MemBehaviorAA.getAssumed()); 6123 return; 6124 } 6125 }; 6126 6127 // Generally, look at the "may-properties" and adjust the assumed state if we 6128 // did not trigger special handling before. 6129 if (UserI->mayReadFromMemory()) 6130 removeAssumedBits(NO_READS); 6131 if (UserI->mayWriteToMemory()) 6132 removeAssumedBits(NO_WRITES); 6133 } 6134 6135 } // namespace 6136 6137 /// -------------------- Memory Locations Attributes --------------------------- 6138 /// Includes read-none, argmemonly, inaccessiblememonly, 6139 /// inaccessiblememorargmemonly 6140 /// ---------------------------------------------------------------------------- 6141 6142 std::string AAMemoryLocation::getMemoryLocationsAsStr( 6143 AAMemoryLocation::MemoryLocationsKind MLK) { 6144 if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS)) 6145 return "all memory"; 6146 if (MLK == AAMemoryLocation::NO_LOCATIONS) 6147 return "no memory"; 6148 std::string S = "memory:"; 6149 if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM)) 6150 S += "stack,"; 6151 if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM)) 6152 S += "constant,"; 6153 if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM)) 6154 S += "internal global,"; 6155 if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM)) 6156 S += "external global,"; 6157 if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM)) 6158 S += "argument,"; 6159 if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM)) 6160 S += "inaccessible,"; 6161 if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM)) 6162 S += "malloced,"; 6163 if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM)) 6164 S += "unknown,"; 6165 S.pop_back(); 6166 return S; 6167 } 6168 6169 namespace { 6170 6171 struct AAMemoryLocationImpl : public AAMemoryLocation { 6172 6173 AAMemoryLocationImpl(const IRPosition &IRP) : AAMemoryLocation(IRP) {} 6174 6175 /// See AbstractAttribute::initialize(...). 6176 void initialize(Attributor &A) override { 6177 intersectAssumedBits(BEST_STATE); 6178 getKnownStateFromValue(getIRPosition(), getState()); 6179 IRAttribute::initialize(A); 6180 } 6181 6182 /// Return the memory behavior information encoded in the IR for \p IRP. 6183 static void getKnownStateFromValue(const IRPosition &IRP, 6184 BitIntegerState &State, 6185 bool IgnoreSubsumingPositions = false) { 6186 SmallVector<Attribute, 2> Attrs; 6187 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); 6188 for (const Attribute &Attr : Attrs) { 6189 switch (Attr.getKindAsEnum()) { 6190 case Attribute::ReadNone: 6191 State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM); 6192 break; 6193 case Attribute::InaccessibleMemOnly: 6194 State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true)); 6195 break; 6196 case Attribute::ArgMemOnly: 6197 State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true)); 6198 break; 6199 case Attribute::InaccessibleMemOrArgMemOnly: 6200 State.addKnownBits( 6201 inverseLocation(NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true)); 6202 break; 6203 default: 6204 llvm_unreachable("Unexpected attribute!"); 6205 } 6206 } 6207 } 6208 6209 /// See AbstractAttribute::getDeducedAttributes(...). 6210 void getDeducedAttributes(LLVMContext &Ctx, 6211 SmallVectorImpl<Attribute> &Attrs) const override { 6212 assert(Attrs.size() == 0); 6213 if (isAssumedReadNone()) { 6214 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); 6215 } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) { 6216 if (isAssumedInaccessibleMemOnly()) 6217 Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly)); 6218 else if (isAssumedArgMemOnly()) 6219 Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly)); 6220 else if (isAssumedInaccessibleOrArgMemOnly()) 6221 Attrs.push_back( 6222 Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly)); 6223 } 6224 assert(Attrs.size() <= 1); 6225 } 6226 6227 /// See AbstractAttribute::manifest(...). 6228 ChangeStatus manifest(Attributor &A) override { 6229 const IRPosition &IRP = getIRPosition(); 6230 6231 // Check if we would improve the existing attributes first. 6232 SmallVector<Attribute, 4> DeducedAttrs; 6233 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); 6234 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { 6235 return IRP.hasAttr(Attr.getKindAsEnum(), 6236 /* IgnoreSubsumingPositions */ true); 6237 })) 6238 return ChangeStatus::UNCHANGED; 6239 6240 // Clear existing attributes. 6241 IRP.removeAttrs(AttrKinds); 6242 if (isAssumedReadNone()) 6243 IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds); 6244 6245 // Use the generic manifest method. 6246 return IRAttribute::manifest(A); 6247 } 6248 6249 /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...). 6250 bool checkForAllAccessesToMemoryKind( 6251 const function_ref<bool(const Instruction *, const Value *, AccessKind, 6252 MemoryLocationsKind)> &Pred, 6253 MemoryLocationsKind RequestedMLK) const override { 6254 if (!isValidState()) 6255 return false; 6256 6257 MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation(); 6258 if (AssumedMLK == NO_LOCATIONS) 6259 return true; 6260 6261 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) { 6262 if (CurMLK & RequestedMLK) 6263 continue; 6264 6265 const auto &Accesses = AccessKindAccessesMap.lookup(CurMLK); 6266 for (const AccessInfo &AI : Accesses) { 6267 if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK)) 6268 return false; 6269 } 6270 } 6271 6272 return true; 6273 } 6274 6275 ChangeStatus indicatePessimisticFixpoint() override { 6276 // If we give up and indicate a pessimistic fixpoint this instruction will 6277 // become an access for all potential access kinds: 6278 // TODO: Add pointers for argmemonly and globals to improve the results of 6279 // checkForAllAccessesToMemoryKind. 6280 bool Changed = false; 6281 MemoryLocationsKind KnownMLK = getKnown(); 6282 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()); 6283 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) 6284 if (!(CurMLK & KnownMLK)) 6285 updateStateAndAccessesMap(getState(), AccessKindAccessesMap, CurMLK, I, 6286 nullptr, Changed); 6287 return AAMemoryLocation::indicatePessimisticFixpoint(); 6288 } 6289 6290 protected: 6291 /// Helper struct to tie together an instruction that has a read or write 6292 /// effect with the pointer it accesses (if any). 6293 struct AccessInfo { 6294 6295 /// The instruction that caused the access. 6296 const Instruction *I; 6297 6298 /// The base pointer that is accessed, or null if unknown. 6299 const Value *Ptr; 6300 6301 /// The kind of access (read/write/read+write). 6302 AccessKind Kind; 6303 6304 bool operator==(const AccessInfo &RHS) const { 6305 return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind; 6306 } 6307 bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const { 6308 if (LHS.I != RHS.I) 6309 return LHS.I < RHS.I; 6310 if (LHS.Ptr != RHS.Ptr) 6311 return LHS.Ptr < RHS.Ptr; 6312 if (LHS.Kind != RHS.Kind) 6313 return LHS.Kind < RHS.Kind; 6314 return false; 6315 } 6316 }; 6317 6318 /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the 6319 /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind. 6320 using AccessKindAccessesMapTy = 6321 DenseMap<unsigned, SmallSet<AccessInfo, 8, AccessInfo>>; 6322 AccessKindAccessesMapTy AccessKindAccessesMap; 6323 6324 /// Return the kind(s) of location that may be accessed by \p V. 6325 AAMemoryLocation::MemoryLocationsKind 6326 categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed); 6327 6328 /// Update the state \p State and the AccessKindAccessesMap given that \p I is 6329 /// an access to a \p MLK memory location with the access pointer \p Ptr. 6330 static void updateStateAndAccessesMap(AAMemoryLocation::StateType &State, 6331 AccessKindAccessesMapTy &AccessMap, 6332 MemoryLocationsKind MLK, 6333 const Instruction *I, const Value *Ptr, 6334 bool &Changed) { 6335 // TODO: The kind should be determined at the call sites based on the 6336 // information we have there. 6337 AccessKind Kind = READ_WRITE; 6338 if (I) { 6339 Kind = I->mayReadFromMemory() ? READ : NONE; 6340 Kind = AccessKind(Kind | (I->mayWriteToMemory() ? WRITE : NONE)); 6341 } 6342 6343 assert(isPowerOf2_32(MLK) && "Expected a single location set!"); 6344 Changed |= AccessMap[MLK].insert(AccessInfo{I, Ptr, Kind}).second; 6345 State.removeAssumedBits(MLK); 6346 } 6347 6348 /// Determine the underlying locations kinds for \p Ptr, e.g., globals or 6349 /// arguments, and update the state and access map accordingly. 6350 void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr, 6351 AAMemoryLocation::StateType &State, bool &Changed); 6352 6353 /// The set of IR attributes AAMemoryLocation deals with. 6354 static const Attribute::AttrKind AttrKinds[4]; 6355 }; 6356 6357 const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = { 6358 Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly, 6359 Attribute::InaccessibleMemOrArgMemOnly}; 6360 6361 void AAMemoryLocationImpl::categorizePtrValue( 6362 Attributor &A, const Instruction &I, const Value &Ptr, 6363 AAMemoryLocation::StateType &State, bool &Changed) { 6364 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for " 6365 << Ptr << " [" 6366 << getMemoryLocationsAsStr(State.getAssumed()) << "]\n"); 6367 6368 auto StripGEPCB = [](Value *V) -> Value * { 6369 auto *GEP = dyn_cast<GEPOperator>(V); 6370 while (GEP) { 6371 V = GEP->getPointerOperand(); 6372 GEP = dyn_cast<GEPOperator>(V); 6373 } 6374 return V; 6375 }; 6376 6377 auto VisitValueCB = [&](Value &V, AAMemoryLocation::StateType &T, 6378 bool Stripped) -> bool { 6379 assert(!isa<GEPOperator>(V) && "GEPs should have been stripped."); 6380 if (isa<UndefValue>(V)) 6381 return true; 6382 if (auto *Arg = dyn_cast<Argument>(&V)) { 6383 if (Arg->hasByValAttr()) 6384 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_LOCAL_MEM, &I, 6385 &V, Changed); 6386 else 6387 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_ARGUMENT_MEM, &I, 6388 &V, Changed); 6389 return true; 6390 } 6391 if (auto *GV = dyn_cast<GlobalValue>(&V)) { 6392 if (GV->hasLocalLinkage()) 6393 updateStateAndAccessesMap(T, AccessKindAccessesMap, 6394 NO_GLOBAL_INTERNAL_MEM, &I, &V, Changed); 6395 else 6396 updateStateAndAccessesMap(T, AccessKindAccessesMap, 6397 NO_GLOBAL_EXTERNAL_MEM, &I, &V, Changed); 6398 return true; 6399 } 6400 if (isa<AllocaInst>(V)) { 6401 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_LOCAL_MEM, &I, &V, 6402 Changed); 6403 return true; 6404 } 6405 if (ImmutableCallSite ICS = ImmutableCallSite(&V)) { 6406 const auto &NoAliasAA = 6407 A.getAAFor<AANoAlias>(*this, IRPosition::callsite_returned(ICS)); 6408 if (NoAliasAA.isAssumedNoAlias()) { 6409 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_MALLOCED_MEM, &I, 6410 &V, Changed); 6411 return true; 6412 } 6413 } 6414 6415 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_UNKOWN_MEM, &I, &V, 6416 Changed); 6417 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value cannot be categorized: " 6418 << V << " -> " << getMemoryLocationsAsStr(T.getAssumed()) 6419 << "\n"); 6420 return true; 6421 }; 6422 6423 if (!genericValueTraversal<AAMemoryLocation, AAMemoryLocation::StateType>( 6424 A, IRPosition::value(Ptr), *this, State, VisitValueCB, 6425 /* MaxValues */ 32, StripGEPCB)) { 6426 LLVM_DEBUG( 6427 dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n"); 6428 updateStateAndAccessesMap(State, AccessKindAccessesMap, NO_UNKOWN_MEM, &I, 6429 nullptr, Changed); 6430 } else { 6431 LLVM_DEBUG( 6432 dbgs() 6433 << "[AAMemoryLocation] Accessed locations with pointer locations: " 6434 << getMemoryLocationsAsStr(State.getAssumed()) << "\n"); 6435 } 6436 } 6437 6438 AAMemoryLocation::MemoryLocationsKind 6439 AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I, 6440 bool &Changed) { 6441 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for " 6442 << I << "\n"); 6443 6444 AAMemoryLocation::StateType AccessedLocs; 6445 AccessedLocs.intersectAssumedBits(NO_LOCATIONS); 6446 6447 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { 6448 6449 // First check if we assume any memory is access is visible. 6450 const auto &ICSMemLocationAA = 6451 A.getAAFor<AAMemoryLocation>(*this, IRPosition::callsite_function(ICS)); 6452 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I 6453 << " [" << ICSMemLocationAA << "]\n"); 6454 6455 if (ICSMemLocationAA.isAssumedReadNone()) 6456 return NO_LOCATIONS; 6457 6458 if (ICSMemLocationAA.isAssumedInaccessibleMemOnly()) { 6459 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, 6460 NO_INACCESSIBLE_MEM, &I, nullptr, Changed); 6461 return AccessedLocs.getAssumed(); 6462 } 6463 6464 uint32_t ICSAssumedNotAccessedLocs = 6465 ICSMemLocationAA.getAssumedNotAccessedLocation(); 6466 6467 // Set the argmemonly and global bit as we handle them separately below. 6468 uint32_t ICSAssumedNotAccessedLocsNoArgMem = 6469 ICSAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM; 6470 6471 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) { 6472 if (ICSAssumedNotAccessedLocsNoArgMem & CurMLK) 6473 continue; 6474 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, CurMLK, &I, 6475 nullptr, Changed); 6476 } 6477 6478 // Now handle global memory if it might be accessed. 6479 bool HasGlobalAccesses = !(ICSAssumedNotAccessedLocs & NO_GLOBAL_MEM); 6480 if (HasGlobalAccesses) { 6481 auto AccessPred = [&](const Instruction *, const Value *Ptr, 6482 AccessKind Kind, MemoryLocationsKind MLK) { 6483 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, MLK, &I, 6484 Ptr, Changed); 6485 return true; 6486 }; 6487 if (!ICSMemLocationAA.checkForAllAccessesToMemoryKind( 6488 AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false))) 6489 return AccessedLocs.getWorstState(); 6490 } 6491 6492 LLVM_DEBUG( 6493 dbgs() << "[AAMemoryLocation] Accessed state before argument handling: " 6494 << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"); 6495 6496 // Now handle argument memory if it might be accessed. 6497 bool HasArgAccesses = !(ICSAssumedNotAccessedLocs & NO_ARGUMENT_MEM); 6498 if (HasArgAccesses) { 6499 for (unsigned ArgNo = 0, e = ICS.getNumArgOperands(); ArgNo < e; 6500 ++ArgNo) { 6501 6502 // Skip non-pointer arguments. 6503 const Value *ArgOp = ICS.getArgOperand(ArgNo); 6504 if (!ArgOp->getType()->isPtrOrPtrVectorTy()) 6505 continue; 6506 6507 // Skip readnone arguments. 6508 const IRPosition &ArgOpIRP = IRPosition::callsite_argument(ICS, ArgNo); 6509 const auto &ArgOpMemLocationAA = A.getAAFor<AAMemoryBehavior>( 6510 *this, ArgOpIRP, /* TrackDependence */ true, DepClassTy::OPTIONAL); 6511 6512 if (ArgOpMemLocationAA.isAssumedReadNone()) 6513 continue; 6514 6515 // Categorize potentially accessed pointer arguments as if there was an 6516 // access instruction with them as pointer. 6517 categorizePtrValue(A, I, *ArgOp, AccessedLocs, Changed); 6518 } 6519 } 6520 6521 LLVM_DEBUG( 6522 dbgs() << "[AAMemoryLocation] Accessed state after argument handling: " 6523 << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"); 6524 6525 return AccessedLocs.getAssumed(); 6526 } 6527 6528 if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) { 6529 LLVM_DEBUG( 6530 dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: " 6531 << I << " [" << *Ptr << "]\n"); 6532 categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed); 6533 return AccessedLocs.getAssumed(); 6534 } 6535 6536 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: " 6537 << I << "\n"); 6538 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, NO_UNKOWN_MEM, 6539 &I, nullptr, Changed); 6540 return AccessedLocs.getAssumed(); 6541 } 6542 6543 /// An AA to represent the memory behavior function attributes. 6544 struct AAMemoryLocationFunction final : public AAMemoryLocationImpl { 6545 AAMemoryLocationFunction(const IRPosition &IRP) : AAMemoryLocationImpl(IRP) {} 6546 6547 /// See AbstractAttribute::updateImpl(Attributor &A). 6548 virtual ChangeStatus updateImpl(Attributor &A) override { 6549 6550 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( 6551 *this, getIRPosition(), /* TrackDependence */ false); 6552 if (MemBehaviorAA.isAssumedReadNone()) { 6553 if (MemBehaviorAA.isKnownReadNone()) 6554 return indicateOptimisticFixpoint(); 6555 assert(isAssumedReadNone() && 6556 "AAMemoryLocation was not read-none but AAMemoryBehavior was!"); 6557 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); 6558 return ChangeStatus::UNCHANGED; 6559 } 6560 6561 // The current assumed state used to determine a change. 6562 auto AssumedState = getAssumed(); 6563 bool Changed = false; 6564 6565 auto CheckRWInst = [&](Instruction &I) { 6566 MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed); 6567 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I 6568 << ": " << getMemoryLocationsAsStr(MLK) << "\n"); 6569 removeAssumedBits(inverseLocation(MLK, false, false)); 6570 return true; 6571 }; 6572 6573 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this)) 6574 return indicatePessimisticFixpoint(); 6575 6576 Changed |= AssumedState != getAssumed(); 6577 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 6578 } 6579 6580 /// See AbstractAttribute::trackStatistics() 6581 void trackStatistics() const override { 6582 if (isAssumedReadNone()) 6583 STATS_DECLTRACK_FN_ATTR(readnone) 6584 else if (isAssumedArgMemOnly()) 6585 STATS_DECLTRACK_FN_ATTR(argmemonly) 6586 else if (isAssumedInaccessibleMemOnly()) 6587 STATS_DECLTRACK_FN_ATTR(inaccessiblememonly) 6588 else if (isAssumedInaccessibleOrArgMemOnly()) 6589 STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly) 6590 } 6591 }; 6592 6593 /// AAMemoryLocation attribute for call sites. 6594 struct AAMemoryLocationCallSite final : AAMemoryLocationImpl { 6595 AAMemoryLocationCallSite(const IRPosition &IRP) : AAMemoryLocationImpl(IRP) {} 6596 6597 /// See AbstractAttribute::initialize(...). 6598 void initialize(Attributor &A) override { 6599 AAMemoryLocationImpl::initialize(A); 6600 Function *F = getAssociatedFunction(); 6601 if (!F || !F->hasExactDefinition()) 6602 indicatePessimisticFixpoint(); 6603 } 6604 6605 /// See AbstractAttribute::updateImpl(...). 6606 ChangeStatus updateImpl(Attributor &A) override { 6607 // TODO: Once we have call site specific value information we can provide 6608 // call site specific liveness liveness information and then it makes 6609 // sense to specialize attributes for call sites arguments instead of 6610 // redirecting requests to the callee argument. 6611 Function *F = getAssociatedFunction(); 6612 const IRPosition &FnPos = IRPosition::function(*F); 6613 auto &FnAA = A.getAAFor<AAMemoryLocation>(*this, FnPos); 6614 bool Changed = false; 6615 auto AccessPred = [&](const Instruction *I, const Value *Ptr, 6616 AccessKind Kind, MemoryLocationsKind MLK) { 6617 updateStateAndAccessesMap(getState(), AccessKindAccessesMap, MLK, I, Ptr, 6618 Changed); 6619 return true; 6620 }; 6621 if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS)) 6622 return indicatePessimisticFixpoint(); 6623 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 6624 } 6625 6626 /// See AbstractAttribute::trackStatistics() 6627 void trackStatistics() const override { 6628 if (isAssumedReadNone()) 6629 STATS_DECLTRACK_CS_ATTR(readnone) 6630 } 6631 }; 6632 6633 /// ------------------ Value Constant Range Attribute ------------------------- 6634 6635 struct AAValueConstantRangeImpl : AAValueConstantRange { 6636 using StateType = IntegerRangeState; 6637 AAValueConstantRangeImpl(const IRPosition &IRP) : AAValueConstantRange(IRP) {} 6638 6639 /// See AbstractAttribute::getAsStr(). 6640 const std::string getAsStr() const override { 6641 std::string Str; 6642 llvm::raw_string_ostream OS(Str); 6643 OS << "range(" << getBitWidth() << ")<"; 6644 getKnown().print(OS); 6645 OS << " / "; 6646 getAssumed().print(OS); 6647 OS << ">"; 6648 return OS.str(); 6649 } 6650 6651 /// Helper function to get a SCEV expr for the associated value at program 6652 /// point \p I. 6653 const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const { 6654 if (!getAnchorScope()) 6655 return nullptr; 6656 6657 ScalarEvolution *SE = 6658 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>( 6659 *getAnchorScope()); 6660 6661 LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>( 6662 *getAnchorScope()); 6663 6664 if (!SE || !LI) 6665 return nullptr; 6666 6667 const SCEV *S = SE->getSCEV(&getAssociatedValue()); 6668 if (!I) 6669 return S; 6670 6671 return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent())); 6672 } 6673 6674 /// Helper function to get a range from SCEV for the associated value at 6675 /// program point \p I. 6676 ConstantRange getConstantRangeFromSCEV(Attributor &A, 6677 const Instruction *I = nullptr) const { 6678 if (!getAnchorScope()) 6679 return getWorstState(getBitWidth()); 6680 6681 ScalarEvolution *SE = 6682 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>( 6683 *getAnchorScope()); 6684 6685 const SCEV *S = getSCEV(A, I); 6686 if (!SE || !S) 6687 return getWorstState(getBitWidth()); 6688 6689 return SE->getUnsignedRange(S); 6690 } 6691 6692 /// Helper function to get a range from LVI for the associated value at 6693 /// program point \p I. 6694 ConstantRange 6695 getConstantRangeFromLVI(Attributor &A, 6696 const Instruction *CtxI = nullptr) const { 6697 if (!getAnchorScope()) 6698 return getWorstState(getBitWidth()); 6699 6700 LazyValueInfo *LVI = 6701 A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>( 6702 *getAnchorScope()); 6703 6704 if (!LVI || !CtxI) 6705 return getWorstState(getBitWidth()); 6706 return LVI->getConstantRange(&getAssociatedValue(), 6707 const_cast<BasicBlock *>(CtxI->getParent()), 6708 const_cast<Instruction *>(CtxI)); 6709 } 6710 6711 /// See AAValueConstantRange::getKnownConstantRange(..). 6712 ConstantRange 6713 getKnownConstantRange(Attributor &A, 6714 const Instruction *CtxI = nullptr) const override { 6715 if (!CtxI || CtxI == getCtxI()) 6716 return getKnown(); 6717 6718 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI); 6719 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI); 6720 return getKnown().intersectWith(SCEVR).intersectWith(LVIR); 6721 } 6722 6723 /// See AAValueConstantRange::getAssumedConstantRange(..). 6724 ConstantRange 6725 getAssumedConstantRange(Attributor &A, 6726 const Instruction *CtxI = nullptr) const override { 6727 // TODO: Make SCEV use Attributor assumption. 6728 // We may be able to bound a variable range via assumptions in 6729 // Attributor. ex.) If x is assumed to be in [1, 3] and y is known to 6730 // evolve to x^2 + x, then we can say that y is in [2, 12]. 6731 6732 if (!CtxI || CtxI == getCtxI()) 6733 return getAssumed(); 6734 6735 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI); 6736 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI); 6737 return getAssumed().intersectWith(SCEVR).intersectWith(LVIR); 6738 } 6739 6740 /// See AbstractAttribute::initialize(..). 6741 void initialize(Attributor &A) override { 6742 // Intersect a range given by SCEV. 6743 intersectKnown(getConstantRangeFromSCEV(A, getCtxI())); 6744 6745 // Intersect a range given by LVI. 6746 intersectKnown(getConstantRangeFromLVI(A, getCtxI())); 6747 } 6748 6749 /// Helper function to create MDNode for range metadata. 6750 static MDNode * 6751 getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx, 6752 const ConstantRange &AssumedConstantRange) { 6753 Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get( 6754 Ty, AssumedConstantRange.getLower())), 6755 ConstantAsMetadata::get(ConstantInt::get( 6756 Ty, AssumedConstantRange.getUpper()))}; 6757 return MDNode::get(Ctx, LowAndHigh); 6758 } 6759 6760 /// Return true if \p Assumed is included in \p KnownRanges. 6761 static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) { 6762 6763 if (Assumed.isFullSet()) 6764 return false; 6765 6766 if (!KnownRanges) 6767 return true; 6768 6769 // If multiple ranges are annotated in IR, we give up to annotate assumed 6770 // range for now. 6771 6772 // TODO: If there exists a known range which containts assumed range, we 6773 // can say assumed range is better. 6774 if (KnownRanges->getNumOperands() > 2) 6775 return false; 6776 6777 ConstantInt *Lower = 6778 mdconst::extract<ConstantInt>(KnownRanges->getOperand(0)); 6779 ConstantInt *Upper = 6780 mdconst::extract<ConstantInt>(KnownRanges->getOperand(1)); 6781 6782 ConstantRange Known(Lower->getValue(), Upper->getValue()); 6783 return Known.contains(Assumed) && Known != Assumed; 6784 } 6785 6786 /// Helper function to set range metadata. 6787 static bool 6788 setRangeMetadataIfisBetterRange(Instruction *I, 6789 const ConstantRange &AssumedConstantRange) { 6790 auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range); 6791 if (isBetterRange(AssumedConstantRange, OldRangeMD)) { 6792 if (!AssumedConstantRange.isEmptySet()) { 6793 I->setMetadata(LLVMContext::MD_range, 6794 getMDNodeForConstantRange(I->getType(), I->getContext(), 6795 AssumedConstantRange)); 6796 return true; 6797 } 6798 } 6799 return false; 6800 } 6801 6802 /// See AbstractAttribute::manifest() 6803 ChangeStatus manifest(Attributor &A) override { 6804 ChangeStatus Changed = ChangeStatus::UNCHANGED; 6805 ConstantRange AssumedConstantRange = getAssumedConstantRange(A); 6806 assert(!AssumedConstantRange.isFullSet() && "Invalid state"); 6807 6808 auto &V = getAssociatedValue(); 6809 if (!AssumedConstantRange.isEmptySet() && 6810 !AssumedConstantRange.isSingleElement()) { 6811 if (Instruction *I = dyn_cast<Instruction>(&V)) 6812 if (isa<CallInst>(I) || isa<LoadInst>(I)) 6813 if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange)) 6814 Changed = ChangeStatus::CHANGED; 6815 } 6816 6817 return Changed; 6818 } 6819 }; 6820 6821 struct AAValueConstantRangeArgument final 6822 : AAArgumentFromCallSiteArguments< 6823 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState> { 6824 AAValueConstantRangeArgument(const IRPosition &IRP) 6825 : AAArgumentFromCallSiteArguments< 6826 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState>( 6827 IRP) {} 6828 6829 /// See AbstractAttribute::trackStatistics() 6830 void trackStatistics() const override { 6831 STATS_DECLTRACK_ARG_ATTR(value_range) 6832 } 6833 }; 6834 6835 struct AAValueConstantRangeReturned 6836 : AAReturnedFromReturnedValues<AAValueConstantRange, 6837 AAValueConstantRangeImpl> { 6838 using Base = AAReturnedFromReturnedValues<AAValueConstantRange, 6839 AAValueConstantRangeImpl>; 6840 AAValueConstantRangeReturned(const IRPosition &IRP) : Base(IRP) {} 6841 6842 /// See AbstractAttribute::initialize(...). 6843 void initialize(Attributor &A) override {} 6844 6845 /// See AbstractAttribute::trackStatistics() 6846 void trackStatistics() const override { 6847 STATS_DECLTRACK_FNRET_ATTR(value_range) 6848 } 6849 }; 6850 6851 struct AAValueConstantRangeFloating : AAValueConstantRangeImpl { 6852 AAValueConstantRangeFloating(const IRPosition &IRP) 6853 : AAValueConstantRangeImpl(IRP) {} 6854 6855 /// See AbstractAttribute::initialize(...). 6856 void initialize(Attributor &A) override { 6857 AAValueConstantRangeImpl::initialize(A); 6858 Value &V = getAssociatedValue(); 6859 6860 if (auto *C = dyn_cast<ConstantInt>(&V)) { 6861 unionAssumed(ConstantRange(C->getValue())); 6862 indicateOptimisticFixpoint(); 6863 return; 6864 } 6865 6866 if (isa<UndefValue>(&V)) { 6867 // Collapse the undef state to 0. 6868 unionAssumed(ConstantRange(APInt(getBitWidth(), 0))); 6869 indicateOptimisticFixpoint(); 6870 return; 6871 } 6872 6873 if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V)) 6874 return; 6875 // If it is a load instruction with range metadata, use it. 6876 if (LoadInst *LI = dyn_cast<LoadInst>(&V)) 6877 if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) { 6878 intersectKnown(getConstantRangeFromMetadata(*RangeMD)); 6879 return; 6880 } 6881 6882 // We can work with PHI and select instruction as we traverse their operands 6883 // during update. 6884 if (isa<SelectInst>(V) || isa<PHINode>(V)) 6885 return; 6886 6887 // Otherwise we give up. 6888 indicatePessimisticFixpoint(); 6889 6890 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: " 6891 << getAssociatedValue() << "\n"); 6892 } 6893 6894 bool calculateBinaryOperator( 6895 Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T, 6896 Instruction *CtxI, 6897 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { 6898 Value *LHS = BinOp->getOperand(0); 6899 Value *RHS = BinOp->getOperand(1); 6900 // TODO: Allow non integers as well. 6901 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) 6902 return false; 6903 6904 auto &LHSAA = 6905 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS)); 6906 QuerriedAAs.push_back(&LHSAA); 6907 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI); 6908 6909 auto &RHSAA = 6910 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS)); 6911 QuerriedAAs.push_back(&RHSAA); 6912 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI); 6913 6914 auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange); 6915 6916 T.unionAssumed(AssumedRange); 6917 6918 // TODO: Track a known state too. 6919 6920 return T.isValidState(); 6921 } 6922 6923 bool calculateCastInst( 6924 Attributor &A, CastInst *CastI, IntegerRangeState &T, Instruction *CtxI, 6925 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { 6926 assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!"); 6927 // TODO: Allow non integers as well. 6928 Value &OpV = *CastI->getOperand(0); 6929 if (!OpV.getType()->isIntegerTy()) 6930 return false; 6931 6932 auto &OpAA = 6933 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(OpV)); 6934 QuerriedAAs.push_back(&OpAA); 6935 T.unionAssumed( 6936 OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth())); 6937 return T.isValidState(); 6938 } 6939 6940 bool 6941 calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T, 6942 Instruction *CtxI, 6943 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { 6944 Value *LHS = CmpI->getOperand(0); 6945 Value *RHS = CmpI->getOperand(1); 6946 // TODO: Allow non integers as well. 6947 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) 6948 return false; 6949 6950 auto &LHSAA = 6951 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS)); 6952 QuerriedAAs.push_back(&LHSAA); 6953 auto &RHSAA = 6954 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS)); 6955 QuerriedAAs.push_back(&RHSAA); 6956 6957 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI); 6958 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI); 6959 6960 // If one of them is empty set, we can't decide. 6961 if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet()) 6962 return true; 6963 6964 bool MustTrue = false, MustFalse = false; 6965 6966 auto AllowedRegion = 6967 ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange); 6968 6969 auto SatisfyingRegion = ConstantRange::makeSatisfyingICmpRegion( 6970 CmpI->getPredicate(), RHSAARange); 6971 6972 if (AllowedRegion.intersectWith(LHSAARange).isEmptySet()) 6973 MustFalse = true; 6974 6975 if (SatisfyingRegion.contains(LHSAARange)) 6976 MustTrue = true; 6977 6978 assert((!MustTrue || !MustFalse) && 6979 "Either MustTrue or MustFalse should be false!"); 6980 6981 if (MustTrue) 6982 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1))); 6983 else if (MustFalse) 6984 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0))); 6985 else 6986 T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true)); 6987 6988 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA 6989 << " " << RHSAA << "\n"); 6990 6991 // TODO: Track a known state too. 6992 return T.isValidState(); 6993 } 6994 6995 /// See AbstractAttribute::updateImpl(...). 6996 ChangeStatus updateImpl(Attributor &A) override { 6997 Instruction *CtxI = getCtxI(); 6998 auto VisitValueCB = [&](Value &V, IntegerRangeState &T, 6999 bool Stripped) -> bool { 7000 Instruction *I = dyn_cast<Instruction>(&V); 7001 if (!I) { 7002 7003 // If the value is not instruction, we query AA to Attributor. 7004 const auto &AA = 7005 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(V)); 7006 7007 // Clamp operator is not used to utilize a program point CtxI. 7008 T.unionAssumed(AA.getAssumedConstantRange(A, CtxI)); 7009 7010 return T.isValidState(); 7011 } 7012 7013 SmallVector<const AAValueConstantRange *, 4> QuerriedAAs; 7014 if (auto *BinOp = dyn_cast<BinaryOperator>(I)) { 7015 if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs)) 7016 return false; 7017 } else if (auto *CmpI = dyn_cast<CmpInst>(I)) { 7018 if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs)) 7019 return false; 7020 } else if (auto *CastI = dyn_cast<CastInst>(I)) { 7021 if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs)) 7022 return false; 7023 } else { 7024 // Give up with other instructions. 7025 // TODO: Add other instructions 7026 7027 T.indicatePessimisticFixpoint(); 7028 return false; 7029 } 7030 7031 // Catch circular reasoning in a pessimistic way for now. 7032 // TODO: Check how the range evolves and if we stripped anything, see also 7033 // AADereferenceable or AAAlign for similar situations. 7034 for (const AAValueConstantRange *QueriedAA : QuerriedAAs) { 7035 if (QueriedAA != this) 7036 continue; 7037 // If we are in a stady state we do not need to worry. 7038 if (T.getAssumed() == getState().getAssumed()) 7039 continue; 7040 T.indicatePessimisticFixpoint(); 7041 } 7042 7043 return T.isValidState(); 7044 }; 7045 7046 IntegerRangeState T(getBitWidth()); 7047 7048 if (!genericValueTraversal<AAValueConstantRange, IntegerRangeState>( 7049 A, getIRPosition(), *this, T, VisitValueCB)) 7050 return indicatePessimisticFixpoint(); 7051 7052 return clampStateAndIndicateChange(getState(), T); 7053 } 7054 7055 /// See AbstractAttribute::trackStatistics() 7056 void trackStatistics() const override { 7057 STATS_DECLTRACK_FLOATING_ATTR(value_range) 7058 } 7059 }; 7060 7061 struct AAValueConstantRangeFunction : AAValueConstantRangeImpl { 7062 AAValueConstantRangeFunction(const IRPosition &IRP) 7063 : AAValueConstantRangeImpl(IRP) {} 7064 7065 /// See AbstractAttribute::initialize(...). 7066 ChangeStatus updateImpl(Attributor &A) override { 7067 llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will " 7068 "not be called"); 7069 } 7070 7071 /// See AbstractAttribute::trackStatistics() 7072 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) } 7073 }; 7074 7075 struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction { 7076 AAValueConstantRangeCallSite(const IRPosition &IRP) 7077 : AAValueConstantRangeFunction(IRP) {} 7078 7079 /// See AbstractAttribute::trackStatistics() 7080 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) } 7081 }; 7082 7083 struct AAValueConstantRangeCallSiteReturned 7084 : AACallSiteReturnedFromReturned<AAValueConstantRange, 7085 AAValueConstantRangeImpl> { 7086 AAValueConstantRangeCallSiteReturned(const IRPosition &IRP) 7087 : AACallSiteReturnedFromReturned<AAValueConstantRange, 7088 AAValueConstantRangeImpl>(IRP) {} 7089 7090 /// See AbstractAttribute::initialize(...). 7091 void initialize(Attributor &A) override { 7092 // If it is a load instruction with range metadata, use the metadata. 7093 if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue())) 7094 if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range)) 7095 intersectKnown(getConstantRangeFromMetadata(*RangeMD)); 7096 7097 AAValueConstantRangeImpl::initialize(A); 7098 } 7099 7100 /// See AbstractAttribute::trackStatistics() 7101 void trackStatistics() const override { 7102 STATS_DECLTRACK_CSRET_ATTR(value_range) 7103 } 7104 }; 7105 struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating { 7106 AAValueConstantRangeCallSiteArgument(const IRPosition &IRP) 7107 : AAValueConstantRangeFloating(IRP) {} 7108 7109 /// See AbstractAttribute::trackStatistics() 7110 void trackStatistics() const override { 7111 STATS_DECLTRACK_CSARG_ATTR(value_range) 7112 } 7113 }; 7114 7115 } // namespace 7116 /// ---------------------------------------------------------------------------- 7117 /// Attributor 7118 /// ---------------------------------------------------------------------------- 7119 7120 bool Attributor::isAssumedDead(const AbstractAttribute &AA, 7121 const AAIsDead *FnLivenessAA, 7122 bool CheckBBLivenessOnly, DepClassTy DepClass) { 7123 const IRPosition &IRP = AA.getIRPosition(); 7124 if (!Functions.count(IRP.getAnchorScope())) 7125 return false; 7126 return isAssumedDead(IRP, &AA, FnLivenessAA, CheckBBLivenessOnly, DepClass); 7127 } 7128 7129 bool Attributor::isAssumedDead(const Use &U, 7130 const AbstractAttribute *QueryingAA, 7131 const AAIsDead *FnLivenessAA, 7132 bool CheckBBLivenessOnly, DepClassTy DepClass) { 7133 Instruction *UserI = dyn_cast<Instruction>(U.getUser()); 7134 if (!UserI) 7135 return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA, 7136 CheckBBLivenessOnly, DepClass); 7137 7138 if (CallSite CS = CallSite(UserI)) { 7139 // For call site argument uses we can check if the argument is 7140 // unused/dead. 7141 if (CS.isArgOperand(&U)) { 7142 const IRPosition &CSArgPos = 7143 IRPosition::callsite_argument(CS, CS.getArgumentNo(&U)); 7144 return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA, 7145 CheckBBLivenessOnly, DepClass); 7146 } 7147 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) { 7148 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction()); 7149 return isAssumedDead(RetPos, QueryingAA, FnLivenessAA, CheckBBLivenessOnly, 7150 DepClass); 7151 } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) { 7152 BasicBlock *IncomingBB = PHI->getIncomingBlock(U); 7153 return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA, 7154 CheckBBLivenessOnly, DepClass); 7155 } 7156 7157 return isAssumedDead(IRPosition::value(*UserI), QueryingAA, FnLivenessAA, 7158 CheckBBLivenessOnly, DepClass); 7159 } 7160 7161 bool Attributor::isAssumedDead(const Instruction &I, 7162 const AbstractAttribute *QueryingAA, 7163 const AAIsDead *FnLivenessAA, 7164 bool CheckBBLivenessOnly, DepClassTy DepClass) { 7165 if (!FnLivenessAA) 7166 FnLivenessAA = lookupAAFor<AAIsDead>(IRPosition::function(*I.getFunction()), 7167 QueryingAA, 7168 /* TrackDependence */ false); 7169 7170 // If we have a context instruction and a liveness AA we use it. 7171 if (FnLivenessAA && 7172 FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() && 7173 FnLivenessAA->isAssumedDead(&I)) { 7174 if (QueryingAA) 7175 recordDependence(*FnLivenessAA, *QueryingAA, DepClass); 7176 return true; 7177 } 7178 7179 if (CheckBBLivenessOnly) 7180 return false; 7181 7182 const AAIsDead &IsDeadAA = getOrCreateAAFor<AAIsDead>( 7183 IRPosition::value(I), QueryingAA, /* TrackDependence */ false); 7184 // Don't check liveness for AAIsDead. 7185 if (QueryingAA == &IsDeadAA) 7186 return false; 7187 7188 if (IsDeadAA.isAssumedDead()) { 7189 if (QueryingAA) 7190 recordDependence(IsDeadAA, *QueryingAA, DepClass); 7191 return true; 7192 } 7193 7194 return false; 7195 } 7196 7197 bool Attributor::isAssumedDead(const IRPosition &IRP, 7198 const AbstractAttribute *QueryingAA, 7199 const AAIsDead *FnLivenessAA, 7200 bool CheckBBLivenessOnly, DepClassTy DepClass) { 7201 Instruction *CtxI = IRP.getCtxI(); 7202 if (CtxI && 7203 isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, 7204 /* CheckBBLivenessOnly */ true, 7205 CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL)) 7206 return true; 7207 7208 if (CheckBBLivenessOnly) 7209 return false; 7210 7211 // If we haven't succeeded we query the specific liveness info for the IRP. 7212 const AAIsDead *IsDeadAA; 7213 if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE) 7214 IsDeadAA = &getOrCreateAAFor<AAIsDead>( 7215 IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())), 7216 QueryingAA, /* TrackDependence */ false); 7217 else 7218 IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, 7219 /* TrackDependence */ false); 7220 // Don't check liveness for AAIsDead. 7221 if (QueryingAA == IsDeadAA) 7222 return false; 7223 7224 if (IsDeadAA->isAssumedDead()) { 7225 if (QueryingAA) 7226 recordDependence(*IsDeadAA, *QueryingAA, DepClass); 7227 return true; 7228 } 7229 7230 return false; 7231 } 7232 7233 bool Attributor::checkForAllUses( 7234 const function_ref<bool(const Use &, bool &)> &Pred, 7235 const AbstractAttribute &QueryingAA, const Value &V, 7236 DepClassTy LivenessDepClass) { 7237 7238 // Check the trivial case first as it catches void values. 7239 if (V.use_empty()) 7240 return true; 7241 7242 // If the value is replaced by another one, for now a constant, we do not have 7243 // uses. Note that this requires users of `checkForAllUses` to not recurse but 7244 // instead use the `follow` callback argument to look at transitive users, 7245 // however, that should be clear from the presence of the argument. 7246 bool UsedAssumedInformation = false; 7247 Optional<Constant *> C = 7248 getAssumedConstant(*this, V, QueryingAA, UsedAssumedInformation); 7249 if (C.hasValue() && C.getValue()) { 7250 LLVM_DEBUG(dbgs() << "[Attributor] Value is simplified, uses skipped: " << V 7251 << " -> " << *C.getValue() << "\n"); 7252 return true; 7253 } 7254 7255 const IRPosition &IRP = QueryingAA.getIRPosition(); 7256 SmallVector<const Use *, 16> Worklist; 7257 SmallPtrSet<const Use *, 16> Visited; 7258 7259 for (const Use &U : V.uses()) 7260 Worklist.push_back(&U); 7261 7262 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size() 7263 << " initial uses to check\n"); 7264 7265 const Function *ScopeFn = IRP.getAnchorScope(); 7266 const auto *LivenessAA = 7267 ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn), 7268 /* TrackDependence */ false) 7269 : nullptr; 7270 7271 while (!Worklist.empty()) { 7272 const Use *U = Worklist.pop_back_val(); 7273 if (!Visited.insert(U).second) 7274 continue; 7275 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << " in " 7276 << *U->getUser() << "\n"); 7277 if (isAssumedDead(*U, &QueryingAA, LivenessAA, 7278 /* CheckBBLivenessOnly */ false, LivenessDepClass)) { 7279 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n"); 7280 continue; 7281 } 7282 7283 bool Follow = false; 7284 if (!Pred(*U, Follow)) 7285 return false; 7286 if (!Follow) 7287 continue; 7288 for (const Use &UU : U->getUser()->uses()) 7289 Worklist.push_back(&UU); 7290 } 7291 7292 return true; 7293 } 7294 7295 bool Attributor::checkForAllCallSites( 7296 const function_ref<bool(AbstractCallSite)> &Pred, 7297 const AbstractAttribute &QueryingAA, bool RequireAllCallSites, 7298 bool &AllCallSitesKnown) { 7299 // We can try to determine information from 7300 // the call sites. However, this is only possible all call sites are known, 7301 // hence the function has internal linkage. 7302 const IRPosition &IRP = QueryingAA.getIRPosition(); 7303 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 7304 if (!AssociatedFunction) { 7305 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP 7306 << "\n"); 7307 AllCallSitesKnown = false; 7308 return false; 7309 } 7310 7311 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites, 7312 &QueryingAA, AllCallSitesKnown); 7313 } 7314 7315 bool Attributor::checkForAllCallSites( 7316 const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn, 7317 bool RequireAllCallSites, const AbstractAttribute *QueryingAA, 7318 bool &AllCallSitesKnown) { 7319 if (RequireAllCallSites && !Fn.hasLocalLinkage()) { 7320 LLVM_DEBUG( 7321 dbgs() 7322 << "[Attributor] Function " << Fn.getName() 7323 << " has no internal linkage, hence not all call sites are known\n"); 7324 AllCallSitesKnown = false; 7325 return false; 7326 } 7327 7328 // If we do not require all call sites we might not see all. 7329 AllCallSitesKnown = RequireAllCallSites; 7330 7331 SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses())); 7332 for (unsigned u = 0; u < Uses.size(); ++u) { 7333 const Use &U = *Uses[u]; 7334 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << *U << " in " 7335 << *U.getUser() << "\n"); 7336 if (isAssumedDead(U, QueryingAA, nullptr, /* CheckBBLivenessOnly */ true)) { 7337 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n"); 7338 continue; 7339 } 7340 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) { 7341 if (CE->isCast() && CE->getType()->isPointerTy() && 7342 CE->getType()->getPointerElementType()->isFunctionTy()) { 7343 for (const Use &CEU : CE->uses()) 7344 Uses.push_back(&CEU); 7345 continue; 7346 } 7347 } 7348 7349 AbstractCallSite ACS(&U); 7350 if (!ACS) { 7351 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName() 7352 << " has non call site use " << *U.get() << " in " 7353 << *U.getUser() << "\n"); 7354 // BlockAddress users are allowed. 7355 if (isa<BlockAddress>(U.getUser())) 7356 continue; 7357 return false; 7358 } 7359 7360 const Use *EffectiveUse = 7361 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U; 7362 if (!ACS.isCallee(EffectiveUse)) { 7363 if (!RequireAllCallSites) 7364 continue; 7365 LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser() 7366 << " is an invalid use of " << Fn.getName() << "\n"); 7367 return false; 7368 } 7369 7370 // Make sure the arguments that can be matched between the call site and the 7371 // callee argee on their type. It is unlikely they do not and it doesn't 7372 // make sense for all attributes to know/care about this. 7373 assert(&Fn == ACS.getCalledFunction() && "Expected known callee"); 7374 unsigned MinArgsParams = 7375 std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size()); 7376 for (unsigned u = 0; u < MinArgsParams; ++u) { 7377 Value *CSArgOp = ACS.getCallArgOperand(u); 7378 if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) { 7379 LLVM_DEBUG( 7380 dbgs() << "[Attributor] Call site / callee argument type mismatch [" 7381 << u << "@" << Fn.getName() << ": " 7382 << *Fn.getArg(u)->getType() << " vs. " 7383 << *ACS.getCallArgOperand(u)->getType() << "\n"); 7384 return false; 7385 } 7386 } 7387 7388 if (Pred(ACS)) 7389 continue; 7390 7391 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " 7392 << *ACS.getInstruction() << "\n"); 7393 return false; 7394 } 7395 7396 return true; 7397 } 7398 7399 bool Attributor::checkForAllReturnedValuesAndReturnInsts( 7400 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> 7401 &Pred, 7402 const AbstractAttribute &QueryingAA) { 7403 7404 const IRPosition &IRP = QueryingAA.getIRPosition(); 7405 // Since we need to provide return instructions we have to have an exact 7406 // definition. 7407 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 7408 if (!AssociatedFunction) 7409 return false; 7410 7411 // If this is a call site query we use the call site specific return values 7412 // and liveness information. 7413 // TODO: use the function scope once we have call site AAReturnedValues. 7414 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 7415 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); 7416 if (!AARetVal.getState().isValidState()) 7417 return false; 7418 7419 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); 7420 } 7421 7422 bool Attributor::checkForAllReturnedValues( 7423 const function_ref<bool(Value &)> &Pred, 7424 const AbstractAttribute &QueryingAA) { 7425 7426 const IRPosition &IRP = QueryingAA.getIRPosition(); 7427 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 7428 if (!AssociatedFunction) 7429 return false; 7430 7431 // TODO: use the function scope once we have call site AAReturnedValues. 7432 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 7433 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); 7434 if (!AARetVal.getState().isValidState()) 7435 return false; 7436 7437 return AARetVal.checkForAllReturnedValuesAndReturnInsts( 7438 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { 7439 return Pred(RV); 7440 }); 7441 } 7442 7443 static bool checkForAllInstructionsImpl( 7444 Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap, 7445 const function_ref<bool(Instruction &)> &Pred, 7446 const AbstractAttribute *QueryingAA, const AAIsDead *LivenessAA, 7447 const ArrayRef<unsigned> &Opcodes, bool CheckBBLivenessOnly = false) { 7448 for (unsigned Opcode : Opcodes) { 7449 for (Instruction *I : OpcodeInstMap[Opcode]) { 7450 // Skip dead instructions. 7451 if (A && A->isAssumedDead(IRPosition::value(*I), QueryingAA, LivenessAA, 7452 CheckBBLivenessOnly)) 7453 continue; 7454 7455 if (!Pred(*I)) 7456 return false; 7457 } 7458 } 7459 return true; 7460 } 7461 7462 bool Attributor::checkForAllInstructions( 7463 const llvm::function_ref<bool(Instruction &)> &Pred, 7464 const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes, 7465 bool CheckBBLivenessOnly) { 7466 7467 const IRPosition &IRP = QueryingAA.getIRPosition(); 7468 // Since we need to provide instructions we have to have an exact definition. 7469 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 7470 if (!AssociatedFunction) 7471 return false; 7472 7473 // TODO: use the function scope once we have call site AAReturnedValues. 7474 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 7475 const auto &LivenessAA = 7476 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); 7477 7478 auto &OpcodeInstMap = 7479 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); 7480 if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA, 7481 &LivenessAA, Opcodes, CheckBBLivenessOnly)) 7482 return false; 7483 7484 return true; 7485 } 7486 7487 bool Attributor::checkForAllReadWriteInstructions( 7488 const llvm::function_ref<bool(Instruction &)> &Pred, 7489 AbstractAttribute &QueryingAA) { 7490 7491 const Function *AssociatedFunction = 7492 QueryingAA.getIRPosition().getAssociatedFunction(); 7493 if (!AssociatedFunction) 7494 return false; 7495 7496 // TODO: use the function scope once we have call site AAReturnedValues. 7497 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 7498 const auto &LivenessAA = 7499 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); 7500 7501 for (Instruction *I : 7502 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { 7503 // Skip dead instructions. 7504 if (isAssumedDead(IRPosition::value(*I), &QueryingAA, &LivenessAA)) 7505 continue; 7506 7507 if (!Pred(*I)) 7508 return false; 7509 } 7510 7511 return true; 7512 } 7513 7514 ChangeStatus Attributor::run() { 7515 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " 7516 << AllAbstractAttributes.size() 7517 << " abstract attributes.\n"); 7518 7519 // Now that all abstract attributes are collected and initialized we start 7520 // the abstract analysis. 7521 7522 unsigned IterationCounter = 1; 7523 7524 SmallVector<AbstractAttribute *, 64> ChangedAAs; 7525 SetVector<AbstractAttribute *> Worklist, InvalidAAs; 7526 Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); 7527 7528 bool RecomputeDependences = false; 7529 7530 do { 7531 // Remember the size to determine new attributes. 7532 size_t NumAAs = AllAbstractAttributes.size(); 7533 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter 7534 << ", Worklist size: " << Worklist.size() << "\n"); 7535 7536 // For invalid AAs we can fix dependent AAs that have a required dependence, 7537 // thereby folding long dependence chains in a single step without the need 7538 // to run updates. 7539 for (unsigned u = 0; u < InvalidAAs.size(); ++u) { 7540 AbstractAttribute *InvalidAA = InvalidAAs[u]; 7541 auto &QuerriedAAs = QueryMap[InvalidAA]; 7542 LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has " 7543 << QuerriedAAs.RequiredAAs.size() << "/" 7544 << QuerriedAAs.OptionalAAs.size() 7545 << " required/optional dependences\n"); 7546 for (AbstractAttribute *DepOnInvalidAA : QuerriedAAs.RequiredAAs) { 7547 AbstractState &DOIAAState = DepOnInvalidAA->getState(); 7548 DOIAAState.indicatePessimisticFixpoint(); 7549 ++NumAttributesFixedDueToRequiredDependences; 7550 assert(DOIAAState.isAtFixpoint() && "Expected fixpoint state!"); 7551 if (!DOIAAState.isValidState()) 7552 InvalidAAs.insert(DepOnInvalidAA); 7553 else 7554 ChangedAAs.push_back(DepOnInvalidAA); 7555 } 7556 if (!RecomputeDependences) 7557 Worklist.insert(QuerriedAAs.OptionalAAs.begin(), 7558 QuerriedAAs.OptionalAAs.end()); 7559 } 7560 7561 // If dependences (=QueryMap) are recomputed we have to look at all abstract 7562 // attributes again, regardless of what changed in the last iteration. 7563 if (RecomputeDependences) { 7564 LLVM_DEBUG( 7565 dbgs() << "[Attributor] Run all AAs to recompute dependences\n"); 7566 QueryMap.clear(); 7567 ChangedAAs.clear(); 7568 Worklist.insert(AllAbstractAttributes.begin(), 7569 AllAbstractAttributes.end()); 7570 } 7571 7572 // Add all abstract attributes that are potentially dependent on one that 7573 // changed to the work list. 7574 for (AbstractAttribute *ChangedAA : ChangedAAs) { 7575 auto &QuerriedAAs = QueryMap[ChangedAA]; 7576 Worklist.insert(QuerriedAAs.OptionalAAs.begin(), 7577 QuerriedAAs.OptionalAAs.end()); 7578 Worklist.insert(QuerriedAAs.RequiredAAs.begin(), 7579 QuerriedAAs.RequiredAAs.end()); 7580 } 7581 7582 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter 7583 << ", Worklist+Dependent size: " << Worklist.size() 7584 << "\n"); 7585 7586 // Reset the changed and invalid set. 7587 ChangedAAs.clear(); 7588 InvalidAAs.clear(); 7589 7590 // Update all abstract attribute in the work list and record the ones that 7591 // changed. 7592 for (AbstractAttribute *AA : Worklist) 7593 if (!AA->getState().isAtFixpoint() && 7594 !isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true)) { 7595 QueriedNonFixAA = false; 7596 if (AA->update(*this) == ChangeStatus::CHANGED) { 7597 ChangedAAs.push_back(AA); 7598 if (!AA->getState().isValidState()) 7599 InvalidAAs.insert(AA); 7600 } else if (!QueriedNonFixAA) { 7601 // If the attribute did not query any non-fix information, the state 7602 // will not change and we can indicate that right away. 7603 AA->getState().indicateOptimisticFixpoint(); 7604 } 7605 } 7606 7607 // Check if we recompute the dependences in the next iteration. 7608 RecomputeDependences = (DepRecomputeInterval > 0 && 7609 IterationCounter % DepRecomputeInterval == 0); 7610 7611 // Add attributes to the changed set if they have been created in the last 7612 // iteration. 7613 ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs, 7614 AllAbstractAttributes.end()); 7615 7616 // Reset the work list and repopulate with the changed abstract attributes. 7617 // Note that dependent ones are added above. 7618 Worklist.clear(); 7619 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); 7620 7621 } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations || 7622 VerifyMaxFixpointIterations)); 7623 7624 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " 7625 << IterationCounter << "/" << MaxFixpointIterations 7626 << " iterations\n"); 7627 7628 size_t NumFinalAAs = AllAbstractAttributes.size(); 7629 7630 // Reset abstract arguments not settled in a sound fixpoint by now. This 7631 // happens when we stopped the fixpoint iteration early. Note that only the 7632 // ones marked as "changed" *and* the ones transitively depending on them 7633 // need to be reverted to a pessimistic state. Others might not be in a 7634 // fixpoint state but we can use the optimistic results for them anyway. 7635 SmallPtrSet<AbstractAttribute *, 32> Visited; 7636 for (unsigned u = 0; u < ChangedAAs.size(); u++) { 7637 AbstractAttribute *ChangedAA = ChangedAAs[u]; 7638 if (!Visited.insert(ChangedAA).second) 7639 continue; 7640 7641 AbstractState &State = ChangedAA->getState(); 7642 if (!State.isAtFixpoint()) { 7643 State.indicatePessimisticFixpoint(); 7644 7645 NumAttributesTimedOut++; 7646 } 7647 7648 auto &QuerriedAAs = QueryMap[ChangedAA]; 7649 ChangedAAs.append(QuerriedAAs.OptionalAAs.begin(), 7650 QuerriedAAs.OptionalAAs.end()); 7651 ChangedAAs.append(QuerriedAAs.RequiredAAs.begin(), 7652 QuerriedAAs.RequiredAAs.end()); 7653 } 7654 7655 LLVM_DEBUG({ 7656 if (!Visited.empty()) 7657 dbgs() << "\n[Attributor] Finalized " << Visited.size() 7658 << " abstract attributes.\n"; 7659 }); 7660 7661 unsigned NumManifested = 0; 7662 unsigned NumAtFixpoint = 0; 7663 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; 7664 for (AbstractAttribute *AA : AllAbstractAttributes) { 7665 AbstractState &State = AA->getState(); 7666 7667 // If there is not already a fixpoint reached, we can now take the 7668 // optimistic state. This is correct because we enforced a pessimistic one 7669 // on abstract attributes that were transitively dependent on a changed one 7670 // already above. 7671 if (!State.isAtFixpoint()) 7672 State.indicateOptimisticFixpoint(); 7673 7674 // If the state is invalid, we do not try to manifest it. 7675 if (!State.isValidState()) 7676 continue; 7677 7678 // Skip dead code. 7679 if (isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true)) 7680 continue; 7681 // Manifest the state and record if we changed the IR. 7682 ChangeStatus LocalChange = AA->manifest(*this); 7683 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) 7684 AA->trackStatistics(); 7685 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA 7686 << "\n"); 7687 7688 ManifestChange = ManifestChange | LocalChange; 7689 7690 NumAtFixpoint++; 7691 NumManifested += (LocalChange == ChangeStatus::CHANGED); 7692 } 7693 7694 (void)NumManifested; 7695 (void)NumAtFixpoint; 7696 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested 7697 << " arguments while " << NumAtFixpoint 7698 << " were in a valid fixpoint state\n"); 7699 7700 NumAttributesManifested += NumManifested; 7701 NumAttributesValidFixpoint += NumAtFixpoint; 7702 7703 (void)NumFinalAAs; 7704 if (NumFinalAAs != AllAbstractAttributes.size()) { 7705 for (unsigned u = NumFinalAAs; u < AllAbstractAttributes.size(); ++u) 7706 errs() << "Unexpected abstract attribute: " << *AllAbstractAttributes[u] 7707 << " :: " 7708 << AllAbstractAttributes[u]->getIRPosition().getAssociatedValue() 7709 << "\n"; 7710 llvm_unreachable("Expected the final number of abstract attributes to " 7711 "remain unchanged!"); 7712 } 7713 7714 // Delete stuff at the end to avoid invalid references and a nice order. 7715 { 7716 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least " 7717 << ToBeDeletedFunctions.size() << " functions and " 7718 << ToBeDeletedBlocks.size() << " blocks and " 7719 << ToBeDeletedInsts.size() << " instructions and " 7720 << ToBeChangedUses.size() << " uses\n"); 7721 7722 SmallVector<WeakTrackingVH, 32> DeadInsts; 7723 SmallVector<Instruction *, 32> TerminatorsToFold; 7724 7725 for (auto &It : ToBeChangedUses) { 7726 Use *U = It.first; 7727 Value *NewV = It.second; 7728 Value *OldV = U->get(); 7729 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() 7730 << " instead of " << *OldV << "\n"); 7731 U->set(NewV); 7732 // Do not modify call instructions outside the SCC. 7733 if (auto *CB = dyn_cast<CallBase>(OldV)) 7734 if (!Functions.count(CB->getCaller())) 7735 continue; 7736 if (Instruction *I = dyn_cast<Instruction>(OldV)) { 7737 CGModifiedFunctions.insert(I->getFunction()); 7738 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) && 7739 isInstructionTriviallyDead(I)) 7740 DeadInsts.push_back(I); 7741 } 7742 if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) { 7743 Instruction *UserI = cast<Instruction>(U->getUser()); 7744 if (isa<UndefValue>(NewV)) { 7745 ToBeChangedToUnreachableInsts.insert(UserI); 7746 } else { 7747 TerminatorsToFold.push_back(UserI); 7748 } 7749 } 7750 } 7751 for (auto &V : InvokeWithDeadSuccessor) 7752 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) { 7753 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind); 7754 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn); 7755 bool Invoke2CallAllowed = 7756 !AAIsDeadFunction::mayCatchAsynchronousExceptions( 7757 *II->getFunction()); 7758 assert((UnwindBBIsDead || NormalBBIsDead) && 7759 "Invoke does not have dead successors!"); 7760 BasicBlock *BB = II->getParent(); 7761 BasicBlock *NormalDestBB = II->getNormalDest(); 7762 if (UnwindBBIsDead) { 7763 Instruction *NormalNextIP = &NormalDestBB->front(); 7764 if (Invoke2CallAllowed) { 7765 changeToCall(II); 7766 NormalNextIP = BB->getTerminator(); 7767 } 7768 if (NormalBBIsDead) 7769 ToBeChangedToUnreachableInsts.insert(NormalNextIP); 7770 } else { 7771 assert(NormalBBIsDead && "Broken invariant!"); 7772 if (!NormalDestBB->getUniquePredecessor()) 7773 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); 7774 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front()); 7775 } 7776 } 7777 for (auto &V : ToBeChangedToUnreachableInsts) 7778 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) { 7779 CGModifiedFunctions.insert(I->getFunction()); 7780 changeToUnreachable(I, /* UseLLVMTrap */ false); 7781 } 7782 for (Instruction *I : TerminatorsToFold) { 7783 CGModifiedFunctions.insert(I->getFunction()); 7784 ConstantFoldTerminator(I->getParent()); 7785 } 7786 7787 for (auto &V : ToBeDeletedInsts) { 7788 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) { 7789 CGModifiedFunctions.insert(I->getFunction()); 7790 if (!I->getType()->isVoidTy()) 7791 I->replaceAllUsesWith(UndefValue::get(I->getType())); 7792 if (!isa<PHINode>(I) && isInstructionTriviallyDead(I)) 7793 DeadInsts.push_back(I); 7794 else 7795 I->eraseFromParent(); 7796 } 7797 } 7798 7799 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); 7800 7801 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { 7802 SmallVector<BasicBlock *, 8> ToBeDeletedBBs; 7803 ToBeDeletedBBs.reserve(NumDeadBlocks); 7804 for (BasicBlock *BB : ToBeDeletedBlocks) { 7805 CGModifiedFunctions.insert(BB->getParent()); 7806 ToBeDeletedBBs.push_back(BB); 7807 } 7808 // Actually we do not delete the blocks but squash them into a single 7809 // unreachable but untangling branches that jump here is something we need 7810 // to do in a more generic way. 7811 DetatchDeadBlocks(ToBeDeletedBBs, nullptr); 7812 STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted."); 7813 BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size(); 7814 } 7815 7816 // Identify dead internal functions and delete them. This happens outside 7817 // the other fixpoint analysis as we might treat potentially dead functions 7818 // as live to lower the number of iterations. If they happen to be dead, the 7819 // below fixpoint loop will identify and eliminate them. 7820 SmallVector<Function *, 8> InternalFns; 7821 for (Function *F : Functions) 7822 if (F->hasLocalLinkage()) 7823 InternalFns.push_back(F); 7824 7825 bool FoundDeadFn = true; 7826 while (FoundDeadFn) { 7827 FoundDeadFn = false; 7828 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { 7829 Function *F = InternalFns[u]; 7830 if (!F) 7831 continue; 7832 7833 bool AllCallSitesKnown; 7834 if (!checkForAllCallSites( 7835 [this](AbstractCallSite ACS) { 7836 return ToBeDeletedFunctions.count( 7837 ACS.getInstruction()->getFunction()); 7838 }, 7839 *F, true, nullptr, AllCallSitesKnown)) 7840 continue; 7841 7842 ToBeDeletedFunctions.insert(F); 7843 InternalFns[u] = nullptr; 7844 FoundDeadFn = true; 7845 } 7846 } 7847 } 7848 7849 // Rewrite the functions as requested during manifest. 7850 ManifestChange = 7851 ManifestChange | rewriteFunctionSignatures(CGModifiedFunctions); 7852 7853 for (Function *Fn : CGModifiedFunctions) 7854 CGUpdater.reanalyzeFunction(*Fn); 7855 7856 STATS_DECL(AAIsDead, Function, "Number of dead functions deleted."); 7857 BUILD_STAT_NAME(AAIsDead, Function) += ToBeDeletedFunctions.size(); 7858 7859 for (Function *Fn : ToBeDeletedFunctions) 7860 CGUpdater.removeFunction(*Fn); 7861 7862 if (VerifyMaxFixpointIterations && 7863 IterationCounter != MaxFixpointIterations) { 7864 errs() << "\n[Attributor] Fixpoint iteration done after: " 7865 << IterationCounter << "/" << MaxFixpointIterations 7866 << " iterations\n"; 7867 llvm_unreachable("The fixpoint was not reached with exactly the number of " 7868 "specified iterations!"); 7869 } 7870 7871 return ManifestChange; 7872 } 7873 7874 bool Attributor::isValidFunctionSignatureRewrite( 7875 Argument &Arg, ArrayRef<Type *> ReplacementTypes) { 7876 7877 auto CallSiteCanBeChanged = [](AbstractCallSite ACS) { 7878 // Forbid must-tail calls for now. 7879 return !ACS.isCallbackCall() && !ACS.getCallSite().isMustTailCall(); 7880 }; 7881 7882 Function *Fn = Arg.getParent(); 7883 // Avoid var-arg functions for now. 7884 if (Fn->isVarArg()) { 7885 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n"); 7886 return false; 7887 } 7888 7889 // Avoid functions with complicated argument passing semantics. 7890 AttributeList FnAttributeList = Fn->getAttributes(); 7891 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) || 7892 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) || 7893 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca)) { 7894 LLVM_DEBUG( 7895 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n"); 7896 return false; 7897 } 7898 7899 // Avoid callbacks for now. 7900 bool AllCallSitesKnown; 7901 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr, 7902 AllCallSitesKnown)) { 7903 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n"); 7904 return false; 7905 } 7906 7907 auto InstPred = [](Instruction &I) { 7908 if (auto *CI = dyn_cast<CallInst>(&I)) 7909 return !CI->isMustTailCall(); 7910 return true; 7911 }; 7912 7913 // Forbid must-tail calls for now. 7914 // TODO: 7915 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn); 7916 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr, 7917 nullptr, {Instruction::Call})) { 7918 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n"); 7919 return false; 7920 } 7921 7922 return true; 7923 } 7924 7925 bool Attributor::registerFunctionSignatureRewrite( 7926 Argument &Arg, ArrayRef<Type *> ReplacementTypes, 7927 ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB, 7928 ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) { 7929 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " 7930 << Arg.getParent()->getName() << " with " 7931 << ReplacementTypes.size() << " replacements\n"); 7932 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) && 7933 "Cannot register an invalid rewrite"); 7934 7935 Function *Fn = Arg.getParent(); 7936 SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = ArgumentReplacementMap[Fn]; 7937 if (ARIs.empty()) 7938 ARIs.resize(Fn->arg_size()); 7939 7940 // If we have a replacement already with less than or equal new arguments, 7941 // ignore this request. 7942 ArgumentReplacementInfo *&ARI = ARIs[Arg.getArgNo()]; 7943 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) { 7944 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n"); 7945 return false; 7946 } 7947 7948 // If we have a replacement already but we like the new one better, delete 7949 // the old. 7950 if (ARI) 7951 delete ARI; 7952 7953 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " 7954 << Arg.getParent()->getName() << " with " 7955 << ReplacementTypes.size() << " replacements\n"); 7956 7957 // Remember the replacement. 7958 ARI = new ArgumentReplacementInfo(*this, Arg, ReplacementTypes, 7959 std::move(CalleeRepairCB), 7960 std::move(ACSRepairCB)); 7961 7962 return true; 7963 } 7964 7965 ChangeStatus Attributor::rewriteFunctionSignatures( 7966 SmallPtrSetImpl<Function *> &ModifiedFns) { 7967 ChangeStatus Changed = ChangeStatus::UNCHANGED; 7968 7969 for (auto &It : ArgumentReplacementMap) { 7970 Function *OldFn = It.getFirst(); 7971 7972 // Deleted functions do not require rewrites. 7973 if (ToBeDeletedFunctions.count(OldFn)) 7974 continue; 7975 7976 const SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = It.getSecond(); 7977 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!"); 7978 7979 SmallVector<Type *, 16> NewArgumentTypes; 7980 SmallVector<AttributeSet, 16> NewArgumentAttributes; 7981 7982 // Collect replacement argument types and copy over existing attributes. 7983 AttributeList OldFnAttributeList = OldFn->getAttributes(); 7984 for (Argument &Arg : OldFn->args()) { 7985 if (ArgumentReplacementInfo *ARI = ARIs[Arg.getArgNo()]) { 7986 NewArgumentTypes.append(ARI->ReplacementTypes.begin(), 7987 ARI->ReplacementTypes.end()); 7988 NewArgumentAttributes.append(ARI->getNumReplacementArgs(), 7989 AttributeSet()); 7990 } else { 7991 NewArgumentTypes.push_back(Arg.getType()); 7992 NewArgumentAttributes.push_back( 7993 OldFnAttributeList.getParamAttributes(Arg.getArgNo())); 7994 } 7995 } 7996 7997 FunctionType *OldFnTy = OldFn->getFunctionType(); 7998 Type *RetTy = OldFnTy->getReturnType(); 7999 8000 // Construct the new function type using the new arguments types. 8001 FunctionType *NewFnTy = 8002 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg()); 8003 8004 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName() 8005 << "' from " << *OldFn->getFunctionType() << " to " 8006 << *NewFnTy << "\n"); 8007 8008 // Create the new function body and insert it into the module. 8009 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(), 8010 OldFn->getAddressSpace(), ""); 8011 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn); 8012 NewFn->takeName(OldFn); 8013 NewFn->copyAttributesFrom(OldFn); 8014 8015 // Patch the pointer to LLVM function in debug info descriptor. 8016 NewFn->setSubprogram(OldFn->getSubprogram()); 8017 OldFn->setSubprogram(nullptr); 8018 8019 // Recompute the parameter attributes list based on the new arguments for 8020 // the function. 8021 LLVMContext &Ctx = OldFn->getContext(); 8022 NewFn->setAttributes(AttributeList::get( 8023 Ctx, OldFnAttributeList.getFnAttributes(), 8024 OldFnAttributeList.getRetAttributes(), NewArgumentAttributes)); 8025 8026 // Since we have now created the new function, splice the body of the old 8027 // function right into the new function, leaving the old rotting hulk of the 8028 // function empty. 8029 NewFn->getBasicBlockList().splice(NewFn->begin(), 8030 OldFn->getBasicBlockList()); 8031 8032 // Set of all "call-like" instructions that invoke the old function mapped 8033 // to their new replacements. 8034 SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs; 8035 8036 // Callback to create a new "call-like" instruction for a given one. 8037 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) { 8038 CallBase *OldCB = cast<CallBase>(ACS.getInstruction()); 8039 const AttributeList &OldCallAttributeList = OldCB->getAttributes(); 8040 8041 // Collect the new argument operands for the replacement call site. 8042 SmallVector<Value *, 16> NewArgOperands; 8043 SmallVector<AttributeSet, 16> NewArgOperandAttributes; 8044 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) { 8045 unsigned NewFirstArgNum = NewArgOperands.size(); 8046 (void)NewFirstArgNum; // only used inside assert. 8047 if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { 8048 if (ARI->ACSRepairCB) 8049 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands); 8050 assert(ARI->getNumReplacementArgs() + NewFirstArgNum == 8051 NewArgOperands.size() && 8052 "ACS repair callback did not provide as many operand as new " 8053 "types were registered!"); 8054 // TODO: Exose the attribute set to the ACS repair callback 8055 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(), 8056 AttributeSet()); 8057 } else { 8058 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum)); 8059 NewArgOperandAttributes.push_back( 8060 OldCallAttributeList.getParamAttributes(OldArgNum)); 8061 } 8062 } 8063 8064 assert(NewArgOperands.size() == NewArgOperandAttributes.size() && 8065 "Mismatch # argument operands vs. # argument operand attributes!"); 8066 assert(NewArgOperands.size() == NewFn->arg_size() && 8067 "Mismatch # argument operands vs. # function arguments!"); 8068 8069 SmallVector<OperandBundleDef, 4> OperandBundleDefs; 8070 OldCB->getOperandBundlesAsDefs(OperandBundleDefs); 8071 8072 // Create a new call or invoke instruction to replace the old one. 8073 CallBase *NewCB; 8074 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) { 8075 NewCB = 8076 InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(), 8077 NewArgOperands, OperandBundleDefs, "", OldCB); 8078 } else { 8079 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs, 8080 "", OldCB); 8081 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind()); 8082 NewCB = NewCI; 8083 } 8084 8085 // Copy over various properties and the new attributes. 8086 uint64_t W; 8087 if (OldCB->extractProfTotalWeight(W)) 8088 NewCB->setProfWeight(W); 8089 NewCB->setCallingConv(OldCB->getCallingConv()); 8090 NewCB->setDebugLoc(OldCB->getDebugLoc()); 8091 NewCB->takeName(OldCB); 8092 NewCB->setAttributes(AttributeList::get( 8093 Ctx, OldCallAttributeList.getFnAttributes(), 8094 OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes)); 8095 8096 CallSitePairs.push_back({OldCB, NewCB}); 8097 return true; 8098 }; 8099 8100 // Use the CallSiteReplacementCreator to create replacement call sites. 8101 bool AllCallSitesKnown; 8102 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn, 8103 true, nullptr, AllCallSitesKnown); 8104 (void)Success; 8105 assert(Success && "Assumed call site replacement to succeed!"); 8106 8107 // Rewire the arguments. 8108 auto OldFnArgIt = OldFn->arg_begin(); 8109 auto NewFnArgIt = NewFn->arg_begin(); 8110 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); 8111 ++OldArgNum, ++OldFnArgIt) { 8112 if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { 8113 if (ARI->CalleeRepairCB) 8114 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt); 8115 NewFnArgIt += ARI->ReplacementTypes.size(); 8116 } else { 8117 NewFnArgIt->takeName(&*OldFnArgIt); 8118 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt); 8119 ++NewFnArgIt; 8120 } 8121 } 8122 8123 // Eliminate the instructions *after* we visited all of them. 8124 for (auto &CallSitePair : CallSitePairs) { 8125 CallBase &OldCB = *CallSitePair.first; 8126 CallBase &NewCB = *CallSitePair.second; 8127 // We do not modify the call graph here but simply reanalyze the old 8128 // function. This should be revisited once the old PM is gone. 8129 ModifiedFns.insert(OldCB.getFunction()); 8130 OldCB.replaceAllUsesWith(&NewCB); 8131 OldCB.eraseFromParent(); 8132 } 8133 8134 // Replace the function in the call graph (if any). 8135 CGUpdater.replaceFunctionWith(*OldFn, *NewFn); 8136 8137 // If the old function was modified and needed to be reanalyzed, the new one 8138 // does now. 8139 if (ModifiedFns.erase(OldFn)) 8140 ModifiedFns.insert(NewFn); 8141 8142 Changed = ChangeStatus::CHANGED; 8143 } 8144 8145 return Changed; 8146 } 8147 8148 void Attributor::initializeInformationCache(Function &F) { 8149 8150 // Walk all instructions to find interesting instructions that might be 8151 // queried by abstract attributes during their initialization or update. 8152 // This has to happen before we create attributes. 8153 auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; 8154 auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; 8155 8156 for (Instruction &I : instructions(&F)) { 8157 bool IsInterestingOpcode = false; 8158 8159 // To allow easy access to all instructions in a function with a given 8160 // opcode we store them in the InfoCache. As not all opcodes are interesting 8161 // to concrete attributes we only cache the ones that are as identified in 8162 // the following switch. 8163 // Note: There are no concrete attributes now so this is initially empty. 8164 switch (I.getOpcode()) { 8165 default: 8166 assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && 8167 "New call site/base instruction type needs to be known int the " 8168 "Attributor."); 8169 break; 8170 case Instruction::Load: 8171 // The alignment of a pointer is interesting for loads. 8172 case Instruction::Store: 8173 // The alignment of a pointer is interesting for stores. 8174 case Instruction::Call: 8175 case Instruction::CallBr: 8176 case Instruction::Invoke: 8177 case Instruction::CleanupRet: 8178 case Instruction::CatchSwitch: 8179 case Instruction::AtomicRMW: 8180 case Instruction::AtomicCmpXchg: 8181 case Instruction::Br: 8182 case Instruction::Resume: 8183 case Instruction::Ret: 8184 IsInterestingOpcode = true; 8185 } 8186 if (IsInterestingOpcode) 8187 InstOpcodeMap[I.getOpcode()].push_back(&I); 8188 if (I.mayReadOrWriteMemory()) 8189 ReadOrWriteInsts.push_back(&I); 8190 } 8191 } 8192 8193 void Attributor::recordDependence(const AbstractAttribute &FromAA, 8194 const AbstractAttribute &ToAA, 8195 DepClassTy DepClass) { 8196 if (FromAA.getState().isAtFixpoint()) 8197 return; 8198 8199 if (DepClass == DepClassTy::REQUIRED) 8200 QueryMap[&FromAA].RequiredAAs.insert( 8201 const_cast<AbstractAttribute *>(&ToAA)); 8202 else 8203 QueryMap[&FromAA].OptionalAAs.insert( 8204 const_cast<AbstractAttribute *>(&ToAA)); 8205 QueriedNonFixAA = true; 8206 } 8207 8208 void Attributor::identifyDefaultAbstractAttributes(Function &F) { 8209 if (!VisitedFunctions.insert(&F).second) 8210 return; 8211 if (F.isDeclaration()) 8212 return; 8213 8214 IRPosition FPos = IRPosition::function(F); 8215 8216 // Check for dead BasicBlocks in every function. 8217 // We need dead instruction detection because we do not want to deal with 8218 // broken IR in which SSA rules do not apply. 8219 getOrCreateAAFor<AAIsDead>(FPos); 8220 8221 // Every function might be "will-return". 8222 getOrCreateAAFor<AAWillReturn>(FPos); 8223 8224 // Every function might contain instructions that cause "undefined behavior". 8225 getOrCreateAAFor<AAUndefinedBehavior>(FPos); 8226 8227 // Every function can be nounwind. 8228 getOrCreateAAFor<AANoUnwind>(FPos); 8229 8230 // Every function might be marked "nosync" 8231 getOrCreateAAFor<AANoSync>(FPos); 8232 8233 // Every function might be "no-free". 8234 getOrCreateAAFor<AANoFree>(FPos); 8235 8236 // Every function might be "no-return". 8237 getOrCreateAAFor<AANoReturn>(FPos); 8238 8239 // Every function might be "no-recurse". 8240 getOrCreateAAFor<AANoRecurse>(FPos); 8241 8242 // Every function might be "readnone/readonly/writeonly/...". 8243 getOrCreateAAFor<AAMemoryBehavior>(FPos); 8244 8245 // Every function can be "readnone/argmemonly/inaccessiblememonly/...". 8246 getOrCreateAAFor<AAMemoryLocation>(FPos); 8247 8248 // Every function might be applicable for Heap-To-Stack conversion. 8249 if (EnableHeapToStack) 8250 getOrCreateAAFor<AAHeapToStack>(FPos); 8251 8252 // Return attributes are only appropriate if the return type is non void. 8253 Type *ReturnType = F.getReturnType(); 8254 if (!ReturnType->isVoidTy()) { 8255 // Argument attribute "returned" --- Create only one per function even 8256 // though it is an argument attribute. 8257 getOrCreateAAFor<AAReturnedValues>(FPos); 8258 8259 IRPosition RetPos = IRPosition::returned(F); 8260 8261 // Every returned value might be dead. 8262 getOrCreateAAFor<AAIsDead>(RetPos); 8263 8264 // Every function might be simplified. 8265 getOrCreateAAFor<AAValueSimplify>(RetPos); 8266 8267 if (ReturnType->isPointerTy()) { 8268 8269 // Every function with pointer return type might be marked align. 8270 getOrCreateAAFor<AAAlign>(RetPos); 8271 8272 // Every function with pointer return type might be marked nonnull. 8273 getOrCreateAAFor<AANonNull>(RetPos); 8274 8275 // Every function with pointer return type might be marked noalias. 8276 getOrCreateAAFor<AANoAlias>(RetPos); 8277 8278 // Every function with pointer return type might be marked 8279 // dereferenceable. 8280 getOrCreateAAFor<AADereferenceable>(RetPos); 8281 } 8282 } 8283 8284 for (Argument &Arg : F.args()) { 8285 IRPosition ArgPos = IRPosition::argument(Arg); 8286 8287 // Every argument might be simplified. 8288 getOrCreateAAFor<AAValueSimplify>(ArgPos); 8289 8290 // Every argument might be dead. 8291 getOrCreateAAFor<AAIsDead>(ArgPos); 8292 8293 if (Arg.getType()->isPointerTy()) { 8294 // Every argument with pointer type might be marked nonnull. 8295 getOrCreateAAFor<AANonNull>(ArgPos); 8296 8297 // Every argument with pointer type might be marked noalias. 8298 getOrCreateAAFor<AANoAlias>(ArgPos); 8299 8300 // Every argument with pointer type might be marked dereferenceable. 8301 getOrCreateAAFor<AADereferenceable>(ArgPos); 8302 8303 // Every argument with pointer type might be marked align. 8304 getOrCreateAAFor<AAAlign>(ArgPos); 8305 8306 // Every argument with pointer type might be marked nocapture. 8307 getOrCreateAAFor<AANoCapture>(ArgPos); 8308 8309 // Every argument with pointer type might be marked 8310 // "readnone/readonly/writeonly/..." 8311 getOrCreateAAFor<AAMemoryBehavior>(ArgPos); 8312 8313 // Every argument with pointer type might be marked nofree. 8314 getOrCreateAAFor<AANoFree>(ArgPos); 8315 8316 // Every argument with pointer type might be privatizable (or promotable) 8317 getOrCreateAAFor<AAPrivatizablePtr>(ArgPos); 8318 } 8319 } 8320 8321 auto CallSitePred = [&](Instruction &I) -> bool { 8322 CallSite CS(&I); 8323 IRPosition CSRetPos = IRPosition::callsite_returned(CS); 8324 8325 // Call sites might be dead if they do not have side effects and no live 8326 // users. The return value might be dead if there are no live users. 8327 getOrCreateAAFor<AAIsDead>(CSRetPos); 8328 8329 if (Function *Callee = CS.getCalledFunction()) { 8330 // Skip declerations except if annotations on their call sites were 8331 // explicitly requested. 8332 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() && 8333 !Callee->hasMetadata(LLVMContext::MD_callback)) 8334 return true; 8335 8336 if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) { 8337 8338 IRPosition CSRetPos = IRPosition::callsite_returned(CS); 8339 8340 // Call site return integer values might be limited by a constant range. 8341 if (Callee->getReturnType()->isIntegerTy()) 8342 getOrCreateAAFor<AAValueConstantRange>(CSRetPos); 8343 } 8344 8345 for (int i = 0, e = CS.getNumArgOperands(); i < e; i++) { 8346 8347 IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); 8348 8349 // Every call site argument might be dead. 8350 getOrCreateAAFor<AAIsDead>(CSArgPos); 8351 8352 // Call site argument might be simplified. 8353 getOrCreateAAFor<AAValueSimplify>(CSArgPos); 8354 8355 if (!CS.getArgument(i)->getType()->isPointerTy()) 8356 continue; 8357 8358 // Call site argument attribute "non-null". 8359 getOrCreateAAFor<AANonNull>(CSArgPos); 8360 8361 // Call site argument attribute "no-alias". 8362 getOrCreateAAFor<AANoAlias>(CSArgPos); 8363 8364 // Call site argument attribute "dereferenceable". 8365 getOrCreateAAFor<AADereferenceable>(CSArgPos); 8366 8367 // Call site argument attribute "align". 8368 getOrCreateAAFor<AAAlign>(CSArgPos); 8369 8370 // Call site argument attribute 8371 // "readnone/readonly/writeonly/..." 8372 getOrCreateAAFor<AAMemoryBehavior>(CSArgPos); 8373 8374 // Call site argument attribute "nofree". 8375 getOrCreateAAFor<AANoFree>(CSArgPos); 8376 } 8377 } 8378 return true; 8379 }; 8380 8381 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); 8382 bool Success; 8383 Success = checkForAllInstructionsImpl( 8384 nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr, 8385 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, 8386 (unsigned)Instruction::Call}); 8387 (void)Success; 8388 assert(Success && "Expected the check call to be successful!"); 8389 8390 auto LoadStorePred = [&](Instruction &I) -> bool { 8391 if (isa<LoadInst>(I)) 8392 getOrCreateAAFor<AAAlign>( 8393 IRPosition::value(*cast<LoadInst>(I).getPointerOperand())); 8394 else 8395 getOrCreateAAFor<AAAlign>( 8396 IRPosition::value(*cast<StoreInst>(I).getPointerOperand())); 8397 return true; 8398 }; 8399 Success = checkForAllInstructionsImpl( 8400 nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr, 8401 {(unsigned)Instruction::Load, (unsigned)Instruction::Store}); 8402 (void)Success; 8403 assert(Success && "Expected the check call to be successful!"); 8404 } 8405 8406 /// Helpers to ease debugging through output streams and print calls. 8407 /// 8408 ///{ 8409 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { 8410 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); 8411 } 8412 8413 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { 8414 switch (AP) { 8415 case IRPosition::IRP_INVALID: 8416 return OS << "inv"; 8417 case IRPosition::IRP_FLOAT: 8418 return OS << "flt"; 8419 case IRPosition::IRP_RETURNED: 8420 return OS << "fn_ret"; 8421 case IRPosition::IRP_CALL_SITE_RETURNED: 8422 return OS << "cs_ret"; 8423 case IRPosition::IRP_FUNCTION: 8424 return OS << "fn"; 8425 case IRPosition::IRP_CALL_SITE: 8426 return OS << "cs"; 8427 case IRPosition::IRP_ARGUMENT: 8428 return OS << "arg"; 8429 case IRPosition::IRP_CALL_SITE_ARGUMENT: 8430 return OS << "cs_arg"; 8431 } 8432 llvm_unreachable("Unknown attribute position!"); 8433 } 8434 8435 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { 8436 const Value &AV = Pos.getAssociatedValue(); 8437 return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" 8438 << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; 8439 } 8440 8441 template <typename base_ty, base_ty BestState, base_ty WorstState> 8442 raw_ostream & 8443 llvm::operator<<(raw_ostream &OS, 8444 const IntegerStateBase<base_ty, BestState, WorstState> &S) { 8445 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" 8446 << static_cast<const AbstractState &>(S); 8447 } 8448 8449 raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) { 8450 OS << "range-state(" << S.getBitWidth() << ")<"; 8451 S.getKnown().print(OS); 8452 OS << " / "; 8453 S.getAssumed().print(OS); 8454 OS << ">"; 8455 8456 return OS << static_cast<const AbstractState &>(S); 8457 } 8458 8459 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { 8460 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); 8461 } 8462 8463 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { 8464 AA.print(OS); 8465 return OS; 8466 } 8467 8468 void AbstractAttribute::print(raw_ostream &OS) const { 8469 OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() 8470 << "]"; 8471 } 8472 ///} 8473 8474 /// ---------------------------------------------------------------------------- 8475 /// Pass (Manager) Boilerplate 8476 /// ---------------------------------------------------------------------------- 8477 8478 static bool runAttributorOnFunctions(InformationCache &InfoCache, 8479 SetVector<Function *> &Functions, 8480 AnalysisGetter &AG, 8481 CallGraphUpdater &CGUpdater) { 8482 if (DisableAttributor || Functions.empty()) 8483 return false; 8484 8485 LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << Functions.size() 8486 << " functions.\n"); 8487 8488 // Create an Attributor and initially empty information cache that is filled 8489 // while we identify default attribute opportunities. 8490 Attributor A(Functions, InfoCache, CGUpdater, DepRecInterval); 8491 8492 for (Function *F : Functions) 8493 A.initializeInformationCache(*F); 8494 8495 for (Function *F : Functions) { 8496 if (F->hasExactDefinition()) 8497 NumFnWithExactDefinition++; 8498 else 8499 NumFnWithoutExactDefinition++; 8500 8501 // We look at internal functions only on-demand but if any use is not a 8502 // direct call or outside the current set of analyzed functions, we have to 8503 // do it eagerly. 8504 if (F->hasLocalLinkage()) { 8505 if (llvm::all_of(F->uses(), [&Functions](const Use &U) { 8506 ImmutableCallSite ICS(U.getUser()); 8507 return ICS && ICS.isCallee(&U) && 8508 Functions.count(const_cast<Function *>(ICS.getCaller())); 8509 })) 8510 continue; 8511 } 8512 8513 // Populate the Attributor with abstract attribute opportunities in the 8514 // function and the information cache with IR information. 8515 A.identifyDefaultAbstractAttributes(*F); 8516 } 8517 8518 ChangeStatus Changed = A.run(); 8519 assert(!verifyModule(*Functions.front()->getParent(), &errs()) && 8520 "Module verification failed!"); 8521 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size() 8522 << " functions, result: " << Changed << ".\n"); 8523 return Changed == ChangeStatus::CHANGED; 8524 } 8525 8526 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { 8527 FunctionAnalysisManager &FAM = 8528 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 8529 AnalysisGetter AG(FAM); 8530 8531 SetVector<Function *> Functions; 8532 for (Function &F : M) 8533 Functions.insert(&F); 8534 8535 CallGraphUpdater CGUpdater; 8536 InformationCache InfoCache(M, AG, /* CGSCC */ nullptr); 8537 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) { 8538 // FIXME: Think about passes we will preserve and add them here. 8539 return PreservedAnalyses::none(); 8540 } 8541 return PreservedAnalyses::all(); 8542 } 8543 8544 PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C, 8545 CGSCCAnalysisManager &AM, 8546 LazyCallGraph &CG, 8547 CGSCCUpdateResult &UR) { 8548 FunctionAnalysisManager &FAM = 8549 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager(); 8550 AnalysisGetter AG(FAM); 8551 8552 SetVector<Function *> Functions; 8553 for (LazyCallGraph::Node &N : C) 8554 Functions.insert(&N.getFunction()); 8555 8556 if (Functions.empty()) 8557 return PreservedAnalyses::all(); 8558 8559 Module &M = *Functions.back()->getParent(); 8560 CallGraphUpdater CGUpdater; 8561 CGUpdater.initialize(CG, C, AM, UR); 8562 InformationCache InfoCache(M, AG, /* CGSCC */ &Functions); 8563 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) { 8564 // FIXME: Think about passes we will preserve and add them here. 8565 return PreservedAnalyses::none(); 8566 } 8567 return PreservedAnalyses::all(); 8568 } 8569 8570 namespace { 8571 8572 struct AttributorLegacyPass : public ModulePass { 8573 static char ID; 8574 8575 AttributorLegacyPass() : ModulePass(ID) { 8576 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); 8577 } 8578 8579 bool runOnModule(Module &M) override { 8580 if (skipModule(M)) 8581 return false; 8582 8583 AnalysisGetter AG; 8584 SetVector<Function *> Functions; 8585 for (Function &F : M) 8586 Functions.insert(&F); 8587 8588 CallGraphUpdater CGUpdater; 8589 InformationCache InfoCache(M, AG, /* CGSCC */ nullptr); 8590 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater); 8591 } 8592 8593 void getAnalysisUsage(AnalysisUsage &AU) const override { 8594 // FIXME: Think about passes we will preserve and add them here. 8595 AU.addRequired<TargetLibraryInfoWrapperPass>(); 8596 } 8597 }; 8598 8599 struct AttributorCGSCCLegacyPass : public CallGraphSCCPass { 8600 CallGraphUpdater CGUpdater; 8601 static char ID; 8602 8603 AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) { 8604 initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry()); 8605 } 8606 8607 bool runOnSCC(CallGraphSCC &SCC) override { 8608 if (skipSCC(SCC)) 8609 return false; 8610 8611 SetVector<Function *> Functions; 8612 for (CallGraphNode *CGN : SCC) 8613 if (Function *Fn = CGN->getFunction()) 8614 if (!Fn->isDeclaration()) 8615 Functions.insert(Fn); 8616 8617 if (Functions.empty()) 8618 return false; 8619 8620 AnalysisGetter AG; 8621 CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph()); 8622 CGUpdater.initialize(CG, SCC); 8623 Module &M = *Functions.back()->getParent(); 8624 InformationCache InfoCache(M, AG, /* CGSCC */ &Functions); 8625 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater); 8626 } 8627 8628 bool doFinalization(CallGraph &CG) override { return CGUpdater.finalize(); } 8629 8630 void getAnalysisUsage(AnalysisUsage &AU) const override { 8631 // FIXME: Think about passes we will preserve and add them here. 8632 AU.addRequired<TargetLibraryInfoWrapperPass>(); 8633 CallGraphSCCPass::getAnalysisUsage(AU); 8634 } 8635 }; 8636 8637 } // end anonymous namespace 8638 8639 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } 8640 Pass *llvm::createAttributorCGSCCLegacyPass() { 8641 return new AttributorCGSCCLegacyPass(); 8642 } 8643 8644 char AttributorLegacyPass::ID = 0; 8645 char AttributorCGSCCLegacyPass::ID = 0; 8646 8647 const char AAReturnedValues::ID = 0; 8648 const char AANoUnwind::ID = 0; 8649 const char AANoSync::ID = 0; 8650 const char AANoFree::ID = 0; 8651 const char AANonNull::ID = 0; 8652 const char AANoRecurse::ID = 0; 8653 const char AAWillReturn::ID = 0; 8654 const char AAUndefinedBehavior::ID = 0; 8655 const char AANoAlias::ID = 0; 8656 const char AAReachability::ID = 0; 8657 const char AANoReturn::ID = 0; 8658 const char AAIsDead::ID = 0; 8659 const char AADereferenceable::ID = 0; 8660 const char AAAlign::ID = 0; 8661 const char AANoCapture::ID = 0; 8662 const char AAValueSimplify::ID = 0; 8663 const char AAHeapToStack::ID = 0; 8664 const char AAPrivatizablePtr::ID = 0; 8665 const char AAMemoryBehavior::ID = 0; 8666 const char AAMemoryLocation::ID = 0; 8667 const char AAValueConstantRange::ID = 0; 8668 8669 // Macro magic to create the static generator function for attributes that 8670 // follow the naming scheme. 8671 8672 #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \ 8673 case IRPosition::PK: \ 8674 llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!"); 8675 8676 #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \ 8677 case IRPosition::PK: \ 8678 AA = new CLASS##SUFFIX(IRP); \ 8679 break; 8680 8681 #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 8682 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 8683 CLASS *AA = nullptr; \ 8684 switch (IRP.getPositionKind()) { \ 8685 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 8686 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ 8687 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ 8688 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ 8689 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ 8690 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ 8691 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 8692 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ 8693 } \ 8694 return *AA; \ 8695 } 8696 8697 #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 8698 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 8699 CLASS *AA = nullptr; \ 8700 switch (IRP.getPositionKind()) { \ 8701 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 8702 SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \ 8703 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ 8704 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ 8705 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ 8706 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ 8707 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ 8708 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ 8709 } \ 8710 return *AA; \ 8711 } 8712 8713 #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 8714 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 8715 CLASS *AA = nullptr; \ 8716 switch (IRP.getPositionKind()) { \ 8717 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 8718 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 8719 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ 8720 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ 8721 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ 8722 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ 8723 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ 8724 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ 8725 } \ 8726 return *AA; \ 8727 } 8728 8729 #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 8730 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 8731 CLASS *AA = nullptr; \ 8732 switch (IRP.getPositionKind()) { \ 8733 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 8734 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ 8735 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ 8736 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ 8737 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ 8738 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ 8739 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ 8740 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 8741 } \ 8742 return *AA; \ 8743 } 8744 8745 #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 8746 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 8747 CLASS *AA = nullptr; \ 8748 switch (IRP.getPositionKind()) { \ 8749 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 8750 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ 8751 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 8752 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ 8753 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ 8754 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ 8755 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ 8756 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ 8757 } \ 8758 return *AA; \ 8759 } 8760 8761 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) 8762 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) 8763 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) 8764 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) 8765 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) 8766 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) 8767 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation) 8768 8769 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) 8770 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) 8771 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr) 8772 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) 8773 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) 8774 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture) 8775 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange) 8776 8777 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify) 8778 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) 8779 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) 8780 8781 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack) 8782 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability) 8783 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior) 8784 8785 CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior) 8786 8787 #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION 8788 #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION 8789 #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION 8790 #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION 8791 #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION 8792 #undef SWITCH_PK_CREATE 8793 #undef SWITCH_PK_INV 8794 8795 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", 8796 "Deduce and propagate attributes", false, false) 8797 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 8798 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", 8799 "Deduce and propagate attributes", false, false) 8800 INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc", 8801 "Deduce and propagate attributes (CGSCC pass)", false, 8802 false) 8803 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 8804 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 8805 INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc", 8806 "Deduce and propagate attributes (CGSCC pass)", false, 8807 false) 8808