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