MachineRegisterInfo: Remove UsedPhysReg infrastructure
[oota-llvm.git] / include / llvm / CodeGen / MIRYamlMapping.h
1 //===- MIRYAMLMapping.h - Describes the mapping between MIR and YAML ------===//
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 // The MIR serialization library is currently a work in progress. It can't
11 // serialize machine functions at this time.
12 //
13 // This file implements the mapping between various MIR data structures and
14 // their corresponding YAML representation.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_LIB_CODEGEN_MIRYAMLMAPPING_H
19 #define LLVM_LIB_CODEGEN_MIRYAMLMAPPING_H
20
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/YAMLTraits.h"
23 #include <vector>
24
25 namespace llvm {
26 namespace yaml {
27
28 /// A wrapper around std::string which contains a source range that's being
29 /// set during parsing.
30 struct StringValue {
31   std::string Value;
32   SMRange SourceRange;
33
34   StringValue() {}
35   StringValue(std::string Value) : Value(std::move(Value)) {}
36
37   bool operator==(const StringValue &Other) const {
38     return Value == Other.Value;
39   }
40 };
41
42 template <> struct ScalarTraits<StringValue> {
43   static void output(const StringValue &S, void *, llvm::raw_ostream &OS) {
44     OS << S.Value;
45   }
46
47   static StringRef input(StringRef Scalar, void *Ctx, StringValue &S) {
48     S.Value = Scalar.str();
49     if (const auto *Node =
50             reinterpret_cast<yaml::Input *>(Ctx)->getCurrentNode())
51       S.SourceRange = Node->getSourceRange();
52     return "";
53   }
54
55   static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
56 };
57
58 struct FlowStringValue : StringValue {
59   FlowStringValue() {}
60   FlowStringValue(std::string Value) : StringValue(Value) {}
61 };
62
63 template <> struct ScalarTraits<FlowStringValue> {
64   static void output(const FlowStringValue &S, void *, llvm::raw_ostream &OS) {
65     return ScalarTraits<StringValue>::output(S, nullptr, OS);
66   }
67
68   static StringRef input(StringRef Scalar, void *Ctx, FlowStringValue &S) {
69     return ScalarTraits<StringValue>::input(Scalar, Ctx, S);
70   }
71
72   static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
73 };
74
75 } // end namespace yaml
76 } // end namespace llvm
77
78 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::StringValue)
79 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::FlowStringValue)
80
81 namespace llvm {
82 namespace yaml {
83
84 struct VirtualRegisterDefinition {
85   unsigned ID;
86   StringValue Class;
87   // TODO: Serialize the virtual register hints.
88 };
89
90 template <> struct MappingTraits<VirtualRegisterDefinition> {
91   static void mapping(IO &YamlIO, VirtualRegisterDefinition &Reg) {
92     YamlIO.mapRequired("id", Reg.ID);
93     YamlIO.mapRequired("class", Reg.Class);
94   }
95
96   static const bool flow = true;
97 };
98
99 struct MachineBasicBlock {
100   unsigned ID;
101   StringValue Name;
102   unsigned Alignment = 0;
103   bool IsLandingPad = false;
104   bool AddressTaken = false;
105   // TODO: Serialize the successor weights and liveins.
106   std::vector<FlowStringValue> Successors;
107
108   std::vector<StringValue> Instructions;
109 };
110
111 template <> struct MappingTraits<MachineBasicBlock> {
112   static void mapping(IO &YamlIO, MachineBasicBlock &MBB) {
113     YamlIO.mapRequired("id", MBB.ID);
114     YamlIO.mapOptional("name", MBB.Name,
115                        StringValue()); // Don't print out an empty name.
116     YamlIO.mapOptional("alignment", MBB.Alignment);
117     YamlIO.mapOptional("isLandingPad", MBB.IsLandingPad);
118     YamlIO.mapOptional("addressTaken", MBB.AddressTaken);
119     YamlIO.mapOptional("successors", MBB.Successors);
120     YamlIO.mapOptional("instructions", MBB.Instructions);
121   }
122 };
123
124 /// Serializable representation of stack object from the MachineFrameInfo class.
125 ///
126 /// The flags 'isImmutable' and 'isAliased' aren't serialized, as they are
127 /// determined by the object's type and frame information flags.
128 /// Dead stack objects aren't serialized.
129 ///
130 /// TODO: Determine isPreallocated flag by mapping between objects and local
131 /// objects (Serialize local objects).
132 struct MachineStackObject {
133   enum ObjectType { DefaultType, SpillSlot, VariableSized };
134   // TODO: Serialize LLVM alloca reference.
135   unsigned ID;
136   ObjectType Type = DefaultType;
137   int64_t Offset = 0;
138   uint64_t Size = 0;
139   unsigned Alignment = 0;
140 };
141
142 template <> struct ScalarEnumerationTraits<MachineStackObject::ObjectType> {
143   static void enumeration(yaml::IO &IO, MachineStackObject::ObjectType &Type) {
144     IO.enumCase(Type, "default", MachineStackObject::DefaultType);
145     IO.enumCase(Type, "spill-slot", MachineStackObject::SpillSlot);
146     IO.enumCase(Type, "variable-sized", MachineStackObject::VariableSized);
147   }
148 };
149
150 template <> struct MappingTraits<MachineStackObject> {
151   static void mapping(yaml::IO &YamlIO, MachineStackObject &Object) {
152     YamlIO.mapRequired("id", Object.ID);
153     YamlIO.mapOptional(
154         "type", Object.Type,
155         MachineStackObject::DefaultType); // Don't print the default type.
156     YamlIO.mapOptional("offset", Object.Offset);
157     if (Object.Type != MachineStackObject::VariableSized)
158       YamlIO.mapRequired("size", Object.Size);
159     YamlIO.mapOptional("alignment", Object.Alignment);
160   }
161
162   static const bool flow = true;
163 };
164
165 /// Serializable representation of the fixed stack object from the
166 /// MachineFrameInfo class.
167 struct FixedMachineStackObject {
168   enum ObjectType { DefaultType, SpillSlot };
169   unsigned ID;
170   ObjectType Type = DefaultType;
171   int64_t Offset = 0;
172   uint64_t Size = 0;
173   unsigned Alignment = 0;
174   bool IsImmutable = false;
175   bool IsAliased = false;
176 };
177
178 template <>
179 struct ScalarEnumerationTraits<FixedMachineStackObject::ObjectType> {
180   static void enumeration(yaml::IO &IO,
181                           FixedMachineStackObject::ObjectType &Type) {
182     IO.enumCase(Type, "default", FixedMachineStackObject::DefaultType);
183     IO.enumCase(Type, "spill-slot", FixedMachineStackObject::SpillSlot);
184   }
185 };
186
187 template <> struct MappingTraits<FixedMachineStackObject> {
188   static void mapping(yaml::IO &YamlIO, FixedMachineStackObject &Object) {
189     YamlIO.mapRequired("id", Object.ID);
190     YamlIO.mapOptional(
191         "type", Object.Type,
192         FixedMachineStackObject::DefaultType); // Don't print the default type.
193     YamlIO.mapOptional("offset", Object.Offset);
194     YamlIO.mapOptional("size", Object.Size);
195     YamlIO.mapOptional("alignment", Object.Alignment);
196     if (Object.Type != FixedMachineStackObject::SpillSlot) {
197       YamlIO.mapOptional("isImmutable", Object.IsImmutable);
198       YamlIO.mapOptional("isAliased", Object.IsAliased);
199     }
200   }
201
202   static const bool flow = true;
203 };
204
205 } // end namespace yaml
206 } // end namespace llvm
207
208 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::VirtualRegisterDefinition)
209 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineBasicBlock)
210 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineStackObject)
211 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::FixedMachineStackObject)
212
213 namespace llvm {
214 namespace yaml {
215
216 /// Serializable representation of MachineFrameInfo.
217 ///
218 /// Doesn't serialize attributes like 'StackAlignment', 'IsStackRealignable' and
219 /// 'RealignOption' as they are determined by the target and LLVM function
220 /// attributes.
221 /// It also doesn't serialize attributes like 'NumFixedObject' and
222 /// 'HasVarSizedObjects' as they are determined by the frame objects themselves.
223 struct MachineFrameInfo {
224   bool IsFrameAddressTaken = false;
225   bool IsReturnAddressTaken = false;
226   bool HasStackMap = false;
227   bool HasPatchPoint = false;
228   uint64_t StackSize = 0;
229   int OffsetAdjustment = 0;
230   unsigned MaxAlignment = 0;
231   bool AdjustsStack = false;
232   bool HasCalls = false;
233   // TODO: Serialize StackProtectorIdx and FunctionContextIdx
234   unsigned MaxCallFrameSize = 0;
235   // TODO: Serialize callee saved info.
236   // TODO: Serialize local frame objects.
237   bool HasOpaqueSPAdjustment = false;
238   bool HasVAStart = false;
239   bool HasMustTailInVarArgFunc = false;
240   // TODO: Serialize save and restore MBB references.
241 };
242
243 template <> struct MappingTraits<MachineFrameInfo> {
244   static void mapping(IO &YamlIO, MachineFrameInfo &MFI) {
245     YamlIO.mapOptional("isFrameAddressTaken", MFI.IsFrameAddressTaken);
246     YamlIO.mapOptional("isReturnAddressTaken", MFI.IsReturnAddressTaken);
247     YamlIO.mapOptional("hasStackMap", MFI.HasStackMap);
248     YamlIO.mapOptional("hasPatchPoint", MFI.HasPatchPoint);
249     YamlIO.mapOptional("stackSize", MFI.StackSize);
250     YamlIO.mapOptional("offsetAdjustment", MFI.OffsetAdjustment);
251     YamlIO.mapOptional("maxAlignment", MFI.MaxAlignment);
252     YamlIO.mapOptional("adjustsStack", MFI.AdjustsStack);
253     YamlIO.mapOptional("hasCalls", MFI.HasCalls);
254     YamlIO.mapOptional("maxCallFrameSize", MFI.MaxCallFrameSize);
255     YamlIO.mapOptional("hasOpaqueSPAdjustment", MFI.HasOpaqueSPAdjustment);
256     YamlIO.mapOptional("hasVAStart", MFI.HasVAStart);
257     YamlIO.mapOptional("hasMustTailInVarArgFunc", MFI.HasMustTailInVarArgFunc);
258   }
259 };
260
261 struct MachineFunction {
262   StringRef Name;
263   unsigned Alignment = 0;
264   bool ExposesReturnsTwice = false;
265   bool HasInlineAsm = false;
266   // Register information
267   bool IsSSA = false;
268   bool TracksRegLiveness = false;
269   bool TracksSubRegLiveness = false;
270   std::vector<VirtualRegisterDefinition> VirtualRegisters;
271   // TODO: Serialize the various register masks.
272   // TODO: Serialize live in registers.
273   // Frame information
274   MachineFrameInfo FrameInfo;
275   std::vector<FixedMachineStackObject> FixedStackObjects;
276   std::vector<MachineStackObject> StackObjects;
277
278   std::vector<MachineBasicBlock> BasicBlocks;
279 };
280
281 template <> struct MappingTraits<MachineFunction> {
282   static void mapping(IO &YamlIO, MachineFunction &MF) {
283     YamlIO.mapRequired("name", MF.Name);
284     YamlIO.mapOptional("alignment", MF.Alignment);
285     YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
286     YamlIO.mapOptional("hasInlineAsm", MF.HasInlineAsm);
287     YamlIO.mapOptional("isSSA", MF.IsSSA);
288     YamlIO.mapOptional("tracksRegLiveness", MF.TracksRegLiveness);
289     YamlIO.mapOptional("tracksSubRegLiveness", MF.TracksSubRegLiveness);
290     YamlIO.mapOptional("registers", MF.VirtualRegisters);
291     YamlIO.mapOptional("frameInfo", MF.FrameInfo);
292     YamlIO.mapOptional("fixedStack", MF.FixedStackObjects);
293     YamlIO.mapOptional("stack", MF.StackObjects);
294     YamlIO.mapOptional("body", MF.BasicBlocks);
295   }
296 };
297
298 } // end namespace yaml
299 } // end namespace llvm
300
301 #endif