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