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