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