| 1 | //===- FunctionAttrs.h - Compute function attributes ------------*- C++ -*-===// |
| 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 | /// Provides passes for computing function attributes based on interprocedural |
| 11 | /// analyses. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_TRANSFORMS_IPO_FUNCTIONATTRS_H |
| 16 | #define LLVM_TRANSFORMS_IPO_FUNCTIONATTRS_H |
| 17 | |
| 18 | #include "llvm/Analysis/AliasAnalysis.h" |
| 19 | #include "llvm/Analysis/CGSCCPassManager.h" |
| 20 | #include "llvm/Analysis/LazyCallGraph.h" |
| 21 | #include "llvm/IR/PassManager.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class GlobalValueSummary; |
| 27 | class ModuleSummaryIndex; |
| 28 | class Function; |
| 29 | class Module; |
| 30 | |
| 31 | /// Returns the memory access properties of this copy of the function. |
| 32 | LLVM_ABI MemoryEffects computeFunctionBodyMemoryAccess(Function &F, |
| 33 | AAResults &AAR); |
| 34 | |
| 35 | /// Propagate function attributes for function summaries along the index's |
| 36 | /// callgraph during thinlink |
| 37 | LLVM_ABI bool thinLTOPropagateFunctionAttrs( |
| 38 | ModuleSummaryIndex &Index, |
| 39 | function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)> |
| 40 | isPrevailing); |
| 41 | |
| 42 | /// Computes function attributes in post-order over the call graph. |
| 43 | /// |
| 44 | /// By operating in post-order, this pass computes precise attributes for |
| 45 | /// called functions prior to processsing their callers. This "bottom-up" |
| 46 | /// approach allows powerful interprocedural inference of function attributes |
| 47 | /// like memory access patterns, etc. It can discover functions that do not |
| 48 | /// access memory, or only read memory, and give them the readnone/readonly |
| 49 | /// attribute. It also discovers function arguments that are not captured by |
| 50 | /// the function and marks them with the nocapture attribute. |
| 51 | struct PostOrderFunctionAttrsPass : PassInfoMixin<PostOrderFunctionAttrsPass> { |
| 52 | PostOrderFunctionAttrsPass(bool SkipNonRecursive = false) |
| 53 | : SkipNonRecursive(SkipNonRecursive) {} |
| 54 | LLVM_ABI PreservedAnalyses run(LazyCallGraph::SCC &C, |
| 55 | CGSCCAnalysisManager &AM, LazyCallGraph &CG, |
| 56 | CGSCCUpdateResult &UR); |
| 57 | |
| 58 | LLVM_ABI void |
| 59 | printPipeline(raw_ostream &OS, |
| 60 | function_ref<StringRef(StringRef)> MapClassName2PassName); |
| 61 | |
| 62 | private: |
| 63 | bool SkipNonRecursive; |
| 64 | }; |
| 65 | |
| 66 | /// A pass to do RPO deduction and propagation of function attributes. |
| 67 | /// |
| 68 | /// This pass provides a general RPO or "top down" propagation of |
| 69 | /// function attributes. For a few (rare) cases, we can deduce significantly |
| 70 | /// more about function attributes by working in RPO, so this pass |
| 71 | /// provides the complement to the post-order pass above where the majority of |
| 72 | /// deduction is performed. |
| 73 | // FIXME: Currently there is no RPO CGSCC pass structure to slide into and so |
| 74 | // this is a boring module pass, but eventually it should be an RPO CGSCC pass |
| 75 | // when such infrastructure is available. |
| 76 | class ReversePostOrderFunctionAttrsPass |
| 77 | : public PassInfoMixin<ReversePostOrderFunctionAttrsPass> { |
| 78 | public: |
| 79 | LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); |
| 80 | }; |
| 81 | |
| 82 | /// Additional 'norecurse' attribute deduction during postlink LTO phase. |
| 83 | /// |
| 84 | /// This is a module pass that infers 'norecurse' attribute on functions. |
| 85 | /// It runs during LTO and analyzes the module's call graph to find functions |
| 86 | /// that are guaranteed not to call themselves, either directly or indirectly. |
| 87 | /// The pass uses a module-wide flag which checks if any function's address is |
| 88 | /// taken or any function in the module has external linkage, to safely handle |
| 89 | /// indirect and library function calls from current function. |
| 90 | class NoRecurseLTOInferencePass |
| 91 | : public PassInfoMixin<NoRecurseLTOInferencePass> { |
| 92 | public: |
| 93 | LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 94 | }; |
| 95 | } // end namespace llvm |
| 96 | |
| 97 | #endif // LLVM_TRANSFORMS_IPO_FUNCTIONATTRS_H |
| 98 | |