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