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