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