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