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
16#include "llvm/IR/PassManager.h"
17#include "llvm/Pass.h"
18
19namespace llvm {
20
21/// Interface to access stack safety analysis results for single function.
22class StackSafetyInfo {
23public:
24  struct FunctionInfo;
25
26private:
27  std::unique_ptr<FunctionInfo> Info;
28
29public:
30  StackSafetyInfo();
31  StackSafetyInfo(FunctionInfo &&Info);
32  StackSafetyInfo(StackSafetyInfo &&);
33  StackSafetyInfo &operator=(StackSafetyInfo &&);
34  ~StackSafetyInfo();
35
36  // TODO: Add useful for client methods.
37  void print(raw_ostream &O) const;
38};
39
40/// StackSafetyInfo wrapper for the new pass manager.
41class StackSafetyAnalysis : public AnalysisInfoMixin<StackSafetyAnalysis> {
42  friend AnalysisInfoMixin<StackSafetyAnalysis>;
43  static AnalysisKey Key;
44
45public:
46  using Result = StackSafetyInfo;
47  StackSafetyInfo run(Function &F, FunctionAnalysisManager &AM);
48};
49
50/// Printer pass for the \c StackSafetyAnalysis results.
51class StackSafetyPrinterPass : public PassInfoMixin<StackSafetyPrinterPass> {
52  raw_ostream &OS;
53
54public:
55  explicit StackSafetyPrinterPass(raw_ostream &OS) : OS(OS) {}
56  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
57};
58
59/// StackSafetyInfo wrapper for the legacy pass manager
60class StackSafetyInfoWrapperPass : public FunctionPass {
61  StackSafetyInfo SSI;
62
63public:
64  static char ID;
65  StackSafetyInfoWrapperPass();
66
67  const StackSafetyInfo &getResult() const { return SSI; }
68
69  void print(raw_ostream &O, const Module *M) const override;
70  void getAnalysisUsage(AnalysisUsage &AU) const override;
71
72  bool runOnFunction(Function &F) override;
73};
74
75using StackSafetyGlobalInfo = std::map<const GlobalValue *, StackSafetyInfo>;
76
77/// This pass performs the global (interprocedural) stack safety analysis (new
78/// pass manager).
79class StackSafetyGlobalAnalysis
80    : public AnalysisInfoMixin<StackSafetyGlobalAnalysis> {
81  friend AnalysisInfoMixin<StackSafetyGlobalAnalysis>;
82  static AnalysisKey Key;
83
84public:
85  using Result = StackSafetyGlobalInfo;
86  Result run(Module &M, ModuleAnalysisManager &AM);
87};
88
89/// Printer pass for the \c StackSafetyGlobalAnalysis results.
90class StackSafetyGlobalPrinterPass
91    : public PassInfoMixin<StackSafetyGlobalPrinterPass> {
92  raw_ostream &OS;
93
94public:
95  explicit StackSafetyGlobalPrinterPass(raw_ostream &OS) : OS(OS) {}
96  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
97};
98
99/// This pass performs the global (interprocedural) stack safety analysis
100/// (legacy pass manager).
101class StackSafetyGlobalInfoWrapperPass : public ModulePass {
102  StackSafetyGlobalInfo SSI;
103
104public:
105  static char ID;
106
107  StackSafetyGlobalInfoWrapperPass();
108
109  const StackSafetyGlobalInfo &getResult() const { return SSI; }
110
111  void print(raw_ostream &O, const Module *M) const override;
112  void getAnalysisUsage(AnalysisUsage &AU) const override;
113
114  bool runOnModule(Module &M) override;
115};
116
117} // end namespace llvm
118
119#endif // LLVM_ANALYSIS_STACKSAFETYANALYSIS_H
120