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