• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- llvm/CodeGen/PseudoSourceValue.h ------------------------*- 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 // This file contains the declaration of the PseudoSourceValue class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CODEGEN_PSEUDOSOURCEVALUE_H
15 #define LLVM_CODEGEN_PSEUDOSOURCEVALUE_H
16 
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/IR/Value.h"
20 #include "llvm/IR/ValueMap.h"
21 #include <map>
22 
23 namespace llvm {
24 
25 class MachineFrameInfo;
26 class MachineMemOperand;
27 class raw_ostream;
28 
29 raw_ostream &operator<<(raw_ostream &OS, const MachineMemOperand &MMO);
30 class PseudoSourceValue;
31 raw_ostream &operator<<(raw_ostream &OS, const PseudoSourceValue* PSV);
32 
33 /// Special value supplied for machine level alias analysis. It indicates that
34 /// a memory access references the functions stack frame (e.g., a spill slot),
35 /// below the stack frame (e.g., argument space), or constant pool.
36 class PseudoSourceValue {
37 public:
38   enum PSVKind {
39     Stack,
40     GOT,
41     JumpTable,
42     ConstantPool,
43     FixedStack,
44     GlobalValueCallEntry,
45     ExternalSymbolCallEntry
46   };
47 
48 private:
49   PSVKind Kind;
50   friend raw_ostream &llvm::operator<<(raw_ostream &OS,
51                                        const PseudoSourceValue* PSV);
52 
53   friend class MachineMemOperand; // For printCustom().
54 
55   /// Implement printing for PseudoSourceValue. This is called from
56   /// Value::print or Value's operator<<.
57   virtual void printCustom(raw_ostream &O) const;
58 
59 public:
60   explicit PseudoSourceValue(PSVKind Kind);
61 
62   virtual ~PseudoSourceValue();
63 
kind()64   PSVKind kind() const { return Kind; }
65 
isStack()66   bool isStack() const { return Kind == Stack; }
isGOT()67   bool isGOT() const { return Kind == GOT; }
isConstantPool()68   bool isConstantPool() const { return Kind == ConstantPool; }
isJumpTable()69   bool isJumpTable() const { return Kind == JumpTable; }
70 
71   /// Test whether the memory pointed to by this PseudoSourceValue has a
72   /// constant value.
73   virtual bool isConstant(const MachineFrameInfo *) const;
74 
75   /// Test whether the memory pointed to by this PseudoSourceValue may also be
76   /// pointed to by an LLVM IR Value.
77   virtual bool isAliased(const MachineFrameInfo *) const;
78 
79   /// Return true if the memory pointed to by this PseudoSourceValue can ever
80   /// alias an LLVM IR Value.
81   virtual bool mayAlias(const MachineFrameInfo *) const;
82 };
83 
84 /// A specialized PseudoSourceValue for holding FixedStack values, which must
85 /// include a frame index.
86 class FixedStackPseudoSourceValue : public PseudoSourceValue {
87   const int FI;
88 
89 public:
FixedStackPseudoSourceValue(int FI)90   explicit FixedStackPseudoSourceValue(int FI)
91       : PseudoSourceValue(FixedStack), FI(FI) {}
92 
classof(const PseudoSourceValue * V)93   static inline bool classof(const PseudoSourceValue *V) {
94     return V->kind() == FixedStack;
95   }
96 
97   bool isConstant(const MachineFrameInfo *MFI) const override;
98 
99   bool isAliased(const MachineFrameInfo *MFI) const override;
100 
101   bool mayAlias(const MachineFrameInfo *) const override;
102 
103   void printCustom(raw_ostream &OS) const override;
104 
getFrameIndex()105   int getFrameIndex() const { return FI; }
106 };
107 
108 class CallEntryPseudoSourceValue : public PseudoSourceValue {
109 protected:
110   CallEntryPseudoSourceValue(PSVKind Kind);
111 
112 public:
113   bool isConstant(const MachineFrameInfo *) const override;
114   bool isAliased(const MachineFrameInfo *) const override;
115   bool mayAlias(const MachineFrameInfo *) const override;
116 };
117 
118 /// A specialized pseudo soruce value for holding GlobalValue values.
119 class GlobalValuePseudoSourceValue : public CallEntryPseudoSourceValue {
120   const GlobalValue *GV;
121 
122 public:
123   GlobalValuePseudoSourceValue(const GlobalValue *GV);
124 
classof(const PseudoSourceValue * V)125   static inline bool classof(const PseudoSourceValue *V) {
126     return V->kind() == GlobalValueCallEntry;
127   }
128 
getValue()129   const GlobalValue *getValue() const { return GV; }
130 };
131 
132 /// A specialized pseudo source value for holding external symbol values.
133 class ExternalSymbolPseudoSourceValue : public CallEntryPseudoSourceValue {
134   const char *ES;
135 
136 public:
137   ExternalSymbolPseudoSourceValue(const char *ES);
138 
classof(const PseudoSourceValue * V)139   static inline bool classof(const PseudoSourceValue *V) {
140     return V->kind() == ExternalSymbolCallEntry;
141   }
142 
getSymbol()143   const char *getSymbol() const { return ES; }
144 };
145 
146 /// Manages creation of pseudo source values.
147 class PseudoSourceValueManager {
148   const PseudoSourceValue StackPSV, GOTPSV, JumpTablePSV, ConstantPoolPSV;
149   std::map<int, std::unique_ptr<FixedStackPseudoSourceValue>> FSValues;
150   StringMap<std::unique_ptr<const ExternalSymbolPseudoSourceValue>>
151       ExternalCallEntries;
152   ValueMap<const GlobalValue *,
153            std::unique_ptr<const GlobalValuePseudoSourceValue>>
154       GlobalCallEntries;
155 
156 public:
157   PseudoSourceValueManager();
158 
159   /// Return a pseudo source value referencing the area below the stack frame of
160   /// a function, e.g., the argument space.
161   const PseudoSourceValue *getStack();
162 
163   /// Return a pseudo source value referencing the global offset table
164   /// (or something the like).
165   const PseudoSourceValue *getGOT();
166 
167   /// Return a pseudo source value referencing the constant pool. Since constant
168   /// pools are constant, this doesn't need to identify a specific constant
169   /// pool entry.
170   const PseudoSourceValue *getConstantPool();
171 
172   /// Return a pseudo source value referencing a jump table. Since jump tables
173   /// are constant, this doesn't need to identify a specific jump table.
174   const PseudoSourceValue *getJumpTable();
175 
176   /// Return a pseudo source value referencing a fixed stack frame entry,
177   /// e.g., a spill slot.
178   const PseudoSourceValue *getFixedStack(int FI);
179 
180   const PseudoSourceValue *getGlobalValueCallEntry(const GlobalValue *GV);
181 
182   const PseudoSourceValue *getExternalSymbolCallEntry(const char *ES);
183 };
184 
185 } // end namespace llvm
186 
187 #endif
188