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/SetVector.h" 21 #include "llvm/ADT/SmallPtrSet.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/Statistic.h" 24 #include "llvm/Analysis/CaptureTracking.h" 25 #include "llvm/Analysis/EHPersonalities.h" 26 #include "llvm/Analysis/GlobalsModRef.h" 27 #include "llvm/Analysis/Loads.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/Support/CommandLine.h" 35 #include "llvm/Support/Debug.h" 36 #include "llvm/Support/raw_ostream.h" 37 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 38 #include "llvm/Transforms/Utils/Local.h" 39 40 #include <cassert> 41 42 using namespace llvm; 43 44 #define DEBUG_TYPE "attributor" 45 46 STATISTIC(NumFnWithExactDefinition, 47 "Number of function with exact definitions"); 48 STATISTIC(NumFnWithoutExactDefinition, 49 "Number of function without exact definitions"); 50 STATISTIC(NumAttributesTimedOut, 51 "Number of abstract attributes timed out before fixpoint"); 52 STATISTIC(NumAttributesValidFixpoint, 53 "Number of abstract attributes in a valid fixpoint state"); 54 STATISTIC(NumAttributesManifested, 55 "Number of abstract attributes manifested in IR"); 56 57 // Some helper macros to deal with statistics tracking. 58 // 59 // Usage: 60 // For simple IR attribute tracking overload trackStatistics in the abstract 61 // attribute and choose the right STATS_DECLTRACK_********* macro, 62 // e.g.,: 63 // void trackStatistics() const override { 64 // STATS_DECLTRACK_ARG_ATTR(returned) 65 // } 66 // If there is a single "increment" side one can use the macro 67 // STATS_DECLTRACK with a custom message. If there are multiple increment 68 // sides, STATS_DECL and STATS_TRACK can also be used separatly. 69 // 70 #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \ 71 ("Number of " #TYPE " marked '" #NAME "'") 72 #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME 73 #define STATS_DECL(NAME, TYPE, MSG) STATISTIC(BUILD_STAT_NAME(NAME, TYPE), MSG); 74 #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE)); 75 #define STATS_DECLTRACK(NAME, TYPE, MSG) \ 76 STATS_DECL(NAME, TYPE, MSG) \ 77 STATS_TRACK(NAME, TYPE) 78 #define STATS_DECLTRACK_ARG_ATTR(NAME) \ 79 STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME)) 80 #define STATS_DECLTRACK_CSARG_ATTR(NAME) \ 81 STATS_DECLTRACK(NAME, CSArguments, \ 82 BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME)) 83 #define STATS_DECLTRACK_FN_ATTR(NAME) \ 84 STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME)) 85 #define STATS_DECLTRACK_CS_ATTR(NAME) \ 86 STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME)) 87 #define STATS_DECLTRACK_FNRET_ATTR(NAME) \ 88 STATS_DECLTRACK(NAME, FunctionReturn, \ 89 BUILD_STAT_MSG_IR_ATTR(function returns, NAME)); 90 #define STATS_DECLTRACK_CSRET_ATTR(NAME) \ 91 STATS_DECLTRACK(NAME, CSReturn, \ 92 BUILD_STAT_MSG_IR_ATTR(call site returns, NAME)) 93 #define STATS_DECLTRACK_FLOATING_ATTR(NAME) \ 94 STATS_DECLTRACK(NAME, Floating, \ 95 ("Number of floating values known to be '" #NAME "'")) 96 97 // TODO: Determine a good default value. 98 // 99 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads 100 // (when run with the first 5 abstract attributes). The results also indicate 101 // that we never reach 32 iterations but always find a fixpoint sooner. 102 // 103 // This will become more evolved once we perform two interleaved fixpoint 104 // iterations: bottom-up and top-down. 105 static cl::opt<unsigned> 106 MaxFixpointIterations("attributor-max-iterations", cl::Hidden, 107 cl::desc("Maximal number of fixpoint iterations."), 108 cl::init(32)); 109 110 static cl::opt<bool> DisableAttributor( 111 "attributor-disable", cl::Hidden, 112 cl::desc("Disable the attributor inter-procedural deduction pass."), 113 cl::init(true)); 114 115 static cl::opt<bool> VerifyAttributor( 116 "attributor-verify", cl::Hidden, 117 cl::desc("Verify the Attributor deduction and " 118 "manifestation of attributes -- may issue false-positive errors"), 119 cl::init(false)); 120 121 /// Logic operators for the change status enum class. 122 /// 123 ///{ 124 ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) { 125 return l == ChangeStatus::CHANGED ? l : r; 126 } 127 ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) { 128 return l == ChangeStatus::UNCHANGED ? l : r; 129 } 130 ///} 131 132 /// Recursively visit all values that might become \p IRP at some point. This 133 /// will be done by looking through cast instructions, selects, phis, and calls 134 /// with the "returned" attribute. Once we cannot look through the value any 135 /// further, the callback \p VisitValueCB is invoked and passed the current 136 /// value, the \p State, and a flag to indicate if we stripped anything. To 137 /// limit how much effort is invested, we will never visit more values than 138 /// specified by \p MaxValues. 139 template <typename AAType, typename StateTy> 140 bool genericValueTraversal( 141 Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State, 142 const function_ref<void(Value &, StateTy &, bool)> &VisitValueCB, 143 int MaxValues = 8) { 144 145 const AAIsDead *LivenessAA = nullptr; 146 if (IRP.getAnchorScope()) 147 LivenessAA = A.getAAFor<AAIsDead>( 148 QueryingAA, IRPosition::function(*IRP.getAnchorScope())); 149 150 // TODO: Use Positions here to allow context sensitivity in VisitValueCB 151 SmallPtrSet<Value *, 16> Visited; 152 SmallVector<Value *, 16> Worklist; 153 Worklist.push_back(&IRP.getAssociatedValue()); 154 155 int Iteration = 0; 156 do { 157 Value *V = Worklist.pop_back_val(); 158 159 // Check if we should process the current value. To prevent endless 160 // recursion keep a record of the values we followed! 161 if (!Visited.insert(V).second) 162 continue; 163 164 // Make sure we limit the compile time for complex expressions. 165 if (Iteration++ >= MaxValues) 166 return false; 167 168 // Explicitly look through calls with a "returned" attribute if we do 169 // not have a pointer as stripPointerCasts only works on them. 170 Value *NewV = nullptr; 171 if (V->getType()->isPointerTy()) { 172 NewV = V->stripPointerCasts(); 173 } else { 174 CallSite CS(V); 175 if (CS && CS.getCalledFunction()) { 176 for (Argument &Arg : CS.getCalledFunction()->args()) 177 if (Arg.hasReturnedAttr()) { 178 NewV = CS.getArgOperand(Arg.getArgNo()); 179 break; 180 } 181 } 182 } 183 if (NewV && NewV != V) { 184 Worklist.push_back(NewV); 185 continue; 186 } 187 188 // Look through select instructions, visit both potential values. 189 if (auto *SI = dyn_cast<SelectInst>(V)) { 190 Worklist.push_back(SI->getTrueValue()); 191 Worklist.push_back(SI->getFalseValue()); 192 continue; 193 } 194 195 // Look through phi nodes, visit all live operands. 196 if (auto *PHI = dyn_cast<PHINode>(V)) { 197 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { 198 const BasicBlock *IncomingBB = PHI->getIncomingBlock(u); 199 if (!LivenessAA || 200 !LivenessAA->isAssumedDead(IncomingBB->getTerminator())) 201 Worklist.push_back(PHI->getIncomingValue(u)); 202 } 203 continue; 204 } 205 206 // Once a leaf is reached we inform the user through the callback. 207 VisitValueCB(*V, State, Iteration > 1); 208 } while (!Worklist.empty()); 209 210 // All values have been visited. 211 return true; 212 } 213 214 /// Return true if \p New is equal or worse than \p Old. 215 static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { 216 if (!Old.isIntAttribute()) 217 return true; 218 219 return Old.getValueAsInt() >= New.getValueAsInt(); 220 } 221 222 /// Return true if the information provided by \p Attr was added to the 223 /// attribute list \p Attrs. This is only the case if it was not already present 224 /// in \p Attrs at the position describe by \p PK and \p AttrIdx. 225 static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, 226 AttributeList &Attrs, int AttrIdx) { 227 228 if (Attr.isEnumAttribute()) { 229 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 230 if (Attrs.hasAttribute(AttrIdx, Kind)) 231 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) 232 return false; 233 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); 234 return true; 235 } 236 if (Attr.isStringAttribute()) { 237 StringRef Kind = Attr.getKindAsString(); 238 if (Attrs.hasAttribute(AttrIdx, Kind)) 239 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) 240 return false; 241 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); 242 return true; 243 } 244 if (Attr.isIntAttribute()) { 245 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 246 if (Attrs.hasAttribute(AttrIdx, Kind)) 247 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind))) 248 return false; 249 Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind); 250 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr); 251 return true; 252 } 253 254 llvm_unreachable("Expected enum or string attribute!"); 255 } 256 257 ChangeStatus AbstractAttribute::update(Attributor &A) { 258 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 259 if (getState().isAtFixpoint()) 260 return HasChanged; 261 262 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n"); 263 264 HasChanged = updateImpl(A); 265 266 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this 267 << "\n"); 268 269 return HasChanged; 270 } 271 272 ChangeStatus 273 IRAttributeManifest::manifestAttrs(Attributor &A, IRPosition &IRP, 274 const ArrayRef<Attribute> &DeducedAttrs) { 275 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 276 277 Function *ScopeFn = IRP.getAssociatedFunction(); 278 IRPosition::Kind PK = IRP.getPositionKind(); 279 280 // In the following some generic code that will manifest attributes in 281 // DeducedAttrs if they improve the current IR. Due to the different 282 // annotation positions we use the underlying AttributeList interface. 283 284 AttributeList Attrs; 285 switch (PK) { 286 case IRPosition::IRP_INVALID: 287 case IRPosition::IRP_FLOAT: 288 llvm_unreachable("Cannot manifest at a floating or invalid position!"); 289 case IRPosition::IRP_ARGUMENT: 290 case IRPosition::IRP_FUNCTION: 291 case IRPosition::IRP_RETURNED: 292 Attrs = ScopeFn->getAttributes(); 293 break; 294 case IRPosition::IRP_CALL_SITE: 295 case IRPosition::IRP_CALL_SITE_RETURNED: 296 case IRPosition::IRP_CALL_SITE_ARGUMENT: 297 Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes(); 298 break; 299 } 300 301 LLVMContext &Ctx = IRP.getAnchorValue().getContext(); 302 for (const Attribute &Attr : DeducedAttrs) { 303 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx())) 304 continue; 305 306 HasChanged = ChangeStatus::CHANGED; 307 } 308 309 if (HasChanged == ChangeStatus::UNCHANGED) 310 return HasChanged; 311 312 switch (PK) { 313 case IRPosition::IRP_ARGUMENT: 314 case IRPosition::IRP_FUNCTION: 315 case IRPosition::IRP_RETURNED: 316 ScopeFn->setAttributes(Attrs); 317 break; 318 case IRPosition::IRP_CALL_SITE: 319 case IRPosition::IRP_CALL_SITE_RETURNED: 320 case IRPosition::IRP_CALL_SITE_ARGUMENT: 321 CallSite(&IRP.getAnchorValue()).setAttributes(Attrs); 322 break; 323 case IRPosition::IRP_INVALID: 324 case IRPosition::IRP_FLOAT: 325 break; 326 } 327 328 return HasChanged; 329 } 330 331 const IRPosition IRPosition::EmptyKey(255); 332 const IRPosition IRPosition::TombstoneKey(256); 333 334 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) { 335 IRPositions.emplace_back(IRP); 336 337 ImmutableCallSite ICS(&IRP.getAnchorValue()); 338 switch (IRP.getPositionKind()) { 339 case IRPosition::IRP_INVALID: 340 case IRPosition::IRP_FLOAT: 341 case IRPosition::IRP_FUNCTION: 342 return; 343 case IRPosition::IRP_ARGUMENT: 344 case IRPosition::IRP_RETURNED: 345 IRPositions.emplace_back( 346 IRPosition::function(*IRP.getAssociatedFunction())); 347 return; 348 case IRPosition::IRP_CALL_SITE: 349 assert(ICS && "Expected call site!"); 350 // TODO: We need to look at the operand bundles similar to the redirection 351 // in CallBase. 352 if (!ICS.hasOperandBundles()) 353 if (const Function *Callee = ICS.getCalledFunction()) 354 IRPositions.emplace_back(IRPosition::function(*Callee)); 355 return; 356 case IRPosition::IRP_CALL_SITE_RETURNED: 357 assert(ICS && "Expected call site!"); 358 // TODO: We need to look at the operand bundles similar to the redirection 359 // in CallBase. 360 if (!ICS.hasOperandBundles()) { 361 if (const Function *Callee = ICS.getCalledFunction()) { 362 IRPositions.emplace_back(IRPosition::returned(*Callee)); 363 IRPositions.emplace_back(IRPosition::function(*Callee)); 364 } 365 } 366 IRPositions.emplace_back( 367 IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction()))); 368 return; 369 case IRPosition::IRP_CALL_SITE_ARGUMENT: { 370 int ArgNo = IRP.getArgNo(); 371 assert(ICS && ArgNo >= 0 && "Expected call site!"); 372 // TODO: We need to look at the operand bundles similar to the redirection 373 // in CallBase. 374 if (!ICS.hasOperandBundles()) { 375 const Function *Callee = ICS.getCalledFunction(); 376 if (Callee && Callee->arg_size() > unsigned(ArgNo)) 377 IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo))); 378 if (Callee) 379 IRPositions.emplace_back(IRPosition::function(*Callee)); 380 } 381 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue())); 382 return; 383 } 384 } 385 } 386 387 bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs) const { 388 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) 389 for (Attribute::AttrKind AK : AKs) 390 if (EquivIRP.getAttr(AK).getKindAsEnum() == AK) 391 return true; 392 return false; 393 } 394 395 void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs, 396 SmallVectorImpl<Attribute> &Attrs) const { 397 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) 398 for (Attribute::AttrKind AK : AKs) { 399 const Attribute &Attr = EquivIRP.getAttr(AK); 400 if (Attr.getKindAsEnum() == AK) 401 Attrs.push_back(Attr); 402 } 403 } 404 405 void IRPosition::verify() { 406 switch (KindOrArgNo) { 407 default: 408 assert(KindOrArgNo >= 0 && "Expected argument or call site argument!"); 409 assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) && 410 "Expected call base or argument for positive attribute index!"); 411 if (auto *Arg = dyn_cast<Argument>(AnchorVal)) { 412 assert(Arg->getArgNo() == unsigned(getArgNo()) && 413 "Argument number mismatch!"); 414 assert(Arg == &getAssociatedValue() && "Associated value mismatch!"); 415 } else { 416 auto &CB = cast<CallBase>(*AnchorVal); 417 (void)CB; 418 assert(CB.arg_size() > unsigned(getArgNo()) && 419 "Call site argument number mismatch!"); 420 assert(CB.getArgOperand(getArgNo()) == &getAssociatedValue() && 421 "Associated value mismatch!"); 422 } 423 break; 424 case IRP_INVALID: 425 assert(!AnchorVal && "Expected no value for an invalid position!"); 426 break; 427 case IRP_FLOAT: 428 assert((!isa<CallBase>(&getAssociatedValue()) && 429 !isa<Argument>(&getAssociatedValue())) && 430 "Expected specialized kind for call base and argument values!"); 431 break; 432 case IRP_RETURNED: 433 assert(isa<Function>(AnchorVal) && 434 "Expected function for a 'returned' position!"); 435 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 436 break; 437 case IRP_CALL_SITE_RETURNED: 438 assert((isa<CallBase>(AnchorVal)) && 439 "Expected call base for 'call site returned' position!"); 440 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 441 break; 442 case IRP_CALL_SITE: 443 assert((isa<CallBase>(AnchorVal)) && 444 "Expected call base for 'call site function' position!"); 445 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 446 break; 447 case IRP_FUNCTION: 448 assert(isa<Function>(AnchorVal) && 449 "Expected function for a 'function' position!"); 450 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!"); 451 break; 452 } 453 } 454 455 /// -----------------------NoUnwind Function Attribute-------------------------- 456 457 struct AANoUnwindImpl : AANoUnwind { 458 AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {} 459 460 const std::string getAsStr() const override { 461 return getAssumed() ? "nounwind" : "may-unwind"; 462 } 463 464 /// See AbstractAttribute::updateImpl(...). 465 ChangeStatus updateImpl(Attributor &A) override; 466 }; 467 468 struct AANoUnwindFunction final : public AANoUnwindImpl { 469 AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {} 470 471 /// See AbstractAttribute::trackStatistics() 472 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) } 473 }; 474 475 ChangeStatus AANoUnwindImpl::updateImpl(Attributor &A) { 476 477 // The map from instruction opcodes to those instructions in the function. 478 auto Opcodes = { 479 (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, 480 (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet, 481 (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; 482 483 auto CheckForNoUnwind = [&](Instruction &I) { 484 if (!I.mayThrow()) 485 return true; 486 487 auto *NoUnwindAA = A.getAAFor<AANoUnwind>(*this, IRPosition::value(I)); 488 return NoUnwindAA && NoUnwindAA->isAssumedNoUnwind(); 489 }; 490 491 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes)) 492 return indicatePessimisticFixpoint(); 493 494 return ChangeStatus::UNCHANGED; 495 } 496 497 /// --------------------- Function Return Values ------------------------------- 498 499 /// "Attribute" that collects all potential returned values and the return 500 /// instructions that they arise from. 501 /// 502 /// If there is a unique returned value R, the manifest method will: 503 /// - mark R with the "returned" attribute, if R is an argument. 504 class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { 505 506 /// Mapping of values potentially returned by the associated function to the 507 /// return instructions that might return them. 508 DenseMap<Value *, SmallPtrSet<ReturnInst *, 2>> ReturnedValues; 509 510 SmallPtrSet<CallBase *, 8> UnresolvedCalls; 511 512 /// State flags 513 /// 514 ///{ 515 bool IsFixed; 516 bool IsValidState; 517 ///} 518 519 public: 520 AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {} 521 522 /// See AbstractAttribute::initialize(...). 523 void initialize(Attributor &A) override { 524 // Reset the state. 525 IsFixed = false; 526 IsValidState = true; 527 ReturnedValues.clear(); 528 529 Function *F = getAssociatedFunction(); 530 if (!F || !F->hasExactDefinition()) { 531 indicatePessimisticFixpoint(); 532 return; 533 } 534 535 // The map from instruction opcodes to those instructions in the function. 536 auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); 537 538 // Look through all arguments, if one is marked as returned we are done. 539 for (Argument &Arg : F->args()) { 540 if (Arg.hasReturnedAttr()) { 541 auto &ReturnInstSet = ReturnedValues[&Arg]; 542 for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) 543 ReturnInstSet.insert(cast<ReturnInst>(RI)); 544 545 indicateOptimisticFixpoint(); 546 return; 547 } 548 } 549 } 550 551 /// See AbstractAttribute::manifest(...). 552 ChangeStatus manifest(Attributor &A) override; 553 554 /// See AbstractAttribute::getState(...). 555 AbstractState &getState() override { return *this; } 556 557 /// See AbstractAttribute::getState(...). 558 const AbstractState &getState() const override { return *this; } 559 560 /// See AbstractAttribute::updateImpl(Attributor &A). 561 ChangeStatus updateImpl(Attributor &A) override; 562 563 llvm::iterator_range<iterator> returned_values() override { 564 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); 565 } 566 567 llvm::iterator_range<const_iterator> returned_values() const override { 568 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); 569 } 570 571 const SmallPtrSetImpl<CallBase *> &getUnresolvedCalls() const override { 572 return UnresolvedCalls; 573 } 574 575 /// Return the number of potential return values, -1 if unknown. 576 size_t getNumReturnValues() const override { 577 return isValidState() ? ReturnedValues.size() : -1; 578 } 579 580 /// Return an assumed unique return value if a single candidate is found. If 581 /// there cannot be one, return a nullptr. If it is not clear yet, return the 582 /// Optional::NoneType. 583 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; 584 585 /// See AbstractState::checkForAllReturnedValues(...). 586 bool checkForAllReturnedValuesAndReturnInsts( 587 const function_ref<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> 588 &Pred) const override; 589 590 /// Pretty print the attribute similar to the IR representation. 591 const std::string getAsStr() const override; 592 593 /// See AbstractState::isAtFixpoint(). 594 bool isAtFixpoint() const override { return IsFixed; } 595 596 /// See AbstractState::isValidState(). 597 bool isValidState() const override { return IsValidState; } 598 599 /// See AbstractState::indicateOptimisticFixpoint(...). 600 ChangeStatus indicateOptimisticFixpoint() override { 601 IsFixed = true; 602 IsValidState &= true; 603 return ChangeStatus::UNCHANGED; 604 } 605 606 ChangeStatus indicatePessimisticFixpoint() override { 607 IsFixed = true; 608 IsValidState = false; 609 return ChangeStatus::CHANGED; 610 } 611 }; 612 613 ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { 614 ChangeStatus Changed = ChangeStatus::UNCHANGED; 615 616 // Bookkeeping. 617 assert(isValidState()); 618 STATS_DECLTRACK(KnownReturnValues, FunctionReturn, 619 "Number of function with known return values"); 620 621 // Check if we have an assumed unique return value that we could manifest. 622 Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); 623 624 if (!UniqueRV.hasValue() || !UniqueRV.getValue()) 625 return Changed; 626 627 // Bookkeeping. 628 STATS_DECLTRACK(UniqueReturnValue, FunctionReturn, 629 "Number of function with unique return"); 630 631 // If the assumed unique return value is an argument, annotate it. 632 if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { 633 getIRPosition() = IRPosition::argument(*UniqueRVArg); 634 Changed = IRAttribute::manifest(A) | Changed; 635 } 636 637 return Changed; 638 } 639 640 const std::string AAReturnedValuesImpl::getAsStr() const { 641 return (isAtFixpoint() ? "returns(#" : "may-return(#") + 642 (isValidState() ? std::to_string(getNumReturnValues()) : "?") + 643 ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]"; 644 } 645 646 Optional<Value *> 647 AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { 648 // If checkForAllReturnedValues provides a unique value, ignoring potential 649 // undef values that can also be present, it is assumed to be the actual 650 // return value and forwarded to the caller of this method. If there are 651 // multiple, a nullptr is returned indicating there cannot be a unique 652 // returned value. 653 Optional<Value *> UniqueRV; 654 655 auto Pred = [&](Value &RV) -> bool { 656 // If we found a second returned value and neither the current nor the saved 657 // one is an undef, there is no unique returned value. Undefs are special 658 // since we can pretend they have any value. 659 if (UniqueRV.hasValue() && UniqueRV != &RV && 660 !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) { 661 UniqueRV = nullptr; 662 return false; 663 } 664 665 // Do not overwrite a value with an undef. 666 if (!UniqueRV.hasValue() || !isa<UndefValue>(RV)) 667 UniqueRV = &RV; 668 669 return true; 670 }; 671 672 if (!A.checkForAllReturnedValues(Pred, *this)) 673 UniqueRV = nullptr; 674 675 return UniqueRV; 676 } 677 678 bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( 679 const function_ref<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> 680 &Pred) const { 681 if (!isValidState()) 682 return false; 683 684 // Check all returned values but ignore call sites as long as we have not 685 // encountered an overdefined one during an update. 686 for (auto &It : ReturnedValues) { 687 Value *RV = It.first; 688 const SmallPtrSetImpl<ReturnInst *> &RetInsts = It.second; 689 690 CallBase *CB = dyn_cast<CallBase>(RV); 691 if (CB && !UnresolvedCalls.count(CB)) 692 continue; 693 694 if (!Pred(*RV, RetInsts)) 695 return false; 696 } 697 698 return true; 699 } 700 701 ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { 702 size_t NumUnresolvedCalls = UnresolvedCalls.size(); 703 bool Changed = false; 704 705 // State used in the value traversals starting in returned values. 706 struct RVState { 707 // The map in which we collect return values -> return instrs. 708 decltype(ReturnedValues) &RetValsMap; 709 // The flag to indicate a change. 710 bool Changed; 711 // The return instrs we come from. 712 SmallPtrSet<ReturnInst *, 2> RetInsts; 713 }; 714 715 // Callback for a leaf value returned by the associated function. 716 auto VisitValueCB = [](Value &Val, RVState &RVS, bool) { 717 auto Size = RVS.RetValsMap[&Val].size(); 718 RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end()); 719 bool Inserted = RVS.RetValsMap[&Val].size() != Size; 720 RVS.Changed |= Inserted; 721 LLVM_DEBUG({ 722 if (Inserted) 723 dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val 724 << " => " << RVS.RetInsts.size() << "\n"; 725 }); 726 }; 727 728 // Helper method to invoke the generic value traversal. 729 auto VisitReturnedValue = [&](Value &RV, RVState &RVS) { 730 IRPosition RetValPos = IRPosition::value(RV); 731 return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this, 732 RVS, VisitValueCB); 733 }; 734 735 // Callback for all "return intructions" live in the associated function. 736 auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) { 737 ReturnInst &Ret = cast<ReturnInst>(I); 738 RVState RVS({ReturnedValues, false, {}}); 739 RVS.RetInsts.insert(&Ret); 740 Changed |= RVS.Changed; 741 return VisitReturnedValue(*Ret.getReturnValue(), RVS); 742 }; 743 744 // Start by discovering returned values from all live returned instructions in 745 // the associated function. 746 if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret})) 747 return indicatePessimisticFixpoint(); 748 749 // Once returned values "directly" present in the code are handled we try to 750 // resolve returned calls. 751 decltype(ReturnedValues) NewRVsMap; 752 for (auto &It : ReturnedValues) { 753 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first 754 << " by #" << It.second.size() << " RIs\n"); 755 CallBase *CB = dyn_cast<CallBase>(It.first); 756 if (!CB || UnresolvedCalls.count(CB)) 757 continue; 758 759 const auto *RetValAAPtr = 760 A.getAAFor<AAReturnedValues>(*this, IRPosition::callsite_function(*CB)); 761 762 // Skip dead ends, thus if we do not know anything about the returned 763 // call we mark it as unresolved and it will stay that way. 764 if (!RetValAAPtr || !RetValAAPtr->getState().isValidState()) { 765 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB 766 << "\n"); 767 UnresolvedCalls.insert(CB); 768 continue; 769 } 770 771 const auto &RetValAA = *RetValAAPtr; 772 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: " 773 << static_cast<const AbstractAttribute &>(RetValAA) 774 << "\n"); 775 776 // If we know something but not everyting about the returned values, keep 777 // track of that too. Hence, remember transitively unresolved calls. 778 UnresolvedCalls.insert(RetValAA.getUnresolvedCalls().begin(), 779 RetValAA.getUnresolvedCalls().end()); 780 781 // Now track transitively returned values. 782 for (auto &RetValAAIt : RetValAA.returned_values()) { 783 Value *RetVal = RetValAAIt.first; 784 if (Argument *Arg = dyn_cast<Argument>(RetVal)) { 785 // Arguments are mapped to call site operands and we begin the traversal 786 // again. 787 RVState RVS({NewRVsMap, false, RetValAAIt.second}); 788 VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS); 789 continue; 790 } else if (isa<CallBase>(RetVal)) { 791 // Call sites are resolved by the callee attribute over time, no need to 792 // do anything for us. 793 continue; 794 } else if (isa<Constant>(RetVal)) { 795 // Constants are valid everywhere, we can simply take them. 796 NewRVsMap[RetVal].insert(It.second.begin(), It.second.end()); 797 continue; 798 } 799 // Anything that did not fit in the above categories cannot be resolved, 800 // mark the call as unresolved. 801 LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value " 802 "cannot be translated: " 803 << *RetVal << "\n"); 804 UnresolvedCalls.insert(CB); 805 } 806 } 807 808 // To avoid modifications to the ReturnedValues map while we iterate over it 809 // we kept record of potential new entries in a copy map, NewRVsMap. 810 for (auto &It : NewRVsMap) { 811 assert(!It.second.empty() && "Entry does not add anything."); 812 auto &ReturnInsts = ReturnedValues[It.first]; 813 for (ReturnInst *RI : It.second) 814 if (ReturnInsts.insert(RI).second) { 815 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value " 816 << *It.first << " => " << *RI << "\n"); 817 Changed = true; 818 } 819 } 820 821 Changed |= (NumUnresolvedCalls != UnresolvedCalls.size()); 822 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; 823 } 824 825 struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { 826 AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {} 827 828 /// See AbstractAttribute::trackStatistics() 829 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) } 830 }; 831 832 /// ------------------------ NoSync Function Attribute ------------------------- 833 834 struct AANoSyncImpl : AANoSync { 835 AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {} 836 837 const std::string getAsStr() const override { 838 return getAssumed() ? "nosync" : "may-sync"; 839 } 840 841 /// See AbstractAttribute::updateImpl(...). 842 ChangeStatus updateImpl(Attributor &A) override; 843 844 /// Helper function used to determine whether an instruction is non-relaxed 845 /// atomic. In other words, if an atomic instruction does not have unordered 846 /// or monotonic ordering 847 static bool isNonRelaxedAtomic(Instruction *I); 848 849 /// Helper function used to determine whether an instruction is volatile. 850 static bool isVolatile(Instruction *I); 851 852 /// Helper function uset to check if intrinsic is volatile (memcpy, memmove, 853 /// memset). 854 static bool isNoSyncIntrinsic(Instruction *I); 855 }; 856 857 struct AANoSyncFunction final : public AANoSyncImpl { 858 AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {} 859 860 /// See AbstractAttribute::trackStatistics() 861 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) } 862 }; 863 864 bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) { 865 if (!I->isAtomic()) 866 return false; 867 868 AtomicOrdering Ordering; 869 switch (I->getOpcode()) { 870 case Instruction::AtomicRMW: 871 Ordering = cast<AtomicRMWInst>(I)->getOrdering(); 872 break; 873 case Instruction::Store: 874 Ordering = cast<StoreInst>(I)->getOrdering(); 875 break; 876 case Instruction::Load: 877 Ordering = cast<LoadInst>(I)->getOrdering(); 878 break; 879 case Instruction::Fence: { 880 auto *FI = cast<FenceInst>(I); 881 if (FI->getSyncScopeID() == SyncScope::SingleThread) 882 return false; 883 Ordering = FI->getOrdering(); 884 break; 885 } 886 case Instruction::AtomicCmpXchg: { 887 AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering(); 888 AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering(); 889 // Only if both are relaxed, than it can be treated as relaxed. 890 // Otherwise it is non-relaxed. 891 if (Success != AtomicOrdering::Unordered && 892 Success != AtomicOrdering::Monotonic) 893 return true; 894 if (Failure != AtomicOrdering::Unordered && 895 Failure != AtomicOrdering::Monotonic) 896 return true; 897 return false; 898 } 899 default: 900 llvm_unreachable( 901 "New atomic operations need to be known in the attributor."); 902 } 903 904 // Relaxed. 905 if (Ordering == AtomicOrdering::Unordered || 906 Ordering == AtomicOrdering::Monotonic) 907 return false; 908 return true; 909 } 910 911 /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics. 912 /// FIXME: We should ipmrove the handling of intrinsics. 913 bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) { 914 if (auto *II = dyn_cast<IntrinsicInst>(I)) { 915 switch (II->getIntrinsicID()) { 916 /// Element wise atomic memory intrinsics are can only be unordered, 917 /// therefore nosync. 918 case Intrinsic::memset_element_unordered_atomic: 919 case Intrinsic::memmove_element_unordered_atomic: 920 case Intrinsic::memcpy_element_unordered_atomic: 921 return true; 922 case Intrinsic::memset: 923 case Intrinsic::memmove: 924 case Intrinsic::memcpy: 925 if (!cast<MemIntrinsic>(II)->isVolatile()) 926 return true; 927 return false; 928 default: 929 return false; 930 } 931 } 932 return false; 933 } 934 935 bool AANoSyncImpl::isVolatile(Instruction *I) { 936 assert(!ImmutableCallSite(I) && !isa<CallBase>(I) && 937 "Calls should not be checked here"); 938 939 switch (I->getOpcode()) { 940 case Instruction::AtomicRMW: 941 return cast<AtomicRMWInst>(I)->isVolatile(); 942 case Instruction::Store: 943 return cast<StoreInst>(I)->isVolatile(); 944 case Instruction::Load: 945 return cast<LoadInst>(I)->isVolatile(); 946 case Instruction::AtomicCmpXchg: 947 return cast<AtomicCmpXchgInst>(I)->isVolatile(); 948 default: 949 return false; 950 } 951 } 952 953 ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { 954 955 auto CheckRWInstForNoSync = [&](Instruction &I) { 956 /// We are looking for volatile instructions or Non-Relaxed atomics. 957 /// FIXME: We should ipmrove the handling of intrinsics. 958 959 if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I)) 960 return true; 961 962 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) { 963 if (ICS.hasFnAttr(Attribute::NoSync)) 964 return true; 965 966 auto *NoSyncAA = 967 A.getAAFor<AANoSyncImpl>(*this, IRPosition::callsite_function(ICS)); 968 if (NoSyncAA && NoSyncAA->isAssumedNoSync()) 969 return true; 970 return false; 971 } 972 973 if (!isVolatile(&I) && !isNonRelaxedAtomic(&I)) 974 return true; 975 976 return false; 977 }; 978 979 auto CheckForNoSync = [&](Instruction &I) { 980 // At this point we handled all read/write effects and they are all 981 // nosync, so they can be skipped. 982 if (I.mayReadOrWriteMemory()) 983 return true; 984 985 // non-convergent and readnone imply nosync. 986 return !ImmutableCallSite(&I).isConvergent(); 987 }; 988 989 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) || 990 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this)) 991 return indicatePessimisticFixpoint(); 992 993 return ChangeStatus::UNCHANGED; 994 } 995 996 /// ------------------------ No-Free Attributes ---------------------------- 997 998 struct AANoFreeImpl : public AANoFree { 999 AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {} 1000 1001 /// See AbstractAttribute::getAsStr(). 1002 const std::string getAsStr() const override { 1003 return getAssumed() ? "nofree" : "may-free"; 1004 } 1005 1006 /// See AbstractAttribute::updateImpl(...). 1007 ChangeStatus updateImpl(Attributor &A) override; 1008 }; 1009 1010 struct AANoFreeFunction final : public AANoFreeImpl { 1011 AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {} 1012 1013 /// See AbstractAttribute::trackStatistics() 1014 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) } 1015 }; 1016 1017 ChangeStatus AANoFreeImpl::updateImpl(Attributor &A) { 1018 1019 auto CheckForNoFree = [&](Instruction &I) { 1020 ImmutableCallSite ICS(&I); 1021 if (ICS.hasFnAttr(Attribute::NoFree)) 1022 return true; 1023 1024 auto *NoFreeAA = 1025 A.getAAFor<AANoFreeImpl>(*this, IRPosition::callsite_function(ICS)); 1026 return NoFreeAA && NoFreeAA->isAssumedNoFree(); 1027 }; 1028 1029 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this)) 1030 return indicatePessimisticFixpoint(); 1031 return ChangeStatus::UNCHANGED; 1032 } 1033 1034 /// ------------------------ NonNull Argument Attribute ------------------------ 1035 struct AANonNullImpl : AANonNull { 1036 AANonNullImpl(const IRPosition &IRP) : AANonNull(IRP) {} 1037 1038 /// See AbstractAttribute::getAsStr(). 1039 const std::string getAsStr() const override { 1040 return getAssumed() ? "nonnull" : "may-null"; 1041 } 1042 1043 /// See AbstractAttribute::initialize(...). 1044 void initialize(Attributor &A) override { 1045 if (hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) 1046 indicateOptimisticFixpoint(); 1047 } 1048 1049 /// Generate a predicate that checks if a given value is assumed nonnull. 1050 /// The generated function returns true if a value satisfies any of 1051 /// following conditions. 1052 /// (i) A value is known nonZero(=nonnull). 1053 /// (ii) A value is associated with AANonNull and its isAssumedNonNull() is 1054 /// true. 1055 std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> 1056 generatePredicate(Attributor &); 1057 }; 1058 1059 std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> 1060 AANonNullImpl::generatePredicate(Attributor &A) { 1061 // FIXME: The `AAReturnedValues` should provide the predicate with the 1062 // `ReturnInst` vector as well such that we can use the control flow sensitive 1063 // version of `isKnownNonZero`. This should fix `test11` in 1064 // `test/Transforms/FunctionAttrs/nonnull.ll` 1065 1066 std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> Pred = 1067 [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &RetInsts) -> bool { 1068 if (isKnownNonZero(&RV, A.getDataLayout())) 1069 return true; 1070 1071 if (ImmutableCallSite ICS = ImmutableCallSite(&RV)) 1072 if (ICS.hasRetAttr(Attribute::NonNull)) 1073 return true; 1074 1075 auto *NonNullAA = A.getAAFor<AANonNull>(*this, IRPosition::value(RV)); 1076 return (NonNullAA && NonNullAA->isAssumedNonNull()); 1077 }; 1078 1079 return Pred; 1080 } 1081 1082 /// NonNull attribute for function return value. 1083 struct AANonNullReturned final : AANonNullImpl { 1084 AANonNullReturned(const IRPosition &IRP) : AANonNullImpl(IRP) {} 1085 1086 /// See AbstractAttribute::updateImpl(...). 1087 ChangeStatus updateImpl(Attributor &A) override; 1088 1089 /// See AbstractAttribute::trackStatistics() 1090 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) } 1091 }; 1092 1093 ChangeStatus AANonNullReturned::updateImpl(Attributor &A) { 1094 1095 std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> Pred = 1096 this->generatePredicate(A); 1097 1098 if (!A.checkForAllReturnedValuesAndReturnInsts(Pred, *this)) 1099 return indicatePessimisticFixpoint(); 1100 return ChangeStatus::UNCHANGED; 1101 } 1102 1103 /// NonNull attribute for function argument. 1104 struct AANonNullArgument final : AANonNullImpl { 1105 AANonNullArgument(const IRPosition &IRP) : AANonNullImpl(IRP) {} 1106 1107 /// See AbstractAttribute::updateImpl(...). 1108 ChangeStatus updateImpl(Attributor &A) override; 1109 1110 /// See AbstractAttribute::trackStatistics() 1111 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) } 1112 }; 1113 1114 /// NonNull attribute for a call site argument. 1115 struct AANonNullCallSiteArgument final : AANonNullImpl { 1116 AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullImpl(IRP) {} 1117 1118 /// See AbstractAttribute::initialize(...). 1119 void initialize(Attributor &A) override { 1120 AANonNullImpl::initialize(A); 1121 if (!isKnownNonNull() && 1122 isKnownNonZero(&getAssociatedValue(), A.getDataLayout())) 1123 indicateOptimisticFixpoint(); 1124 } 1125 1126 /// See AbstractAttribute::updateImpl(Attributor &A). 1127 ChangeStatus updateImpl(Attributor &A) override; 1128 1129 /// See AbstractAttribute::trackStatistics() 1130 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) } 1131 }; 1132 1133 ChangeStatus AANonNullArgument::updateImpl(Attributor &A) { 1134 unsigned ArgNo = getArgNo(); 1135 1136 // Callback function 1137 std::function<bool(CallSite)> CallSiteCheck = [&](CallSite CS) { 1138 assert(CS && "Sanity check: Call site was not initialized properly!"); 1139 1140 IRPosition CSArgPos = IRPosition::callsite_argument(CS, ArgNo); 1141 if (CSArgPos.hasAttr({Attribute::NonNull, Attribute::Dereferenceable})) 1142 return true; 1143 1144 // Check that NonNullAA is AANonNullCallSiteArgument. 1145 if (auto *NonNullAA = A.getAAFor<AANonNullImpl>(*this, CSArgPos)) { 1146 ImmutableCallSite ICS(&NonNullAA->getAnchorValue()); 1147 if (ICS && CS.getInstruction() == ICS.getInstruction()) 1148 return NonNullAA->isAssumedNonNull(); 1149 return false; 1150 } 1151 1152 Value *V = CS.getArgOperand(ArgNo); 1153 if (isKnownNonZero(V, A.getDataLayout())) 1154 return true; 1155 1156 return false; 1157 }; 1158 if (!A.checkForAllCallSites(CallSiteCheck, *this, true)) 1159 return indicatePessimisticFixpoint(); 1160 return ChangeStatus::UNCHANGED; 1161 } 1162 1163 ChangeStatus AANonNullCallSiteArgument::updateImpl(Attributor &A) { 1164 // NOTE: Never look at the argument of the callee in this method. 1165 // If we do this, "nonnull" is always deduced because of the assumption. 1166 1167 Value &V = getAssociatedValue(); 1168 auto *NonNullAA = A.getAAFor<AANonNull>(*this, IRPosition::value(V)); 1169 1170 if (!NonNullAA || !NonNullAA->isAssumedNonNull()) 1171 return indicatePessimisticFixpoint(); 1172 1173 return ChangeStatus::UNCHANGED; 1174 } 1175 1176 /// ------------------------ Will-Return Attributes ---------------------------- 1177 1178 struct AAWillReturnImpl : public AAWillReturn { 1179 AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {} 1180 1181 /// See AbstractAttribute::getAsStr() 1182 const std::string getAsStr() const override { 1183 return getAssumed() ? "willreturn" : "may-noreturn"; 1184 } 1185 }; 1186 1187 struct AAWillReturnFunction final : AAWillReturnImpl { 1188 AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {} 1189 1190 /// See AbstractAttribute::initialize(...). 1191 void initialize(Attributor &A) override; 1192 1193 /// See AbstractAttribute::updateImpl(...). 1194 ChangeStatus updateImpl(Attributor &A) override; 1195 1196 /// See AbstractAttribute::trackStatistics() 1197 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) } 1198 }; 1199 1200 // Helper function that checks whether a function has any cycle. 1201 // TODO: Replace with more efficent code 1202 bool containsCycle(Function &F) { 1203 SmallPtrSet<BasicBlock *, 32> Visited; 1204 1205 // Traverse BB by dfs and check whether successor is already visited. 1206 for (BasicBlock *BB : depth_first(&F)) { 1207 Visited.insert(BB); 1208 for (auto *SuccBB : successors(BB)) { 1209 if (Visited.count(SuccBB)) 1210 return true; 1211 } 1212 } 1213 return false; 1214 } 1215 1216 // Helper function that checks the function have a loop which might become an 1217 // endless loop 1218 // FIXME: Any cycle is regarded as endless loop for now. 1219 // We have to allow some patterns. 1220 bool containsPossiblyEndlessLoop(Function &F) { return containsCycle(F); } 1221 1222 void AAWillReturnFunction::initialize(Attributor &A) { 1223 Function &F = *getAnchorScope(); 1224 1225 if (containsPossiblyEndlessLoop(F)) 1226 indicatePessimisticFixpoint(); 1227 } 1228 1229 ChangeStatus AAWillReturnFunction::updateImpl(Attributor &A) { 1230 // The map from instruction opcodes to those instructions in the function. 1231 1232 auto CheckForWillReturn = [&](Instruction &I) { 1233 ImmutableCallSite ICS(&I); 1234 if (ICS.hasFnAttr(Attribute::WillReturn)) 1235 return true; 1236 1237 IRPosition IPos = IRPosition::callsite_function(ICS); 1238 auto *WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos); 1239 if (!WillReturnAA || !WillReturnAA->isAssumedWillReturn()) 1240 return false; 1241 1242 // FIXME: Prohibit any recursion for now. 1243 if (ICS.hasFnAttr(Attribute::NoRecurse)) 1244 return true; 1245 1246 auto *NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos); 1247 return NoRecurseAA && NoRecurseAA->isAssumedNoRecurse(); 1248 }; 1249 1250 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this)) 1251 return indicatePessimisticFixpoint(); 1252 1253 return ChangeStatus::UNCHANGED; 1254 } 1255 1256 /// ------------------------ NoAlias Argument Attribute ------------------------ 1257 1258 struct AANoAliasImpl : AANoAlias { 1259 AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {} 1260 1261 const std::string getAsStr() const override { 1262 return getAssumed() ? "noalias" : "may-alias"; 1263 } 1264 }; 1265 1266 /// NoAlias attribute for function return value. 1267 struct AANoAliasReturned final : AANoAliasImpl { 1268 AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {} 1269 1270 /// See AbstractAttriubute::initialize(...). 1271 void initialize(Attributor &A) override { 1272 Function &F = *getAnchorScope(); 1273 1274 // Already noalias. 1275 if (F.returnDoesNotAlias()) { 1276 indicateOptimisticFixpoint(); 1277 return; 1278 } 1279 } 1280 1281 /// See AbstractAttribute::updateImpl(...). 1282 virtual ChangeStatus updateImpl(Attributor &A) override; 1283 1284 /// See AbstractAttribute::trackStatistics() 1285 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) } 1286 }; 1287 1288 ChangeStatus AANoAliasReturned::updateImpl(Attributor &A) { 1289 1290 auto CheckReturnValue = [&](Value &RV) -> bool { 1291 if (Constant *C = dyn_cast<Constant>(&RV)) 1292 if (C->isNullValue() || isa<UndefValue>(C)) 1293 return true; 1294 1295 /// For now, we can only deduce noalias if we have call sites. 1296 /// FIXME: add more support. 1297 ImmutableCallSite ICS(&RV); 1298 if (!ICS) 1299 return false; 1300 1301 if (!ICS.returnDoesNotAlias()) { 1302 auto *NoAliasAA = 1303 A.getAAFor<AANoAlias>(*this, IRPosition::callsite_returned(ICS)); 1304 if (!NoAliasAA || !NoAliasAA->isAssumedNoAlias()) 1305 return false; 1306 } 1307 1308 /// FIXME: We can improve capture check in two ways: 1309 /// 1. Use the AANoCapture facilities. 1310 /// 2. Use the location of return insts for escape queries. 1311 if (PointerMayBeCaptured(&RV, /* ReturnCaptures */ false, 1312 /* StoreCaptures */ true)) 1313 return false; 1314 1315 return true; 1316 }; 1317 1318 if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) 1319 return indicatePessimisticFixpoint(); 1320 1321 return ChangeStatus::UNCHANGED; 1322 } 1323 1324 /// -------------------AAIsDead Function Attribute----------------------- 1325 1326 struct AAIsDeadImpl : public AAIsDead { 1327 AAIsDeadImpl(const IRPosition &IRP) : AAIsDead(IRP) {} 1328 1329 void initialize(Attributor &A) override { 1330 const Function &F = *getAnchorScope(); 1331 1332 ToBeExploredPaths.insert(&(F.getEntryBlock().front())); 1333 AssumedLiveBlocks.insert(&(F.getEntryBlock())); 1334 for (size_t i = 0; i < ToBeExploredPaths.size(); ++i) 1335 if (const Instruction *NextNoReturnI = 1336 findNextNoReturn(A, ToBeExploredPaths[i])) 1337 NoReturnCalls.insert(NextNoReturnI); 1338 } 1339 1340 /// Find the next assumed noreturn instruction in the block of \p I starting 1341 /// from, thus including, \p I. 1342 /// 1343 /// The caller is responsible to monitor the ToBeExploredPaths set as new 1344 /// instructions discovered in other basic block will be placed in there. 1345 /// 1346 /// \returns The next assumed noreturn instructions in the block of \p I 1347 /// starting from, thus including, \p I. 1348 const Instruction *findNextNoReturn(Attributor &A, const Instruction *I); 1349 1350 /// See AbstractAttribute::getAsStr(). 1351 const std::string getAsStr() const override { 1352 return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + 1353 std::to_string(getAnchorScope()->size()) + "][#NRI " + 1354 std::to_string(NoReturnCalls.size()) + "]"; 1355 } 1356 1357 /// See AbstractAttribute::manifest(...). 1358 ChangeStatus manifest(Attributor &A) override { 1359 assert(getState().isValidState() && 1360 "Attempted to manifest an invalid state!"); 1361 1362 ChangeStatus HasChanged = ChangeStatus::UNCHANGED; 1363 const Function &F = *getAssociatedFunction(); 1364 1365 // Flag to determine if we can change an invoke to a call assuming the 1366 // callee is nounwind. This is not possible if the personality of the 1367 // function allows to catch asynchronous exceptions. 1368 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); 1369 1370 for (const Instruction *NRC : NoReturnCalls) { 1371 Instruction *I = const_cast<Instruction *>(NRC); 1372 BasicBlock *BB = I->getParent(); 1373 Instruction *SplitPos = I->getNextNode(); 1374 1375 if (auto *II = dyn_cast<InvokeInst>(I)) { 1376 // If we keep the invoke the split position is at the beginning of the 1377 // normal desitination block (it invokes a noreturn function after all). 1378 BasicBlock *NormalDestBB = II->getNormalDest(); 1379 SplitPos = &NormalDestBB->front(); 1380 1381 /// Invoke is replaced with a call and unreachable is placed after it if 1382 /// the callee is nounwind and noreturn. Otherwise, we keep the invoke 1383 /// and only place an unreachable in the normal successor. 1384 if (Invoke2CallAllowed) { 1385 if (Function *Callee = II->getCalledFunction()) { 1386 auto *AANoUnw = 1387 A.getAAFor<AANoUnwind>(*this, IRPosition::function(*Callee)); 1388 if (Callee->hasFnAttribute(Attribute::NoUnwind) || 1389 (AANoUnw && AANoUnw->isAssumedNoUnwind())) { 1390 LLVM_DEBUG(dbgs() 1391 << "[AAIsDead] Replace invoke with call inst\n"); 1392 // We do not need an invoke (II) but instead want a call followed 1393 // by an unreachable. However, we do not remove II as other 1394 // abstract attributes might have it cached as part of their 1395 // results. Given that we modify the CFG anyway, we simply keep II 1396 // around but in a new dead block. To avoid II being live through 1397 // a different edge we have to ensure the block we place it in is 1398 // only reached from the current block of II and then not reached 1399 // at all when we insert the unreachable. 1400 SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c"); 1401 CallInst *CI = createCallMatchingInvoke(II); 1402 CI->insertBefore(II); 1403 CI->takeName(II); 1404 II->replaceAllUsesWith(CI); 1405 SplitPos = CI->getNextNode(); 1406 } 1407 } 1408 } 1409 } 1410 1411 BB = SplitPos->getParent(); 1412 SplitBlock(BB, SplitPos); 1413 changeToUnreachable(BB->getTerminator(), /* UseLLVMTrap */ false); 1414 HasChanged = ChangeStatus::CHANGED; 1415 } 1416 1417 return HasChanged; 1418 } 1419 1420 /// See AbstractAttribute::updateImpl(...). 1421 ChangeStatus updateImpl(Attributor &A) override; 1422 1423 /// See AAIsDead::isAssumedDead(BasicBlock *). 1424 bool isAssumedDead(const BasicBlock *BB) const override { 1425 assert(BB->getParent() == getAnchorScope() && 1426 "BB must be in the same anchor scope function."); 1427 1428 if (!getAssumed()) 1429 return false; 1430 return !AssumedLiveBlocks.count(BB); 1431 } 1432 1433 /// See AAIsDead::isKnownDead(BasicBlock *). 1434 bool isKnownDead(const BasicBlock *BB) const override { 1435 return getKnown() && isAssumedDead(BB); 1436 } 1437 1438 /// See AAIsDead::isAssumed(Instruction *I). 1439 bool isAssumedDead(const Instruction *I) const override { 1440 assert(I->getParent()->getParent() == getAnchorScope() && 1441 "Instruction must be in the same anchor scope function."); 1442 1443 if (!getAssumed()) 1444 return false; 1445 1446 // If it is not in AssumedLiveBlocks then it for sure dead. 1447 // Otherwise, it can still be after noreturn call in a live block. 1448 if (!AssumedLiveBlocks.count(I->getParent())) 1449 return true; 1450 1451 // If it is not after a noreturn call, than it is live. 1452 return isAfterNoReturn(I); 1453 } 1454 1455 /// See AAIsDead::isKnownDead(Instruction *I). 1456 bool isKnownDead(const Instruction *I) const override { 1457 return getKnown() && isAssumedDead(I); 1458 } 1459 1460 /// Check if instruction is after noreturn call, in other words, assumed dead. 1461 bool isAfterNoReturn(const Instruction *I) const; 1462 1463 /// Determine if \p F might catch asynchronous exceptions. 1464 static bool mayCatchAsynchronousExceptions(const Function &F) { 1465 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); 1466 } 1467 1468 /// Collection of to be explored paths. 1469 SmallSetVector<const Instruction *, 8> ToBeExploredPaths; 1470 1471 /// Collection of all assumed live BasicBlocks. 1472 DenseSet<const BasicBlock *> AssumedLiveBlocks; 1473 1474 /// Collection of calls with noreturn attribute, assumed or knwon. 1475 SmallSetVector<const Instruction *, 4> NoReturnCalls; 1476 }; 1477 1478 struct AAIsDeadFunction final : public AAIsDeadImpl { 1479 AAIsDeadFunction(const IRPosition &IRP) : AAIsDeadImpl(IRP) {} 1480 1481 /// See AbstractAttribute::trackStatistics() 1482 void trackStatistics() const override { 1483 STATS_DECL(DeadBlocks, Function, 1484 "Number of basic blocks classified as dead"); 1485 BUILD_STAT_NAME(DeadBlocks, Function) += 1486 getAnchorScope()->size() - AssumedLiveBlocks.size(); 1487 STATS_DECL(PartiallyDeadBlocks, Function, 1488 "Number of basic blocks classified as partially dead"); 1489 BUILD_STAT_NAME(PartiallyDeadBlocks, Function) += NoReturnCalls.size(); 1490 } 1491 }; 1492 1493 bool AAIsDeadImpl::isAfterNoReturn(const Instruction *I) const { 1494 const Instruction *PrevI = I->getPrevNode(); 1495 while (PrevI) { 1496 if (NoReturnCalls.count(PrevI)) 1497 return true; 1498 PrevI = PrevI->getPrevNode(); 1499 } 1500 return false; 1501 } 1502 1503 const Instruction *AAIsDeadImpl::findNextNoReturn(Attributor &A, 1504 const Instruction *I) { 1505 const BasicBlock *BB = I->getParent(); 1506 const Function &F = *BB->getParent(); 1507 1508 // Flag to determine if we can change an invoke to a call assuming the callee 1509 // is nounwind. This is not possible if the personality of the function allows 1510 // to catch asynchronous exceptions. 1511 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); 1512 1513 // TODO: We should have a function that determines if an "edge" is dead. 1514 // Edges could be from an instruction to the next or from a terminator 1515 // to the successor. For now, we need to special case the unwind block 1516 // of InvokeInst below. 1517 1518 while (I) { 1519 ImmutableCallSite ICS(I); 1520 1521 if (ICS) { 1522 const IRPosition &IPos = IRPosition::callsite_function(ICS); 1523 // Regarless of the no-return property of an invoke instruction we only 1524 // learn that the regular successor is not reachable through this 1525 // instruction but the unwind block might still be. 1526 if (auto *Invoke = dyn_cast<InvokeInst>(I)) { 1527 // Use nounwind to justify the unwind block is dead as well. 1528 auto *AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos); 1529 if (!Invoke2CallAllowed || 1530 (!AANoUnw || !AANoUnw->isAssumedNoUnwind())) { 1531 AssumedLiveBlocks.insert(Invoke->getUnwindDest()); 1532 ToBeExploredPaths.insert(&Invoke->getUnwindDest()->front()); 1533 } 1534 } 1535 1536 auto *NoReturnAA = A.getAAFor<AANoReturn>(*this, IPos); 1537 if (ICS.hasFnAttr(Attribute::NoReturn) || 1538 (NoReturnAA && NoReturnAA->isAssumedNoReturn())) 1539 return I; 1540 } 1541 1542 I = I->getNextNode(); 1543 } 1544 1545 // get new paths (reachable blocks). 1546 for (const BasicBlock *SuccBB : successors(BB)) { 1547 AssumedLiveBlocks.insert(SuccBB); 1548 ToBeExploredPaths.insert(&SuccBB->front()); 1549 } 1550 1551 // No noreturn instruction found. 1552 return nullptr; 1553 } 1554 1555 ChangeStatus AAIsDeadImpl::updateImpl(Attributor &A) { 1556 // Temporary collection to iterate over existing noreturn instructions. This 1557 // will alow easier modification of NoReturnCalls collection 1558 SmallVector<const Instruction *, 8> NoReturnChanged; 1559 ChangeStatus Status = ChangeStatus::UNCHANGED; 1560 1561 for (const Instruction *I : NoReturnCalls) 1562 NoReturnChanged.push_back(I); 1563 1564 for (const Instruction *I : NoReturnChanged) { 1565 size_t Size = ToBeExploredPaths.size(); 1566 1567 const Instruction *NextNoReturnI = findNextNoReturn(A, I); 1568 if (NextNoReturnI != I) { 1569 Status = ChangeStatus::CHANGED; 1570 NoReturnCalls.remove(I); 1571 if (NextNoReturnI) 1572 NoReturnCalls.insert(NextNoReturnI); 1573 } 1574 1575 // Explore new paths. 1576 while (Size != ToBeExploredPaths.size()) { 1577 Status = ChangeStatus::CHANGED; 1578 if (const Instruction *NextNoReturnI = 1579 findNextNoReturn(A, ToBeExploredPaths[Size++])) 1580 NoReturnCalls.insert(NextNoReturnI); 1581 } 1582 } 1583 1584 LLVM_DEBUG(dbgs() << "[AAIsDead] AssumedLiveBlocks: " 1585 << AssumedLiveBlocks.size() << " Total number of blocks: " 1586 << getAnchorScope()->size() << "\n"); 1587 1588 // If we know everything is live there is no need to query for liveness. 1589 if (NoReturnCalls.empty() && 1590 getAnchorScope()->size() == AssumedLiveBlocks.size()) { 1591 // Indicating a pessimistic fixpoint will cause the state to be "invalid" 1592 // which will cause the Attributor to not return the AAIsDead on request, 1593 // which will prevent us from querying isAssumedDead(). 1594 indicatePessimisticFixpoint(); 1595 assert(!isValidState() && "Expected an invalid state!"); 1596 } 1597 1598 return Status; 1599 } 1600 1601 /// -------------------- Dereferenceable Argument Attribute -------------------- 1602 1603 struct DerefState : AbstractState { 1604 1605 /// State representing for dereferenceable bytes. 1606 IntegerState DerefBytesState; 1607 1608 /// State representing that whether the value is globaly dereferenceable. 1609 BooleanState GlobalState; 1610 1611 /// See AbstractState::isValidState() 1612 bool isValidState() const override { return DerefBytesState.isValidState(); } 1613 1614 /// See AbstractState::isAtFixpoint() 1615 bool isAtFixpoint() const override { 1616 return !isValidState() || 1617 (DerefBytesState.isAtFixpoint() && GlobalState.isAtFixpoint()); 1618 } 1619 1620 /// See AbstractState::indicateOptimisticFixpoint(...) 1621 ChangeStatus indicateOptimisticFixpoint() override { 1622 DerefBytesState.indicateOptimisticFixpoint(); 1623 GlobalState.indicateOptimisticFixpoint(); 1624 return ChangeStatus::UNCHANGED; 1625 } 1626 1627 /// See AbstractState::indicatePessimisticFixpoint(...) 1628 ChangeStatus indicatePessimisticFixpoint() override { 1629 DerefBytesState.indicatePessimisticFixpoint(); 1630 GlobalState.indicatePessimisticFixpoint(); 1631 return ChangeStatus::CHANGED; 1632 } 1633 1634 /// Update known dereferenceable bytes. 1635 void takeKnownDerefBytesMaximum(uint64_t Bytes) { 1636 DerefBytesState.takeKnownMaximum(Bytes); 1637 } 1638 1639 /// Update assumed dereferenceable bytes. 1640 void takeAssumedDerefBytesMinimum(uint64_t Bytes) { 1641 DerefBytesState.takeAssumedMinimum(Bytes); 1642 } 1643 1644 /// Equality for DerefState. 1645 bool operator==(const DerefState &R) { 1646 return this->DerefBytesState == R.DerefBytesState && 1647 this->GlobalState == R.GlobalState; 1648 } 1649 1650 /// Inequality for IntegerState. 1651 bool operator!=(const DerefState &R) { return !(*this == R); } 1652 1653 /// See IntegerState::operator^= 1654 DerefState operator^=(const DerefState &R) { 1655 DerefBytesState ^= R.DerefBytesState; 1656 GlobalState ^= R.GlobalState; 1657 return *this; 1658 } 1659 1660 /// See IntegerState::operator&= 1661 DerefState operator&=(const DerefState &R) { 1662 DerefBytesState &= R.DerefBytesState; 1663 GlobalState &= R.GlobalState; 1664 return *this; 1665 } 1666 1667 /// See IntegerState::operator|= 1668 DerefState operator|=(const DerefState &R) { 1669 DerefBytesState |= R.DerefBytesState; 1670 GlobalState |= R.GlobalState; 1671 return *this; 1672 } 1673 }; 1674 1675 struct AADereferenceableImpl : AADereferenceable, DerefState { 1676 AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {} 1677 using StateType = DerefState; 1678 1679 void initialize(Attributor &A) override { 1680 SmallVector<Attribute, 4> Attrs; 1681 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, 1682 Attrs); 1683 for (const Attribute &Attr : Attrs) 1684 takeKnownDerefBytesMaximum(Attr.getValueAsInt()); 1685 1686 NonNullAA = A.getAAFor<AANonNull>(*this, getIRPosition()); 1687 } 1688 1689 /// See AbstractAttribute::getState() 1690 /// { 1691 StateType &getState() override { return *this; } 1692 const StateType &getState() const override { return *this; } 1693 /// } 1694 1695 /// See AADereferenceable::getAssumedDereferenceableBytes(). 1696 uint32_t getAssumedDereferenceableBytes() const override { 1697 return DerefBytesState.getAssumed(); 1698 } 1699 1700 /// See AADereferenceable::getKnownDereferenceableBytes(). 1701 uint32_t getKnownDereferenceableBytes() const override { 1702 return DerefBytesState.getKnown(); 1703 } 1704 1705 /// See AADereferenceable::isAssumedGlobal(). 1706 bool isAssumedGlobal() const override { return GlobalState.getAssumed(); } 1707 1708 /// See AADereferenceable::isKnownGlobal(). 1709 bool isKnownGlobal() const override { return GlobalState.getKnown(); } 1710 1711 bool isAssumedNonNull() const override { 1712 return NonNullAA && NonNullAA->isAssumedNonNull(); 1713 } 1714 1715 void getDeducedAttributes(LLVMContext &Ctx, 1716 SmallVectorImpl<Attribute> &Attrs) const override { 1717 // TODO: Add *_globally support 1718 if (isAssumedNonNull()) 1719 Attrs.emplace_back(Attribute::getWithDereferenceableBytes( 1720 Ctx, getAssumedDereferenceableBytes())); 1721 else 1722 Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( 1723 Ctx, getAssumedDereferenceableBytes())); 1724 } 1725 uint64_t computeAssumedDerefenceableBytes(Attributor &A, Value &V, 1726 bool &IsGlobal); 1727 1728 /// See AbstractAttribute::getAsStr(). 1729 const std::string getAsStr() const override { 1730 if (!getAssumedDereferenceableBytes()) 1731 return "unknown-dereferenceable"; 1732 return std::string("dereferenceable") + 1733 (isAssumedNonNull() ? "" : "_or_null") + 1734 (isAssumedGlobal() ? "_globally" : "") + "<" + 1735 std::to_string(getKnownDereferenceableBytes()) + "-" + 1736 std::to_string(getAssumedDereferenceableBytes()) + ">"; 1737 } 1738 1739 private: 1740 const AANonNull *NonNullAA = nullptr; 1741 }; 1742 1743 struct AADereferenceableReturned final : AADereferenceableImpl { 1744 AADereferenceableReturned(const IRPosition &IRP) 1745 : AADereferenceableImpl(IRP) {} 1746 1747 /// See AbstractAttribute::updateImpl(...). 1748 ChangeStatus updateImpl(Attributor &A) override; 1749 1750 /// See AbstractAttribute::trackStatistics() 1751 void trackStatistics() const override { 1752 STATS_DECLTRACK_FNRET_ATTR(dereferenceable) 1753 } 1754 }; 1755 1756 // Helper function that returns dereferenceable bytes. 1757 static uint64_t calcDifferenceIfBaseIsNonNull(int64_t DerefBytes, 1758 int64_t Offset, bool IsNonNull) { 1759 if (!IsNonNull) 1760 return 0; 1761 return std::max((int64_t)0, DerefBytes - Offset); 1762 } 1763 1764 uint64_t 1765 AADereferenceableImpl::computeAssumedDerefenceableBytes(Attributor &A, Value &V, 1766 bool &IsGlobal) { 1767 // TODO: Tracking the globally flag. 1768 IsGlobal = false; 1769 1770 // First, we try to get information about V from Attributor. 1771 if (auto *DerefAA = 1772 A.getAAFor<AADereferenceable>(*this, IRPosition::value(V))) { 1773 return DerefAA->getAssumedDereferenceableBytes(); 1774 } 1775 1776 // Otherwise, we try to compute assumed bytes from base pointer. 1777 const DataLayout &DL = A.getDataLayout(); 1778 unsigned IdxWidth = 1779 DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); 1780 APInt Offset(IdxWidth, 0); 1781 Value *Base = V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset); 1782 1783 if (auto *BaseDerefAA = 1784 A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base))) { 1785 return calcDifferenceIfBaseIsNonNull( 1786 BaseDerefAA->getAssumedDereferenceableBytes(), Offset.getSExtValue(), 1787 Offset != 0 || BaseDerefAA->isAssumedNonNull()); 1788 } 1789 1790 // Then, use IR information. 1791 1792 if (isDereferenceablePointer(Base, Base->getType(), DL)) 1793 return calcDifferenceIfBaseIsNonNull( 1794 DL.getTypeStoreSize(Base->getType()->getPointerElementType()), 1795 Offset.getSExtValue(), 1796 !NullPointerIsDefined(getAnchorScope(), 1797 V.getType()->getPointerAddressSpace())); 1798 1799 return 0; 1800 } 1801 1802 ChangeStatus AADereferenceableReturned::updateImpl(Attributor &A) { 1803 auto BeforeState = static_cast<DerefState>(*this); 1804 1805 bool IsGlobal = isAssumedGlobal(); 1806 1807 auto CheckReturnValue = [&](Value &RV) -> bool { 1808 takeAssumedDerefBytesMinimum( 1809 computeAssumedDerefenceableBytes(A, RV, IsGlobal)); 1810 return isValidState(); 1811 }; 1812 1813 if (A.checkForAllReturnedValues(CheckReturnValue, *this)) { 1814 GlobalState.intersectAssumedBits(IsGlobal); 1815 return BeforeState == static_cast<DerefState>(*this) 1816 ? ChangeStatus::UNCHANGED 1817 : ChangeStatus::CHANGED; 1818 } 1819 return indicatePessimisticFixpoint(); 1820 } 1821 1822 struct AADereferenceableArgument final : AADereferenceableImpl { 1823 AADereferenceableArgument(const IRPosition &IRP) 1824 : AADereferenceableImpl(IRP) {} 1825 1826 /// See AbstractAttribute::updateImpl(...). 1827 ChangeStatus updateImpl(Attributor &A) override; 1828 1829 /// See AbstractAttribute::trackStatistics() 1830 void trackStatistics() const override { 1831 STATS_DECLTRACK_ARG_ATTR(dereferenceable) 1832 } 1833 }; 1834 1835 ChangeStatus AADereferenceableArgument::updateImpl(Attributor &A) { 1836 Argument &Arg = cast<Argument>(getAnchorValue()); 1837 1838 auto BeforeState = static_cast<DerefState>(*this); 1839 1840 unsigned ArgNo = Arg.getArgNo(); 1841 1842 bool IsGlobal = isAssumedGlobal(); 1843 1844 // Callback function 1845 std::function<bool(CallSite)> CallSiteCheck = [&](CallSite CS) -> bool { 1846 assert(CS && "Sanity check: Call site was not initialized properly!"); 1847 1848 // Check that DereferenceableAA is AADereferenceableCallSiteArgument. 1849 if (auto *DereferenceableAA = A.getAAFor<AADereferenceable>( 1850 *this, IRPosition::callsite_argument(CS, ArgNo))) { 1851 ImmutableCallSite ICS( 1852 &DereferenceableAA->getIRPosition().getAnchorValue()); 1853 if (ICS && CS.getInstruction() == ICS.getInstruction()) { 1854 takeAssumedDerefBytesMinimum( 1855 DereferenceableAA->getAssumedDereferenceableBytes()); 1856 IsGlobal &= DereferenceableAA->isAssumedGlobal(); 1857 return isValidState(); 1858 } 1859 } 1860 1861 takeAssumedDerefBytesMinimum(computeAssumedDerefenceableBytes( 1862 A, *CS.getArgOperand(ArgNo), IsGlobal)); 1863 1864 return isValidState(); 1865 }; 1866 1867 if (!A.checkForAllCallSites(CallSiteCheck, *this, true)) 1868 return indicatePessimisticFixpoint(); 1869 1870 GlobalState.intersectAssumedBits(IsGlobal); 1871 1872 return BeforeState == static_cast<DerefState>(*this) ? ChangeStatus::UNCHANGED 1873 : ChangeStatus::CHANGED; 1874 } 1875 1876 /// Dereferenceable attribute for a call site argument. 1877 struct AADereferenceableCallSiteArgument final : AADereferenceableImpl { 1878 AADereferenceableCallSiteArgument(const IRPosition &IRP) 1879 : AADereferenceableImpl(IRP) {} 1880 1881 /// See AbstractAttribute::updateImpl(Attributor &A). 1882 ChangeStatus updateImpl(Attributor &A) override; 1883 1884 /// See AbstractAttribute::trackStatistics() 1885 void trackStatistics() const override { 1886 STATS_DECLTRACK_CSARG_ATTR(dereferenceable) 1887 } 1888 }; 1889 1890 ChangeStatus AADereferenceableCallSiteArgument::updateImpl(Attributor &A) { 1891 // NOTE: Never look at the argument of the callee in this method. 1892 // If we do this, "dereferenceable" is always deduced because of the 1893 // assumption. 1894 1895 Value &V = getAssociatedValue(); 1896 1897 auto BeforeState = static_cast<DerefState>(*this); 1898 1899 bool IsGlobal = isAssumedGlobal(); 1900 1901 takeAssumedDerefBytesMinimum( 1902 computeAssumedDerefenceableBytes(A, V, IsGlobal)); 1903 GlobalState.intersectAssumedBits(IsGlobal); 1904 1905 return BeforeState == static_cast<DerefState>(*this) ? ChangeStatus::UNCHANGED 1906 : ChangeStatus::CHANGED; 1907 } 1908 1909 // ------------------------ Align Argument Attribute ------------------------ 1910 1911 struct AAAlignImpl : AAAlign { 1912 AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {} 1913 1914 // Max alignemnt value allowed in IR 1915 static const unsigned MAX_ALIGN = 1U << 29; 1916 1917 const std::string getAsStr() const override { 1918 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + 1919 "-" + std::to_string(getAssumedAlign()) + ">") 1920 : "unknown-align"; 1921 } 1922 1923 /// See AbstractAttriubute::initialize(...). 1924 void initialize(Attributor &A) override { 1925 takeAssumedMinimum(MAX_ALIGN); 1926 1927 SmallVector<Attribute, 4> Attrs; 1928 getAttrs({Attribute::Alignment}, Attrs); 1929 for (const Attribute &Attr : Attrs) 1930 takeKnownMaximum(Attr.getValueAsInt()); 1931 } 1932 1933 /// See AbstractAttribute::getDeducedAttributes 1934 virtual void 1935 getDeducedAttributes(LLVMContext &Ctx, 1936 SmallVectorImpl<Attribute> &Attrs) const override { 1937 Attrs.emplace_back(Attribute::getWithAlignment(Ctx, getAssumedAlign())); 1938 } 1939 }; 1940 1941 /// Align attribute for function return value. 1942 struct AAAlignReturned final : AAAlignImpl { 1943 AAAlignReturned(const IRPosition &IRP) : AAAlignImpl(IRP) {} 1944 1945 /// See AbstractAttribute::updateImpl(...). 1946 ChangeStatus updateImpl(Attributor &A) override; 1947 1948 /// See AbstractAttribute::trackStatistics() 1949 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) } 1950 }; 1951 1952 ChangeStatus AAAlignReturned::updateImpl(Attributor &A) { 1953 1954 // Currently, align<n> is deduced if alignments in return values are assumed 1955 // as greater than n. We reach pessimistic fixpoint if any of the return value 1956 // wouldn't have align. If no assumed state was used for reasoning, an 1957 // optimistic fixpoint is reached earlier. 1958 1959 base_t BeforeState = getAssumed(); 1960 auto CheckReturnValue = 1961 [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &RetInsts) -> bool { 1962 auto *AlignAA = A.getAAFor<AAAlign>(*this, IRPosition::value(RV)); 1963 1964 if (AlignAA) 1965 takeAssumedMinimum(AlignAA->getAssumedAlign()); 1966 else 1967 // Use IR information. 1968 takeAssumedMinimum(RV.getPointerAlignment(A.getDataLayout())); 1969 1970 return isValidState(); 1971 }; 1972 1973 if (!A.checkForAllReturnedValuesAndReturnInsts(CheckReturnValue, *this)) 1974 return indicatePessimisticFixpoint(); 1975 1976 return (getAssumed() != BeforeState) ? ChangeStatus::CHANGED 1977 : ChangeStatus::UNCHANGED; 1978 } 1979 1980 /// Align attribute for function argument. 1981 struct AAAlignArgument final : AAAlignImpl { 1982 AAAlignArgument(const IRPosition &IRP) : AAAlignImpl(IRP) {} 1983 1984 /// See AbstractAttribute::updateImpl(...). 1985 virtual ChangeStatus updateImpl(Attributor &A) override; 1986 1987 /// See AbstractAttribute::trackStatistics() 1988 void trackStatistics() const override{STATS_DECLTRACK_ARG_ATTR(aligned)}; 1989 }; 1990 1991 ChangeStatus AAAlignArgument::updateImpl(Attributor &A) { 1992 1993 Argument &Arg = cast<Argument>(getAnchorValue()); 1994 1995 unsigned ArgNo = Arg.getArgNo(); 1996 const DataLayout &DL = A.getDataLayout(); 1997 1998 auto BeforeState = getAssumed(); 1999 2000 // Callback function 2001 std::function<bool(CallSite)> CallSiteCheck = [&](CallSite CS) { 2002 assert(CS && "Sanity check: Call site was not initialized properly!"); 2003 2004 auto *AlignAA = 2005 A.getAAFor<AAAlign>(*this, IRPosition::callsite_argument(CS, ArgNo)); 2006 2007 // Check that AlignAA is AAAlignCallSiteArgument. 2008 if (AlignAA) { 2009 ImmutableCallSite ICS(&AlignAA->getIRPosition().getAnchorValue()); 2010 if (ICS && CS.getInstruction() == ICS.getInstruction()) { 2011 takeAssumedMinimum(AlignAA->getAssumedAlign()); 2012 return isValidState(); 2013 } 2014 } 2015 2016 Value *V = CS.getArgOperand(ArgNo); 2017 takeAssumedMinimum(V->getPointerAlignment(DL)); 2018 return isValidState(); 2019 }; 2020 2021 if (!A.checkForAllCallSites(CallSiteCheck, *this, true)) 2022 indicatePessimisticFixpoint(); 2023 2024 return BeforeState == getAssumed() ? ChangeStatus::UNCHANGED 2025 : ChangeStatus ::CHANGED; 2026 } 2027 2028 struct AAAlignCallSiteArgument final : AAAlignImpl { 2029 AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignImpl(IRP) {} 2030 2031 /// See AbstractAttribute::initialize(...). 2032 void initialize(Attributor &A) override { 2033 takeKnownMaximum( 2034 getAssociatedValue().getPointerAlignment(A.getDataLayout())); 2035 } 2036 2037 /// See AbstractAttribute::updateImpl(Attributor &A). 2038 ChangeStatus updateImpl(Attributor &A) override; 2039 2040 /// See AbstractAttribute::trackStatistics() 2041 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) } 2042 }; 2043 2044 ChangeStatus AAAlignCallSiteArgument::updateImpl(Attributor &A) { 2045 // NOTE: Never look at the argument of the callee in this method. 2046 // If we do this, "align" is always deduced because of the assumption. 2047 2048 auto BeforeState = getAssumed(); 2049 2050 Value &V = getAssociatedValue(); 2051 auto *AlignAA = A.getAAFor<AAAlign>(*this, IRPosition::value(V)); 2052 2053 if (AlignAA) 2054 takeAssumedMinimum(AlignAA->getAssumedAlign()); 2055 else 2056 indicatePessimisticFixpoint(); 2057 2058 return BeforeState == getAssumed() ? ChangeStatus::UNCHANGED 2059 : ChangeStatus::CHANGED; 2060 } 2061 2062 /// ------------------ Function No-Return Attribute ---------------------------- 2063 struct AANoReturnImpl : public AANoReturn { 2064 AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {} 2065 2066 /// See AbstractAttribute::getAsStr(). 2067 const std::string getAsStr() const override { 2068 return getAssumed() ? "noreturn" : "may-return"; 2069 } 2070 2071 /// See AbstractAttribute::initialize(...). 2072 void initialize(Attributor &A) override { 2073 if (hasAttr({getAttrKind()})) 2074 indicateOptimisticFixpoint(); 2075 } 2076 2077 /// See AbstractAttribute::updateImpl(Attributor &A). 2078 virtual ChangeStatus updateImpl(Attributor &A) override { 2079 auto CheckForNoReturn = [](Instruction &) { return false; }; 2080 if (!A.checkForAllInstructions(CheckForNoReturn, *this, 2081 {(unsigned)Instruction::Ret})) 2082 return indicatePessimisticFixpoint(); 2083 return ChangeStatus::UNCHANGED; 2084 } 2085 }; 2086 2087 struct AANoReturnFunction final : AANoReturnImpl { 2088 AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {} 2089 2090 /// See AbstractAttribute::trackStatistics() 2091 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) } 2092 }; 2093 2094 /// ---------------------------------------------------------------------------- 2095 /// Attributor 2096 /// ---------------------------------------------------------------------------- 2097 2098 bool Attributor::isAssumedDead(const AbstractAttribute &AA, 2099 const AAIsDead *LivenessAA) { 2100 const Instruction *CtxI = AA.getIRPosition().getCtxI(); 2101 if (!CtxI) 2102 return false; 2103 2104 if (!LivenessAA) 2105 LivenessAA = 2106 getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction())); 2107 if (!LivenessAA || !LivenessAA->isAssumedDead(CtxI)) 2108 return false; 2109 2110 // TODO: Do not track dependences automatically but add it here as only a 2111 // "is-assumed-dead" result causes a dependence. 2112 return true; 2113 } 2114 2115 bool Attributor::checkForAllCallSites(const function_ref<bool(CallSite)> &Pred, 2116 const AbstractAttribute &QueryingAA, 2117 bool RequireAllCallSites) { 2118 // We can try to determine information from 2119 // the call sites. However, this is only possible all call sites are known, 2120 // hence the function has internal linkage. 2121 const IRPosition &IRP = QueryingAA.getIRPosition(); 2122 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 2123 if (!AssociatedFunction) 2124 return false; 2125 2126 if (RequireAllCallSites && !AssociatedFunction->hasInternalLinkage()) { 2127 LLVM_DEBUG( 2128 dbgs() 2129 << "[Attributor] Function " << AssociatedFunction->getName() 2130 << " has no internal linkage, hence not all call sites are known\n"); 2131 return false; 2132 } 2133 2134 for (const Use &U : AssociatedFunction->uses()) { 2135 Instruction *I = cast<Instruction>(U.getUser()); 2136 Function *Caller = I->getFunction(); 2137 2138 auto *LivenessAA = 2139 getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*Caller)); 2140 2141 // Skip dead calls. 2142 if (LivenessAA && LivenessAA->isAssumedDead(I)) 2143 continue; 2144 2145 CallSite CS(U.getUser()); 2146 if (!CS || !CS.isCallee(&U) || !CS.getCaller()->hasExactDefinition()) { 2147 if (!RequireAllCallSites) 2148 continue; 2149 2150 LLVM_DEBUG(dbgs() << "[Attributor] User " << *U.getUser() 2151 << " is an invalid use of " 2152 << AssociatedFunction->getName() << "\n"); 2153 return false; 2154 } 2155 2156 if (Pred(CS)) 2157 continue; 2158 2159 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for " 2160 << *CS.getInstruction() << "\n"); 2161 return false; 2162 } 2163 2164 return true; 2165 } 2166 2167 bool Attributor::checkForAllReturnedValuesAndReturnInsts( 2168 const function_ref<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> 2169 &Pred, 2170 const AbstractAttribute &QueryingAA) { 2171 2172 const IRPosition &IRP = QueryingAA.getIRPosition(); 2173 // Since we need to provide return instructions we have to have an exact 2174 // definition. 2175 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 2176 if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition()) 2177 return false; 2178 2179 // If this is a call site query we use the call site specific return values 2180 // and liveness information. 2181 const IRPosition &QueryIRP = IRPosition::function_scope(IRP); 2182 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); 2183 if (!AARetVal || !AARetVal->getState().isValidState()) 2184 return false; 2185 2186 return AARetVal->checkForAllReturnedValuesAndReturnInsts(Pred); 2187 } 2188 2189 bool Attributor::checkForAllReturnedValues( 2190 const function_ref<bool(Value &)> &Pred, 2191 const AbstractAttribute &QueryingAA) { 2192 2193 const IRPosition &IRP = QueryingAA.getIRPosition(); 2194 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 2195 if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition()) 2196 return false; 2197 2198 const IRPosition &QueryIRP = IRPosition::function_scope(IRP); 2199 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP); 2200 if (!AARetVal || !AARetVal->getState().isValidState()) 2201 return false; 2202 2203 return AARetVal->checkForAllReturnedValuesAndReturnInsts( 2204 [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &) { 2205 return Pred(RV); 2206 }); 2207 } 2208 2209 bool Attributor::checkForAllInstructions( 2210 const llvm::function_ref<bool(Instruction &)> &Pred, 2211 const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) { 2212 2213 const IRPosition &IRP = QueryingAA.getIRPosition(); 2214 // Since we need to provide instructions we have to have an exact definition. 2215 const Function *AssociatedFunction = IRP.getAssociatedFunction(); 2216 if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition()) 2217 return false; 2218 2219 const IRPosition &QueryIRP = IRPosition::function_scope(IRP); 2220 const auto &LivenessAA = getAAFor<AAIsDead>(QueryingAA, QueryIRP); 2221 2222 auto &OpcodeInstMap = 2223 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction); 2224 for (unsigned Opcode : Opcodes) { 2225 for (Instruction *I : OpcodeInstMap[Opcode]) { 2226 // Skip dead instructions. 2227 if (LivenessAA && LivenessAA->isAssumedDead(I)) 2228 continue; 2229 2230 if (!Pred(*I)) 2231 return false; 2232 } 2233 } 2234 2235 return true; 2236 } 2237 2238 bool Attributor::checkForAllReadWriteInstructions( 2239 const llvm::function_ref<bool(Instruction &)> &Pred, 2240 AbstractAttribute &QueryingAA) { 2241 2242 const Function *AssociatedFunction = 2243 QueryingAA.getIRPosition().getAssociatedFunction(); 2244 if (!AssociatedFunction) 2245 return false; 2246 2247 const auto &LivenessAA = 2248 getAAFor<AAIsDead>(QueryingAA, QueryingAA.getIRPosition()); 2249 2250 for (Instruction *I : 2251 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) { 2252 // Skip dead instructions. 2253 if (LivenessAA && LivenessAA->isAssumedDead(I)) 2254 continue; 2255 2256 if (!Pred(*I)) 2257 return false; 2258 } 2259 2260 return true; 2261 } 2262 2263 ChangeStatus Attributor::run() { 2264 // Initialize all abstract attributes. 2265 for (AbstractAttribute *AA : AllAbstractAttributes) 2266 AA->initialize(*this); 2267 2268 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " 2269 << AllAbstractAttributes.size() 2270 << " abstract attributes.\n"); 2271 2272 // Now that all abstract attributes are collected and initialized we start 2273 // the abstract analysis. 2274 2275 unsigned IterationCounter = 1; 2276 2277 SmallVector<AbstractAttribute *, 64> ChangedAAs; 2278 SetVector<AbstractAttribute *> Worklist; 2279 Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); 2280 2281 do { 2282 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter 2283 << ", Worklist size: " << Worklist.size() << "\n"); 2284 2285 // Add all abstract attributes that are potentially dependent on one that 2286 // changed to the work list. 2287 for (AbstractAttribute *ChangedAA : ChangedAAs) { 2288 auto &QuerriedAAs = QueryMap[ChangedAA]; 2289 Worklist.insert(QuerriedAAs.begin(), QuerriedAAs.end()); 2290 } 2291 2292 // Reset the changed set. 2293 ChangedAAs.clear(); 2294 2295 // Update all abstract attribute in the work list and record the ones that 2296 // changed. 2297 for (AbstractAttribute *AA : Worklist) 2298 if (!isAssumedDead(*AA, nullptr)) 2299 if (AA->update(*this) == ChangeStatus::CHANGED) 2300 ChangedAAs.push_back(AA); 2301 2302 // Reset the work list and repopulate with the changed abstract attributes. 2303 // Note that dependent ones are added above. 2304 Worklist.clear(); 2305 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end()); 2306 2307 } while (!Worklist.empty() && ++IterationCounter < MaxFixpointIterations); 2308 2309 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: " 2310 << IterationCounter << "/" << MaxFixpointIterations 2311 << " iterations\n"); 2312 2313 bool FinishedAtFixpoint = Worklist.empty(); 2314 2315 // Reset abstract arguments not settled in a sound fixpoint by now. This 2316 // happens when we stopped the fixpoint iteration early. Note that only the 2317 // ones marked as "changed" *and* the ones transitively depending on them 2318 // need to be reverted to a pessimistic state. Others might not be in a 2319 // fixpoint state but we can use the optimistic results for them anyway. 2320 SmallPtrSet<AbstractAttribute *, 32> Visited; 2321 for (unsigned u = 0; u < ChangedAAs.size(); u++) { 2322 AbstractAttribute *ChangedAA = ChangedAAs[u]; 2323 if (!Visited.insert(ChangedAA).second) 2324 continue; 2325 2326 AbstractState &State = ChangedAA->getState(); 2327 if (!State.isAtFixpoint()) { 2328 State.indicatePessimisticFixpoint(); 2329 2330 NumAttributesTimedOut++; 2331 } 2332 2333 auto &QuerriedAAs = QueryMap[ChangedAA]; 2334 ChangedAAs.append(QuerriedAAs.begin(), QuerriedAAs.end()); 2335 } 2336 2337 LLVM_DEBUG({ 2338 if (!Visited.empty()) 2339 dbgs() << "\n[Attributor] Finalized " << Visited.size() 2340 << " abstract attributes.\n"; 2341 }); 2342 2343 unsigned NumManifested = 0; 2344 unsigned NumAtFixpoint = 0; 2345 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; 2346 for (AbstractAttribute *AA : AllAbstractAttributes) { 2347 AbstractState &State = AA->getState(); 2348 2349 // If there is not already a fixpoint reached, we can now take the 2350 // optimistic state. This is correct because we enforced a pessimistic one 2351 // on abstract attributes that were transitively dependent on a changed one 2352 // already above. 2353 if (!State.isAtFixpoint()) 2354 State.indicateOptimisticFixpoint(); 2355 2356 // If the state is invalid, we do not try to manifest it. 2357 if (!State.isValidState()) 2358 continue; 2359 2360 // Skip dead code. 2361 if (isAssumedDead(*AA, nullptr)) 2362 continue; 2363 // Manifest the state and record if we changed the IR. 2364 ChangeStatus LocalChange = AA->manifest(*this); 2365 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled()) 2366 AA->trackStatistics(); 2367 2368 ManifestChange = ManifestChange | LocalChange; 2369 2370 NumAtFixpoint++; 2371 NumManifested += (LocalChange == ChangeStatus::CHANGED); 2372 } 2373 2374 (void)NumManifested; 2375 (void)NumAtFixpoint; 2376 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested 2377 << " arguments while " << NumAtFixpoint 2378 << " were in a valid fixpoint state\n"); 2379 2380 // If verification is requested, we finished this run at a fixpoint, and the 2381 // IR was changed, we re-run the whole fixpoint analysis, starting at 2382 // re-initialization of the arguments. This re-run should not result in an IR 2383 // change. Though, the (virtual) state of attributes at the end of the re-run 2384 // might be more optimistic than the known state or the IR state if the better 2385 // state cannot be manifested. 2386 if (VerifyAttributor && FinishedAtFixpoint && 2387 ManifestChange == ChangeStatus::CHANGED) { 2388 VerifyAttributor = false; 2389 ChangeStatus VerifyStatus = run(); 2390 if (VerifyStatus != ChangeStatus::UNCHANGED) 2391 llvm_unreachable( 2392 "Attributor verification failed, re-run did result in an IR change " 2393 "even after a fixpoint was reached in the original run. (False " 2394 "positives possible!)"); 2395 VerifyAttributor = true; 2396 } 2397 2398 NumAttributesManifested += NumManifested; 2399 NumAttributesValidFixpoint += NumAtFixpoint; 2400 2401 return ManifestChange; 2402 } 2403 2404 /// Helper function that checks if an abstract attribute of type \p AAType 2405 /// should be created for IR position \p IRP and if so creates and registers it 2406 /// with the Attributor \p A. 2407 /// 2408 /// This method will look at the provided whitelist. If one is given and the 2409 /// kind \p AAType::ID is not contained, no abstract attribute is created. 2410 /// 2411 /// \returns The created abstract argument, or nullptr if none was created. 2412 template <typename AAType> 2413 static AAType *checkAndRegisterAA(const IRPosition &IRP, Attributor &A, 2414 DenseSet<const char *> *Whitelist) { 2415 if (Whitelist && !Whitelist->count(&AAType::ID)) 2416 return nullptr; 2417 2418 return &A.registerAA<AAType>(*new AAType(IRP)); 2419 } 2420 2421 void Attributor::identifyDefaultAbstractAttributes( 2422 Function &F, DenseSet<const char *> *Whitelist) { 2423 2424 IRPosition FPos = IRPosition::function(F); 2425 2426 // Check for dead BasicBlocks in every function. 2427 // We need dead instruction detection because we do not want to deal with 2428 // broken IR in which SSA rules do not apply. 2429 checkAndRegisterAA<AAIsDeadFunction>(FPos, *this, /* Whitelist */ nullptr); 2430 2431 // Every function might be "will-return". 2432 checkAndRegisterAA<AAWillReturnFunction>(FPos, *this, Whitelist); 2433 2434 // Every function can be nounwind. 2435 checkAndRegisterAA<AANoUnwindFunction>(FPos, *this, Whitelist); 2436 2437 // Every function might be marked "nosync" 2438 checkAndRegisterAA<AANoSyncFunction>(FPos, *this, Whitelist); 2439 2440 // Every function might be "no-free". 2441 checkAndRegisterAA<AANoFreeFunction>(FPos, *this, Whitelist); 2442 2443 // Every function might be "no-return". 2444 checkAndRegisterAA<AANoReturnFunction>(FPos, *this, Whitelist); 2445 2446 // Return attributes are only appropriate if the return type is non void. 2447 Type *ReturnType = F.getReturnType(); 2448 if (!ReturnType->isVoidTy()) { 2449 // Argument attribute "returned" --- Create only one per function even 2450 // though it is an argument attribute. 2451 checkAndRegisterAA<AAReturnedValuesFunction>(FPos, *this, Whitelist); 2452 2453 if (ReturnType->isPointerTy()) { 2454 IRPosition RetPos = IRPosition::returned(F); 2455 2456 // Every function with pointer return type might be marked align. 2457 checkAndRegisterAA<AAAlignReturned>(RetPos, *this, Whitelist); 2458 2459 // Every function with pointer return type might be marked nonnull. 2460 checkAndRegisterAA<AANonNullReturned>(RetPos, *this, Whitelist); 2461 2462 // Every function with pointer return type might be marked noalias. 2463 checkAndRegisterAA<AANoAliasReturned>(RetPos, *this, Whitelist); 2464 2465 // Every function with pointer return type might be marked 2466 // dereferenceable. 2467 checkAndRegisterAA<AADereferenceableReturned>(RetPos, *this, Whitelist); 2468 } 2469 } 2470 2471 for (Argument &Arg : F.args()) { 2472 if (Arg.getType()->isPointerTy()) { 2473 IRPosition ArgPos = IRPosition::argument(Arg); 2474 // Every argument with pointer type might be marked nonnull. 2475 checkAndRegisterAA<AANonNullArgument>(ArgPos, *this, Whitelist); 2476 2477 // Every argument with pointer type might be marked dereferenceable. 2478 checkAndRegisterAA<AADereferenceableArgument>(ArgPos, *this, Whitelist); 2479 2480 // Every argument with pointer type might be marked align. 2481 checkAndRegisterAA<AAAlignArgument>(ArgPos, *this, Whitelist); 2482 } 2483 } 2484 2485 // Walk all instructions to find more attribute opportunities and also 2486 // interesting instructions that might be queried by abstract attributes 2487 // during their initialization or update. 2488 auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F]; 2489 auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F]; 2490 2491 for (Instruction &I : instructions(&F)) { 2492 bool IsInterestingOpcode = false; 2493 2494 // To allow easy access to all instructions in a function with a given 2495 // opcode we store them in the InfoCache. As not all opcodes are interesting 2496 // to concrete attributes we only cache the ones that are as identified in 2497 // the following switch. 2498 // Note: There are no concrete attributes now so this is initially empty. 2499 switch (I.getOpcode()) { 2500 default: 2501 assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) && 2502 "New call site/base instruction type needs to be known int the " 2503 "attributor."); 2504 break; 2505 case Instruction::Call: 2506 case Instruction::CallBr: 2507 case Instruction::Invoke: 2508 case Instruction::CleanupRet: 2509 case Instruction::CatchSwitch: 2510 case Instruction::Resume: 2511 case Instruction::Ret: 2512 IsInterestingOpcode = true; 2513 } 2514 if (IsInterestingOpcode) 2515 InstOpcodeMap[I.getOpcode()].push_back(&I); 2516 if (I.mayReadOrWriteMemory()) 2517 ReadOrWriteInsts.push_back(&I); 2518 2519 CallSite CS(&I); 2520 if (CS && CS.getCalledFunction()) { 2521 for (int i = 0, e = CS.getCalledFunction()->arg_size(); i < e; i++) { 2522 if (!CS.getArgument(i)->getType()->isPointerTy()) 2523 continue; 2524 IRPosition CSArgPos = IRPosition::callsite_argument(CS, i); 2525 2526 // Call site argument attribute "non-null". 2527 checkAndRegisterAA<AANonNullCallSiteArgument>(CSArgPos, *this, 2528 Whitelist); 2529 2530 // Call site argument attribute "dereferenceable". 2531 checkAndRegisterAA<AADereferenceableCallSiteArgument>(CSArgPos, *this, 2532 Whitelist); 2533 2534 // Call site argument attribute "align". 2535 checkAndRegisterAA<AAAlignCallSiteArgument>(CSArgPos, *this, Whitelist); 2536 } 2537 } 2538 } 2539 } 2540 2541 /// Helpers to ease debugging through output streams and print calls. 2542 /// 2543 ///{ 2544 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) { 2545 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged"); 2546 } 2547 2548 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) { 2549 switch (AP) { 2550 case IRPosition::IRP_INVALID: 2551 return OS << "inv"; 2552 case IRPosition::IRP_FLOAT: 2553 return OS << "flt"; 2554 case IRPosition::IRP_RETURNED: 2555 return OS << "fn_ret"; 2556 case IRPosition::IRP_CALL_SITE_RETURNED: 2557 return OS << "cs_ret"; 2558 case IRPosition::IRP_FUNCTION: 2559 return OS << "fn"; 2560 case IRPosition::IRP_CALL_SITE: 2561 return OS << "cs"; 2562 case IRPosition::IRP_ARGUMENT: 2563 return OS << "arg"; 2564 case IRPosition::IRP_CALL_SITE_ARGUMENT: 2565 return OS << "cs_arg"; 2566 } 2567 llvm_unreachable("Unknown attribute position!"); 2568 } 2569 2570 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) { 2571 const Value &AV = Pos.getAssociatedValue(); 2572 return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " [" 2573 << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}"; 2574 } 2575 2576 raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerState &S) { 2577 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")" 2578 << static_cast<const AbstractState &>(S); 2579 } 2580 2581 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) { 2582 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : "")); 2583 } 2584 2585 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) { 2586 AA.print(OS); 2587 return OS; 2588 } 2589 2590 void AbstractAttribute::print(raw_ostream &OS) const { 2591 OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() 2592 << "]"; 2593 } 2594 ///} 2595 2596 /// ---------------------------------------------------------------------------- 2597 /// Pass (Manager) Boilerplate 2598 /// ---------------------------------------------------------------------------- 2599 2600 static bool runAttributorOnModule(Module &M) { 2601 if (DisableAttributor) 2602 return false; 2603 2604 LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size() 2605 << " functions.\n"); 2606 2607 // Create an Attributor and initially empty information cache that is filled 2608 // while we identify default attribute opportunities. 2609 InformationCache InfoCache(M.getDataLayout()); 2610 Attributor A(InfoCache); 2611 2612 for (Function &F : M) { 2613 // TODO: Not all attributes require an exact definition. Find a way to 2614 // enable deduction for some but not all attributes in case the 2615 // definition might be changed at runtime, see also 2616 // http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html. 2617 // TODO: We could always determine abstract attributes and if sufficient 2618 // information was found we could duplicate the functions that do not 2619 // have an exact definition. 2620 if (!F.hasExactDefinition()) { 2621 NumFnWithoutExactDefinition++; 2622 continue; 2623 } 2624 2625 // For now we ignore naked and optnone functions. 2626 if (F.hasFnAttribute(Attribute::Naked) || 2627 F.hasFnAttribute(Attribute::OptimizeNone)) 2628 continue; 2629 2630 NumFnWithExactDefinition++; 2631 2632 // Populate the Attributor with abstract attribute opportunities in the 2633 // function and the information cache with IR information. 2634 A.identifyDefaultAbstractAttributes(F); 2635 } 2636 2637 return A.run() == ChangeStatus::CHANGED; 2638 } 2639 2640 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { 2641 if (runAttributorOnModule(M)) { 2642 // FIXME: Think about passes we will preserve and add them here. 2643 return PreservedAnalyses::none(); 2644 } 2645 return PreservedAnalyses::all(); 2646 } 2647 2648 namespace { 2649 2650 struct AttributorLegacyPass : public ModulePass { 2651 static char ID; 2652 2653 AttributorLegacyPass() : ModulePass(ID) { 2654 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry()); 2655 } 2656 2657 bool runOnModule(Module &M) override { 2658 if (skipModule(M)) 2659 return false; 2660 return runAttributorOnModule(M); 2661 } 2662 2663 void getAnalysisUsage(AnalysisUsage &AU) const override { 2664 // FIXME: Think about passes we will preserve and add them here. 2665 AU.setPreservesCFG(); 2666 } 2667 }; 2668 2669 } // end anonymous namespace 2670 2671 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); } 2672 2673 char AttributorLegacyPass::ID = 0; 2674 2675 const char AAReturnedValues::ID = 0; 2676 const char AANoUnwind::ID = 0; 2677 const char AANoSync::ID = 0; 2678 const char AANoFree::ID = 0; 2679 const char AANonNull::ID = 0; 2680 const char AANoRecurse::ID = 0; 2681 const char AAWillReturn::ID = 0; 2682 const char AANoAlias::ID = 0; 2683 const char AANoReturn::ID = 0; 2684 const char AAIsDead::ID = 0; 2685 const char AADereferenceable::ID = 0; 2686 const char AAAlign::ID = 0; 2687 2688 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor", 2689 "Deduce and propagate attributes", false, false) 2690 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor", 2691 "Deduce and propagate attributes", false, false) 2692