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