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