LLVM 24.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
20namespace llvm {
21
22class AllocaInst;
23namespace cl {
25} // namespace cl
26class ScalarEvolution;
27
28/// Interface to access stack safety analysis results for single function.
30public:
31 struct InfoTy;
32
33private:
34 Function *F = nullptr;
35 std::function<ScalarEvolution &()> GetSE;
36 mutable std::unique_ptr<InfoTy> Info;
37
38public:
41 std::function<ScalarEvolution &()> GetSE);
45
46 LLVM_ABI const InfoTy &getInfo() const;
47
48 // TODO: Add useful for client methods.
49 LLVM_ABI void print(raw_ostream &O) const;
50
51 /// Parameters use for a FunctionSummary.
52 /// Function collects access information of all pointer parameters.
53 /// Information includes a range of direct access of parameters by the
54 /// functions and all call sites accepting the parameter.
55 /// StackSafety assumes that missing parameter information means possibility
56 /// of access to the parameter with any offset, so we can correctly link
57 /// code without StackSafety information, e.g. non-ThinLTO.
58 LLVM_ABI std::vector<FunctionSummary::ParamAccess>
60};
61
63public:
64 struct InfoTy;
65
66private:
67 Module *M = nullptr;
68 std::function<const StackSafetyInfo &(Function &F)> GetSSI;
69 const ModuleSummaryIndex *Index = nullptr;
70 mutable std::unique_ptr<InfoTy> Info;
71 const InfoTy &getInfo() const;
72
73public:
76 Module *M, std::function<const StackSafetyInfo &(Function &F)> GetSSI,
77 const ModuleSummaryIndex *Index);
81
82 // Whether we can prove that all accesses to this Alloca are in-range and
83 // during its lifetime.
84 LLVM_ABI bool isSafe(const AllocaInst &AI) const;
85
86 // Returns true if the instruction can be proven to do only two types of
87 // memory accesses:
88 // (1) live stack locations in-bounds or
89 // (2) non-stack locations.
90 LLVM_ABI bool stackAccessIsSafe(const Instruction &I) const;
91 LLVM_ABI void print(raw_ostream &O) const;
92 LLVM_ABI void dump() const;
93};
94
95/// StackSafetyInfo wrapper for the new pass manager.
96class StackSafetyAnalysis : public AnalysisInfoMixin<StackSafetyAnalysis> {
98 static AnalysisKey Key;
99
100public:
103};
104
105/// Printer pass for the \c StackSafetyAnalysis results.
107 : public RequiredPassInfoMixin<StackSafetyPrinterPass> {
108 raw_ostream &OS;
109
110public:
111 explicit StackSafetyPrinterPass(raw_ostream &OS) : OS(OS) {}
113};
114
115/// StackSafetyInfo wrapper for the legacy pass manager
117 StackSafetyInfo SSI;
118
119public:
120 static char ID;
122
123 const StackSafetyInfo &getResult() const { return SSI; }
124
125 void print(raw_ostream &O, const Module *M) const override;
126 void getAnalysisUsage(AnalysisUsage &AU) const override;
127
128 bool runOnFunction(Function &F) override;
129};
130
131/// This pass performs the global (interprocedural) stack safety analysis (new
132/// pass manager).
134 : public AnalysisInfoMixin<StackSafetyGlobalAnalysis> {
136 static AnalysisKey Key;
137
138public:
141};
142
143/// Printer pass for the \c StackSafetyGlobalAnalysis results.
145 : public RequiredPassInfoMixin<StackSafetyGlobalPrinterPass> {
146 raw_ostream &OS;
147
148public:
151};
152
153/// This pass performs the global (interprocedural) stack safety analysis
154/// (legacy pass manager).
157
158public:
159 static char ID;
160
163
164 const StackSafetyGlobalInfo &getResult() const { return SSGI; }
165
166 void print(raw_ostream &O, const Module *M) const override;
167 void getAnalysisUsage(AnalysisUsage &AU) const override;
168
169 bool runOnModule(Module &M) override;
170};
171
173
174/// Reads -stack-safety-max-iterations from LibraryOptions, or its global value
175/// if null.
176LLVM_ABI void
177generateParamAccessSummary(const cl::LibraryOptionsContext *LibraryOptions,
178 ModuleSummaryIndex &Index);
179
180} // end namespace llvm
181
182#endif // LLVM_ANALYSIS_STACKSAFETYANALYSIS_H
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_ABI
Definition Compiler.h:211
static bool runOnFunction(Function &F, bool PostInlining)
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
an instruction to allocate memory on the stack
Represent the analysis usage information of a pass.
FunctionPass(char &pid)
Definition Pass.h:314
ModulePass(char &pid)
Definition Pass.h:255
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
The main scalar evolution driver.
StackSafetyInfo wrapper for the new pass manager.
LLVM_ABI StackSafetyInfo run(Function &F, FunctionAnalysisManager &AM)
This pass performs the global (interprocedural) stack safety analysis (new pass manager).
LLVM_ABI Result run(Module &M, ModuleAnalysisManager &AM)
const StackSafetyGlobalInfo & getResult() const
LLVM_ABI StackSafetyGlobalInfo(StackSafetyGlobalInfo &&)
LLVM_ABI void print(raw_ostream &O) const
LLVM_ABI bool stackAccessIsSafe(const Instruction &I) const
LLVM_ABI bool isSafe(const AllocaInst &AI) const
LLVM_ABI StackSafetyGlobalInfo & operator=(StackSafetyGlobalInfo &&)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
const StackSafetyInfo & getResult() const
Interface to access stack safety analysis results for single function.
LLVM_ABI void print(raw_ostream &O) const
LLVM_ABI ~StackSafetyInfo()
LLVM_ABI const InfoTy & getInfo() const
LLVM_ABI StackSafetyInfo(StackSafetyInfo &&)
LLVM_ABI StackSafetyInfo()
LLVM_ABI StackSafetyInfo & operator=(StackSafetyInfo &&)
LLVM_ABI std::vector< FunctionSummary::ParamAccess > getParamAccesses(ModuleSummaryIndex &Index) const
Parameters use for a FunctionSummary.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This namespace contains all of the command line option processing machinery.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool needsParamAccessSummary(const Module &M)
LLVM_ABI void generateParamAccessSummary(const cl::LibraryOptionsContext *LibraryOptions, ModuleSummaryIndex &Index)
Reads -stack-safety-max-iterations from LibraryOptions, or its global value if null.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A CRTP mix-in for passes that should not be skipped.