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