1 //===- Attributor.cpp - Module-wide attribute 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 // This file implements an inter procedural pass that deduces and/or propagating 10 // attributes. This is done in an abstract interpretation style fixpoint 11 // iteration. See the Attributor.h file comment and the class descriptions in 12 // that file for more information. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/Transforms/IPO/Attributor.h" 17 18 #include "llvm/ADT/DepthFirstIterator.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/SmallPtrSet.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/Analysis/CaptureTracking.h" 24 #include "llvm/Analysis/EHPersonalities.h" 25 #include "llvm/Analysis/GlobalsModRef.h" 26 #include "llvm/Analysis/Loads.h" 27 #include "llvm/Analysis/MemoryBuiltins.h" 28 #include "llvm/Analysis/ValueTracking.h" 29 #include "llvm/IR/Argument.h" 30 #include "llvm/IR/Attributes.h" 31 #include "llvm/IR/CFG.h" 32 #include "llvm/IR/InstIterator.h" 33 #include "llvm/IR/IntrinsicInst.h" 34 #include "llvm/InitializePasses.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Support/Debug.h" 37 #include "llvm/Support/raw_ostream.h" 38 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 39 #include "llvm/Transforms/Utils/Local.h" 40 41 #include <cassert> 42 43 using namespace llvm; 44 45 #define DEBUG_TYPE "attributor" 46 47 STATISTIC(NumFnWithExactDefinition, 48 "Number of function with exact definitions"); 49 STATISTIC(NumFnWithoutExactDefinition, 50 "Number of function without exact definitions"); 51 STATISTIC(NumAttributesTimedOut, 52 "Number of abstract attributes timed out before fixpoint"); 53 STATISTIC(NumAttributesValidFixpoint, 54 "Number of abstract attributes in a valid fixpoint state"); 55 STATISTIC(NumAttributesManifested, 56 "Number of abstract attributes manifested in IR"); 57 STATISTIC(NumAttributesFixedDueToRequiredDependences, 58 "Number of abstract attributes fixed due to required dependences"); 59 60 // Some helper macros to deal with statistics tracking. 61 // 62 // Usage: 63 // For simple IR attribute tracking overload trackStatistics in the abstract 64 // attribute and choose the right STATS_DECLTRACK_********* macro, 65 // e.g.,: 66 // void trackStatistics() const override { 67 // STATS_DECLTRACK_ARG_ATTR(returned) 68 // } 69 // If there is a single "increment" side one can use the macro 70 // STATS_DECLTRACK with a custom message. If there are multiple increment 71 // sides, STATS_DECL and STATS_TRACK can also be used separatly. 72 // 73 #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \ 74 ("Number of " #TYPE " marked '" #NAME "'") 75 #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME 76 #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG); 77 #define STATS_DECL(NAME, TYPE, MSG) \ 78 STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG); 79 #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE)); 80 #define STATS_DECLTRACK(NAME, TYPE, MSG) \ 81 { \ 82 STATS_DECL(NAME, TYPE, MSG) \ 83 STATS_TRACK(NAME, TYPE) \ 84 } 85 #define STATS_DECLTRACK_ARG_ATTR(NAME) \ 86 STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME)) 87 #define STATS_DECLTRACK_CSARG_ATTR(NAME) \ 88 STATS_DECLTRACK(NAME, CSArguments, \ 89 BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME)) 90 #define STATS_DECLTRACK_FN_ATTR(NAME) \ 91 STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME)) 92 #define STATS_DECLTRACK_CS_ATTR(NAME) \ 93 STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME)) 94 #define STATS_DECLTRACK_FNRET_ATTR(NAME) \ 95 STATS_DECLTRACK(NAME, FunctionReturn, \ 96 BUILD_STAT_MSG_IR_ATTR(function returns, NAME)) 97 #define STATS_DECLTRACK_CSRET_ATTR(NAME) \ 98 STATS_DECLTRACK(NAME, CSReturn, \ 99 BUILD_STAT_MSG_IR_ATTR(call site returns, NAME)) 100 #define STATS_DECLTRACK_FLOATING_ATTR(NAME) \ 101 STATS_DECLTRACK(NAME, Floating, \ 102 ("Number of floating values known to be '" #NAME "'")) 103 104 // TODO: Determine a good default value. 105 // 106 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads 107 // (when run with the first 5 abstract attributes). The results also indicate 108 // that we never reach 32 iterations but always find a fixpoint sooner. 109 // 110 // This will become more evolved once we perform two interleaved fixpoint 111 // iterations: bottom-up and top-down. 112 static cl::opt<unsigned> 113 MaxFixpointIterations("attributor-max-iterations", cl::Hidden, 114 cl::desc("Maximal number of fixpoint iterations."), 115 cl::init(32)); 116 static cl::opt<bool> VerifyMaxFixpointIterations( 117 "attributor-max-iterations-verify", cl::Hidden, 118 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"), 119 cl::init(false)); 120 121 static cl::opt<bool> DisableAttributor( 122 "attributor-disable", cl::Hidden, 123 cl::desc("Disable the attributor inter-procedural deduction pass."), 124 cl::init(true)); 125 126 static cl::opt<bool> AnnotateDeclarationCallSites( 127 "attributor-annotate-decl-cs", cl::Hidden, 128 cl::desc("Annoate call sites of function declarations."), cl::init(false)); 129 130 static cl::opt<bool> ManifestInternal( 131 "attributor-manifest-internal", cl::Hidden, 132 cl::desc("Manifest Attributor internal string attributes."), 133 cl::init(false)); 134 135 static cl::opt<unsigned> DepRecInterval( 136 "attributor-dependence-recompute-interval", cl::Hidden, 137 cl::desc("Number of iterations until dependences are recomputed."), 138 cl::init(4)); 139 140 static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion", 141 cl::init(true), cl::Hidden); 142 143 static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128), 144 cl::Hidden); 145 146 /// Logic operators for the change status enum class. 147 /// 148 ///{ 149 ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) { 150 return l == ChangeStatus::CHANGED ? l : r; 151 } 152 ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) { 153 return l == ChangeStatus::UNCHANGED ? l : r; 154 } 155 ///} 156 157 /// Recursively visit all values that might become \p IRP at some point. This 158 /// will be done by looking through cast instructions, selects, phis, and calls 159 /// with the "returned" attribute. Once we cannot look through the value any 160 /// further, the callback \p VisitValueCB is invoked and passed the current 161 /// value, the \p State, and a flag to indicate if we stripped anything. To 162 /// limit how much effort is invested, we will never visit more values than 163 /// specified by \p MaxValues. 164 template <typename AAType, typename StateTy> 165 static bool genericValueTraversal( 166 Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State, 167 const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB, 168 int MaxValues = 8) { 169 170 const AAIsDead *LivenessAA = nullptr; 171 if (IRP.getAnchorScope()) 172 LivenessAA = &A.getAAFor<AAIsDead>( 173 QueryingAA, IRPosition::function(*IRP.getAnchorScope()), 174 /* TrackDependence */ false); 175 bool AnyDead = false; 176 177 // TODO: Use Positions here to allow context sensitivity in VisitValueCB 178 SmallPtrSet<Value *, 16> Visited; 179 SmallVector<Value *, 16> Worklist; 180 Worklist.push_back(&IRP.getAssociatedValue()); 181 182 int Iteration = 0; 183 do { 184 Value *V = Worklist.pop_back_val(); 185 186 // Check if we should process the current value. To prevent endless 187 // recursion keep a record of the values we followed! 188 if (!Visited.insert(V).second) 189 continue; 190 191 // Make sure we limit the compile time for complex expressions. 192 if (Iteration++ >= MaxValues) 193 return false; 194 195 // Explicitly look through calls with a "returned" attribute if we do 196 // not have a pointer as stripPointerCasts only works on them. 197 Value *NewV = nullptr; 198 if (V->getType()->isPointerTy()) { 199 NewV = V->stripPointerCasts(); 200 } else { 201 CallSite CS(V); 202 if (CS && CS.getCalledFunction()) { 203 for (Argument &Arg : CS.getCalledFunction()->args()) 204 if (Arg.hasReturnedAttr()) { 205 NewV = CS.getArgOperand(Arg.getArgNo()); 206 break; 207 } 208 } 209 } 210 if (NewV && NewV != V) { 211 Worklist.push_back(NewV); 212 continue; 213 } 214 215 // Look through select instructions, visit both potential values. 216 if (auto *SI = dyn_cast<SelectInst>(V)) { 217 Worklist.push_back(SI->getTrueValue()); 218 Worklist.push_back(SI->getFalseValue()); 219 continue; 220 } 221 222 // Look through phi nodes, visit all live operands. 223 if (auto *PHI = dyn_cast<PHINode>(V)) { 224 assert(LivenessAA && 225 "Expected liveness in the presence of instructions!"); 226 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { 227 const BasicBlock *IncomingBB = PHI->getIncomingBlock(u); 228 if (LivenessAA->isAssumedDead(IncomingBB->getTerminator())) { 229 AnyDead = true; 230 continue; 231 } 232 Worklist.push_back(PHI->getIncomingValue(u)); 233 } 234 continue; 235 } 236 237 // Once a leaf is reached we inform the user through the callback. 238 if (!VisitValueCB(*V, State, Iteration > 1)) 239 return false; 240 } while (!Worklist.empty()); 241 242 // If we actually used liveness information so we have to record a dependence. 243 if (AnyDead) 244 A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL); 245 246 // All values have been visited. 247 return true; 248 } 249 250 /// Return true if \p New is equal or worse than \p Old. 251 static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { 252 if (!Old.isIntAttribute()) 253 return true; 254 255 return Old.getValueAsInt() >= New.getValueAsInt(); 256 } 257 258 /// Return true if the information provided by \p Attr was added to the 259 /// attribute list \p Attrs. This is only the case if it was not already present 260 /// in \p Attrs at the position describe by \p PK and \p AttrIdx. 261 static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, 262 AttributeList &Attrs, int AttrIdx) { 263 264 if (Attr.isEnumAttribute()) { 265 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 266 if (Attrs.hasAttribute(AttrIdx, Kind)) 267 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) 268 return false; 269 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); 270 return true; 271 } 272 if (Attr.isStringAttribute()) { 273 StringRef Kind = Attr.getKindAsString(); 274 if (Attrs.hasAttribute(AttrIdx, Kind)) 275 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) 276 return false; 277 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); 278 return true; 279 } 280 if (Attr.isIntAttribute()) { 281 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 282 if (Attrs.hasAttribute(AttrIdx, Kind)) 283 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) 284 return false; 285 Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind); 286 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); 287 return true; 288 } 289 290 llvm_unreachable("Expected enum or string attribute!"); 291 } 292 static const Value *getPointerOperand(const Instruction *I) { 293 if (auto *LI = dyn_cast<LoadInst>(I)) 294 if (!LI->isVolatile()) 295 return LI->getPointerOperand(); 296 297 if (auto *SI = dyn_cast<StoreInst>(I)) 298 if (!SI->isVolatile()) 299 return SI->getPointerOperand(); 300 301 if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) 302 if (!CXI->isVolatile()) 303 return CXI->getPointerOperand(); 304 305 if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) 306 if (!RMWI->isVolatile()) 307 return RMWI->getPointerOperand(); 308 309 return nullptr; 310 } 311 static const Value * 312 getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset, 313 const DataLayout &DL, 314 bool AllowNonInbounds = false) { 315 const Value *Ptr = getPointerOperand(I); 316 if (!Ptr) 317 return nullptr; 318 319 return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL, 320 AllowNonInbounds); 321 } 322 323 ChangeStatus AbstractAttribute::update(Attributor &A) { 324 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 325 if (getState().isAtFixpoint()) 326 return HasChanged; 327 328 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n"); 329 330 HasChanged = updateImpl(A); 331 332 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this 333 << "\n"); 334 335 return HasChanged; 336 } 337 338 ChangeStatus 339 IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP, 340 const ArrayRef<Attribute> &DeducedAttrs) { 341 Function *ScopeFn = IRP.getAssociatedFunction(); 342 IRPosition::Kind PK = IRP.getPositionKind(); 343 344 // In the following some generic code that will manifest attributes in 345 // DeducedAttrs if they improve the current IR. Due to the different 346 // annotation positions we use the underlying AttributeList interface. 347 348 AttributeList Attrs; 349 switch (PK) { 350 case IRPosition::IRP_INVALID: 351 case IRPosition::IRP_FLOAT: 352 return ChangeStatus::UNCHANGED; 353 case IRPosition::IRP_ARGUMENT: 354 case IRPosition::IRP_FUNCTION: 355 case IRPosition::IRP_RETURNED: 356 Attrs = ScopeFn->getAttributes(); 357 break; 358 case IRPosition::IRP_CALL_SITE: 359 case IRPosition::IRP_CALL_SITE_RETURNED: 360 case IRPosition::IRP_CALL_SITE_ARGUMENT: 361 Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes(); 362 break; 363 } 364 365 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 366 LLVMContext &Ctx = IRP.getAnchorValue().getContext(); 367 for (const Attribute &Attr : DeducedAttrs) { 368 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx())) 369 continue; 370 371 HasChanged = ChangeStatus::CHANGED; 372 } 373 374 if (HasChanged == ChangeStatus::UNCHANGED) 375 return HasChanged; 376 377 switch (PK) { 378 case IRPosition::IRP_ARGUMENT: 379 case IRPosition::IRP_FUNCTION: 380 case IRPosition::IRP_RETURNED: 381 ScopeFn->setAttributes(Attrs); 382 break; 383 case IRPosition::IRP_CALL_SITE: 384 case IRPosition::IRP_CALL_SITE_RETURNED: 385 case IRPosition::IRP_CALL_SITE_ARGUMENT: 386 CallSite(&IRP.getAnchorValue()).setAttributes(Attrs); 387 break; 388 case IRPosition::IRP_INVALID: 389 case IRPosition::IRP_FLOAT: 390 break; 391 } 392 393 return HasChanged; 394 } 395 396 const IRPosition IRPosition::EmptyKey(255); 397 const IRPosition IRPosition::TombstoneKey(256); 398 399 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) { 400 IRPositions.emplace_back(IRP); 401 402 ImmutableCallSite ICS(&IRP.getAnchorValue()); 403 switch (IRP.getPositionKind()) { 404 case IRPosition::IRP_INVALID: 405 case IRPosition::IRP_FLOAT: 406 case IRPosition::IRP_FUNCTION: 407 return; 408 case IRPosition::IRP_ARGUMENT: 409 case IRPosition::IRP_RETURNED: 410 IRPositions.emplace_back( 411 IRPosition::function(*IRP.getAssociatedFunction())); 412 return; 413 case IRPosition::IRP_CALL_SITE: 414 assert(ICS && "Expected call site!"); 415 // TODO: We need to look at the operand bundles similar to the redirection 416 // in CallBase. 417 if (!ICS.hasOperandBundles()) 418 if (const Function *Callee = ICS.getCalledFunction()) 419 IRPositions.emplace_back(IRPosition::function(*Callee)); 420 return; 421 case IRPosition::IRP_CALL_SITE_RETURNED: 422 assert(ICS && "Expected call site!"); 423 // TODO: We need to look at the operand bundles similar to the redirection 424 // in CallBase. 425 if (!ICS.hasOperandBundles()) { 426 if (const Function *Callee = ICS.getCalledFunction()) { 427 IRPositions.emplace_back(IRPosition::returned(*Callee)); 428 IRPositions.emplace_back(IRPosition::function(*Callee)); 429 } 430 } 431 IRPositions.emplace_back( 432 IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction()))); 433 return; 434 case IRPosition::IRP_CALL_SITE_ARGUMENT: { 435 int ArgNo = IRP.getArgNo(); 436 assert(ICS && ArgNo >= 0 && "Expected call site!"); 437 // TODO: We need to look at the operand bundles similar to the redirection 438 // in CallBase. 439 if (!ICS.hasOperandBundles()) { 440 const Function *Callee = ICS.getCalledFunction(); 441 if (Callee && Callee->arg_size() > unsigned(ArgNo)) 442 IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo))); 443 if (Callee) 444 IRPositions.emplace_back(IRPosition::function(*Callee)); 445 } 446 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue())); 447 return; 448 } 449 } 450 } 451 452 bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs, 453 bool IgnoreSubsumingPositions) const { 454 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { 455 for (Attribute::AttrKind AK : AKs) 456 if (EquivIRP.getAttr(AK).getKindAsEnum() == AK) 457 return true; 458 // The first position returned by the SubsumingPositionIterator is 459 // always the position itself. If we ignore subsuming positions we 460 // are done after the first iteration. 461 if (IgnoreSubsumingPositions) 462 break; 463 } 464 return false; 465 } 466 467 void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs, 468 SmallVectorImpl<Attribute> &Attrs, 469 bool IgnoreSubsumingPositions) const { 470 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) { 471 for (Attribute::AttrKind AK : AKs) { 472 const Attribute &Attr = EquivIRP.getAttr(AK); 473 if (Attr.getKindAsEnum() == AK) 474 Attrs.push_back(Attr); 475 } 476 // The first position returned by the SubsumingPositionIterator is 477 // always the position itself. If we ignore subsuming positions we 478 // are done after the first iteration. 479 if (IgnoreSubsumingPositions) 480 break; 481 } 482 } 483 484 void IRPosition::verify() { 485 switch (KindOrArgNo) { 486 default: 487 assert(KindOrArgNo >= 0 && "Expected argument or call site argument!"); 488 assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) && 489 "Expected call base or argument for positive attribute index!"); 490 if (isa<Argument>(AnchorVal)) { 491 assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) && 492 "Argument number mismatch!"); 493 assert(cast<Argument>(AnchorVal) == &getAssociatedValue() && 494 "Associated value mismatch!"); 495 } else { 496 assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) && 497 "Call site argument number mismatch!"); 498 assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) == 499 &getAssociatedValue() && 500 "Associated value mismatch!"); 501 } 502 break; 503 case IRP_INVALID: 504 assert(!AnchorVal && "Expected no value for an invalid position!"); 505 break; 506 case IRP_FLOAT: 507 assert((!isa<CallBase>(&getAssociatedValue()) && 508 !isa<Argument>(&getAssociatedValue())) && 509 "Expected specialized kind for call base and argument values!"); 510 break; 511 case IRP_RETURNED: 512 assert(isa<Function>(AnchorVal) && 513 "Expected function for a 'returned' position!"); 514 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 515 break; 516 case IRP_CALL_SITE_RETURNED: 517 assert((isa<CallBase>(AnchorVal)) && 518 "Expected call base for 'call site returned' position!"); 519 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 520 break; 521 case IRP_CALL_SITE: 522 assert((isa<CallBase>(AnchorVal)) && 523 "Expected call base for 'call site function' position!"); 524 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 525 break; 526 case IRP_FUNCTION: 527 assert(isa<Function>(AnchorVal) && 528 "Expected function for a 'function' position!"); 529 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 530 break; 531 } 532 } 533 534 namespace { 535 /// Helper function to clamp a state \p S of type \p StateType with the 536 /// information in \p R and indicate/return if \p S did change (as-in update is 537 /// required to be run again). 538 template <typename StateType> 539 ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) { 540 auto Assumed = S.getAssumed(); 541 S ^= R; 542 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED 543 : ChangeStatus::CHANGED; 544 } 545 546 /// Clamp the information known for all returned values of a function 547 /// (identified by \p QueryingAA) into \p S. 548 template <typename AAType, typename StateType = typename AAType::StateType> 549 static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA, 550 StateType &S) { 551 LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for " 552 << static_cast<const AbstractAttribute &>(QueryingAA) 553 << " into " << S << "\n"); 554 555 assert((QueryingAA.getIRPosition().getPositionKind() == 556 IRPosition::IRP_RETURNED || 557 QueryingAA.getIRPosition().getPositionKind() == 558 IRPosition::IRP_CALL_SITE_RETURNED) && 559 "Can only clamp returned value states for a function returned or call " 560 "site returned position!"); 561 562 // Use an optional state as there might not be any return values and we want 563 // to join (IntegerState::operator&) the state of all there are. 564 Optional<StateType> T; 565 566 // Callback for each possibly returned value. 567 auto CheckReturnValue = [&](Value &RV) -> bool { 568 const IRPosition &RVPos = IRPosition::value(RV); 569 const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos); 570 LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr() 571 << " @ " << RVPos << "\n"); 572 const StateType &AAS = static_cast<const StateType &>(AA.getState()); 573 if (T.hasValue()) 574 *T &= AAS; 575 else 576 T = AAS; 577 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T 578 << "\n"); 579 return T->isValidState(); 580 }; 581 582 if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA)) 583 S.indicatePessimisticFixpoint(); 584 else if (T.hasValue()) 585 S ^= *T; 586 } 587 588 /// Helper class to compose two generic deduction 589 template <typename AAType, typename Base, typename StateType, 590 template <typename...> class F, template <typename...> class G> 591 struct AAComposeTwoGenericDeduction 592 : public F<AAType, G<AAType, Base, StateType>, StateType> { 593 AAComposeTwoGenericDeduction(const IRPosition &IRP) 594 : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {} 595 596 /// See AbstractAttribute::updateImpl(...). 597 ChangeStatus updateImpl(Attributor &A) override { 598 ChangeStatus ChangedF = 599 F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A); 600 ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A); 601 return ChangedF | ChangedG; 602 } 603 }; 604 605 /// Helper class for generic deduction: return value -> returned position. 606 template <typename AAType, typename Base, 607 typename StateType = typename AAType::StateType> 608 struct AAReturnedFromReturnedValues : public Base { 609 AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {} 610 611 /// See AbstractAttribute::updateImpl(...). 612 ChangeStatus updateImpl(Attributor &A) override { 613 StateType S; 614 clampReturnedValueStates<AAType, StateType>(A, *this, S); 615 // TODO: If we know we visited all returned values, thus no are assumed 616 // dead, we can take the known information from the state T. 617 return clampStateAndIndicateChange<StateType>(this->getState(), S); 618 } 619 }; 620 621 /// Clamp the information known at all call sites for a given argument 622 /// (identified by \p QueryingAA) into \p S. 623 template <typename AAType, typename StateType = typename AAType::StateType> 624 static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA, 625 StateType &S) { 626 LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for " 627 << static_cast<const AbstractAttribute &>(QueryingAA) 628 << " into " << S << "\n"); 629 630 assert(QueryingAA.getIRPosition().getPositionKind() == 631 IRPosition::IRP_ARGUMENT && 632 "Can only clamp call site argument states for an argument position!"); 633 634 // Use an optional state as there might not be any return values and we want 635 // to join (IntegerState::operator&) the state of all there are. 636 Optional<StateType> T; 637 638 // The argument number which is also the call site argument number. 639 unsigned ArgNo = QueryingAA.getIRPosition().getArgNo(); 640 641 auto CallSiteCheck = [&](AbstractCallSite ACS) { 642 const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); 643 // Check if a coresponding argument was found or if it is on not associated 644 // (which can happen for callback calls). 645 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) 646 return false; 647 648 const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos); 649 LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction() 650 << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n"); 651 const StateType &AAS = static_cast<const StateType &>(AA.getState()); 652 if (T.hasValue()) 653 *T &= AAS; 654 else 655 T = AAS; 656 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T 657 << "\n"); 658 return T->isValidState(); 659 }; 660 661 if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true)) 662 S.indicatePessimisticFixpoint(); 663 else if (T.hasValue()) 664 S ^= *T; 665 } 666 667 /// Helper class for generic deduction: call site argument -> argument position. 668 template <typename AAType, typename Base, 669 typename StateType = typename AAType::StateType> 670 struct AAArgumentFromCallSiteArguments : public Base { 671 AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {} 672 673 /// See AbstractAttribute::updateImpl(...). 674 ChangeStatus updateImpl(Attributor &A) override { 675 StateType S; 676 clampCallSiteArgumentStates<AAType, StateType>(A, *this, S); 677 // TODO: If we know we visited all incoming values, thus no are assumed 678 // dead, we can take the known information from the state T. 679 return clampStateAndIndicateChange<StateType>(this->getState(), S); 680 } 681 }; 682 683 /// Helper class for generic replication: function returned -> cs returned. 684 template <typename AAType, typename Base, 685 typename StateType = typename AAType::StateType> 686 struct AACallSiteReturnedFromReturned : public Base { 687 AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {} 688 689 /// See AbstractAttribute::updateImpl(...). 690 ChangeStatus updateImpl(Attributor &A) override { 691 assert(this->getIRPosition().getPositionKind() == 692 IRPosition::IRP_CALL_SITE_RETURNED && 693 "Can only wrap function returned positions for call site returned " 694 "positions!"); 695 auto &S = this->getState(); 696 697 const Function *AssociatedFunction = 698 this->getIRPosition().getAssociatedFunction(); 699 if (!AssociatedFunction) 700 return S.indicatePessimisticFixpoint(); 701 702 IRPosition FnPos = IRPosition::returned(*AssociatedFunction); 703 const AAType &AA = A.getAAFor<AAType>(*this, FnPos); 704 return clampStateAndIndicateChange( 705 S, static_cast<const typename AAType::StateType &>(AA.getState())); 706 } 707 }; 708 709 /// Helper class for generic deduction using must-be-executed-context 710 /// Base class is required to have `followUse` method. 711 712 /// bool followUse(Attributor &A, const Use *U, const Instruction *I) 713 /// U - Underlying use. 714 /// I - The user of the \p U. 715 /// `followUse` returns true if the value should be tracked transitively. 716 717 template <typename AAType, typename Base, 718 typename StateType = typename AAType::StateType> 719 struct AAFromMustBeExecutedContext : public Base { 720 AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {} 721 722 void initialize(Attributor &A) override { 723 Base::initialize(A); 724 const IRPosition &IRP = this->getIRPosition(); 725 Instruction *CtxI = IRP.getCtxI(); 726 727 if (!CtxI) 728 return; 729 730 for (const Use &U : IRP.getAssociatedValue().uses()) 731 Uses.insert(&U); 732 } 733 734 /// See AbstractAttribute::updateImpl(...). 735 ChangeStatus updateImpl(Attributor &A) override { 736 auto BeforeState = this->getState(); 737 auto &S = this->getState(); 738 Instruction *CtxI = this->getIRPosition().getCtxI(); 739 if (!CtxI) 740 return ChangeStatus::UNCHANGED; 741 742 MustBeExecutedContextExplorer &Explorer = 743 A.getInfoCache().getMustBeExecutedContextExplorer(); 744 745 auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI); 746 for (unsigned u = 0; u < Uses.size(); ++u) { 747 const Use *U = Uses[u]; 748 if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) { 749 bool Found = Explorer.findInContextOf(UserI, EIt, EEnd); 750 if (Found && Base::followUse(A, U, UserI)) 751 for (const Use &Us : UserI->uses()) 752 Uses.insert(&Us); 753 } 754 } 755 756 return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED; 757 } 758 759 private: 760 /// Container for (transitive) uses of the associated value. 761 SetVector<const Use *> Uses; 762 }; 763 764 template <typename AAType, typename Base, 765 typename StateType = typename AAType::StateType> 766 using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext = 767 AAComposeTwoGenericDeduction<AAType, Base, StateType, 768 AAFromMustBeExecutedContext, 769 AAArgumentFromCallSiteArguments>; 770 771 template <typename AAType, typename Base, 772 typename StateType = typename AAType::StateType> 773 using AACallSiteReturnedFromReturnedAndMustBeExecutedContext = 774 AAComposeTwoGenericDeduction<AAType, Base, StateType, 775 AAFromMustBeExecutedContext, 776 AACallSiteReturnedFromReturned>; 777 778 /// -----------------------NoUnwind Function Attribute-------------------------- 779 780 struct AANoUnwindImpl : AANoUnwind { 781 AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {} 782 783 const std::string getAsStr() const override { 784 return getAssumed() ? "nounwind" : "may-unwind"; 785 } 786 787 /// See AbstractAttribute::updateImpl(...). 788 ChangeStatus updateImpl(Attributor &A) override { 789 auto Opcodes = { 790 (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, 791 (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet, 792 (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; 793 794 auto CheckForNoUnwind = [&](Instruction &I) { 795 if (!I.mayThrow()) 796 return true; 797 798 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { 799 const auto &NoUnwindAA = 800 A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS)); 801 return NoUnwindAA.isAssumedNoUnwind(); 802 } 803 return false; 804 }; 805 806 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes)) 807 return indicatePessimisticFixpoint(); 808 809 return ChangeStatus::UNCHANGED; 810 } 811 }; 812 813 struct AANoUnwindFunction final : public AANoUnwindImpl { 814 AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} 815 816 /// See AbstractAttribute::trackStatistics() 817 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) } 818 }; 819 820 /// NoUnwind attribute deduction for a call sites. 821 struct AANoUnwindCallSite final : AANoUnwindImpl { 822 AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} 823 824 /// See AbstractAttribute::initialize(...). 825 void initialize(Attributor &A) override { 826 AANoUnwindImpl::initialize(A); 827 Function *F = getAssociatedFunction(); 828 if (!F) 829 indicatePessimisticFixpoint(); 830 } 831 832 /// See AbstractAttribute::updateImpl(...). 833 ChangeStatus updateImpl(Attributor &A) override { 834 // TODO: Once we have call site specific value information we can provide 835 // call site specific liveness information and then it makes 836 // sense to specialize attributes for call sites arguments instead of 837 // redirecting requests to the callee argument. 838 Function *F = getAssociatedFunction(); 839 const IRPosition &FnPos = IRPosition::function(*F); 840 auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos); 841 return clampStateAndIndicateChange( 842 getState(), 843 static_cast<const AANoUnwind::StateType &>(FnAA.getState())); 844 } 845 846 /// See AbstractAttribute::trackStatistics() 847 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); } 848 }; 849 850 /// --------------------- Function Return Values ------------------------------- 851 852 /// "Attribute" that collects all potential returned values and the return 853 /// instructions that they arise from. 854 /// 855 /// If there is a unique returned value R, the manifest method will: 856 /// - mark R with the "returned" attribute, if R is an argument. 857 class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { 858 859 /// Mapping of values potentially returned by the associated function to the 860 /// return instructions that might return them. 861 MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues; 862 863 /// Mapping to remember the number of returned values for a call site such 864 /// that we can avoid updates if nothing changed. 865 DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA; 866 867 /// Set of unresolved calls returned by the associated function. 868 SmallSetVector<CallBase *, 4> UnresolvedCalls; 869 870 /// State flags 871 /// 872 ///{ 873 bool IsFixed = false; 874 bool IsValidState = true; 875 ///} 876 877 public: 878 AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {} 879 880 /// See AbstractAttribute::initialize(...). 881 void initialize(Attributor &A) override { 882 // Reset the state. 883 IsFixed = false; 884 IsValidState = true; 885 ReturnedValues.clear(); 886 887 Function *F = getAssociatedFunction(); 888 if (!F) { 889 indicatePessimisticFixpoint(); 890 return; 891 } 892 893 // The map from instruction opcodes to those instructions in the function. 894 auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); 895 896 // Look through all arguments, if one is marked as returned we are done. 897 for (Argument &Arg : F->args()) { 898 if (Arg.hasReturnedAttr()) { 899 auto &ReturnInstSet = ReturnedValues[&Arg]; 900 for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) 901 ReturnInstSet.insert(cast<ReturnInst>(RI)); 902 903 indicateOptimisticFixpoint(); 904 return; 905 } 906 } 907 908 if (!F->hasExactDefinition()) 909 indicatePessimisticFixpoint(); 910 } 911 912 /// See AbstractAttribute::manifest(...). 913 ChangeStatus manifest(Attributor &A) override; 914 915 /// See AbstractAttribute::getState(...). 916 AbstractState &getState() override { return *this; } 917 918 /// See AbstractAttribute::getState(...). 919 const AbstractState &getState() const override { return *this; } 920 921 /// See AbstractAttribute::updateImpl(Attributor &A). 922 ChangeStatus updateImpl(Attributor &A) override; 923 924 llvm::iterator_range<iterator> returned_values() override { 925 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); 926 } 927 928 llvm::iterator_range<const_iterator> returned_values() const override { 929 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); 930 } 931 932 const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override { 933 return UnresolvedCalls; 934 } 935 936 /// Return the number of potential return values, -1 if unknown. 937 size_t getNumReturnValues() const override { 938 return isValidState() ? ReturnedValues.size() : -1; 939 } 940 941 /// Return an assumed unique return value if a single candidate is found. If 942 /// there cannot be one, return a nullptr. If it is not clear yet, return the 943 /// Optional::NoneType. 944 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; 945 946 /// See AbstractState::checkForAllReturnedValues(...). 947 bool checkForAllReturnedValuesAndReturnInsts( 948 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> 949 &Pred) const override; 950 951 /// Pretty print the attribute similar to the IR representation. 952 const std::string getAsStr() const override; 953 954 /// See AbstractState::isAtFixpoint(). 955 bool isAtFixpoint() const override { return IsFixed; } 956 957 /// See AbstractState::isValidState(). 958 bool isValidState() const override { return IsValidState; } 959 960 /// See AbstractState::indicateOptimisticFixpoint(...). 961 ChangeStatus indicateOptimisticFixpoint() override { 962 IsFixed = true; 963 return ChangeStatus::UNCHANGED; 964 } 965 966 ChangeStatus indicatePessimisticFixpoint() override { 967 IsFixed = true; 968 IsValidState = false; 969 return ChangeStatus::CHANGED; 970 } 971 }; 972 973 ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { 974 ChangeStatus Changed = ChangeStatus::UNCHANGED; 975 976 // Bookkeeping. 977 assert(isValidState()); 978 STATS_DECLTRACK(KnownReturnValues, FunctionReturn, 979 "Number of function with known return values"); 980 981 // Check if we have an assumed unique return value that we could manifest. 982 Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); 983 984 if (!UniqueRV.hasValue() || !UniqueRV.getValue()) 985 return Changed; 986 987 // Bookkeeping. 988 STATS_DECLTRACK(UniqueReturnValue, FunctionReturn, 989 "Number of function with unique return"); 990 991 // Callback to replace the uses of CB with the constant C. 992 auto ReplaceCallSiteUsersWith = [](CallBase &CB, Constant &C) { 993 if (CB.getNumUses() == 0 || CB.isMustTailCall()) 994 return ChangeStatus::UNCHANGED; 995 CB.replaceAllUsesWith(&C); 996 return ChangeStatus::CHANGED; 997 }; 998 999 // If the assumed unique return value is an argument, annotate it. 1000 if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { 1001 // TODO: This should be handled differently! 1002 this->AnchorVal = UniqueRVArg; 1003 this->KindOrArgNo = UniqueRVArg->getArgNo(); 1004 Changed = IRAttribute::manifest(A); 1005 } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) { 1006 // We can replace the returned value with the unique returned constant. 1007 Value &AnchorValue = getAnchorValue(); 1008 if (Function *F = dyn_cast<Function>(&AnchorValue)) { 1009 for (const Use &U : F->uses()) 1010 if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) 1011 if (CB->isCallee(&U)) { 1012 Constant *RVCCast = 1013 CB->getType() == RVC->getType() 1014 ? RVC 1015 : ConstantExpr::getTruncOrBitCast(RVC, CB->getType()); 1016 Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed; 1017 } 1018 } else { 1019 assert(isa<CallBase>(AnchorValue) && 1020 "Expcected a function or call base anchor!"); 1021 Constant *RVCCast = 1022 AnchorValue.getType() == RVC->getType() 1023 ? RVC 1024 : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType()); 1025 Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast); 1026 } 1027 if (Changed == ChangeStatus::CHANGED) 1028 STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn, 1029 "Number of function returns replaced by constant return"); 1030 } 1031 1032 return Changed; 1033 } 1034 1035 const std::string AAReturnedValuesImpl::getAsStr() const { 1036 return (isAtFixpoint() ? "returns(#" : "may-return(#") + 1037 (isValidState() ? std::to_string(getNumReturnValues()) : "?") + 1038 ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]"; 1039 } 1040 1041 Optional<Value *> 1042 AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { 1043 // If checkForAllReturnedValues provides a unique value, ignoring potential 1044 // undef values that can also be present, it is assumed to be the actual 1045 // return value and forwarded to the caller of this method. If there are 1046 // multiple, a nullptr is returned indicating there cannot be a unique 1047 // returned value. 1048 Optional<Value *> UniqueRV; 1049 1050 auto Pred = [&](Value &RV) -> bool { 1051 // If we found a second returned value and neither the current nor the saved 1052 // one is an undef, there is no unique returned value. Undefs are special 1053 // since we can pretend they have any value. 1054 if (UniqueRV.hasValue() && UniqueRV != &RV && 1055 !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) { 1056 UniqueRV = nullptr; 1057 return false; 1058 } 1059 1060 // Do not overwrite a value with an undef. 1061 if (!UniqueRV.hasValue() || !isa<UndefValue>(RV)) 1062 UniqueRV = &RV; 1063 1064 return true; 1065 }; 1066 1067 if (!A.checkForAllReturnedValues(Pred, *this)) 1068 UniqueRV = nullptr; 1069 1070 return UniqueRV; 1071 } 1072 1073 bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( 1074 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> 1075 &Pred) const { 1076 if (!isValidState()) 1077 return false; 1078 1079 // Check all returned values but ignore call sites as long as we have not 1080 // encountered an overdefined one during an update. 1081 for (auto &It : ReturnedValues) { 1082 Value *RV = It.first; 1083 1084 CallBase *CB = dyn_cast<CallBase>(RV); 1085 if (CB && !UnresolvedCalls.count(CB)) 1086 continue; 1087 1088 if (!Pred(*RV, It.second)) 1089 return false; 1090 } 1091 1092 return true; 1093 } 1094 1095 ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { 1096 size_t NumUnresolvedCalls = UnresolvedCalls.size(); 1097 bool Changed = false; 1098 1099 // State used in the value traversals starting in returned values. 1100 struct RVState { 1101 // The map in which we collect return values -> return instrs. 1102 decltype(ReturnedValues) &RetValsMap; 1103 // The flag to indicate a change. 1104 bool &Changed; 1105 // The return instrs we come from. 1106 SmallSetVector<ReturnInst *, 4> RetInsts; 1107 }; 1108 1109 // Callback for a leaf value returned by the associated function. 1110 auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool { 1111 auto Size = RVS.RetValsMap[&Val].size(); 1112 RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end()); 1113 bool Inserted = RVS.RetValsMap[&Val].size() != Size; 1114 RVS.Changed |= Inserted; 1115 LLVM_DEBUG({ 1116 if (Inserted) 1117 dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val 1118 << " => " << RVS.RetInsts.size() << "\n"; 1119 }); 1120 return true; 1121 }; 1122 1123 // Helper method to invoke the generic value traversal. 1124 auto VisitReturnedValue = [&](Value &RV, RVState &RVS) { 1125 IRPosition RetValPos = IRPosition::value(RV); 1126 return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this, 1127 RVS, VisitValueCB); 1128 }; 1129 1130 // Callback for all "return intructions" live in the associated function. 1131 auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) { 1132 ReturnInst &Ret = cast<ReturnInst>(I); 1133 RVState RVS({ReturnedValues, Changed, {}}); 1134 RVS.RetInsts.insert(&Ret); 1135 return VisitReturnedValue(*Ret.getReturnValue(), RVS); 1136 }; 1137 1138 // Start by discovering returned values from all live returned instructions in 1139 // the associated function. 1140 if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret})) 1141 return indicatePessimisticFixpoint(); 1142 1143 // Once returned values "directly" present in the code are handled we try to 1144 // resolve returned calls. 1145 decltype(ReturnedValues) NewRVsMap; 1146 for (auto &It : ReturnedValues) { 1147 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first 1148 << " by #" << It.second.size() << " RIs\n"); 1149 CallBase *CB = dyn_cast<CallBase>(It.first); 1150 if (!CB || UnresolvedCalls.count(CB)) 1151 continue; 1152 1153 if (!CB->getCalledFunction()) { 1154 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB 1155 << "\n"); 1156 UnresolvedCalls.insert(CB); 1157 continue; 1158 } 1159 1160 // TODO: use the function scope once we have call site AAReturnedValues. 1161 const auto &RetValAA = A.getAAFor<AAReturnedValues>( 1162 *this, IRPosition::function(*CB->getCalledFunction())); 1163 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: " 1164 << static_cast<const AbstractAttribute &>(RetValAA) 1165 << "\n"); 1166 1167 // Skip dead ends, thus if we do not know anything about the returned 1168 // call we mark it as unresolved and it will stay that way. 1169 if (!RetValAA.getState().isValidState()) { 1170 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB 1171 << "\n"); 1172 UnresolvedCalls.insert(CB); 1173 continue; 1174 } 1175 1176 // Do not try to learn partial information. If the callee has unresolved 1177 // return values we will treat the call as unresolved/opaque. 1178 auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls(); 1179 if (!RetValAAUnresolvedCalls.empty()) { 1180 UnresolvedCalls.insert(CB); 1181 continue; 1182 } 1183 1184 // Now check if we can track transitively returned values. If possible, thus 1185 // if all return value can be represented in the current scope, do so. 1186 bool Unresolved = false; 1187 for (auto &RetValAAIt : RetValAA.returned_values()) { 1188 Value *RetVal = RetValAAIt.first; 1189 if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) || 1190 isa<Constant>(RetVal)) 1191 continue; 1192 // Anything that did not fit in the above categories cannot be resolved, 1193 // mark the call as unresolved. 1194 LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value " 1195 "cannot be translated: " 1196 << *RetVal << "\n"); 1197 UnresolvedCalls.insert(CB); 1198 Unresolved = true; 1199 break; 1200 } 1201 1202 if (Unresolved) 1203 continue; 1204 1205 // Now track transitively returned values. 1206 unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB]; 1207 if (NumRetAA == RetValAA.getNumReturnValues()) { 1208 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not " 1209 "changed since it was seen last\n"); 1210 continue; 1211 } 1212 NumRetAA = RetValAA.getNumReturnValues(); 1213 1214 for (auto &RetValAAIt : RetValAA.returned_values()) { 1215 Value *RetVal = RetValAAIt.first; 1216 if (Argument *Arg = dyn_cast<Argument>(RetVal)) { 1217 // Arguments are mapped to call site operands and we begin the traversal 1218 // again. 1219 bool Unused = false; 1220 RVState RVS({NewRVsMap, Unused, RetValAAIt.second}); 1221 VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS); 1222 continue; 1223 } else if (isa<CallBase>(RetVal)) { 1224 // Call sites are resolved by the callee attribute over time, no need to 1225 // do anything for us. 1226 continue; 1227 } else if (isa<Constant>(RetVal)) { 1228 // Constants are valid everywhere, we can simply take them. 1229 NewRVsMap[RetVal].insert(It.second.begin(), It.second.end()); 1230 continue; 1231 } 1232 } 1233 } 1234 1235 // To avoid modifications to the ReturnedValues map while we iterate over it 1236 // we kept record of potential new entries in a copy map, NewRVsMap. 1237 for (auto &It : NewRVsMap) { 1238 assert(!It.second.empty() && "Entry does not add anything."); 1239 auto &ReturnInsts = ReturnedValues[It.first]; 1240 for (ReturnInst *RI : It.second) 1241 if (ReturnInsts.insert(RI)) { 1242 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value " 1243 << *It.first << " => " << *RI << "\n"); 1244 Changed = true; 1245 } 1246 } 1247 1248 Changed |= (NumUnresolvedCalls != UnresolvedCalls.size()); 1249 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 1250 } 1251 1252 struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { 1253 AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} 1254 1255 /// See AbstractAttribute::trackStatistics() 1256 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) } 1257 }; 1258 1259 /// Returned values information for a call sites. 1260 struct AAReturnedValuesCallSite final : AAReturnedValuesImpl { 1261 AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} 1262 1263 /// See AbstractAttribute::initialize(...). 1264 void initialize(Attributor &A) override { 1265 // TODO: Once we have call site specific value information we can provide 1266 // call site specific liveness information and then it makes 1267 // sense to specialize attributes for call sites instead of 1268 // redirecting requests to the callee. 1269 llvm_unreachable("Abstract attributes for returned values are not " 1270 "supported for call sites yet!"); 1271 } 1272 1273 /// See AbstractAttribute::updateImpl(...). 1274 ChangeStatus updateImpl(Attributor &A) override { 1275 return indicatePessimisticFixpoint(); 1276 } 1277 1278 /// See AbstractAttribute::trackStatistics() 1279 void trackStatistics() const override {} 1280 }; 1281 1282 /// ------------------------ NoSync Function Attribute ------------------------- 1283 1284 struct AANoSyncImpl : AANoSync { 1285 AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {} 1286 1287 const std::string getAsStr() const override { 1288 return getAssumed() ? "nosync" : "may-sync"; 1289 } 1290 1291 /// See AbstractAttribute::updateImpl(...). 1292 ChangeStatus updateImpl(Attributor &A) override; 1293 1294 /// Helper function used to determine whether an instruction is non-relaxed 1295 /// atomic. In other words, if an atomic instruction does not have unordered 1296 /// or monotonic ordering 1297 static bool isNonRelaxedAtomic(Instruction *I); 1298 1299 /// Helper function used to determine whether an instruction is volatile. 1300 static bool isVolatile(Instruction *I); 1301 1302 /// Helper function uset to check if intrinsic is volatile (memcpy, memmove, 1303 /// memset). 1304 static bool isNoSyncIntrinsic(Instruction *I); 1305 }; 1306 1307 bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) { 1308 if (!I->isAtomic()) 1309 return false; 1310 1311 AtomicOrdering Ordering; 1312 switch (I->getOpcode()) { 1313 case Instruction::AtomicRMW: 1314 Ordering = cast<AtomicRMWInst>(I)->getOrdering(); 1315 break; 1316 case Instruction::Store: 1317 Ordering = cast<StoreInst>(I)->getOrdering(); 1318 break; 1319 case Instruction::Load: 1320 Ordering = cast<LoadInst>(I)->getOrdering(); 1321 break; 1322 case Instruction::Fence: { 1323 auto *FI = cast<FenceInst>(I); 1324 if (FI->getSyncScopeID() == SyncScope::SingleThread) 1325 return false; 1326 Ordering = FI->getOrdering(); 1327 break; 1328 } 1329 case Instruction::AtomicCmpXchg: { 1330 AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering(); 1331 AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering(); 1332 // Only if both are relaxed, than it can be treated as relaxed. 1333 // Otherwise it is non-relaxed. 1334 if (Success != AtomicOrdering::Unordered && 1335 Success != AtomicOrdering::Monotonic) 1336 return true; 1337 if (Failure != AtomicOrdering::Unordered && 1338 Failure != AtomicOrdering::Monotonic) 1339 return true; 1340 return false; 1341 } 1342 default: 1343 llvm_unreachable( 1344 "New atomic operations need to be known in the attributor."); 1345 } 1346 1347 // Relaxed. 1348 if (Ordering == AtomicOrdering::Unordered || 1349 Ordering == AtomicOrdering::Monotonic) 1350 return false; 1351 return true; 1352 } 1353 1354 /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics. 1355 /// FIXME: We should ipmrove the handling of intrinsics. 1356 bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) { 1357 if (auto *II = dyn_cast<IntrinsicInst>(I)) { 1358 switch (II->getIntrinsicID()) { 1359 /// Element wise atomic memory intrinsics are can only be unordered, 1360 /// therefore nosync. 1361 case Intrinsic::memset_element_unordered_atomic: 1362 case Intrinsic::memmove_element_unordered_atomic: 1363 case Intrinsic::memcpy_element_unordered_atomic: 1364 return true; 1365 case Intrinsic::memset: 1366 case Intrinsic::memmove: 1367 case Intrinsic::memcpy: 1368 if (!cast<MemIntrinsic>(II)->isVolatile()) 1369 return true; 1370 return false; 1371 default: 1372 return false; 1373 } 1374 } 1375 return false; 1376 } 1377 1378 bool AANoSyncImpl::isVolatile(Instruction *I) { 1379 assert(!ImmutableCallSite(I) && !isa<CallBase>(I) && 1380 "Calls should not be checked here"); 1381 1382 switch (I->getOpcode()) { 1383 case Instruction::AtomicRMW: 1384 return cast<AtomicRMWInst>(I)->isVolatile(); 1385 case Instruction::Store: 1386 return cast<StoreInst>(I)->isVolatile(); 1387 case Instruction::Load: 1388 return cast<LoadInst>(I)->isVolatile(); 1389 case Instruction::AtomicCmpXchg: 1390 return cast<AtomicCmpXchgInst>(I)->isVolatile(); 1391 default: 1392 return false; 1393 } 1394 } 1395 1396 ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { 1397 1398 auto CheckRWInstForNoSync = [&](Instruction &I) { 1399 /// We are looking for volatile instructions or Non-Relaxed atomics. 1400 /// FIXME: We should improve the handling of intrinsics. 1401 1402 if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I)) 1403 return true; 1404 1405 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { 1406 if (ICS.hasFnAttr(Attribute::NoSync)) 1407 return true; 1408 1409 const auto &NoSyncAA = 1410 A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS)); 1411 if (NoSyncAA.isAssumedNoSync()) 1412 return true; 1413 return false; 1414 } 1415 1416 if (!isVolatile(&I) && !isNonRelaxedAtomic(&I)) 1417 return true; 1418 1419 return false; 1420 }; 1421 1422 auto CheckForNoSync = [&](Instruction &I) { 1423 // At this point we handled all read/write effects and they are all 1424 // nosync, so they can be skipped. 1425 if (I.mayReadOrWriteMemory()) 1426 return true; 1427 1428 // non-convergent and readnone imply nosync. 1429 return !ImmutableCallSite(&I).isConvergent(); 1430 }; 1431 1432 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) || 1433 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this)) 1434 return indicatePessimisticFixpoint(); 1435 1436 return ChangeStatus::UNCHANGED; 1437 } 1438 1439 struct AANoSyncFunction final : public AANoSyncImpl { 1440 AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {} 1441 1442 /// See AbstractAttribute::trackStatistics() 1443 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) } 1444 }; 1445 1446 /// NoSync attribute deduction for a call sites. 1447 struct AANoSyncCallSite final : AANoSyncImpl { 1448 AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {} 1449 1450 /// See AbstractAttribute::initialize(...). 1451 void initialize(Attributor &A) override { 1452 AANoSyncImpl::initialize(A); 1453 Function *F = getAssociatedFunction(); 1454 if (!F) 1455 indicatePessimisticFixpoint(); 1456 } 1457 1458 /// See AbstractAttribute::updateImpl(...). 1459 ChangeStatus updateImpl(Attributor &A) override { 1460 // TODO: Once we have call site specific value information we can provide 1461 // call site specific liveness information and then it makes 1462 // sense to specialize attributes for call sites arguments instead of 1463 // redirecting requests to the callee argument. 1464 Function *F = getAssociatedFunction(); 1465 const IRPosition &FnPos = IRPosition::function(*F); 1466 auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos); 1467 return clampStateAndIndicateChange( 1468 getState(), static_cast<const AANoSync::StateType &>(FnAA.getState())); 1469 } 1470 1471 /// See AbstractAttribute::trackStatistics() 1472 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); } 1473 }; 1474 1475 /// ------------------------ No-Free Attributes ---------------------------- 1476 1477 struct AANoFreeImpl : public AANoFree { 1478 AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {} 1479 1480 /// See AbstractAttribute::updateImpl(...). 1481 ChangeStatus updateImpl(Attributor &A) override { 1482 auto CheckForNoFree = [&](Instruction &I) { 1483 ImmutableCallSite ICS(&I); 1484 if (ICS.hasFnAttr(Attribute::NoFree)) 1485 return true; 1486 1487 const auto &NoFreeAA = 1488 A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS)); 1489 return NoFreeAA.isAssumedNoFree(); 1490 }; 1491 1492 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this)) 1493 return indicatePessimisticFixpoint(); 1494 return ChangeStatus::UNCHANGED; 1495 } 1496 1497 /// See AbstractAttribute::getAsStr(). 1498 const std::string getAsStr() const override { 1499 return getAssumed() ? "nofree" : "may-free"; 1500 } 1501 }; 1502 1503 struct AANoFreeFunction final : public AANoFreeImpl { 1504 AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {} 1505 1506 /// See AbstractAttribute::trackStatistics() 1507 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) } 1508 }; 1509 1510 /// NoFree attribute deduction for a call sites. 1511 struct AANoFreeCallSite final : AANoFreeImpl { 1512 AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {} 1513 1514 /// See AbstractAttribute::initialize(...). 1515 void initialize(Attributor &A) override { 1516 AANoFreeImpl::initialize(A); 1517 Function *F = getAssociatedFunction(); 1518 if (!F) 1519 indicatePessimisticFixpoint(); 1520 } 1521 1522 /// See AbstractAttribute::updateImpl(...). 1523 ChangeStatus updateImpl(Attributor &A) override { 1524 // TODO: Once we have call site specific value information we can provide 1525 // call site specific liveness information and then it makes 1526 // sense to specialize attributes for call sites arguments instead of 1527 // redirecting requests to the callee argument. 1528 Function *F = getAssociatedFunction(); 1529 const IRPosition &FnPos = IRPosition::function(*F); 1530 auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos); 1531 return clampStateAndIndicateChange( 1532 getState(), static_cast<const AANoFree::StateType &>(FnAA.getState())); 1533 } 1534 1535 /// See AbstractAttribute::trackStatistics() 1536 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); } 1537 }; 1538 1539 /// NoFree attribute for floating values. 1540 struct AANoFreeFloating : AANoFreeImpl { 1541 AANoFreeFloating(const IRPosition &IRP) : AANoFreeImpl(IRP) {} 1542 1543 /// See AbstractAttribute::trackStatistics() 1544 void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)} 1545 1546 /// See Abstract Attribute::updateImpl(...). 1547 ChangeStatus updateImpl(Attributor &A) override { 1548 const IRPosition &IRP = getIRPosition(); 1549 1550 const auto &NoFreeAA = 1551 A.getAAFor<AANoFree>(*this, IRPosition::function_scope(IRP)); 1552 if (NoFreeAA.isAssumedNoFree()) 1553 return ChangeStatus::UNCHANGED; 1554 1555 Value &AssociatedValue = getIRPosition().getAssociatedValue(); 1556 auto Pred = [&](const Use &U, bool &Follow) -> bool { 1557 Instruction *UserI = cast<Instruction>(U.getUser()); 1558 if (auto *CB = dyn_cast<CallBase>(UserI)) { 1559 if (CB->isBundleOperand(&U)) 1560 return false; 1561 if (!CB->isArgOperand(&U)) 1562 return true; 1563 unsigned ArgNo = CB->getArgOperandNo(&U); 1564 1565 const auto &NoFreeArg = A.getAAFor<AANoFree>( 1566 *this, IRPosition::callsite_argument(*CB, ArgNo)); 1567 return NoFreeArg.isAssumedNoFree(); 1568 } 1569 1570 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || 1571 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { 1572 Follow = true; 1573 return true; 1574 } 1575 1576 // Unknown user. 1577 return false; 1578 }; 1579 if (!A.checkForAllUses(Pred, *this, AssociatedValue)) 1580 return indicatePessimisticFixpoint(); 1581 1582 return ChangeStatus::UNCHANGED; 1583 } 1584 }; 1585 1586 /// NoFree attribute for a call site argument. 1587 struct AANoFreeArgument final : AANoFreeFloating { 1588 AANoFreeArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {} 1589 1590 /// See AbstractAttribute::trackStatistics() 1591 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) } 1592 }; 1593 1594 /// NoFree attribute for call site arguments. 1595 struct AANoFreeCallSiteArgument final : AANoFreeFloating { 1596 AANoFreeCallSiteArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {} 1597 1598 /// See AbstractAttribute::updateImpl(...). 1599 ChangeStatus updateImpl(Attributor &A) override { 1600 // TODO: Once we have call site specific value information we can provide 1601 // call site specific liveness information and then it makes 1602 // sense to specialize attributes for call sites arguments instead of 1603 // redirecting requests to the callee argument. 1604 Argument *Arg = getAssociatedArgument(); 1605 if (!Arg) 1606 return indicatePessimisticFixpoint(); 1607 const IRPosition &ArgPos = IRPosition::argument(*Arg); 1608 auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos); 1609 return clampStateAndIndicateChange( 1610 getState(), static_cast<const AANoFree::StateType &>(ArgAA.getState())); 1611 } 1612 1613 /// See AbstractAttribute::trackStatistics() 1614 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)}; 1615 }; 1616 1617 /// NoFree attribute for function return value. 1618 struct AANoFreeReturned final : AANoFreeFloating { 1619 AANoFreeReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) { 1620 llvm_unreachable("NoFree is not applicable to function returns!"); 1621 } 1622 1623 /// See AbstractAttribute::initialize(...). 1624 void initialize(Attributor &A) override { 1625 llvm_unreachable("NoFree is not applicable to function returns!"); 1626 } 1627 1628 /// See AbstractAttribute::updateImpl(...). 1629 ChangeStatus updateImpl(Attributor &A) override { 1630 llvm_unreachable("NoFree is not applicable to function returns!"); 1631 } 1632 1633 /// See AbstractAttribute::trackStatistics() 1634 void trackStatistics() const override {} 1635 }; 1636 1637 /// NoFree attribute deduction for a call site return value. 1638 struct AANoFreeCallSiteReturned final : AANoFreeFloating { 1639 AANoFreeCallSiteReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {} 1640 1641 ChangeStatus manifest(Attributor &A) override { 1642 return ChangeStatus::UNCHANGED; 1643 } 1644 /// See AbstractAttribute::trackStatistics() 1645 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) } 1646 }; 1647 1648 /// ------------------------ NonNull Argument Attribute ------------------------ 1649 static int64_t getKnownNonNullAndDerefBytesForUse( 1650 Attributor &A, AbstractAttribute &QueryingAA, Value &AssociatedValue, 1651 const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) { 1652 TrackUse = false; 1653 1654 const Value *UseV = U->get(); 1655 if (!UseV->getType()->isPointerTy()) 1656 return 0; 1657 1658 Type *PtrTy = UseV->getType(); 1659 const Function *F = I->getFunction(); 1660 bool NullPointerIsDefined = 1661 F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true; 1662 const DataLayout &DL = A.getInfoCache().getDL(); 1663 if (ImmutableCallSite ICS = ImmutableCallSite(I)) { 1664 if (ICS.isBundleOperand(U)) 1665 return 0; 1666 1667 if (ICS.isCallee(U)) { 1668 IsNonNull |= !NullPointerIsDefined; 1669 return 0; 1670 } 1671 1672 unsigned ArgNo = ICS.getArgumentNo(U); 1673 IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); 1674 // As long as we only use known information there is no need to track 1675 // dependences here. 1676 auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP, 1677 /* TrackDependence */ false); 1678 IsNonNull |= DerefAA.isKnownNonNull(); 1679 return DerefAA.getKnownDereferenceableBytes(); 1680 } 1681 1682 // We need to follow common pointer manipulation uses to the accesses they 1683 // feed into. We can try to be smart to avoid looking through things we do not 1684 // like for now, e.g., non-inbounds GEPs. 1685 if (isa<CastInst>(I)) { 1686 TrackUse = true; 1687 return 0; 1688 } 1689 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) 1690 if (GEP->hasAllConstantIndices()) { 1691 TrackUse = true; 1692 return 0; 1693 } 1694 1695 int64_t Offset; 1696 if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) { 1697 if (Base == &AssociatedValue && getPointerOperand(I) == UseV) { 1698 int64_t DerefBytes = 1699 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset; 1700 1701 IsNonNull |= !NullPointerIsDefined; 1702 return std::max(int64_t(0), DerefBytes); 1703 } 1704 } 1705 1706 /// Corner case when an offset is 0. 1707 if (const Value *Base = getBasePointerOfAccessPointerOperand( 1708 I, Offset, DL, /*AllowNonInbounds*/ true)) { 1709 if (Offset == 0 && Base == &AssociatedValue && 1710 getPointerOperand(I) == UseV) { 1711 int64_t DerefBytes = 1712 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()); 1713 IsNonNull |= !NullPointerIsDefined; 1714 return std::max(int64_t(0), DerefBytes); 1715 } 1716 } 1717 if (const Value *Base = 1718 GetPointerBaseWithConstantOffset(UseV, Offset, DL, 1719 /*AllowNonInbounds*/ false)) { 1720 if (Base == &AssociatedValue) { 1721 // As long as we only use known information there is no need to track 1722 // dependences here. 1723 auto &DerefAA = A.getAAFor<AADereferenceable>( 1724 QueryingAA, IRPosition::value(*Base), /* TrackDependence */ false); 1725 IsNonNull |= (!NullPointerIsDefined && DerefAA.isKnownNonNull()); 1726 IsNonNull |= (!NullPointerIsDefined && (Offset != 0)); 1727 int64_t DerefBytes = DerefAA.getKnownDereferenceableBytes(); 1728 return std::max(int64_t(0), DerefBytes - std::max(int64_t(0), Offset)); 1729 } 1730 } 1731 1732 return 0; 1733 } 1734 1735 struct AANonNullImpl : AANonNull { 1736 AANonNullImpl(const IRPosition &IRP) 1737 : AANonNull(IRP), 1738 NullIsDefined(NullPointerIsDefined( 1739 getAnchorScope(), 1740 getAssociatedValue().getType()->getPointerAddressSpace())) {} 1741 1742 /// See AbstractAttribute::initialize(...). 1743 void initialize(Attributor &A) override { 1744 if (!NullIsDefined && 1745 hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) 1746 indicateOptimisticFixpoint(); 1747 else if (isa<ConstantPointerNull>(getAssociatedValue())) 1748 indicatePessimisticFixpoint(); 1749 else 1750 AANonNull::initialize(A); 1751 } 1752 1753 /// See AAFromMustBeExecutedContext 1754 bool followUse(Attributor &A, const Use *U, const Instruction *I) { 1755 bool IsNonNull = false; 1756 bool TrackUse = false; 1757 getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I, 1758 IsNonNull, TrackUse); 1759 setKnown(IsNonNull); 1760 return TrackUse; 1761 } 1762 1763 /// See AbstractAttribute::getAsStr(). 1764 const std::string getAsStr() const override { 1765 return getAssumed() ? "nonnull" : "may-null"; 1766 } 1767 1768 /// Flag to determine if the underlying value can be null and still allow 1769 /// valid accesses. 1770 const bool NullIsDefined; 1771 }; 1772 1773 /// NonNull attribute for a floating value. 1774 struct AANonNullFloating 1775 : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> { 1776 using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>; 1777 AANonNullFloating(const IRPosition &IRP) : Base(IRP) {} 1778 1779 /// See AbstractAttribute::updateImpl(...). 1780 ChangeStatus updateImpl(Attributor &A) override { 1781 ChangeStatus Change = Base::updateImpl(A); 1782 if (isKnownNonNull()) 1783 return Change; 1784 1785 if (!NullIsDefined) { 1786 const auto &DerefAA = 1787 A.getAAFor<AADereferenceable>(*this, getIRPosition()); 1788 if (DerefAA.getAssumedDereferenceableBytes()) 1789 return Change; 1790 } 1791 1792 const DataLayout &DL = A.getDataLayout(); 1793 1794 DominatorTree *DT = nullptr; 1795 InformationCache &InfoCache = A.getInfoCache(); 1796 if (const Function *Fn = getAnchorScope()) 1797 DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn); 1798 1799 auto VisitValueCB = [&](Value &V, AANonNull::StateType &T, 1800 bool Stripped) -> bool { 1801 const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); 1802 if (!Stripped && this == &AA) { 1803 if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, getCtxI(), DT)) 1804 T.indicatePessimisticFixpoint(); 1805 } else { 1806 // Use abstract attribute information. 1807 const AANonNull::StateType &NS = 1808 static_cast<const AANonNull::StateType &>(AA.getState()); 1809 T ^= NS; 1810 } 1811 return T.isValidState(); 1812 }; 1813 1814 StateType T; 1815 if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this, 1816 T, VisitValueCB)) 1817 return indicatePessimisticFixpoint(); 1818 1819 return clampStateAndIndicateChange(getState(), T); 1820 } 1821 1822 /// See AbstractAttribute::trackStatistics() 1823 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } 1824 }; 1825 1826 /// NonNull attribute for function return value. 1827 struct AANonNullReturned final 1828 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> { 1829 AANonNullReturned(const IRPosition &IRP) 1830 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {} 1831 1832 /// See AbstractAttribute::trackStatistics() 1833 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } 1834 }; 1835 1836 /// NonNull attribute for function argument. 1837 struct AANonNullArgument final 1838 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, 1839 AANonNullImpl> { 1840 AANonNullArgument(const IRPosition &IRP) 1841 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull, 1842 AANonNullImpl>( 1843 IRP) {} 1844 1845 /// See AbstractAttribute::trackStatistics() 1846 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } 1847 }; 1848 1849 struct AANonNullCallSiteArgument final : AANonNullFloating { 1850 AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {} 1851 1852 /// See AbstractAttribute::trackStatistics() 1853 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } 1854 }; 1855 1856 /// NonNull attribute for a call site return position. 1857 struct AANonNullCallSiteReturned final 1858 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, 1859 AANonNullImpl> { 1860 AANonNullCallSiteReturned(const IRPosition &IRP) 1861 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull, 1862 AANonNullImpl>( 1863 IRP) {} 1864 1865 /// See AbstractAttribute::trackStatistics() 1866 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) } 1867 }; 1868 1869 /// ------------------------ No-Recurse Attributes ---------------------------- 1870 1871 struct AANoRecurseImpl : public AANoRecurse { 1872 AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {} 1873 1874 /// See AbstractAttribute::getAsStr() 1875 const std::string getAsStr() const override { 1876 return getAssumed() ? "norecurse" : "may-recurse"; 1877 } 1878 }; 1879 1880 struct AANoRecurseFunction final : AANoRecurseImpl { 1881 AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} 1882 1883 /// See AbstractAttribute::initialize(...). 1884 void initialize(Attributor &A) override { 1885 AANoRecurseImpl::initialize(A); 1886 if (const Function *F = getAnchorScope()) 1887 if (A.getInfoCache().getSccSize(*F) == 1) 1888 return; 1889 indicatePessimisticFixpoint(); 1890 } 1891 1892 /// See AbstractAttribute::updateImpl(...). 1893 ChangeStatus updateImpl(Attributor &A) override { 1894 1895 auto CheckForNoRecurse = [&](Instruction &I) { 1896 ImmutableCallSite ICS(&I); 1897 if (ICS.hasFnAttr(Attribute::NoRecurse)) 1898 return true; 1899 1900 const auto &NoRecurseAA = 1901 A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS)); 1902 if (!NoRecurseAA.isAssumedNoRecurse()) 1903 return false; 1904 1905 // Recursion to the same function 1906 if (ICS.getCalledFunction() == getAnchorScope()) 1907 return false; 1908 1909 return true; 1910 }; 1911 1912 if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this)) 1913 return indicatePessimisticFixpoint(); 1914 return ChangeStatus::UNCHANGED; 1915 } 1916 1917 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } 1918 }; 1919 1920 /// NoRecurse attribute deduction for a call sites. 1921 struct AANoRecurseCallSite final : AANoRecurseImpl { 1922 AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {} 1923 1924 /// See AbstractAttribute::initialize(...). 1925 void initialize(Attributor &A) override { 1926 AANoRecurseImpl::initialize(A); 1927 Function *F = getAssociatedFunction(); 1928 if (!F) 1929 indicatePessimisticFixpoint(); 1930 } 1931 1932 /// See AbstractAttribute::updateImpl(...). 1933 ChangeStatus updateImpl(Attributor &A) override { 1934 // TODO: Once we have call site specific value information we can provide 1935 // call site specific liveness information and then it makes 1936 // sense to specialize attributes for call sites arguments instead of 1937 // redirecting requests to the callee argument. 1938 Function *F = getAssociatedFunction(); 1939 const IRPosition &FnPos = IRPosition::function(*F); 1940 auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos); 1941 return clampStateAndIndicateChange( 1942 getState(), 1943 static_cast<const AANoRecurse::StateType &>(FnAA.getState())); 1944 } 1945 1946 /// See AbstractAttribute::trackStatistics() 1947 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); } 1948 }; 1949 1950 /// ------------------------ Will-Return Attributes ---------------------------- 1951 1952 // Helper function that checks whether a function has any cycle. 1953 // TODO: Replace with more efficent code 1954 static bool containsCycle(Function &F) { 1955 SmallPtrSet<BasicBlock *, 32> Visited; 1956 1957 // Traverse BB by dfs and check whether successor is already visited. 1958 for (BasicBlock *BB : depth_first(&F)) { 1959 Visited.insert(BB); 1960 for (auto *SuccBB : successors(BB)) { 1961 if (Visited.count(SuccBB)) 1962 return true; 1963 } 1964 } 1965 return false; 1966 } 1967 1968 // Helper function that checks the function have a loop which might become an 1969 // endless loop 1970 // FIXME: Any cycle is regarded as endless loop for now. 1971 // We have to allow some patterns. 1972 static bool containsPossiblyEndlessLoop(Function *F) { 1973 return !F || !F->hasExactDefinition() || containsCycle(*F); 1974 } 1975 1976 struct AAWillReturnImpl : public AAWillReturn { 1977 AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} 1978 1979 /// See AbstractAttribute::initialize(...). 1980 void initialize(Attributor &A) override { 1981 AAWillReturn::initialize(A); 1982 1983 Function *F = getAssociatedFunction(); 1984 if (containsPossiblyEndlessLoop(F)) 1985 indicatePessimisticFixpoint(); 1986 } 1987 1988 /// See AbstractAttribute::updateImpl(...). 1989 ChangeStatus updateImpl(Attributor &A) override { 1990 auto CheckForWillReturn = [&](Instruction &I) { 1991 IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I)); 1992 const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); 1993 if (WillReturnAA.isKnownWillReturn()) 1994 return true; 1995 if (!WillReturnAA.isAssumedWillReturn()) 1996 return false; 1997 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); 1998 return NoRecurseAA.isAssumedNoRecurse(); 1999 }; 2000 2001 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) 2002 return indicatePessimisticFixpoint(); 2003 2004 return ChangeStatus::UNCHANGED; 2005 } 2006 2007 /// See AbstractAttribute::getAsStr() 2008 const std::string getAsStr() const override { 2009 return getAssumed() ? "willreturn" : "may-noreturn"; 2010 } 2011 }; 2012 2013 struct AAWillReturnFunction final : AAWillReturnImpl { 2014 AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} 2015 2016 /// See AbstractAttribute::trackStatistics() 2017 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) } 2018 }; 2019 2020 /// WillReturn attribute deduction for a call sites. 2021 struct AAWillReturnCallSite final : AAWillReturnImpl { 2022 AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} 2023 2024 /// See AbstractAttribute::initialize(...). 2025 void initialize(Attributor &A) override { 2026 AAWillReturnImpl::initialize(A); 2027 Function *F = getAssociatedFunction(); 2028 if (!F) 2029 indicatePessimisticFixpoint(); 2030 } 2031 2032 /// See AbstractAttribute::updateImpl(...). 2033 ChangeStatus updateImpl(Attributor &A) override { 2034 // TODO: Once we have call site specific value information we can provide 2035 // call site specific liveness information and then it makes 2036 // sense to specialize attributes for call sites arguments instead of 2037 // redirecting requests to the callee argument. 2038 Function *F = getAssociatedFunction(); 2039 const IRPosition &FnPos = IRPosition::function(*F); 2040 auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos); 2041 return clampStateAndIndicateChange( 2042 getState(), 2043 static_cast<const AAWillReturn::StateType &>(FnAA.getState())); 2044 } 2045 2046 /// See AbstractAttribute::trackStatistics() 2047 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); } 2048 }; 2049 2050 /// -------------------AAReachability Attribute-------------------------- 2051 2052 struct AAReachabilityImpl : AAReachability { 2053 AAReachabilityImpl(const IRPosition &IRP) : AAReachability(IRP) {} 2054 2055 const std::string getAsStr() const override { 2056 // TODO: Return the number of reachable queries. 2057 return "reachable"; 2058 } 2059 2060 /// See AbstractAttribute::initialize(...). 2061 void initialize(Attributor &A) override { 2062 indicatePessimisticFixpoint(); 2063 } 2064 2065 /// See AbstractAttribute::updateImpl(...). 2066 ChangeStatus updateImpl(Attributor &A) override { 2067 return indicatePessimisticFixpoint(); 2068 } 2069 }; 2070 2071 struct AAReachabilityFunction final : public AAReachabilityImpl { 2072 AAReachabilityFunction(const IRPosition &IRP) : AAReachabilityImpl(IRP) {} 2073 2074 /// See AbstractAttribute::trackStatistics() 2075 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); } 2076 }; 2077 2078 /// ------------------------ NoAlias Argument Attribute ------------------------ 2079 2080 struct AANoAliasImpl : AANoAlias { 2081 AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {} 2082 2083 const std::string getAsStr() const override { 2084 return getAssumed() ? "noalias" : "may-alias"; 2085 } 2086 }; 2087 2088 /// NoAlias attribute for a floating value. 2089 struct AANoAliasFloating final : AANoAliasImpl { 2090 AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {} 2091 2092 /// See AbstractAttribute::initialize(...). 2093 void initialize(Attributor &A) override { 2094 AANoAliasImpl::initialize(A); 2095 Value &Val = getAssociatedValue(); 2096 if (isa<AllocaInst>(Val)) 2097 indicateOptimisticFixpoint(); 2098 if (isa<ConstantPointerNull>(Val) && 2099 Val.getType()->getPointerAddressSpace() == 0) 2100 indicateOptimisticFixpoint(); 2101 } 2102 2103 /// See AbstractAttribute::updateImpl(...). 2104 ChangeStatus updateImpl(Attributor &A) override { 2105 // TODO: Implement this. 2106 return indicatePessimisticFixpoint(); 2107 } 2108 2109 /// See AbstractAttribute::trackStatistics() 2110 void trackStatistics() const override { 2111 STATS_DECLTRACK_FLOATING_ATTR(noalias) 2112 } 2113 }; 2114 2115 /// NoAlias attribute for an argument. 2116 struct AANoAliasArgument final 2117 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> { 2118 AANoAliasArgument(const IRPosition &IRP) 2119 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>(IRP) {} 2120 2121 /// See AbstractAttribute::trackStatistics() 2122 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) } 2123 }; 2124 2125 struct AANoAliasCallSiteArgument final : AANoAliasImpl { 2126 AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {} 2127 2128 /// See AbstractAttribute::initialize(...). 2129 void initialize(Attributor &A) override { 2130 // See callsite argument attribute and callee argument attribute. 2131 ImmutableCallSite ICS(&getAnchorValue()); 2132 if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias)) 2133 indicateOptimisticFixpoint(); 2134 } 2135 2136 /// See AbstractAttribute::updateImpl(...). 2137 ChangeStatus updateImpl(Attributor &A) override { 2138 // We can deduce "noalias" if the following conditions hold. 2139 // (i) Associated value is assumed to be noalias in the definition. 2140 // (ii) Associated value is assumed to be no-capture in all the uses 2141 // possibly executed before this callsite. 2142 // (iii) There is no other pointer argument which could alias with the 2143 // value. 2144 2145 const Value &V = getAssociatedValue(); 2146 const IRPosition IRP = IRPosition::value(V); 2147 2148 // (i) Check whether noalias holds in the definition. 2149 2150 auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP); 2151 2152 if (!NoAliasAA.isAssumedNoAlias()) 2153 return indicatePessimisticFixpoint(); 2154 2155 LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] " << V 2156 << " is assumed NoAlias in the definition\n"); 2157 2158 // (ii) Check whether the value is captured in the scope using AANoCapture. 2159 // FIXME: This is conservative though, it is better to look at CFG and 2160 // check only uses possibly executed before this callsite. 2161 2162 auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); 2163 if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { 2164 LLVM_DEBUG( 2165 dbgs() << "[Attributor][AANoAliasCSArg] " << V 2166 << " cannot be noalias as it is potentially captured\n"); 2167 return indicatePessimisticFixpoint(); 2168 } 2169 2170 // (iii) Check there is no other pointer argument which could alias with the 2171 // value. 2172 ImmutableCallSite ICS(&getAnchorValue()); 2173 for (unsigned i = 0; i < ICS.getNumArgOperands(); i++) { 2174 if (getArgNo() == (int)i) 2175 continue; 2176 const Value *ArgOp = ICS.getArgOperand(i); 2177 if (!ArgOp->getType()->isPointerTy()) 2178 continue; 2179 2180 if (const Function *F = getAnchorScope()) { 2181 if (AAResults *AAR = A.getInfoCache().getAAResultsForFunction(*F)) { 2182 bool IsAliasing = AAR->isNoAlias(&getAssociatedValue(), ArgOp); 2183 LLVM_DEBUG(dbgs() 2184 << "[Attributor][NoAliasCSArg] Check alias between " 2185 "callsite arguments " 2186 << AAR->isNoAlias(&getAssociatedValue(), ArgOp) << " " 2187 << getAssociatedValue() << " " << *ArgOp << " => " 2188 << (IsAliasing ? "" : "no-") << "alias \n"); 2189 2190 if (IsAliasing) 2191 continue; 2192 } 2193 } 2194 return indicatePessimisticFixpoint(); 2195 } 2196 2197 return ChangeStatus::UNCHANGED; 2198 } 2199 2200 /// See AbstractAttribute::trackStatistics() 2201 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) } 2202 }; 2203 2204 /// NoAlias attribute for function return value. 2205 struct AANoAliasReturned final : AANoAliasImpl { 2206 AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} 2207 2208 /// See AbstractAttribute::updateImpl(...). 2209 virtual ChangeStatus updateImpl(Attributor &A) override { 2210 2211 auto CheckReturnValue = [&](Value &RV) -> bool { 2212 if (Constant *C = dyn_cast<Constant>(&RV)) 2213 if (C->isNullValue() || isa<UndefValue>(C)) 2214 return true; 2215 2216 /// For now, we can only deduce noalias if we have call sites. 2217 /// FIXME: add more support. 2218 ImmutableCallSite ICS(&RV); 2219 if (!ICS) 2220 return false; 2221 2222 const IRPosition &RVPos = IRPosition::value(RV); 2223 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos); 2224 if (!NoAliasAA.isAssumedNoAlias()) 2225 return false; 2226 2227 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos); 2228 return NoCaptureAA.isAssumedNoCaptureMaybeReturned(); 2229 }; 2230 2231 if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) 2232 return indicatePessimisticFixpoint(); 2233 2234 return ChangeStatus::UNCHANGED; 2235 } 2236 2237 /// See AbstractAttribute::trackStatistics() 2238 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } 2239 }; 2240 2241 /// NoAlias attribute deduction for a call site return value. 2242 struct AANoAliasCallSiteReturned final : AANoAliasImpl { 2243 AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} 2244 2245 /// See AbstractAttribute::initialize(...). 2246 void initialize(Attributor &A) override { 2247 AANoAliasImpl::initialize(A); 2248 Function *F = getAssociatedFunction(); 2249 if (!F) 2250 indicatePessimisticFixpoint(); 2251 } 2252 2253 /// See AbstractAttribute::updateImpl(...). 2254 ChangeStatus updateImpl(Attributor &A) override { 2255 // TODO: Once we have call site specific value information we can provide 2256 // call site specific liveness information and then it makes 2257 // sense to specialize attributes for call sites arguments instead of 2258 // redirecting requests to the callee argument. 2259 Function *F = getAssociatedFunction(); 2260 const IRPosition &FnPos = IRPosition::returned(*F); 2261 auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos); 2262 return clampStateAndIndicateChange( 2263 getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState())); 2264 } 2265 2266 /// See AbstractAttribute::trackStatistics() 2267 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); } 2268 }; 2269 2270 /// -------------------AAIsDead Function Attribute----------------------- 2271 2272 struct AAIsDeadValueImpl : public AAIsDead { 2273 AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {} 2274 2275 /// See AAIsDead::isAssumedDead(). 2276 bool isAssumedDead() const override { return getAssumed(); } 2277 2278 /// See AAIsDead::isAssumedDead(BasicBlock *). 2279 bool isAssumedDead(const BasicBlock *BB) const override { return false; } 2280 2281 /// See AAIsDead::isKnownDead(BasicBlock *). 2282 bool isKnownDead(const BasicBlock *BB) const override { return false; } 2283 2284 /// See AAIsDead::isAssumedDead(Instruction *I). 2285 bool isAssumedDead(const Instruction *I) const override { 2286 return I == getCtxI() && isAssumedDead(); 2287 } 2288 2289 /// See AAIsDead::isKnownDead(Instruction *I). 2290 bool isKnownDead(const Instruction *I) const override { 2291 return I == getCtxI() && getKnown(); 2292 } 2293 2294 /// See AbstractAttribute::getAsStr(). 2295 const std::string getAsStr() const override { 2296 return isAssumedDead() ? "assumed-dead" : "assumed-live"; 2297 } 2298 }; 2299 2300 struct AAIsDeadFloating : public AAIsDeadValueImpl { 2301 AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} 2302 2303 /// See AbstractAttribute::initialize(...). 2304 void initialize(Attributor &A) override { 2305 if (Instruction *I = dyn_cast<Instruction>(&getAssociatedValue())) 2306 if (!wouldInstructionBeTriviallyDead(I)) 2307 indicatePessimisticFixpoint(); 2308 if (isa<UndefValue>(getAssociatedValue())) 2309 indicatePessimisticFixpoint(); 2310 } 2311 2312 /// See AbstractAttribute::updateImpl(...). 2313 ChangeStatus updateImpl(Attributor &A) override { 2314 auto UsePred = [&](const Use &U, bool &Follow) { 2315 Instruction *UserI = cast<Instruction>(U.getUser()); 2316 if (CallSite CS = CallSite(UserI)) { 2317 if (!CS.isArgOperand(&U)) 2318 return false; 2319 const IRPosition &CSArgPos = 2320 IRPosition::callsite_argument(CS, CS.getArgumentNo(&U)); 2321 const auto &CSArgIsDead = A.getAAFor<AAIsDead>(*this, CSArgPos); 2322 return CSArgIsDead.isAssumedDead(); 2323 } 2324 if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) { 2325 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction()); 2326 const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, RetPos); 2327 return RetIsDeadAA.isAssumedDead(); 2328 } 2329 Follow = true; 2330 return wouldInstructionBeTriviallyDead(UserI); 2331 }; 2332 2333 if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) 2334 return indicatePessimisticFixpoint(); 2335 return ChangeStatus::UNCHANGED; 2336 } 2337 2338 /// See AbstractAttribute::manifest(...). 2339 ChangeStatus manifest(Attributor &A) override { 2340 Value &V = getAssociatedValue(); 2341 if (auto *I = dyn_cast<Instruction>(&V)) 2342 if (wouldInstructionBeTriviallyDead(I)) { 2343 A.deleteAfterManifest(*I); 2344 return ChangeStatus::CHANGED; 2345 } 2346 2347 if (V.use_empty()) 2348 return ChangeStatus::UNCHANGED; 2349 2350 UndefValue &UV = *UndefValue::get(V.getType()); 2351 bool AnyChange = false; 2352 for (Use &U : V.uses()) 2353 AnyChange |= A.changeUseAfterManifest(U, UV); 2354 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 2355 } 2356 2357 /// See AbstractAttribute::trackStatistics() 2358 void trackStatistics() const override { 2359 STATS_DECLTRACK_FLOATING_ATTR(IsDead) 2360 } 2361 }; 2362 2363 struct AAIsDeadArgument : public AAIsDeadFloating { 2364 AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} 2365 2366 /// See AbstractAttribute::initialize(...). 2367 void initialize(Attributor &A) override { 2368 if (!getAssociatedFunction()->hasExactDefinition()) 2369 indicatePessimisticFixpoint(); 2370 } 2371 2372 /// See AbstractAttribute::trackStatistics() 2373 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) } 2374 }; 2375 2376 struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl { 2377 AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} 2378 2379 /// See AbstractAttribute::initialize(...). 2380 void initialize(Attributor &A) override { 2381 if (isa<UndefValue>(getAssociatedValue())) 2382 indicatePessimisticFixpoint(); 2383 } 2384 2385 /// See AbstractAttribute::updateImpl(...). 2386 ChangeStatus updateImpl(Attributor &A) override { 2387 // TODO: Once we have call site specific value information we can provide 2388 // call site specific liveness information and then it makes 2389 // sense to specialize attributes for call sites arguments instead of 2390 // redirecting requests to the callee argument. 2391 Argument *Arg = getAssociatedArgument(); 2392 if (!Arg) 2393 return indicatePessimisticFixpoint(); 2394 const IRPosition &ArgPos = IRPosition::argument(*Arg); 2395 auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos); 2396 return clampStateAndIndicateChange( 2397 getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState())); 2398 } 2399 2400 /// See AbstractAttribute::manifest(...). 2401 ChangeStatus manifest(Attributor &A) override { 2402 CallBase &CB = cast<CallBase>(getAnchorValue()); 2403 Use &U = CB.getArgOperandUse(getArgNo()); 2404 assert(!isa<UndefValue>(U.get()) && 2405 "Expected undef values to be filtered out!"); 2406 UndefValue &UV = *UndefValue::get(U->getType()); 2407 if (A.changeUseAfterManifest(U, UV)) 2408 return ChangeStatus::CHANGED; 2409 return ChangeStatus::UNCHANGED; 2410 } 2411 2412 /// See AbstractAttribute::trackStatistics() 2413 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) } 2414 }; 2415 2416 struct AAIsDeadReturned : public AAIsDeadValueImpl { 2417 AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {} 2418 2419 /// See AbstractAttribute::updateImpl(...). 2420 ChangeStatus updateImpl(Attributor &A) override { 2421 2422 auto PredForCallSite = [&](AbstractCallSite ACS) { 2423 if (ACS.isCallbackCall()) 2424 return false; 2425 const IRPosition &CSRetPos = 2426 IRPosition::callsite_returned(ACS.getCallSite()); 2427 const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, CSRetPos); 2428 return RetIsDeadAA.isAssumedDead(); 2429 }; 2430 2431 if (!A.checkForAllCallSites(PredForCallSite, *this, true)) 2432 return indicatePessimisticFixpoint(); 2433 2434 return ChangeStatus::UNCHANGED; 2435 } 2436 2437 /// See AbstractAttribute::manifest(...). 2438 ChangeStatus manifest(Attributor &A) override { 2439 // TODO: Rewrite the signature to return void? 2440 bool AnyChange = false; 2441 UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType()); 2442 auto RetInstPred = [&](Instruction &I) { 2443 ReturnInst &RI = cast<ReturnInst>(I); 2444 if (!isa<UndefValue>(RI.getReturnValue())) 2445 AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV); 2446 return true; 2447 }; 2448 A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret}); 2449 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 2450 } 2451 2452 /// See AbstractAttribute::trackStatistics() 2453 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) } 2454 }; 2455 2456 struct AAIsDeadCallSiteReturned : public AAIsDeadFloating { 2457 AAIsDeadCallSiteReturned(const IRPosition &IRP) : AAIsDeadFloating(IRP) {} 2458 2459 /// See AbstractAttribute::initialize(...). 2460 void initialize(Attributor &A) override {} 2461 2462 /// See AbstractAttribute::trackStatistics() 2463 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(IsDead) } 2464 }; 2465 2466 struct AAIsDeadFunction : public AAIsDead { 2467 AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {} 2468 2469 /// See AbstractAttribute::initialize(...). 2470 void initialize(Attributor &A) override { 2471 const Function *F = getAssociatedFunction(); 2472 if (F && !F->isDeclaration()) { 2473 ToBeExploredFrom.insert(&F->getEntryBlock().front()); 2474 assumeLive(A, F->getEntryBlock()); 2475 } 2476 } 2477 2478 /// See AbstractAttribute::getAsStr(). 2479 const std::string getAsStr() const override { 2480 return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + 2481 std::to_string(getAssociatedFunction()->size()) + "][#TBEP " + 2482 std::to_string(ToBeExploredFrom.size()) + "][#KDE " + 2483 std::to_string(KnownDeadEnds.size()) + "]"; 2484 } 2485 2486 /// See AbstractAttribute::manifest(...). 2487 ChangeStatus manifest(Attributor &A) override { 2488 assert(getState().isValidState() && 2489 "Attempted to manifest an invalid state!"); 2490 2491 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 2492 Function &F = *getAssociatedFunction(); 2493 2494 if (AssumedLiveBlocks.empty()) { 2495 A.deleteAfterManifest(F); 2496 return ChangeStatus::CHANGED; 2497 } 2498 2499 // Flag to determine if we can change an invoke to a call assuming the 2500 // callee is nounwind. This is not possible if the personality of the 2501 // function allows to catch asynchronous exceptions. 2502 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); 2503 2504 KnownDeadEnds.set_union(ToBeExploredFrom); 2505 for (const Instruction *DeadEndI : KnownDeadEnds) { 2506 auto *CB = dyn_cast<CallBase>(DeadEndI); 2507 if (!CB) 2508 continue; 2509 const auto &NoReturnAA = 2510 A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB)); 2511 bool MayReturn = !NoReturnAA.isAssumedNoReturn(); 2512 if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB))) 2513 continue; 2514 Instruction *I = const_cast<Instruction *>(DeadEndI); 2515 BasicBlock *BB = I->getParent(); 2516 Instruction *SplitPos = I->getNextNode(); 2517 // TODO: mark stuff before unreachable instructions as dead. 2518 2519 if (auto *II = dyn_cast<InvokeInst>(I)) { 2520 // If we keep the invoke the split position is at the beginning of the 2521 // normal desitination block (it invokes a noreturn function after all). 2522 BasicBlock *NormalDestBB = II->getNormalDest(); 2523 SplitPos = &NormalDestBB->front(); 2524 2525 /// Invoke is replaced with a call and unreachable is placed after it if 2526 /// the callee is nounwind and noreturn. Otherwise, we keep the invoke 2527 /// and only place an unreachable in the normal successor. 2528 if (Invoke2CallAllowed) { 2529 if (II->getCalledFunction()) { 2530 const IRPosition &IPos = IRPosition::callsite_function(*II); 2531 const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos); 2532 if (AANoUnw.isAssumedNoUnwind()) { 2533 LLVM_DEBUG(dbgs() 2534 << "[AAIsDead] Replace invoke with call inst\n"); 2535 CallInst *CI = createCallMatchingInvoke(II); 2536 CI->insertBefore(II); 2537 CI->takeName(II); 2538 II->replaceAllUsesWith(CI); 2539 2540 // If this is a nounwind + mayreturn invoke we only remove the unwind edge. 2541 // This is done by moving the invoke into a new and dead block and connecting 2542 // the normal destination of the invoke with a branch that follows the call 2543 // replacement we created above. 2544 if (MayReturn) { 2545 BasicBlock *NewDeadBB = SplitBlock(BB, II, nullptr, nullptr, nullptr, ".i2c"); 2546 assert(isa<BranchInst>(BB->getTerminator()) && 2547 BB->getTerminator()->getNumSuccessors() == 1 && 2548 BB->getTerminator()->getSuccessor(0) == NewDeadBB); 2549 new UnreachableInst(I->getContext(), NewDeadBB); 2550 BB->getTerminator()->setOperand(0, NormalDestBB); 2551 A.deleteAfterManifest(*II); 2552 continue; 2553 } 2554 2555 // We do not need an invoke (II) but instead want a call followed 2556 // by an unreachable. However, we do not remove II as other 2557 // abstract attributes might have it cached as part of their 2558 // results. Given that we modify the CFG anyway, we simply keep II 2559 // around but in a new dead block. To avoid II being live through 2560 // a different edge we have to ensure the block we place it in is 2561 // only reached from the current block of II and then not reached 2562 // at all when we insert the unreachable. 2563 SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c"); 2564 SplitPos = CI->getNextNode(); 2565 } 2566 } 2567 } 2568 2569 if (SplitPos == &NormalDestBB->front()) { 2570 // If this is an invoke of a noreturn function the edge to the normal 2571 // destination block is dead but not necessarily the block itself. 2572 // TODO: We need to move to an edge based system during deduction and 2573 // also manifest. 2574 assert(!NormalDestBB->isLandingPad() && 2575 "Expected the normal destination not to be a landingpad!"); 2576 if (NormalDestBB->getUniquePredecessor() == BB) { 2577 assumeLive(A, *NormalDestBB); 2578 } else { 2579 BasicBlock *SplitBB = 2580 SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); 2581 // The split block is live even if it contains only an unreachable 2582 // instruction at the end. 2583 assumeLive(A, *SplitBB); 2584 SplitPos = SplitBB->getTerminator(); 2585 HasChanged = ChangeStatus::CHANGED; 2586 } 2587 } 2588 } 2589 2590 if (isa_and_nonnull<UnreachableInst>(SplitPos)) 2591 continue; 2592 2593 BB = SplitPos->getParent(); 2594 SplitBlock(BB, SplitPos); 2595 changeToUnreachable(BB->getTerminator(), /* UseLLVMTrap */ false); 2596 HasChanged = ChangeStatus::CHANGED; 2597 } 2598 2599 for (BasicBlock &BB : F) 2600 if (!AssumedLiveBlocks.count(&BB)) 2601 A.deleteAfterManifest(BB); 2602 2603 return HasChanged; 2604 } 2605 2606 /// See AbstractAttribute::updateImpl(...). 2607 ChangeStatus updateImpl(Attributor &A) override; 2608 2609 /// See AbstractAttribute::trackStatistics() 2610 void trackStatistics() const override {} 2611 2612 /// Returns true if the function is assumed dead. 2613 bool isAssumedDead() const override { return false; } 2614 2615 /// See AAIsDead::isAssumedDead(BasicBlock *). 2616 bool isAssumedDead(const BasicBlock *BB) const override { 2617 assert(BB->getParent() == getAssociatedFunction() && 2618 "BB must be in the same anchor scope function."); 2619 2620 if (!getAssumed()) 2621 return false; 2622 return !AssumedLiveBlocks.count(BB); 2623 } 2624 2625 /// See AAIsDead::isKnownDead(BasicBlock *). 2626 bool isKnownDead(const BasicBlock *BB) const override { 2627 return getKnown() && isAssumedDead(BB); 2628 } 2629 2630 /// See AAIsDead::isAssumed(Instruction *I). 2631 bool isAssumedDead(const Instruction *I) const override { 2632 assert(I->getParent()->getParent() == getAssociatedFunction() && 2633 "Instruction must be in the same anchor scope function."); 2634 2635 if (!getAssumed()) 2636 return false; 2637 2638 // If it is not in AssumedLiveBlocks then it for sure dead. 2639 // Otherwise, it can still be after noreturn call in a live block. 2640 if (!AssumedLiveBlocks.count(I->getParent())) 2641 return true; 2642 2643 // If it is not after a liveness barrier it is live. 2644 const Instruction *PrevI = I->getPrevNode(); 2645 while (PrevI) { 2646 if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI)) 2647 return true; 2648 PrevI = PrevI->getPrevNode(); 2649 } 2650 return false; 2651 } 2652 2653 /// See AAIsDead::isKnownDead(Instruction *I). 2654 bool isKnownDead(const Instruction *I) const override { 2655 return getKnown() && isAssumedDead(I); 2656 } 2657 2658 /// Determine if \p F might catch asynchronous exceptions. 2659 static bool mayCatchAsynchronousExceptions(const Function &F) { 2660 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); 2661 } 2662 2663 /// Assume \p BB is (partially) live now and indicate to the Attributor \p A 2664 /// that internal function called from \p BB should now be looked at. 2665 bool assumeLive(Attributor &A, const BasicBlock &BB) { 2666 if (!AssumedLiveBlocks.insert(&BB).second) 2667 return false; 2668 2669 // We assume that all of BB is (probably) live now and if there are calls to 2670 // internal functions we will assume that those are now live as well. This 2671 // is a performance optimization for blocks with calls to a lot of internal 2672 // functions. It can however cause dead functions to be treated as live. 2673 for (const Instruction &I : BB) 2674 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) 2675 if (const Function *F = ICS.getCalledFunction()) 2676 if (F->hasLocalLinkage()) 2677 A.markLiveInternalFunction(*F); 2678 return true; 2679 } 2680 2681 /// Collection of instructions that need to be explored again, e.g., we 2682 /// did assume they do not transfer control to (one of their) successors. 2683 SmallSetVector<const Instruction *, 8> ToBeExploredFrom; 2684 2685 /// Collection of instructions that are known to not transfer control. 2686 SmallSetVector<const Instruction *, 8> KnownDeadEnds; 2687 2688 /// Collection of all assumed live BasicBlocks. 2689 DenseSet<const BasicBlock *> AssumedLiveBlocks; 2690 }; 2691 2692 static bool 2693 identifyAliveSuccessors(Attributor &A, const CallBase &CB, 2694 AbstractAttribute &AA, 2695 SmallVectorImpl<const Instruction *> &AliveSuccessors) { 2696 const IRPosition &IPos = IRPosition::callsite_function(CB); 2697 2698 const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos); 2699 if (NoReturnAA.isAssumedNoReturn()) 2700 return !NoReturnAA.isKnownNoReturn(); 2701 if (CB.isTerminator()) 2702 AliveSuccessors.push_back(&CB.getSuccessor(0)->front()); 2703 else 2704 AliveSuccessors.push_back(CB.getNextNode()); 2705 return false; 2706 } 2707 2708 static bool 2709 identifyAliveSuccessors(Attributor &A, const InvokeInst &II, 2710 AbstractAttribute &AA, 2711 SmallVectorImpl<const Instruction *> &AliveSuccessors) { 2712 bool UsedAssumedInformation = 2713 identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors); 2714 2715 // First, determine if we can change an invoke to a call assuming the 2716 // callee is nounwind. This is not possible if the personality of the 2717 // function allows to catch asynchronous exceptions. 2718 if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) { 2719 AliveSuccessors.push_back(&II.getUnwindDest()->front()); 2720 } else { 2721 const IRPosition &IPos = IRPosition::callsite_function(II); 2722 const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos); 2723 if (AANoUnw.isAssumedNoUnwind()) { 2724 UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind(); 2725 } else { 2726 AliveSuccessors.push_back(&II.getUnwindDest()->front()); 2727 } 2728 } 2729 return UsedAssumedInformation; 2730 } 2731 2732 static Optional<ConstantInt *> 2733 getAssumedConstant(Attributor &A, const Value &V, AbstractAttribute &AA, 2734 bool &UsedAssumedInformation) { 2735 const auto &ValueSimplifyAA = 2736 A.getAAFor<AAValueSimplify>(AA, IRPosition::value(V)); 2737 Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A); 2738 UsedAssumedInformation |= !ValueSimplifyAA.isKnown(); 2739 if (!SimplifiedV.hasValue()) 2740 return llvm::None; 2741 if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) 2742 return llvm::None; 2743 return dyn_cast_or_null<ConstantInt>(SimplifiedV.getValue()); 2744 } 2745 2746 static bool 2747 identifyAliveSuccessors(Attributor &A, const BranchInst &BI, 2748 AbstractAttribute &AA, 2749 SmallVectorImpl<const Instruction *> &AliveSuccessors) { 2750 bool UsedAssumedInformation = false; 2751 if (BI.getNumSuccessors() == 1) { 2752 AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); 2753 } else { 2754 Optional<ConstantInt *> CI = 2755 getAssumedConstant(A, *BI.getCondition(), AA, UsedAssumedInformation); 2756 if (!CI.hasValue()) { 2757 // No value yet, assume both edges are dead. 2758 } else if (CI.getValue()) { 2759 const BasicBlock *SuccBB = 2760 BI.getSuccessor(1 - CI.getValue()->getZExtValue()); 2761 AliveSuccessors.push_back(&SuccBB->front()); 2762 } else { 2763 AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); 2764 AliveSuccessors.push_back(&BI.getSuccessor(1)->front()); 2765 UsedAssumedInformation = false; 2766 } 2767 } 2768 return UsedAssumedInformation; 2769 } 2770 2771 static bool 2772 identifyAliveSuccessors(Attributor &A, const SwitchInst &SI, 2773 AbstractAttribute &AA, 2774 SmallVectorImpl<const Instruction *> &AliveSuccessors) { 2775 bool UsedAssumedInformation = false; 2776 Optional<ConstantInt *> CI = 2777 getAssumedConstant(A, *SI.getCondition(), AA, UsedAssumedInformation); 2778 if (!CI.hasValue()) { 2779 // No value yet, assume all edges are dead. 2780 } else if (CI.getValue()) { 2781 for (auto &CaseIt : SI.cases()) { 2782 if (CaseIt.getCaseValue() == CI.getValue()) { 2783 AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front()); 2784 return UsedAssumedInformation; 2785 } 2786 } 2787 AliveSuccessors.push_back(&SI.getDefaultDest()->front()); 2788 return UsedAssumedInformation; 2789 } else { 2790 for (const BasicBlock *SuccBB : successors(SI.getParent())) 2791 AliveSuccessors.push_back(&SuccBB->front()); 2792 } 2793 return UsedAssumedInformation; 2794 } 2795 2796 ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) { 2797 ChangeStatus Change = ChangeStatus::UNCHANGED; 2798 2799 LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" 2800 << getAssociatedFunction()->size() << "] BBs and " 2801 << ToBeExploredFrom.size() << " exploration points and " 2802 << KnownDeadEnds.size() << " known dead ends\n"); 2803 2804 // Copy and clear the list of instructions we need to explore from. It is 2805 // refilled with instructions the next update has to look at. 2806 SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(), 2807 ToBeExploredFrom.end()); 2808 decltype(ToBeExploredFrom) NewToBeExploredFrom; 2809 2810 SmallVector<const Instruction *, 8> AliveSuccessors; 2811 while (!Worklist.empty()) { 2812 const Instruction *I = Worklist.pop_back_val(); 2813 LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n"); 2814 2815 AliveSuccessors.clear(); 2816 2817 bool UsedAssumedInformation = false; 2818 switch (I->getOpcode()) { 2819 // TODO: look for (assumed) UB to backwards propagate "deadness". 2820 default: 2821 if (I->isTerminator()) { 2822 for (const BasicBlock *SuccBB : successors(I->getParent())) 2823 AliveSuccessors.push_back(&SuccBB->front()); 2824 } else { 2825 AliveSuccessors.push_back(I->getNextNode()); 2826 } 2827 break; 2828 case Instruction::Call: 2829 UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I), 2830 *this, AliveSuccessors); 2831 break; 2832 case Instruction::Invoke: 2833 UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I), 2834 *this, AliveSuccessors); 2835 break; 2836 case Instruction::Br: 2837 UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I), 2838 *this, AliveSuccessors); 2839 break; 2840 case Instruction::Switch: 2841 UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I), 2842 *this, AliveSuccessors); 2843 break; 2844 } 2845 2846 if (UsedAssumedInformation) { 2847 NewToBeExploredFrom.insert(I); 2848 } else { 2849 Change = ChangeStatus::CHANGED; 2850 if (AliveSuccessors.empty() || 2851 (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors())) 2852 KnownDeadEnds.insert(I); 2853 } 2854 2855 LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: " 2856 << AliveSuccessors.size() << " UsedAssumedInformation: " 2857 << UsedAssumedInformation << "\n"); 2858 2859 for (const Instruction *AliveSuccessor : AliveSuccessors) { 2860 if (!I->isTerminator()) { 2861 assert(AliveSuccessors.size() == 1 && 2862 "Non-terminator expected to have a single successor!"); 2863 Worklist.push_back(AliveSuccessor); 2864 } else { 2865 if (assumeLive(A, *AliveSuccessor->getParent())) 2866 Worklist.push_back(AliveSuccessor); 2867 } 2868 } 2869 } 2870 2871 ToBeExploredFrom = std::move(NewToBeExploredFrom); 2872 2873 // If we know everything is live there is no need to query for liveness. 2874 // Instead, indicating a pessimistic fixpoint will cause the state to be 2875 // "invalid" and all queries to be answered conservatively without lookups. 2876 // To be in this state we have to (1) finished the exploration and (3) not 2877 // discovered any non-trivial dead end and (2) not ruled unreachable code 2878 // dead. 2879 if (ToBeExploredFrom.empty() && 2880 getAssociatedFunction()->size() == AssumedLiveBlocks.size() && 2881 llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) { 2882 return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0; 2883 })) 2884 return indicatePessimisticFixpoint(); 2885 return Change; 2886 } 2887 2888 /// Liveness information for a call sites. 2889 struct AAIsDeadCallSite final : AAIsDeadFunction { 2890 AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {} 2891 2892 /// See AbstractAttribute::initialize(...). 2893 void initialize(Attributor &A) override { 2894 // TODO: Once we have call site specific value information we can provide 2895 // call site specific liveness information and then it makes 2896 // sense to specialize attributes for call sites instead of 2897 // redirecting requests to the callee. 2898 llvm_unreachable("Abstract attributes for liveness are not " 2899 "supported for call sites yet!"); 2900 } 2901 2902 /// See AbstractAttribute::updateImpl(...). 2903 ChangeStatus updateImpl(Attributor &A) override { 2904 return indicatePessimisticFixpoint(); 2905 } 2906 2907 /// See AbstractAttribute::trackStatistics() 2908 void trackStatistics() const override {} 2909 }; 2910 2911 /// -------------------- Dereferenceable Argument Attribute -------------------- 2912 2913 template <> 2914 ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S, 2915 const DerefState &R) { 2916 ChangeStatus CS0 = 2917 clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState); 2918 ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState); 2919 return CS0 | CS1; 2920 } 2921 2922 struct AADereferenceableImpl : AADereferenceable { 2923 AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} 2924 using StateType = DerefState; 2925 2926 void initialize(Attributor &A) override { 2927 SmallVector<Attribute, 4> Attrs; 2928 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, 2929 Attrs); 2930 for (const Attribute &Attr : Attrs) 2931 takeKnownDerefBytesMaximum(Attr.getValueAsInt()); 2932 2933 NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition()); 2934 2935 const IRPosition &IRP = this->getIRPosition(); 2936 bool IsFnInterface = IRP.isFnInterfaceKind(); 2937 const Function *FnScope = IRP.getAnchorScope(); 2938 if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition())) 2939 indicatePessimisticFixpoint(); 2940 } 2941 2942 /// See AbstractAttribute::getState() 2943 /// { 2944 StateType &getState() override { return *this; } 2945 const StateType &getState() const override { return *this; } 2946 /// } 2947 2948 /// Helper function for collecting accessed bytes in must-be-executed-context 2949 void addAccessedBytesForUse(Attributor &A, const Use *U, 2950 const Instruction *I) { 2951 const Value *UseV = U->get(); 2952 if (!UseV->getType()->isPointerTy()) 2953 return; 2954 2955 Type *PtrTy = UseV->getType(); 2956 const DataLayout &DL = A.getDataLayout(); 2957 int64_t Offset; 2958 if (const Value *Base = getBasePointerOfAccessPointerOperand( 2959 I, Offset, DL, /*AllowNonInbounds*/ true)) { 2960 if (Base == &getAssociatedValue() && getPointerOperand(I) == UseV) { 2961 uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType()); 2962 addAccessedBytes(Offset, Size); 2963 } 2964 } 2965 return; 2966 } 2967 2968 /// See AAFromMustBeExecutedContext 2969 bool followUse(Attributor &A, const Use *U, const Instruction *I) { 2970 bool IsNonNull = false; 2971 bool TrackUse = false; 2972 int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse( 2973 A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse); 2974 2975 addAccessedBytesForUse(A, U, I); 2976 takeKnownDerefBytesMaximum(DerefBytes); 2977 return TrackUse; 2978 } 2979 2980 /// See AbstractAttribute::manifest(...). 2981 ChangeStatus manifest(Attributor &A) override { 2982 ChangeStatus Change = AADereferenceable::manifest(A); 2983 if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) { 2984 removeAttrs({Attribute::DereferenceableOrNull}); 2985 return ChangeStatus::CHANGED; 2986 } 2987 return Change; 2988 } 2989 2990 void getDeducedAttributes(LLVMContext &Ctx, 2991 SmallVectorImpl<Attribute> &Attrs) const override { 2992 // TODO: Add *_globally support 2993 if (isAssumedNonNull()) 2994 Attrs.emplace_back(Attribute::getWithDereferenceableBytes( 2995 Ctx, getAssumedDereferenceableBytes())); 2996 else 2997 Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( 2998 Ctx, getAssumedDereferenceableBytes())); 2999 } 3000 3001 /// See AbstractAttribute::getAsStr(). 3002 const std::string getAsStr() const override { 3003 if (!getAssumedDereferenceableBytes()) 3004 return "unknown-dereferenceable"; 3005 return std::string("dereferenceable") + 3006 (isAssumedNonNull() ? "" : "_or_null") + 3007 (isAssumedGlobal() ? "_globally" : "") + "<" + 3008 std::to_string(getKnownDereferenceableBytes()) + "-" + 3009 std::to_string(getAssumedDereferenceableBytes()) + ">"; 3010 } 3011 }; 3012 3013 /// Dereferenceable attribute for a floating value. 3014 struct AADereferenceableFloating 3015 : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> { 3016 using Base = 3017 AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>; 3018 AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {} 3019 3020 /// See AbstractAttribute::updateImpl(...). 3021 ChangeStatus updateImpl(Attributor &A) override { 3022 ChangeStatus Change = Base::updateImpl(A); 3023 3024 const DataLayout &DL = A.getDataLayout(); 3025 3026 auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool { 3027 unsigned IdxWidth = 3028 DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); 3029 APInt Offset(IdxWidth, 0); 3030 const Value *Base = 3031 V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); 3032 3033 const auto &AA = 3034 A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base)); 3035 int64_t DerefBytes = 0; 3036 if (!Stripped && this == &AA) { 3037 // Use IR information if we did not strip anything. 3038 // TODO: track globally. 3039 bool CanBeNull; 3040 DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull); 3041 T.GlobalState.indicatePessimisticFixpoint(); 3042 } else { 3043 const DerefState &DS = static_cast<const DerefState &>(AA.getState()); 3044 DerefBytes = DS.DerefBytesState.getAssumed(); 3045 T.GlobalState &= DS.GlobalState; 3046 } 3047 3048 // For now we do not try to "increase" dereferenceability due to negative 3049 // indices as we first have to come up with code to deal with loops and 3050 // for overflows of the dereferenceable bytes. 3051 int64_t OffsetSExt = Offset.getSExtValue(); 3052 if (OffsetSExt < 0) 3053 OffsetSExt = 0; 3054 3055 T.takeAssumedDerefBytesMinimum( 3056 std::max(int64_t(0), DerefBytes - OffsetSExt)); 3057 3058 if (this == &AA) { 3059 if (!Stripped) { 3060 // If nothing was stripped IR information is all we got. 3061 T.takeKnownDerefBytesMaximum( 3062 std::max(int64_t(0), DerefBytes - OffsetSExt)); 3063 T.indicatePessimisticFixpoint(); 3064 } else if (OffsetSExt > 0) { 3065 // If something was stripped but there is circular reasoning we look 3066 // for the offset. If it is positive we basically decrease the 3067 // dereferenceable bytes in a circluar loop now, which will simply 3068 // drive them down to the known value in a very slow way which we 3069 // can accelerate. 3070 T.indicatePessimisticFixpoint(); 3071 } 3072 } 3073 3074 return T.isValidState(); 3075 }; 3076 3077 DerefState T; 3078 if (!genericValueTraversal<AADereferenceable, DerefState>( 3079 A, getIRPosition(), *this, T, VisitValueCB)) 3080 return indicatePessimisticFixpoint(); 3081 3082 return Change | clampStateAndIndicateChange(getState(), T); 3083 } 3084 3085 /// See AbstractAttribute::trackStatistics() 3086 void trackStatistics() const override { 3087 STATS_DECLTRACK_FLOATING_ATTR(dereferenceable) 3088 } 3089 }; 3090 3091 /// Dereferenceable attribute for a return value. 3092 struct AADereferenceableReturned final 3093 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, 3094 DerefState> { 3095 AADereferenceableReturned(const IRPosition &IRP) 3096 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl, 3097 DerefState>(IRP) {} 3098 3099 /// See AbstractAttribute::trackStatistics() 3100 void trackStatistics() const override { 3101 STATS_DECLTRACK_FNRET_ATTR(dereferenceable) 3102 } 3103 }; 3104 3105 /// Dereferenceable attribute for an argument 3106 struct AADereferenceableArgument final 3107 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< 3108 AADereferenceable, AADereferenceableImpl, DerefState> { 3109 using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext< 3110 AADereferenceable, AADereferenceableImpl, DerefState>; 3111 AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {} 3112 3113 /// See AbstractAttribute::trackStatistics() 3114 void trackStatistics() const override { 3115 STATS_DECLTRACK_ARG_ATTR(dereferenceable) 3116 } 3117 }; 3118 3119 /// Dereferenceable attribute for a call site argument. 3120 struct AADereferenceableCallSiteArgument final : AADereferenceableFloating { 3121 AADereferenceableCallSiteArgument(const IRPosition &IRP) 3122 : AADereferenceableFloating(IRP) {} 3123 3124 /// See AbstractAttribute::trackStatistics() 3125 void trackStatistics() const override { 3126 STATS_DECLTRACK_CSARG_ATTR(dereferenceable) 3127 } 3128 }; 3129 3130 /// Dereferenceable attribute deduction for a call site return value. 3131 struct AADereferenceableCallSiteReturned final 3132 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext< 3133 AADereferenceable, AADereferenceableImpl> { 3134 using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext< 3135 AADereferenceable, AADereferenceableImpl>; 3136 AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} 3137 3138 /// See AbstractAttribute::trackStatistics() 3139 void trackStatistics() const override { 3140 STATS_DECLTRACK_CS_ATTR(dereferenceable); 3141 } 3142 }; 3143 3144 // ------------------------ Align Argument Attribute ------------------------ 3145 3146 static unsigned int getKnownAlignForUse(Attributor &A, 3147 AbstractAttribute &QueryingAA, 3148 Value &AssociatedValue, const Use *U, 3149 const Instruction *I, bool &TrackUse) { 3150 // We need to follow common pointer manipulation uses to the accesses they 3151 // feed into. 3152 if (isa<CastInst>(I)) { 3153 TrackUse = true; 3154 return 0; 3155 } 3156 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { 3157 if (GEP->hasAllConstantIndices()) { 3158 TrackUse = true; 3159 return 0; 3160 } 3161 } 3162 3163 unsigned Alignment = 0; 3164 if (ImmutableCallSite ICS = ImmutableCallSite(I)) { 3165 if (ICS.isBundleOperand(U) || ICS.isCallee(U)) 3166 return 0; 3167 3168 unsigned ArgNo = ICS.getArgumentNo(U); 3169 IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo); 3170 // As long as we only use known information there is no need to track 3171 // dependences here. 3172 auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, 3173 /* TrackDependence */ false); 3174 Alignment = AlignAA.getKnownAlign(); 3175 } 3176 3177 const Value *UseV = U->get(); 3178 if (auto *SI = dyn_cast<StoreInst>(I)) 3179 Alignment = SI->getAlignment(); 3180 else if (auto *LI = dyn_cast<LoadInst>(I)) 3181 Alignment = LI->getAlignment(); 3182 3183 if (Alignment <= 1) 3184 return 0; 3185 3186 auto &DL = A.getDataLayout(); 3187 int64_t Offset; 3188 3189 if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) { 3190 if (Base == &AssociatedValue) { 3191 // BasePointerAddr + Offset = Alignment * Q for some integer Q. 3192 // So we can say that the maximum power of two which is a divisor of 3193 // gcd(Offset, Alignment) is an alignment. 3194 3195 uint32_t gcd = 3196 greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment); 3197 Alignment = llvm::PowerOf2Floor(gcd); 3198 } 3199 } 3200 3201 return Alignment; 3202 } 3203 struct AAAlignImpl : AAAlign { 3204 AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} 3205 3206 /// See AbstractAttribute::initialize(...). 3207 void initialize(Attributor &A) override { 3208 SmallVector<Attribute, 4> Attrs; 3209 getAttrs({Attribute::Alignment}, Attrs); 3210 for (const Attribute &Attr : Attrs) 3211 takeKnownMaximum(Attr.getValueAsInt()); 3212 3213 if (getIRPosition().isFnInterfaceKind() && 3214 (!getAssociatedFunction() || 3215 !getAssociatedFunction()->hasExactDefinition())) 3216 indicatePessimisticFixpoint(); 3217 } 3218 3219 /// See AbstractAttribute::manifest(...). 3220 ChangeStatus manifest(Attributor &A) override { 3221 ChangeStatus Changed = ChangeStatus::UNCHANGED; 3222 3223 // Check for users that allow alignment annotations. 3224 Value &AnchorVal = getIRPosition().getAnchorValue(); 3225 for (const Use &U : AnchorVal.uses()) { 3226 if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { 3227 if (SI->getPointerOperand() == &AnchorVal) 3228 if (SI->getAlignment() < getAssumedAlign()) { 3229 STATS_DECLTRACK(AAAlign, Store, 3230 "Number of times alignemnt added to a store"); 3231 SI->setAlignment(Align(getAssumedAlign())); 3232 Changed = ChangeStatus::CHANGED; 3233 } 3234 } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { 3235 if (LI->getPointerOperand() == &AnchorVal) 3236 if (LI->getAlignment() < getAssumedAlign()) { 3237 LI->setAlignment(Align(getAssumedAlign())); 3238 STATS_DECLTRACK(AAAlign, Load, 3239 "Number of times alignemnt added to a load"); 3240 Changed = ChangeStatus::CHANGED; 3241 } 3242 } 3243 } 3244 3245 return AAAlign::manifest(A) | Changed; 3246 } 3247 3248 // TODO: Provide a helper to determine the implied ABI alignment and check in 3249 // the existing manifest method and a new one for AAAlignImpl that value 3250 // to avoid making the alignment explicit if it did not improve. 3251 3252 /// See AbstractAttribute::getDeducedAttributes 3253 virtual void 3254 getDeducedAttributes(LLVMContext &Ctx, 3255 SmallVectorImpl<Attribute> &Attrs) const override { 3256 if (getAssumedAlign() > 1) 3257 Attrs.emplace_back( 3258 Attribute::getWithAlignment(Ctx, Align(getAssumedAlign()))); 3259 } 3260 /// See AAFromMustBeExecutedContext 3261 bool followUse(Attributor &A, const Use *U, const Instruction *I) { 3262 bool TrackUse = false; 3263 3264 unsigned int KnownAlign = 3265 getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse); 3266 takeKnownMaximum(KnownAlign); 3267 3268 return TrackUse; 3269 } 3270 3271 /// See AbstractAttribute::getAsStr(). 3272 const std::string getAsStr() const override { 3273 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + 3274 "-" + std::to_string(getAssumedAlign()) + ">") 3275 : "unknown-align"; 3276 } 3277 }; 3278 3279 /// Align attribute for a floating value. 3280 struct AAAlignFloating : AAFromMustBeExecutedContext<AAAlign, AAAlignImpl> { 3281 using Base = AAFromMustBeExecutedContext<AAAlign, AAAlignImpl>; 3282 AAAlignFloating(const IRPosition &IRP) : Base(IRP) {} 3283 3284 /// See AbstractAttribute::updateImpl(...). 3285 ChangeStatus updateImpl(Attributor &A) override { 3286 Base::updateImpl(A); 3287 3288 const DataLayout &DL = A.getDataLayout(); 3289 3290 auto VisitValueCB = [&](Value &V, AAAlign::StateType &T, 3291 bool Stripped) -> bool { 3292 const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); 3293 if (!Stripped && this == &AA) { 3294 // Use only IR information if we did not strip anything. 3295 const MaybeAlign PA = V.getPointerAlignment(DL); 3296 T.takeKnownMaximum(PA ? PA->value() : 0); 3297 T.indicatePessimisticFixpoint(); 3298 } else { 3299 // Use abstract attribute information. 3300 const AAAlign::StateType &DS = 3301 static_cast<const AAAlign::StateType &>(AA.getState()); 3302 T ^= DS; 3303 } 3304 return T.isValidState(); 3305 }; 3306 3307 StateType T; 3308 if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T, 3309 VisitValueCB)) 3310 return indicatePessimisticFixpoint(); 3311 3312 // TODO: If we know we visited all incoming values, thus no are assumed 3313 // dead, we can take the known information from the state T. 3314 return clampStateAndIndicateChange(getState(), T); 3315 } 3316 3317 /// See AbstractAttribute::trackStatistics() 3318 void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) } 3319 }; 3320 3321 /// Align attribute for function return value. 3322 struct AAAlignReturned final 3323 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> { 3324 AAAlignReturned(const IRPosition &IRP) 3325 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {} 3326 3327 /// See AbstractAttribute::trackStatistics() 3328 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } 3329 }; 3330 3331 /// Align attribute for function argument. 3332 struct AAAlignArgument final 3333 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, 3334 AAAlignImpl> { 3335 AAAlignArgument(const IRPosition &IRP) 3336 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign, 3337 AAAlignImpl>( 3338 IRP) {} 3339 3340 /// See AbstractAttribute::trackStatistics() 3341 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) } 3342 }; 3343 3344 struct AAAlignCallSiteArgument final : AAAlignFloating { 3345 AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {} 3346 3347 /// See AbstractAttribute::manifest(...). 3348 ChangeStatus manifest(Attributor &A) override { 3349 return AAAlignImpl::manifest(A); 3350 } 3351 3352 /// See AbstractAttribute::trackStatistics() 3353 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } 3354 }; 3355 3356 /// Align attribute deduction for a call site return value. 3357 struct AAAlignCallSiteReturned final 3358 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, 3359 AAAlignImpl> { 3360 using Base = 3361 AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign, 3362 AAAlignImpl>; 3363 AAAlignCallSiteReturned(const IRPosition &IRP) : Base(IRP) {} 3364 3365 /// See AbstractAttribute::initialize(...). 3366 void initialize(Attributor &A) override { 3367 Base::initialize(A); 3368 Function *F = getAssociatedFunction(); 3369 if (!F) 3370 indicatePessimisticFixpoint(); 3371 } 3372 3373 /// See AbstractAttribute::trackStatistics() 3374 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); } 3375 }; 3376 3377 /// ------------------ Function No-Return Attribute ---------------------------- 3378 struct AANoReturnImpl : public AANoReturn { 3379 AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} 3380 3381 /// See AbstractAttribute::initialize(...). 3382 void initialize(Attributor &A) override { 3383 AANoReturn::initialize(A); 3384 Function *F = getAssociatedFunction(); 3385 if (!F) 3386 indicatePessimisticFixpoint(); 3387 } 3388 3389 /// See AbstractAttribute::getAsStr(). 3390 const std::string getAsStr() const override { 3391 return getAssumed() ? "noreturn" : "may-return"; 3392 } 3393 3394 /// See AbstractAttribute::updateImpl(Attributor &A). 3395 virtual ChangeStatus updateImpl(Attributor &A) override { 3396 auto CheckForNoReturn = [](Instruction &) { return false; }; 3397 if (!A.checkForAllInstructions(CheckForNoReturn, *this, 3398 {(unsigned)Instruction::Ret})) 3399 return indicatePessimisticFixpoint(); 3400 return ChangeStatus::UNCHANGED; 3401 } 3402 }; 3403 3404 struct AANoReturnFunction final : AANoReturnImpl { 3405 AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} 3406 3407 /// See AbstractAttribute::trackStatistics() 3408 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } 3409 }; 3410 3411 /// NoReturn attribute deduction for a call sites. 3412 struct AANoReturnCallSite final : AANoReturnImpl { 3413 AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {} 3414 3415 /// See AbstractAttribute::updateImpl(...). 3416 ChangeStatus updateImpl(Attributor &A) override { 3417 // TODO: Once we have call site specific value information we can provide 3418 // call site specific liveness information and then it makes 3419 // sense to specialize attributes for call sites arguments instead of 3420 // redirecting requests to the callee argument. 3421 Function *F = getAssociatedFunction(); 3422 const IRPosition &FnPos = IRPosition::function(*F); 3423 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos); 3424 return clampStateAndIndicateChange( 3425 getState(), 3426 static_cast<const AANoReturn::StateType &>(FnAA.getState())); 3427 } 3428 3429 /// See AbstractAttribute::trackStatistics() 3430 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); } 3431 }; 3432 3433 /// ----------------------- Variable Capturing --------------------------------- 3434 3435 /// A class to hold the state of for no-capture attributes. 3436 struct AANoCaptureImpl : public AANoCapture { 3437 AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {} 3438 3439 /// See AbstractAttribute::initialize(...). 3440 void initialize(Attributor &A) override { 3441 if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) { 3442 indicateOptimisticFixpoint(); 3443 return; 3444 } 3445 Function *AnchorScope = getAnchorScope(); 3446 if (isFnInterfaceKind() && 3447 (!AnchorScope || !AnchorScope->hasExactDefinition())) { 3448 indicatePessimisticFixpoint(); 3449 return; 3450 } 3451 3452 // You cannot "capture" null in the default address space. 3453 if (isa<ConstantPointerNull>(getAssociatedValue()) && 3454 getAssociatedValue().getType()->getPointerAddressSpace() == 0) { 3455 indicateOptimisticFixpoint(); 3456 return; 3457 } 3458 3459 const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope; 3460 3461 // Check what state the associated function can actually capture. 3462 if (F) 3463 determineFunctionCaptureCapabilities(getIRPosition(), *F, *this); 3464 else 3465 indicatePessimisticFixpoint(); 3466 } 3467 3468 /// See AbstractAttribute::updateImpl(...). 3469 ChangeStatus updateImpl(Attributor &A) override; 3470 3471 /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...). 3472 virtual void 3473 getDeducedAttributes(LLVMContext &Ctx, 3474 SmallVectorImpl<Attribute> &Attrs) const override { 3475 if (!isAssumedNoCaptureMaybeReturned()) 3476 return; 3477 3478 if (getArgNo() >= 0) { 3479 if (isAssumedNoCapture()) 3480 Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture)); 3481 else if (ManifestInternal) 3482 Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned")); 3483 } 3484 } 3485 3486 /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known 3487 /// depending on the ability of the function associated with \p IRP to capture 3488 /// state in memory and through "returning/throwing", respectively. 3489 static void determineFunctionCaptureCapabilities(const IRPosition &IRP, 3490 const Function &F, 3491 BitIntegerState &State) { 3492 // TODO: Once we have memory behavior attributes we should use them here. 3493 3494 // If we know we cannot communicate or write to memory, we do not care about 3495 // ptr2int anymore. 3496 if (F.onlyReadsMemory() && F.doesNotThrow() && 3497 F.getReturnType()->isVoidTy()) { 3498 State.addKnownBits(NO_CAPTURE); 3499 return; 3500 } 3501 3502 // A function cannot capture state in memory if it only reads memory, it can 3503 // however return/throw state and the state might be influenced by the 3504 // pointer value, e.g., loading from a returned pointer might reveal a bit. 3505 if (F.onlyReadsMemory()) 3506 State.addKnownBits(NOT_CAPTURED_IN_MEM); 3507 3508 // A function cannot communicate state back if it does not through 3509 // exceptions and doesn not return values. 3510 if (F.doesNotThrow() && F.getReturnType()->isVoidTy()) 3511 State.addKnownBits(NOT_CAPTURED_IN_RET); 3512 3513 // Check existing "returned" attributes. 3514 int ArgNo = IRP.getArgNo(); 3515 if (F.doesNotThrow() && ArgNo >= 0) { 3516 for (unsigned u = 0, e = F.arg_size(); u < e; ++u) 3517 if (F.hasParamAttribute(u, Attribute::Returned)) { 3518 if (u == unsigned(ArgNo)) 3519 State.removeAssumedBits(NOT_CAPTURED_IN_RET); 3520 else if (F.onlyReadsMemory()) 3521 State.addKnownBits(NO_CAPTURE); 3522 else 3523 State.addKnownBits(NOT_CAPTURED_IN_RET); 3524 break; 3525 } 3526 } 3527 } 3528 3529 /// See AbstractState::getAsStr(). 3530 const std::string getAsStr() const override { 3531 if (isKnownNoCapture()) 3532 return "known not-captured"; 3533 if (isAssumedNoCapture()) 3534 return "assumed not-captured"; 3535 if (isKnownNoCaptureMaybeReturned()) 3536 return "known not-captured-maybe-returned"; 3537 if (isAssumedNoCaptureMaybeReturned()) 3538 return "assumed not-captured-maybe-returned"; 3539 return "assumed-captured"; 3540 } 3541 }; 3542 3543 /// Attributor-aware capture tracker. 3544 struct AACaptureUseTracker final : public CaptureTracker { 3545 3546 /// Create a capture tracker that can lookup in-flight abstract attributes 3547 /// through the Attributor \p A. 3548 /// 3549 /// If a use leads to a potential capture, \p CapturedInMemory is set and the 3550 /// search is stopped. If a use leads to a return instruction, 3551 /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed. 3552 /// If a use leads to a ptr2int which may capture the value, 3553 /// \p CapturedInInteger is set. If a use is found that is currently assumed 3554 /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies 3555 /// set. All values in \p PotentialCopies are later tracked as well. For every 3556 /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0, 3557 /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger 3558 /// conservatively set to true. 3559 AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA, 3560 const AAIsDead &IsDeadAA, AANoCapture::StateType &State, 3561 SmallVectorImpl<const Value *> &PotentialCopies, 3562 unsigned &RemainingUsesToExplore) 3563 : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State), 3564 PotentialCopies(PotentialCopies), 3565 RemainingUsesToExplore(RemainingUsesToExplore) {} 3566 3567 /// Determine if \p V maybe captured. *Also updates the state!* 3568 bool valueMayBeCaptured(const Value *V) { 3569 if (V->getType()->isPointerTy()) { 3570 PointerMayBeCaptured(V, this); 3571 } else { 3572 State.indicatePessimisticFixpoint(); 3573 } 3574 return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); 3575 } 3576 3577 /// See CaptureTracker::tooManyUses(). 3578 void tooManyUses() override { 3579 State.removeAssumedBits(AANoCapture::NO_CAPTURE); 3580 } 3581 3582 bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override { 3583 if (CaptureTracker::isDereferenceableOrNull(O, DL)) 3584 return true; 3585 const auto &DerefAA = 3586 A.getAAFor<AADereferenceable>(NoCaptureAA, IRPosition::value(*O)); 3587 return DerefAA.getAssumedDereferenceableBytes(); 3588 } 3589 3590 /// See CaptureTracker::captured(...). 3591 bool captured(const Use *U) override { 3592 Instruction *UInst = cast<Instruction>(U->getUser()); 3593 LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst 3594 << "\n"); 3595 3596 // Because we may reuse the tracker multiple times we keep track of the 3597 // number of explored uses ourselves as well. 3598 if (RemainingUsesToExplore-- == 0) { 3599 LLVM_DEBUG(dbgs() << " - too many uses to explore!\n"); 3600 return isCapturedIn(/* Memory */ true, /* Integer */ true, 3601 /* Return */ true); 3602 } 3603 3604 // Deal with ptr2int by following uses. 3605 if (isa<PtrToIntInst>(UInst)) { 3606 LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n"); 3607 return valueMayBeCaptured(UInst); 3608 } 3609 3610 // Explicitly catch return instructions. 3611 if (isa<ReturnInst>(UInst)) 3612 return isCapturedIn(/* Memory */ false, /* Integer */ false, 3613 /* Return */ true); 3614 3615 // For now we only use special logic for call sites. However, the tracker 3616 // itself knows about a lot of other non-capturing cases already. 3617 CallSite CS(UInst); 3618 if (!CS || !CS.isArgOperand(U)) 3619 return isCapturedIn(/* Memory */ true, /* Integer */ true, 3620 /* Return */ true); 3621 3622 unsigned ArgNo = CS.getArgumentNo(U); 3623 const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo); 3624 // If we have a abstract no-capture attribute for the argument we can use 3625 // it to justify a non-capture attribute here. This allows recursion! 3626 auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos); 3627 if (ArgNoCaptureAA.isAssumedNoCapture()) 3628 return isCapturedIn(/* Memory */ false, /* Integer */ false, 3629 /* Return */ false); 3630 if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { 3631 addPotentialCopy(CS); 3632 return isCapturedIn(/* Memory */ false, /* Integer */ false, 3633 /* Return */ false); 3634 } 3635 3636 // Lastly, we could not find a reason no-capture can be assumed so we don't. 3637 return isCapturedIn(/* Memory */ true, /* Integer */ true, 3638 /* Return */ true); 3639 } 3640 3641 /// Register \p CS as potential copy of the value we are checking. 3642 void addPotentialCopy(CallSite CS) { 3643 PotentialCopies.push_back(CS.getInstruction()); 3644 } 3645 3646 /// See CaptureTracker::shouldExplore(...). 3647 bool shouldExplore(const Use *U) override { 3648 // Check liveness. 3649 return !IsDeadAA.isAssumedDead(cast<Instruction>(U->getUser())); 3650 } 3651 3652 /// Update the state according to \p CapturedInMem, \p CapturedInInt, and 3653 /// \p CapturedInRet, then return the appropriate value for use in the 3654 /// CaptureTracker::captured() interface. 3655 bool isCapturedIn(bool CapturedInMem, bool CapturedInInt, 3656 bool CapturedInRet) { 3657 LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int " 3658 << CapturedInInt << "|Ret " << CapturedInRet << "]\n"); 3659 if (CapturedInMem) 3660 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM); 3661 if (CapturedInInt) 3662 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT); 3663 if (CapturedInRet) 3664 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET); 3665 return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); 3666 } 3667 3668 private: 3669 /// The attributor providing in-flight abstract attributes. 3670 Attributor &A; 3671 3672 /// The abstract attribute currently updated. 3673 AANoCapture &NoCaptureAA; 3674 3675 /// The abstract liveness state. 3676 const AAIsDead &IsDeadAA; 3677 3678 /// The state currently updated. 3679 AANoCapture::StateType &State; 3680 3681 /// Set of potential copies of the tracked value. 3682 SmallVectorImpl<const Value *> &PotentialCopies; 3683 3684 /// Global counter to limit the number of explored uses. 3685 unsigned &RemainingUsesToExplore; 3686 }; 3687 3688 ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) { 3689 const IRPosition &IRP = getIRPosition(); 3690 const Value *V = 3691 getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue(); 3692 if (!V) 3693 return indicatePessimisticFixpoint(); 3694 3695 const Function *F = 3696 getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope(); 3697 assert(F && "Expected a function!"); 3698 const IRPosition &FnPos = IRPosition::function(*F); 3699 const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos); 3700 3701 AANoCapture::StateType T; 3702 3703 // Readonly means we cannot capture through memory. 3704 const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); 3705 if (FnMemAA.isAssumedReadOnly()) { 3706 T.addKnownBits(NOT_CAPTURED_IN_MEM); 3707 if (FnMemAA.isKnownReadOnly()) 3708 addKnownBits(NOT_CAPTURED_IN_MEM); 3709 } 3710 3711 // Make sure all returned values are different than the underlying value. 3712 // TODO: we could do this in a more sophisticated way inside 3713 // AAReturnedValues, e.g., track all values that escape through returns 3714 // directly somehow. 3715 auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) { 3716 bool SeenConstant = false; 3717 for (auto &It : RVAA.returned_values()) { 3718 if (isa<Constant>(It.first)) { 3719 if (SeenConstant) 3720 return false; 3721 SeenConstant = true; 3722 } else if (!isa<Argument>(It.first) || 3723 It.first == getAssociatedArgument()) 3724 return false; 3725 } 3726 return true; 3727 }; 3728 3729 const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(*this, FnPos); 3730 if (NoUnwindAA.isAssumedNoUnwind()) { 3731 bool IsVoidTy = F->getReturnType()->isVoidTy(); 3732 const AAReturnedValues *RVAA = 3733 IsVoidTy ? nullptr : &A.getAAFor<AAReturnedValues>(*this, FnPos); 3734 if (IsVoidTy || CheckReturnedArgs(*RVAA)) { 3735 T.addKnownBits(NOT_CAPTURED_IN_RET); 3736 if (T.isKnown(NOT_CAPTURED_IN_MEM)) 3737 return ChangeStatus::UNCHANGED; 3738 if (NoUnwindAA.isKnownNoUnwind() && 3739 (IsVoidTy || RVAA->getState().isAtFixpoint())) { 3740 addKnownBits(NOT_CAPTURED_IN_RET); 3741 if (isKnown(NOT_CAPTURED_IN_MEM)) 3742 return indicateOptimisticFixpoint(); 3743 } 3744 } 3745 } 3746 3747 // Use the CaptureTracker interface and logic with the specialized tracker, 3748 // defined in AACaptureUseTracker, that can look at in-flight abstract 3749 // attributes and directly updates the assumed state. 3750 SmallVector<const Value *, 4> PotentialCopies; 3751 unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore; 3752 AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies, 3753 RemainingUsesToExplore); 3754 3755 // Check all potential copies of the associated value until we can assume 3756 // none will be captured or we have to assume at least one might be. 3757 unsigned Idx = 0; 3758 PotentialCopies.push_back(V); 3759 while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size()) 3760 Tracker.valueMayBeCaptured(PotentialCopies[Idx++]); 3761 3762 AANoCapture::StateType &S = getState(); 3763 auto Assumed = S.getAssumed(); 3764 S.intersectAssumedBits(T.getAssumed()); 3765 if (!isAssumedNoCaptureMaybeReturned()) 3766 return indicatePessimisticFixpoint(); 3767 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED 3768 : ChangeStatus::CHANGED; 3769 } 3770 3771 /// NoCapture attribute for function arguments. 3772 struct AANoCaptureArgument final : AANoCaptureImpl { 3773 AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} 3774 3775 /// See AbstractAttribute::trackStatistics() 3776 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) } 3777 }; 3778 3779 /// NoCapture attribute for call site arguments. 3780 struct AANoCaptureCallSiteArgument final : AANoCaptureImpl { 3781 AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} 3782 3783 /// See AbstractAttribute::initialize(...). 3784 void initialize(Attributor &A) override { 3785 if (Argument *Arg = getAssociatedArgument()) 3786 if (Arg->hasByValAttr()) 3787 indicateOptimisticFixpoint(); 3788 AANoCaptureImpl::initialize(A); 3789 } 3790 3791 /// See AbstractAttribute::updateImpl(...). 3792 ChangeStatus updateImpl(Attributor &A) override { 3793 // TODO: Once we have call site specific value information we can provide 3794 // call site specific liveness information and then it makes 3795 // sense to specialize attributes for call sites arguments instead of 3796 // redirecting requests to the callee argument. 3797 Argument *Arg = getAssociatedArgument(); 3798 if (!Arg) 3799 return indicatePessimisticFixpoint(); 3800 const IRPosition &ArgPos = IRPosition::argument(*Arg); 3801 auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos); 3802 return clampStateAndIndicateChange( 3803 getState(), 3804 static_cast<const AANoCapture::StateType &>(ArgAA.getState())); 3805 } 3806 3807 /// See AbstractAttribute::trackStatistics() 3808 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)}; 3809 }; 3810 3811 /// NoCapture attribute for floating values. 3812 struct AANoCaptureFloating final : AANoCaptureImpl { 3813 AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} 3814 3815 /// See AbstractAttribute::trackStatistics() 3816 void trackStatistics() const override { 3817 STATS_DECLTRACK_FLOATING_ATTR(nocapture) 3818 } 3819 }; 3820 3821 /// NoCapture attribute for function return value. 3822 struct AANoCaptureReturned final : AANoCaptureImpl { 3823 AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) { 3824 llvm_unreachable("NoCapture is not applicable to function returns!"); 3825 } 3826 3827 /// See AbstractAttribute::initialize(...). 3828 void initialize(Attributor &A) override { 3829 llvm_unreachable("NoCapture is not applicable to function returns!"); 3830 } 3831 3832 /// See AbstractAttribute::updateImpl(...). 3833 ChangeStatus updateImpl(Attributor &A) override { 3834 llvm_unreachable("NoCapture is not applicable to function returns!"); 3835 } 3836 3837 /// See AbstractAttribute::trackStatistics() 3838 void trackStatistics() const override {} 3839 }; 3840 3841 /// NoCapture attribute deduction for a call site return value. 3842 struct AANoCaptureCallSiteReturned final : AANoCaptureImpl { 3843 AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {} 3844 3845 /// See AbstractAttribute::trackStatistics() 3846 void trackStatistics() const override { 3847 STATS_DECLTRACK_CSRET_ATTR(nocapture) 3848 } 3849 }; 3850 3851 /// ------------------ Value Simplify Attribute ---------------------------- 3852 struct AAValueSimplifyImpl : AAValueSimplify { 3853 AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {} 3854 3855 /// See AbstractAttribute::getAsStr(). 3856 const std::string getAsStr() const override { 3857 return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple") 3858 : "not-simple"; 3859 } 3860 3861 /// See AbstractAttribute::trackStatistics() 3862 void trackStatistics() const override {} 3863 3864 /// See AAValueSimplify::getAssumedSimplifiedValue() 3865 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { 3866 if (!getAssumed()) 3867 return const_cast<Value *>(&getAssociatedValue()); 3868 return SimplifiedAssociatedValue; 3869 } 3870 void initialize(Attributor &A) override {} 3871 3872 /// Helper function for querying AAValueSimplify and updating candicate. 3873 /// \param QueryingValue Value trying to unify with SimplifiedValue 3874 /// \param AccumulatedSimplifiedValue Current simplification result. 3875 static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA, 3876 Value &QueryingValue, 3877 Optional<Value *> &AccumulatedSimplifiedValue) { 3878 // FIXME: Add a typecast support. 3879 3880 auto &ValueSimpifyAA = A.getAAFor<AAValueSimplify>( 3881 QueryingAA, IRPosition::value(QueryingValue)); 3882 3883 Optional<Value *> QueryingValueSimplified = 3884 ValueSimpifyAA.getAssumedSimplifiedValue(A); 3885 3886 if (!QueryingValueSimplified.hasValue()) 3887 return true; 3888 3889 if (!QueryingValueSimplified.getValue()) 3890 return false; 3891 3892 Value &QueryingValueSimplifiedUnwrapped = 3893 *QueryingValueSimplified.getValue(); 3894 3895 if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped)) 3896 return true; 3897 3898 if (AccumulatedSimplifiedValue.hasValue()) 3899 return AccumulatedSimplifiedValue == QueryingValueSimplified; 3900 3901 LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << QueryingValue 3902 << " is assumed to be " 3903 << QueryingValueSimplifiedUnwrapped << "\n"); 3904 3905 AccumulatedSimplifiedValue = QueryingValueSimplified; 3906 return true; 3907 } 3908 3909 /// See AbstractAttribute::manifest(...). 3910 ChangeStatus manifest(Attributor &A) override { 3911 ChangeStatus Changed = ChangeStatus::UNCHANGED; 3912 3913 if (!SimplifiedAssociatedValue.hasValue() || 3914 !SimplifiedAssociatedValue.getValue()) 3915 return Changed; 3916 3917 if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) { 3918 // We can replace the AssociatedValue with the constant. 3919 Value &V = getAssociatedValue(); 3920 if (!V.user_empty() && &V != C && V.getType() == C->getType()) { 3921 LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << V << " -> " << *C 3922 << "\n"); 3923 V.replaceAllUsesWith(C); 3924 Changed = ChangeStatus::CHANGED; 3925 } 3926 } 3927 3928 return Changed | AAValueSimplify::manifest(A); 3929 } 3930 3931 protected: 3932 // An assumed simplified value. Initially, it is set to Optional::None, which 3933 // means that the value is not clear under current assumption. If in the 3934 // pessimistic state, getAssumedSimplifiedValue doesn't return this value but 3935 // returns orignal associated value. 3936 Optional<Value *> SimplifiedAssociatedValue; 3937 }; 3938 3939 struct AAValueSimplifyArgument final : AAValueSimplifyImpl { 3940 AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} 3941 3942 void initialize(Attributor &A) override { 3943 AAValueSimplifyImpl::initialize(A); 3944 if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration()) 3945 indicatePessimisticFixpoint(); 3946 if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest}, 3947 /* IgnoreSubsumingPositions */ true)) 3948 indicatePessimisticFixpoint(); 3949 } 3950 3951 /// See AbstractAttribute::updateImpl(...). 3952 ChangeStatus updateImpl(Attributor &A) override { 3953 // Byval is only replacable if it is readonly otherwise we would write into 3954 // the replaced value and not the copy that byval creates implicitly. 3955 Argument *Arg = getAssociatedArgument(); 3956 if (Arg->hasByValAttr()) { 3957 const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition()); 3958 if (!MemAA.isAssumedReadOnly()) 3959 return indicatePessimisticFixpoint(); 3960 } 3961 3962 bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); 3963 3964 auto PredForCallSite = [&](AbstractCallSite ACS) { 3965 // Check if we have an associated argument or not (which can happen for 3966 // callback calls). 3967 Value *ArgOp = ACS.getCallArgOperand(getArgNo()); 3968 if (!ArgOp) 3969 return false; 3970 // We can only propagate thread independent values through callbacks. 3971 // This is different to direct/indirect call sites because for them we 3972 // know the thread executing the caller and callee is the same. For 3973 // callbacks this is not guaranteed, thus a thread dependent value could 3974 // be different for the caller and callee, making it invalid to propagate. 3975 if (ACS.isCallbackCall()) 3976 if (auto *C = dyn_cast<Constant>(ArgOp)) 3977 if (C->isThreadDependent()) 3978 return false; 3979 return checkAndUpdate(A, *this, *ArgOp, SimplifiedAssociatedValue); 3980 }; 3981 3982 if (!A.checkForAllCallSites(PredForCallSite, *this, true)) 3983 return indicatePessimisticFixpoint(); 3984 3985 // If a candicate was found in this update, return CHANGED. 3986 return HasValueBefore == SimplifiedAssociatedValue.hasValue() 3987 ? ChangeStatus::UNCHANGED 3988 : ChangeStatus ::CHANGED; 3989 } 3990 3991 /// See AbstractAttribute::trackStatistics() 3992 void trackStatistics() const override { 3993 STATS_DECLTRACK_ARG_ATTR(value_simplify) 3994 } 3995 }; 3996 3997 struct AAValueSimplifyReturned : AAValueSimplifyImpl { 3998 AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} 3999 4000 /// See AbstractAttribute::updateImpl(...). 4001 ChangeStatus updateImpl(Attributor &A) override { 4002 bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); 4003 4004 auto PredForReturned = [&](Value &V) { 4005 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); 4006 }; 4007 4008 if (!A.checkForAllReturnedValues(PredForReturned, *this)) 4009 return indicatePessimisticFixpoint(); 4010 4011 // If a candicate was found in this update, return CHANGED. 4012 return HasValueBefore == SimplifiedAssociatedValue.hasValue() 4013 ? ChangeStatus::UNCHANGED 4014 : ChangeStatus ::CHANGED; 4015 } 4016 /// See AbstractAttribute::trackStatistics() 4017 void trackStatistics() const override { 4018 STATS_DECLTRACK_FNRET_ATTR(value_simplify) 4019 } 4020 }; 4021 4022 struct AAValueSimplifyFloating : AAValueSimplifyImpl { 4023 AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} 4024 4025 /// See AbstractAttribute::initialize(...). 4026 void initialize(Attributor &A) override { 4027 Value &V = getAnchorValue(); 4028 4029 // TODO: add other stuffs 4030 if (isa<Constant>(V) || isa<UndefValue>(V)) 4031 indicatePessimisticFixpoint(); 4032 } 4033 4034 /// See AbstractAttribute::updateImpl(...). 4035 ChangeStatus updateImpl(Attributor &A) override { 4036 bool HasValueBefore = SimplifiedAssociatedValue.hasValue(); 4037 4038 auto VisitValueCB = [&](Value &V, BooleanState, bool Stripped) -> bool { 4039 auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V)); 4040 if (!Stripped && this == &AA) { 4041 // TODO: Look the instruction and check recursively. 4042 LLVM_DEBUG( 4043 dbgs() << "[Attributor][ValueSimplify] Can't be stripped more : " 4044 << V << "\n"); 4045 indicatePessimisticFixpoint(); 4046 return false; 4047 } 4048 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue); 4049 }; 4050 4051 if (!genericValueTraversal<AAValueSimplify, BooleanState>( 4052 A, getIRPosition(), *this, static_cast<BooleanState &>(*this), 4053 VisitValueCB)) 4054 return indicatePessimisticFixpoint(); 4055 4056 // If a candicate was found in this update, return CHANGED. 4057 4058 return HasValueBefore == SimplifiedAssociatedValue.hasValue() 4059 ? ChangeStatus::UNCHANGED 4060 : ChangeStatus ::CHANGED; 4061 } 4062 4063 /// See AbstractAttribute::trackStatistics() 4064 void trackStatistics() const override { 4065 STATS_DECLTRACK_FLOATING_ATTR(value_simplify) 4066 } 4067 }; 4068 4069 struct AAValueSimplifyFunction : AAValueSimplifyImpl { 4070 AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {} 4071 4072 /// See AbstractAttribute::initialize(...). 4073 void initialize(Attributor &A) override { 4074 SimplifiedAssociatedValue = &getAnchorValue(); 4075 indicateOptimisticFixpoint(); 4076 } 4077 /// See AbstractAttribute::initialize(...). 4078 ChangeStatus updateImpl(Attributor &A) override { 4079 llvm_unreachable( 4080 "AAValueSimplify(Function|CallSite)::updateImpl will not be called"); 4081 } 4082 /// See AbstractAttribute::trackStatistics() 4083 void trackStatistics() const override { 4084 STATS_DECLTRACK_FN_ATTR(value_simplify) 4085 } 4086 }; 4087 4088 struct AAValueSimplifyCallSite : AAValueSimplifyFunction { 4089 AAValueSimplifyCallSite(const IRPosition &IRP) 4090 : AAValueSimplifyFunction(IRP) {} 4091 /// See AbstractAttribute::trackStatistics() 4092 void trackStatistics() const override { 4093 STATS_DECLTRACK_CS_ATTR(value_simplify) 4094 } 4095 }; 4096 4097 struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned { 4098 AAValueSimplifyCallSiteReturned(const IRPosition &IRP) 4099 : AAValueSimplifyReturned(IRP) {} 4100 4101 void trackStatistics() const override { 4102 STATS_DECLTRACK_CSRET_ATTR(value_simplify) 4103 } 4104 }; 4105 struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating { 4106 AAValueSimplifyCallSiteArgument(const IRPosition &IRP) 4107 : AAValueSimplifyFloating(IRP) {} 4108 4109 void trackStatistics() const override { 4110 STATS_DECLTRACK_CSARG_ATTR(value_simplify) 4111 } 4112 }; 4113 4114 /// ----------------------- Heap-To-Stack Conversion --------------------------- 4115 struct AAHeapToStackImpl : public AAHeapToStack { 4116 AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {} 4117 4118 const std::string getAsStr() const override { 4119 return "[H2S] Mallocs: " + std::to_string(MallocCalls.size()); 4120 } 4121 4122 ChangeStatus manifest(Attributor &A) override { 4123 assert(getState().isValidState() && 4124 "Attempted to manifest an invalid state!"); 4125 4126 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 4127 Function *F = getAssociatedFunction(); 4128 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); 4129 4130 for (Instruction *MallocCall : MallocCalls) { 4131 // This malloc cannot be replaced. 4132 if (BadMallocCalls.count(MallocCall)) 4133 continue; 4134 4135 for (Instruction *FreeCall : FreesForMalloc[MallocCall]) { 4136 LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n"); 4137 A.deleteAfterManifest(*FreeCall); 4138 HasChanged = ChangeStatus::CHANGED; 4139 } 4140 4141 LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall 4142 << "\n"); 4143 4144 Constant *Size; 4145 if (isCallocLikeFn(MallocCall, TLI)) { 4146 auto *Num = cast<ConstantInt>(MallocCall->getOperand(0)); 4147 auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1)); 4148 APInt TotalSize = SizeT->getValue() * Num->getValue(); 4149 Size = 4150 ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize); 4151 } else { 4152 Size = cast<ConstantInt>(MallocCall->getOperand(0)); 4153 } 4154 4155 unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace(); 4156 Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS, 4157 Size, "", MallocCall->getNextNode()); 4158 4159 if (AI->getType() != MallocCall->getType()) 4160 AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc", 4161 AI->getNextNode()); 4162 4163 MallocCall->replaceAllUsesWith(AI); 4164 4165 if (auto *II = dyn_cast<InvokeInst>(MallocCall)) { 4166 auto *NBB = II->getNormalDest(); 4167 BranchInst::Create(NBB, MallocCall->getParent()); 4168 A.deleteAfterManifest(*MallocCall); 4169 } else { 4170 A.deleteAfterManifest(*MallocCall); 4171 } 4172 4173 if (isCallocLikeFn(MallocCall, TLI)) { 4174 auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc", 4175 AI->getNextNode()); 4176 Value *Ops[] = { 4177 BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size, 4178 ConstantInt::get(Type::getInt1Ty(F->getContext()), false)}; 4179 4180 Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()}; 4181 Module *M = F->getParent(); 4182 Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys); 4183 CallInst::Create(Fn, Ops, "", BI->getNextNode()); 4184 } 4185 HasChanged = ChangeStatus::CHANGED; 4186 } 4187 4188 return HasChanged; 4189 } 4190 4191 /// Collection of all malloc calls in a function. 4192 SmallSetVector<Instruction *, 4> MallocCalls; 4193 4194 /// Collection of malloc calls that cannot be converted. 4195 DenseSet<const Instruction *> BadMallocCalls; 4196 4197 /// A map for each malloc call to the set of associated free calls. 4198 DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc; 4199 4200 ChangeStatus updateImpl(Attributor &A) override; 4201 }; 4202 4203 ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) { 4204 const Function *F = getAssociatedFunction(); 4205 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); 4206 4207 MustBeExecutedContextExplorer &Explorer = 4208 A.getInfoCache().getMustBeExecutedContextExplorer(); 4209 4210 auto FreeCheck = [&](Instruction &I) { 4211 const auto &Frees = FreesForMalloc.lookup(&I); 4212 if (Frees.size() != 1) 4213 return false; 4214 Instruction *UniqueFree = *Frees.begin(); 4215 return Explorer.findInContextOf(UniqueFree, I.getNextNode()); 4216 }; 4217 4218 auto UsesCheck = [&](Instruction &I) { 4219 bool ValidUsesOnly = true; 4220 bool MustUse = true; 4221 auto Pred = [&](const Use &U, bool &Follow) -> bool { 4222 Instruction *UserI = cast<Instruction>(U.getUser()); 4223 if (isa<LoadInst>(UserI)) 4224 return true; 4225 if (auto *SI = dyn_cast<StoreInst>(UserI)) { 4226 if (SI->getValueOperand() == U.get()) { 4227 LLVM_DEBUG(dbgs() 4228 << "[H2S] escaping store to memory: " << *UserI << "\n"); 4229 ValidUsesOnly = false; 4230 } else { 4231 // A store into the malloc'ed memory is fine. 4232 } 4233 return true; 4234 } 4235 if (auto *CB = dyn_cast<CallBase>(UserI)) { 4236 if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd()) 4237 return true; 4238 // Record malloc. 4239 if (isFreeCall(UserI, TLI)) { 4240 if (MustUse) { 4241 FreesForMalloc[&I].insert(UserI); 4242 } else { 4243 LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: " 4244 << *UserI << "\n"); 4245 ValidUsesOnly = false; 4246 } 4247 return true; 4248 } 4249 4250 unsigned ArgNo = CB->getArgOperandNo(&U); 4251 4252 const auto &NoCaptureAA = A.getAAFor<AANoCapture>( 4253 *this, IRPosition::callsite_argument(*CB, ArgNo)); 4254 4255 // If a callsite argument use is nofree, we are fine. 4256 const auto &ArgNoFreeAA = A.getAAFor<AANoFree>( 4257 *this, IRPosition::callsite_argument(*CB, ArgNo)); 4258 4259 if (!NoCaptureAA.isAssumedNoCapture() || 4260 !ArgNoFreeAA.isAssumedNoFree()) { 4261 LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n"); 4262 ValidUsesOnly = false; 4263 } 4264 return true; 4265 } 4266 4267 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || 4268 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { 4269 MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI)); 4270 Follow = true; 4271 return true; 4272 } 4273 // Unknown user for which we can not track uses further (in a way that 4274 // makes sense). 4275 LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n"); 4276 ValidUsesOnly = false; 4277 return true; 4278 }; 4279 A.checkForAllUses(Pred, *this, I); 4280 return ValidUsesOnly; 4281 }; 4282 4283 auto MallocCallocCheck = [&](Instruction &I) { 4284 if (BadMallocCalls.count(&I)) 4285 return true; 4286 4287 bool IsMalloc = isMallocLikeFn(&I, TLI); 4288 bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI); 4289 if (!IsMalloc && !IsCalloc) { 4290 BadMallocCalls.insert(&I); 4291 return true; 4292 } 4293 4294 if (IsMalloc) { 4295 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0))) 4296 if (Size->getValue().sle(MaxHeapToStackSize)) 4297 if (UsesCheck(I) || FreeCheck(I)) { 4298 MallocCalls.insert(&I); 4299 return true; 4300 } 4301 } else if (IsCalloc) { 4302 bool Overflow = false; 4303 if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0))) 4304 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1))) 4305 if ((Size->getValue().umul_ov(Num->getValue(), Overflow)) 4306 .sle(MaxHeapToStackSize)) 4307 if (!Overflow && (UsesCheck(I) || FreeCheck(I))) { 4308 MallocCalls.insert(&I); 4309 return true; 4310 } 4311 } 4312 4313 BadMallocCalls.insert(&I); 4314 return true; 4315 }; 4316 4317 size_t NumBadMallocs = BadMallocCalls.size(); 4318 4319 A.checkForAllCallLikeInstructions(MallocCallocCheck, *this); 4320 4321 if (NumBadMallocs != BadMallocCalls.size()) 4322 return ChangeStatus::CHANGED; 4323 4324 return ChangeStatus::UNCHANGED; 4325 } 4326 4327 struct AAHeapToStackFunction final : public AAHeapToStackImpl { 4328 AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {} 4329 4330 /// See AbstractAttribute::trackStatistics() 4331 void trackStatistics() const override { 4332 STATS_DECL(MallocCalls, Function, 4333 "Number of malloc calls converted to allocas"); 4334 for (auto *C : MallocCalls) 4335 if (!BadMallocCalls.count(C)) 4336 ++BUILD_STAT_NAME(MallocCalls, Function); 4337 } 4338 }; 4339 4340 /// -------------------- Memory Behavior Attributes ---------------------------- 4341 /// Includes read-none, read-only, and write-only. 4342 /// ---------------------------------------------------------------------------- 4343 struct AAMemoryBehaviorImpl : public AAMemoryBehavior { 4344 AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {} 4345 4346 /// See AbstractAttribute::initialize(...). 4347 void initialize(Attributor &A) override { 4348 intersectAssumedBits(BEST_STATE); 4349 getKnownStateFromValue(getIRPosition(), getState()); 4350 IRAttribute::initialize(A); 4351 } 4352 4353 /// Return the memory behavior information encoded in the IR for \p IRP. 4354 static void getKnownStateFromValue(const IRPosition &IRP, 4355 BitIntegerState &State, 4356 bool IgnoreSubsumingPositions = false) { 4357 SmallVector<Attribute, 2> Attrs; 4358 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); 4359 for (const Attribute &Attr : Attrs) { 4360 switch (Attr.getKindAsEnum()) { 4361 case Attribute::ReadNone: 4362 State.addKnownBits(NO_ACCESSES); 4363 break; 4364 case Attribute::ReadOnly: 4365 State.addKnownBits(NO_WRITES); 4366 break; 4367 case Attribute::WriteOnly: 4368 State.addKnownBits(NO_READS); 4369 break; 4370 default: 4371 llvm_unreachable("Unexpcted attribute!"); 4372 } 4373 } 4374 4375 if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) { 4376 if (!I->mayReadFromMemory()) 4377 State.addKnownBits(NO_READS); 4378 if (!I->mayWriteToMemory()) 4379 State.addKnownBits(NO_WRITES); 4380 } 4381 } 4382 4383 /// See AbstractAttribute::getDeducedAttributes(...). 4384 void getDeducedAttributes(LLVMContext &Ctx, 4385 SmallVectorImpl<Attribute> &Attrs) const override { 4386 assert(Attrs.size() == 0); 4387 if (isAssumedReadNone()) 4388 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); 4389 else if (isAssumedReadOnly()) 4390 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly)); 4391 else if (isAssumedWriteOnly()) 4392 Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly)); 4393 assert(Attrs.size() <= 1); 4394 } 4395 4396 /// See AbstractAttribute::manifest(...). 4397 ChangeStatus manifest(Attributor &A) override { 4398 const IRPosition &IRP = getIRPosition(); 4399 4400 // Check if we would improve the existing attributes first. 4401 SmallVector<Attribute, 4> DeducedAttrs; 4402 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); 4403 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { 4404 return IRP.hasAttr(Attr.getKindAsEnum(), 4405 /* IgnoreSubsumingPositions */ true); 4406 })) 4407 return ChangeStatus::UNCHANGED; 4408 4409 // Clear existing attributes. 4410 IRP.removeAttrs(AttrKinds); 4411 4412 // Use the generic manifest method. 4413 return IRAttribute::manifest(A); 4414 } 4415 4416 /// See AbstractState::getAsStr(). 4417 const std::string getAsStr() const override { 4418 if (isAssumedReadNone()) 4419 return "readnone"; 4420 if (isAssumedReadOnly()) 4421 return "readonly"; 4422 if (isAssumedWriteOnly()) 4423 return "writeonly"; 4424 return "may-read/write"; 4425 } 4426 4427 /// The set of IR attributes AAMemoryBehavior deals with. 4428 static const Attribute::AttrKind AttrKinds[3]; 4429 }; 4430 4431 const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = { 4432 Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly}; 4433 4434 /// Memory behavior attribute for a floating value. 4435 struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl { 4436 AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} 4437 4438 /// See AbstractAttribute::initialize(...). 4439 void initialize(Attributor &A) override { 4440 AAMemoryBehaviorImpl::initialize(A); 4441 // Initialize the use vector with all direct uses of the associated value. 4442 for (const Use &U : getAssociatedValue().uses()) 4443 Uses.insert(&U); 4444 } 4445 4446 /// See AbstractAttribute::updateImpl(...). 4447 ChangeStatus updateImpl(Attributor &A) override; 4448 4449 /// See AbstractAttribute::trackStatistics() 4450 void trackStatistics() const override { 4451 if (isAssumedReadNone()) 4452 STATS_DECLTRACK_FLOATING_ATTR(readnone) 4453 else if (isAssumedReadOnly()) 4454 STATS_DECLTRACK_FLOATING_ATTR(readonly) 4455 else if (isAssumedWriteOnly()) 4456 STATS_DECLTRACK_FLOATING_ATTR(writeonly) 4457 } 4458 4459 private: 4460 /// Return true if users of \p UserI might access the underlying 4461 /// variable/location described by \p U and should therefore be analyzed. 4462 bool followUsersOfUseIn(Attributor &A, const Use *U, 4463 const Instruction *UserI); 4464 4465 /// Update the state according to the effect of use \p U in \p UserI. 4466 void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI); 4467 4468 protected: 4469 /// Container for (transitive) uses of the associated argument. 4470 SetVector<const Use *> Uses; 4471 }; 4472 4473 /// Memory behavior attribute for function argument. 4474 struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating { 4475 AAMemoryBehaviorArgument(const IRPosition &IRP) 4476 : AAMemoryBehaviorFloating(IRP) {} 4477 4478 /// See AbstractAttribute::initialize(...). 4479 void initialize(Attributor &A) override { 4480 intersectAssumedBits(BEST_STATE); 4481 const IRPosition &IRP = getIRPosition(); 4482 // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we 4483 // can query it when we use has/getAttr. That would allow us to reuse the 4484 // initialize of the base class here. 4485 bool HasByVal = 4486 IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true); 4487 getKnownStateFromValue(IRP, getState(), 4488 /* IgnoreSubsumingPositions */ HasByVal); 4489 4490 // Initialize the use vector with all direct uses of the associated value. 4491 Argument *Arg = getAssociatedArgument(); 4492 if (!Arg || !Arg->getParent()->hasExactDefinition()) { 4493 indicatePessimisticFixpoint(); 4494 } else { 4495 // Initialize the use vector with all direct uses of the associated value. 4496 for (const Use &U : Arg->uses()) 4497 Uses.insert(&U); 4498 } 4499 } 4500 4501 ChangeStatus manifest(Attributor &A) override { 4502 // TODO: From readattrs.ll: "inalloca parameters are always 4503 // considered written" 4504 if (hasAttr({Attribute::InAlloca})) { 4505 removeKnownBits(NO_WRITES); 4506 removeAssumedBits(NO_WRITES); 4507 } 4508 return AAMemoryBehaviorFloating::manifest(A); 4509 } 4510 4511 /// See AbstractAttribute::trackStatistics() 4512 void trackStatistics() const override { 4513 if (isAssumedReadNone()) 4514 STATS_DECLTRACK_ARG_ATTR(readnone) 4515 else if (isAssumedReadOnly()) 4516 STATS_DECLTRACK_ARG_ATTR(readonly) 4517 else if (isAssumedWriteOnly()) 4518 STATS_DECLTRACK_ARG_ATTR(writeonly) 4519 } 4520 }; 4521 4522 struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument { 4523 AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP) 4524 : AAMemoryBehaviorArgument(IRP) {} 4525 4526 /// See AbstractAttribute::initialize(...). 4527 void initialize(Attributor &A) override { 4528 if (Argument *Arg = getAssociatedArgument()) { 4529 if (Arg->hasByValAttr()) { 4530 addKnownBits(NO_WRITES); 4531 removeKnownBits(NO_READS); 4532 removeAssumedBits(NO_READS); 4533 } 4534 } else { 4535 } 4536 AAMemoryBehaviorArgument::initialize(A); 4537 } 4538 4539 /// See AbstractAttribute::updateImpl(...). 4540 ChangeStatus updateImpl(Attributor &A) override { 4541 // TODO: Once we have call site specific value information we can provide 4542 // call site specific liveness liveness information and then it makes 4543 // sense to specialize attributes for call sites arguments instead of 4544 // redirecting requests to the callee argument. 4545 Argument *Arg = getAssociatedArgument(); 4546 const IRPosition &ArgPos = IRPosition::argument(*Arg); 4547 auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); 4548 return clampStateAndIndicateChange( 4549 getState(), 4550 static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState())); 4551 } 4552 4553 /// See AbstractAttribute::trackStatistics() 4554 void trackStatistics() const override { 4555 if (isAssumedReadNone()) 4556 STATS_DECLTRACK_CSARG_ATTR(readnone) 4557 else if (isAssumedReadOnly()) 4558 STATS_DECLTRACK_CSARG_ATTR(readonly) 4559 else if (isAssumedWriteOnly()) 4560 STATS_DECLTRACK_CSARG_ATTR(writeonly) 4561 } 4562 }; 4563 4564 /// Memory behavior attribute for a call site return position. 4565 struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating { 4566 AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP) 4567 : AAMemoryBehaviorFloating(IRP) {} 4568 4569 /// See AbstractAttribute::manifest(...). 4570 ChangeStatus manifest(Attributor &A) override { 4571 // We do not annotate returned values. 4572 return ChangeStatus::UNCHANGED; 4573 } 4574 4575 /// See AbstractAttribute::trackStatistics() 4576 void trackStatistics() const override {} 4577 }; 4578 4579 /// An AA to represent the memory behavior function attributes. 4580 struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl { 4581 AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} 4582 4583 /// See AbstractAttribute::updateImpl(Attributor &A). 4584 virtual ChangeStatus updateImpl(Attributor &A) override; 4585 4586 /// See AbstractAttribute::manifest(...). 4587 ChangeStatus manifest(Attributor &A) override { 4588 Function &F = cast<Function>(getAnchorValue()); 4589 if (isAssumedReadNone()) { 4590 F.removeFnAttr(Attribute::ArgMemOnly); 4591 F.removeFnAttr(Attribute::InaccessibleMemOnly); 4592 F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly); 4593 } 4594 return AAMemoryBehaviorImpl::manifest(A); 4595 } 4596 4597 /// See AbstractAttribute::trackStatistics() 4598 void trackStatistics() const override { 4599 if (isAssumedReadNone()) 4600 STATS_DECLTRACK_FN_ATTR(readnone) 4601 else if (isAssumedReadOnly()) 4602 STATS_DECLTRACK_FN_ATTR(readonly) 4603 else if (isAssumedWriteOnly()) 4604 STATS_DECLTRACK_FN_ATTR(writeonly) 4605 } 4606 }; 4607 4608 /// AAMemoryBehavior attribute for call sites. 4609 struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl { 4610 AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {} 4611 4612 /// See AbstractAttribute::initialize(...). 4613 void initialize(Attributor &A) override { 4614 AAMemoryBehaviorImpl::initialize(A); 4615 Function *F = getAssociatedFunction(); 4616 if (!F || !F->hasExactDefinition()) 4617 indicatePessimisticFixpoint(); 4618 } 4619 4620 /// See AbstractAttribute::updateImpl(...). 4621 ChangeStatus updateImpl(Attributor &A) override { 4622 // TODO: Once we have call site specific value information we can provide 4623 // call site specific liveness liveness information and then it makes 4624 // sense to specialize attributes for call sites arguments instead of 4625 // redirecting requests to the callee argument. 4626 Function *F = getAssociatedFunction(); 4627 const IRPosition &FnPos = IRPosition::function(*F); 4628 auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); 4629 return clampStateAndIndicateChange( 4630 getState(), 4631 static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState())); 4632 } 4633 4634 /// See AbstractAttribute::trackStatistics() 4635 void trackStatistics() const override { 4636 if (isAssumedReadNone()) 4637 STATS_DECLTRACK_CS_ATTR(readnone) 4638 else if (isAssumedReadOnly()) 4639 STATS_DECLTRACK_CS_ATTR(readonly) 4640 else if (isAssumedWriteOnly()) 4641 STATS_DECLTRACK_CS_ATTR(writeonly) 4642 } 4643 }; 4644 } // namespace 4645 4646 ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) { 4647 4648 // The current assumed state used to determine a change. 4649 auto AssumedState = getAssumed(); 4650 4651 auto CheckRWInst = [&](Instruction &I) { 4652 // If the instruction has an own memory behavior state, use it to restrict 4653 // the local state. No further analysis is required as the other memory 4654 // state is as optimistic as it gets. 4655 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { 4656 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( 4657 *this, IRPosition::callsite_function(ICS)); 4658 intersectAssumedBits(MemBehaviorAA.getAssumed()); 4659 return !isAtFixpoint(); 4660 } 4661 4662 // Remove access kind modifiers if necessary. 4663 if (I.mayReadFromMemory()) 4664 removeAssumedBits(NO_READS); 4665 if (I.mayWriteToMemory()) 4666 removeAssumedBits(NO_WRITES); 4667 return !isAtFixpoint(); 4668 }; 4669 4670 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this)) 4671 return indicatePessimisticFixpoint(); 4672 4673 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED 4674 : ChangeStatus::UNCHANGED; 4675 } 4676 4677 ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) { 4678 4679 const IRPosition &IRP = getIRPosition(); 4680 const IRPosition &FnPos = IRPosition::function_scope(IRP); 4681 AAMemoryBehavior::StateType &S = getState(); 4682 4683 // First, check the function scope. We take the known information and we avoid 4684 // work if the assumed information implies the current assumed information for 4685 // this attribute. This is a valid for all but byval arguments. 4686 Argument *Arg = IRP.getAssociatedArgument(); 4687 AAMemoryBehavior::base_t FnMemAssumedState = 4688 AAMemoryBehavior::StateType::getWorstState(); 4689 if (!Arg || !Arg->hasByValAttr()) { 4690 const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos); 4691 FnMemAssumedState = FnMemAA.getAssumed(); 4692 S.addKnownBits(FnMemAA.getKnown()); 4693 if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed()) 4694 return ChangeStatus::UNCHANGED; 4695 } 4696 4697 // Make sure the value is not captured (except through "return"), if 4698 // it is, any information derived would be irrelevant anyway as we cannot 4699 // check the potential aliases introduced by the capture. However, no need 4700 // to fall back to anythign less optimistic than the function state. 4701 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP); 4702 if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { 4703 S.intersectAssumedBits(FnMemAssumedState); 4704 return ChangeStatus::CHANGED; 4705 } 4706 4707 // The current assumed state used to determine a change. 4708 auto AssumedState = S.getAssumed(); 4709 4710 // Liveness information to exclude dead users. 4711 // TODO: Take the FnPos once we have call site specific liveness information. 4712 const auto &LivenessAA = A.getAAFor<AAIsDead>( 4713 *this, IRPosition::function(*IRP.getAssociatedFunction())); 4714 4715 // Visit and expand uses until all are analyzed or a fixpoint is reached. 4716 for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) { 4717 const Use *U = Uses[i]; 4718 Instruction *UserI = cast<Instruction>(U->getUser()); 4719 LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI 4720 << " [Dead: " << (LivenessAA.isAssumedDead(UserI)) 4721 << "]\n"); 4722 if (LivenessAA.isAssumedDead(UserI)) 4723 continue; 4724 4725 // Check if the users of UserI should also be visited. 4726 if (followUsersOfUseIn(A, U, UserI)) 4727 for (const Use &UserIUse : UserI->uses()) 4728 Uses.insert(&UserIUse); 4729 4730 // If UserI might touch memory we analyze the use in detail. 4731 if (UserI->mayReadOrWriteMemory()) 4732 analyzeUseIn(A, U, UserI); 4733 } 4734 4735 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED 4736 : ChangeStatus::UNCHANGED; 4737 } 4738 4739 bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U, 4740 const Instruction *UserI) { 4741 // The loaded value is unrelated to the pointer argument, no need to 4742 // follow the users of the load. 4743 if (isa<LoadInst>(UserI)) 4744 return false; 4745 4746 // By default we follow all uses assuming UserI might leak information on U, 4747 // we have special handling for call sites operands though. 4748 ImmutableCallSite ICS(UserI); 4749 if (!ICS || !ICS.isArgOperand(U)) 4750 return true; 4751 4752 // If the use is a call argument known not to be captured, the users of 4753 // the call do not need to be visited because they have to be unrelated to 4754 // the input. Note that this check is not trivial even though we disallow 4755 // general capturing of the underlying argument. The reason is that the 4756 // call might the argument "through return", which we allow and for which we 4757 // need to check call users. 4758 unsigned ArgNo = ICS.getArgumentNo(U); 4759 const auto &ArgNoCaptureAA = 4760 A.getAAFor<AANoCapture>(*this, IRPosition::callsite_argument(ICS, ArgNo)); 4761 return !ArgNoCaptureAA.isAssumedNoCapture(); 4762 } 4763 4764 void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U, 4765 const Instruction *UserI) { 4766 assert(UserI->mayReadOrWriteMemory()); 4767 4768 switch (UserI->getOpcode()) { 4769 default: 4770 // TODO: Handle all atomics and other side-effect operations we know of. 4771 break; 4772 case Instruction::Load: 4773 // Loads cause the NO_READS property to disappear. 4774 removeAssumedBits(NO_READS); 4775 return; 4776 4777 case Instruction::Store: 4778 // Stores cause the NO_WRITES property to disappear if the use is the 4779 // pointer operand. Note that we do assume that capturing was taken care of 4780 // somewhere else. 4781 if (cast<StoreInst>(UserI)->getPointerOperand() == U->get()) 4782 removeAssumedBits(NO_WRITES); 4783 return; 4784 4785 case Instruction::Call: 4786 case Instruction::CallBr: 4787 case Instruction::Invoke: { 4788 // For call sites we look at the argument memory behavior attribute (this 4789 // could be recursive!) in order to restrict our own state. 4790 ImmutableCallSite ICS(UserI); 4791 4792 // Give up on operand bundles. 4793 if (ICS.isBundleOperand(U)) { 4794 indicatePessimisticFixpoint(); 4795 return; 4796 } 4797 4798 // Calling a function does read the function pointer, maybe write it if the 4799 // function is self-modifying. 4800 if (ICS.isCallee(U)) { 4801 removeAssumedBits(NO_READS); 4802 break; 4803 } 4804 4805 // Adjust the possible access behavior based on the information on the 4806 // argument. 4807 unsigned ArgNo = ICS.getArgumentNo(U); 4808 const IRPosition &ArgPos = IRPosition::callsite_argument(ICS, ArgNo); 4809 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos); 4810 // "assumed" has at most the same bits as the MemBehaviorAA assumed 4811 // and at least "known". 4812 intersectAssumedBits(MemBehaviorAA.getAssumed()); 4813 return; 4814 } 4815 }; 4816 4817 // Generally, look at the "may-properties" and adjust the assumed state if we 4818 // did not trigger special handling before. 4819 if (UserI->mayReadFromMemory()) 4820 removeAssumedBits(NO_READS); 4821 if (UserI->mayWriteToMemory()) 4822 removeAssumedBits(NO_WRITES); 4823 } 4824 4825 /// ---------------------------------------------------------------------------- 4826 /// Attributor 4827 /// ---------------------------------------------------------------------------- 4828 4829 bool Attributor::isAssumedDead(const AbstractAttribute &AA, 4830 const AAIsDead *LivenessAA) { 4831 const Instruction *CtxI = AA.getIRPosition().getCtxI(); 4832 if (!CtxI) 4833 return false; 4834 4835 // TODO: Find a good way to utilize fine and coarse grained liveness 4836 // information. 4837 if (!LivenessAA) 4838 LivenessAA = 4839 &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()), 4840 /* TrackDependence */ false); 4841 4842 // Don't check liveness for AAIsDead. 4843 if (&AA == LivenessAA) 4844 return false; 4845 4846 if (!LivenessAA->isAssumedDead(CtxI)) 4847 return false; 4848 4849 // We actually used liveness information so we have to record a dependence. 4850 recordDependence(*LivenessAA, AA, DepClassTy::OPTIONAL); 4851 4852 return true; 4853 } 4854 4855 bool Attributor::checkForAllUses( 4856 const function_ref<bool(const Use &, bool &)> &Pred, 4857 const AbstractAttribute &QueryingAA, const Value &V) { 4858 const IRPosition &IRP = QueryingAA.getIRPosition(); 4859 SmallVector<const Use *, 16> Worklist; 4860 SmallPtrSet<const Use *, 16> Visited; 4861 4862 for (const Use &U : V.uses()) 4863 Worklist.push_back(&U); 4864 4865 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size() 4866 << " initial uses to check\n"); 4867 4868 if (Worklist.empty()) 4869 return true; 4870 4871 bool AnyDead = false; 4872 const Function *ScopeFn = IRP.getAnchorScope(); 4873 const auto *LivenessAA = 4874 ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn), 4875 /* TrackDependence */ false) 4876 : nullptr; 4877 4878 while (!Worklist.empty()) { 4879 const Use *U = Worklist.pop_back_val(); 4880 if (!Visited.insert(U).second) 4881 continue; 4882 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << "\n"); 4883 if (Instruction *UserI = dyn_cast<Instruction>(U->getUser())) 4884 if (LivenessAA && LivenessAA->isAssumedDead(UserI)) { 4885 LLVM_DEBUG(dbgs() << "[Attributor] Dead user: " << *UserI << ": " 4886 << static_cast<const AbstractAttribute &>(*LivenessAA) 4887 << "\n"); 4888 AnyDead = true; 4889 continue; 4890 } 4891 4892 bool Follow = false; 4893 if (!Pred(*U, Follow)) 4894 return false; 4895 if (!Follow) 4896 continue; 4897 for (const Use &UU : U->getUser()->uses()) 4898 Worklist.push_back(&UU); 4899 } 4900 4901 if (AnyDead) 4902 recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL); 4903 4904 return true; 4905 } 4906 4907 bool Attributor::checkForAllCallSites( 4908 const function_ref<bool(AbstractCallSite)> &Pred, 4909 const AbstractAttribute &QueryingAA, bool RequireAllCallSites) { 4910 // We can try to determine information from 4911 // the call sites. However, this is only possible all call sites are known, 4912 // hence the function has internal linkage. 4913 const IRPosition &IRP = QueryingAA.getIRPosition(); 4914 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 4915 if (!AssociatedFunction) { 4916 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP 4917 << "\n"); 4918 return false; 4919 } 4920 4921 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites, 4922 &QueryingAA); 4923 } 4924 4925 bool Attributor::checkForAllCallSites( 4926 const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn, 4927 bool RequireAllCallSites, const AbstractAttribute *QueryingAA) { 4928 if (RequireAllCallSites && !Fn.hasLocalLinkage()) { 4929 LLVM_DEBUG( 4930 dbgs() 4931 << "[Attributor] Function " << Fn.getName() 4932 << " has no internal linkage, hence not all call sites are known\n"); 4933 return false; 4934 } 4935 4936 for (const Use &U : Fn.uses()) { 4937 AbstractCallSite ACS(&U); 4938 if (!ACS) { 4939 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName() 4940 << " has non call site use " << *U.get() << " in " 4941 << *U.getUser() << "\n"); 4942 // BlockAddress users are allowed. 4943 if (isa<BlockAddress>(U.getUser())) 4944 continue; 4945 return false; 4946 } 4947 4948 Instruction *I = ACS.getInstruction(); 4949 Function *Caller = I->getFunction(); 4950 4951 const auto *LivenessAA = 4952 lookupAAFor<AAIsDead>(IRPosition::function(*Caller), QueryingAA, 4953 /* TrackDependence */ false); 4954 4955 // Skip dead calls. 4956 if (LivenessAA && LivenessAA->isAssumedDead(I)) { 4957 // We actually used liveness information so we have to record a 4958 // dependence. 4959 if (QueryingAA) 4960 recordDependence(*LivenessAA, *QueryingAA, DepClassTy::OPTIONAL); 4961 continue; 4962 } 4963 4964 const Use *EffectiveUse = 4965 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U; 4966 if (!ACS.isCallee(EffectiveUse)) { 4967 if (!RequireAllCallSites) 4968 continue; 4969 LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser() 4970 << " is an invalid use of " << Fn.getName() << "\n"); 4971 return false; 4972 } 4973 4974 if (Pred(ACS)) 4975 continue; 4976 4977 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " 4978 << *ACS.getInstruction() << "\n"); 4979 return false; 4980 } 4981 4982 return true; 4983 } 4984 4985 bool Attributor::checkForAllReturnedValuesAndReturnInsts( 4986 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> 4987 &Pred, 4988 const AbstractAttribute &QueryingAA) { 4989 4990 const IRPosition &IRP = QueryingAA.getIRPosition(); 4991 // Since we need to provide return instructions we have to have an exact 4992 // definition. 4993 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 4994 if (!AssociatedFunction) 4995 return false; 4996 4997 // If this is a call site query we use the call site specific return values 4998 // and liveness information. 4999 // TODO: use the function scope once we have call site AAReturnedValues. 5000 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 5001 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); 5002 if (!AARetVal.getState().isValidState()) 5003 return false; 5004 5005 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred); 5006 } 5007 5008 bool Attributor::checkForAllReturnedValues( 5009 const function_ref<bool(Value &)> &Pred, 5010 const AbstractAttribute &QueryingAA) { 5011 5012 const IRPosition &IRP = QueryingAA.getIRPosition(); 5013 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 5014 if (!AssociatedFunction) 5015 return false; 5016 5017 // TODO: use the function scope once we have call site AAReturnedValues. 5018 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 5019 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); 5020 if (!AARetVal.getState().isValidState()) 5021 return false; 5022 5023 return AARetVal.checkForAllReturnedValuesAndReturnInsts( 5024 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) { 5025 return Pred(RV); 5026 }); 5027 } 5028 5029 static bool 5030 checkForAllInstructionsImpl(InformationCache::OpcodeInstMapTy &OpcodeInstMap, 5031 const function_ref<bool(Instruction &)> &Pred, 5032 const AAIsDead *LivenessAA, bool &AnyDead, 5033 const ArrayRef<unsigned> &Opcodes) { 5034 for (unsigned Opcode : Opcodes) { 5035 for (Instruction *I : OpcodeInstMap[Opcode]) { 5036 // Skip dead instructions. 5037 if (LivenessAA && LivenessAA->isAssumedDead(I)) { 5038 AnyDead = true; 5039 continue; 5040 } 5041 5042 if (!Pred(*I)) 5043 return false; 5044 } 5045 } 5046 return true; 5047 } 5048 5049 bool Attributor::checkForAllInstructions( 5050 const llvm::function_ref<bool(Instruction &)> &Pred, 5051 const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) { 5052 5053 const IRPosition &IRP = QueryingAA.getIRPosition(); 5054 // Since we need to provide instructions we have to have an exact definition. 5055 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 5056 if (!AssociatedFunction) 5057 return false; 5058 5059 // TODO: use the function scope once we have call site AAReturnedValues. 5060 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 5061 const auto &LivenessAA = 5062 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); 5063 bool AnyDead = false; 5064 5065 auto &OpcodeInstMap = 5066 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); 5067 if (!checkForAllInstructionsImpl(OpcodeInstMap, Pred, &LivenessAA, AnyDead, 5068 Opcodes)) 5069 return false; 5070 5071 // If we actually used liveness information so we have to record a dependence. 5072 if (AnyDead) 5073 recordDependence(LivenessAA, QueryingAA, DepClassTy::OPTIONAL); 5074 5075 return true; 5076 } 5077 5078 bool Attributor::checkForAllReadWriteInstructions( 5079 const llvm::function_ref<bool(Instruction &)> &Pred, 5080 AbstractAttribute &QueryingAA) { 5081 5082 const Function *AssociatedFunction = 5083 QueryingAA.getIRPosition().getAssociatedFunction(); 5084 if (!AssociatedFunction) 5085 return false; 5086 5087 // TODO: use the function scope once we have call site AAReturnedValues. 5088 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction); 5089 const auto &LivenessAA = 5090 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false); 5091 bool AnyDead = false; 5092 5093 for (Instruction *I : 5094 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { 5095 // Skip dead instructions. 5096 if (LivenessAA.isAssumedDead(I)) { 5097 AnyDead = true; 5098 continue; 5099 } 5100 5101 if (!Pred(*I)) 5102 return false; 5103 } 5104 5105 // If we actually used liveness information so we have to record a dependence. 5106 if (AnyDead) 5107 recordDependence(LivenessAA, QueryingAA, DepClassTy::OPTIONAL); 5108 5109 return true; 5110 } 5111 5112 ChangeStatus Attributor::run(Module &M) { 5113 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " 5114 << AllAbstractAttributes.size() 5115 << " abstract attributes.\n"); 5116 5117 // Now that all abstract attributes are collected and initialized we start 5118 // the abstract analysis. 5119 5120 unsigned IterationCounter = 1; 5121 5122 SmallVector<AbstractAttribute *, 64> ChangedAAs; 5123 SetVector<AbstractAttribute *> Worklist, InvalidAAs; 5124 Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); 5125 5126 bool RecomputeDependences = false; 5127 5128 do { 5129 // Remember the size to determine new attributes. 5130 size_t NumAAs = AllAbstractAttributes.size(); 5131 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter 5132 << ", Worklist size: " << Worklist.size() << "\n"); 5133 5134 // For invalid AAs we can fix dependent AAs that have a required dependence, 5135 // thereby folding long dependence chains in a single step without the need 5136 // to run updates. 5137 for (unsigned u = 0; u < InvalidAAs.size(); ++u) { 5138 AbstractAttribute *InvalidAA = InvalidAAs[u]; 5139 auto &QuerriedAAs = QueryMap[InvalidAA]; 5140 LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has " 5141 << QuerriedAAs.RequiredAAs.size() << "/" 5142 << QuerriedAAs.OptionalAAs.size() 5143 << " required/optional dependences\n"); 5144 for (AbstractAttribute *DepOnInvalidAA : QuerriedAAs.RequiredAAs) { 5145 AbstractState &DOIAAState = DepOnInvalidAA->getState(); 5146 DOIAAState.indicatePessimisticFixpoint(); 5147 ++NumAttributesFixedDueToRequiredDependences; 5148 assert(DOIAAState.isAtFixpoint() && "Expected fixpoint state!"); 5149 if (!DOIAAState.isValidState()) 5150 InvalidAAs.insert(DepOnInvalidAA); 5151 } 5152 if (!RecomputeDependences) 5153 Worklist.insert(QuerriedAAs.OptionalAAs.begin(), 5154 QuerriedAAs.OptionalAAs.end()); 5155 } 5156 5157 // If dependences (=QueryMap) are recomputed we have to look at all abstract 5158 // attributes again, regardless of what changed in the last iteration. 5159 if (RecomputeDependences) { 5160 LLVM_DEBUG( 5161 dbgs() << "[Attributor] Run all AAs to recompute dependences\n"); 5162 QueryMap.clear(); 5163 ChangedAAs.clear(); 5164 Worklist.insert(AllAbstractAttributes.begin(), 5165 AllAbstractAttributes.end()); 5166 } 5167 5168 // Add all abstract attributes that are potentially dependent on one that 5169 // changed to the work list. 5170 for (AbstractAttribute *ChangedAA : ChangedAAs) { 5171 auto &QuerriedAAs = QueryMap[ChangedAA]; 5172 Worklist.insert(QuerriedAAs.OptionalAAs.begin(), 5173 QuerriedAAs.OptionalAAs.end()); 5174 Worklist.insert(QuerriedAAs.RequiredAAs.begin(), 5175 QuerriedAAs.RequiredAAs.end()); 5176 } 5177 5178 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter 5179 << ", Worklist+Dependent size: " << Worklist.size() 5180 << "\n"); 5181 5182 // Reset the changed and invalid set. 5183 ChangedAAs.clear(); 5184 InvalidAAs.clear(); 5185 5186 // Update all abstract attribute in the work list and record the ones that 5187 // changed. 5188 for (AbstractAttribute *AA : Worklist) 5189 if (!AA->getState().isAtFixpoint() && !isAssumedDead(*AA, nullptr)) { 5190 QueriedNonFixAA = false; 5191 if (AA->update(*this) == ChangeStatus::CHANGED) { 5192 ChangedAAs.push_back(AA); 5193 if (!AA->getState().isValidState()) 5194 InvalidAAs.insert(AA); 5195 } else if (!QueriedNonFixAA) { 5196 // If the attribute did not query any non-fix information, the state 5197 // will not change and we can indicate that right away. 5198 AA->getState().indicateOptimisticFixpoint(); 5199 } 5200 } 5201 5202 // Check if we recompute the dependences in the next iteration. 5203 RecomputeDependences = (DepRecomputeInterval > 0 && 5204 IterationCounter % DepRecomputeInterval == 0); 5205 5206 // Add attributes to the changed set if they have been created in the last 5207 // iteration. 5208 ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs, 5209 AllAbstractAttributes.end()); 5210 5211 // Reset the work list and repopulate with the changed abstract attributes. 5212 // Note that dependent ones are added above. 5213 Worklist.clear(); 5214 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); 5215 5216 } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations || 5217 VerifyMaxFixpointIterations)); 5218 5219 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " 5220 << IterationCounter << "/" << MaxFixpointIterations 5221 << " iterations\n"); 5222 5223 size_t NumFinalAAs = AllAbstractAttributes.size(); 5224 5225 // Reset abstract arguments not settled in a sound fixpoint by now. This 5226 // happens when we stopped the fixpoint iteration early. Note that only the 5227 // ones marked as "changed" *and* the ones transitively depending on them 5228 // need to be reverted to a pessimistic state. Others might not be in a 5229 // fixpoint state but we can use the optimistic results for them anyway. 5230 SmallPtrSet<AbstractAttribute *, 32> Visited; 5231 for (unsigned u = 0; u < ChangedAAs.size(); u++) { 5232 AbstractAttribute *ChangedAA = ChangedAAs[u]; 5233 if (!Visited.insert(ChangedAA).second) 5234 continue; 5235 5236 AbstractState &State = ChangedAA->getState(); 5237 if (!State.isAtFixpoint()) { 5238 State.indicatePessimisticFixpoint(); 5239 5240 NumAttributesTimedOut++; 5241 } 5242 5243 auto &QuerriedAAs = QueryMap[ChangedAA]; 5244 ChangedAAs.append(QuerriedAAs.OptionalAAs.begin(), 5245 QuerriedAAs.OptionalAAs.end()); 5246 ChangedAAs.append(QuerriedAAs.RequiredAAs.begin(), 5247 QuerriedAAs.RequiredAAs.end()); 5248 } 5249 5250 LLVM_DEBUG({ 5251 if (!Visited.empty()) 5252 dbgs() << "\n[Attributor] Finalized " << Visited.size() 5253 << " abstract attributes.\n"; 5254 }); 5255 5256 unsigned NumManifested = 0; 5257 unsigned NumAtFixpoint = 0; 5258 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; 5259 for (AbstractAttribute *AA : AllAbstractAttributes) { 5260 AbstractState &State = AA->getState(); 5261 5262 // If there is not already a fixpoint reached, we can now take the 5263 // optimistic state. This is correct because we enforced a pessimistic one 5264 // on abstract attributes that were transitively dependent on a changed one 5265 // already above. 5266 if (!State.isAtFixpoint()) 5267 State.indicateOptimisticFixpoint(); 5268 5269 // If the state is invalid, we do not try to manifest it. 5270 if (!State.isValidState()) 5271 continue; 5272 5273 // Skip dead code. 5274 if (isAssumedDead(*AA, nullptr)) 5275 continue; 5276 // Manifest the state and record if we changed the IR. 5277 ChangeStatus LocalChange = AA->manifest(*this); 5278 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) 5279 AA->trackStatistics(); 5280 5281 ManifestChange = ManifestChange | LocalChange; 5282 5283 NumAtFixpoint++; 5284 NumManifested += (LocalChange == ChangeStatus::CHANGED); 5285 } 5286 5287 (void)NumManifested; 5288 (void)NumAtFixpoint; 5289 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested 5290 << " arguments while " << NumAtFixpoint 5291 << " were in a valid fixpoint state\n"); 5292 5293 NumAttributesManifested += NumManifested; 5294 NumAttributesValidFixpoint += NumAtFixpoint; 5295 5296 (void)NumFinalAAs; 5297 assert( 5298 NumFinalAAs == AllAbstractAttributes.size() && 5299 "Expected the final number of abstract attributes to remain unchanged!"); 5300 5301 // Delete stuff at the end to avoid invalid references and a nice order. 5302 { 5303 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least " 5304 << ToBeDeletedFunctions.size() << " functions and " 5305 << ToBeDeletedBlocks.size() << " blocks and " 5306 << ToBeDeletedInsts.size() << " instructions and " 5307 << ToBeChangedUses.size() << " uses\n"); 5308 5309 SmallVector<Instruction *, 32> DeadInsts; 5310 SmallVector<Instruction *, 32> TerminatorsToFold; 5311 SmallVector<Instruction *, 32> UnreachablesToInsert; 5312 5313 for (auto &It : ToBeChangedUses) { 5314 Use *U = It.first; 5315 Value *NewV = It.second; 5316 Value *OldV = U->get(); 5317 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() 5318 << " instead of " << *OldV << "\n"); 5319 U->set(NewV); 5320 if (Instruction *I = dyn_cast<Instruction>(OldV)) 5321 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) && 5322 isInstructionTriviallyDead(I)) { 5323 DeadInsts.push_back(I); 5324 } 5325 if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) { 5326 Instruction *UserI = cast<Instruction>(U->getUser()); 5327 if (isa<UndefValue>(NewV)) { 5328 UnreachablesToInsert.push_back(UserI); 5329 } else { 5330 TerminatorsToFold.push_back(UserI); 5331 } 5332 } 5333 } 5334 for (Instruction *I : UnreachablesToInsert) 5335 changeToUnreachable(I, /* UseLLVMTrap */ false); 5336 for (Instruction *I : TerminatorsToFold) 5337 ConstantFoldTerminator(I->getParent()); 5338 5339 for (Instruction *I : ToBeDeletedInsts) { 5340 I->replaceAllUsesWith(UndefValue::get(I->getType())); 5341 if (!isa<PHINode>(I) && isInstructionTriviallyDead(I)) 5342 DeadInsts.push_back(I); 5343 else 5344 I->eraseFromParent(); 5345 } 5346 5347 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); 5348 5349 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { 5350 SmallVector<BasicBlock *, 8> ToBeDeletedBBs; 5351 ToBeDeletedBBs.reserve(NumDeadBlocks); 5352 ToBeDeletedBBs.append(ToBeDeletedBlocks.begin(), ToBeDeletedBlocks.end()); 5353 // Actually we do not delete the blocks but squash them into a single 5354 // unreachable but untangling branches that jump here is something we need 5355 // to do in a more generic way. 5356 DetatchDeadBlocks(ToBeDeletedBBs, nullptr); 5357 STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted."); 5358 BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size(); 5359 } 5360 5361 STATS_DECL(AAIsDead, Function, "Number of dead functions deleted."); 5362 for (Function *Fn : ToBeDeletedFunctions) { 5363 Fn->replaceAllUsesWith(UndefValue::get(Fn->getType())); 5364 Fn->eraseFromParent(); 5365 STATS_TRACK(AAIsDead, Function); 5366 } 5367 5368 // Identify dead internal functions and delete them. This happens outside 5369 // the other fixpoint analysis as we might treat potentially dead functions 5370 // as live to lower the number of iterations. If they happen to be dead, the 5371 // below fixpoint loop will identify and eliminate them. 5372 SmallVector<Function *, 8> InternalFns; 5373 for (Function &F : M) 5374 if (F.hasLocalLinkage()) 5375 InternalFns.push_back(&F); 5376 5377 bool FoundDeadFn = true; 5378 while (FoundDeadFn) { 5379 FoundDeadFn = false; 5380 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { 5381 Function *F = InternalFns[u]; 5382 if (!F) 5383 continue; 5384 5385 if (!checkForAllCallSites([](AbstractCallSite ACS) { return false; }, 5386 *F, true, nullptr)) 5387 continue; 5388 5389 STATS_TRACK(AAIsDead, Function); 5390 ToBeDeletedFunctions.insert(F); 5391 F->deleteBody(); 5392 F->replaceAllUsesWith(UndefValue::get(F->getType())); 5393 F->eraseFromParent(); 5394 InternalFns[u] = nullptr; 5395 FoundDeadFn = true; 5396 } 5397 } 5398 } 5399 5400 if (VerifyMaxFixpointIterations && 5401 IterationCounter != MaxFixpointIterations) { 5402 errs() << "\n[Attributor] Fixpoint iteration done after: " 5403 << IterationCounter << "/" << MaxFixpointIterations 5404 << " iterations\n"; 5405 llvm_unreachable("The fixpoint was not reached with exactly the number of " 5406 "specified iterations!"); 5407 } 5408 5409 return ManifestChange; 5410 } 5411 5412 void Attributor::initializeInformationCache(Function &F) { 5413 5414 // Walk all instructions to find interesting instructions that might be 5415 // queried by abstract attributes during their initialization or update. 5416 // This has to happen before we create attributes. 5417 auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; 5418 auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; 5419 5420 for (Instruction &I : instructions(&F)) { 5421 bool IsInterestingOpcode = false; 5422 5423 // To allow easy access to all instructions in a function with a given 5424 // opcode we store them in the InfoCache. As not all opcodes are interesting 5425 // to concrete attributes we only cache the ones that are as identified in 5426 // the following switch. 5427 // Note: There are no concrete attributes now so this is initially empty. 5428 switch (I.getOpcode()) { 5429 default: 5430 assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && 5431 "New call site/base instruction type needs to be known int the " 5432 "Attributor."); 5433 break; 5434 case Instruction::Load: 5435 // The alignment of a pointer is interesting for loads. 5436 case Instruction::Store: 5437 // The alignment of a pointer is interesting for stores. 5438 case Instruction::Call: 5439 case Instruction::CallBr: 5440 case Instruction::Invoke: 5441 case Instruction::CleanupRet: 5442 case Instruction::CatchSwitch: 5443 case Instruction::Resume: 5444 case Instruction::Ret: 5445 IsInterestingOpcode = true; 5446 } 5447 if (IsInterestingOpcode) 5448 InstOpcodeMap[I.getOpcode()].push_back(&I); 5449 if (I.mayReadOrWriteMemory()) 5450 ReadOrWriteInsts.push_back(&I); 5451 } 5452 } 5453 5454 void Attributor::recordDependence(const AbstractAttribute &FromAA, 5455 const AbstractAttribute &ToAA, 5456 DepClassTy DepClass) { 5457 if (FromAA.getState().isAtFixpoint()) 5458 return; 5459 5460 if (DepClass == DepClassTy::REQUIRED) 5461 QueryMap[&FromAA].RequiredAAs.insert( 5462 const_cast<AbstractAttribute *>(&ToAA)); 5463 else 5464 QueryMap[&FromAA].OptionalAAs.insert( 5465 const_cast<AbstractAttribute *>(&ToAA)); 5466 QueriedNonFixAA = true; 5467 } 5468 5469 void Attributor::identifyDefaultAbstractAttributes(Function &F) { 5470 if (!VisitedFunctions.insert(&F).second) 5471 return; 5472 if (F.isDeclaration()) 5473 return; 5474 5475 IRPosition FPos = IRPosition::function(F); 5476 5477 // Check for dead BasicBlocks in every function. 5478 // We need dead instruction detection because we do not want to deal with 5479 // broken IR in which SSA rules do not apply. 5480 getOrCreateAAFor<AAIsDead>(FPos); 5481 5482 // Every function might be "will-return". 5483 getOrCreateAAFor<AAWillReturn>(FPos); 5484 5485 // Every function can be nounwind. 5486 getOrCreateAAFor<AANoUnwind>(FPos); 5487 5488 // Every function might be marked "nosync" 5489 getOrCreateAAFor<AANoSync>(FPos); 5490 5491 // Every function might be "no-free". 5492 getOrCreateAAFor<AANoFree>(FPos); 5493 5494 // Every function might be "no-return". 5495 getOrCreateAAFor<AANoReturn>(FPos); 5496 5497 // Every function might be "no-recurse". 5498 getOrCreateAAFor<AANoRecurse>(FPos); 5499 5500 // Every function might be "readnone/readonly/writeonly/...". 5501 getOrCreateAAFor<AAMemoryBehavior>(FPos); 5502 5503 // Every function might be applicable for Heap-To-Stack conversion. 5504 if (EnableHeapToStack) 5505 getOrCreateAAFor<AAHeapToStack>(FPos); 5506 5507 // Return attributes are only appropriate if the return type is non void. 5508 Type *ReturnType = F.getReturnType(); 5509 if (!ReturnType->isVoidTy()) { 5510 // Argument attribute "returned" --- Create only one per function even 5511 // though it is an argument attribute. 5512 getOrCreateAAFor<AAReturnedValues>(FPos); 5513 5514 IRPosition RetPos = IRPosition::returned(F); 5515 5516 // Every returned value might be dead. 5517 getOrCreateAAFor<AAIsDead>(RetPos); 5518 5519 // Every function might be simplified. 5520 getOrCreateAAFor<AAValueSimplify>(RetPos); 5521 5522 if (ReturnType->isPointerTy()) { 5523 5524 // Every function with pointer return type might be marked align. 5525 getOrCreateAAFor<AAAlign>(RetPos); 5526 5527 // Every function with pointer return type might be marked nonnull. 5528 getOrCreateAAFor<AANonNull>(RetPos); 5529 5530 // Every function with pointer return type might be marked noalias. 5531 getOrCreateAAFor<AANoAlias>(RetPos); 5532 5533 // Every function with pointer return type might be marked 5534 // dereferenceable. 5535 getOrCreateAAFor<AADereferenceable>(RetPos); 5536 } 5537 } 5538 5539 for (Argument &Arg : F.args()) { 5540 IRPosition ArgPos = IRPosition::argument(Arg); 5541 5542 // Every argument might be simplified. 5543 getOrCreateAAFor<AAValueSimplify>(ArgPos); 5544 5545 if (Arg.getType()->isPointerTy()) { 5546 // Every argument with pointer type might be marked nonnull. 5547 getOrCreateAAFor<AANonNull>(ArgPos); 5548 5549 // Every argument with pointer type might be marked noalias. 5550 getOrCreateAAFor<AANoAlias>(ArgPos); 5551 5552 // Every argument with pointer type might be marked dereferenceable. 5553 getOrCreateAAFor<AADereferenceable>(ArgPos); 5554 5555 // Every argument with pointer type might be marked align. 5556 getOrCreateAAFor<AAAlign>(ArgPos); 5557 5558 // Every argument with pointer type might be marked nocapture. 5559 getOrCreateAAFor<AANoCapture>(ArgPos); 5560 5561 // Every argument with pointer type might be marked 5562 // "readnone/readonly/writeonly/..." 5563 getOrCreateAAFor<AAMemoryBehavior>(ArgPos); 5564 5565 // Every argument with pointer type might be marked nofree. 5566 getOrCreateAAFor<AANoFree>(ArgPos); 5567 } 5568 } 5569 5570 auto CallSitePred = [&](Instruction &I) -> bool { 5571 CallSite CS(&I); 5572 if (Function *Callee = CS.getCalledFunction()) { 5573 // Skip declerations except if annotations on their call sites were 5574 // explicitly requested. 5575 if (!AnnotateDeclarationCallSites && Callee->isDeclaration()) 5576 return true; 5577 5578 if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) { 5579 IRPosition CSRetPos = IRPosition::callsite_returned(CS); 5580 5581 // Call site return values might be dead. 5582 getOrCreateAAFor<AAIsDead>(CSRetPos); 5583 } 5584 5585 for (int i = 0, e = Callee->arg_size(); i < e; i++) { 5586 5587 IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); 5588 5589 // Every call site argument might be dead. 5590 getOrCreateAAFor<AAIsDead>(CSArgPos); 5591 5592 // Call site argument might be simplified. 5593 getOrCreateAAFor<AAValueSimplify>(CSArgPos); 5594 5595 if (!CS.getArgument(i)->getType()->isPointerTy()) 5596 continue; 5597 5598 // Call site argument attribute "non-null". 5599 getOrCreateAAFor<AANonNull>(CSArgPos); 5600 5601 // Call site argument attribute "no-alias". 5602 getOrCreateAAFor<AANoAlias>(CSArgPos); 5603 5604 // Call site argument attribute "dereferenceable". 5605 getOrCreateAAFor<AADereferenceable>(CSArgPos); 5606 5607 // Call site argument attribute "align". 5608 getOrCreateAAFor<AAAlign>(CSArgPos); 5609 5610 // Call site argument attribute "nofree". 5611 getOrCreateAAFor<AANoFree>(CSArgPos); 5612 } 5613 } 5614 return true; 5615 }; 5616 5617 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); 5618 bool Success, AnyDead = false; 5619 Success = checkForAllInstructionsImpl( 5620 OpcodeInstMap, CallSitePred, nullptr, AnyDead, 5621 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, 5622 (unsigned)Instruction::Call}); 5623 (void)Success; 5624 assert(Success && !AnyDead && "Expected the check call to be successful!"); 5625 5626 auto LoadStorePred = [&](Instruction &I) -> bool { 5627 if (isa<LoadInst>(I)) 5628 getOrCreateAAFor<AAAlign>( 5629 IRPosition::value(*cast<LoadInst>(I).getPointerOperand())); 5630 else 5631 getOrCreateAAFor<AAAlign>( 5632 IRPosition::value(*cast<StoreInst>(I).getPointerOperand())); 5633 return true; 5634 }; 5635 Success = checkForAllInstructionsImpl( 5636 OpcodeInstMap, LoadStorePred, nullptr, AnyDead, 5637 {(unsigned)Instruction::Load, (unsigned)Instruction::Store}); 5638 (void)Success; 5639 assert(Success && !AnyDead && "Expected the check call to be successful!"); 5640 } 5641 5642 /// Helpers to ease debugging through output streams and print calls. 5643 /// 5644 ///{ 5645 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { 5646 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); 5647 } 5648 5649 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { 5650 switch (AP) { 5651 case IRPosition::IRP_INVALID: 5652 return OS << "inv"; 5653 case IRPosition::IRP_FLOAT: 5654 return OS << "flt"; 5655 case IRPosition::IRP_RETURNED: 5656 return OS << "fn_ret"; 5657 case IRPosition::IRP_CALL_SITE_RETURNED: 5658 return OS << "cs_ret"; 5659 case IRPosition::IRP_FUNCTION: 5660 return OS << "fn"; 5661 case IRPosition::IRP_CALL_SITE: 5662 return OS << "cs"; 5663 case IRPosition::IRP_ARGUMENT: 5664 return OS << "arg"; 5665 case IRPosition::IRP_CALL_SITE_ARGUMENT: 5666 return OS << "cs_arg"; 5667 } 5668 llvm_unreachable("Unknown attribute position!"); 5669 } 5670 5671 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { 5672 const Value &AV = Pos.getAssociatedValue(); 5673 return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" 5674 << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; 5675 } 5676 5677 template <typename base_ty, base_ty BestState, base_ty WorstState> 5678 raw_ostream &llvm:: 5679 operator<<(raw_ostream &OS, 5680 const IntegerStateBase<base_ty, BestState, WorstState> &S) { 5681 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" 5682 << static_cast<const AbstractState &>(S); 5683 } 5684 5685 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { 5686 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); 5687 } 5688 5689 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { 5690 AA.print(OS); 5691 return OS; 5692 } 5693 5694 void AbstractAttribute::print(raw_ostream &OS) const { 5695 OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() 5696 << "]"; 5697 } 5698 ///} 5699 5700 /// ---------------------------------------------------------------------------- 5701 /// Pass (Manager) Boilerplate 5702 /// ---------------------------------------------------------------------------- 5703 5704 static bool runAttributorOnModule(Module &M, AnalysisGetter &AG) { 5705 if (DisableAttributor) 5706 return false; 5707 5708 LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size() 5709 << " functions.\n"); 5710 5711 // Create an Attributor and initially empty information cache that is filled 5712 // while we identify default attribute opportunities. 5713 InformationCache InfoCache(M, AG); 5714 Attributor A(InfoCache, DepRecInterval); 5715 5716 for (Function &F : M) 5717 A.initializeInformationCache(F); 5718 5719 for (Function &F : M) { 5720 if (F.hasExactDefinition()) 5721 NumFnWithExactDefinition++; 5722 else 5723 NumFnWithoutExactDefinition++; 5724 5725 // We look at internal functions only on-demand but if any use is not a 5726 // direct call, we have to do it eagerly. 5727 if (F.hasLocalLinkage()) { 5728 if (llvm::all_of(F.uses(), [](const Use &U) { 5729 return ImmutableCallSite(U.getUser()) && 5730 ImmutableCallSite(U.getUser()).isCallee(&U); 5731 })) 5732 continue; 5733 } 5734 5735 // Populate the Attributor with abstract attribute opportunities in the 5736 // function and the information cache with IR information. 5737 A.identifyDefaultAbstractAttributes(F); 5738 } 5739 5740 return A.run(M) == ChangeStatus::CHANGED; 5741 } 5742 5743 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { 5744 AnalysisGetter AG(AM); 5745 if (runAttributorOnModule(M, AG)) { 5746 // FIXME: Think about passes we will preserve and add them here. 5747 return PreservedAnalyses::none(); 5748 } 5749 return PreservedAnalyses::all(); 5750 } 5751 5752 namespace { 5753 5754 struct AttributorLegacyPass : public ModulePass { 5755 static char ID; 5756 5757 AttributorLegacyPass() : ModulePass(ID) { 5758 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); 5759 } 5760 5761 bool runOnModule(Module &M) override { 5762 if (skipModule(M)) 5763 return false; 5764 5765 AnalysisGetter AG; 5766 return runAttributorOnModule(M, AG); 5767 } 5768 5769 void getAnalysisUsage(AnalysisUsage &AU) const override { 5770 // FIXME: Think about passes we will preserve and add them here. 5771 AU.addRequired<TargetLibraryInfoWrapperPass>(); 5772 } 5773 }; 5774 5775 } // end anonymous namespace 5776 5777 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } 5778 5779 char AttributorLegacyPass::ID = 0; 5780 5781 const char AAReturnedValues::ID = 0; 5782 const char AANoUnwind::ID = 0; 5783 const char AANoSync::ID = 0; 5784 const char AANoFree::ID = 0; 5785 const char AANonNull::ID = 0; 5786 const char AANoRecurse::ID = 0; 5787 const char AAWillReturn::ID = 0; 5788 const char AANoAlias::ID = 0; 5789 const char AAReachability::ID = 0; 5790 const char AANoReturn::ID = 0; 5791 const char AAIsDead::ID = 0; 5792 const char AADereferenceable::ID = 0; 5793 const char AAAlign::ID = 0; 5794 const char AANoCapture::ID = 0; 5795 const char AAValueSimplify::ID = 0; 5796 const char AAHeapToStack::ID = 0; 5797 const char AAMemoryBehavior::ID = 0; 5798 5799 // Macro magic to create the static generator function for attributes that 5800 // follow the naming scheme. 5801 5802 #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \ 5803 case IRPosition::PK: \ 5804 llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!"); 5805 5806 #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \ 5807 case IRPosition::PK: \ 5808 AA = new CLASS##SUFFIX(IRP); \ 5809 break; 5810 5811 #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 5812 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 5813 CLASS *AA = nullptr; \ 5814 switch (IRP.getPositionKind()) { \ 5815 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 5816 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ 5817 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ 5818 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ 5819 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ 5820 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ 5821 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 5822 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ 5823 } \ 5824 return *AA; \ 5825 } 5826 5827 #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 5828 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 5829 CLASS *AA = nullptr; \ 5830 switch (IRP.getPositionKind()) { \ 5831 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 5832 SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \ 5833 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ 5834 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ 5835 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ 5836 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ 5837 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ 5838 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ 5839 } \ 5840 return *AA; \ 5841 } 5842 5843 #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 5844 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 5845 CLASS *AA = nullptr; \ 5846 switch (IRP.getPositionKind()) { \ 5847 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 5848 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 5849 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ 5850 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ 5851 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ 5852 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ 5853 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ 5854 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ 5855 } \ 5856 return *AA; \ 5857 } 5858 5859 #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 5860 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 5861 CLASS *AA = nullptr; \ 5862 switch (IRP.getPositionKind()) { \ 5863 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 5864 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ 5865 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ 5866 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ 5867 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ 5868 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ 5869 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ 5870 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 5871 } \ 5872 return *AA; \ 5873 } 5874 5875 #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ 5876 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ 5877 CLASS *AA = nullptr; \ 5878 switch (IRP.getPositionKind()) { \ 5879 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ 5880 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ 5881 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ 5882 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ 5883 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ 5884 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ 5885 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ 5886 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ 5887 } \ 5888 return *AA; \ 5889 } 5890 5891 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) 5892 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) 5893 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) 5894 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) 5895 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) 5896 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) 5897 5898 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) 5899 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) 5900 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) 5901 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) 5902 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture) 5903 5904 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify) 5905 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) 5906 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) 5907 5908 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack) 5909 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability) 5910 5911 CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior) 5912 5913 #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION 5914 #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION 5915 #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION 5916 #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION 5917 #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION 5918 #undef SWITCH_PK_CREATE 5919 #undef SWITCH_PK_INV 5920 5921 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", 5922 "Deduce and propagate attributes", false, false) 5923 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 5924 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", 5925 "Deduce and propagate attributes", false, false) 5926