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