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