LLVM  14.0.0git
StackSafetyAnalysis.h
Go to the documentation of this file.
1 //===- StackSafetyAnalysis.h - Stack memory safety analysis -----*- 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 // Stack Safety Analysis detects allocas and arguments with safe access.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_ANALYSIS_STACKSAFETYANALYSIS_H
14 #define LLVM_ANALYSIS_STACKSAFETYANALYSIS_H
15 
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/Pass.h"
19 
20 namespace llvm {
21 
22 class AllocaInst;
23 class ScalarEvolution;
24 
25 /// Interface to access stack safety analysis results for single function.
27 public:
28  struct InfoTy;
29 
30 private:
31  Function *F = nullptr;
32  std::function<ScalarEvolution &()> GetSE;
33  mutable std::unique_ptr<InfoTy> Info;
34 
35 public:
41 
42  const InfoTy &getInfo() const;
43 
44  // TODO: Add useful for client methods.
45  void print(raw_ostream &O) const;
46 
47  /// Parameters use for a FunctionSummary.
48  /// Function collects access information of all pointer parameters.
49  /// Information includes a range of direct access of parameters by the
50  /// functions and all call sites accepting the parameter.
51  /// StackSafety assumes that missing parameter information means possibility
52  /// of access to the parameter with any offset, so we can correctly link
53  /// code without StackSafety information, e.g. non-ThinLTO.
54  std::vector<FunctionSummary::ParamAccess>
56 };
57 
59 public:
60  struct InfoTy;
61 
62 private:
63  Module *M = nullptr;
64  std::function<const StackSafetyInfo &(Function &F)> GetSSI;
65  const ModuleSummaryIndex *Index = nullptr;
66  mutable std::unique_ptr<InfoTy> Info;
67  const InfoTy &getInfo() const;
68 
69 public:
72  Module *M, std::function<const StackSafetyInfo &(Function &F)> GetSSI,
73  const ModuleSummaryIndex *Index);
77 
78  // Whether we can prove that all accesses to this Alloca are in-range and
79  // during its lifetime.
80  bool isSafe(const AllocaInst &AI) const;
81 
82  // Returns true if the instruction can be proven to do only two types of
83  // memory accesses:
84  // (1) live stack locations in-bounds or
85  // (2) non-stack locations.
86  bool stackAccessIsSafe(const Instruction &I) const;
87  void print(raw_ostream &O) const;
88  void dump() const;
89 };
90 
91 /// StackSafetyInfo wrapper for the new pass manager.
92 class StackSafetyAnalysis : public AnalysisInfoMixin<StackSafetyAnalysis> {
94  static AnalysisKey Key;
95 
96 public:
99 };
100 
101 /// Printer pass for the \c StackSafetyAnalysis results.
102 class StackSafetyPrinterPass : public PassInfoMixin<StackSafetyPrinterPass> {
103  raw_ostream &OS;
104 
105 public:
106  explicit StackSafetyPrinterPass(raw_ostream &OS) : OS(OS) {}
108 };
109 
110 /// StackSafetyInfo wrapper for the legacy pass manager
112  StackSafetyInfo SSI;
113 
114 public:
115  static char ID;
117 
118  const StackSafetyInfo &getResult() const { return SSI; }
119 
120  void print(raw_ostream &O, const Module *M) const override;
121  void getAnalysisUsage(AnalysisUsage &AU) const override;
122 
123  bool runOnFunction(Function &F) override;
124 };
125 
126 /// This pass performs the global (interprocedural) stack safety analysis (new
127 /// pass manager).
129  : public AnalysisInfoMixin<StackSafetyGlobalAnalysis> {
131  static AnalysisKey Key;
132 
133 public:
136 };
137 
138 /// Printer pass for the \c StackSafetyGlobalAnalysis results.
140  : public PassInfoMixin<StackSafetyGlobalPrinterPass> {
141  raw_ostream &OS;
142 
143 public:
144  explicit StackSafetyGlobalPrinterPass(raw_ostream &OS) : OS(OS) {}
146 };
147 
148 /// This pass performs the global (interprocedural) stack safety analysis
149 /// (legacy pass manager).
152 
153 public:
154  static char ID;
155 
158 
159  const StackSafetyGlobalInfo &getResult() const { return SSGI; }
160 
161  void print(raw_ostream &O, const Module *M) const override;
162  void getAnalysisUsage(AnalysisUsage &AU) const override;
163 
164  bool runOnModule(Module &M) override;
165 };
166 
167 bool needsParamAccessSummary(const Module &M);
168 
169 void generateParamAccessSummary(ModuleSummaryIndex &Index);
170 
171 } // end namespace llvm
172 
173 #endif // LLVM_ANALYSIS_STACKSAFETYANALYSIS_H
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::StackSafetyInfo::StackSafetyInfo
StackSafetyInfo()
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::StackSafetyGlobalInfo::isSafe
bool isSafe(const AllocaInst &AI) const
Definition: StackSafetyAnalysis.cpp:967
llvm::StackSafetyInfo::print
void print(raw_ostream &O) const
Definition: StackSafetyAnalysis.cpp:872
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
llvm::Function
Definition: Function.h:62
Pass.h
llvm::StackSafetyInfoWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: StackSafetyAnalysis.cpp:1024
llvm::StackSafetyPrinterPass
Printer pass for the StackSafetyAnalysis results.
Definition: StackSafetyAnalysis.h:102
llvm::ScalarEvolution
The main scalar evolution driver.
Definition: ScalarEvolution.h:460
llvm::StackSafetyGlobalPrinterPass
Printer pass for the StackSafetyGlobalAnalysis results.
Definition: StackSafetyAnalysis.h:139
llvm::needsParamAccessSummary
bool needsParamAccessSummary(const Module &M)
Definition: StackSafetyAnalysis.cpp:1095
llvm::StackSafetyGlobalInfo::operator=
StackSafetyGlobalInfo & operator=(StackSafetyGlobalInfo &&)
llvm::StackSafetyAnalysis::run
StackSafetyInfo run(Function &F, FunctionAnalysisManager &AM)
Definition: StackSafetyAnalysis.cpp:1004
llvm::generateParamAccessSummary
void generateParamAccessSummary(ModuleSummaryIndex &Index)
Definition: StackSafetyAnalysis.cpp:1104
ModuleSummaryIndex.h
llvm::StackSafetyAnalysis
StackSafetyInfo wrapper for the new pass manager.
Definition: StackSafetyAnalysis.h:92
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::StackSafetyInfoWrapperPass::getResult
const StackSafetyInfo & getResult() const
Definition: StackSafetyAnalysis.h:118
llvm::StackSafetyGlobalInfoWrapperPass::ID
static char ID
Definition: StackSafetyAnalysis.h:154
llvm::StackSafetyInfoWrapperPass::ID
static char ID
Definition: StackSafetyAnalysis.h:115
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::StackSafetyGlobalInfoWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: StackSafetyAnalysis.cpp:1075
llvm::Instruction
Definition: Instruction.h:45
llvm::StackSafetyInfoWrapperPass::StackSafetyInfoWrapperPass
StackSafetyInfoWrapperPass()
Definition: StackSafetyAnalysis.cpp:1020
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::StackSafetyGlobalInfo::InfoTy
Definition: StackSafetyAnalysis.cpp:231
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::StackSafetyPrinterPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: StackSafetyAnalysis.cpp:1011
llvm::StackSafetyGlobalInfo::~StackSafetyGlobalInfo
~StackSafetyGlobalInfo()
llvm::StackSafetyInfo::getParamAccesses
std::vector< FunctionSummary::ParamAccess > getParamAccesses(ModuleSummaryIndex &Index) const
Parameters use for a FunctionSummary.
Definition: StackSafetyAnalysis.cpp:910
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:206
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::StackSafetyGlobalAnalysis
This pass performs the global (interprocedural) stack safety analysis (new pass manager).
Definition: StackSafetyAnalysis.h:128
llvm::StackSafetyGlobalInfo::StackSafetyGlobalInfo
StackSafetyGlobalInfo()
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
I
#define I(x, y, z)
Definition: MD5.cpp:59
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:397
llvm::StackSafetyGlobalAnalysis::run
Result run(Module &M, ModuleAnalysisManager &AM)
Definition: StackSafetyAnalysis.cpp:1042
llvm::StackSafetyInfo
Interface to access stack safety analysis results for single function.
Definition: StackSafetyAnalysis.h:26
llvm::StackSafetyGlobalInfoWrapperPass::StackSafetyGlobalInfoWrapperPass
StackSafetyGlobalInfoWrapperPass()
Definition: StackSafetyAnalysis.cpp:1062
llvm::StackSafetyInfoWrapperPass
StackSafetyInfo wrapper for the legacy pass manager.
Definition: StackSafetyAnalysis.h:111
llvm::StackSafetyInfoWrapperPass::print
void print(raw_ostream &O, const Module *M) const override
print - Print out the internal state of the pass.
Definition: StackSafetyAnalysis.cpp:1029
llvm::StackSafetyGlobalInfo::dump
void dump() const
Definition: StackSafetyAnalysis.cpp:1000
llvm::StackSafetyInfoWrapperPass::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: StackSafetyAnalysis.cpp:1033
llvm::StackSafetyGlobalPrinterPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: StackSafetyAnalysis.cpp:1053
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::StackSafetyInfo::operator=
StackSafetyInfo & operator=(StackSafetyInfo &&)
llvm::StackSafetyGlobalInfoWrapperPass::print
void print(raw_ostream &O, const Module *M) const override
print - Print out the internal state of the pass.
Definition: StackSafetyAnalysis.cpp:1070
PassManager.h
llvm::StackSafetyInfo::~StackSafetyInfo
~StackSafetyInfo()
llvm::StackSafetyGlobalInfoWrapperPass
This pass performs the global (interprocedural) stack safety analysis (legacy pass manager).
Definition: StackSafetyAnalysis.h:150
llvm::StackSafetyGlobalInfo::stackAccessIsSafe
bool stackAccessIsSafe(const Instruction &I) const
Definition: StackSafetyAnalysis.cpp:972
llvm::StackSafetyInfo::getInfo
const InfoTy & getInfo() const
Definition: StackSafetyAnalysis.cpp:864
llvm::ModuleSummaryIndex
Class to hold module path string table and global value map, and encapsulate methods for operating on...
Definition: ModuleSummaryIndex.h:1078
llvm::StackSafetyGlobalInfoWrapperPass::runOnModule
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Definition: StackSafetyAnalysis.cpp:1081
llvm::StackSafetyPrinterPass::StackSafetyPrinterPass
StackSafetyPrinterPass(raw_ostream &OS)
Definition: StackSafetyAnalysis.h:106
llvm::StackSafetyGlobalInfo
Definition: StackSafetyAnalysis.h:58
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::StackSafetyGlobalInfo::print
void print(raw_ostream &O) const
Definition: StackSafetyAnalysis.cpp:977
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition: Instructions.h:62
llvm::StackSafetyInfo::InfoTy
Definition: StackSafetyAnalysis.cpp:227
llvm::StackSafetyGlobalInfoWrapperPass::~StackSafetyGlobalInfoWrapperPass
~StackSafetyGlobalInfoWrapperPass()
llvm::StackSafetyGlobalPrinterPass::StackSafetyGlobalPrinterPass
StackSafetyGlobalPrinterPass(raw_ostream &OS)
Definition: StackSafetyAnalysis.h:144
llvm::StackSafetyGlobalInfoWrapperPass::getResult
const StackSafetyGlobalInfo & getResult() const
Definition: StackSafetyAnalysis.h:159