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