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