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