1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef RCC_CODEGEN_MACHINEBASICBLOCKATTRIBUTES_H
#define RCC_CODEGEN_MACHINEBASICBLOCKATTRIBUTES_H
#include "rcc/CodeGen/MachineIRTypes.h"
#include "rcc/Support/Error.h"
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
namespace rcc {
enum class MachineBasicBlockAttribute : std::uint8_t {
MachineBlockAddressTaken,
IRBlockAddressTaken,
LandingPad,
InlineAsmBrIndirectTarget,
EHFuncletEntry,
EHScopeEntry,
Count,
};
[[nodiscard]] std::string_view
getMachineBasicBlockAttributeName(MachineBasicBlockAttribute Attribute) noexcept;
enum class MachineBasicBlockSectionKind : std::uint8_t {
Numbered,
Exception,
Cold,
};
class MachineBasicBlockSectionID final {
public:
constexpr MachineBasicBlockSectionID() noexcept = default;
[[nodiscard]] static constexpr MachineBasicBlockSectionID
numbered(std::uint32_t Number) noexcept {
return MachineBasicBlockSectionID(MachineBasicBlockSectionKind::Numbered, Number);
}
[[nodiscard]] static constexpr MachineBasicBlockSectionID exception() noexcept {
return MachineBasicBlockSectionID(MachineBasicBlockSectionKind::Exception, 0);
}
[[nodiscard]] static constexpr MachineBasicBlockSectionID cold() noexcept {
return MachineBasicBlockSectionID(MachineBasicBlockSectionKind::Cold, 0);
}
[[nodiscard]] constexpr MachineBasicBlockSectionKind kind() const noexcept {
return Kind;
}
[[nodiscard]] constexpr std::uint32_t number() const noexcept {
return Number;
}
[[nodiscard]] constexpr bool isDefault() const noexcept {
return Kind == MachineBasicBlockSectionKind::Numbered && Number == 0;
}
friend constexpr bool operator==(MachineBasicBlockSectionID Left,
MachineBasicBlockSectionID Right) noexcept = default;
private:
constexpr MachineBasicBlockSectionID(MachineBasicBlockSectionKind Kind,
std::uint32_t Number) noexcept
: Kind(Kind), Number(Number) {}
MachineBasicBlockSectionKind Kind = MachineBasicBlockSectionKind::Numbered;
std::uint32_t Number = 0;
};
struct MachineBasicBlockProfileID final {
std::uint32_t BaseID = 0;
std::uint32_t CloneID = 0;
friend constexpr bool operator==(MachineBasicBlockProfileID Left,
MachineBasicBlockProfileID Right) noexcept = default;
};
class MachineBasicBlockAttributes final {
public:
MachineBasicBlockAttributes() = default;
[[nodiscard]] static Expected<MachineBasicBlockAttributes> fromRawFlags(std::uint8_t RawFlags);
[[nodiscard]] bool has(MachineBasicBlockAttribute Attribute) const noexcept {
if (!isValidAttribute(Attribute))
return false;
return (Flags & flagFor(Attribute)) != 0;
}
void set(MachineBasicBlockAttribute Attribute) noexcept {
if (isValidAttribute(Attribute))
Flags |= flagFor(Attribute);
}
void clear(MachineBasicBlockAttribute Attribute) noexcept {
if (isValidAttribute(Attribute))
Flags &= static_cast<std::uint8_t>(~flagFor(Attribute));
}
[[nodiscard]] std::uint8_t rawFlags() const noexcept {
return Flags;
}
[[nodiscard]] std::optional<std::string_view> name() const noexcept {
if (!Name)
return std::nullopt;
return *Name;
}
[[nodiscard]] Expected<void> setName(std::string NewName);
void clearName() noexcept {
Name.reset();
}
[[nodiscard]] std::optional<std::string_view> irBlockName() const noexcept {
if (!IRBlockName)
return std::nullopt;
return *IRBlockName;
}
[[nodiscard]] Expected<void> setIRBlockName(std::string NewName);
void clearIRBlockName() noexcept {
IRBlockName.reset();
}
[[nodiscard]] Alignment alignment() const noexcept {
return BlockAlignment;
}
void setAlignment(Alignment NewAlignment) noexcept {
BlockAlignment = NewAlignment;
}
void clearAlignment() noexcept {
BlockAlignment = Alignment{};
}
[[nodiscard]] MachineBasicBlockSectionID sectionID() const noexcept {
return SectionID;
}
void setSectionID(MachineBasicBlockSectionID NewSectionID) noexcept {
SectionID = NewSectionID;
}
[[nodiscard]] const std::optional<MachineBasicBlockProfileID> &profileID() const noexcept {
return ProfileID;
}
void setProfileID(MachineBasicBlockProfileID NewProfileID) noexcept {
ProfileID = NewProfileID;
}
void clearProfileID() noexcept {
ProfileID.reset();
}
[[nodiscard]] std::uint32_t callFrameSize() const noexcept {
return CallFrameSize;
}
void setCallFrameSize(std::uint32_t Size) noexcept {
CallFrameSize = Size;
}
[[nodiscard]] Expected<void> verify() const;
friend bool operator==(const MachineBasicBlockAttributes &Left,
const MachineBasicBlockAttributes &Right) = default;
private:
static constexpr std::uint8_t AttributeCount =
static_cast<std::uint8_t>(MachineBasicBlockAttribute::Count);
static_assert(AttributeCount < 8);
static constexpr std::uint8_t ValidFlags =
static_cast<std::uint8_t>((std::uint8_t{1} << AttributeCount) - 1);
[[nodiscard]] static constexpr bool
isValidAttribute(MachineBasicBlockAttribute Attribute) noexcept {
return static_cast<std::uint8_t>(Attribute) < AttributeCount;
}
[[nodiscard]] static constexpr std::uint8_t
flagFor(MachineBasicBlockAttribute Attribute) noexcept {
return static_cast<std::uint8_t>(std::uint8_t{1} << static_cast<std::uint8_t>(Attribute));
}
std::uint8_t Flags = 0;
std::optional<std::string> Name;
std::optional<std::string> IRBlockName;
Alignment BlockAlignment;
MachineBasicBlockSectionID SectionID;
std::optional<MachineBasicBlockProfileID> ProfileID;
std::uint32_t CallFrameSize = 0;
};
} // namespace rcc
#endif // RCC_CODEGEN_MACHINEBASICBLOCKATTRIBUTES_H
|