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