1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// This file implements interprocedural passes which walk the 12 /// call-graph deducing and/or propagating function attributes. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/Transforms/IPO/FunctionAttrs.h" 17 #include "llvm/ADT/SCCIterator.h" 18 #include "llvm/ADT/SetVector.h" 19 #include "llvm/ADT/SmallSet.h" 20 #include "llvm/ADT/Statistic.h" 21 #include "llvm/ADT/StringSwitch.h" 22 #include "llvm/Analysis/AliasAnalysis.h" 23 #include "llvm/Analysis/AssumptionCache.h" 24 #include "llvm/Analysis/BasicAliasAnalysis.h" 25 #include "llvm/Analysis/CallGraph.h" 26 #include "llvm/Analysis/CallGraphSCCPass.h" 27 #include "llvm/Analysis/CaptureTracking.h" 28 #include "llvm/Analysis/TargetLibraryInfo.h" 29 #include "llvm/Analysis/ValueTracking.h" 30 #include "llvm/IR/GlobalVariable.h" 31 #include "llvm/IR/InstIterator.h" 32 #include "llvm/IR/IntrinsicInst.h" 33 #include "llvm/IR/LLVMContext.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/raw_ostream.h" 36 #include "llvm/Transforms/IPO.h" 37 using namespace llvm; 38 39 #define DEBUG_TYPE "functionattrs" 40 41 STATISTIC(NumReadNone, "Number of functions marked readnone"); 42 STATISTIC(NumReadOnly, "Number of functions marked readonly"); 43 STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); 44 STATISTIC(NumReturned, "Number of arguments marked returned"); 45 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone"); 46 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly"); 47 STATISTIC(NumNoAlias, "Number of function returns marked noalias"); 48 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull"); 49 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse"); 50 51 // FIXME: This is disabled by default to avoid exposing security vulnerabilities 52 // in C/C++ code compiled by clang: 53 // http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html 54 static cl::opt<bool> EnableNonnullArgPropagation( 55 "enable-nonnull-arg-prop", cl::Hidden, 56 cl::desc("Try to propagate nonnull argument attributes from callsites to " 57 "caller functions.")); 58 59 namespace { 60 typedef SmallSetVector<Function *, 8> SCCNodeSet; 61 } 62 63 /// Returns the memory access attribute for function F using AAR for AA results, 64 /// where SCCNodes is the current SCC. 65 /// 66 /// If ThisBody is true, this function may examine the function body and will 67 /// return a result pertaining to this copy of the function. If it is false, the 68 /// result will be based only on AA results for the function declaration; it 69 /// will be assumed that some other (perhaps less optimized) version of the 70 /// function may be selected at link time. 71 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody, 72 AAResults &AAR, 73 const SCCNodeSet &SCCNodes) { 74 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F); 75 if (MRB == FMRB_DoesNotAccessMemory) 76 // Already perfect! 77 return MAK_ReadNone; 78 79 if (!ThisBody) { 80 if (AliasAnalysis::onlyReadsMemory(MRB)) 81 return MAK_ReadOnly; 82 83 // Conservatively assume it writes to memory. 84 return MAK_MayWrite; 85 } 86 87 // Scan the function body for instructions that may read or write memory. 88 bool ReadsMemory = false; 89 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { 90 Instruction *I = &*II; 91 92 // Some instructions can be ignored even if they read or write memory. 93 // Detect these now, skipping to the next instruction if one is found. 94 CallSite CS(cast<Value>(I)); 95 if (CS) { 96 // Ignore calls to functions in the same SCC, as long as the call sites 97 // don't have operand bundles. Calls with operand bundles are allowed to 98 // have memory effects not described by the memory effects of the call 99 // target. 100 if (!CS.hasOperandBundles() && CS.getCalledFunction() && 101 SCCNodes.count(CS.getCalledFunction())) 102 continue; 103 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS); 104 105 // If the call doesn't access memory, we're done. 106 if (!(MRB & MRI_ModRef)) 107 continue; 108 109 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) { 110 // The call could access any memory. If that includes writes, give up. 111 if (MRB & MRI_Mod) 112 return MAK_MayWrite; 113 // If it reads, note it. 114 if (MRB & MRI_Ref) 115 ReadsMemory = true; 116 continue; 117 } 118 119 // Check whether all pointer arguments point to local memory, and 120 // ignore calls that only access local memory. 121 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); 122 CI != CE; ++CI) { 123 Value *Arg = *CI; 124 if (!Arg->getType()->isPtrOrPtrVectorTy()) 125 continue; 126 127 AAMDNodes AAInfo; 128 I->getAAMetadata(AAInfo); 129 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo); 130 131 // Skip accesses to local or constant memory as they don't impact the 132 // externally visible mod/ref behavior. 133 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) 134 continue; 135 136 if (MRB & MRI_Mod) 137 // Writes non-local memory. Give up. 138 return MAK_MayWrite; 139 if (MRB & MRI_Ref) 140 // Ok, it reads non-local memory. 141 ReadsMemory = true; 142 } 143 continue; 144 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 145 // Ignore non-volatile loads from local memory. (Atomic is okay here.) 146 if (!LI->isVolatile()) { 147 MemoryLocation Loc = MemoryLocation::get(LI); 148 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) 149 continue; 150 } 151 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 152 // Ignore non-volatile stores to local memory. (Atomic is okay here.) 153 if (!SI->isVolatile()) { 154 MemoryLocation Loc = MemoryLocation::get(SI); 155 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) 156 continue; 157 } 158 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { 159 // Ignore vaargs on local memory. 160 MemoryLocation Loc = MemoryLocation::get(VI); 161 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) 162 continue; 163 } 164 165 // Any remaining instructions need to be taken seriously! Check if they 166 // read or write memory. 167 if (I->mayWriteToMemory()) 168 // Writes memory. Just give up. 169 return MAK_MayWrite; 170 171 // If this instruction may read memory, remember that. 172 ReadsMemory |= I->mayReadFromMemory(); 173 } 174 175 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone; 176 } 177 178 MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F, 179 AAResults &AAR) { 180 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {}); 181 } 182 183 /// Deduce readonly/readnone attributes for the SCC. 184 template <typename AARGetterT> 185 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) { 186 // Check if any of the functions in the SCC read or write memory. If they 187 // write memory then they can't be marked readnone or readonly. 188 bool ReadsMemory = false; 189 for (Function *F : SCCNodes) { 190 // Call the callable parameter to look up AA results for this function. 191 AAResults &AAR = AARGetter(*F); 192 193 // Non-exact function definitions may not be selected at link time, and an 194 // alternative version that writes to memory may be selected. See the 195 // comment on GlobalValue::isDefinitionExact for more details. 196 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(), 197 AAR, SCCNodes)) { 198 case MAK_MayWrite: 199 return false; 200 case MAK_ReadOnly: 201 ReadsMemory = true; 202 break; 203 case MAK_ReadNone: 204 // Nothing to do! 205 break; 206 } 207 } 208 209 // Success! Functions in this SCC do not access memory, or only read memory. 210 // Give them the appropriate attribute. 211 bool MadeChange = false; 212 for (Function *F : SCCNodes) { 213 if (F->doesNotAccessMemory()) 214 // Already perfect! 215 continue; 216 217 if (F->onlyReadsMemory() && ReadsMemory) 218 // No change. 219 continue; 220 221 MadeChange = true; 222 223 // Clear out any existing attributes. 224 F->removeFnAttr(Attribute::ReadOnly); 225 F->removeFnAttr(Attribute::ReadNone); 226 227 // Add in the new attribute. 228 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone); 229 230 if (ReadsMemory) 231 ++NumReadOnly; 232 else 233 ++NumReadNone; 234 } 235 236 return MadeChange; 237 } 238 239 namespace { 240 /// For a given pointer Argument, this retains a list of Arguments of functions 241 /// in the same SCC that the pointer data flows into. We use this to build an 242 /// SCC of the arguments. 243 struct ArgumentGraphNode { 244 Argument *Definition; 245 SmallVector<ArgumentGraphNode *, 4> Uses; 246 }; 247 248 class ArgumentGraph { 249 // We store pointers to ArgumentGraphNode objects, so it's important that 250 // that they not move around upon insert. 251 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy; 252 253 ArgumentMapTy ArgumentMap; 254 255 // There is no root node for the argument graph, in fact: 256 // void f(int *x, int *y) { if (...) f(x, y); } 257 // is an example where the graph is disconnected. The SCCIterator requires a 258 // single entry point, so we maintain a fake ("synthetic") root node that 259 // uses every node. Because the graph is directed and nothing points into 260 // the root, it will not participate in any SCCs (except for its own). 261 ArgumentGraphNode SyntheticRoot; 262 263 public: 264 ArgumentGraph() { SyntheticRoot.Definition = nullptr; } 265 266 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator; 267 268 iterator begin() { return SyntheticRoot.Uses.begin(); } 269 iterator end() { return SyntheticRoot.Uses.end(); } 270 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } 271 272 ArgumentGraphNode *operator[](Argument *A) { 273 ArgumentGraphNode &Node = ArgumentMap[A]; 274 Node.Definition = A; 275 SyntheticRoot.Uses.push_back(&Node); 276 return &Node; 277 } 278 }; 279 280 /// This tracker checks whether callees are in the SCC, and if so it does not 281 /// consider that a capture, instead adding it to the "Uses" list and 282 /// continuing with the analysis. 283 struct ArgumentUsesTracker : public CaptureTracker { 284 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) 285 : Captured(false), SCCNodes(SCCNodes) {} 286 287 void tooManyUses() override { Captured = true; } 288 289 bool captured(const Use *U) override { 290 CallSite CS(U->getUser()); 291 if (!CS.getInstruction()) { 292 Captured = true; 293 return true; 294 } 295 296 Function *F = CS.getCalledFunction(); 297 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) { 298 Captured = true; 299 return true; 300 } 301 302 // Note: the callee and the two successor blocks *follow* the argument 303 // operands. This means there is no need to adjust UseIndex to account for 304 // these. 305 306 unsigned UseIndex = 307 std::distance(const_cast<const Use *>(CS.arg_begin()), U); 308 309 assert(UseIndex < CS.data_operands_size() && 310 "Indirect function calls should have been filtered above!"); 311 312 if (UseIndex >= CS.getNumArgOperands()) { 313 // Data operand, but not a argument operand -- must be a bundle operand 314 assert(CS.hasOperandBundles() && "Must be!"); 315 316 // CaptureTracking told us that we're being captured by an operand bundle 317 // use. In this case it does not matter if the callee is within our SCC 318 // or not -- we've been captured in some unknown way, and we have to be 319 // conservative. 320 Captured = true; 321 return true; 322 } 323 324 if (UseIndex >= F->arg_size()) { 325 assert(F->isVarArg() && "More params than args in non-varargs call"); 326 Captured = true; 327 return true; 328 } 329 330 Uses.push_back(&*std::next(F->arg_begin(), UseIndex)); 331 return false; 332 } 333 334 bool Captured; // True only if certainly captured (used outside our SCC). 335 SmallVector<Argument *, 4> Uses; // Uses within our SCC. 336 337 const SCCNodeSet &SCCNodes; 338 }; 339 } 340 341 namespace llvm { 342 template <> struct GraphTraits<ArgumentGraphNode *> { 343 typedef ArgumentGraphNode *NodeRef; 344 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType; 345 346 static NodeRef getEntryNode(NodeRef A) { return A; } 347 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); } 348 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); } 349 }; 350 template <> 351 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> { 352 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); } 353 static ChildIteratorType nodes_begin(ArgumentGraph *AG) { 354 return AG->begin(); 355 } 356 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); } 357 }; 358 } 359 360 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. 361 static Attribute::AttrKind 362 determinePointerReadAttrs(Argument *A, 363 const SmallPtrSet<Argument *, 8> &SCCNodes) { 364 365 SmallVector<Use *, 32> Worklist; 366 SmallSet<Use *, 32> Visited; 367 368 // inalloca arguments are always clobbered by the call. 369 if (A->hasInAllocaAttr()) 370 return Attribute::None; 371 372 bool IsRead = false; 373 // We don't need to track IsWritten. If A is written to, return immediately. 374 375 for (Use &U : A->uses()) { 376 Visited.insert(&U); 377 Worklist.push_back(&U); 378 } 379 380 while (!Worklist.empty()) { 381 Use *U = Worklist.pop_back_val(); 382 Instruction *I = cast<Instruction>(U->getUser()); 383 384 switch (I->getOpcode()) { 385 case Instruction::BitCast: 386 case Instruction::GetElementPtr: 387 case Instruction::PHI: 388 case Instruction::Select: 389 case Instruction::AddrSpaceCast: 390 // The original value is not read/written via this if the new value isn't. 391 for (Use &UU : I->uses()) 392 if (Visited.insert(&UU).second) 393 Worklist.push_back(&UU); 394 break; 395 396 case Instruction::Call: 397 case Instruction::Invoke: { 398 bool Captures = true; 399 400 if (I->getType()->isVoidTy()) 401 Captures = false; 402 403 auto AddUsersToWorklistIfCapturing = [&] { 404 if (Captures) 405 for (Use &UU : I->uses()) 406 if (Visited.insert(&UU).second) 407 Worklist.push_back(&UU); 408 }; 409 410 CallSite CS(I); 411 if (CS.doesNotAccessMemory()) { 412 AddUsersToWorklistIfCapturing(); 413 continue; 414 } 415 416 Function *F = CS.getCalledFunction(); 417 if (!F) { 418 if (CS.onlyReadsMemory()) { 419 IsRead = true; 420 AddUsersToWorklistIfCapturing(); 421 continue; 422 } 423 return Attribute::None; 424 } 425 426 // Note: the callee and the two successor blocks *follow* the argument 427 // operands. This means there is no need to adjust UseIndex to account 428 // for these. 429 430 unsigned UseIndex = std::distance(CS.arg_begin(), U); 431 432 // U cannot be the callee operand use: since we're exploring the 433 // transitive uses of an Argument, having such a use be a callee would 434 // imply the CallSite is an indirect call or invoke; and we'd take the 435 // early exit above. 436 assert(UseIndex < CS.data_operands_size() && 437 "Data operand use expected!"); 438 439 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands(); 440 441 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) { 442 assert(F->isVarArg() && "More params than args in non-varargs call"); 443 return Attribute::None; 444 } 445 446 Captures &= !CS.doesNotCapture(UseIndex); 447 448 // Since the optimizer (by design) cannot see the data flow corresponding 449 // to a operand bundle use, these cannot participate in the optimistic SCC 450 // analysis. Instead, we model the operand bundle uses as arguments in 451 // call to a function external to the SCC. 452 if (IsOperandBundleUse || 453 !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) { 454 455 // The accessors used on CallSite here do the right thing for calls and 456 // invokes with operand bundles. 457 458 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex)) 459 return Attribute::None; 460 if (!CS.doesNotAccessMemory(UseIndex)) 461 IsRead = true; 462 } 463 464 AddUsersToWorklistIfCapturing(); 465 break; 466 } 467 468 case Instruction::Load: 469 // A volatile load has side effects beyond what readonly can be relied 470 // upon. 471 if (cast<LoadInst>(I)->isVolatile()) 472 return Attribute::None; 473 474 IsRead = true; 475 break; 476 477 case Instruction::ICmp: 478 case Instruction::Ret: 479 break; 480 481 default: 482 return Attribute::None; 483 } 484 } 485 486 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone; 487 } 488 489 /// Deduce returned attributes for the SCC. 490 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) { 491 bool Changed = false; 492 493 // Check each function in turn, determining if an argument is always returned. 494 for (Function *F : SCCNodes) { 495 // We can infer and propagate function attributes only when we know that the 496 // definition we'll get at link time is *exactly* the definition we see now. 497 // For more details, see GlobalValue::mayBeDerefined. 498 if (!F->hasExactDefinition()) 499 continue; 500 501 if (F->getReturnType()->isVoidTy()) 502 continue; 503 504 // There is nothing to do if an argument is already marked as 'returned'. 505 if (any_of(F->args(), 506 [](const Argument &Arg) { return Arg.hasReturnedAttr(); })) 507 continue; 508 509 auto FindRetArg = [&]() -> Value * { 510 Value *RetArg = nullptr; 511 for (BasicBlock &BB : *F) 512 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) { 513 // Note that stripPointerCasts should look through functions with 514 // returned arguments. 515 Value *RetVal = Ret->getReturnValue()->stripPointerCasts(); 516 if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType()) 517 return nullptr; 518 519 if (!RetArg) 520 RetArg = RetVal; 521 else if (RetArg != RetVal) 522 return nullptr; 523 } 524 525 return RetArg; 526 }; 527 528 if (Value *RetArg = FindRetArg()) { 529 auto *A = cast<Argument>(RetArg); 530 A->addAttr(Attribute::Returned); 531 ++NumReturned; 532 Changed = true; 533 } 534 } 535 536 return Changed; 537 } 538 539 /// If a callsite has arguments that are also arguments to the parent function, 540 /// try to propagate attributes from the callsite's arguments to the parent's 541 /// arguments. This may be important because inlining can cause information loss 542 /// when attribute knowledge disappears with the inlined call. 543 static bool addArgumentAttrsFromCallsites(Function &F) { 544 if (!EnableNonnullArgPropagation) 545 return false; 546 547 bool Changed = false; 548 549 // For an argument attribute to transfer from a callsite to the parent, the 550 // call must be guaranteed to execute every time the parent is called. 551 // Conservatively, just check for calls in the entry block that are guaranteed 552 // to execute. 553 // TODO: This could be enhanced by testing if the callsite post-dominates the 554 // entry block or by doing simple forward walks or backward walks to the 555 // callsite. 556 BasicBlock &Entry = F.getEntryBlock(); 557 for (Instruction &I : Entry) { 558 if (auto CS = CallSite(&I)) { 559 if (auto *CalledFunc = CS.getCalledFunction()) { 560 for (auto &CSArg : CalledFunc->args()) { 561 if (!CSArg.hasNonNullAttr()) 562 continue; 563 564 // If the non-null callsite argument operand is an argument to 'F' 565 // (the caller) and the call is guaranteed to execute, then the value 566 // must be non-null throughout 'F'. 567 auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo())); 568 if (FArg && !FArg->hasNonNullAttr()) { 569 FArg->addAttr(Attribute::NonNull); 570 Changed = true; 571 } 572 } 573 } 574 } 575 if (!isGuaranteedToTransferExecutionToSuccessor(&I)) 576 break; 577 } 578 579 return Changed; 580 } 581 582 /// Deduce nocapture attributes for the SCC. 583 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) { 584 bool Changed = false; 585 586 ArgumentGraph AG; 587 588 // Check each function in turn, determining which pointer arguments are not 589 // captured. 590 for (Function *F : SCCNodes) { 591 // We can infer and propagate function attributes only when we know that the 592 // definition we'll get at link time is *exactly* the definition we see now. 593 // For more details, see GlobalValue::mayBeDerefined. 594 if (!F->hasExactDefinition()) 595 continue; 596 597 Changed |= addArgumentAttrsFromCallsites(*F); 598 599 // Functions that are readonly (or readnone) and nounwind and don't return 600 // a value can't capture arguments. Don't analyze them. 601 if (F->onlyReadsMemory() && F->doesNotThrow() && 602 F->getReturnType()->isVoidTy()) { 603 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; 604 ++A) { 605 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { 606 A->addAttr(Attribute::NoCapture); 607 ++NumNoCapture; 608 Changed = true; 609 } 610 } 611 continue; 612 } 613 614 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; 615 ++A) { 616 if (!A->getType()->isPointerTy()) 617 continue; 618 bool HasNonLocalUses = false; 619 if (!A->hasNoCaptureAttr()) { 620 ArgumentUsesTracker Tracker(SCCNodes); 621 PointerMayBeCaptured(&*A, &Tracker); 622 if (!Tracker.Captured) { 623 if (Tracker.Uses.empty()) { 624 // If it's trivially not captured, mark it nocapture now. 625 A->addAttr(Attribute::NoCapture); 626 ++NumNoCapture; 627 Changed = true; 628 } else { 629 // If it's not trivially captured and not trivially not captured, 630 // then it must be calling into another function in our SCC. Save 631 // its particulars for Argument-SCC analysis later. 632 ArgumentGraphNode *Node = AG[&*A]; 633 for (Argument *Use : Tracker.Uses) { 634 Node->Uses.push_back(AG[Use]); 635 if (Use != &*A) 636 HasNonLocalUses = true; 637 } 638 } 639 } 640 // Otherwise, it's captured. Don't bother doing SCC analysis on it. 641 } 642 if (!HasNonLocalUses && !A->onlyReadsMemory()) { 643 // Can we determine that it's readonly/readnone without doing an SCC? 644 // Note that we don't allow any calls at all here, or else our result 645 // will be dependent on the iteration order through the functions in the 646 // SCC. 647 SmallPtrSet<Argument *, 8> Self; 648 Self.insert(&*A); 649 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self); 650 if (R != Attribute::None) { 651 A->addAttr(R); 652 Changed = true; 653 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; 654 } 655 } 656 } 657 } 658 659 // The graph we've collected is partial because we stopped scanning for 660 // argument uses once we solved the argument trivially. These partial nodes 661 // show up as ArgumentGraphNode objects with an empty Uses list, and for 662 // these nodes the final decision about whether they capture has already been 663 // made. If the definition doesn't have a 'nocapture' attribute by now, it 664 // captures. 665 666 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) { 667 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I; 668 if (ArgumentSCC.size() == 1) { 669 if (!ArgumentSCC[0]->Definition) 670 continue; // synthetic root node 671 672 // eg. "void f(int* x) { if (...) f(x); }" 673 if (ArgumentSCC[0]->Uses.size() == 1 && 674 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { 675 Argument *A = ArgumentSCC[0]->Definition; 676 A->addAttr(Attribute::NoCapture); 677 ++NumNoCapture; 678 Changed = true; 679 } 680 continue; 681 } 682 683 bool SCCCaptured = false; 684 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); 685 I != E && !SCCCaptured; ++I) { 686 ArgumentGraphNode *Node = *I; 687 if (Node->Uses.empty()) { 688 if (!Node->Definition->hasNoCaptureAttr()) 689 SCCCaptured = true; 690 } 691 } 692 if (SCCCaptured) 693 continue; 694 695 SmallPtrSet<Argument *, 8> ArgumentSCCNodes; 696 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for 697 // quickly looking up whether a given Argument is in this ArgumentSCC. 698 for (ArgumentGraphNode *I : ArgumentSCC) { 699 ArgumentSCCNodes.insert(I->Definition); 700 } 701 702 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); 703 I != E && !SCCCaptured; ++I) { 704 ArgumentGraphNode *N = *I; 705 for (ArgumentGraphNode *Use : N->Uses) { 706 Argument *A = Use->Definition; 707 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A)) 708 continue; 709 SCCCaptured = true; 710 break; 711 } 712 } 713 if (SCCCaptured) 714 continue; 715 716 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 717 Argument *A = ArgumentSCC[i]->Definition; 718 A->addAttr(Attribute::NoCapture); 719 ++NumNoCapture; 720 Changed = true; 721 } 722 723 // We also want to compute readonly/readnone. With a small number of false 724 // negatives, we can assume that any pointer which is captured isn't going 725 // to be provably readonly or readnone, since by definition we can't 726 // analyze all uses of a captured pointer. 727 // 728 // The false negatives happen when the pointer is captured by a function 729 // that promises readonly/readnone behaviour on the pointer, then the 730 // pointer's lifetime ends before anything that writes to arbitrary memory. 731 // Also, a readonly/readnone pointer may be returned, but returning a 732 // pointer is capturing it. 733 734 Attribute::AttrKind ReadAttr = Attribute::ReadNone; 735 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 736 Argument *A = ArgumentSCC[i]->Definition; 737 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes); 738 if (K == Attribute::ReadNone) 739 continue; 740 if (K == Attribute::ReadOnly) { 741 ReadAttr = Attribute::ReadOnly; 742 continue; 743 } 744 ReadAttr = K; 745 break; 746 } 747 748 if (ReadAttr != Attribute::None) { 749 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 750 Argument *A = ArgumentSCC[i]->Definition; 751 // Clear out existing readonly/readnone attributes 752 A->removeAttr(Attribute::ReadOnly); 753 A->removeAttr(Attribute::ReadNone); 754 A->addAttr(ReadAttr); 755 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; 756 Changed = true; 757 } 758 } 759 } 760 761 return Changed; 762 } 763 764 /// Tests whether a function is "malloc-like". 765 /// 766 /// A function is "malloc-like" if it returns either null or a pointer that 767 /// doesn't alias any other pointer visible to the caller. 768 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) { 769 SmallSetVector<Value *, 8> FlowsToReturn; 770 for (BasicBlock &BB : *F) 771 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) 772 FlowsToReturn.insert(Ret->getReturnValue()); 773 774 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { 775 Value *RetVal = FlowsToReturn[i]; 776 777 if (Constant *C = dyn_cast<Constant>(RetVal)) { 778 if (!C->isNullValue() && !isa<UndefValue>(C)) 779 return false; 780 781 continue; 782 } 783 784 if (isa<Argument>(RetVal)) 785 return false; 786 787 if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) 788 switch (RVI->getOpcode()) { 789 // Extend the analysis by looking upwards. 790 case Instruction::BitCast: 791 case Instruction::GetElementPtr: 792 case Instruction::AddrSpaceCast: 793 FlowsToReturn.insert(RVI->getOperand(0)); 794 continue; 795 case Instruction::Select: { 796 SelectInst *SI = cast<SelectInst>(RVI); 797 FlowsToReturn.insert(SI->getTrueValue()); 798 FlowsToReturn.insert(SI->getFalseValue()); 799 continue; 800 } 801 case Instruction::PHI: { 802 PHINode *PN = cast<PHINode>(RVI); 803 for (Value *IncValue : PN->incoming_values()) 804 FlowsToReturn.insert(IncValue); 805 continue; 806 } 807 808 // Check whether the pointer came from an allocation. 809 case Instruction::Alloca: 810 break; 811 case Instruction::Call: 812 case Instruction::Invoke: { 813 CallSite CS(RVI); 814 if (CS.hasRetAttr(Attribute::NoAlias)) 815 break; 816 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) 817 break; 818 LLVM_FALLTHROUGH; 819 } 820 default: 821 return false; // Did not come from an allocation. 822 } 823 824 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) 825 return false; 826 } 827 828 return true; 829 } 830 831 /// Deduce noalias attributes for the SCC. 832 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) { 833 // Check each function in turn, determining which functions return noalias 834 // pointers. 835 for (Function *F : SCCNodes) { 836 // Already noalias. 837 if (F->returnDoesNotAlias()) 838 continue; 839 840 // We can infer and propagate function attributes only when we know that the 841 // definition we'll get at link time is *exactly* the definition we see now. 842 // For more details, see GlobalValue::mayBeDerefined. 843 if (!F->hasExactDefinition()) 844 return false; 845 846 // We annotate noalias return values, which are only applicable to 847 // pointer types. 848 if (!F->getReturnType()->isPointerTy()) 849 continue; 850 851 if (!isFunctionMallocLike(F, SCCNodes)) 852 return false; 853 } 854 855 bool MadeChange = false; 856 for (Function *F : SCCNodes) { 857 if (F->returnDoesNotAlias() || 858 !F->getReturnType()->isPointerTy()) 859 continue; 860 861 F->setReturnDoesNotAlias(); 862 ++NumNoAlias; 863 MadeChange = true; 864 } 865 866 return MadeChange; 867 } 868 869 /// Tests whether this function is known to not return null. 870 /// 871 /// Requires that the function returns a pointer. 872 /// 873 /// Returns true if it believes the function will not return a null, and sets 874 /// \p Speculative based on whether the returned conclusion is a speculative 875 /// conclusion due to SCC calls. 876 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes, 877 bool &Speculative) { 878 assert(F->getReturnType()->isPointerTy() && 879 "nonnull only meaningful on pointer types"); 880 Speculative = false; 881 882 SmallSetVector<Value *, 8> FlowsToReturn; 883 for (BasicBlock &BB : *F) 884 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) 885 FlowsToReturn.insert(Ret->getReturnValue()); 886 887 auto &DL = F->getParent()->getDataLayout(); 888 889 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { 890 Value *RetVal = FlowsToReturn[i]; 891 892 // If this value is locally known to be non-null, we're good 893 if (isKnownNonZero(RetVal, DL)) 894 continue; 895 896 // Otherwise, we need to look upwards since we can't make any local 897 // conclusions. 898 Instruction *RVI = dyn_cast<Instruction>(RetVal); 899 if (!RVI) 900 return false; 901 switch (RVI->getOpcode()) { 902 // Extend the analysis by looking upwards. 903 case Instruction::BitCast: 904 case Instruction::GetElementPtr: 905 case Instruction::AddrSpaceCast: 906 FlowsToReturn.insert(RVI->getOperand(0)); 907 continue; 908 case Instruction::Select: { 909 SelectInst *SI = cast<SelectInst>(RVI); 910 FlowsToReturn.insert(SI->getTrueValue()); 911 FlowsToReturn.insert(SI->getFalseValue()); 912 continue; 913 } 914 case Instruction::PHI: { 915 PHINode *PN = cast<PHINode>(RVI); 916 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 917 FlowsToReturn.insert(PN->getIncomingValue(i)); 918 continue; 919 } 920 case Instruction::Call: 921 case Instruction::Invoke: { 922 CallSite CS(RVI); 923 Function *Callee = CS.getCalledFunction(); 924 // A call to a node within the SCC is assumed to return null until 925 // proven otherwise 926 if (Callee && SCCNodes.count(Callee)) { 927 Speculative = true; 928 continue; 929 } 930 return false; 931 } 932 default: 933 return false; // Unknown source, may be null 934 }; 935 llvm_unreachable("should have either continued or returned"); 936 } 937 938 return true; 939 } 940 941 /// Deduce nonnull attributes for the SCC. 942 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) { 943 // Speculative that all functions in the SCC return only nonnull 944 // pointers. We may refute this as we analyze functions. 945 bool SCCReturnsNonNull = true; 946 947 bool MadeChange = false; 948 949 // Check each function in turn, determining which functions return nonnull 950 // pointers. 951 for (Function *F : SCCNodes) { 952 // Already nonnull. 953 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex, 954 Attribute::NonNull)) 955 continue; 956 957 // We can infer and propagate function attributes only when we know that the 958 // definition we'll get at link time is *exactly* the definition we see now. 959 // For more details, see GlobalValue::mayBeDerefined. 960 if (!F->hasExactDefinition()) 961 return false; 962 963 // We annotate nonnull return values, which are only applicable to 964 // pointer types. 965 if (!F->getReturnType()->isPointerTy()) 966 continue; 967 968 bool Speculative = false; 969 if (isReturnNonNull(F, SCCNodes, Speculative)) { 970 if (!Speculative) { 971 // Mark the function eagerly since we may discover a function 972 // which prevents us from speculating about the entire SCC 973 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n"); 974 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); 975 ++NumNonNullReturn; 976 MadeChange = true; 977 } 978 continue; 979 } 980 // At least one function returns something which could be null, can't 981 // speculate any more. 982 SCCReturnsNonNull = false; 983 } 984 985 if (SCCReturnsNonNull) { 986 for (Function *F : SCCNodes) { 987 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex, 988 Attribute::NonNull) || 989 !F->getReturnType()->isPointerTy()) 990 continue; 991 992 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n"); 993 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); 994 ++NumNonNullReturn; 995 MadeChange = true; 996 } 997 } 998 999 return MadeChange; 1000 } 1001 1002 /// Remove the convergent attribute from all functions in the SCC if every 1003 /// callsite within the SCC is not convergent (except for calls to functions 1004 /// within the SCC). Returns true if changes were made. 1005 static bool removeConvergentAttrs(const SCCNodeSet &SCCNodes) { 1006 // For every function in SCC, ensure that either 1007 // * it is not convergent, or 1008 // * we can remove its convergent attribute. 1009 bool HasConvergentFn = false; 1010 for (Function *F : SCCNodes) { 1011 if (!F->isConvergent()) continue; 1012 HasConvergentFn = true; 1013 1014 // Can't remove convergent from function declarations. 1015 if (F->isDeclaration()) return false; 1016 1017 // Can't remove convergent if any of our functions has a convergent call to a 1018 // function not in the SCC. 1019 for (Instruction &I : instructions(*F)) { 1020 CallSite CS(&I); 1021 // Bail if CS is a convergent call to a function not in the SCC. 1022 if (CS && CS.isConvergent() && 1023 SCCNodes.count(CS.getCalledFunction()) == 0) 1024 return false; 1025 } 1026 } 1027 1028 // If the SCC doesn't have any convergent functions, we have nothing to do. 1029 if (!HasConvergentFn) return false; 1030 1031 // If we got here, all of the calls the SCC makes to functions not in the SCC 1032 // are non-convergent. Therefore all of the SCC's functions can also be made 1033 // non-convergent. We'll remove the attr from the callsites in 1034 // InstCombineCalls. 1035 for (Function *F : SCCNodes) { 1036 if (!F->isConvergent()) continue; 1037 1038 DEBUG(dbgs() << "Removing convergent attr from fn " << F->getName() 1039 << "\n"); 1040 F->setNotConvergent(); 1041 } 1042 return true; 1043 } 1044 1045 static bool setDoesNotRecurse(Function &F) { 1046 if (F.doesNotRecurse()) 1047 return false; 1048 F.setDoesNotRecurse(); 1049 ++NumNoRecurse; 1050 return true; 1051 } 1052 1053 static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) { 1054 // Try and identify functions that do not recurse. 1055 1056 // If the SCC contains multiple nodes we know for sure there is recursion. 1057 if (SCCNodes.size() != 1) 1058 return false; 1059 1060 Function *F = *SCCNodes.begin(); 1061 if (!F || F->isDeclaration() || F->doesNotRecurse()) 1062 return false; 1063 1064 // If all of the calls in F are identifiable and are to norecurse functions, F 1065 // is norecurse. This check also detects self-recursion as F is not currently 1066 // marked norecurse, so any called from F to F will not be marked norecurse. 1067 for (Instruction &I : instructions(*F)) 1068 if (auto CS = CallSite(&I)) { 1069 Function *Callee = CS.getCalledFunction(); 1070 if (!Callee || Callee == F || !Callee->doesNotRecurse()) 1071 // Function calls a potentially recursive function. 1072 return false; 1073 } 1074 1075 // Every call was to a non-recursive function other than this function, and 1076 // we have no indirect recursion as the SCC size is one. This function cannot 1077 // recurse. 1078 return setDoesNotRecurse(*F); 1079 } 1080 1081 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C, 1082 CGSCCAnalysisManager &AM, 1083 LazyCallGraph &CG, 1084 CGSCCUpdateResult &) { 1085 FunctionAnalysisManager &FAM = 1086 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager(); 1087 1088 // We pass a lambda into functions to wire them up to the analysis manager 1089 // for getting function analyses. 1090 auto AARGetter = [&](Function &F) -> AAResults & { 1091 return FAM.getResult<AAManager>(F); 1092 }; 1093 1094 // Fill SCCNodes with the elements of the SCC. Also track whether there are 1095 // any external or opt-none nodes that will prevent us from optimizing any 1096 // part of the SCC. 1097 SCCNodeSet SCCNodes; 1098 bool HasUnknownCall = false; 1099 for (LazyCallGraph::Node &N : C) { 1100 Function &F = N.getFunction(); 1101 if (F.hasFnAttribute(Attribute::OptimizeNone)) { 1102 // Treat any function we're trying not to optimize as if it were an 1103 // indirect call and omit it from the node set used below. 1104 HasUnknownCall = true; 1105 continue; 1106 } 1107 // Track whether any functions in this SCC have an unknown call edge. 1108 // Note: if this is ever a performance hit, we can common it with 1109 // subsequent routines which also do scans over the instructions of the 1110 // function. 1111 if (!HasUnknownCall) 1112 for (Instruction &I : instructions(F)) 1113 if (auto CS = CallSite(&I)) 1114 if (!CS.getCalledFunction()) { 1115 HasUnknownCall = true; 1116 break; 1117 } 1118 1119 SCCNodes.insert(&F); 1120 } 1121 1122 bool Changed = false; 1123 Changed |= addArgumentReturnedAttrs(SCCNodes); 1124 Changed |= addReadAttrs(SCCNodes, AARGetter); 1125 Changed |= addArgumentAttrs(SCCNodes); 1126 1127 // If we have no external nodes participating in the SCC, we can deduce some 1128 // more precise attributes as well. 1129 if (!HasUnknownCall) { 1130 Changed |= addNoAliasAttrs(SCCNodes); 1131 Changed |= addNonNullAttrs(SCCNodes); 1132 Changed |= removeConvergentAttrs(SCCNodes); 1133 Changed |= addNoRecurseAttrs(SCCNodes); 1134 } 1135 1136 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); 1137 } 1138 1139 namespace { 1140 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass { 1141 static char ID; // Pass identification, replacement for typeid 1142 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) { 1143 initializePostOrderFunctionAttrsLegacyPassPass( 1144 *PassRegistry::getPassRegistry()); 1145 } 1146 1147 bool runOnSCC(CallGraphSCC &SCC) override; 1148 1149 void getAnalysisUsage(AnalysisUsage &AU) const override { 1150 AU.setPreservesCFG(); 1151 AU.addRequired<AssumptionCacheTracker>(); 1152 getAAResultsAnalysisUsage(AU); 1153 CallGraphSCCPass::getAnalysisUsage(AU); 1154 } 1155 }; 1156 } 1157 1158 char PostOrderFunctionAttrsLegacyPass::ID = 0; 1159 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs", 1160 "Deduce function attributes", false, false) 1161 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 1162 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 1163 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs", 1164 "Deduce function attributes", false, false) 1165 1166 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() { 1167 return new PostOrderFunctionAttrsLegacyPass(); 1168 } 1169 1170 template <typename AARGetterT> 1171 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) { 1172 bool Changed = false; 1173 1174 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up 1175 // whether a given CallGraphNode is in this SCC. Also track whether there are 1176 // any external or opt-none nodes that will prevent us from optimizing any 1177 // part of the SCC. 1178 SCCNodeSet SCCNodes; 1179 bool ExternalNode = false; 1180 for (CallGraphNode *I : SCC) { 1181 Function *F = I->getFunction(); 1182 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) { 1183 // External node or function we're trying not to optimize - we both avoid 1184 // transform them and avoid leveraging information they provide. 1185 ExternalNode = true; 1186 continue; 1187 } 1188 1189 SCCNodes.insert(F); 1190 } 1191 1192 // Skip it if the SCC only contains optnone functions. 1193 if (SCCNodes.empty()) 1194 return Changed; 1195 1196 Changed |= addArgumentReturnedAttrs(SCCNodes); 1197 Changed |= addReadAttrs(SCCNodes, AARGetter); 1198 Changed |= addArgumentAttrs(SCCNodes); 1199 1200 // If we have no external nodes participating in the SCC, we can deduce some 1201 // more precise attributes as well. 1202 if (!ExternalNode) { 1203 Changed |= addNoAliasAttrs(SCCNodes); 1204 Changed |= addNonNullAttrs(SCCNodes); 1205 Changed |= removeConvergentAttrs(SCCNodes); 1206 Changed |= addNoRecurseAttrs(SCCNodes); 1207 } 1208 1209 return Changed; 1210 } 1211 1212 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) { 1213 if (skipSCC(SCC)) 1214 return false; 1215 return runImpl(SCC, LegacyAARGetter(*this)); 1216 } 1217 1218 namespace { 1219 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass { 1220 static char ID; // Pass identification, replacement for typeid 1221 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) { 1222 initializeReversePostOrderFunctionAttrsLegacyPassPass( 1223 *PassRegistry::getPassRegistry()); 1224 } 1225 1226 bool runOnModule(Module &M) override; 1227 1228 void getAnalysisUsage(AnalysisUsage &AU) const override { 1229 AU.setPreservesCFG(); 1230 AU.addRequired<CallGraphWrapperPass>(); 1231 AU.addPreserved<CallGraphWrapperPass>(); 1232 } 1233 }; 1234 } 1235 1236 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0; 1237 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs", 1238 "Deduce function attributes in RPO", false, false) 1239 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 1240 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs", 1241 "Deduce function attributes in RPO", false, false) 1242 1243 Pass *llvm::createReversePostOrderFunctionAttrsPass() { 1244 return new ReversePostOrderFunctionAttrsLegacyPass(); 1245 } 1246 1247 static bool addNoRecurseAttrsTopDown(Function &F) { 1248 // We check the preconditions for the function prior to calling this to avoid 1249 // the cost of building up a reversible post-order list. We assert them here 1250 // to make sure none of the invariants this relies on were violated. 1251 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!"); 1252 assert(!F.doesNotRecurse() && 1253 "This function has already been deduced as norecurs!"); 1254 assert(F.hasInternalLinkage() && 1255 "Can only do top-down deduction for internal linkage functions!"); 1256 1257 // If F is internal and all of its uses are calls from a non-recursive 1258 // functions, then none of its calls could in fact recurse without going 1259 // through a function marked norecurse, and so we can mark this function too 1260 // as norecurse. Note that the uses must actually be calls -- otherwise 1261 // a pointer to this function could be returned from a norecurse function but 1262 // this function could be recursively (indirectly) called. Note that this 1263 // also detects if F is directly recursive as F is not yet marked as 1264 // a norecurse function. 1265 for (auto *U : F.users()) { 1266 auto *I = dyn_cast<Instruction>(U); 1267 if (!I) 1268 return false; 1269 CallSite CS(I); 1270 if (!CS || !CS.getParent()->getParent()->doesNotRecurse()) 1271 return false; 1272 } 1273 return setDoesNotRecurse(F); 1274 } 1275 1276 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) { 1277 // We only have a post-order SCC traversal (because SCCs are inherently 1278 // discovered in post-order), so we accumulate them in a vector and then walk 1279 // it in reverse. This is simpler than using the RPO iterator infrastructure 1280 // because we need to combine SCC detection and the PO walk of the call 1281 // graph. We can also cheat egregiously because we're primarily interested in 1282 // synthesizing norecurse and so we can only save the singular SCCs as SCCs 1283 // with multiple functions in them will clearly be recursive. 1284 SmallVector<Function *, 16> Worklist; 1285 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) { 1286 if (I->size() != 1) 1287 continue; 1288 1289 Function *F = I->front()->getFunction(); 1290 if (F && !F->isDeclaration() && !F->doesNotRecurse() && 1291 F->hasInternalLinkage()) 1292 Worklist.push_back(F); 1293 } 1294 1295 bool Changed = false; 1296 for (auto *F : reverse(Worklist)) 1297 Changed |= addNoRecurseAttrsTopDown(*F); 1298 1299 return Changed; 1300 } 1301 1302 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) { 1303 if (skipModule(M)) 1304 return false; 1305 1306 auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); 1307 1308 return deduceFunctionAttributeInRPO(M, CG); 1309 } 1310 1311 PreservedAnalyses 1312 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) { 1313 auto &CG = AM.getResult<CallGraphAnalysis>(M); 1314 1315 if (!deduceFunctionAttributeInRPO(M, CG)) 1316 return PreservedAnalyses::all(); 1317 1318 PreservedAnalyses PA; 1319 PA.preserve<CallGraphAnalysis>(); 1320 return PA; 1321 } 1322