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/CGSCCPassManager.h" 19 #include "llvm/Analysis/LazyCallGraph.h" 20 #include "llvm/IR/ModuleSummaryIndex.h" 21 #include "llvm/IR/PassManager.h" 22 23 namespace llvm { 24 25 class AAResults; 26 class Function; 27 class Module; 28 class Pass; 29 30 /// The three kinds of memory access relevant to 'readonly' and 31 /// 'readnone' attributes. 32 enum MemoryAccessKind { 33 MAK_ReadNone = 0, 34 MAK_ReadOnly = 1, 35 MAK_MayWrite = 2, 36 MAK_WriteOnly = 3 37 }; 38 39 /// Returns the memory access properties of this copy of the function. 40 MemoryAccessKind computeFunctionBodyMemoryAccess(Function &F, AAResults &AAR); 41 42 /// Propagate function attributes for function summaries along the index's 43 /// callgraph during thinlink 44 bool thinLTOPropagateFunctionAttrs( 45 ModuleSummaryIndex &Index, 46 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)> 47 isPrevailing); 48 49 /// Computes function attributes in post-order over the call graph. 50 /// 51 /// By operating in post-order, this pass computes precise attributes for 52 /// called functions prior to processsing their callers. This "bottom-up" 53 /// approach allows powerful interprocedural inference of function attributes 54 /// like memory access patterns, etc. It can discover functions that do not 55 /// access memory, or only read memory, and give them the readnone/readonly 56 /// attribute. It also discovers function arguments that are not captured by 57 /// the function and marks them with the nocapture attribute. 58 struct PostOrderFunctionAttrsPass : PassInfoMixin<PostOrderFunctionAttrsPass> { 59 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, 60 LazyCallGraph &CG, CGSCCUpdateResult &UR); 61 }; 62 63 /// Create a legacy pass manager instance of a pass to compute function attrs 64 /// in post-order. 65 Pass *createPostOrderFunctionAttrsLegacyPass(); 66 67 /// A pass to do RPO deduction and propagation of function attributes. 68 /// 69 /// This pass provides a general RPO or "top down" propagation of 70 /// function attributes. For a few (rare) cases, we can deduce significantly 71 /// more about function attributes by working in RPO, so this pass 72 /// provides the complement to the post-order pass above where the majority of 73 /// deduction is performed. 74 // FIXME: Currently there is no RPO CGSCC pass structure to slide into and so 75 // this is a boring module pass, but eventually it should be an RPO CGSCC pass 76 // when such infrastructure is available. 77 class ReversePostOrderFunctionAttrsPass 78 : public PassInfoMixin<ReversePostOrderFunctionAttrsPass> { 79 public: 80 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 81 }; 82 83 } // end namespace llvm 84 85 #endif // LLVM_TRANSFORMS_IPO_FUNCTIONATTRS_H 86