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