FunctionAttrs.cpp 53 KB

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