[DAGCombiner] Eliminate dead stores to stack.
[llvm-complete.git] / include / llvm / CodeGen / MIRYamlMapping.h
blobb600ec92e18d6a4ee866cfb8a70bbaff7f501911
1 //===- MIRYamlMapping.h - Describes the mapping between MIR and YAML ------===//
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.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MIRYAMLMAPPING_H
15 #define LLVM_CODEGEN_MIRYAMLMAPPING_H
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/CodeGen/MachineJumpTableInfo.h"
20 #include "llvm/Support/SMLoc.h"
21 #include "llvm/Support/YAMLTraits.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <algorithm>
24 #include <cstdint>
25 #include <string>
26 #include <vector>
28 namespace llvm {
29 namespace yaml {
31 /// A wrapper around std::string which contains a source range that's being
32 /// set during parsing.
33 struct StringValue {
34 std::string Value;
35 SMRange SourceRange;
37 StringValue() = default;
38 StringValue(std::string Value) : Value(std::move(Value)) {}
40 bool operator==(const StringValue &Other) const {
41 return Value == Other.Value;
45 template <> struct ScalarTraits<StringValue> {
46 static void output(const StringValue &S, void *, raw_ostream &OS) {
47 OS << S.Value;
50 static StringRef input(StringRef Scalar, void *Ctx, StringValue &S) {
51 S.Value = Scalar.str();
52 if (const auto *Node =
53 reinterpret_cast<yaml::Input *>(Ctx)->getCurrentNode())
54 S.SourceRange = Node->getSourceRange();
55 return "";
58 static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
61 struct FlowStringValue : StringValue {
62 FlowStringValue() = default;
63 FlowStringValue(std::string Value) : StringValue(std::move(Value)) {}
66 template <> struct ScalarTraits<FlowStringValue> {
67 static void output(const FlowStringValue &S, void *, raw_ostream &OS) {
68 return ScalarTraits<StringValue>::output(S, nullptr, OS);
71 static StringRef input(StringRef Scalar, void *Ctx, FlowStringValue &S) {
72 return ScalarTraits<StringValue>::input(Scalar, Ctx, S);
75 static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
78 struct BlockStringValue {
79 StringValue Value;
81 bool operator==(const BlockStringValue &Other) const {
82 return Value == Other.Value;
86 template <> struct BlockScalarTraits<BlockStringValue> {
87 static void output(const BlockStringValue &S, void *Ctx, raw_ostream &OS) {
88 return ScalarTraits<StringValue>::output(S.Value, Ctx, OS);
91 static StringRef input(StringRef Scalar, void *Ctx, BlockStringValue &S) {
92 return ScalarTraits<StringValue>::input(Scalar, Ctx, S.Value);
96 /// A wrapper around unsigned which contains a source range that's being set
97 /// during parsing.
98 struct UnsignedValue {
99 unsigned Value = 0;
100 SMRange SourceRange;
102 UnsignedValue() = default;
103 UnsignedValue(unsigned Value) : Value(Value) {}
105 bool operator==(const UnsignedValue &Other) const {
106 return Value == Other.Value;
110 template <> struct ScalarTraits<UnsignedValue> {
111 static void output(const UnsignedValue &Value, void *Ctx, raw_ostream &OS) {
112 return ScalarTraits<unsigned>::output(Value.Value, Ctx, OS);
115 static StringRef input(StringRef Scalar, void *Ctx, UnsignedValue &Value) {
116 if (const auto *Node =
117 reinterpret_cast<yaml::Input *>(Ctx)->getCurrentNode())
118 Value.SourceRange = Node->getSourceRange();
119 return ScalarTraits<unsigned>::input(Scalar, Ctx, Value.Value);
122 static QuotingType mustQuote(StringRef Scalar) {
123 return ScalarTraits<unsigned>::mustQuote(Scalar);
127 template <> struct ScalarEnumerationTraits<MachineJumpTableInfo::JTEntryKind> {
128 static void enumeration(yaml::IO &IO,
129 MachineJumpTableInfo::JTEntryKind &EntryKind) {
130 IO.enumCase(EntryKind, "block-address",
131 MachineJumpTableInfo::EK_BlockAddress);
132 IO.enumCase(EntryKind, "gp-rel64-block-address",
133 MachineJumpTableInfo::EK_GPRel64BlockAddress);
134 IO.enumCase(EntryKind, "gp-rel32-block-address",
135 MachineJumpTableInfo::EK_GPRel32BlockAddress);
136 IO.enumCase(EntryKind, "label-difference32",
137 MachineJumpTableInfo::EK_LabelDifference32);
138 IO.enumCase(EntryKind, "inline", MachineJumpTableInfo::EK_Inline);
139 IO.enumCase(EntryKind, "custom32", MachineJumpTableInfo::EK_Custom32);
143 } // end namespace yaml
144 } // end namespace llvm
146 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::StringValue)
147 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::FlowStringValue)
148 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::UnsignedValue)
150 namespace llvm {
151 namespace yaml {
153 struct VirtualRegisterDefinition {
154 UnsignedValue ID;
155 StringValue Class;
156 StringValue PreferredRegister;
158 // TODO: Serialize the target specific register hints.
160 bool operator==(const VirtualRegisterDefinition &Other) const {
161 return ID == Other.ID && Class == Other.Class &&
162 PreferredRegister == Other.PreferredRegister;
166 template <> struct MappingTraits<VirtualRegisterDefinition> {
167 static void mapping(IO &YamlIO, VirtualRegisterDefinition &Reg) {
168 YamlIO.mapRequired("id", Reg.ID);
169 YamlIO.mapRequired("class", Reg.Class);
170 YamlIO.mapOptional("preferred-register", Reg.PreferredRegister,
171 StringValue()); // Don't print out when it's empty.
174 static const bool flow = true;
177 struct MachineFunctionLiveIn {
178 StringValue Register;
179 StringValue VirtualRegister;
181 bool operator==(const MachineFunctionLiveIn &Other) const {
182 return Register == Other.Register &&
183 VirtualRegister == Other.VirtualRegister;
187 template <> struct MappingTraits<MachineFunctionLiveIn> {
188 static void mapping(IO &YamlIO, MachineFunctionLiveIn &LiveIn) {
189 YamlIO.mapRequired("reg", LiveIn.Register);
190 YamlIO.mapOptional(
191 "virtual-reg", LiveIn.VirtualRegister,
192 StringValue()); // Don't print the virtual register when it's empty.
195 static const bool flow = true;
198 /// Serializable representation of stack object from the MachineFrameInfo class.
200 /// The flags 'isImmutable' and 'isAliased' aren't serialized, as they are
201 /// determined by the object's type and frame information flags.
202 /// Dead stack objects aren't serialized.
204 /// The 'isPreallocated' flag is determined by the local offset.
205 struct MachineStackObject {
206 enum ObjectType { DefaultType, SpillSlot, VariableSized };
207 UnsignedValue ID;
208 StringValue Name;
209 // TODO: Serialize unnamed LLVM alloca reference.
210 ObjectType Type = DefaultType;
211 int64_t Offset = 0;
212 uint64_t Size = 0;
213 unsigned Alignment = 0;
214 uint8_t StackID = 0;
215 StringValue CalleeSavedRegister;
216 bool CalleeSavedRestored = true;
217 Optional<int64_t> LocalOffset;
218 StringValue DebugVar;
219 StringValue DebugExpr;
220 StringValue DebugLoc;
222 bool operator==(const MachineStackObject &Other) const {
223 return ID == Other.ID && Name == Other.Name && Type == Other.Type &&
224 Offset == Other.Offset && Size == Other.Size &&
225 Alignment == Other.Alignment &&
226 StackID == Other.StackID &&
227 CalleeSavedRegister == Other.CalleeSavedRegister &&
228 CalleeSavedRestored == Other.CalleeSavedRestored &&
229 LocalOffset == Other.LocalOffset && DebugVar == Other.DebugVar &&
230 DebugExpr == Other.DebugExpr && DebugLoc == Other.DebugLoc;
234 template <> struct ScalarEnumerationTraits<MachineStackObject::ObjectType> {
235 static void enumeration(yaml::IO &IO, MachineStackObject::ObjectType &Type) {
236 IO.enumCase(Type, "default", MachineStackObject::DefaultType);
237 IO.enumCase(Type, "spill-slot", MachineStackObject::SpillSlot);
238 IO.enumCase(Type, "variable-sized", MachineStackObject::VariableSized);
242 template <> struct MappingTraits<MachineStackObject> {
243 static void mapping(yaml::IO &YamlIO, MachineStackObject &Object) {
244 YamlIO.mapRequired("id", Object.ID);
245 YamlIO.mapOptional("name", Object.Name,
246 StringValue()); // Don't print out an empty name.
247 YamlIO.mapOptional(
248 "type", Object.Type,
249 MachineStackObject::DefaultType); // Don't print the default type.
250 YamlIO.mapOptional("offset", Object.Offset, (int64_t)0);
251 if (Object.Type != MachineStackObject::VariableSized)
252 YamlIO.mapRequired("size", Object.Size);
253 YamlIO.mapOptional("alignment", Object.Alignment, (unsigned)0);
254 YamlIO.mapOptional("stack-id", Object.StackID);
255 YamlIO.mapOptional("callee-saved-register", Object.CalleeSavedRegister,
256 StringValue()); // Don't print it out when it's empty.
257 YamlIO.mapOptional("callee-saved-restored", Object.CalleeSavedRestored,
258 true);
259 YamlIO.mapOptional("local-offset", Object.LocalOffset, Optional<int64_t>());
260 YamlIO.mapOptional("debug-info-variable", Object.DebugVar,
261 StringValue()); // Don't print it out when it's empty.
262 YamlIO.mapOptional("debug-info-expression", Object.DebugExpr,
263 StringValue()); // Don't print it out when it's empty.
264 YamlIO.mapOptional("debug-info-location", Object.DebugLoc,
265 StringValue()); // Don't print it out when it's empty.
268 static const bool flow = true;
271 /// Serializable representation of the fixed stack object from the
272 /// MachineFrameInfo class.
273 struct FixedMachineStackObject {
274 enum ObjectType { DefaultType, SpillSlot };
275 UnsignedValue ID;
276 ObjectType Type = DefaultType;
277 int64_t Offset = 0;
278 uint64_t Size = 0;
279 unsigned Alignment = 0;
280 uint8_t StackID = 0;
281 bool IsImmutable = false;
282 bool IsAliased = false;
283 StringValue CalleeSavedRegister;
284 bool CalleeSavedRestored = true;
285 StringValue DebugVar;
286 StringValue DebugExpr;
287 StringValue DebugLoc;
289 bool operator==(const FixedMachineStackObject &Other) const {
290 return ID == Other.ID && Type == Other.Type && Offset == Other.Offset &&
291 Size == Other.Size && Alignment == Other.Alignment &&
292 StackID == Other.StackID &&
293 IsImmutable == Other.IsImmutable && IsAliased == Other.IsAliased &&
294 CalleeSavedRegister == Other.CalleeSavedRegister &&
295 CalleeSavedRestored == Other.CalleeSavedRestored &&
296 DebugVar == Other.DebugVar && DebugExpr == Other.DebugExpr
297 && DebugLoc == Other.DebugLoc;
301 template <>
302 struct ScalarEnumerationTraits<FixedMachineStackObject::ObjectType> {
303 static void enumeration(yaml::IO &IO,
304 FixedMachineStackObject::ObjectType &Type) {
305 IO.enumCase(Type, "default", FixedMachineStackObject::DefaultType);
306 IO.enumCase(Type, "spill-slot", FixedMachineStackObject::SpillSlot);
310 template <> struct MappingTraits<FixedMachineStackObject> {
311 static void mapping(yaml::IO &YamlIO, FixedMachineStackObject &Object) {
312 YamlIO.mapRequired("id", Object.ID);
313 YamlIO.mapOptional(
314 "type", Object.Type,
315 FixedMachineStackObject::DefaultType); // Don't print the default type.
316 YamlIO.mapOptional("offset", Object.Offset, (int64_t)0);
317 YamlIO.mapOptional("size", Object.Size, (uint64_t)0);
318 YamlIO.mapOptional("alignment", Object.Alignment, (unsigned)0);
319 YamlIO.mapOptional("stack-id", Object.StackID);
320 if (Object.Type != FixedMachineStackObject::SpillSlot) {
321 YamlIO.mapOptional("isImmutable", Object.IsImmutable, false);
322 YamlIO.mapOptional("isAliased", Object.IsAliased, false);
324 YamlIO.mapOptional("callee-saved-register", Object.CalleeSavedRegister,
325 StringValue()); // Don't print it out when it's empty.
326 YamlIO.mapOptional("callee-saved-restored", Object.CalleeSavedRestored,
327 true);
328 YamlIO.mapOptional("debug-info-variable", Object.DebugVar,
329 StringValue()); // Don't print it out when it's empty.
330 YamlIO.mapOptional("debug-info-expression", Object.DebugExpr,
331 StringValue()); // Don't print it out when it's empty.
332 YamlIO.mapOptional("debug-info-location", Object.DebugLoc,
333 StringValue()); // Don't print it out when it's empty.
336 static const bool flow = true;
339 struct MachineConstantPoolValue {
340 UnsignedValue ID;
341 StringValue Value;
342 unsigned Alignment = 0;
343 bool IsTargetSpecific = false;
345 bool operator==(const MachineConstantPoolValue &Other) const {
346 return ID == Other.ID && Value == Other.Value &&
347 Alignment == Other.Alignment &&
348 IsTargetSpecific == Other.IsTargetSpecific;
352 template <> struct MappingTraits<MachineConstantPoolValue> {
353 static void mapping(IO &YamlIO, MachineConstantPoolValue &Constant) {
354 YamlIO.mapRequired("id", Constant.ID);
355 YamlIO.mapOptional("value", Constant.Value, StringValue());
356 YamlIO.mapOptional("alignment", Constant.Alignment, (unsigned)0);
357 YamlIO.mapOptional("isTargetSpecific", Constant.IsTargetSpecific, false);
361 struct MachineJumpTable {
362 struct Entry {
363 UnsignedValue ID;
364 std::vector<FlowStringValue> Blocks;
366 bool operator==(const Entry &Other) const {
367 return ID == Other.ID && Blocks == Other.Blocks;
371 MachineJumpTableInfo::JTEntryKind Kind = MachineJumpTableInfo::EK_Custom32;
372 std::vector<Entry> Entries;
374 bool operator==(const MachineJumpTable &Other) const {
375 return Kind == Other.Kind && Entries == Other.Entries;
379 template <> struct MappingTraits<MachineJumpTable::Entry> {
380 static void mapping(IO &YamlIO, MachineJumpTable::Entry &Entry) {
381 YamlIO.mapRequired("id", Entry.ID);
382 YamlIO.mapOptional("blocks", Entry.Blocks, std::vector<FlowStringValue>());
386 } // end namespace yaml
387 } // end namespace llvm
389 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineFunctionLiveIn)
390 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::VirtualRegisterDefinition)
391 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineStackObject)
392 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::FixedMachineStackObject)
393 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineConstantPoolValue)
394 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineJumpTable::Entry)
396 namespace llvm {
397 namespace yaml {
399 template <> struct MappingTraits<MachineJumpTable> {
400 static void mapping(IO &YamlIO, MachineJumpTable &JT) {
401 YamlIO.mapRequired("kind", JT.Kind);
402 YamlIO.mapOptional("entries", JT.Entries,
403 std::vector<MachineJumpTable::Entry>());
407 /// Serializable representation of MachineFrameInfo.
409 /// Doesn't serialize attributes like 'StackAlignment', 'IsStackRealignable' and
410 /// 'RealignOption' as they are determined by the target and LLVM function
411 /// attributes.
412 /// It also doesn't serialize attributes like 'NumFixedObject' and
413 /// 'HasVarSizedObjects' as they are determined by the frame objects themselves.
414 struct MachineFrameInfo {
415 bool IsFrameAddressTaken = false;
416 bool IsReturnAddressTaken = false;
417 bool HasStackMap = false;
418 bool HasPatchPoint = false;
419 uint64_t StackSize = 0;
420 int OffsetAdjustment = 0;
421 unsigned MaxAlignment = 0;
422 bool AdjustsStack = false;
423 bool HasCalls = false;
424 StringValue StackProtector;
425 // TODO: Serialize FunctionContextIdx
426 unsigned MaxCallFrameSize = ~0u; ///< ~0u means: not computed yet.
427 unsigned CVBytesOfCalleeSavedRegisters = 0;
428 bool HasOpaqueSPAdjustment = false;
429 bool HasVAStart = false;
430 bool HasMustTailInVarArgFunc = false;
431 unsigned LocalFrameSize = 0;
432 StringValue SavePoint;
433 StringValue RestorePoint;
435 bool operator==(const MachineFrameInfo &Other) const {
436 return IsFrameAddressTaken == Other.IsFrameAddressTaken &&
437 IsReturnAddressTaken == Other.IsReturnAddressTaken &&
438 HasStackMap == Other.HasStackMap &&
439 HasPatchPoint == Other.HasPatchPoint &&
440 StackSize == Other.StackSize &&
441 OffsetAdjustment == Other.OffsetAdjustment &&
442 MaxAlignment == Other.MaxAlignment &&
443 AdjustsStack == Other.AdjustsStack && HasCalls == Other.HasCalls &&
444 StackProtector == Other.StackProtector &&
445 MaxCallFrameSize == Other.MaxCallFrameSize &&
446 CVBytesOfCalleeSavedRegisters ==
447 Other.CVBytesOfCalleeSavedRegisters &&
448 HasOpaqueSPAdjustment == Other.HasOpaqueSPAdjustment &&
449 HasVAStart == Other.HasVAStart &&
450 HasMustTailInVarArgFunc == Other.HasMustTailInVarArgFunc &&
451 LocalFrameSize == Other.LocalFrameSize &&
452 SavePoint == Other.SavePoint && RestorePoint == Other.RestorePoint;
456 template <> struct MappingTraits<MachineFrameInfo> {
457 static void mapping(IO &YamlIO, MachineFrameInfo &MFI) {
458 YamlIO.mapOptional("isFrameAddressTaken", MFI.IsFrameAddressTaken, false);
459 YamlIO.mapOptional("isReturnAddressTaken", MFI.IsReturnAddressTaken, false);
460 YamlIO.mapOptional("hasStackMap", MFI.HasStackMap, false);
461 YamlIO.mapOptional("hasPatchPoint", MFI.HasPatchPoint, false);
462 YamlIO.mapOptional("stackSize", MFI.StackSize, (uint64_t)0);
463 YamlIO.mapOptional("offsetAdjustment", MFI.OffsetAdjustment, (int)0);
464 YamlIO.mapOptional("maxAlignment", MFI.MaxAlignment, (unsigned)0);
465 YamlIO.mapOptional("adjustsStack", MFI.AdjustsStack, false);
466 YamlIO.mapOptional("hasCalls", MFI.HasCalls, false);
467 YamlIO.mapOptional("stackProtector", MFI.StackProtector,
468 StringValue()); // Don't print it out when it's empty.
469 YamlIO.mapOptional("maxCallFrameSize", MFI.MaxCallFrameSize, (unsigned)~0);
470 YamlIO.mapOptional("cvBytesOfCalleeSavedRegisters",
471 MFI.CVBytesOfCalleeSavedRegisters, 0U);
472 YamlIO.mapOptional("hasOpaqueSPAdjustment", MFI.HasOpaqueSPAdjustment,
473 false);
474 YamlIO.mapOptional("hasVAStart", MFI.HasVAStart, false);
475 YamlIO.mapOptional("hasMustTailInVarArgFunc", MFI.HasMustTailInVarArgFunc,
476 false);
477 YamlIO.mapOptional("localFrameSize", MFI.LocalFrameSize, (unsigned)0);
478 YamlIO.mapOptional("savePoint", MFI.SavePoint,
479 StringValue()); // Don't print it out when it's empty.
480 YamlIO.mapOptional("restorePoint", MFI.RestorePoint,
481 StringValue()); // Don't print it out when it's empty.
485 struct MachineFunction {
486 StringRef Name;
487 unsigned Alignment = 0;
488 bool ExposesReturnsTwice = false;
489 // GISel MachineFunctionProperties.
490 bool Legalized = false;
491 bool RegBankSelected = false;
492 bool Selected = false;
493 bool FailedISel = false;
494 // Register information
495 bool TracksRegLiveness = false;
496 bool HasWinCFI = false;
497 std::vector<VirtualRegisterDefinition> VirtualRegisters;
498 std::vector<MachineFunctionLiveIn> LiveIns;
499 Optional<std::vector<FlowStringValue>> CalleeSavedRegisters;
500 // TODO: Serialize the various register masks.
501 // Frame information
502 MachineFrameInfo FrameInfo;
503 std::vector<FixedMachineStackObject> FixedStackObjects;
504 std::vector<MachineStackObject> StackObjects;
505 std::vector<MachineConstantPoolValue> Constants; /// Constant pool.
506 MachineJumpTable JumpTableInfo;
507 BlockStringValue Body;
510 template <> struct MappingTraits<MachineFunction> {
511 static void mapping(IO &YamlIO, MachineFunction &MF) {
512 YamlIO.mapRequired("name", MF.Name);
513 YamlIO.mapOptional("alignment", MF.Alignment, (unsigned)0);
514 YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice, false);
515 YamlIO.mapOptional("legalized", MF.Legalized, false);
516 YamlIO.mapOptional("regBankSelected", MF.RegBankSelected, false);
517 YamlIO.mapOptional("selected", MF.Selected, false);
518 YamlIO.mapOptional("failedISel", MF.FailedISel, false);
519 YamlIO.mapOptional("tracksRegLiveness", MF.TracksRegLiveness, false);
520 YamlIO.mapOptional("hasWinCFI", MF.HasWinCFI, false);
521 YamlIO.mapOptional("registers", MF.VirtualRegisters,
522 std::vector<VirtualRegisterDefinition>());
523 YamlIO.mapOptional("liveins", MF.LiveIns,
524 std::vector<MachineFunctionLiveIn>());
525 YamlIO.mapOptional("calleeSavedRegisters", MF.CalleeSavedRegisters,
526 Optional<std::vector<FlowStringValue>>());
527 YamlIO.mapOptional("frameInfo", MF.FrameInfo, MachineFrameInfo());
528 YamlIO.mapOptional("fixedStack", MF.FixedStackObjects,
529 std::vector<FixedMachineStackObject>());
530 YamlIO.mapOptional("stack", MF.StackObjects,
531 std::vector<MachineStackObject>());
532 YamlIO.mapOptional("constants", MF.Constants,
533 std::vector<MachineConstantPoolValue>());
534 if (!YamlIO.outputting() || !MF.JumpTableInfo.Entries.empty())
535 YamlIO.mapOptional("jumpTable", MF.JumpTableInfo, MachineJumpTable());
536 YamlIO.mapOptional("body", MF.Body, BlockStringValue());
540 } // end namespace yaml
541 } // end namespace llvm
543 #endif // LLVM_CODEGEN_MIRYAMLMAPPING_H