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