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