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