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