MIRYamlMapping.h revision 353358
1//===- MIRYamlMapping.h - Describe mapping between MIR and YAML--*- 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 implements the mapping between various MIR data structures and
10// their corresponding YAML representation.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_MIRYAMLMAPPING_H
15#define LLVM_CODEGEN_MIRYAMLMAPPING_H
16
17#include "llvm/ADT/Optional.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/CodeGen/MachineJumpTableInfo.h"
20#include "llvm/CodeGen/TargetFrameLowering.h"
21#include "llvm/Support/SMLoc.h"
22#include "llvm/Support/YAMLTraits.h"
23#include "llvm/Support/raw_ostream.h"
24#include <algorithm>
25#include <cstdint>
26#include <string>
27#include <vector>
28
29namespace llvm {
30namespace yaml {
31
32/// A wrapper around std::string which contains a source range that's being
33/// set during parsing.
34struct StringValue {
35  std::string Value;
36  SMRange SourceRange;
37
38  StringValue() = default;
39  StringValue(std::string Value) : Value(std::move(Value)) {}
40  StringValue(const char Val[]) : Value(Val) {}
41
42  bool operator==(const StringValue &Other) const {
43    return Value == Other.Value;
44  }
45};
46
47template <> struct ScalarTraits<StringValue> {
48  static void output(const StringValue &S, void *, raw_ostream &OS) {
49    OS << S.Value;
50  }
51
52  static StringRef input(StringRef Scalar, void *Ctx, StringValue &S) {
53    S.Value = Scalar.str();
54    if (const auto *Node =
55            reinterpret_cast<yaml::Input *>(Ctx)->getCurrentNode())
56      S.SourceRange = Node->getSourceRange();
57    return "";
58  }
59
60  static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
61};
62
63struct FlowStringValue : StringValue {
64  FlowStringValue() = default;
65  FlowStringValue(std::string Value) : StringValue(std::move(Value)) {}
66};
67
68template <> struct ScalarTraits<FlowStringValue> {
69  static void output(const FlowStringValue &S, void *, raw_ostream &OS) {
70    return ScalarTraits<StringValue>::output(S, nullptr, OS);
71  }
72
73  static StringRef input(StringRef Scalar, void *Ctx, FlowStringValue &S) {
74    return ScalarTraits<StringValue>::input(Scalar, Ctx, S);
75  }
76
77  static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
78};
79
80struct BlockStringValue {
81  StringValue Value;
82
83  bool operator==(const BlockStringValue &Other) const {
84    return Value == Other.Value;
85  }
86};
87
88template <> struct BlockScalarTraits<BlockStringValue> {
89  static void output(const BlockStringValue &S, void *Ctx, raw_ostream &OS) {
90    return ScalarTraits<StringValue>::output(S.Value, Ctx, OS);
91  }
92
93  static StringRef input(StringRef Scalar, void *Ctx, BlockStringValue &S) {
94    return ScalarTraits<StringValue>::input(Scalar, Ctx, S.Value);
95  }
96};
97
98/// A wrapper around unsigned which contains a source range that's being set
99/// during parsing.
100struct UnsignedValue {
101  unsigned Value = 0;
102  SMRange SourceRange;
103
104  UnsignedValue() = default;
105  UnsignedValue(unsigned Value) : Value(Value) {}
106
107  bool operator==(const UnsignedValue &Other) const {
108    return Value == Other.Value;
109  }
110};
111
112template <> struct ScalarTraits<UnsignedValue> {
113  static void output(const UnsignedValue &Value, void *Ctx, raw_ostream &OS) {
114    return ScalarTraits<unsigned>::output(Value.Value, Ctx, OS);
115  }
116
117  static StringRef input(StringRef Scalar, void *Ctx, UnsignedValue &Value) {
118    if (const auto *Node =
119            reinterpret_cast<yaml::Input *>(Ctx)->getCurrentNode())
120      Value.SourceRange = Node->getSourceRange();
121    return ScalarTraits<unsigned>::input(Scalar, Ctx, Value.Value);
122  }
123
124  static QuotingType mustQuote(StringRef Scalar) {
125    return ScalarTraits<unsigned>::mustQuote(Scalar);
126  }
127};
128
129template <> struct ScalarEnumerationTraits<MachineJumpTableInfo::JTEntryKind> {
130  static void enumeration(yaml::IO &IO,
131                          MachineJumpTableInfo::JTEntryKind &EntryKind) {
132    IO.enumCase(EntryKind, "block-address",
133                MachineJumpTableInfo::EK_BlockAddress);
134    IO.enumCase(EntryKind, "gp-rel64-block-address",
135                MachineJumpTableInfo::EK_GPRel64BlockAddress);
136    IO.enumCase(EntryKind, "gp-rel32-block-address",
137                MachineJumpTableInfo::EK_GPRel32BlockAddress);
138    IO.enumCase(EntryKind, "label-difference32",
139                MachineJumpTableInfo::EK_LabelDifference32);
140    IO.enumCase(EntryKind, "inline", MachineJumpTableInfo::EK_Inline);
141    IO.enumCase(EntryKind, "custom32", MachineJumpTableInfo::EK_Custom32);
142  }
143};
144
145} // end namespace yaml
146} // end namespace llvm
147
148LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::StringValue)
149LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::FlowStringValue)
150LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::UnsignedValue)
151
152namespace llvm {
153namespace yaml {
154
155struct VirtualRegisterDefinition {
156  UnsignedValue ID;
157  StringValue Class;
158  StringValue PreferredRegister;
159
160  // TODO: Serialize the target specific register hints.
161
162  bool operator==(const VirtualRegisterDefinition &Other) const {
163    return ID == Other.ID && Class == Other.Class &&
164           PreferredRegister == Other.PreferredRegister;
165  }
166};
167
168template <> struct MappingTraits<VirtualRegisterDefinition> {
169  static void mapping(IO &YamlIO, VirtualRegisterDefinition &Reg) {
170    YamlIO.mapRequired("id", Reg.ID);
171    YamlIO.mapRequired("class", Reg.Class);
172    YamlIO.mapOptional("preferred-register", Reg.PreferredRegister,
173                       StringValue()); // Don't print out when it's empty.
174  }
175
176  static const bool flow = true;
177};
178
179struct MachineFunctionLiveIn {
180  StringValue Register;
181  StringValue VirtualRegister;
182
183  bool operator==(const MachineFunctionLiveIn &Other) const {
184    return Register == Other.Register &&
185           VirtualRegister == Other.VirtualRegister;
186  }
187};
188
189template <> struct MappingTraits<MachineFunctionLiveIn> {
190  static void mapping(IO &YamlIO, MachineFunctionLiveIn &LiveIn) {
191    YamlIO.mapRequired("reg", LiveIn.Register);
192    YamlIO.mapOptional(
193        "virtual-reg", LiveIn.VirtualRegister,
194        StringValue()); // Don't print the virtual register when it's empty.
195  }
196
197  static const bool flow = true;
198};
199
200/// Serializable representation of stack object from the MachineFrameInfo class.
201///
202/// The flags 'isImmutable' and 'isAliased' aren't serialized, as they are
203/// determined by the object's type and frame information flags.
204/// Dead stack objects aren't serialized.
205///
206/// The 'isPreallocated' flag is determined by the local offset.
207struct MachineStackObject {
208  enum ObjectType { DefaultType, SpillSlot, VariableSized };
209  UnsignedValue ID;
210  StringValue Name;
211  // TODO: Serialize unnamed LLVM alloca reference.
212  ObjectType Type = DefaultType;
213  int64_t Offset = 0;
214  uint64_t Size = 0;
215  unsigned Alignment = 0;
216  TargetStackID::Value StackID;
217  StringValue CalleeSavedRegister;
218  bool CalleeSavedRestored = true;
219  Optional<int64_t> LocalOffset;
220  StringValue DebugVar;
221  StringValue DebugExpr;
222  StringValue DebugLoc;
223
224  bool operator==(const MachineStackObject &Other) const {
225    return ID == Other.ID && Name == Other.Name && Type == Other.Type &&
226           Offset == Other.Offset && Size == Other.Size &&
227           Alignment == Other.Alignment &&
228           StackID == Other.StackID &&
229           CalleeSavedRegister == Other.CalleeSavedRegister &&
230           CalleeSavedRestored == Other.CalleeSavedRestored &&
231           LocalOffset == Other.LocalOffset && DebugVar == Other.DebugVar &&
232           DebugExpr == Other.DebugExpr && DebugLoc == Other.DebugLoc;
233  }
234};
235
236template <> struct ScalarEnumerationTraits<MachineStackObject::ObjectType> {
237  static void enumeration(yaml::IO &IO, MachineStackObject::ObjectType &Type) {
238    IO.enumCase(Type, "default", MachineStackObject::DefaultType);
239    IO.enumCase(Type, "spill-slot", MachineStackObject::SpillSlot);
240    IO.enumCase(Type, "variable-sized", MachineStackObject::VariableSized);
241  }
242};
243
244template <> struct MappingTraits<MachineStackObject> {
245  static void mapping(yaml::IO &YamlIO, MachineStackObject &Object) {
246    YamlIO.mapRequired("id", Object.ID);
247    YamlIO.mapOptional("name", Object.Name,
248                       StringValue()); // Don't print out an empty name.
249    YamlIO.mapOptional(
250        "type", Object.Type,
251        MachineStackObject::DefaultType); // Don't print the default type.
252    YamlIO.mapOptional("offset", Object.Offset, (int64_t)0);
253    if (Object.Type != MachineStackObject::VariableSized)
254      YamlIO.mapRequired("size", Object.Size);
255    YamlIO.mapOptional("alignment", Object.Alignment, (unsigned)0);
256    YamlIO.mapOptional("stack-id", Object.StackID, TargetStackID::Default);
257    YamlIO.mapOptional("callee-saved-register", Object.CalleeSavedRegister,
258                       StringValue()); // Don't print it out when it's empty.
259    YamlIO.mapOptional("callee-saved-restored", Object.CalleeSavedRestored,
260                       true);
261    YamlIO.mapOptional("local-offset", Object.LocalOffset, Optional<int64_t>());
262    YamlIO.mapOptional("debug-info-variable", Object.DebugVar,
263                       StringValue()); // Don't print it out when it's empty.
264    YamlIO.mapOptional("debug-info-expression", Object.DebugExpr,
265                       StringValue()); // Don't print it out when it's empty.
266    YamlIO.mapOptional("debug-info-location", Object.DebugLoc,
267                       StringValue()); // Don't print it out when it's empty.
268  }
269
270  static const bool flow = true;
271};
272
273/// Serializable representation of the fixed stack object from the
274/// MachineFrameInfo class.
275struct FixedMachineStackObject {
276  enum ObjectType { DefaultType, SpillSlot };
277  UnsignedValue ID;
278  ObjectType Type = DefaultType;
279  int64_t Offset = 0;
280  uint64_t Size = 0;
281  unsigned Alignment = 0;
282  TargetStackID::Value StackID;
283  bool IsImmutable = false;
284  bool IsAliased = false;
285  StringValue CalleeSavedRegister;
286  bool CalleeSavedRestored = true;
287  StringValue DebugVar;
288  StringValue DebugExpr;
289  StringValue DebugLoc;
290
291  bool operator==(const FixedMachineStackObject &Other) const {
292    return ID == Other.ID && Type == Other.Type && Offset == Other.Offset &&
293           Size == Other.Size && Alignment == Other.Alignment &&
294           StackID == Other.StackID &&
295           IsImmutable == Other.IsImmutable && IsAliased == Other.IsAliased &&
296           CalleeSavedRegister == Other.CalleeSavedRegister &&
297           CalleeSavedRestored == Other.CalleeSavedRestored &&
298           DebugVar == Other.DebugVar && DebugExpr == Other.DebugExpr
299           && DebugLoc == Other.DebugLoc;
300  }
301};
302
303template <>
304struct ScalarEnumerationTraits<FixedMachineStackObject::ObjectType> {
305  static void enumeration(yaml::IO &IO,
306                          FixedMachineStackObject::ObjectType &Type) {
307    IO.enumCase(Type, "default", FixedMachineStackObject::DefaultType);
308    IO.enumCase(Type, "spill-slot", FixedMachineStackObject::SpillSlot);
309  }
310};
311
312template <>
313struct ScalarEnumerationTraits<TargetStackID::Value> {
314  static void enumeration(yaml::IO &IO, TargetStackID::Value &ID) {
315    IO.enumCase(ID, "default", TargetStackID::Default);
316    IO.enumCase(ID, "sgpr-spill", TargetStackID::SGPRSpill);
317    IO.enumCase(ID, "noalloc", TargetStackID::NoAlloc);
318  }
319};
320
321template <> struct MappingTraits<FixedMachineStackObject> {
322  static void mapping(yaml::IO &YamlIO, FixedMachineStackObject &Object) {
323    YamlIO.mapRequired("id", Object.ID);
324    YamlIO.mapOptional(
325        "type", Object.Type,
326        FixedMachineStackObject::DefaultType); // Don't print the default type.
327    YamlIO.mapOptional("offset", Object.Offset, (int64_t)0);
328    YamlIO.mapOptional("size", Object.Size, (uint64_t)0);
329    YamlIO.mapOptional("alignment", Object.Alignment, (unsigned)0);
330    YamlIO.mapOptional("stack-id", Object.StackID, TargetStackID::Default);
331    if (Object.Type != FixedMachineStackObject::SpillSlot) {
332      YamlIO.mapOptional("isImmutable", Object.IsImmutable, false);
333      YamlIO.mapOptional("isAliased", Object.IsAliased, false);
334    }
335    YamlIO.mapOptional("callee-saved-register", Object.CalleeSavedRegister,
336                       StringValue()); // Don't print it out when it's empty.
337    YamlIO.mapOptional("callee-saved-restored", Object.CalleeSavedRestored,
338                     true);
339    YamlIO.mapOptional("debug-info-variable", Object.DebugVar,
340                       StringValue()); // Don't print it out when it's empty.
341    YamlIO.mapOptional("debug-info-expression", Object.DebugExpr,
342                       StringValue()); // Don't print it out when it's empty.
343    YamlIO.mapOptional("debug-info-location", Object.DebugLoc,
344                       StringValue()); // Don't print it out when it's empty.
345  }
346
347  static const bool flow = true;
348};
349
350
351/// Serializable representation of CallSiteInfo.
352struct CallSiteInfo {
353  // Representation of call argument and register which is used to
354  // transfer it.
355  struct ArgRegPair {
356    StringValue Reg;
357    uint16_t ArgNo;
358
359    bool operator==(const ArgRegPair &Other) const {
360      return Reg == Other.Reg && ArgNo == Other.ArgNo;
361    }
362  };
363
364  /// Identifies call instruction location in machine function.
365  struct MachineInstrLoc {
366    unsigned BlockNum;
367    unsigned Offset;
368
369    bool operator==(const MachineInstrLoc &Other) const {
370      return BlockNum == Other.BlockNum && Offset == Other.Offset;
371    }
372  };
373
374  MachineInstrLoc CallLocation;
375  std::vector<ArgRegPair> ArgForwardingRegs;
376
377  bool operator==(const CallSiteInfo &Other) const {
378    return CallLocation.BlockNum == Other.CallLocation.BlockNum &&
379           CallLocation.Offset == Other.CallLocation.Offset;
380  }
381};
382
383template <> struct MappingTraits<CallSiteInfo::ArgRegPair> {
384  static void mapping(IO &YamlIO, CallSiteInfo::ArgRegPair &ArgReg) {
385    YamlIO.mapRequired("arg", ArgReg.ArgNo);
386    YamlIO.mapRequired("reg", ArgReg.Reg);
387  }
388
389  static const bool flow = true;
390};
391}
392}
393
394LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::CallSiteInfo::ArgRegPair)
395
396namespace llvm {
397namespace yaml {
398
399template <> struct MappingTraits<CallSiteInfo> {
400  static void mapping(IO &YamlIO, CallSiteInfo &CSInfo) {
401    YamlIO.mapRequired("bb", CSInfo.CallLocation.BlockNum);
402    YamlIO.mapRequired("offset", CSInfo.CallLocation.Offset);
403    YamlIO.mapOptional("fwdArgRegs", CSInfo.ArgForwardingRegs,
404                       std::vector<CallSiteInfo::ArgRegPair>());
405  }
406
407  static const bool flow = true;
408};
409
410struct MachineConstantPoolValue {
411  UnsignedValue ID;
412  StringValue Value;
413  unsigned Alignment = 0;
414  bool IsTargetSpecific = false;
415
416  bool operator==(const MachineConstantPoolValue &Other) const {
417    return ID == Other.ID && Value == Other.Value &&
418           Alignment == Other.Alignment &&
419           IsTargetSpecific == Other.IsTargetSpecific;
420  }
421};
422
423template <> struct MappingTraits<MachineConstantPoolValue> {
424  static void mapping(IO &YamlIO, MachineConstantPoolValue &Constant) {
425    YamlIO.mapRequired("id", Constant.ID);
426    YamlIO.mapOptional("value", Constant.Value, StringValue());
427    YamlIO.mapOptional("alignment", Constant.Alignment, (unsigned)0);
428    YamlIO.mapOptional("isTargetSpecific", Constant.IsTargetSpecific, false);
429  }
430};
431
432struct MachineJumpTable {
433  struct Entry {
434    UnsignedValue ID;
435    std::vector<FlowStringValue> Blocks;
436
437    bool operator==(const Entry &Other) const {
438      return ID == Other.ID && Blocks == Other.Blocks;
439    }
440  };
441
442  MachineJumpTableInfo::JTEntryKind Kind = MachineJumpTableInfo::EK_Custom32;
443  std::vector<Entry> Entries;
444
445  bool operator==(const MachineJumpTable &Other) const {
446    return Kind == Other.Kind && Entries == Other.Entries;
447  }
448};
449
450template <> struct MappingTraits<MachineJumpTable::Entry> {
451  static void mapping(IO &YamlIO, MachineJumpTable::Entry &Entry) {
452    YamlIO.mapRequired("id", Entry.ID);
453    YamlIO.mapOptional("blocks", Entry.Blocks, std::vector<FlowStringValue>());
454  }
455};
456
457} // end namespace yaml
458} // end namespace llvm
459
460LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineFunctionLiveIn)
461LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::VirtualRegisterDefinition)
462LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineStackObject)
463LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::FixedMachineStackObject)
464LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::CallSiteInfo)
465LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineConstantPoolValue)
466LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineJumpTable::Entry)
467
468namespace llvm {
469namespace yaml {
470
471template <> struct MappingTraits<MachineJumpTable> {
472  static void mapping(IO &YamlIO, MachineJumpTable &JT) {
473    YamlIO.mapRequired("kind", JT.Kind);
474    YamlIO.mapOptional("entries", JT.Entries,
475                       std::vector<MachineJumpTable::Entry>());
476  }
477};
478
479/// Serializable representation of MachineFrameInfo.
480///
481/// Doesn't serialize attributes like 'StackAlignment', 'IsStackRealignable' and
482/// 'RealignOption' as they are determined by the target and LLVM function
483/// attributes.
484/// It also doesn't serialize attributes like 'NumFixedObject' and
485/// 'HasVarSizedObjects' as they are determined by the frame objects themselves.
486struct MachineFrameInfo {
487  bool IsFrameAddressTaken = false;
488  bool IsReturnAddressTaken = false;
489  bool HasStackMap = false;
490  bool HasPatchPoint = false;
491  uint64_t StackSize = 0;
492  int OffsetAdjustment = 0;
493  unsigned MaxAlignment = 0;
494  bool AdjustsStack = false;
495  bool HasCalls = false;
496  StringValue StackProtector;
497  // TODO: Serialize FunctionContextIdx
498  unsigned MaxCallFrameSize = ~0u; ///< ~0u means: not computed yet.
499  unsigned CVBytesOfCalleeSavedRegisters = 0;
500  bool HasOpaqueSPAdjustment = false;
501  bool HasVAStart = false;
502  bool HasMustTailInVarArgFunc = false;
503  unsigned LocalFrameSize = 0;
504  StringValue SavePoint;
505  StringValue RestorePoint;
506
507  bool operator==(const MachineFrameInfo &Other) const {
508    return IsFrameAddressTaken == Other.IsFrameAddressTaken &&
509           IsReturnAddressTaken == Other.IsReturnAddressTaken &&
510           HasStackMap == Other.HasStackMap &&
511           HasPatchPoint == Other.HasPatchPoint &&
512           StackSize == Other.StackSize &&
513           OffsetAdjustment == Other.OffsetAdjustment &&
514           MaxAlignment == Other.MaxAlignment &&
515           AdjustsStack == Other.AdjustsStack && HasCalls == Other.HasCalls &&
516           StackProtector == Other.StackProtector &&
517           MaxCallFrameSize == Other.MaxCallFrameSize &&
518           CVBytesOfCalleeSavedRegisters ==
519               Other.CVBytesOfCalleeSavedRegisters &&
520           HasOpaqueSPAdjustment == Other.HasOpaqueSPAdjustment &&
521           HasVAStart == Other.HasVAStart &&
522           HasMustTailInVarArgFunc == Other.HasMustTailInVarArgFunc &&
523           LocalFrameSize == Other.LocalFrameSize &&
524           SavePoint == Other.SavePoint && RestorePoint == Other.RestorePoint;
525  }
526};
527
528template <> struct MappingTraits<MachineFrameInfo> {
529  static void mapping(IO &YamlIO, MachineFrameInfo &MFI) {
530    YamlIO.mapOptional("isFrameAddressTaken", MFI.IsFrameAddressTaken, false);
531    YamlIO.mapOptional("isReturnAddressTaken", MFI.IsReturnAddressTaken, false);
532    YamlIO.mapOptional("hasStackMap", MFI.HasStackMap, false);
533    YamlIO.mapOptional("hasPatchPoint", MFI.HasPatchPoint, false);
534    YamlIO.mapOptional("stackSize", MFI.StackSize, (uint64_t)0);
535    YamlIO.mapOptional("offsetAdjustment", MFI.OffsetAdjustment, (int)0);
536    YamlIO.mapOptional("maxAlignment", MFI.MaxAlignment, (unsigned)0);
537    YamlIO.mapOptional("adjustsStack", MFI.AdjustsStack, false);
538    YamlIO.mapOptional("hasCalls", MFI.HasCalls, false);
539    YamlIO.mapOptional("stackProtector", MFI.StackProtector,
540                       StringValue()); // Don't print it out when it's empty.
541    YamlIO.mapOptional("maxCallFrameSize", MFI.MaxCallFrameSize, (unsigned)~0);
542    YamlIO.mapOptional("cvBytesOfCalleeSavedRegisters",
543                       MFI.CVBytesOfCalleeSavedRegisters, 0U);
544    YamlIO.mapOptional("hasOpaqueSPAdjustment", MFI.HasOpaqueSPAdjustment,
545                       false);
546    YamlIO.mapOptional("hasVAStart", MFI.HasVAStart, false);
547    YamlIO.mapOptional("hasMustTailInVarArgFunc", MFI.HasMustTailInVarArgFunc,
548                       false);
549    YamlIO.mapOptional("localFrameSize", MFI.LocalFrameSize, (unsigned)0);
550    YamlIO.mapOptional("savePoint", MFI.SavePoint,
551                       StringValue()); // Don't print it out when it's empty.
552    YamlIO.mapOptional("restorePoint", MFI.RestorePoint,
553                       StringValue()); // Don't print it out when it's empty.
554  }
555};
556
557/// Targets should override this in a way that mirrors the implementation of
558/// llvm::MachineFunctionInfo.
559struct MachineFunctionInfo {
560  virtual ~MachineFunctionInfo() {}
561  virtual void mappingImpl(IO &YamlIO) {}
562};
563
564template <> struct MappingTraits<std::unique_ptr<MachineFunctionInfo>> {
565  static void mapping(IO &YamlIO, std::unique_ptr<MachineFunctionInfo> &MFI) {
566    if (MFI)
567      MFI->mappingImpl(YamlIO);
568  }
569};
570
571struct MachineFunction {
572  StringRef Name;
573  unsigned Alignment = 0;
574  bool ExposesReturnsTwice = false;
575  // GISel MachineFunctionProperties.
576  bool Legalized = false;
577  bool RegBankSelected = false;
578  bool Selected = false;
579  bool FailedISel = false;
580  // Register information
581  bool TracksRegLiveness = false;
582  bool HasWinCFI = false;
583  std::vector<VirtualRegisterDefinition> VirtualRegisters;
584  std::vector<MachineFunctionLiveIn> LiveIns;
585  Optional<std::vector<FlowStringValue>> CalleeSavedRegisters;
586  // TODO: Serialize the various register masks.
587  // Frame information
588  MachineFrameInfo FrameInfo;
589  std::vector<FixedMachineStackObject> FixedStackObjects;
590  std::vector<MachineStackObject> StackObjects;
591  std::vector<MachineConstantPoolValue> Constants; /// Constant pool.
592  std::unique_ptr<MachineFunctionInfo> MachineFuncInfo;
593  std::vector<CallSiteInfo> CallSitesInfo;
594  MachineJumpTable JumpTableInfo;
595  BlockStringValue Body;
596};
597
598template <> struct MappingTraits<MachineFunction> {
599  static void mapping(IO &YamlIO, MachineFunction &MF) {
600    YamlIO.mapRequired("name", MF.Name);
601    YamlIO.mapOptional("alignment", MF.Alignment, (unsigned)0);
602    YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice, false);
603    YamlIO.mapOptional("legalized", MF.Legalized, false);
604    YamlIO.mapOptional("regBankSelected", MF.RegBankSelected, false);
605    YamlIO.mapOptional("selected", MF.Selected, false);
606    YamlIO.mapOptional("failedISel", MF.FailedISel, false);
607    YamlIO.mapOptional("tracksRegLiveness", MF.TracksRegLiveness, false);
608    YamlIO.mapOptional("hasWinCFI", MF.HasWinCFI, false);
609    YamlIO.mapOptional("registers", MF.VirtualRegisters,
610                       std::vector<VirtualRegisterDefinition>());
611    YamlIO.mapOptional("liveins", MF.LiveIns,
612                       std::vector<MachineFunctionLiveIn>());
613    YamlIO.mapOptional("calleeSavedRegisters", MF.CalleeSavedRegisters,
614                       Optional<std::vector<FlowStringValue>>());
615    YamlIO.mapOptional("frameInfo", MF.FrameInfo, MachineFrameInfo());
616    YamlIO.mapOptional("fixedStack", MF.FixedStackObjects,
617                       std::vector<FixedMachineStackObject>());
618    YamlIO.mapOptional("stack", MF.StackObjects,
619                       std::vector<MachineStackObject>());
620    YamlIO.mapOptional("callSites", MF.CallSitesInfo,
621                       std::vector<CallSiteInfo>());
622    YamlIO.mapOptional("constants", MF.Constants,
623                       std::vector<MachineConstantPoolValue>());
624    YamlIO.mapOptional("machineFunctionInfo", MF.MachineFuncInfo);
625    if (!YamlIO.outputting() || !MF.JumpTableInfo.Entries.empty())
626      YamlIO.mapOptional("jumpTable", MF.JumpTableInfo, MachineJumpTable());
627    YamlIO.mapOptional("body", MF.Body, BlockStringValue());
628  }
629};
630
631} // end namespace yaml
632} // end namespace llvm
633
634#endif // LLVM_CODEGEN_MIRYAMLMAPPING_H
635