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