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 if (A.hasSimplificationCallback(getIRPosition())) 5071 indicatePessimisticFixpoint(); 5072 } 5073 5074 /// See AbstractAttribute::getAsStr(). 5075 const std::string getAsStr() const override { 5076 LLVM_DEBUG({ 5077 errs() << "SAV: " << SimplifiedAssociatedValue << " "; 5078 if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue) 5079 errs() << "SAV: " << **SimplifiedAssociatedValue << " "; 5080 }); 5081 return isValidState() ? (isAtFixpoint() ? "simplified" : "maybe-simple") 5082 : "not-simple"; 5083 } 5084 5085 /// See AbstractAttribute::trackStatistics() 5086 void trackStatistics() const override {} 5087 5088 /// See AAValueSimplify::getAssumedSimplifiedValue() 5089 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { 5090 return SimplifiedAssociatedValue; 5091 } 5092 5093 /// Return a value we can use as replacement for the associated one, or 5094 /// nullptr if we don't have one that makes sense. 5095 Value *getReplacementValue(Attributor &A) const { 5096 Value *NewV; 5097 NewV = SimplifiedAssociatedValue.hasValue() 5098 ? SimplifiedAssociatedValue.getValue() 5099 : UndefValue::get(getAssociatedType()); 5100 if (!NewV) 5101 return nullptr; 5102 NewV = AA::getWithType(*NewV, *getAssociatedType()); 5103 if (!NewV || NewV == &getAssociatedValue()) 5104 return nullptr; 5105 const Instruction *CtxI = getCtxI(); 5106 if (CtxI && !AA::isValidAtPosition(*NewV, *CtxI, A.getInfoCache())) 5107 return nullptr; 5108 if (!CtxI && !AA::isValidInScope(*NewV, getAnchorScope())) 5109 return nullptr; 5110 return NewV; 5111 } 5112 5113 /// Helper function for querying AAValueSimplify and updating candicate. 5114 /// \param IRP The value position we are trying to unify with SimplifiedValue 5115 bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA, 5116 const IRPosition &IRP, bool Simplify = true) { 5117 bool UsedAssumedInformation = false; 5118 Optional<Value *> QueryingValueSimplified = &IRP.getAssociatedValue(); 5119 if (Simplify) 5120 QueryingValueSimplified = 5121 A.getAssumedSimplified(IRP, QueryingAA, UsedAssumedInformation); 5122 return unionAssumed(QueryingValueSimplified); 5123 } 5124 5125 /// Returns a candidate is found or not 5126 template <typename AAType> bool askSimplifiedValueFor(Attributor &A) { 5127 if (!getAssociatedValue().getType()->isIntegerTy()) 5128 return false; 5129 5130 // This will also pass the call base context. 5131 const auto &AA = 5132 A.getAAFor<AAType>(*this, getIRPosition(), DepClassTy::NONE); 5133 5134 Optional<ConstantInt *> COpt = AA.getAssumedConstantInt(A); 5135 5136 if (!COpt.hasValue()) { 5137 SimplifiedAssociatedValue = llvm::None; 5138 A.recordDependence(AA, *this, DepClassTy::OPTIONAL); 5139 return true; 5140 } 5141 if (auto *C = COpt.getValue()) { 5142 SimplifiedAssociatedValue = C; 5143 A.recordDependence(AA, *this, DepClassTy::OPTIONAL); 5144 return true; 5145 } 5146 return false; 5147 } 5148 5149 bool askSimplifiedValueForOtherAAs(Attributor &A) { 5150 if (askSimplifiedValueFor<AAValueConstantRange>(A)) 5151 return true; 5152 if (askSimplifiedValueFor<AAPotentialValues>(A)) 5153 return true; 5154 return false; 5155 } 5156 5157 /// See AbstractAttribute::manifest(...). 5158 ChangeStatus manifest(Attributor &A) override { 5159 ChangeStatus Changed = ChangeStatus::UNCHANGED; 5160 if (getAssociatedValue().user_empty()) 5161 return Changed; 5162 5163 if (auto *NewV = getReplacementValue(A)) { 5164 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << getAssociatedValue() << " -> " 5165 << *NewV << " :: " << *this << "\n"); 5166 if (A.changeValueAfterManifest(getAssociatedValue(), *NewV)) 5167 Changed = ChangeStatus::CHANGED; 5168 } 5169 5170 return Changed | AAValueSimplify::manifest(A); 5171 } 5172 5173 /// See AbstractState::indicatePessimisticFixpoint(...). 5174 ChangeStatus indicatePessimisticFixpoint() override { 5175 SimplifiedAssociatedValue = &getAssociatedValue(); 5176 return AAValueSimplify::indicatePessimisticFixpoint(); 5177 } 5178 5179 static bool handleLoad(Attributor &A, const AbstractAttribute &AA, 5180 LoadInst &L, function_ref<bool(Value &)> Union) { 5181 auto UnionWrapper = [&](Value &V, Value &Obj) { 5182 if (isa<AllocaInst>(Obj)) 5183 return Union(V); 5184 if (!AA::isDynamicallyUnique(A, AA, V)) 5185 return false; 5186 if (!AA::isValidAtPosition(V, L, A.getInfoCache())) 5187 return false; 5188 return Union(V); 5189 }; 5190 5191 Value &Ptr = *L.getPointerOperand(); 5192 SmallVector<Value *, 8> Objects; 5193 if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, AA, &L)) 5194 return false; 5195 5196 for (Value *Obj : Objects) { 5197 LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n"); 5198 if (isa<UndefValue>(Obj)) 5199 continue; 5200 if (isa<ConstantPointerNull>(Obj)) { 5201 // A null pointer access can be undefined but any offset from null may 5202 // be OK. We do not try to optimize the latter. 5203 bool UsedAssumedInformation = false; 5204 if (!NullPointerIsDefined(L.getFunction(), 5205 Ptr.getType()->getPointerAddressSpace()) && 5206 A.getAssumedSimplified(Ptr, AA, UsedAssumedInformation) == Obj) 5207 continue; 5208 return false; 5209 } 5210 if (!isa<AllocaInst>(Obj) && !isa<GlobalVariable>(Obj)) 5211 return false; 5212 Constant *InitialVal = AA::getInitialValueForObj(*Obj, *L.getType()); 5213 if (!InitialVal || !Union(*InitialVal)) 5214 return false; 5215 5216 LLVM_DEBUG(dbgs() << "Underlying object amenable to load-store " 5217 "propagation, checking accesses next.\n"); 5218 5219 auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) { 5220 LLVM_DEBUG(dbgs() << " - visit access " << Acc << "\n"); 5221 if (!Acc.isWrite()) 5222 return true; 5223 if (Acc.isWrittenValueYetUndetermined()) 5224 return true; 5225 Value *Content = Acc.getWrittenValue(); 5226 if (!Content) 5227 return false; 5228 Value *CastedContent = 5229 AA::getWithType(*Content, *AA.getAssociatedType()); 5230 if (!CastedContent) 5231 return false; 5232 if (IsExact) 5233 return UnionWrapper(*CastedContent, *Obj); 5234 if (auto *C = dyn_cast<Constant>(CastedContent)) 5235 if (C->isNullValue() || C->isAllOnesValue() || isa<UndefValue>(C)) 5236 return UnionWrapper(*CastedContent, *Obj); 5237 return false; 5238 }; 5239 5240 auto &PI = A.getAAFor<AAPointerInfo>(AA, IRPosition::value(*Obj), 5241 DepClassTy::REQUIRED); 5242 if (!PI.forallInterferingAccesses(L, CheckAccess)) 5243 return false; 5244 } 5245 return true; 5246 } 5247 }; 5248 5249 struct AAValueSimplifyArgument final : AAValueSimplifyImpl { 5250 AAValueSimplifyArgument(const IRPosition &IRP, Attributor &A) 5251 : AAValueSimplifyImpl(IRP, A) {} 5252 5253 void initialize(Attributor &A) override { 5254 AAValueSimplifyImpl::initialize(A); 5255 if (!getAnchorScope() || getAnchorScope()->isDeclaration()) 5256 indicatePessimisticFixpoint(); 5257 if (hasAttr({Attribute::InAlloca, Attribute::Preallocated, 5258 Attribute::StructRet, Attribute::Nest, Attribute::ByVal}, 5259 /* IgnoreSubsumingPositions */ true)) 5260 indicatePessimisticFixpoint(); 5261 5262 // FIXME: This is a hack to prevent us from propagating function poiner in 5263 // the new pass manager CGSCC pass as it creates call edges the 5264 // CallGraphUpdater cannot handle yet. 5265 Value &V = getAssociatedValue(); 5266 if (V.getType()->isPointerTy() && 5267 V.getType()->getPointerElementType()->isFunctionTy() && 5268 !A.isModulePass()) 5269 indicatePessimisticFixpoint(); 5270 } 5271 5272 /// See AbstractAttribute::updateImpl(...). 5273 ChangeStatus updateImpl(Attributor &A) override { 5274 // Byval is only replacable if it is readonly otherwise we would write into 5275 // the replaced value and not the copy that byval creates implicitly. 5276 Argument *Arg = getAssociatedArgument(); 5277 if (Arg->hasByValAttr()) { 5278 // TODO: We probably need to verify synchronization is not an issue, e.g., 5279 // there is no race by not copying a constant byval. 5280 const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), 5281 DepClassTy::REQUIRED); 5282 if (!MemAA.isAssumedReadOnly()) 5283 return indicatePessimisticFixpoint(); 5284 } 5285 5286 auto Before = SimplifiedAssociatedValue; 5287 5288 auto PredForCallSite = [&](AbstractCallSite ACS) { 5289 const IRPosition &ACSArgPos = 5290 IRPosition::callsite_argument(ACS, getCallSiteArgNo()); 5291 // Check if a coresponding argument was found or if it is on not 5292 // associated (which can happen for callback calls). 5293 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) 5294 return false; 5295 5296 // Simplify the argument operand explicitly and check if the result is 5297 // valid in the current scope. This avoids refering to simplified values 5298 // in other functions, e.g., we don't want to say a an argument in a 5299 // static function is actually an argument in a different function. 5300 bool UsedAssumedInformation = false; 5301 Optional<Constant *> SimpleArgOp = 5302 A.getAssumedConstant(ACSArgPos, *this, UsedAssumedInformation); 5303 if (!SimpleArgOp.hasValue()) 5304 return true; 5305 if (!SimpleArgOp.getValue()) 5306 return false; 5307 if (!AA::isDynamicallyUnique(A, *this, **SimpleArgOp)) 5308 return false; 5309 return unionAssumed(*SimpleArgOp); 5310 }; 5311 5312 // Generate a answer specific to a call site context. 5313 bool Success; 5314 bool AllCallSitesKnown; 5315 if (hasCallBaseContext() && 5316 getCallBaseContext()->getCalledFunction() == Arg->getParent()) 5317 Success = PredForCallSite( 5318 AbstractCallSite(&getCallBaseContext()->getCalledOperandUse())); 5319 else 5320 Success = A.checkForAllCallSites(PredForCallSite, *this, true, 5321 AllCallSitesKnown); 5322 5323 if (!Success) 5324 if (!askSimplifiedValueForOtherAAs(A)) 5325 return indicatePessimisticFixpoint(); 5326 5327 // If a candicate was found in this update, return CHANGED. 5328 return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED 5329 : ChangeStatus ::CHANGED; 5330 } 5331 5332 /// See AbstractAttribute::trackStatistics() 5333 void trackStatistics() const override { 5334 STATS_DECLTRACK_ARG_ATTR(value_simplify) 5335 } 5336 }; 5337 5338 struct AAValueSimplifyReturned : AAValueSimplifyImpl { 5339 AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A) 5340 : AAValueSimplifyImpl(IRP, A) {} 5341 5342 /// See AAValueSimplify::getAssumedSimplifiedValue() 5343 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { 5344 if (!isValidState()) 5345 return nullptr; 5346 return SimplifiedAssociatedValue; 5347 } 5348 5349 /// See AbstractAttribute::updateImpl(...). 5350 ChangeStatus updateImpl(Attributor &A) override { 5351 auto Before = SimplifiedAssociatedValue; 5352 5353 auto PredForReturned = [&](Value &V) { 5354 return checkAndUpdate(A, *this, 5355 IRPosition::value(V, getCallBaseContext())); 5356 }; 5357 5358 if (!A.checkForAllReturnedValues(PredForReturned, *this)) 5359 if (!askSimplifiedValueForOtherAAs(A)) 5360 return indicatePessimisticFixpoint(); 5361 5362 // If a candicate was found in this update, return CHANGED. 5363 return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED 5364 : ChangeStatus ::CHANGED; 5365 } 5366 5367 ChangeStatus manifest(Attributor &A) override { 5368 ChangeStatus Changed = ChangeStatus::UNCHANGED; 5369 5370 if (auto *NewV = getReplacementValue(A)) { 5371 auto PredForReturned = 5372 [&](Value &, const SmallSetVector<ReturnInst *, 4> &RetInsts) { 5373 for (ReturnInst *RI : RetInsts) { 5374 Value *ReturnedVal = RI->getReturnValue(); 5375 if (ReturnedVal == NewV || isa<UndefValue>(ReturnedVal)) 5376 return true; 5377 assert(RI->getFunction() == getAnchorScope() && 5378 "ReturnInst in wrong function!"); 5379 LLVM_DEBUG(dbgs() 5380 << "[ValueSimplify] " << *ReturnedVal << " -> " 5381 << *NewV << " in " << *RI << " :: " << *this << "\n"); 5382 if (A.changeUseAfterManifest(RI->getOperandUse(0), *NewV)) 5383 Changed = ChangeStatus::CHANGED; 5384 } 5385 return true; 5386 }; 5387 A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this); 5388 } 5389 5390 return Changed | AAValueSimplify::manifest(A); 5391 } 5392 5393 /// See AbstractAttribute::trackStatistics() 5394 void trackStatistics() const override { 5395 STATS_DECLTRACK_FNRET_ATTR(value_simplify) 5396 } 5397 }; 5398 5399 struct AAValueSimplifyFloating : AAValueSimplifyImpl { 5400 AAValueSimplifyFloating(const IRPosition &IRP, Attributor &A) 5401 : AAValueSimplifyImpl(IRP, A) {} 5402 5403 /// See AbstractAttribute::initialize(...). 5404 void initialize(Attributor &A) override { 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 AAValueSimplifyImpl::initialize(A); 5643 if (!getAssociatedFunction()) 5644 indicatePessimisticFixpoint(); 5645 } 5646 5647 /// See AbstractAttribute::updateImpl(...). 5648 ChangeStatus updateImpl(Attributor &A) override { 5649 auto Before = SimplifiedAssociatedValue; 5650 auto &RetAA = A.getAAFor<AAReturnedValues>( 5651 *this, IRPosition::function(*getAssociatedFunction()), 5652 DepClassTy::REQUIRED); 5653 auto PredForReturned = 5654 [&](Value &RetVal, const SmallSetVector<ReturnInst *, 4> &RetInsts) { 5655 bool UsedAssumedInformation = false; 5656 Optional<Value *> CSRetVal = A.translateArgumentToCallSiteContent( 5657 &RetVal, *cast<CallBase>(getCtxI()), *this, 5658 UsedAssumedInformation); 5659 SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice( 5660 SimplifiedAssociatedValue, CSRetVal, getAssociatedType()); 5661 return SimplifiedAssociatedValue != Optional<Value *>(nullptr); 5662 }; 5663 if (!RetAA.checkForAllReturnedValuesAndReturnInsts(PredForReturned)) 5664 if (!askSimplifiedValueForOtherAAs(A)) 5665 return indicatePessimisticFixpoint(); 5666 return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED 5667 : ChangeStatus ::CHANGED; 5668 } 5669 5670 void trackStatistics() const override { 5671 STATS_DECLTRACK_CSRET_ATTR(value_simplify) 5672 } 5673 }; 5674 5675 struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating { 5676 AAValueSimplifyCallSiteArgument(const IRPosition &IRP, Attributor &A) 5677 : AAValueSimplifyFloating(IRP, A) {} 5678 5679 /// See AbstractAttribute::manifest(...). 5680 ChangeStatus manifest(Attributor &A) override { 5681 ChangeStatus Changed = ChangeStatus::UNCHANGED; 5682 5683 if (auto *NewV = getReplacementValue(A)) { 5684 Use &U = cast<CallBase>(&getAnchorValue()) 5685 ->getArgOperandUse(getCallSiteArgNo()); 5686 if (A.changeUseAfterManifest(U, *NewV)) 5687 Changed = ChangeStatus::CHANGED; 5688 } 5689 5690 return Changed | AAValueSimplify::manifest(A); 5691 } 5692 5693 void trackStatistics() const override { 5694 STATS_DECLTRACK_CSARG_ATTR(value_simplify) 5695 } 5696 }; 5697 5698 /// ----------------------- Heap-To-Stack Conversion --------------------------- 5699 struct AAHeapToStackFunction final : public AAHeapToStack { 5700 5701 struct AllocationInfo { 5702 /// The call that allocates the memory. 5703 CallBase *const CB; 5704 5705 /// The kind of allocation. 5706 const enum class AllocationKind { 5707 MALLOC, 5708 CALLOC, 5709 ALIGNED_ALLOC, 5710 } Kind; 5711 5712 /// The library function id for the allocation. 5713 LibFunc LibraryFunctionId = NotLibFunc; 5714 5715 /// The status wrt. a rewrite. 5716 enum { 5717 STACK_DUE_TO_USE, 5718 STACK_DUE_TO_FREE, 5719 INVALID, 5720 } Status = STACK_DUE_TO_USE; 5721 5722 /// Flag to indicate if we encountered a use that might free this allocation 5723 /// but which is not in the deallocation infos. 5724 bool HasPotentiallyFreeingUnknownUses = false; 5725 5726 /// The set of free calls that use this allocation. 5727 SmallPtrSet<CallBase *, 1> PotentialFreeCalls{}; 5728 }; 5729 5730 struct DeallocationInfo { 5731 /// The call that deallocates the memory. 5732 CallBase *const CB; 5733 5734 /// Flag to indicate if we don't know all objects this deallocation might 5735 /// free. 5736 bool MightFreeUnknownObjects = false; 5737 5738 /// The set of allocation calls that are potentially freed. 5739 SmallPtrSet<CallBase *, 1> PotentialAllocationCalls{}; 5740 }; 5741 5742 AAHeapToStackFunction(const IRPosition &IRP, Attributor &A) 5743 : AAHeapToStack(IRP, A) {} 5744 5745 ~AAHeapToStackFunction() { 5746 // Ensure we call the destructor so we release any memory allocated in the 5747 // sets. 5748 for (auto &It : AllocationInfos) 5749 It.getSecond()->~AllocationInfo(); 5750 for (auto &It : DeallocationInfos) 5751 It.getSecond()->~DeallocationInfo(); 5752 } 5753 5754 void initialize(Attributor &A) override { 5755 AAHeapToStack::initialize(A); 5756 5757 const Function *F = getAnchorScope(); 5758 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); 5759 5760 auto AllocationIdentifierCB = [&](Instruction &I) { 5761 CallBase *CB = dyn_cast<CallBase>(&I); 5762 if (!CB) 5763 return true; 5764 if (isFreeCall(CB, TLI)) { 5765 DeallocationInfos[CB] = new (A.Allocator) DeallocationInfo{CB}; 5766 return true; 5767 } 5768 bool IsMalloc = isMallocLikeFn(CB, TLI); 5769 bool IsAlignedAllocLike = !IsMalloc && isAlignedAllocLikeFn(CB, TLI); 5770 bool IsCalloc = 5771 !IsMalloc && !IsAlignedAllocLike && isCallocLikeFn(CB, TLI); 5772 if (!IsMalloc && !IsAlignedAllocLike && !IsCalloc) 5773 return true; 5774 auto Kind = 5775 IsMalloc ? AllocationInfo::AllocationKind::MALLOC 5776 : (IsCalloc ? AllocationInfo::AllocationKind::CALLOC 5777 : AllocationInfo::AllocationKind::ALIGNED_ALLOC); 5778 5779 AllocationInfo *AI = new (A.Allocator) AllocationInfo{CB, Kind}; 5780 AllocationInfos[CB] = AI; 5781 TLI->getLibFunc(*CB, AI->LibraryFunctionId); 5782 return true; 5783 }; 5784 5785 bool UsedAssumedInformation = false; 5786 bool Success = A.checkForAllCallLikeInstructions( 5787 AllocationIdentifierCB, *this, UsedAssumedInformation, 5788 /* CheckBBLivenessOnly */ false, 5789 /* CheckPotentiallyDead */ true); 5790 (void)Success; 5791 assert(Success && "Did not expect the call base visit callback to fail!"); 5792 } 5793 5794 const std::string getAsStr() const override { 5795 unsigned NumH2SMallocs = 0, NumInvalidMallocs = 0; 5796 for (const auto &It : AllocationInfos) { 5797 if (It.second->Status == AllocationInfo::INVALID) 5798 ++NumInvalidMallocs; 5799 else 5800 ++NumH2SMallocs; 5801 } 5802 return "[H2S] Mallocs Good/Bad: " + std::to_string(NumH2SMallocs) + "/" + 5803 std::to_string(NumInvalidMallocs); 5804 } 5805 5806 /// See AbstractAttribute::trackStatistics(). 5807 void trackStatistics() const override { 5808 STATS_DECL( 5809 MallocCalls, Function, 5810 "Number of malloc/calloc/aligned_alloc calls converted to allocas"); 5811 for (auto &It : AllocationInfos) 5812 if (It.second->Status != AllocationInfo::INVALID) 5813 ++BUILD_STAT_NAME(MallocCalls, Function); 5814 } 5815 5816 bool isAssumedHeapToStack(const CallBase &CB) const override { 5817 if (isValidState()) 5818 if (AllocationInfo *AI = AllocationInfos.lookup(&CB)) 5819 return AI->Status != AllocationInfo::INVALID; 5820 return false; 5821 } 5822 5823 bool isAssumedHeapToStackRemovedFree(CallBase &CB) const override { 5824 if (!isValidState()) 5825 return false; 5826 5827 for (auto &It : AllocationInfos) { 5828 AllocationInfo &AI = *It.second; 5829 if (AI.Status == AllocationInfo::INVALID) 5830 continue; 5831 5832 if (AI.PotentialFreeCalls.count(&CB)) 5833 return true; 5834 } 5835 5836 return false; 5837 } 5838 5839 ChangeStatus manifest(Attributor &A) override { 5840 assert(getState().isValidState() && 5841 "Attempted to manifest an invalid state!"); 5842 5843 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 5844 Function *F = getAnchorScope(); 5845 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); 5846 5847 for (auto &It : AllocationInfos) { 5848 AllocationInfo &AI = *It.second; 5849 if (AI.Status == AllocationInfo::INVALID) 5850 continue; 5851 5852 for (CallBase *FreeCall : AI.PotentialFreeCalls) { 5853 LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n"); 5854 A.deleteAfterManifest(*FreeCall); 5855 HasChanged = ChangeStatus::CHANGED; 5856 } 5857 5858 LLVM_DEBUG(dbgs() << "H2S: Removing malloc-like call: " << *AI.CB 5859 << "\n"); 5860 5861 auto Remark = [&](OptimizationRemark OR) { 5862 LibFunc IsAllocShared; 5863 if (TLI->getLibFunc(*AI.CB, IsAllocShared)) 5864 if (IsAllocShared == LibFunc___kmpc_alloc_shared) 5865 return OR << "Moving globalized variable to the stack."; 5866 return OR << "Moving memory allocation from the heap to the stack."; 5867 }; 5868 if (AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared) 5869 A.emitRemark<OptimizationRemark>(AI.CB, "OMP110", Remark); 5870 else 5871 A.emitRemark<OptimizationRemark>(AI.CB, "HeapToStack", Remark); 5872 5873 Value *Size; 5874 Optional<APInt> SizeAPI = getSize(A, *this, AI); 5875 if (SizeAPI.hasValue()) { 5876 Size = ConstantInt::get(AI.CB->getContext(), *SizeAPI); 5877 } else if (AI.Kind == AllocationInfo::AllocationKind::CALLOC) { 5878 auto *Num = AI.CB->getOperand(0); 5879 auto *SizeT = AI.CB->getOperand(1); 5880 IRBuilder<> B(AI.CB); 5881 Size = B.CreateMul(Num, SizeT, "h2s.calloc.size"); 5882 } else if (AI.Kind == AllocationInfo::AllocationKind::ALIGNED_ALLOC) { 5883 Size = AI.CB->getOperand(1); 5884 } else { 5885 Size = AI.CB->getOperand(0); 5886 } 5887 5888 Align Alignment(1); 5889 if (AI.Kind == AllocationInfo::AllocationKind::ALIGNED_ALLOC) { 5890 Optional<APInt> AlignmentAPI = 5891 getAPInt(A, *this, *AI.CB->getArgOperand(0)); 5892 assert(AlignmentAPI.hasValue() && 5893 "Expected an alignment during manifest!"); 5894 Alignment = 5895 max(Alignment, MaybeAlign(AlignmentAPI.getValue().getZExtValue())); 5896 } 5897 5898 unsigned AS = cast<PointerType>(AI.CB->getType())->getAddressSpace(); 5899 Instruction *Alloca = 5900 new AllocaInst(Type::getInt8Ty(F->getContext()), AS, Size, Alignment, 5901 "", AI.CB->getNextNode()); 5902 5903 if (Alloca->getType() != AI.CB->getType()) 5904 Alloca = new BitCastInst(Alloca, AI.CB->getType(), "malloc_bc", 5905 Alloca->getNextNode()); 5906 5907 A.changeValueAfterManifest(*AI.CB, *Alloca); 5908 5909 if (auto *II = dyn_cast<InvokeInst>(AI.CB)) { 5910 auto *NBB = II->getNormalDest(); 5911 BranchInst::Create(NBB, AI.CB->getParent()); 5912 A.deleteAfterManifest(*AI.CB); 5913 } else { 5914 A.deleteAfterManifest(*AI.CB); 5915 } 5916 5917 // Zero out the allocated memory if it was a calloc. 5918 if (AI.Kind == AllocationInfo::AllocationKind::CALLOC) { 5919 auto *BI = new BitCastInst(Alloca, AI.CB->getType(), "calloc_bc", 5920 Alloca->getNextNode()); 5921 Value *Ops[] = { 5922 BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size, 5923 ConstantInt::get(Type::getInt1Ty(F->getContext()), false)}; 5924 5925 Type *Tys[] = {BI->getType(), AI.CB->getOperand(0)->getType()}; 5926 Module *M = F->getParent(); 5927 Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); 5928 CallInst::Create(Fn, Ops, "", BI->getNextNode()); 5929 } 5930 HasChanged = ChangeStatus::CHANGED; 5931 } 5932 5933 return HasChanged; 5934 } 5935 5936 Optional<APInt> getAPInt(Attributor &A, const AbstractAttribute &AA, 5937 Value &V) { 5938 bool UsedAssumedInformation = false; 5939 Optional<Constant *> SimpleV = 5940 A.getAssumedConstant(V, AA, UsedAssumedInformation); 5941 if (!SimpleV.hasValue()) 5942 return APInt(64, 0); 5943 if (auto *CI = dyn_cast_or_null<ConstantInt>(SimpleV.getValue())) 5944 return CI->getValue(); 5945 return llvm::None; 5946 } 5947 5948 Optional<APInt> getSize(Attributor &A, const AbstractAttribute &AA, 5949 AllocationInfo &AI) { 5950 5951 if (AI.Kind == AllocationInfo::AllocationKind::MALLOC) 5952 return getAPInt(A, AA, *AI.CB->getArgOperand(0)); 5953 5954 if (AI.Kind == AllocationInfo::AllocationKind::ALIGNED_ALLOC) 5955 // Only if the alignment is also constant we return a size. 5956 return getAPInt(A, AA, *AI.CB->getArgOperand(0)).hasValue() 5957 ? getAPInt(A, AA, *AI.CB->getArgOperand(1)) 5958 : llvm::None; 5959 5960 assert(AI.Kind == AllocationInfo::AllocationKind::CALLOC && 5961 "Expected only callocs are left"); 5962 Optional<APInt> Num = getAPInt(A, AA, *AI.CB->getArgOperand(0)); 5963 Optional<APInt> Size = getAPInt(A, AA, *AI.CB->getArgOperand(1)); 5964 if (!Num.hasValue() || !Size.hasValue()) 5965 return llvm::None; 5966 bool Overflow = false; 5967 Size = Size.getValue().umul_ov(Num.getValue(), Overflow); 5968 return Overflow ? llvm::None : Size; 5969 } 5970 5971 /// Collection of all malloc-like calls in a function with associated 5972 /// information. 5973 DenseMap<CallBase *, AllocationInfo *> AllocationInfos; 5974 5975 /// Collection of all free-like calls in a function with associated 5976 /// information. 5977 DenseMap<CallBase *, DeallocationInfo *> DeallocationInfos; 5978 5979 ChangeStatus updateImpl(Attributor &A) override; 5980 }; 5981 5982 ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) { 5983 ChangeStatus Changed = ChangeStatus::UNCHANGED; 5984 const Function *F = getAnchorScope(); 5985 5986 const auto &LivenessAA = 5987 A.getAAFor<AAIsDead>(*this, IRPosition::function(*F), DepClassTy::NONE); 5988 5989 MustBeExecutedContextExplorer &Explorer = 5990 A.getInfoCache().getMustBeExecutedContextExplorer(); 5991 5992 bool StackIsAccessibleByOtherThreads = 5993 A.getInfoCache().stackIsAccessibleByOtherThreads(); 5994 5995 // Flag to ensure we update our deallocation information at most once per 5996 // updateImpl call and only if we use the free check reasoning. 5997 bool HasUpdatedFrees = false; 5998 5999 auto UpdateFrees = [&]() { 6000 HasUpdatedFrees = true; 6001 6002 for (auto &It : DeallocationInfos) { 6003 DeallocationInfo &DI = *It.second; 6004 // For now we cannot use deallocations that have unknown inputs, skip 6005 // them. 6006 if (DI.MightFreeUnknownObjects) 6007 continue; 6008 6009 // No need to analyze dead calls, ignore them instead. 6010 bool UsedAssumedInformation = false; 6011 if (A.isAssumedDead(*DI.CB, this, &LivenessAA, UsedAssumedInformation, 6012 /* CheckBBLivenessOnly */ true)) 6013 continue; 6014 6015 // Use the optimistic version to get the freed objects, ignoring dead 6016 // branches etc. 6017 SmallVector<Value *, 8> Objects; 6018 if (!AA::getAssumedUnderlyingObjects(A, *DI.CB->getArgOperand(0), Objects, 6019 *this, DI.CB)) { 6020 LLVM_DEBUG( 6021 dbgs() 6022 << "[H2S] Unexpected failure in getAssumedUnderlyingObjects!\n"); 6023 DI.MightFreeUnknownObjects = true; 6024 continue; 6025 } 6026 6027 // Check each object explicitly. 6028 for (auto *Obj : Objects) { 6029 // Free of null and undef can be ignored as no-ops (or UB in the latter 6030 // case). 6031 if (isa<ConstantPointerNull>(Obj) || isa<UndefValue>(Obj)) 6032 continue; 6033 6034 CallBase *ObjCB = dyn_cast<CallBase>(Obj); 6035 if (!ObjCB) { 6036 LLVM_DEBUG(dbgs() 6037 << "[H2S] Free of a non-call object: " << *Obj << "\n"); 6038 DI.MightFreeUnknownObjects = true; 6039 continue; 6040 } 6041 6042 AllocationInfo *AI = AllocationInfos.lookup(ObjCB); 6043 if (!AI) { 6044 LLVM_DEBUG(dbgs() << "[H2S] Free of a non-allocation object: " << *Obj 6045 << "\n"); 6046 DI.MightFreeUnknownObjects = true; 6047 continue; 6048 } 6049 6050 DI.PotentialAllocationCalls.insert(ObjCB); 6051 } 6052 } 6053 }; 6054 6055 auto FreeCheck = [&](AllocationInfo &AI) { 6056 // If the stack is not accessible by other threads, the "must-free" logic 6057 // doesn't apply as the pointer could be shared and needs to be places in 6058 // "shareable" memory. 6059 if (!StackIsAccessibleByOtherThreads) { 6060 auto &NoSyncAA = 6061 A.getAAFor<AANoSync>(*this, getIRPosition(), DepClassTy::OPTIONAL); 6062 if (!NoSyncAA.isAssumedNoSync()) { 6063 LLVM_DEBUG( 6064 dbgs() << "[H2S] found an escaping use, stack is not accessible by " 6065 "other threads and function is not nosync:\n"); 6066 return false; 6067 } 6068 } 6069 if (!HasUpdatedFrees) 6070 UpdateFrees(); 6071 6072 // TODO: Allow multi exit functions that have different free calls. 6073 if (AI.PotentialFreeCalls.size() != 1) { 6074 LLVM_DEBUG(dbgs() << "[H2S] did not find one free call but " 6075 << AI.PotentialFreeCalls.size() << "\n"); 6076 return false; 6077 } 6078 CallBase *UniqueFree = *AI.PotentialFreeCalls.begin(); 6079 DeallocationInfo *DI = DeallocationInfos.lookup(UniqueFree); 6080 if (!DI) { 6081 LLVM_DEBUG( 6082 dbgs() << "[H2S] unique free call was not known as deallocation call " 6083 << *UniqueFree << "\n"); 6084 return false; 6085 } 6086 if (DI->MightFreeUnknownObjects) { 6087 LLVM_DEBUG( 6088 dbgs() << "[H2S] unique free call might free unknown allocations\n"); 6089 return false; 6090 } 6091 if (DI->PotentialAllocationCalls.size() > 1) { 6092 LLVM_DEBUG(dbgs() << "[H2S] unique free call might free " 6093 << DI->PotentialAllocationCalls.size() 6094 << " different allocations\n"); 6095 return false; 6096 } 6097 if (*DI->PotentialAllocationCalls.begin() != AI.CB) { 6098 LLVM_DEBUG( 6099 dbgs() 6100 << "[H2S] unique free call not known to free this allocation but " 6101 << **DI->PotentialAllocationCalls.begin() << "\n"); 6102 return false; 6103 } 6104 Instruction *CtxI = isa<InvokeInst>(AI.CB) ? AI.CB : AI.CB->getNextNode(); 6105 if (!Explorer.findInContextOf(UniqueFree, CtxI)) { 6106 LLVM_DEBUG( 6107 dbgs() 6108 << "[H2S] unique free call might not be executed with the allocation " 6109 << *UniqueFree << "\n"); 6110 return false; 6111 } 6112 return true; 6113 }; 6114 6115 auto UsesCheck = [&](AllocationInfo &AI) { 6116 bool ValidUsesOnly = true; 6117 6118 auto Pred = [&](const Use &U, bool &Follow) -> bool { 6119 Instruction *UserI = cast<Instruction>(U.getUser()); 6120 if (isa<LoadInst>(UserI)) 6121 return true; 6122 if (auto *SI = dyn_cast<StoreInst>(UserI)) { 6123 if (SI->getValueOperand() == U.get()) { 6124 LLVM_DEBUG(dbgs() 6125 << "[H2S] escaping store to memory: " << *UserI << "\n"); 6126 ValidUsesOnly = false; 6127 } else { 6128 // A store into the malloc'ed memory is fine. 6129 } 6130 return true; 6131 } 6132 if (auto *CB = dyn_cast<CallBase>(UserI)) { 6133 if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd()) 6134 return true; 6135 if (DeallocationInfos.count(CB)) { 6136 AI.PotentialFreeCalls.insert(CB); 6137 return true; 6138 } 6139 6140 unsigned ArgNo = CB->getArgOperandNo(&U); 6141 6142 const auto &NoCaptureAA = A.getAAFor<AANoCapture>( 6143 *this, IRPosition::callsite_argument(*CB, ArgNo), 6144 DepClassTy::OPTIONAL); 6145 6146 // If a call site argument use is nofree, we are fine. 6147 const auto &ArgNoFreeAA = A.getAAFor<AANoFree>( 6148 *this, IRPosition::callsite_argument(*CB, ArgNo), 6149 DepClassTy::OPTIONAL); 6150 6151 bool MaybeCaptured = !NoCaptureAA.isAssumedNoCapture(); 6152 bool MaybeFreed = !ArgNoFreeAA.isAssumedNoFree(); 6153 if (MaybeCaptured || 6154 (AI.LibraryFunctionId != LibFunc___kmpc_alloc_shared && 6155 MaybeFreed)) { 6156 AI.HasPotentiallyFreeingUnknownUses |= MaybeFreed; 6157 6158 // Emit a missed remark if this is missed OpenMP globalization. 6159 auto Remark = [&](OptimizationRemarkMissed ORM) { 6160 return ORM 6161 << "Could not move globalized variable to the stack. " 6162 "Variable is potentially captured in call. Mark " 6163 "parameter as `__attribute__((noescape))` to override."; 6164 }; 6165 6166 if (ValidUsesOnly && 6167 AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared) 6168 A.emitRemark<OptimizationRemarkMissed>(AI.CB, "OMP113", Remark); 6169 6170 LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n"); 6171 ValidUsesOnly = false; 6172 } 6173 return true; 6174 } 6175 6176 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || 6177 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { 6178 Follow = true; 6179 return true; 6180 } 6181 // Unknown user for which we can not track uses further (in a way that 6182 // makes sense). 6183 LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n"); 6184 ValidUsesOnly = false; 6185 return true; 6186 }; 6187 if (!A.checkForAllUses(Pred, *this, *AI.CB)) 6188 return false; 6189 return ValidUsesOnly; 6190 }; 6191 6192 // The actual update starts here. We look at all allocations and depending on 6193 // their status perform the appropriate check(s). 6194 for (auto &It : AllocationInfos) { 6195 AllocationInfo &AI = *It.second; 6196 if (AI.Status == AllocationInfo::INVALID) 6197 continue; 6198 6199 if (MaxHeapToStackSize == -1) { 6200 if (AI.Kind == AllocationInfo::AllocationKind::ALIGNED_ALLOC) 6201 if (!getAPInt(A, *this, *AI.CB->getArgOperand(0)).hasValue()) { 6202 LLVM_DEBUG(dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CB 6203 << "\n"); 6204 AI.Status = AllocationInfo::INVALID; 6205 Changed = ChangeStatus::CHANGED; 6206 continue; 6207 } 6208 } else { 6209 Optional<APInt> Size = getSize(A, *this, AI); 6210 if (!Size.hasValue() || Size.getValue().ugt(MaxHeapToStackSize)) { 6211 LLVM_DEBUG({ 6212 if (!Size.hasValue()) 6213 dbgs() << "[H2S] Unknown allocation size (or alignment): " << *AI.CB 6214 << "\n"; 6215 else 6216 dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. " 6217 << MaxHeapToStackSize << "\n"; 6218 }); 6219 6220 AI.Status = AllocationInfo::INVALID; 6221 Changed = ChangeStatus::CHANGED; 6222 continue; 6223 } 6224 } 6225 6226 switch (AI.Status) { 6227 case AllocationInfo::STACK_DUE_TO_USE: 6228 if (UsesCheck(AI)) 6229 continue; 6230 AI.Status = AllocationInfo::STACK_DUE_TO_FREE; 6231 LLVM_FALLTHROUGH; 6232 case AllocationInfo::STACK_DUE_TO_FREE: 6233 if (FreeCheck(AI)) 6234 continue; 6235 AI.Status = AllocationInfo::INVALID; 6236 Changed = ChangeStatus::CHANGED; 6237 continue; 6238 case AllocationInfo::INVALID: 6239 llvm_unreachable("Invalid allocations should never reach this point!"); 6240 }; 6241 } 6242 6243 return Changed; 6244 } 6245 6246 /// ----------------------- Privatizable Pointers ------------------------------ 6247 struct AAPrivatizablePtrImpl : public AAPrivatizablePtr { 6248 AAPrivatizablePtrImpl(const IRPosition &IRP, Attributor &A) 6249 : AAPrivatizablePtr(IRP, A), PrivatizableType(llvm::None) {} 6250 6251 ChangeStatus indicatePessimisticFixpoint() override { 6252 AAPrivatizablePtr::indicatePessimisticFixpoint(); 6253 PrivatizableType = nullptr; 6254 return ChangeStatus::CHANGED; 6255 } 6256 6257 /// Identify the type we can chose for a private copy of the underlying 6258 /// argument. None means it is not clear yet, nullptr means there is none. 6259 virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0; 6260 6261 /// Return a privatizable type that encloses both T0 and T1. 6262 /// TODO: This is merely a stub for now as we should manage a mapping as well. 6263 Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) { 6264 if (!T0.hasValue()) 6265 return T1; 6266 if (!T1.hasValue()) 6267 return T0; 6268 if (T0 == T1) 6269 return T0; 6270 return nullptr; 6271 } 6272 6273 Optional<Type *> getPrivatizableType() const override { 6274 return PrivatizableType; 6275 } 6276 6277 const std::string getAsStr() const override { 6278 return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]"; 6279 } 6280 6281 protected: 6282 Optional<Type *> PrivatizableType; 6283 }; 6284 6285 // TODO: Do this for call site arguments (probably also other values) as well. 6286 6287 struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl { 6288 AAPrivatizablePtrArgument(const IRPosition &IRP, Attributor &A) 6289 : AAPrivatizablePtrImpl(IRP, A) {} 6290 6291 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...) 6292 Optional<Type *> identifyPrivatizableType(Attributor &A) override { 6293 // If this is a byval argument and we know all the call sites (so we can 6294 // rewrite them), there is no need to check them explicitly. 6295 bool AllCallSitesKnown; 6296 if (getIRPosition().hasAttr(Attribute::ByVal) && 6297 A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this, 6298 true, AllCallSitesKnown)) 6299 return getAssociatedValue().getType()->getPointerElementType(); 6300 6301 Optional<Type *> Ty; 6302 unsigned ArgNo = getIRPosition().getCallSiteArgNo(); 6303 6304 // Make sure the associated call site argument has the same type at all call 6305 // sites and it is an allocation we know is safe to privatize, for now that 6306 // means we only allow alloca instructions. 6307 // TODO: We can additionally analyze the accesses in the callee to create 6308 // the type from that information instead. That is a little more 6309 // involved and will be done in a follow up patch. 6310 auto CallSiteCheck = [&](AbstractCallSite ACS) { 6311 IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); 6312 // Check if a coresponding argument was found or if it is one not 6313 // associated (which can happen for callback calls). 6314 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) 6315 return false; 6316 6317 // Check that all call sites agree on a type. 6318 auto &PrivCSArgAA = 6319 A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos, DepClassTy::REQUIRED); 6320 Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType(); 6321 6322 LLVM_DEBUG({ 6323 dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; 6324 if (CSTy.hasValue() && CSTy.getValue()) 6325 CSTy.getValue()->print(dbgs()); 6326 else if (CSTy.hasValue()) 6327 dbgs() << "<nullptr>"; 6328 else 6329 dbgs() << "<none>"; 6330 }); 6331 6332 Ty = combineTypes(Ty, CSTy); 6333 6334 LLVM_DEBUG({ 6335 dbgs() << " : New Type: "; 6336 if (Ty.hasValue() && Ty.getValue()) 6337 Ty.getValue()->print(dbgs()); 6338 else if (Ty.hasValue()) 6339 dbgs() << "<nullptr>"; 6340 else 6341 dbgs() << "<none>"; 6342 dbgs() << "\n"; 6343 }); 6344 6345 return !Ty.hasValue() || Ty.getValue(); 6346 }; 6347 6348 if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown)) 6349 return nullptr; 6350 return Ty; 6351 } 6352 6353 /// See AbstractAttribute::updateImpl(...). 6354 ChangeStatus updateImpl(Attributor &A) override { 6355 PrivatizableType = identifyPrivatizableType(A); 6356 if (!PrivatizableType.hasValue()) 6357 return ChangeStatus::UNCHANGED; 6358 if (!PrivatizableType.getValue()) 6359 return indicatePessimisticFixpoint(); 6360 6361 // The dependence is optional so we don't give up once we give up on the 6362 // alignment. 6363 A.getAAFor<AAAlign>(*this, IRPosition::value(getAssociatedValue()), 6364 DepClassTy::OPTIONAL); 6365 6366 // Avoid arguments with padding for now. 6367 if (!getIRPosition().hasAttr(Attribute::ByVal) && 6368 !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(), 6369 A.getInfoCache().getDL())) { 6370 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n"); 6371 return indicatePessimisticFixpoint(); 6372 } 6373 6374 // Verify callee and caller agree on how the promoted argument would be 6375 // passed. 6376 // TODO: The use of the ArgumentPromotion interface here is ugly, we need a 6377 // specialized form of TargetTransformInfo::areFunctionArgsABICompatible 6378 // which doesn't require the arguments ArgumentPromotion wanted to pass. 6379 Function &Fn = *getIRPosition().getAnchorScope(); 6380 SmallPtrSet<Argument *, 1> ArgsToPromote, Dummy; 6381 ArgsToPromote.insert(getAssociatedArgument()); 6382 const auto *TTI = 6383 A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn); 6384 if (!TTI || 6385 !ArgumentPromotionPass::areFunctionArgsABICompatible( 6386 Fn, *TTI, ArgsToPromote, Dummy) || 6387 ArgsToPromote.empty()) { 6388 LLVM_DEBUG( 6389 dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for " 6390 << Fn.getName() << "\n"); 6391 return indicatePessimisticFixpoint(); 6392 } 6393 6394 // Collect the types that will replace the privatizable type in the function 6395 // signature. 6396 SmallVector<Type *, 16> ReplacementTypes; 6397 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes); 6398 6399 // Register a rewrite of the argument. 6400 Argument *Arg = getAssociatedArgument(); 6401 if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) { 6402 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n"); 6403 return indicatePessimisticFixpoint(); 6404 } 6405 6406 unsigned ArgNo = Arg->getArgNo(); 6407 6408 // Helper to check if for the given call site the associated argument is 6409 // passed to a callback where the privatization would be different. 6410 auto IsCompatiblePrivArgOfCallback = [&](CallBase &CB) { 6411 SmallVector<const Use *, 4> CallbackUses; 6412 AbstractCallSite::getCallbackUses(CB, CallbackUses); 6413 for (const Use *U : CallbackUses) { 6414 AbstractCallSite CBACS(U); 6415 assert(CBACS && CBACS.isCallbackCall()); 6416 for (Argument &CBArg : CBACS.getCalledFunction()->args()) { 6417 int CBArgNo = CBACS.getCallArgOperandNo(CBArg); 6418 6419 LLVM_DEBUG({ 6420 dbgs() 6421 << "[AAPrivatizablePtr] Argument " << *Arg 6422 << "check if can be privatized in the context of its parent (" 6423 << Arg->getParent()->getName() 6424 << ")\n[AAPrivatizablePtr] because it is an argument in a " 6425 "callback (" 6426 << CBArgNo << "@" << CBACS.getCalledFunction()->getName() 6427 << ")\n[AAPrivatizablePtr] " << CBArg << " : " 6428 << CBACS.getCallArgOperand(CBArg) << " vs " 6429 << CB.getArgOperand(ArgNo) << "\n" 6430 << "[AAPrivatizablePtr] " << CBArg << " : " 6431 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; 6432 }); 6433 6434 if (CBArgNo != int(ArgNo)) 6435 continue; 6436 const auto &CBArgPrivAA = A.getAAFor<AAPrivatizablePtr>( 6437 *this, IRPosition::argument(CBArg), DepClassTy::REQUIRED); 6438 if (CBArgPrivAA.isValidState()) { 6439 auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType(); 6440 if (!CBArgPrivTy.hasValue()) 6441 continue; 6442 if (CBArgPrivTy.getValue() == PrivatizableType) 6443 continue; 6444 } 6445 6446 LLVM_DEBUG({ 6447 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg 6448 << " cannot be privatized in the context of its parent (" 6449 << Arg->getParent()->getName() 6450 << ")\n[AAPrivatizablePtr] because it is an argument in a " 6451 "callback (" 6452 << CBArgNo << "@" << CBACS.getCalledFunction()->getName() 6453 << ").\n[AAPrivatizablePtr] for which the argument " 6454 "privatization is not compatible.\n"; 6455 }); 6456 return false; 6457 } 6458 } 6459 return true; 6460 }; 6461 6462 // Helper to check if for the given call site the associated argument is 6463 // passed to a direct call where the privatization would be different. 6464 auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) { 6465 CallBase *DC = cast<CallBase>(ACS.getInstruction()); 6466 int DCArgNo = ACS.getCallArgOperandNo(ArgNo); 6467 assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->getNumArgOperands() && 6468 "Expected a direct call operand for callback call operand"); 6469 6470 LLVM_DEBUG({ 6471 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg 6472 << " check if be privatized in the context of its parent (" 6473 << Arg->getParent()->getName() 6474 << ")\n[AAPrivatizablePtr] because it is an argument in a " 6475 "direct call of (" 6476 << DCArgNo << "@" << DC->getCalledFunction()->getName() 6477 << ").\n"; 6478 }); 6479 6480 Function *DCCallee = DC->getCalledFunction(); 6481 if (unsigned(DCArgNo) < DCCallee->arg_size()) { 6482 const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>( 6483 *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)), 6484 DepClassTy::REQUIRED); 6485 if (DCArgPrivAA.isValidState()) { 6486 auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType(); 6487 if (!DCArgPrivTy.hasValue()) 6488 return true; 6489 if (DCArgPrivTy.getValue() == PrivatizableType) 6490 return true; 6491 } 6492 } 6493 6494 LLVM_DEBUG({ 6495 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg 6496 << " cannot be privatized in the context of its parent (" 6497 << Arg->getParent()->getName() 6498 << ")\n[AAPrivatizablePtr] because it is an argument in a " 6499 "direct call of (" 6500 << ACS.getInstruction()->getCalledFunction()->getName() 6501 << ").\n[AAPrivatizablePtr] for which the argument " 6502 "privatization is not compatible.\n"; 6503 }); 6504 return false; 6505 }; 6506 6507 // Helper to check if the associated argument is used at the given abstract 6508 // call site in a way that is incompatible with the privatization assumed 6509 // here. 6510 auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) { 6511 if (ACS.isDirectCall()) 6512 return IsCompatiblePrivArgOfCallback(*ACS.getInstruction()); 6513 if (ACS.isCallbackCall()) 6514 return IsCompatiblePrivArgOfDirectCS(ACS); 6515 return false; 6516 }; 6517 6518 bool AllCallSitesKnown; 6519 if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true, 6520 AllCallSitesKnown)) 6521 return indicatePessimisticFixpoint(); 6522 6523 return ChangeStatus::UNCHANGED; 6524 } 6525 6526 /// Given a type to private \p PrivType, collect the constituates (which are 6527 /// used) in \p ReplacementTypes. 6528 static void 6529 identifyReplacementTypes(Type *PrivType, 6530 SmallVectorImpl<Type *> &ReplacementTypes) { 6531 // TODO: For now we expand the privatization type to the fullest which can 6532 // lead to dead arguments that need to be removed later. 6533 assert(PrivType && "Expected privatizable type!"); 6534 6535 // Traverse the type, extract constituate types on the outermost level. 6536 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { 6537 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) 6538 ReplacementTypes.push_back(PrivStructType->getElementType(u)); 6539 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { 6540 ReplacementTypes.append(PrivArrayType->getNumElements(), 6541 PrivArrayType->getElementType()); 6542 } else { 6543 ReplacementTypes.push_back(PrivType); 6544 } 6545 } 6546 6547 /// Initialize \p Base according to the type \p PrivType at position \p IP. 6548 /// The values needed are taken from the arguments of \p F starting at 6549 /// position \p ArgNo. 6550 static void createInitialization(Type *PrivType, Value &Base, Function &F, 6551 unsigned ArgNo, Instruction &IP) { 6552 assert(PrivType && "Expected privatizable type!"); 6553 6554 IRBuilder<NoFolder> IRB(&IP); 6555 const DataLayout &DL = F.getParent()->getDataLayout(); 6556 6557 // Traverse the type, build GEPs and stores. 6558 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { 6559 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType); 6560 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) { 6561 Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo(); 6562 Value *Ptr = 6563 constructPointer(PointeeTy, PrivType, &Base, 6564 PrivStructLayout->getElementOffset(u), IRB, DL); 6565 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP); 6566 } 6567 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { 6568 Type *PointeeTy = PrivArrayType->getElementType(); 6569 Type *PointeePtrTy = PointeeTy->getPointerTo(); 6570 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy); 6571 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) { 6572 Value *Ptr = constructPointer(PointeePtrTy, PrivType, &Base, 6573 u * PointeeTySize, IRB, DL); 6574 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP); 6575 } 6576 } else { 6577 new StoreInst(F.getArg(ArgNo), &Base, &IP); 6578 } 6579 } 6580 6581 /// Extract values from \p Base according to the type \p PrivType at the 6582 /// call position \p ACS. The values are appended to \p ReplacementValues. 6583 void createReplacementValues(Align Alignment, Type *PrivType, 6584 AbstractCallSite ACS, Value *Base, 6585 SmallVectorImpl<Value *> &ReplacementValues) { 6586 assert(Base && "Expected base value!"); 6587 assert(PrivType && "Expected privatizable type!"); 6588 Instruction *IP = ACS.getInstruction(); 6589 6590 IRBuilder<NoFolder> IRB(IP); 6591 const DataLayout &DL = IP->getModule()->getDataLayout(); 6592 6593 if (Base->getType()->getPointerElementType() != PrivType) 6594 Base = BitCastInst::CreateBitOrPointerCast(Base, PrivType->getPointerTo(), 6595 "", ACS.getInstruction()); 6596 6597 // Traverse the type, build GEPs and loads. 6598 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { 6599 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType); 6600 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) { 6601 Type *PointeeTy = PrivStructType->getElementType(u); 6602 Value *Ptr = 6603 constructPointer(PointeeTy->getPointerTo(), PrivType, Base, 6604 PrivStructLayout->getElementOffset(u), IRB, DL); 6605 LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP); 6606 L->setAlignment(Alignment); 6607 ReplacementValues.push_back(L); 6608 } 6609 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { 6610 Type *PointeeTy = PrivArrayType->getElementType(); 6611 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy); 6612 Type *PointeePtrTy = PointeeTy->getPointerTo(); 6613 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) { 6614 Value *Ptr = constructPointer(PointeePtrTy, PrivType, Base, 6615 u * PointeeTySize, IRB, DL); 6616 LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP); 6617 L->setAlignment(Alignment); 6618 ReplacementValues.push_back(L); 6619 } 6620 } else { 6621 LoadInst *L = new LoadInst(PrivType, Base, "", IP); 6622 L->setAlignment(Alignment); 6623 ReplacementValues.push_back(L); 6624 } 6625 } 6626 6627 /// See AbstractAttribute::manifest(...) 6628 ChangeStatus manifest(Attributor &A) override { 6629 if (!PrivatizableType.hasValue()) 6630 return ChangeStatus::UNCHANGED; 6631 assert(PrivatizableType.getValue() && "Expected privatizable type!"); 6632 6633 // Collect all tail calls in the function as we cannot allow new allocas to 6634 // escape into tail recursion. 6635 // TODO: Be smarter about new allocas escaping into tail calls. 6636 SmallVector<CallInst *, 16> TailCalls; 6637 bool UsedAssumedInformation = false; 6638 if (!A.checkForAllInstructions( 6639 [&](Instruction &I) { 6640 CallInst &CI = cast<CallInst>(I); 6641 if (CI.isTailCall()) 6642 TailCalls.push_back(&CI); 6643 return true; 6644 }, 6645 *this, {Instruction::Call}, UsedAssumedInformation)) 6646 return ChangeStatus::UNCHANGED; 6647 6648 Argument *Arg = getAssociatedArgument(); 6649 // Query AAAlign attribute for alignment of associated argument to 6650 // determine the best alignment of loads. 6651 const auto &AlignAA = 6652 A.getAAFor<AAAlign>(*this, IRPosition::value(*Arg), DepClassTy::NONE); 6653 6654 // Callback to repair the associated function. A new alloca is placed at the 6655 // beginning and initialized with the values passed through arguments. The 6656 // new alloca replaces the use of the old pointer argument. 6657 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB = 6658 [=](const Attributor::ArgumentReplacementInfo &ARI, 6659 Function &ReplacementFn, Function::arg_iterator ArgIt) { 6660 BasicBlock &EntryBB = ReplacementFn.getEntryBlock(); 6661 Instruction *IP = &*EntryBB.getFirstInsertionPt(); 6662 Instruction *AI = new AllocaInst(PrivatizableType.getValue(), 0, 6663 Arg->getName() + ".priv", IP); 6664 createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn, 6665 ArgIt->getArgNo(), *IP); 6666 6667 if (AI->getType() != Arg->getType()) 6668 AI = 6669 BitCastInst::CreateBitOrPointerCast(AI, Arg->getType(), "", IP); 6670 Arg->replaceAllUsesWith(AI); 6671 6672 for (CallInst *CI : TailCalls) 6673 CI->setTailCall(false); 6674 }; 6675 6676 // Callback to repair a call site of the associated function. The elements 6677 // of the privatizable type are loaded prior to the call and passed to the 6678 // new function version. 6679 Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB = 6680 [=, &AlignAA](const Attributor::ArgumentReplacementInfo &ARI, 6681 AbstractCallSite ACS, 6682 SmallVectorImpl<Value *> &NewArgOperands) { 6683 // When no alignment is specified for the load instruction, 6684 // natural alignment is assumed. 6685 createReplacementValues( 6686 assumeAligned(AlignAA.getAssumedAlign()), 6687 PrivatizableType.getValue(), ACS, 6688 ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()), 6689 NewArgOperands); 6690 }; 6691 6692 // Collect the types that will replace the privatizable type in the function 6693 // signature. 6694 SmallVector<Type *, 16> ReplacementTypes; 6695 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes); 6696 6697 // Register a rewrite of the argument. 6698 if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes, 6699 std::move(FnRepairCB), 6700 std::move(ACSRepairCB))) 6701 return ChangeStatus::CHANGED; 6702 return ChangeStatus::UNCHANGED; 6703 } 6704 6705 /// See AbstractAttribute::trackStatistics() 6706 void trackStatistics() const override { 6707 STATS_DECLTRACK_ARG_ATTR(privatizable_ptr); 6708 } 6709 }; 6710 6711 struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl { 6712 AAPrivatizablePtrFloating(const IRPosition &IRP, Attributor &A) 6713 : AAPrivatizablePtrImpl(IRP, A) {} 6714 6715 /// See AbstractAttribute::initialize(...). 6716 virtual void initialize(Attributor &A) override { 6717 // TODO: We can privatize more than arguments. 6718 indicatePessimisticFixpoint(); 6719 } 6720 6721 ChangeStatus updateImpl(Attributor &A) override { 6722 llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::" 6723 "updateImpl will not be called"); 6724 } 6725 6726 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...) 6727 Optional<Type *> identifyPrivatizableType(Attributor &A) override { 6728 Value *Obj = getUnderlyingObject(&getAssociatedValue()); 6729 if (!Obj) { 6730 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n"); 6731 return nullptr; 6732 } 6733 6734 if (auto *AI = dyn_cast<AllocaInst>(Obj)) 6735 if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) 6736 if (CI->isOne()) 6737 return Obj->getType()->getPointerElementType(); 6738 if (auto *Arg = dyn_cast<Argument>(Obj)) { 6739 auto &PrivArgAA = A.getAAFor<AAPrivatizablePtr>( 6740 *this, IRPosition::argument(*Arg), DepClassTy::REQUIRED); 6741 if (PrivArgAA.isAssumedPrivatizablePtr()) 6742 return Obj->getType()->getPointerElementType(); 6743 } 6744 6745 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid " 6746 "alloca nor privatizable argument: " 6747 << *Obj << "!\n"); 6748 return nullptr; 6749 } 6750 6751 /// See AbstractAttribute::trackStatistics() 6752 void trackStatistics() const override { 6753 STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr); 6754 } 6755 }; 6756 6757 struct AAPrivatizablePtrCallSiteArgument final 6758 : public AAPrivatizablePtrFloating { 6759 AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP, Attributor &A) 6760 : AAPrivatizablePtrFloating(IRP, A) {} 6761 6762 /// See AbstractAttribute::initialize(...). 6763 void initialize(Attributor &A) override { 6764 if (getIRPosition().hasAttr(Attribute::ByVal)) 6765 indicateOptimisticFixpoint(); 6766 } 6767 6768 /// See AbstractAttribute::updateImpl(...). 6769 ChangeStatus updateImpl(Attributor &A) override { 6770 PrivatizableType = identifyPrivatizableType(A); 6771 if (!PrivatizableType.hasValue()) 6772 return ChangeStatus::UNCHANGED; 6773 if (!PrivatizableType.getValue()) 6774 return indicatePessimisticFixpoint(); 6775 6776 const IRPosition &IRP = getIRPosition(); 6777 auto &NoCaptureAA = 6778 A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::REQUIRED); 6779 if (!NoCaptureAA.isAssumedNoCapture()) { 6780 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n"); 6781 return indicatePessimisticFixpoint(); 6782 } 6783 6784 auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP, DepClassTy::REQUIRED); 6785 if (!NoAliasAA.isAssumedNoAlias()) { 6786 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n"); 6787 return indicatePessimisticFixpoint(); 6788 } 6789 6790 const auto &MemBehaviorAA = 6791 A.getAAFor<AAMemoryBehavior>(*this, IRP, DepClassTy::REQUIRED); 6792 if (!MemBehaviorAA.isAssumedReadOnly()) { 6793 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n"); 6794 return indicatePessimisticFixpoint(); 6795 } 6796 6797 return ChangeStatus::UNCHANGED; 6798 } 6799 6800 /// See AbstractAttribute::trackStatistics() 6801 void trackStatistics() const override { 6802 STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr); 6803 } 6804 }; 6805 6806 struct AAPrivatizablePtrCallSiteReturned final 6807 : public AAPrivatizablePtrFloating { 6808 AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP, Attributor &A) 6809 : AAPrivatizablePtrFloating(IRP, A) {} 6810 6811 /// See AbstractAttribute::initialize(...). 6812 void initialize(Attributor &A) override { 6813 // TODO: We can privatize more than arguments. 6814 indicatePessimisticFixpoint(); 6815 } 6816 6817 /// See AbstractAttribute::trackStatistics() 6818 void trackStatistics() const override { 6819 STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr); 6820 } 6821 }; 6822 6823 struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating { 6824 AAPrivatizablePtrReturned(const IRPosition &IRP, Attributor &A) 6825 : AAPrivatizablePtrFloating(IRP, A) {} 6826 6827 /// See AbstractAttribute::initialize(...). 6828 void initialize(Attributor &A) override { 6829 // TODO: We can privatize more than arguments. 6830 indicatePessimisticFixpoint(); 6831 } 6832 6833 /// See AbstractAttribute::trackStatistics() 6834 void trackStatistics() const override { 6835 STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr); 6836 } 6837 }; 6838 6839 /// -------------------- Memory Behavior Attributes ---------------------------- 6840 /// Includes read-none, read-only, and write-only. 6841 /// ---------------------------------------------------------------------------- 6842 struct AAMemoryBehaviorImpl : public AAMemoryBehavior { 6843 AAMemoryBehaviorImpl(const IRPosition &IRP, Attributor &A) 6844 : AAMemoryBehavior(IRP, A) {} 6845 6846 /// See AbstractAttribute::initialize(...). 6847 void initialize(Attributor &A) override { 6848 intersectAssumedBits(BEST_STATE); 6849 getKnownStateFromValue(getIRPosition(), getState()); 6850 AAMemoryBehavior::initialize(A); 6851 } 6852 6853 /// Return the memory behavior information encoded in the IR for \p IRP. 6854 static void getKnownStateFromValue(const IRPosition &IRP, 6855 BitIntegerState &State, 6856 bool IgnoreSubsumingPositions = false) { 6857 SmallVector<Attribute, 2> Attrs; 6858 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); 6859 for (const Attribute &Attr : Attrs) { 6860 switch (Attr.getKindAsEnum()) { 6861 case Attribute::ReadNone: 6862 State.addKnownBits(NO_ACCESSES); 6863 break; 6864 case Attribute::ReadOnly: 6865 State.addKnownBits(NO_WRITES); 6866 break; 6867 case Attribute::WriteOnly: 6868 State.addKnownBits(NO_READS); 6869 break; 6870 default: 6871 llvm_unreachable("Unexpected attribute!"); 6872 } 6873 } 6874 6875 if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) { 6876 if (!I->mayReadFromMemory()) 6877 State.addKnownBits(NO_READS); 6878 if (!I->mayWriteToMemory()) 6879 State.addKnownBits(NO_WRITES); 6880 } 6881 } 6882 6883 /// See AbstractAttribute::getDeducedAttributes(...). 6884 void getDeducedAttributes(LLVMContext &Ctx, 6885 SmallVectorImpl<Attribute> &Attrs) const override { 6886 assert(Attrs.size() == 0); 6887 if (isAssumedReadNone()) 6888 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); 6889 else if (isAssumedReadOnly()) 6890 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly)); 6891 else if (isAssumedWriteOnly()) 6892 Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly)); 6893 assert(Attrs.size() <= 1); 6894 } 6895 6896 /// See AbstractAttribute::manifest(...). 6897 ChangeStatus manifest(Attributor &A) override { 6898 if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true)) 6899 return ChangeStatus::UNCHANGED; 6900 6901 const IRPosition &IRP = getIRPosition(); 6902 6903 // Check if we would improve the existing attributes first. 6904 SmallVector<Attribute, 4> DeducedAttrs; 6905 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); 6906 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { 6907 return IRP.hasAttr(Attr.getKindAsEnum(), 6908 /* IgnoreSubsumingPositions */ true); 6909 })) 6910 return ChangeStatus::UNCHANGED; 6911 6912 // Clear existing attributes. 6913 IRP.removeAttrs(AttrKinds); 6914 6915 // Use the generic manifest method. 6916 return IRAttribute::manifest(A); 6917 } 6918 6919 /// See AbstractState::getAsStr(). 6920 const std::string getAsStr() const override { 6921 if (isAssumedReadNone()) 6922 return "readnone"; 6923 if (isAssumedReadOnly()) 6924 return "readonly"; 6925 if (isAssumedWriteOnly()) 6926 return "writeonly"; 6927 return "may-read/write"; 6928 } 6929 6930 /// The set of IR attributes AAMemoryBehavior deals with. 6931 static const Attribute::AttrKind AttrKinds[3]; 6932 }; 6933 6934 const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = { 6935 Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly}; 6936 6937 /// Memory behavior attribute for a floating value. 6938 struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl { 6939 AAMemoryBehaviorFloating(const IRPosition &IRP, Attributor &A) 6940 : AAMemoryBehaviorImpl(IRP, A) {} 6941 6942 /// See AbstractAttribute::updateImpl(...). 6943 ChangeStatus updateImpl(Attributor &A) override; 6944 6945 /// See AbstractAttribute::trackStatistics() 6946 void trackStatistics() const override { 6947 if (isAssumedReadNone()) 6948 STATS_DECLTRACK_FLOATING_ATTR(readnone) 6949 else if (isAssumedReadOnly()) 6950 STATS_DECLTRACK_FLOATING_ATTR(readonly) 6951 else if (isAssumedWriteOnly()) 6952 STATS_DECLTRACK_FLOATING_ATTR(writeonly) 6953 } 6954 6955 private: 6956 /// Return true if users of \p UserI might access the underlying 6957 /// variable/location described by \p U and should therefore be analyzed. 6958 bool followUsersOfUseIn(Attributor &A, const Use &U, 6959 const Instruction *UserI); 6960 6961 /// Update the state according to the effect of use \p U in \p UserI. 6962 void analyzeUseIn(Attributor &A, const Use &U, const Instruction *UserI); 6963 }; 6964 6965 /// Memory behavior attribute for function argument. 6966 struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating { 6967 AAMemoryBehaviorArgument(const IRPosition &IRP, Attributor &A) 6968 : AAMemoryBehaviorFloating(IRP, A) {} 6969 6970 /// See AbstractAttribute::initialize(...). 6971 void initialize(Attributor &A) override { 6972 intersectAssumedBits(BEST_STATE); 6973 const IRPosition &IRP = getIRPosition(); 6974 // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we 6975 // can query it when we use has/getAttr. That would allow us to reuse the 6976 // initialize of the base class here. 6977 bool HasByVal = 6978 IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true); 6979 getKnownStateFromValue(IRP, getState(), 6980 /* IgnoreSubsumingPositions */ HasByVal); 6981 6982 // Initialize the use vector with all direct uses of the associated value. 6983 Argument *Arg = getAssociatedArgument(); 6984 if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent()))) 6985 indicatePessimisticFixpoint(); 6986 } 6987 6988 ChangeStatus manifest(Attributor &A) override { 6989 // TODO: Pointer arguments are not supported on vectors of pointers yet. 6990 if (!getAssociatedValue().getType()->isPointerTy()) 6991 return ChangeStatus::UNCHANGED; 6992 6993 // TODO: From readattrs.ll: "inalloca parameters are always 6994 // considered written" 6995 if (hasAttr({Attribute::InAlloca, Attribute::Preallocated})) { 6996 removeKnownBits(NO_WRITES); 6997 removeAssumedBits(NO_WRITES); 6998 } 6999 return AAMemoryBehaviorFloating::manifest(A); 7000 } 7001 7002 /// See AbstractAttribute::trackStatistics() 7003 void trackStatistics() const override { 7004 if (isAssumedReadNone()) 7005 STATS_DECLTRACK_ARG_ATTR(readnone) 7006 else if (isAssumedReadOnly()) 7007 STATS_DECLTRACK_ARG_ATTR(readonly) 7008 else if (isAssumedWriteOnly()) 7009 STATS_DECLTRACK_ARG_ATTR(writeonly) 7010 } 7011 }; 7012 7013 struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument { 7014 AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP, Attributor &A) 7015 : AAMemoryBehaviorArgument(IRP, A) {} 7016 7017 /// See AbstractAttribute::initialize(...). 7018 void initialize(Attributor &A) override { 7019 // If we don't have an associated attribute this is either a variadic call 7020 // or an indirect call, either way, nothing to do here. 7021 Argument *Arg = getAssociatedArgument(); 7022 if (!Arg) { 7023 indicatePessimisticFixpoint(); 7024 return; 7025 } 7026 if (Arg->hasByValAttr()) { 7027 addKnownBits(NO_WRITES); 7028 removeKnownBits(NO_READS); 7029 removeAssumedBits(NO_READS); 7030 } 7031 AAMemoryBehaviorArgument::initialize(A); 7032 if (getAssociatedFunction()->isDeclaration()) 7033 indicatePessimisticFixpoint(); 7034 } 7035 7036 /// See AbstractAttribute::updateImpl(...). 7037 ChangeStatus updateImpl(Attributor &A) override { 7038 // TODO: Once we have call site specific value information we can provide 7039 // call site specific liveness liveness information and then it makes 7040 // sense to specialize attributes for call sites arguments instead of 7041 // redirecting requests to the callee argument. 7042 Argument *Arg = getAssociatedArgument(); 7043 const IRPosition &ArgPos = IRPosition::argument(*Arg); 7044 auto &ArgAA = 7045 A.getAAFor<AAMemoryBehavior>(*this, ArgPos, DepClassTy::REQUIRED); 7046 return clampStateAndIndicateChange(getState(), ArgAA.getState()); 7047 } 7048 7049 /// See AbstractAttribute::trackStatistics() 7050 void trackStatistics() const override { 7051 if (isAssumedReadNone()) 7052 STATS_DECLTRACK_CSARG_ATTR(readnone) 7053 else if (isAssumedReadOnly()) 7054 STATS_DECLTRACK_CSARG_ATTR(readonly) 7055 else if (isAssumedWriteOnly()) 7056 STATS_DECLTRACK_CSARG_ATTR(writeonly) 7057 } 7058 }; 7059 7060 /// Memory behavior attribute for a call site return position. 7061 struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating { 7062 AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP, Attributor &A) 7063 : AAMemoryBehaviorFloating(IRP, A) {} 7064 7065 /// See AbstractAttribute::initialize(...). 7066 void initialize(Attributor &A) override { 7067 AAMemoryBehaviorImpl::initialize(A); 7068 Function *F = getAssociatedFunction(); 7069 if (!F || F->isDeclaration()) 7070 indicatePessimisticFixpoint(); 7071 } 7072 7073 /// See AbstractAttribute::manifest(...). 7074 ChangeStatus manifest(Attributor &A) override { 7075 // We do not annotate returned values. 7076 return ChangeStatus::UNCHANGED; 7077 } 7078 7079 /// See AbstractAttribute::trackStatistics() 7080 void trackStatistics() const override {} 7081 }; 7082 7083 /// An AA to represent the memory behavior function attributes. 7084 struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl { 7085 AAMemoryBehaviorFunction(const IRPosition &IRP, Attributor &A) 7086 : AAMemoryBehaviorImpl(IRP, A) {} 7087 7088 /// See AbstractAttribute::updateImpl(Attributor &A). 7089 virtual ChangeStatus updateImpl(Attributor &A) override; 7090 7091 /// See AbstractAttribute::manifest(...). 7092 ChangeStatus manifest(Attributor &A) override { 7093 Function &F = cast<Function>(getAnchorValue()); 7094 if (isAssumedReadNone()) { 7095 F.removeFnAttr(Attribute::ArgMemOnly); 7096 F.removeFnAttr(Attribute::InaccessibleMemOnly); 7097 F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly); 7098 } 7099 return AAMemoryBehaviorImpl::manifest(A); 7100 } 7101 7102 /// See AbstractAttribute::trackStatistics() 7103 void trackStatistics() const override { 7104 if (isAssumedReadNone()) 7105 STATS_DECLTRACK_FN_ATTR(readnone) 7106 else if (isAssumedReadOnly()) 7107 STATS_DECLTRACK_FN_ATTR(readonly) 7108 else if (isAssumedWriteOnly()) 7109 STATS_DECLTRACK_FN_ATTR(writeonly) 7110 } 7111 }; 7112 7113 /// AAMemoryBehavior attribute for call sites. 7114 struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl { 7115 AAMemoryBehaviorCallSite(const IRPosition &IRP, Attributor &A) 7116 : AAMemoryBehaviorImpl(IRP, A) {} 7117 7118 /// See AbstractAttribute::initialize(...). 7119 void initialize(Attributor &A) override { 7120 AAMemoryBehaviorImpl::initialize(A); 7121 Function *F = getAssociatedFunction(); 7122 if (!F || F->isDeclaration()) 7123 indicatePessimisticFixpoint(); 7124 } 7125 7126 /// See AbstractAttribute::updateImpl(...). 7127 ChangeStatus updateImpl(Attributor &A) override { 7128 // TODO: Once we have call site specific value information we can provide 7129 // call site specific liveness liveness information and then it makes 7130 // sense to specialize attributes for call sites arguments instead of 7131 // redirecting requests to the callee argument. 7132 Function *F = getAssociatedFunction(); 7133 const IRPosition &FnPos = IRPosition::function(*F); 7134 auto &FnAA = 7135 A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::REQUIRED); 7136 return clampStateAndIndicateChange(getState(), FnAA.getState()); 7137 } 7138 7139 /// See AbstractAttribute::trackStatistics() 7140 void trackStatistics() const override { 7141 if (isAssumedReadNone()) 7142 STATS_DECLTRACK_CS_ATTR(readnone) 7143 else if (isAssumedReadOnly()) 7144 STATS_DECLTRACK_CS_ATTR(readonly) 7145 else if (isAssumedWriteOnly()) 7146 STATS_DECLTRACK_CS_ATTR(writeonly) 7147 } 7148 }; 7149 7150 ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) { 7151 7152 // The current assumed state used to determine a change. 7153 auto AssumedState = getAssumed(); 7154 7155 auto CheckRWInst = [&](Instruction &I) { 7156 // If the instruction has an own memory behavior state, use it to restrict 7157 // the local state. No further analysis is required as the other memory 7158 // state is as optimistic as it gets. 7159 if (const auto *CB = dyn_cast<CallBase>(&I)) { 7160 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( 7161 *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED); 7162 intersectAssumedBits(MemBehaviorAA.getAssumed()); 7163 return !isAtFixpoint(); 7164 } 7165 7166 // Remove access kind modifiers if necessary. 7167 if (I.mayReadFromMemory()) 7168 removeAssumedBits(NO_READS); 7169 if (I.mayWriteToMemory()) 7170 removeAssumedBits(NO_WRITES); 7171 return !isAtFixpoint(); 7172 }; 7173 7174 bool UsedAssumedInformation = false; 7175 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this, 7176 UsedAssumedInformation)) 7177 return indicatePessimisticFixpoint(); 7178 7179 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED 7180 : ChangeStatus::UNCHANGED; 7181 } 7182 7183 ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) { 7184 7185 const IRPosition &IRP = getIRPosition(); 7186 const IRPosition &FnPos = IRPosition::function_scope(IRP); 7187 AAMemoryBehavior::StateType &S = getState(); 7188 7189 // First, check the function scope. We take the known information and we avoid 7190 // work if the assumed information implies the current assumed information for 7191 // this attribute. This is a valid for all but byval arguments. 7192 Argument *Arg = IRP.getAssociatedArgument(); 7193 AAMemoryBehavior::base_t FnMemAssumedState = 7194 AAMemoryBehavior::StateType::getWorstState(); 7195 if (!Arg || !Arg->hasByValAttr()) { 7196 const auto &FnMemAA = 7197 A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::OPTIONAL); 7198 FnMemAssumedState = FnMemAA.getAssumed(); 7199 S.addKnownBits(FnMemAA.getKnown()); 7200 if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed()) 7201 return ChangeStatus::UNCHANGED; 7202 } 7203 7204 // The current assumed state used to determine a change. 7205 auto AssumedState = S.getAssumed(); 7206 7207 // Make sure the value is not captured (except through "return"), if 7208 // it is, any information derived would be irrelevant anyway as we cannot 7209 // check the potential aliases introduced by the capture. However, no need 7210 // to fall back to anythign less optimistic than the function state. 7211 const auto &ArgNoCaptureAA = 7212 A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::OPTIONAL); 7213 if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { 7214 S.intersectAssumedBits(FnMemAssumedState); 7215 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED 7216 : ChangeStatus::UNCHANGED; 7217 } 7218 7219 // Visit and expand uses until all are analyzed or a fixpoint is reached. 7220 auto UsePred = [&](const Use &U, bool &Follow) -> bool { 7221 Instruction *UserI = cast<Instruction>(U.getUser()); 7222 LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << *U << " in " << *UserI 7223 << " \n"); 7224 7225 // Droppable users, e.g., llvm::assume does not actually perform any action. 7226 if (UserI->isDroppable()) 7227 return true; 7228 7229 // Check if the users of UserI should also be visited. 7230 Follow = followUsersOfUseIn(A, U, UserI); 7231 7232 // If UserI might touch memory we analyze the use in detail. 7233 if (UserI->mayReadOrWriteMemory()) 7234 analyzeUseIn(A, U, UserI); 7235 7236 return !isAtFixpoint(); 7237 }; 7238 7239 if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) 7240 return indicatePessimisticFixpoint(); 7241 7242 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED 7243 : ChangeStatus::UNCHANGED; 7244 } 7245 7246 bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use &U, 7247 const Instruction *UserI) { 7248 // The loaded value is unrelated to the pointer argument, no need to 7249 // follow the users of the load. 7250 if (isa<LoadInst>(UserI)) 7251 return false; 7252 7253 // By default we follow all uses assuming UserI might leak information on U, 7254 // we have special handling for call sites operands though. 7255 const auto *CB = dyn_cast<CallBase>(UserI); 7256 if (!CB || !CB->isArgOperand(&U)) 7257 return true; 7258 7259 // If the use is a call argument known not to be captured, the users of 7260 // the call do not need to be visited because they have to be unrelated to 7261 // the input. Note that this check is not trivial even though we disallow 7262 // general capturing of the underlying argument. The reason is that the 7263 // call might the argument "through return", which we allow and for which we 7264 // need to check call users. 7265 if (U.get()->getType()->isPointerTy()) { 7266 unsigned ArgNo = CB->getArgOperandNo(&U); 7267 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>( 7268 *this, IRPosition::callsite_argument(*CB, ArgNo), DepClassTy::OPTIONAL); 7269 return !ArgNoCaptureAA.isAssumedNoCapture(); 7270 } 7271 7272 return true; 7273 } 7274 7275 void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use &U, 7276 const Instruction *UserI) { 7277 assert(UserI->mayReadOrWriteMemory()); 7278 7279 switch (UserI->getOpcode()) { 7280 default: 7281 // TODO: Handle all atomics and other side-effect operations we know of. 7282 break; 7283 case Instruction::Load: 7284 // Loads cause the NO_READS property to disappear. 7285 removeAssumedBits(NO_READS); 7286 return; 7287 7288 case Instruction::Store: 7289 // Stores cause the NO_WRITES property to disappear if the use is the 7290 // pointer operand. Note that we do assume that capturing was taken care of 7291 // somewhere else. 7292 if (cast<StoreInst>(UserI)->getPointerOperand() == U.get()) 7293 removeAssumedBits(NO_WRITES); 7294 return; 7295 7296 case Instruction::Call: 7297 case Instruction::CallBr: 7298 case Instruction::Invoke: { 7299 // For call sites we look at the argument memory behavior attribute (this 7300 // could be recursive!) in order to restrict our own state. 7301 const auto *CB = cast<CallBase>(UserI); 7302 7303 // Give up on operand bundles. 7304 if (CB->isBundleOperand(&U)) { 7305 indicatePessimisticFixpoint(); 7306 return; 7307 } 7308 7309 // Calling a function does read the function pointer, maybe write it if the 7310 // function is self-modifying. 7311 if (CB->isCallee(&U)) { 7312 removeAssumedBits(NO_READS); 7313 break; 7314 } 7315 7316 // Adjust the possible access behavior based on the information on the 7317 // argument. 7318 IRPosition Pos; 7319 if (U.get()->getType()->isPointerTy()) 7320 Pos = IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U)); 7321 else 7322 Pos = IRPosition::callsite_function(*CB); 7323 const auto &MemBehaviorAA = 7324 A.getAAFor<AAMemoryBehavior>(*this, Pos, DepClassTy::OPTIONAL); 7325 // "assumed" has at most the same bits as the MemBehaviorAA assumed 7326 // and at least "known". 7327 intersectAssumedBits(MemBehaviorAA.getAssumed()); 7328 return; 7329 } 7330 }; 7331 7332 // Generally, look at the "may-properties" and adjust the assumed state if we 7333 // did not trigger special handling before. 7334 if (UserI->mayReadFromMemory()) 7335 removeAssumedBits(NO_READS); 7336 if (UserI->mayWriteToMemory()) 7337 removeAssumedBits(NO_WRITES); 7338 } 7339 7340 /// -------------------- Memory Locations Attributes --------------------------- 7341 /// Includes read-none, argmemonly, inaccessiblememonly, 7342 /// inaccessiblememorargmemonly 7343 /// ---------------------------------------------------------------------------- 7344 7345 std::string AAMemoryLocation::getMemoryLocationsAsStr( 7346 AAMemoryLocation::MemoryLocationsKind MLK) { 7347 if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS)) 7348 return "all memory"; 7349 if (MLK == AAMemoryLocation::NO_LOCATIONS) 7350 return "no memory"; 7351 std::string S = "memory:"; 7352 if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM)) 7353 S += "stack,"; 7354 if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM)) 7355 S += "constant,"; 7356 if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM)) 7357 S += "internal global,"; 7358 if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM)) 7359 S += "external global,"; 7360 if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM)) 7361 S += "argument,"; 7362 if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM)) 7363 S += "inaccessible,"; 7364 if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM)) 7365 S += "malloced,"; 7366 if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM)) 7367 S += "unknown,"; 7368 S.pop_back(); 7369 return S; 7370 } 7371 7372 namespace { 7373 struct AAMemoryLocationImpl : public AAMemoryLocation { 7374 7375 AAMemoryLocationImpl(const IRPosition &IRP, Attributor &A) 7376 : AAMemoryLocation(IRP, A), Allocator(A.Allocator) { 7377 for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u) 7378 AccessKind2Accesses[u] = nullptr; 7379 } 7380 7381 ~AAMemoryLocationImpl() { 7382 // The AccessSets are allocated via a BumpPtrAllocator, we call 7383 // the destructor manually. 7384 for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u) 7385 if (AccessKind2Accesses[u]) 7386 AccessKind2Accesses[u]->~AccessSet(); 7387 } 7388 7389 /// See AbstractAttribute::initialize(...). 7390 void initialize(Attributor &A) override { 7391 intersectAssumedBits(BEST_STATE); 7392 getKnownStateFromValue(A, getIRPosition(), getState()); 7393 AAMemoryLocation::initialize(A); 7394 } 7395 7396 /// Return the memory behavior information encoded in the IR for \p IRP. 7397 static void getKnownStateFromValue(Attributor &A, const IRPosition &IRP, 7398 BitIntegerState &State, 7399 bool IgnoreSubsumingPositions = false) { 7400 // For internal functions we ignore `argmemonly` and 7401 // `inaccessiblememorargmemonly` as we might break it via interprocedural 7402 // constant propagation. It is unclear if this is the best way but it is 7403 // unlikely this will cause real performance problems. If we are deriving 7404 // attributes for the anchor function we even remove the attribute in 7405 // addition to ignoring it. 7406 bool UseArgMemOnly = true; 7407 Function *AnchorFn = IRP.getAnchorScope(); 7408 if (AnchorFn && A.isRunOn(*AnchorFn)) 7409 UseArgMemOnly = !AnchorFn->hasLocalLinkage(); 7410 7411 SmallVector<Attribute, 2> Attrs; 7412 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); 7413 for (const Attribute &Attr : Attrs) { 7414 switch (Attr.getKindAsEnum()) { 7415 case Attribute::ReadNone: 7416 State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM); 7417 break; 7418 case Attribute::InaccessibleMemOnly: 7419 State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true)); 7420 break; 7421 case Attribute::ArgMemOnly: 7422 if (UseArgMemOnly) 7423 State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true)); 7424 else 7425 IRP.removeAttrs({Attribute::ArgMemOnly}); 7426 break; 7427 case Attribute::InaccessibleMemOrArgMemOnly: 7428 if (UseArgMemOnly) 7429 State.addKnownBits(inverseLocation( 7430 NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true)); 7431 else 7432 IRP.removeAttrs({Attribute::InaccessibleMemOrArgMemOnly}); 7433 break; 7434 default: 7435 llvm_unreachable("Unexpected attribute!"); 7436 } 7437 } 7438 } 7439 7440 /// See AbstractAttribute::getDeducedAttributes(...). 7441 void getDeducedAttributes(LLVMContext &Ctx, 7442 SmallVectorImpl<Attribute> &Attrs) const override { 7443 assert(Attrs.size() == 0); 7444 if (isAssumedReadNone()) { 7445 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); 7446 } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) { 7447 if (isAssumedInaccessibleMemOnly()) 7448 Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly)); 7449 else if (isAssumedArgMemOnly()) 7450 Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly)); 7451 else if (isAssumedInaccessibleOrArgMemOnly()) 7452 Attrs.push_back( 7453 Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly)); 7454 } 7455 assert(Attrs.size() <= 1); 7456 } 7457 7458 /// See AbstractAttribute::manifest(...). 7459 ChangeStatus manifest(Attributor &A) override { 7460 const IRPosition &IRP = getIRPosition(); 7461 7462 // Check if we would improve the existing attributes first. 7463 SmallVector<Attribute, 4> DeducedAttrs; 7464 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); 7465 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { 7466 return IRP.hasAttr(Attr.getKindAsEnum(), 7467 /* IgnoreSubsumingPositions */ true); 7468 })) 7469 return ChangeStatus::UNCHANGED; 7470 7471 // Clear existing attributes. 7472 IRP.removeAttrs(AttrKinds); 7473 if (isAssumedReadNone()) 7474 IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds); 7475 7476 // Use the generic manifest method. 7477 return IRAttribute::manifest(A); 7478 } 7479 7480 /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...). 7481 bool checkForAllAccessesToMemoryKind( 7482 function_ref<bool(const Instruction *, const Value *, AccessKind, 7483 MemoryLocationsKind)> 7484 Pred, 7485 MemoryLocationsKind RequestedMLK) const override { 7486 if (!isValidState()) 7487 return false; 7488 7489 MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation(); 7490 if (AssumedMLK == NO_LOCATIONS) 7491 return true; 7492 7493 unsigned Idx = 0; 7494 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; 7495 CurMLK *= 2, ++Idx) { 7496 if (CurMLK & RequestedMLK) 7497 continue; 7498 7499 if (const AccessSet *Accesses = AccessKind2Accesses[Idx]) 7500 for (const AccessInfo &AI : *Accesses) 7501 if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK)) 7502 return false; 7503 } 7504 7505 return true; 7506 } 7507 7508 ChangeStatus indicatePessimisticFixpoint() override { 7509 // If we give up and indicate a pessimistic fixpoint this instruction will 7510 // become an access for all potential access kinds: 7511 // TODO: Add pointers for argmemonly and globals to improve the results of 7512 // checkForAllAccessesToMemoryKind. 7513 bool Changed = false; 7514 MemoryLocationsKind KnownMLK = getKnown(); 7515 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()); 7516 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) 7517 if (!(CurMLK & KnownMLK)) 7518 updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed, 7519 getAccessKindFromInst(I)); 7520 return AAMemoryLocation::indicatePessimisticFixpoint(); 7521 } 7522 7523 protected: 7524 /// Helper struct to tie together an instruction that has a read or write 7525 /// effect with the pointer it accesses (if any). 7526 struct AccessInfo { 7527 7528 /// The instruction that caused the access. 7529 const Instruction *I; 7530 7531 /// The base pointer that is accessed, or null if unknown. 7532 const Value *Ptr; 7533 7534 /// The kind of access (read/write/read+write). 7535 AccessKind Kind; 7536 7537 bool operator==(const AccessInfo &RHS) const { 7538 return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind; 7539 } 7540 bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const { 7541 if (LHS.I != RHS.I) 7542 return LHS.I < RHS.I; 7543 if (LHS.Ptr != RHS.Ptr) 7544 return LHS.Ptr < RHS.Ptr; 7545 if (LHS.Kind != RHS.Kind) 7546 return LHS.Kind < RHS.Kind; 7547 return false; 7548 } 7549 }; 7550 7551 /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the 7552 /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind. 7553 using AccessSet = SmallSet<AccessInfo, 2, AccessInfo>; 7554 AccessSet *AccessKind2Accesses[llvm::CTLog2<VALID_STATE>()]; 7555 7556 /// Categorize the pointer arguments of CB that might access memory in 7557 /// AccessedLoc and update the state and access map accordingly. 7558 void 7559 categorizeArgumentPointerLocations(Attributor &A, CallBase &CB, 7560 AAMemoryLocation::StateType &AccessedLocs, 7561 bool &Changed); 7562 7563 /// Return the kind(s) of location that may be accessed by \p V. 7564 AAMemoryLocation::MemoryLocationsKind 7565 categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed); 7566 7567 /// Return the access kind as determined by \p I. 7568 AccessKind getAccessKindFromInst(const Instruction *I) { 7569 AccessKind AK = READ_WRITE; 7570 if (I) { 7571 AK = I->mayReadFromMemory() ? READ : NONE; 7572 AK = AccessKind(AK | (I->mayWriteToMemory() ? WRITE : NONE)); 7573 } 7574 return AK; 7575 } 7576 7577 /// Update the state \p State and the AccessKind2Accesses given that \p I is 7578 /// an access of kind \p AK to a \p MLK memory location with the access 7579 /// pointer \p Ptr. 7580 void updateStateAndAccessesMap(AAMemoryLocation::StateType &State, 7581 MemoryLocationsKind MLK, const Instruction *I, 7582 const Value *Ptr, bool &Changed, 7583 AccessKind AK = READ_WRITE) { 7584 7585 assert(isPowerOf2_32(MLK) && "Expected a single location set!"); 7586 auto *&Accesses = AccessKind2Accesses[llvm::Log2_32(MLK)]; 7587 if (!Accesses) 7588 Accesses = new (Allocator) AccessSet(); 7589 Changed |= Accesses->insert(AccessInfo{I, Ptr, AK}).second; 7590 State.removeAssumedBits(MLK); 7591 } 7592 7593 /// Determine the underlying locations kinds for \p Ptr, e.g., globals or 7594 /// arguments, and update the state and access map accordingly. 7595 void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr, 7596 AAMemoryLocation::StateType &State, bool &Changed); 7597 7598 /// Used to allocate access sets. 7599 BumpPtrAllocator &Allocator; 7600 7601 /// The set of IR attributes AAMemoryLocation deals with. 7602 static const Attribute::AttrKind AttrKinds[4]; 7603 }; 7604 7605 const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = { 7606 Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly, 7607 Attribute::InaccessibleMemOrArgMemOnly}; 7608 7609 void AAMemoryLocationImpl::categorizePtrValue( 7610 Attributor &A, const Instruction &I, const Value &Ptr, 7611 AAMemoryLocation::StateType &State, bool &Changed) { 7612 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for " 7613 << Ptr << " [" 7614 << getMemoryLocationsAsStr(State.getAssumed()) << "]\n"); 7615 7616 SmallVector<Value *, 8> Objects; 7617 if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, *this, &I)) { 7618 LLVM_DEBUG( 7619 dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n"); 7620 updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed, 7621 getAccessKindFromInst(&I)); 7622 return; 7623 } 7624 7625 for (Value *Obj : Objects) { 7626 // TODO: recognize the TBAA used for constant accesses. 7627 MemoryLocationsKind MLK = NO_LOCATIONS; 7628 assert(!isa<GEPOperator>(Obj) && "GEPs should have been stripped."); 7629 if (isa<UndefValue>(Obj)) 7630 continue; 7631 if (auto *Arg = dyn_cast<Argument>(Obj)) { 7632 if (Arg->hasByValAttr()) 7633 MLK = NO_LOCAL_MEM; 7634 else 7635 MLK = NO_ARGUMENT_MEM; 7636 } else if (auto *GV = dyn_cast<GlobalValue>(Obj)) { 7637 // Reading constant memory is not treated as a read "effect" by the 7638 // function attr pass so we won't neither. Constants defined by TBAA are 7639 // similar. (We know we do not write it because it is constant.) 7640 if (auto *GVar = dyn_cast<GlobalVariable>(GV)) 7641 if (GVar->isConstant()) 7642 continue; 7643 7644 if (GV->hasLocalLinkage()) 7645 MLK = NO_GLOBAL_INTERNAL_MEM; 7646 else 7647 MLK = NO_GLOBAL_EXTERNAL_MEM; 7648 } else if (isa<ConstantPointerNull>(Obj) && 7649 !NullPointerIsDefined(getAssociatedFunction(), 7650 Ptr.getType()->getPointerAddressSpace())) { 7651 continue; 7652 } else if (isa<AllocaInst>(Obj)) { 7653 MLK = NO_LOCAL_MEM; 7654 } else if (const auto *CB = dyn_cast<CallBase>(Obj)) { 7655 const auto &NoAliasAA = A.getAAFor<AANoAlias>( 7656 *this, IRPosition::callsite_returned(*CB), DepClassTy::OPTIONAL); 7657 if (NoAliasAA.isAssumedNoAlias()) 7658 MLK = NO_MALLOCED_MEM; 7659 else 7660 MLK = NO_UNKOWN_MEM; 7661 } else { 7662 MLK = NO_UNKOWN_MEM; 7663 } 7664 7665 assert(MLK != NO_LOCATIONS && "No location specified!"); 7666 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value can be categorized: " 7667 << *Obj << " -> " << getMemoryLocationsAsStr(MLK) 7668 << "\n"); 7669 updateStateAndAccessesMap(getState(), MLK, &I, Obj, Changed, 7670 getAccessKindFromInst(&I)); 7671 } 7672 7673 LLVM_DEBUG( 7674 dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: " 7675 << getMemoryLocationsAsStr(State.getAssumed()) << "\n"); 7676 } 7677 7678 void AAMemoryLocationImpl::categorizeArgumentPointerLocations( 7679 Attributor &A, CallBase &CB, AAMemoryLocation::StateType &AccessedLocs, 7680 bool &Changed) { 7681 for (unsigned ArgNo = 0, E = CB.getNumArgOperands(); ArgNo < E; ++ArgNo) { 7682 7683 // Skip non-pointer arguments. 7684 const Value *ArgOp = CB.getArgOperand(ArgNo); 7685 if (!ArgOp->getType()->isPtrOrPtrVectorTy()) 7686 continue; 7687 7688 // Skip readnone arguments. 7689 const IRPosition &ArgOpIRP = IRPosition::callsite_argument(CB, ArgNo); 7690 const auto &ArgOpMemLocationAA = 7691 A.getAAFor<AAMemoryBehavior>(*this, ArgOpIRP, DepClassTy::OPTIONAL); 7692 7693 if (ArgOpMemLocationAA.isAssumedReadNone()) 7694 continue; 7695 7696 // Categorize potentially accessed pointer arguments as if there was an 7697 // access instruction with them as pointer. 7698 categorizePtrValue(A, CB, *ArgOp, AccessedLocs, Changed); 7699 } 7700 } 7701 7702 AAMemoryLocation::MemoryLocationsKind 7703 AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I, 7704 bool &Changed) { 7705 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for " 7706 << I << "\n"); 7707 7708 AAMemoryLocation::StateType AccessedLocs; 7709 AccessedLocs.intersectAssumedBits(NO_LOCATIONS); 7710 7711 if (auto *CB = dyn_cast<CallBase>(&I)) { 7712 7713 // First check if we assume any memory is access is visible. 7714 const auto &CBMemLocationAA = A.getAAFor<AAMemoryLocation>( 7715 *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL); 7716 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I 7717 << " [" << CBMemLocationAA << "]\n"); 7718 7719 if (CBMemLocationAA.isAssumedReadNone()) 7720 return NO_LOCATIONS; 7721 7722 if (CBMemLocationAA.isAssumedInaccessibleMemOnly()) { 7723 updateStateAndAccessesMap(AccessedLocs, NO_INACCESSIBLE_MEM, &I, nullptr, 7724 Changed, getAccessKindFromInst(&I)); 7725 return AccessedLocs.getAssumed(); 7726 } 7727 7728 uint32_t CBAssumedNotAccessedLocs = 7729 CBMemLocationAA.getAssumedNotAccessedLocation(); 7730 7731 // Set the argmemonly and global bit as we handle them separately below. 7732 uint32_t CBAssumedNotAccessedLocsNoArgMem = 7733 CBAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM; 7734 7735 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) { 7736 if (CBAssumedNotAccessedLocsNoArgMem & CurMLK) 7737 continue; 7738 updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed, 7739 getAccessKindFromInst(&I)); 7740 } 7741 7742 // Now handle global memory if it might be accessed. This is slightly tricky 7743 // as NO_GLOBAL_MEM has multiple bits set. 7744 bool HasGlobalAccesses = ((~CBAssumedNotAccessedLocs) & NO_GLOBAL_MEM); 7745 if (HasGlobalAccesses) { 7746 auto AccessPred = [&](const Instruction *, const Value *Ptr, 7747 AccessKind Kind, MemoryLocationsKind MLK) { 7748 updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed, 7749 getAccessKindFromInst(&I)); 7750 return true; 7751 }; 7752 if (!CBMemLocationAA.checkForAllAccessesToMemoryKind( 7753 AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false))) 7754 return AccessedLocs.getWorstState(); 7755 } 7756 7757 LLVM_DEBUG( 7758 dbgs() << "[AAMemoryLocation] Accessed state before argument handling: " 7759 << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"); 7760 7761 // Now handle argument memory if it might be accessed. 7762 bool HasArgAccesses = ((~CBAssumedNotAccessedLocs) & NO_ARGUMENT_MEM); 7763 if (HasArgAccesses) 7764 categorizeArgumentPointerLocations(A, *CB, AccessedLocs, Changed); 7765 7766 LLVM_DEBUG( 7767 dbgs() << "[AAMemoryLocation] Accessed state after argument handling: " 7768 << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"); 7769 7770 return AccessedLocs.getAssumed(); 7771 } 7772 7773 if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) { 7774 LLVM_DEBUG( 7775 dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: " 7776 << I << " [" << *Ptr << "]\n"); 7777 categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed); 7778 return AccessedLocs.getAssumed(); 7779 } 7780 7781 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: " 7782 << I << "\n"); 7783 updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed, 7784 getAccessKindFromInst(&I)); 7785 return AccessedLocs.getAssumed(); 7786 } 7787 7788 /// An AA to represent the memory behavior function attributes. 7789 struct AAMemoryLocationFunction final : public AAMemoryLocationImpl { 7790 AAMemoryLocationFunction(const IRPosition &IRP, Attributor &A) 7791 : AAMemoryLocationImpl(IRP, A) {} 7792 7793 /// See AbstractAttribute::updateImpl(Attributor &A). 7794 virtual ChangeStatus updateImpl(Attributor &A) override { 7795 7796 const auto &MemBehaviorAA = 7797 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE); 7798 if (MemBehaviorAA.isAssumedReadNone()) { 7799 if (MemBehaviorAA.isKnownReadNone()) 7800 return indicateOptimisticFixpoint(); 7801 assert(isAssumedReadNone() && 7802 "AAMemoryLocation was not read-none but AAMemoryBehavior was!"); 7803 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); 7804 return ChangeStatus::UNCHANGED; 7805 } 7806 7807 // The current assumed state used to determine a change. 7808 auto AssumedState = getAssumed(); 7809 bool Changed = false; 7810 7811 auto CheckRWInst = [&](Instruction &I) { 7812 MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed); 7813 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I 7814 << ": " << getMemoryLocationsAsStr(MLK) << "\n"); 7815 removeAssumedBits(inverseLocation(MLK, false, false)); 7816 // Stop once only the valid bit set in the *not assumed location*, thus 7817 // once we don't actually exclude any memory locations in the state. 7818 return getAssumedNotAccessedLocation() != VALID_STATE; 7819 }; 7820 7821 bool UsedAssumedInformation = false; 7822 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this, 7823 UsedAssumedInformation)) 7824 return indicatePessimisticFixpoint(); 7825 7826 Changed |= AssumedState != getAssumed(); 7827 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 7828 } 7829 7830 /// See AbstractAttribute::trackStatistics() 7831 void trackStatistics() const override { 7832 if (isAssumedReadNone()) 7833 STATS_DECLTRACK_FN_ATTR(readnone) 7834 else if (isAssumedArgMemOnly()) 7835 STATS_DECLTRACK_FN_ATTR(argmemonly) 7836 else if (isAssumedInaccessibleMemOnly()) 7837 STATS_DECLTRACK_FN_ATTR(inaccessiblememonly) 7838 else if (isAssumedInaccessibleOrArgMemOnly()) 7839 STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly) 7840 } 7841 }; 7842 7843 /// AAMemoryLocation attribute for call sites. 7844 struct AAMemoryLocationCallSite final : AAMemoryLocationImpl { 7845 AAMemoryLocationCallSite(const IRPosition &IRP, Attributor &A) 7846 : AAMemoryLocationImpl(IRP, A) {} 7847 7848 /// See AbstractAttribute::initialize(...). 7849 void initialize(Attributor &A) override { 7850 AAMemoryLocationImpl::initialize(A); 7851 Function *F = getAssociatedFunction(); 7852 if (!F || F->isDeclaration()) 7853 indicatePessimisticFixpoint(); 7854 } 7855 7856 /// See AbstractAttribute::updateImpl(...). 7857 ChangeStatus updateImpl(Attributor &A) override { 7858 // TODO: Once we have call site specific value information we can provide 7859 // call site specific liveness liveness information and then it makes 7860 // sense to specialize attributes for call sites arguments instead of 7861 // redirecting requests to the callee argument. 7862 Function *F = getAssociatedFunction(); 7863 const IRPosition &FnPos = IRPosition::function(*F); 7864 auto &FnAA = 7865 A.getAAFor<AAMemoryLocation>(*this, FnPos, DepClassTy::REQUIRED); 7866 bool Changed = false; 7867 auto AccessPred = [&](const Instruction *I, const Value *Ptr, 7868 AccessKind Kind, MemoryLocationsKind MLK) { 7869 updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed, 7870 getAccessKindFromInst(I)); 7871 return true; 7872 }; 7873 if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS)) 7874 return indicatePessimisticFixpoint(); 7875 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 7876 } 7877 7878 /// See AbstractAttribute::trackStatistics() 7879 void trackStatistics() const override { 7880 if (isAssumedReadNone()) 7881 STATS_DECLTRACK_CS_ATTR(readnone) 7882 } 7883 }; 7884 7885 /// ------------------ Value Constant Range Attribute ------------------------- 7886 7887 struct AAValueConstantRangeImpl : AAValueConstantRange { 7888 using StateType = IntegerRangeState; 7889 AAValueConstantRangeImpl(const IRPosition &IRP, Attributor &A) 7890 : AAValueConstantRange(IRP, A) {} 7891 7892 /// See AbstractAttribute::initialize(..). 7893 void initialize(Attributor &A) override { 7894 if (A.hasSimplificationCallback(getIRPosition())) { 7895 indicatePessimisticFixpoint(); 7896 return; 7897 } 7898 7899 // Intersect a range given by SCEV. 7900 intersectKnown(getConstantRangeFromSCEV(A, getCtxI())); 7901 7902 // Intersect a range given by LVI. 7903 intersectKnown(getConstantRangeFromLVI(A, getCtxI())); 7904 } 7905 7906 /// See AbstractAttribute::getAsStr(). 7907 const std::string getAsStr() const override { 7908 std::string Str; 7909 llvm::raw_string_ostream OS(Str); 7910 OS << "range(" << getBitWidth() << ")<"; 7911 getKnown().print(OS); 7912 OS << " / "; 7913 getAssumed().print(OS); 7914 OS << ">"; 7915 return OS.str(); 7916 } 7917 7918 /// Helper function to get a SCEV expr for the associated value at program 7919 /// point \p I. 7920 const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const { 7921 if (!getAnchorScope()) 7922 return nullptr; 7923 7924 ScalarEvolution *SE = 7925 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>( 7926 *getAnchorScope()); 7927 7928 LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>( 7929 *getAnchorScope()); 7930 7931 if (!SE || !LI) 7932 return nullptr; 7933 7934 const SCEV *S = SE->getSCEV(&getAssociatedValue()); 7935 if (!I) 7936 return S; 7937 7938 return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent())); 7939 } 7940 7941 /// Helper function to get a range from SCEV for the associated value at 7942 /// program point \p I. 7943 ConstantRange getConstantRangeFromSCEV(Attributor &A, 7944 const Instruction *I = nullptr) const { 7945 if (!getAnchorScope()) 7946 return getWorstState(getBitWidth()); 7947 7948 ScalarEvolution *SE = 7949 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>( 7950 *getAnchorScope()); 7951 7952 const SCEV *S = getSCEV(A, I); 7953 if (!SE || !S) 7954 return getWorstState(getBitWidth()); 7955 7956 return SE->getUnsignedRange(S); 7957 } 7958 7959 /// Helper function to get a range from LVI for the associated value at 7960 /// program point \p I. 7961 ConstantRange 7962 getConstantRangeFromLVI(Attributor &A, 7963 const Instruction *CtxI = nullptr) const { 7964 if (!getAnchorScope()) 7965 return getWorstState(getBitWidth()); 7966 7967 LazyValueInfo *LVI = 7968 A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>( 7969 *getAnchorScope()); 7970 7971 if (!LVI || !CtxI) 7972 return getWorstState(getBitWidth()); 7973 return LVI->getConstantRange(&getAssociatedValue(), 7974 const_cast<Instruction *>(CtxI)); 7975 } 7976 7977 /// Return true if \p CtxI is valid for querying outside analyses. 7978 /// This basically makes sure we do not ask intra-procedural analysis 7979 /// about a context in the wrong function or a context that violates 7980 /// dominance assumptions they might have. The \p AllowAACtxI flag indicates 7981 /// if the original context of this AA is OK or should be considered invalid. 7982 bool isValidCtxInstructionForOutsideAnalysis(Attributor &A, 7983 const Instruction *CtxI, 7984 bool AllowAACtxI) const { 7985 if (!CtxI || (!AllowAACtxI && CtxI == getCtxI())) 7986 return false; 7987 7988 // Our context might be in a different function, neither intra-procedural 7989 // analysis (ScalarEvolution nor LazyValueInfo) can handle that. 7990 if (!AA::isValidInScope(getAssociatedValue(), CtxI->getFunction())) 7991 return false; 7992 7993 // If the context is not dominated by the value there are paths to the 7994 // context that do not define the value. This cannot be handled by 7995 // LazyValueInfo so we need to bail. 7996 if (auto *I = dyn_cast<Instruction>(&getAssociatedValue())) { 7997 InformationCache &InfoCache = A.getInfoCache(); 7998 const DominatorTree *DT = 7999 InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>( 8000 *I->getFunction()); 8001 return DT && DT->dominates(I, CtxI); 8002 } 8003 8004 return true; 8005 } 8006 8007 /// See AAValueConstantRange::getKnownConstantRange(..). 8008 ConstantRange 8009 getKnownConstantRange(Attributor &A, 8010 const Instruction *CtxI = nullptr) const override { 8011 if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI, 8012 /* AllowAACtxI */ false)) 8013 return getKnown(); 8014 8015 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI); 8016 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI); 8017 return getKnown().intersectWith(SCEVR).intersectWith(LVIR); 8018 } 8019 8020 /// See AAValueConstantRange::getAssumedConstantRange(..). 8021 ConstantRange 8022 getAssumedConstantRange(Attributor &A, 8023 const Instruction *CtxI = nullptr) const override { 8024 // TODO: Make SCEV use Attributor assumption. 8025 // We may be able to bound a variable range via assumptions in 8026 // Attributor. ex.) If x is assumed to be in [1, 3] and y is known to 8027 // evolve to x^2 + x, then we can say that y is in [2, 12]. 8028 if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI, 8029 /* AllowAACtxI */ false)) 8030 return getAssumed(); 8031 8032 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI); 8033 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI); 8034 return getAssumed().intersectWith(SCEVR).intersectWith(LVIR); 8035 } 8036 8037 /// Helper function to create MDNode for range metadata. 8038 static MDNode * 8039 getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx, 8040 const ConstantRange &AssumedConstantRange) { 8041 Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get( 8042 Ty, AssumedConstantRange.getLower())), 8043 ConstantAsMetadata::get(ConstantInt::get( 8044 Ty, AssumedConstantRange.getUpper()))}; 8045 return MDNode::get(Ctx, LowAndHigh); 8046 } 8047 8048 /// Return true if \p Assumed is included in \p KnownRanges. 8049 static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) { 8050 8051 if (Assumed.isFullSet()) 8052 return false; 8053 8054 if (!KnownRanges) 8055 return true; 8056 8057 // If multiple ranges are annotated in IR, we give up to annotate assumed 8058 // range for now. 8059 8060 // TODO: If there exists a known range which containts assumed range, we 8061 // can say assumed range is better. 8062 if (KnownRanges->getNumOperands() > 2) 8063 return false; 8064 8065 ConstantInt *Lower = 8066 mdconst::extract<ConstantInt>(KnownRanges->getOperand(0)); 8067 ConstantInt *Upper = 8068 mdconst::extract<ConstantInt>(KnownRanges->getOperand(1)); 8069 8070 ConstantRange Known(Lower->getValue(), Upper->getValue()); 8071 return Known.contains(Assumed) && Known != Assumed; 8072 } 8073 8074 /// Helper function to set range metadata. 8075 static bool 8076 setRangeMetadataIfisBetterRange(Instruction *I, 8077 const ConstantRange &AssumedConstantRange) { 8078 auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range); 8079 if (isBetterRange(AssumedConstantRange, OldRangeMD)) { 8080 if (!AssumedConstantRange.isEmptySet()) { 8081 I->setMetadata(LLVMContext::MD_range, 8082 getMDNodeForConstantRange(I->getType(), I->getContext(), 8083 AssumedConstantRange)); 8084 return true; 8085 } 8086 } 8087 return false; 8088 } 8089 8090 /// See AbstractAttribute::manifest() 8091 ChangeStatus manifest(Attributor &A) override { 8092 ChangeStatus Changed = ChangeStatus::UNCHANGED; 8093 ConstantRange AssumedConstantRange = getAssumedConstantRange(A); 8094 assert(!AssumedConstantRange.isFullSet() && "Invalid state"); 8095 8096 auto &V = getAssociatedValue(); 8097 if (!AssumedConstantRange.isEmptySet() && 8098 !AssumedConstantRange.isSingleElement()) { 8099 if (Instruction *I = dyn_cast<Instruction>(&V)) { 8100 assert(I == getCtxI() && "Should not annotate an instruction which is " 8101 "not the context instruction"); 8102 if (isa<CallInst>(I) || isa<LoadInst>(I)) 8103 if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange)) 8104 Changed = ChangeStatus::CHANGED; 8105 } 8106 } 8107 8108 return Changed; 8109 } 8110 }; 8111 8112 struct AAValueConstantRangeArgument final 8113 : AAArgumentFromCallSiteArguments< 8114 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState, 8115 true /* BridgeCallBaseContext */> { 8116 using Base = AAArgumentFromCallSiteArguments< 8117 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState, 8118 true /* BridgeCallBaseContext */>; 8119 AAValueConstantRangeArgument(const IRPosition &IRP, Attributor &A) 8120 : Base(IRP, A) {} 8121 8122 /// See AbstractAttribute::initialize(..). 8123 void initialize(Attributor &A) override { 8124 if (!getAnchorScope() || getAnchorScope()->isDeclaration()) { 8125 indicatePessimisticFixpoint(); 8126 } else { 8127 Base::initialize(A); 8128 } 8129 } 8130 8131 /// See AbstractAttribute::trackStatistics() 8132 void trackStatistics() const override { 8133 STATS_DECLTRACK_ARG_ATTR(value_range) 8134 } 8135 }; 8136 8137 struct AAValueConstantRangeReturned 8138 : AAReturnedFromReturnedValues<AAValueConstantRange, 8139 AAValueConstantRangeImpl, 8140 AAValueConstantRangeImpl::StateType, 8141 /* PropogateCallBaseContext */ true> { 8142 using Base = 8143 AAReturnedFromReturnedValues<AAValueConstantRange, 8144 AAValueConstantRangeImpl, 8145 AAValueConstantRangeImpl::StateType, 8146 /* PropogateCallBaseContext */ true>; 8147 AAValueConstantRangeReturned(const IRPosition &IRP, Attributor &A) 8148 : Base(IRP, A) {} 8149 8150 /// See AbstractAttribute::initialize(...). 8151 void initialize(Attributor &A) override {} 8152 8153 /// See AbstractAttribute::trackStatistics() 8154 void trackStatistics() const override { 8155 STATS_DECLTRACK_FNRET_ATTR(value_range) 8156 } 8157 }; 8158 8159 struct AAValueConstantRangeFloating : AAValueConstantRangeImpl { 8160 AAValueConstantRangeFloating(const IRPosition &IRP, Attributor &A) 8161 : AAValueConstantRangeImpl(IRP, A) {} 8162 8163 /// See AbstractAttribute::initialize(...). 8164 void initialize(Attributor &A) override { 8165 AAValueConstantRangeImpl::initialize(A); 8166 if (isAtFixpoint()) 8167 return; 8168 8169 Value &V = getAssociatedValue(); 8170 8171 if (auto *C = dyn_cast<ConstantInt>(&V)) { 8172 unionAssumed(ConstantRange(C->getValue())); 8173 indicateOptimisticFixpoint(); 8174 return; 8175 } 8176 8177 if (isa<UndefValue>(&V)) { 8178 // Collapse the undef state to 0. 8179 unionAssumed(ConstantRange(APInt(getBitWidth(), 0))); 8180 indicateOptimisticFixpoint(); 8181 return; 8182 } 8183 8184 if (isa<CallBase>(&V)) 8185 return; 8186 8187 if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V)) 8188 return; 8189 8190 // If it is a load instruction with range metadata, use it. 8191 if (LoadInst *LI = dyn_cast<LoadInst>(&V)) 8192 if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) { 8193 intersectKnown(getConstantRangeFromMetadata(*RangeMD)); 8194 return; 8195 } 8196 8197 // We can work with PHI and select instruction as we traverse their operands 8198 // during update. 8199 if (isa<SelectInst>(V) || isa<PHINode>(V)) 8200 return; 8201 8202 // Otherwise we give up. 8203 indicatePessimisticFixpoint(); 8204 8205 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: " 8206 << getAssociatedValue() << "\n"); 8207 } 8208 8209 bool calculateBinaryOperator( 8210 Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T, 8211 const Instruction *CtxI, 8212 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { 8213 Value *LHS = BinOp->getOperand(0); 8214 Value *RHS = BinOp->getOperand(1); 8215 8216 // Simplify the operands first. 8217 bool UsedAssumedInformation = false; 8218 const auto &SimplifiedLHS = 8219 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), 8220 *this, UsedAssumedInformation); 8221 if (!SimplifiedLHS.hasValue()) 8222 return true; 8223 if (!SimplifiedLHS.getValue()) 8224 return false; 8225 LHS = *SimplifiedLHS; 8226 8227 const auto &SimplifiedRHS = 8228 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), 8229 *this, UsedAssumedInformation); 8230 if (!SimplifiedRHS.hasValue()) 8231 return true; 8232 if (!SimplifiedRHS.getValue()) 8233 return false; 8234 RHS = *SimplifiedRHS; 8235 8236 // TODO: Allow non integers as well. 8237 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) 8238 return false; 8239 8240 auto &LHSAA = A.getAAFor<AAValueConstantRange>( 8241 *this, IRPosition::value(*LHS, getCallBaseContext()), 8242 DepClassTy::REQUIRED); 8243 QuerriedAAs.push_back(&LHSAA); 8244 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI); 8245 8246 auto &RHSAA = A.getAAFor<AAValueConstantRange>( 8247 *this, IRPosition::value(*RHS, getCallBaseContext()), 8248 DepClassTy::REQUIRED); 8249 QuerriedAAs.push_back(&RHSAA); 8250 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI); 8251 8252 auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange); 8253 8254 T.unionAssumed(AssumedRange); 8255 8256 // TODO: Track a known state too. 8257 8258 return T.isValidState(); 8259 } 8260 8261 bool calculateCastInst( 8262 Attributor &A, CastInst *CastI, IntegerRangeState &T, 8263 const Instruction *CtxI, 8264 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { 8265 assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!"); 8266 // TODO: Allow non integers as well. 8267 Value *OpV = CastI->getOperand(0); 8268 8269 // Simplify the operand first. 8270 bool UsedAssumedInformation = false; 8271 const auto &SimplifiedOpV = 8272 A.getAssumedSimplified(IRPosition::value(*OpV, getCallBaseContext()), 8273 *this, UsedAssumedInformation); 8274 if (!SimplifiedOpV.hasValue()) 8275 return true; 8276 if (!SimplifiedOpV.getValue()) 8277 return false; 8278 OpV = *SimplifiedOpV; 8279 8280 if (!OpV->getType()->isIntegerTy()) 8281 return false; 8282 8283 auto &OpAA = A.getAAFor<AAValueConstantRange>( 8284 *this, IRPosition::value(*OpV, getCallBaseContext()), 8285 DepClassTy::REQUIRED); 8286 QuerriedAAs.push_back(&OpAA); 8287 T.unionAssumed( 8288 OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth())); 8289 return T.isValidState(); 8290 } 8291 8292 bool 8293 calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T, 8294 const Instruction *CtxI, 8295 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { 8296 Value *LHS = CmpI->getOperand(0); 8297 Value *RHS = CmpI->getOperand(1); 8298 8299 // Simplify the operands first. 8300 bool UsedAssumedInformation = false; 8301 const auto &SimplifiedLHS = 8302 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), 8303 *this, UsedAssumedInformation); 8304 if (!SimplifiedLHS.hasValue()) 8305 return true; 8306 if (!SimplifiedLHS.getValue()) 8307 return false; 8308 LHS = *SimplifiedLHS; 8309 8310 const auto &SimplifiedRHS = 8311 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), 8312 *this, UsedAssumedInformation); 8313 if (!SimplifiedRHS.hasValue()) 8314 return true; 8315 if (!SimplifiedRHS.getValue()) 8316 return false; 8317 RHS = *SimplifiedRHS; 8318 8319 // TODO: Allow non integers as well. 8320 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) 8321 return false; 8322 8323 auto &LHSAA = A.getAAFor<AAValueConstantRange>( 8324 *this, IRPosition::value(*LHS, getCallBaseContext()), 8325 DepClassTy::REQUIRED); 8326 QuerriedAAs.push_back(&LHSAA); 8327 auto &RHSAA = A.getAAFor<AAValueConstantRange>( 8328 *this, IRPosition::value(*RHS, getCallBaseContext()), 8329 DepClassTy::REQUIRED); 8330 QuerriedAAs.push_back(&RHSAA); 8331 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI); 8332 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI); 8333 8334 // If one of them is empty set, we can't decide. 8335 if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet()) 8336 return true; 8337 8338 bool MustTrue = false, MustFalse = false; 8339 8340 auto AllowedRegion = 8341 ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange); 8342 8343 if (AllowedRegion.intersectWith(LHSAARange).isEmptySet()) 8344 MustFalse = true; 8345 8346 if (LHSAARange.icmp(CmpI->getPredicate(), RHSAARange)) 8347 MustTrue = true; 8348 8349 assert((!MustTrue || !MustFalse) && 8350 "Either MustTrue or MustFalse should be false!"); 8351 8352 if (MustTrue) 8353 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1))); 8354 else if (MustFalse) 8355 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0))); 8356 else 8357 T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true)); 8358 8359 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA 8360 << " " << RHSAA << "\n"); 8361 8362 // TODO: Track a known state too. 8363 return T.isValidState(); 8364 } 8365 8366 /// See AbstractAttribute::updateImpl(...). 8367 ChangeStatus updateImpl(Attributor &A) override { 8368 auto VisitValueCB = [&](Value &V, const Instruction *CtxI, 8369 IntegerRangeState &T, bool Stripped) -> bool { 8370 Instruction *I = dyn_cast<Instruction>(&V); 8371 if (!I || isa<CallBase>(I)) { 8372 8373 // Simplify the operand first. 8374 bool UsedAssumedInformation = false; 8375 const auto &SimplifiedOpV = 8376 A.getAssumedSimplified(IRPosition::value(V, getCallBaseContext()), 8377 *this, UsedAssumedInformation); 8378 if (!SimplifiedOpV.hasValue()) 8379 return true; 8380 if (!SimplifiedOpV.getValue()) 8381 return false; 8382 Value *VPtr = *SimplifiedOpV; 8383 8384 // If the value is not instruction, we query AA to Attributor. 8385 const auto &AA = A.getAAFor<AAValueConstantRange>( 8386 *this, IRPosition::value(*VPtr, getCallBaseContext()), 8387 DepClassTy::REQUIRED); 8388 8389 // Clamp operator is not used to utilize a program point CtxI. 8390 T.unionAssumed(AA.getAssumedConstantRange(A, CtxI)); 8391 8392 return T.isValidState(); 8393 } 8394 8395 SmallVector<const AAValueConstantRange *, 4> QuerriedAAs; 8396 if (auto *BinOp = dyn_cast<BinaryOperator>(I)) { 8397 if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs)) 8398 return false; 8399 } else if (auto *CmpI = dyn_cast<CmpInst>(I)) { 8400 if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs)) 8401 return false; 8402 } else if (auto *CastI = dyn_cast<CastInst>(I)) { 8403 if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs)) 8404 return false; 8405 } else { 8406 // Give up with other instructions. 8407 // TODO: Add other instructions 8408 8409 T.indicatePessimisticFixpoint(); 8410 return false; 8411 } 8412 8413 // Catch circular reasoning in a pessimistic way for now. 8414 // TODO: Check how the range evolves and if we stripped anything, see also 8415 // AADereferenceable or AAAlign for similar situations. 8416 for (const AAValueConstantRange *QueriedAA : QuerriedAAs) { 8417 if (QueriedAA != this) 8418 continue; 8419 // If we are in a stady state we do not need to worry. 8420 if (T.getAssumed() == getState().getAssumed()) 8421 continue; 8422 T.indicatePessimisticFixpoint(); 8423 } 8424 8425 return T.isValidState(); 8426 }; 8427 8428 IntegerRangeState T(getBitWidth()); 8429 8430 if (!genericValueTraversal<IntegerRangeState>(A, getIRPosition(), *this, T, 8431 VisitValueCB, getCtxI(), 8432 /* UseValueSimplify */ false)) 8433 return indicatePessimisticFixpoint(); 8434 8435 return clampStateAndIndicateChange(getState(), T); 8436 } 8437 8438 /// See AbstractAttribute::trackStatistics() 8439 void trackStatistics() const override { 8440 STATS_DECLTRACK_FLOATING_ATTR(value_range) 8441 } 8442 }; 8443 8444 struct AAValueConstantRangeFunction : AAValueConstantRangeImpl { 8445 AAValueConstantRangeFunction(const IRPosition &IRP, Attributor &A) 8446 : AAValueConstantRangeImpl(IRP, A) {} 8447 8448 /// See AbstractAttribute::initialize(...). 8449 ChangeStatus updateImpl(Attributor &A) override { 8450 llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will " 8451 "not be called"); 8452 } 8453 8454 /// See AbstractAttribute::trackStatistics() 8455 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) } 8456 }; 8457 8458 struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction { 8459 AAValueConstantRangeCallSite(const IRPosition &IRP, Attributor &A) 8460 : AAValueConstantRangeFunction(IRP, A) {} 8461 8462 /// See AbstractAttribute::trackStatistics() 8463 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) } 8464 }; 8465 8466 struct AAValueConstantRangeCallSiteReturned 8467 : AACallSiteReturnedFromReturned<AAValueConstantRange, 8468 AAValueConstantRangeImpl, 8469 AAValueConstantRangeImpl::StateType, 8470 /* IntroduceCallBaseContext */ true> { 8471 AAValueConstantRangeCallSiteReturned(const IRPosition &IRP, Attributor &A) 8472 : AACallSiteReturnedFromReturned<AAValueConstantRange, 8473 AAValueConstantRangeImpl, 8474 AAValueConstantRangeImpl::StateType, 8475 /* IntroduceCallBaseContext */ true>(IRP, 8476 A) { 8477 } 8478 8479 /// See AbstractAttribute::initialize(...). 8480 void initialize(Attributor &A) override { 8481 // If it is a load instruction with range metadata, use the metadata. 8482 if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue())) 8483 if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range)) 8484 intersectKnown(getConstantRangeFromMetadata(*RangeMD)); 8485 8486 AAValueConstantRangeImpl::initialize(A); 8487 } 8488 8489 /// See AbstractAttribute::trackStatistics() 8490 void trackStatistics() const override { 8491 STATS_DECLTRACK_CSRET_ATTR(value_range) 8492 } 8493 }; 8494 struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating { 8495 AAValueConstantRangeCallSiteArgument(const IRPosition &IRP, Attributor &A) 8496 : AAValueConstantRangeFloating(IRP, A) {} 8497 8498 /// See AbstractAttribute::manifest() 8499 ChangeStatus manifest(Attributor &A) override { 8500 return ChangeStatus::UNCHANGED; 8501 } 8502 8503 /// See AbstractAttribute::trackStatistics() 8504 void trackStatistics() const override { 8505 STATS_DECLTRACK_CSARG_ATTR(value_range) 8506 } 8507 }; 8508 8509 /// ------------------ Potential Values Attribute ------------------------- 8510 8511 struct AAPotentialValuesImpl : AAPotentialValues { 8512 using StateType = PotentialConstantIntValuesState; 8513 8514 AAPotentialValuesImpl(const IRPosition &IRP, Attributor &A) 8515 : AAPotentialValues(IRP, A) {} 8516 8517 /// See AbstractAttribute::initialize(..). 8518 void initialize(Attributor &A) override { 8519 if (A.hasSimplificationCallback(getIRPosition())) 8520 indicatePessimisticFixpoint(); 8521 else 8522 AAPotentialValues::initialize(A); 8523 } 8524 8525 /// See AbstractAttribute::getAsStr(). 8526 const std::string getAsStr() const override { 8527 std::string Str; 8528 llvm::raw_string_ostream OS(Str); 8529 OS << getState(); 8530 return OS.str(); 8531 } 8532 8533 /// See AbstractAttribute::updateImpl(...). 8534 ChangeStatus updateImpl(Attributor &A) override { 8535 return indicatePessimisticFixpoint(); 8536 } 8537 }; 8538 8539 struct AAPotentialValuesArgument final 8540 : AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl, 8541 PotentialConstantIntValuesState> { 8542 using Base = 8543 AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl, 8544 PotentialConstantIntValuesState>; 8545 AAPotentialValuesArgument(const IRPosition &IRP, Attributor &A) 8546 : Base(IRP, A) {} 8547 8548 /// See AbstractAttribute::initialize(..). 8549 void initialize(Attributor &A) override { 8550 if (!getAnchorScope() || getAnchorScope()->isDeclaration()) { 8551 indicatePessimisticFixpoint(); 8552 } else { 8553 Base::initialize(A); 8554 } 8555 } 8556 8557 /// See AbstractAttribute::trackStatistics() 8558 void trackStatistics() const override { 8559 STATS_DECLTRACK_ARG_ATTR(potential_values) 8560 } 8561 }; 8562 8563 struct AAPotentialValuesReturned 8564 : AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl> { 8565 using Base = 8566 AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl>; 8567 AAPotentialValuesReturned(const IRPosition &IRP, Attributor &A) 8568 : Base(IRP, A) {} 8569 8570 /// See AbstractAttribute::trackStatistics() 8571 void trackStatistics() const override { 8572 STATS_DECLTRACK_FNRET_ATTR(potential_values) 8573 } 8574 }; 8575 8576 struct AAPotentialValuesFloating : AAPotentialValuesImpl { 8577 AAPotentialValuesFloating(const IRPosition &IRP, Attributor &A) 8578 : AAPotentialValuesImpl(IRP, A) {} 8579 8580 /// See AbstractAttribute::initialize(..). 8581 void initialize(Attributor &A) override { 8582 AAPotentialValuesImpl::initialize(A); 8583 if (isAtFixpoint()) 8584 return; 8585 8586 Value &V = getAssociatedValue(); 8587 8588 if (auto *C = dyn_cast<ConstantInt>(&V)) { 8589 unionAssumed(C->getValue()); 8590 indicateOptimisticFixpoint(); 8591 return; 8592 } 8593 8594 if (isa<UndefValue>(&V)) { 8595 unionAssumedWithUndef(); 8596 indicateOptimisticFixpoint(); 8597 return; 8598 } 8599 8600 if (isa<BinaryOperator>(&V) || isa<ICmpInst>(&V) || isa<CastInst>(&V)) 8601 return; 8602 8603 if (isa<SelectInst>(V) || isa<PHINode>(V) || isa<LoadInst>(V)) 8604 return; 8605 8606 indicatePessimisticFixpoint(); 8607 8608 LLVM_DEBUG(dbgs() << "[AAPotentialValues] We give up: " 8609 << getAssociatedValue() << "\n"); 8610 } 8611 8612 static bool calculateICmpInst(const ICmpInst *ICI, const APInt &LHS, 8613 const APInt &RHS) { 8614 ICmpInst::Predicate Pred = ICI->getPredicate(); 8615 switch (Pred) { 8616 case ICmpInst::ICMP_UGT: 8617 return LHS.ugt(RHS); 8618 case ICmpInst::ICMP_SGT: 8619 return LHS.sgt(RHS); 8620 case ICmpInst::ICMP_EQ: 8621 return LHS.eq(RHS); 8622 case ICmpInst::ICMP_UGE: 8623 return LHS.uge(RHS); 8624 case ICmpInst::ICMP_SGE: 8625 return LHS.sge(RHS); 8626 case ICmpInst::ICMP_ULT: 8627 return LHS.ult(RHS); 8628 case ICmpInst::ICMP_SLT: 8629 return LHS.slt(RHS); 8630 case ICmpInst::ICMP_NE: 8631 return LHS.ne(RHS); 8632 case ICmpInst::ICMP_ULE: 8633 return LHS.ule(RHS); 8634 case ICmpInst::ICMP_SLE: 8635 return LHS.sle(RHS); 8636 default: 8637 llvm_unreachable("Invalid ICmp predicate!"); 8638 } 8639 } 8640 8641 static APInt calculateCastInst(const CastInst *CI, const APInt &Src, 8642 uint32_t ResultBitWidth) { 8643 Instruction::CastOps CastOp = CI->getOpcode(); 8644 switch (CastOp) { 8645 default: 8646 llvm_unreachable("unsupported or not integer cast"); 8647 case Instruction::Trunc: 8648 return Src.trunc(ResultBitWidth); 8649 case Instruction::SExt: 8650 return Src.sext(ResultBitWidth); 8651 case Instruction::ZExt: 8652 return Src.zext(ResultBitWidth); 8653 case Instruction::BitCast: 8654 return Src; 8655 } 8656 } 8657 8658 static APInt calculateBinaryOperator(const BinaryOperator *BinOp, 8659 const APInt &LHS, const APInt &RHS, 8660 bool &SkipOperation, bool &Unsupported) { 8661 Instruction::BinaryOps BinOpcode = BinOp->getOpcode(); 8662 // Unsupported is set to true when the binary operator is not supported. 8663 // SkipOperation is set to true when UB occur with the given operand pair 8664 // (LHS, RHS). 8665 // TODO: we should look at nsw and nuw keywords to handle operations 8666 // that create poison or undef value. 8667 switch (BinOpcode) { 8668 default: 8669 Unsupported = true; 8670 return LHS; 8671 case Instruction::Add: 8672 return LHS + RHS; 8673 case Instruction::Sub: 8674 return LHS - RHS; 8675 case Instruction::Mul: 8676 return LHS * RHS; 8677 case Instruction::UDiv: 8678 if (RHS.isNullValue()) { 8679 SkipOperation = true; 8680 return LHS; 8681 } 8682 return LHS.udiv(RHS); 8683 case Instruction::SDiv: 8684 if (RHS.isNullValue()) { 8685 SkipOperation = true; 8686 return LHS; 8687 } 8688 return LHS.sdiv(RHS); 8689 case Instruction::URem: 8690 if (RHS.isNullValue()) { 8691 SkipOperation = true; 8692 return LHS; 8693 } 8694 return LHS.urem(RHS); 8695 case Instruction::SRem: 8696 if (RHS.isNullValue()) { 8697 SkipOperation = true; 8698 return LHS; 8699 } 8700 return LHS.srem(RHS); 8701 case Instruction::Shl: 8702 return LHS.shl(RHS); 8703 case Instruction::LShr: 8704 return LHS.lshr(RHS); 8705 case Instruction::AShr: 8706 return LHS.ashr(RHS); 8707 case Instruction::And: 8708 return LHS & RHS; 8709 case Instruction::Or: 8710 return LHS | RHS; 8711 case Instruction::Xor: 8712 return LHS ^ RHS; 8713 } 8714 } 8715 8716 bool calculateBinaryOperatorAndTakeUnion(const BinaryOperator *BinOp, 8717 const APInt &LHS, const APInt &RHS) { 8718 bool SkipOperation = false; 8719 bool Unsupported = false; 8720 APInt Result = 8721 calculateBinaryOperator(BinOp, LHS, RHS, SkipOperation, Unsupported); 8722 if (Unsupported) 8723 return false; 8724 // If SkipOperation is true, we can ignore this operand pair (L, R). 8725 if (!SkipOperation) 8726 unionAssumed(Result); 8727 return isValidState(); 8728 } 8729 8730 ChangeStatus updateWithICmpInst(Attributor &A, ICmpInst *ICI) { 8731 auto AssumedBefore = getAssumed(); 8732 Value *LHS = ICI->getOperand(0); 8733 Value *RHS = ICI->getOperand(1); 8734 8735 // Simplify the operands first. 8736 bool UsedAssumedInformation = false; 8737 const auto &SimplifiedLHS = 8738 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), 8739 *this, UsedAssumedInformation); 8740 if (!SimplifiedLHS.hasValue()) 8741 return ChangeStatus::UNCHANGED; 8742 if (!SimplifiedLHS.getValue()) 8743 return indicatePessimisticFixpoint(); 8744 LHS = *SimplifiedLHS; 8745 8746 const auto &SimplifiedRHS = 8747 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), 8748 *this, UsedAssumedInformation); 8749 if (!SimplifiedRHS.hasValue()) 8750 return ChangeStatus::UNCHANGED; 8751 if (!SimplifiedRHS.getValue()) 8752 return indicatePessimisticFixpoint(); 8753 RHS = *SimplifiedRHS; 8754 8755 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) 8756 return indicatePessimisticFixpoint(); 8757 8758 auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS), 8759 DepClassTy::REQUIRED); 8760 if (!LHSAA.isValidState()) 8761 return indicatePessimisticFixpoint(); 8762 8763 auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS), 8764 DepClassTy::REQUIRED); 8765 if (!RHSAA.isValidState()) 8766 return indicatePessimisticFixpoint(); 8767 8768 const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet(); 8769 const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet(); 8770 8771 // TODO: make use of undef flag to limit potential values aggressively. 8772 bool MaybeTrue = false, MaybeFalse = false; 8773 const APInt Zero(RHS->getType()->getIntegerBitWidth(), 0); 8774 if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) { 8775 // The result of any comparison between undefs can be soundly replaced 8776 // with undef. 8777 unionAssumedWithUndef(); 8778 } else if (LHSAA.undefIsContained()) { 8779 for (const APInt &R : RHSAAPVS) { 8780 bool CmpResult = calculateICmpInst(ICI, Zero, R); 8781 MaybeTrue |= CmpResult; 8782 MaybeFalse |= !CmpResult; 8783 if (MaybeTrue & MaybeFalse) 8784 return indicatePessimisticFixpoint(); 8785 } 8786 } else if (RHSAA.undefIsContained()) { 8787 for (const APInt &L : LHSAAPVS) { 8788 bool CmpResult = calculateICmpInst(ICI, L, Zero); 8789 MaybeTrue |= CmpResult; 8790 MaybeFalse |= !CmpResult; 8791 if (MaybeTrue & MaybeFalse) 8792 return indicatePessimisticFixpoint(); 8793 } 8794 } else { 8795 for (const APInt &L : LHSAAPVS) { 8796 for (const APInt &R : RHSAAPVS) { 8797 bool CmpResult = calculateICmpInst(ICI, L, R); 8798 MaybeTrue |= CmpResult; 8799 MaybeFalse |= !CmpResult; 8800 if (MaybeTrue & MaybeFalse) 8801 return indicatePessimisticFixpoint(); 8802 } 8803 } 8804 } 8805 if (MaybeTrue) 8806 unionAssumed(APInt(/* numBits */ 1, /* val */ 1)); 8807 if (MaybeFalse) 8808 unionAssumed(APInt(/* numBits */ 1, /* val */ 0)); 8809 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED 8810 : ChangeStatus::CHANGED; 8811 } 8812 8813 ChangeStatus updateWithSelectInst(Attributor &A, SelectInst *SI) { 8814 auto AssumedBefore = getAssumed(); 8815 Value *LHS = SI->getTrueValue(); 8816 Value *RHS = SI->getFalseValue(); 8817 8818 // Simplify the operands first. 8819 bool UsedAssumedInformation = false; 8820 const auto &SimplifiedLHS = 8821 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), 8822 *this, UsedAssumedInformation); 8823 if (!SimplifiedLHS.hasValue()) 8824 return ChangeStatus::UNCHANGED; 8825 if (!SimplifiedLHS.getValue()) 8826 return indicatePessimisticFixpoint(); 8827 LHS = *SimplifiedLHS; 8828 8829 const auto &SimplifiedRHS = 8830 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), 8831 *this, UsedAssumedInformation); 8832 if (!SimplifiedRHS.hasValue()) 8833 return ChangeStatus::UNCHANGED; 8834 if (!SimplifiedRHS.getValue()) 8835 return indicatePessimisticFixpoint(); 8836 RHS = *SimplifiedRHS; 8837 8838 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) 8839 return indicatePessimisticFixpoint(); 8840 8841 Optional<Constant *> C = A.getAssumedConstant(*SI->getCondition(), *this, 8842 UsedAssumedInformation); 8843 8844 // Check if we only need one operand. 8845 bool OnlyLeft = false, OnlyRight = false; 8846 if (C.hasValue() && *C && (*C)->isOneValue()) 8847 OnlyLeft = true; 8848 else if (C.hasValue() && *C && (*C)->isZeroValue()) 8849 OnlyRight = true; 8850 8851 const AAPotentialValues *LHSAA = nullptr, *RHSAA = nullptr; 8852 if (!OnlyRight) { 8853 LHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS), 8854 DepClassTy::REQUIRED); 8855 if (!LHSAA->isValidState()) 8856 return indicatePessimisticFixpoint(); 8857 } 8858 if (!OnlyLeft) { 8859 RHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS), 8860 DepClassTy::REQUIRED); 8861 if (!RHSAA->isValidState()) 8862 return indicatePessimisticFixpoint(); 8863 } 8864 8865 if (!LHSAA || !RHSAA) { 8866 // select (true/false), lhs, rhs 8867 auto *OpAA = LHSAA ? LHSAA : RHSAA; 8868 8869 if (OpAA->undefIsContained()) 8870 unionAssumedWithUndef(); 8871 else 8872 unionAssumed(*OpAA); 8873 8874 } else if (LHSAA->undefIsContained() && RHSAA->undefIsContained()) { 8875 // select i1 *, undef , undef => undef 8876 unionAssumedWithUndef(); 8877 } else { 8878 unionAssumed(*LHSAA); 8879 unionAssumed(*RHSAA); 8880 } 8881 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED 8882 : ChangeStatus::CHANGED; 8883 } 8884 8885 ChangeStatus updateWithCastInst(Attributor &A, CastInst *CI) { 8886 auto AssumedBefore = getAssumed(); 8887 if (!CI->isIntegerCast()) 8888 return indicatePessimisticFixpoint(); 8889 assert(CI->getNumOperands() == 1 && "Expected cast to be unary!"); 8890 uint32_t ResultBitWidth = CI->getDestTy()->getIntegerBitWidth(); 8891 Value *Src = CI->getOperand(0); 8892 8893 // Simplify the operand first. 8894 bool UsedAssumedInformation = false; 8895 const auto &SimplifiedSrc = 8896 A.getAssumedSimplified(IRPosition::value(*Src, getCallBaseContext()), 8897 *this, UsedAssumedInformation); 8898 if (!SimplifiedSrc.hasValue()) 8899 return ChangeStatus::UNCHANGED; 8900 if (!SimplifiedSrc.getValue()) 8901 return indicatePessimisticFixpoint(); 8902 Src = *SimplifiedSrc; 8903 8904 auto &SrcAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*Src), 8905 DepClassTy::REQUIRED); 8906 if (!SrcAA.isValidState()) 8907 return indicatePessimisticFixpoint(); 8908 const DenseSet<APInt> &SrcAAPVS = SrcAA.getAssumedSet(); 8909 if (SrcAA.undefIsContained()) 8910 unionAssumedWithUndef(); 8911 else { 8912 for (const APInt &S : SrcAAPVS) { 8913 APInt T = calculateCastInst(CI, S, ResultBitWidth); 8914 unionAssumed(T); 8915 } 8916 } 8917 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED 8918 : ChangeStatus::CHANGED; 8919 } 8920 8921 ChangeStatus updateWithBinaryOperator(Attributor &A, BinaryOperator *BinOp) { 8922 auto AssumedBefore = getAssumed(); 8923 Value *LHS = BinOp->getOperand(0); 8924 Value *RHS = BinOp->getOperand(1); 8925 8926 // Simplify the operands first. 8927 bool UsedAssumedInformation = false; 8928 const auto &SimplifiedLHS = 8929 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), 8930 *this, UsedAssumedInformation); 8931 if (!SimplifiedLHS.hasValue()) 8932 return ChangeStatus::UNCHANGED; 8933 if (!SimplifiedLHS.getValue()) 8934 return indicatePessimisticFixpoint(); 8935 LHS = *SimplifiedLHS; 8936 8937 const auto &SimplifiedRHS = 8938 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), 8939 *this, UsedAssumedInformation); 8940 if (!SimplifiedRHS.hasValue()) 8941 return ChangeStatus::UNCHANGED; 8942 if (!SimplifiedRHS.getValue()) 8943 return indicatePessimisticFixpoint(); 8944 RHS = *SimplifiedRHS; 8945 8946 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) 8947 return indicatePessimisticFixpoint(); 8948 8949 auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS), 8950 DepClassTy::REQUIRED); 8951 if (!LHSAA.isValidState()) 8952 return indicatePessimisticFixpoint(); 8953 8954 auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS), 8955 DepClassTy::REQUIRED); 8956 if (!RHSAA.isValidState()) 8957 return indicatePessimisticFixpoint(); 8958 8959 const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet(); 8960 const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet(); 8961 const APInt Zero = APInt(LHS->getType()->getIntegerBitWidth(), 0); 8962 8963 // TODO: make use of undef flag to limit potential values aggressively. 8964 if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) { 8965 if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, Zero)) 8966 return indicatePessimisticFixpoint(); 8967 } else if (LHSAA.undefIsContained()) { 8968 for (const APInt &R : RHSAAPVS) { 8969 if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, R)) 8970 return indicatePessimisticFixpoint(); 8971 } 8972 } else if (RHSAA.undefIsContained()) { 8973 for (const APInt &L : LHSAAPVS) { 8974 if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, Zero)) 8975 return indicatePessimisticFixpoint(); 8976 } 8977 } else { 8978 for (const APInt &L : LHSAAPVS) { 8979 for (const APInt &R : RHSAAPVS) { 8980 if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, R)) 8981 return indicatePessimisticFixpoint(); 8982 } 8983 } 8984 } 8985 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED 8986 : ChangeStatus::CHANGED; 8987 } 8988 8989 ChangeStatus updateWithPHINode(Attributor &A, PHINode *PHI) { 8990 auto AssumedBefore = getAssumed(); 8991 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { 8992 Value *IncomingValue = PHI->getIncomingValue(u); 8993 8994 // Simplify the operand first. 8995 bool UsedAssumedInformation = false; 8996 const auto &SimplifiedIncomingValue = A.getAssumedSimplified( 8997 IRPosition::value(*IncomingValue, getCallBaseContext()), *this, 8998 UsedAssumedInformation); 8999 if (!SimplifiedIncomingValue.hasValue()) 9000 continue; 9001 if (!SimplifiedIncomingValue.getValue()) 9002 return indicatePessimisticFixpoint(); 9003 IncomingValue = *SimplifiedIncomingValue; 9004 9005 auto &PotentialValuesAA = A.getAAFor<AAPotentialValues>( 9006 *this, IRPosition::value(*IncomingValue), DepClassTy::REQUIRED); 9007 if (!PotentialValuesAA.isValidState()) 9008 return indicatePessimisticFixpoint(); 9009 if (PotentialValuesAA.undefIsContained()) 9010 unionAssumedWithUndef(); 9011 else 9012 unionAssumed(PotentialValuesAA.getAssumed()); 9013 } 9014 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED 9015 : ChangeStatus::CHANGED; 9016 } 9017 9018 ChangeStatus updateWithLoad(Attributor &A, LoadInst &L) { 9019 if (!L.getType()->isIntegerTy()) 9020 return indicatePessimisticFixpoint(); 9021 9022 auto Union = [&](Value &V) { 9023 if (isa<UndefValue>(V)) { 9024 unionAssumedWithUndef(); 9025 return true; 9026 } 9027 if (ConstantInt *CI = dyn_cast<ConstantInt>(&V)) { 9028 unionAssumed(CI->getValue()); 9029 return true; 9030 } 9031 return false; 9032 }; 9033 auto AssumedBefore = getAssumed(); 9034 9035 if (!AAValueSimplifyImpl::handleLoad(A, *this, L, Union)) 9036 return indicatePessimisticFixpoint(); 9037 9038 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED 9039 : ChangeStatus::CHANGED; 9040 } 9041 9042 /// See AbstractAttribute::updateImpl(...). 9043 ChangeStatus updateImpl(Attributor &A) override { 9044 Value &V = getAssociatedValue(); 9045 Instruction *I = dyn_cast<Instruction>(&V); 9046 9047 if (auto *ICI = dyn_cast<ICmpInst>(I)) 9048 return updateWithICmpInst(A, ICI); 9049 9050 if (auto *SI = dyn_cast<SelectInst>(I)) 9051 return updateWithSelectInst(A, SI); 9052 9053 if (auto *CI = dyn_cast<CastInst>(I)) 9054 return updateWithCastInst(A, CI); 9055 9056 if (auto *BinOp = dyn_cast<BinaryOperator>(I)) 9057 return updateWithBinaryOperator(A, BinOp); 9058 9059 if (auto *PHI = dyn_cast<PHINode>(I)) 9060 return updateWithPHINode(A, PHI); 9061 9062 if (auto *L = dyn_cast<LoadInst>(I)) 9063 return updateWithLoad(A, *L); 9064 9065 return indicatePessimisticFixpoint(); 9066 } 9067 9068 /// See AbstractAttribute::trackStatistics() 9069 void trackStatistics() const override { 9070 STATS_DECLTRACK_FLOATING_ATTR(potential_values) 9071 } 9072 }; 9073 9074 struct AAPotentialValuesFunction : AAPotentialValuesImpl { 9075 AAPotentialValuesFunction(const IRPosition &IRP, Attributor &A) 9076 : AAPotentialValuesImpl(IRP, A) {} 9077 9078 /// See AbstractAttribute::initialize(...). 9079 ChangeStatus updateImpl(Attributor &A) override { 9080 llvm_unreachable("AAPotentialValues(Function|CallSite)::updateImpl will " 9081 "not be called"); 9082 } 9083 9084 /// See AbstractAttribute::trackStatistics() 9085 void trackStatistics() const override { 9086 STATS_DECLTRACK_FN_ATTR(potential_values) 9087 } 9088 }; 9089 9090 struct AAPotentialValuesCallSite : AAPotentialValuesFunction { 9091 AAPotentialValuesCallSite(const IRPosition &IRP, Attributor &A) 9092 : AAPotentialValuesFunction(IRP, A) {} 9093 9094 /// See AbstractAttribute::trackStatistics() 9095 void trackStatistics() const override { 9096 STATS_DECLTRACK_CS_ATTR(potential_values) 9097 } 9098 }; 9099 9100 struct AAPotentialValuesCallSiteReturned 9101 : AACallSiteReturnedFromReturned<AAPotentialValues, AAPotentialValuesImpl> { 9102 AAPotentialValuesCallSiteReturned(const IRPosition &IRP, Attributor &A) 9103 : AACallSiteReturnedFromReturned<AAPotentialValues, 9104 AAPotentialValuesImpl>(IRP, A) {} 9105 9106 /// See AbstractAttribute::trackStatistics() 9107 void trackStatistics() const override { 9108 STATS_DECLTRACK_CSRET_ATTR(potential_values) 9109 } 9110 }; 9111 9112 struct AAPotentialValuesCallSiteArgument : AAPotentialValuesFloating { 9113 AAPotentialValuesCallSiteArgument(const IRPosition &IRP, Attributor &A) 9114 : AAPotentialValuesFloating(IRP, A) {} 9115 9116 /// See AbstractAttribute::initialize(..). 9117 void initialize(Attributor &A) override { 9118 AAPotentialValuesImpl::initialize(A); 9119 if (isAtFixpoint()) 9120 return; 9121 9122 Value &V = getAssociatedValue(); 9123 9124 if (auto *C = dyn_cast<ConstantInt>(&V)) { 9125 unionAssumed(C->getValue()); 9126 indicateOptimisticFixpoint(); 9127 return; 9128 } 9129 9130 if (isa<UndefValue>(&V)) { 9131 unionAssumedWithUndef(); 9132 indicateOptimisticFixpoint(); 9133 return; 9134 } 9135 } 9136 9137 /// See AbstractAttribute::updateImpl(...). 9138 ChangeStatus updateImpl(Attributor &A) override { 9139 Value &V = getAssociatedValue(); 9140 auto AssumedBefore = getAssumed(); 9141 auto &AA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(V), 9142 DepClassTy::REQUIRED); 9143 const auto &S = AA.getAssumed(); 9144 unionAssumed(S); 9145 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED 9146 : ChangeStatus::CHANGED; 9147 } 9148 9149 /// See AbstractAttribute::trackStatistics() 9150 void trackStatistics() const override { 9151 STATS_DECLTRACK_CSARG_ATTR(potential_values) 9152 } 9153 }; 9154 9155 /// ------------------------ NoUndef Attribute --------------------------------- 9156 struct AANoUndefImpl : AANoUndef { 9157 AANoUndefImpl(const IRPosition &IRP, Attributor &A) : AANoUndef(IRP, A) {} 9158 9159 /// See AbstractAttribute::initialize(...). 9160 void initialize(Attributor &A) override { 9161 if (getIRPosition().hasAttr({Attribute::NoUndef})) { 9162 indicateOptimisticFixpoint(); 9163 return; 9164 } 9165 Value &V = getAssociatedValue(); 9166 if (isa<UndefValue>(V)) 9167 indicatePessimisticFixpoint(); 9168 else if (isa<FreezeInst>(V)) 9169 indicateOptimisticFixpoint(); 9170 else if (getPositionKind() != IRPosition::IRP_RETURNED && 9171 isGuaranteedNotToBeUndefOrPoison(&V)) 9172 indicateOptimisticFixpoint(); 9173 else 9174 AANoUndef::initialize(A); 9175 } 9176 9177 /// See followUsesInMBEC 9178 bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I, 9179 AANoUndef::StateType &State) { 9180 const Value *UseV = U->get(); 9181 const DominatorTree *DT = nullptr; 9182 AssumptionCache *AC = nullptr; 9183 InformationCache &InfoCache = A.getInfoCache(); 9184 if (Function *F = getAnchorScope()) { 9185 DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F); 9186 AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F); 9187 } 9188 State.setKnown(isGuaranteedNotToBeUndefOrPoison(UseV, AC, I, DT)); 9189 bool TrackUse = false; 9190 // Track use for instructions which must produce undef or poison bits when 9191 // at least one operand contains such bits. 9192 if (isa<CastInst>(*I) || isa<GetElementPtrInst>(*I)) 9193 TrackUse = true; 9194 return TrackUse; 9195 } 9196 9197 /// See AbstractAttribute::getAsStr(). 9198 const std::string getAsStr() const override { 9199 return getAssumed() ? "noundef" : "may-undef-or-poison"; 9200 } 9201 9202 ChangeStatus manifest(Attributor &A) override { 9203 // We don't manifest noundef attribute for dead positions because the 9204 // associated values with dead positions would be replaced with undef 9205 // values. 9206 bool UsedAssumedInformation = false; 9207 if (A.isAssumedDead(getIRPosition(), nullptr, nullptr, 9208 UsedAssumedInformation)) 9209 return ChangeStatus::UNCHANGED; 9210 // A position whose simplified value does not have any value is 9211 // considered to be dead. We don't manifest noundef in such positions for 9212 // the same reason above. 9213 if (!A.getAssumedSimplified(getIRPosition(), *this, UsedAssumedInformation) 9214 .hasValue()) 9215 return ChangeStatus::UNCHANGED; 9216 return AANoUndef::manifest(A); 9217 } 9218 }; 9219 9220 struct AANoUndefFloating : public AANoUndefImpl { 9221 AANoUndefFloating(const IRPosition &IRP, Attributor &A) 9222 : AANoUndefImpl(IRP, A) {} 9223 9224 /// See AbstractAttribute::initialize(...). 9225 void initialize(Attributor &A) override { 9226 AANoUndefImpl::initialize(A); 9227 if (!getState().isAtFixpoint()) 9228 if (Instruction *CtxI = getCtxI()) 9229 followUsesInMBEC(*this, A, getState(), *CtxI); 9230 } 9231 9232 /// See AbstractAttribute::updateImpl(...). 9233 ChangeStatus updateImpl(Attributor &A) override { 9234 auto VisitValueCB = [&](Value &V, const Instruction *CtxI, 9235 AANoUndef::StateType &T, bool Stripped) -> bool { 9236 const auto &AA = A.getAAFor<AANoUndef>(*this, IRPosition::value(V), 9237 DepClassTy::REQUIRED); 9238 if (!Stripped && this == &AA) { 9239 T.indicatePessimisticFixpoint(); 9240 } else { 9241 const AANoUndef::StateType &S = 9242 static_cast<const AANoUndef::StateType &>(AA.getState()); 9243 T ^= S; 9244 } 9245 return T.isValidState(); 9246 }; 9247 9248 StateType T; 9249 if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T, 9250 VisitValueCB, getCtxI())) 9251 return indicatePessimisticFixpoint(); 9252 9253 return clampStateAndIndicateChange(getState(), T); 9254 } 9255 9256 /// See AbstractAttribute::trackStatistics() 9257 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) } 9258 }; 9259 9260 struct AANoUndefReturned final 9261 : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl> { 9262 AANoUndefReturned(const IRPosition &IRP, Attributor &A) 9263 : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl>(IRP, A) {} 9264 9265 /// See AbstractAttribute::trackStatistics() 9266 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) } 9267 }; 9268 9269 struct AANoUndefArgument final 9270 : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl> { 9271 AANoUndefArgument(const IRPosition &IRP, Attributor &A) 9272 : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl>(IRP, A) {} 9273 9274 /// See AbstractAttribute::trackStatistics() 9275 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noundef) } 9276 }; 9277 9278 struct AANoUndefCallSiteArgument final : AANoUndefFloating { 9279 AANoUndefCallSiteArgument(const IRPosition &IRP, Attributor &A) 9280 : AANoUndefFloating(IRP, A) {} 9281 9282 /// See AbstractAttribute::trackStatistics() 9283 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noundef) } 9284 }; 9285 9286 struct AANoUndefCallSiteReturned final 9287 : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl> { 9288 AANoUndefCallSiteReturned(const IRPosition &IRP, Attributor &A) 9289 : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl>(IRP, A) {} 9290 9291 /// See AbstractAttribute::trackStatistics() 9292 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noundef) } 9293 }; 9294 9295 struct AACallEdgesFunction : public AACallEdges { 9296 AACallEdgesFunction(const IRPosition &IRP, Attributor &A) 9297 : AACallEdges(IRP, A) {} 9298 9299 /// See AbstractAttribute::updateImpl(...). 9300 ChangeStatus updateImpl(Attributor &A) override { 9301 ChangeStatus Change = ChangeStatus::UNCHANGED; 9302 bool OldHasUnknownCallee = HasUnknownCallee; 9303 bool OldHasUnknownCalleeNonAsm = HasUnknownCalleeNonAsm; 9304 9305 auto AddCalledFunction = [&](Function *Fn) { 9306 if (CalledFunctions.insert(Fn)) { 9307 Change = ChangeStatus::CHANGED; 9308 LLVM_DEBUG(dbgs() << "[AACallEdges] New call edge: " << Fn->getName() 9309 << "\n"); 9310 } 9311 }; 9312 9313 auto VisitValue = [&](Value &V, const Instruction *CtxI, bool &HasUnknown, 9314 bool Stripped) -> bool { 9315 if (Function *Fn = dyn_cast<Function>(&V)) { 9316 AddCalledFunction(Fn); 9317 } else { 9318 LLVM_DEBUG(dbgs() << "[AACallEdges] Unrecognized value: " << V << "\n"); 9319 HasUnknown = true; 9320 HasUnknownCalleeNonAsm = true; 9321 } 9322 9323 // Explore all values. 9324 return true; 9325 }; 9326 9327 // Process any value that we might call. 9328 auto ProcessCalledOperand = [&](Value *V, Instruction *Ctx) { 9329 if (!genericValueTraversal<bool>(A, IRPosition::value(*V), *this, 9330 HasUnknownCallee, VisitValue, nullptr, 9331 false)) { 9332 // If we haven't gone through all values, assume that there are unknown 9333 // callees. 9334 HasUnknownCallee = true; 9335 HasUnknownCalleeNonAsm = true; 9336 } 9337 }; 9338 9339 auto ProcessCallInst = [&](Instruction &Inst) { 9340 CallBase &CB = static_cast<CallBase &>(Inst); 9341 if (CB.isInlineAsm()) { 9342 HasUnknownCallee = true; 9343 return true; 9344 } 9345 9346 // Process callee metadata if available. 9347 if (auto *MD = Inst.getMetadata(LLVMContext::MD_callees)) { 9348 for (auto &Op : MD->operands()) { 9349 Function *Callee = mdconst::extract_or_null<Function>(Op); 9350 if (Callee) 9351 AddCalledFunction(Callee); 9352 } 9353 // Callees metadata grantees that the called function is one of its 9354 // operands, So we are done. 9355 return true; 9356 } 9357 9358 // The most simple case. 9359 ProcessCalledOperand(CB.getCalledOperand(), &Inst); 9360 9361 // Process callback functions. 9362 SmallVector<const Use *, 4u> CallbackUses; 9363 AbstractCallSite::getCallbackUses(CB, CallbackUses); 9364 for (const Use *U : CallbackUses) 9365 ProcessCalledOperand(U->get(), &Inst); 9366 9367 return true; 9368 }; 9369 9370 // Visit all callable instructions. 9371 bool UsedAssumedInformation = false; 9372 if (!A.checkForAllCallLikeInstructions(ProcessCallInst, *this, 9373 UsedAssumedInformation)) { 9374 // If we haven't looked at all call like instructions, assume that there 9375 // are unknown callees. 9376 HasUnknownCallee = true; 9377 HasUnknownCalleeNonAsm = true; 9378 } 9379 9380 // Track changes. 9381 if (OldHasUnknownCallee != HasUnknownCallee || 9382 OldHasUnknownCalleeNonAsm != HasUnknownCalleeNonAsm) 9383 Change = ChangeStatus::CHANGED; 9384 9385 return Change; 9386 } 9387 9388 virtual const SetVector<Function *> &getOptimisticEdges() const override { 9389 return CalledFunctions; 9390 }; 9391 9392 virtual bool hasUnknownCallee() const override { return HasUnknownCallee; } 9393 9394 virtual bool hasNonAsmUnknownCallee() const override { 9395 return HasUnknownCalleeNonAsm; 9396 } 9397 9398 const std::string getAsStr() const override { 9399 return "CallEdges[" + std::to_string(HasUnknownCallee) + "," + 9400 std::to_string(CalledFunctions.size()) + "]"; 9401 } 9402 9403 void trackStatistics() const override {} 9404 9405 /// Optimistic set of functions that might be called by this function. 9406 SetVector<Function *> CalledFunctions; 9407 9408 /// Is there any call with a unknown callee. 9409 bool HasUnknownCallee = false; 9410 9411 /// Is there any call with a unknown callee, excluding any inline asm. 9412 bool HasUnknownCalleeNonAsm = false; 9413 }; 9414 9415 struct AAFunctionReachabilityFunction : public AAFunctionReachability { 9416 AAFunctionReachabilityFunction(const IRPosition &IRP, Attributor &A) 9417 : AAFunctionReachability(IRP, A) {} 9418 9419 bool canReach(Attributor &A, Function *Fn) const override { 9420 // Assume that we can reach any function if we can reach a call with 9421 // unknown callee. 9422 if (CanReachUnknownCallee) 9423 return true; 9424 9425 if (ReachableQueries.count(Fn)) 9426 return true; 9427 9428 if (UnreachableQueries.count(Fn)) 9429 return false; 9430 9431 const AACallEdges &AAEdges = 9432 A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED); 9433 9434 const SetVector<Function *> &Edges = AAEdges.getOptimisticEdges(); 9435 bool Result = checkIfReachable(A, Edges, Fn); 9436 9437 // Attributor returns attributes as const, so this function has to be 9438 // const for users of this attribute to use it without having to do 9439 // a const_cast. 9440 // This is a hack for us to be able to cache queries. 9441 auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this); 9442 9443 if (Result) 9444 NonConstThis->ReachableQueries.insert(Fn); 9445 else 9446 NonConstThis->UnreachableQueries.insert(Fn); 9447 9448 return Result; 9449 } 9450 9451 /// See AbstractAttribute::updateImpl(...). 9452 ChangeStatus updateImpl(Attributor &A) override { 9453 if (CanReachUnknownCallee) 9454 return ChangeStatus::UNCHANGED; 9455 9456 const AACallEdges &AAEdges = 9457 A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED); 9458 const SetVector<Function *> &Edges = AAEdges.getOptimisticEdges(); 9459 ChangeStatus Change = ChangeStatus::UNCHANGED; 9460 9461 if (AAEdges.hasUnknownCallee()) { 9462 bool OldCanReachUnknown = CanReachUnknownCallee; 9463 CanReachUnknownCallee = true; 9464 return OldCanReachUnknown ? ChangeStatus::UNCHANGED 9465 : ChangeStatus::CHANGED; 9466 } 9467 9468 // Check if any of the unreachable functions become reachable. 9469 for (auto Current = UnreachableQueries.begin(); 9470 Current != UnreachableQueries.end();) { 9471 if (!checkIfReachable(A, Edges, *Current)) { 9472 Current++; 9473 continue; 9474 } 9475 ReachableQueries.insert(*Current); 9476 UnreachableQueries.erase(*Current++); 9477 Change = ChangeStatus::CHANGED; 9478 } 9479 9480 return Change; 9481 } 9482 9483 const std::string getAsStr() const override { 9484 size_t QueryCount = ReachableQueries.size() + UnreachableQueries.size(); 9485 9486 return "FunctionReachability [" + std::to_string(ReachableQueries.size()) + 9487 "," + std::to_string(QueryCount) + "]"; 9488 } 9489 9490 void trackStatistics() const override {} 9491 9492 private: 9493 bool canReachUnknownCallee() const override { return CanReachUnknownCallee; } 9494 9495 bool checkIfReachable(Attributor &A, const SetVector<Function *> &Edges, 9496 Function *Fn) const { 9497 if (Edges.count(Fn)) 9498 return true; 9499 9500 for (Function *Edge : Edges) { 9501 // We don't need a dependency if the result is reachable. 9502 const AAFunctionReachability &EdgeReachability = 9503 A.getAAFor<AAFunctionReachability>(*this, IRPosition::function(*Edge), 9504 DepClassTy::NONE); 9505 9506 if (EdgeReachability.canReach(A, Fn)) 9507 return true; 9508 } 9509 for (Function *Fn : Edges) 9510 A.getAAFor<AAFunctionReachability>(*this, IRPosition::function(*Fn), 9511 DepClassTy::REQUIRED); 9512 9513 return false; 9514 } 9515 9516 /// Set of functions that we know for sure is reachable. 9517 SmallPtrSet<Function *, 8> ReachableQueries; 9518 9519 /// Set of functions that are unreachable, but might become reachable. 9520 SmallPtrSet<Function *, 8> UnreachableQueries; 9521 9522 /// If we can reach a function with a call to a unknown function we assume 9523 /// that we can reach any function. 9524 bool CanReachUnknownCallee = false; 9525 }; 9526 9527 } // namespace 9528 9529 AACallGraphNode *AACallEdgeIterator::operator*() const { 9530 return static_cast<AACallGraphNode *>(const_cast<AACallEdges *>( 9531 &A.getOrCreateAAFor<AACallEdges>(IRPosition::function(**I)))); 9532 } 9533 9534 void AttributorCallGraph::print() { llvm::WriteGraph(outs(), this); } 9535 9536 const char AAReturnedValues::ID = 0; 9537 const char AANoUnwind::ID = 0; 9538 const char AANoSync::ID = 0; 9539 const char AANoFree::ID = 0; 9540 const char AANonNull::ID = 0; 9541 const char AANoRecurse::ID = 0; 9542 const char AAWillReturn::ID = 0; 9543 const char AAUndefinedBehavior::ID = 0; 9544 const char AANoAlias::ID = 0; 9545 const char AAReachability::ID = 0; 9546 const char AANoReturn::ID = 0; 9547 const char AAIsDead::ID = 0; 9548 const char AADereferenceable::ID = 0; 9549 const char AAAlign::ID = 0; 9550 const char AANoCapture::ID = 0; 9551 const char AAValueSimplify::ID = 0; 9552 const char AAHeapToStack::ID = 0; 9553 const char AAPrivatizablePtr::ID = 0; 9554 const char AAMemoryBehavior::ID = 0; 9555 const char AAMemoryLocation::ID = 0; 9556 const char AAValueConstantRange::ID = 0; 9557 const char AAPotentialValues::ID = 0; 9558 const char AANoUndef::ID = 0; 9559 const char AACallEdges::ID = 0; 9560 const char AAFunctionReachability::ID = 0; 9561 const char AAPointerInfo::ID = 0; 9562 9563 // Macro magic to create the static generator function for attributes that 9564 // follow the naming scheme. 9565 9566 #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \ 9567 case IRPosition::PK: \ 9568 llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!"); 9569 9570 #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \ 9571 case IRPosition::PK: \ 9572 AA = new (A.Allocator) CLASS##SUFFIX(IRP, A); \ 9573 ++NumAAs; \ 9574 break; 9575 9576 #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 9577 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 9578 CLASS *AA = nullptr; \ 9579 switch (IRP.getPositionKind()) { \ 9580 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 9581 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ 9582 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ 9583 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ 9584 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ 9585 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ 9586 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 9587 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ 9588 } \ 9589 return *AA; \ 9590 } 9591 9592 #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 9593 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 9594 CLASS *AA = nullptr; \ 9595 switch (IRP.getPositionKind()) { \ 9596 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 9597 SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \ 9598 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ 9599 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ 9600 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ 9601 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ 9602 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ 9603 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ 9604 } \ 9605 return *AA; \ 9606 } 9607 9608 #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 9609 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 9610 CLASS *AA = nullptr; \ 9611 switch (IRP.getPositionKind()) { \ 9612 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 9613 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 9614 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ 9615 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ 9616 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ 9617 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ 9618 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ 9619 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ 9620 } \ 9621 return *AA; \ 9622 } 9623 9624 #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 9625 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 9626 CLASS *AA = nullptr; \ 9627 switch (IRP.getPositionKind()) { \ 9628 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 9629 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ 9630 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ 9631 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ 9632 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ 9633 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ 9634 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ 9635 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 9636 } \ 9637 return *AA; \ 9638 } 9639 9640 #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 9641 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 9642 CLASS *AA = nullptr; \ 9643 switch (IRP.getPositionKind()) { \ 9644 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 9645 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ 9646 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 9647 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ 9648 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ 9649 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ 9650 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ 9651 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ 9652 } \ 9653 return *AA; \ 9654 } 9655 9656 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) 9657 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) 9658 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) 9659 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) 9660 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) 9661 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) 9662 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation) 9663 9664 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) 9665 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) 9666 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr) 9667 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) 9668 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) 9669 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture) 9670 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange) 9671 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPotentialValues) 9672 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUndef) 9673 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPointerInfo) 9674 9675 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify) 9676 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) 9677 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) 9678 9679 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack) 9680 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability) 9681 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior) 9682 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AACallEdges) 9683 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAFunctionReachability) 9684 9685 CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior) 9686 9687 #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION 9688 #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION 9689 #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION 9690 #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION 9691 #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION 9692 #undef SWITCH_PK_CREATE 9693 #undef SWITCH_PK_INV 9694