LLVM 23.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;
23class ScalarEvolution;
24
25/// Interface to access stack safety analysis results for single function.
27public:
28 struct InfoTy;
29
30private:
31 Function *F = nullptr;
32 std::function<ScalarEvolution &()> GetSE;
33 mutable std::unique_ptr<InfoTy> Info;
34
35public:
38 std::function<ScalarEvolution &()> GetSE);
42
43 LLVM_ABI const InfoTy &getInfo() const;
44
45 // TODO: Add useful for client methods.
46 LLVM_ABI void print(raw_ostream &O) const;
47
48 /// Parameters use for a FunctionSummary.
49 /// Function collects access information of all pointer parameters.
50 /// Information includes a range of direct access of parameters by the
51 /// functions and all call sites accepting the parameter.
52 /// StackSafety assumes that missing parameter information means possibility
53 /// of access to the parameter with any offset, so we can correctly link
54 /// code without StackSafety information, e.g. non-ThinLTO.
55 LLVM_ABI std::vector<FunctionSummary::ParamAccess>
57};
58
60public:
61 struct InfoTy;
62
63private:
64 Module *M = nullptr;
65 std::function<const StackSafetyInfo &(Function &F)> GetSSI;
66 const ModuleSummaryIndex *Index = nullptr;
67 mutable std::unique_ptr<InfoTy> Info;
68 const InfoTy &getInfo() const;
69
70public:
73 Module *M, std::function<const StackSafetyInfo &(Function &F)> GetSSI,
74 const ModuleSummaryIndex *Index);
78
79 // Whether we can prove that all accesses to this Alloca are in-range and
80 // during its lifetime.
81 LLVM_ABI bool isSafe(const AllocaInst &AI) const;
82
83 // Returns true if the instruction can be proven to do only two types of
84 // memory accesses:
85 // (1) live stack locations in-bounds or
86 // (2) non-stack locations.
87 LLVM_ABI bool stackAccessIsSafe(const Instruction &I) const;
88 LLVM_ABI void print(raw_ostream &O) const;
89 LLVM_ABI void dump() const;
90};
91
92/// StackSafetyInfo wrapper for the new pass manager.
93class StackSafetyAnalysis : public AnalysisInfoMixin<StackSafetyAnalysis> {
95 static AnalysisKey Key;
96
97public:
100};
101
102/// Printer pass for the \c StackSafetyAnalysis results.
104 : public RequiredPassInfoMixin<StackSafetyPrinterPass> {
105 raw_ostream &OS;
106
107public:
108 explicit StackSafetyPrinterPass(raw_ostream &OS) : OS(OS) {}
110};
111
112/// StackSafetyInfo wrapper for the legacy pass manager
114 StackSafetyInfo SSI;
115
116public:
117 static char ID;
119
120 const StackSafetyInfo &getResult() const { return SSI; }
121
122 void print(raw_ostream &O, const Module *M) const override;
123 void getAnalysisUsage(AnalysisUsage &AU) const override;
124
125 bool runOnFunction(Function &F) override;
126};
127
128/// This pass performs the global (interprocedural) stack safety analysis (new
129/// pass manager).
131 : public AnalysisInfoMixin<StackSafetyGlobalAnalysis> {
133 static AnalysisKey Key;
134
135public:
138};
139
140/// Printer pass for the \c StackSafetyGlobalAnalysis results.
142 : public RequiredPassInfoMixin<StackSafetyGlobalPrinterPass> {
143 raw_ostream &OS;
144
145public:
148};
149
150/// This pass performs the global (interprocedural) stack safety analysis
151/// (legacy pass manager).
154
155public:
156 static char ID;
157
160
161 const StackSafetyGlobalInfo &getResult() const { return SSGI; }
162
163 void print(raw_ostream &O, const Module *M) const override;
164 void getAnalysisUsage(AnalysisUsage &AU) const override;
165
166 bool runOnModule(Module &M) override;
167};
168
170
171LLVM_ABI void generateParamAccessSummary(ModuleSummaryIndex &Index);
172
173} // end namespace llvm
174
175#endif // LLVM_ANALYSIS_STACKSAFETYANALYSIS_H
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_ABI
Definition Compiler.h:213
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:316
ModulePass(char &pid)
Definition Pass.h:257
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:67
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 is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void generateParamAccessSummary(ModuleSummaryIndex &Index)
LLVM_ABI bool needsParamAccessSummary(const Module &M)
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.