LLVM 20.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:
37 StackSafetyInfo(Function *F, std::function<ScalarEvolution &()> GetSE);
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
59public:
60 struct InfoTy;
61
62private:
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
69public:
72 Module *M, std::function<const StackSafetyInfo &(Function &F)> GetSSI,
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.
92class StackSafetyAnalysis : public AnalysisInfoMixin<StackSafetyAnalysis> {
94 static AnalysisKey Key;
95
96public:
99};
100
101/// Printer pass for the \c StackSafetyAnalysis results.
102class StackSafetyPrinterPass : public PassInfoMixin<StackSafetyPrinterPass> {
103 raw_ostream &OS;
104
105public:
108 static bool isRequired() { return true; }
109};
110
111/// StackSafetyInfo wrapper for the legacy pass manager
113 StackSafetyInfo SSI;
114
115public:
116 static char ID;
118
119 const StackSafetyInfo &getResult() const { return SSI; }
120
121 void print(raw_ostream &O, const Module *M) const override;
122 void getAnalysisUsage(AnalysisUsage &AU) const override;
123
124 bool runOnFunction(Function &F) override;
125};
126
127/// This pass performs the global (interprocedural) stack safety analysis (new
128/// pass manager).
130 : public AnalysisInfoMixin<StackSafetyGlobalAnalysis> {
132 static AnalysisKey Key;
133
134public:
137};
138
139/// Printer pass for the \c StackSafetyGlobalAnalysis results.
141 : public PassInfoMixin<StackSafetyGlobalPrinterPass> {
142 raw_ostream &OS;
143
144public:
147 static bool isRequired() { return true; }
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
169bool needsParamAccessSummary(const Module &M);
170
171void generateParamAccessSummary(ModuleSummaryIndex &Index);
172
173} // end namespace llvm
174
175#endif // LLVM_ANALYSIS_STACKSAFETYANALYSIS_H
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
This header defines various interfaces for pass management in LLVM.
raw_pwrite_stream & OS
an instruction to allocate memory on the stack
Definition: Instructions.h:61
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
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:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
The main scalar evolution driver.
StackSafetyInfo wrapper for the new pass manager.
StackSafetyInfo run(Function &F, FunctionAnalysisManager &AM)
This pass performs the global (interprocedural) stack safety analysis (new pass manager).
Result run(Module &M, ModuleAnalysisManager &AM)
This pass performs the global (interprocedural) stack safety analysis (legacy pass manager).
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
const StackSafetyGlobalInfo & getResult() const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void print(raw_ostream &O, const Module *M) const override
print - Print out the internal state of the pass.
StackSafetyGlobalInfo(StackSafetyGlobalInfo &&)
void print(raw_ostream &O) const
bool stackAccessIsSafe(const Instruction &I) const
bool isSafe(const AllocaInst &AI) const
StackSafetyGlobalInfo & operator=(StackSafetyGlobalInfo &&)
Printer pass for the StackSafetyGlobalAnalysis results.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
StackSafetyInfo wrapper for the legacy pass manager.
void print(raw_ostream &O, const Module *M) const override
print - Print out the internal state of the pass.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
const StackSafetyInfo & getResult() const
Interface to access stack safety analysis results for single function.
void print(raw_ostream &O) const
const InfoTy & getInfo() const
StackSafetyInfo(StackSafetyInfo &&)
StackSafetyInfo & operator=(StackSafetyInfo &&)
std::vector< FunctionSummary::ParamAccess > getParamAccesses(ModuleSummaryIndex &Index) const
Parameters use for a FunctionSummary.
Printer pass for the StackSafetyAnalysis results.
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:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void generateParamAccessSummary(ModuleSummaryIndex &Index)
bool needsParamAccessSummary(const Module &M)
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69