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