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