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