aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/MachineIRBuilder.h
blob: 20c4ad032c2431059c2ee4de25f148ded1b8a444 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_MACHINEIRBUILDER_H
#define RCC_CODEGEN_MACHINEIRBUILDER_H

#include "rcc/CodeGen/MachineIRTypes.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/Register.h"
#include "rcc/CodeGen/TargetDescription.h"
#include "rcc/Support/Error.h"

#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <utility>
#include <vector>

namespace rcc {

class MachineBasicBlock;
class MachineFunction;
class MachineInstr;
class MachineMemOperand;
class MachineRegisterInfo;
class RegisterMask;
class GISelCSEInfo;

class MachineInstrBuilder final {
public:
  MachineInstrBuilder(const MachineInstrBuilder &) = delete;
  MachineInstrBuilder &operator=(const MachineInstrBuilder &) = delete;
  MachineInstrBuilder(MachineInstrBuilder &&Other) noexcept;
  MachineInstrBuilder &operator=(MachineInstrBuilder &&Other) noexcept;
  ~MachineInstrBuilder() = default;

  [[nodiscard]] Expected<void> addOperand(MachineOperand &&Operand);
  [[nodiscard]] Expected<void> addImplicitOperand(MachineOperand &&Operand);

  [[nodiscard]] Expected<void> addDef(Register Reg, RegisterOperandFlags AdditionalFlags = {},
                                      SubRegIndex SubReg = {}, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addUse(Register Reg, RegisterOperandFlags AdditionalFlags = {},
                                      SubRegIndex SubReg = {}, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addDefUse(Register Reg, RegisterOperandFlags AdditionalFlags = {},
                                         SubRegIndex SubReg = {}, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addImplicitDef(Register Reg,
                                              RegisterOperandFlags AdditionalFlags = {},
                                              SubRegIndex SubReg = {},
                                              std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addImplicitUse(Register Reg,
                                              RegisterOperandFlags AdditionalFlags = {},
                                              SubRegIndex SubReg = {},
                                              std::uint32_t TargetFlags = 0);

  [[nodiscard]] Expected<void> addImmediate(std::int64_t Value, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addIntegerImmediate(APInt Value, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addFloatingImmediate(APFloat Bits, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addBasicBlock(MachineBasicBlock &Block,
                                             std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addFrameIndex(FrameIndex Index, std::int64_t Offset = 0,
                                             std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addConstantPool(ConstantPoolIndex Index, std::int64_t Offset = 0,
                                               std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addJumpTable(JumpTableIndex Index, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addGlobalSymbol(std::string Name, std::int64_t Offset = 0,
                                               std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addExternalSymbol(std::string Name, std::int64_t Offset = 0,
                                                 std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addBlockAddress(std::string FunctionName, std::string BlockName,
                                               std::int64_t Offset = 0,
                                               std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addRegisterMask(std::shared_ptr<const RegisterMask> Mask,
                                               std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addLiveOutRegisterMask(std::shared_ptr<const RegisterMask> Mask,
                                                      std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addMetadata(MachineMetadataValue Value,
                                           std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addTargetIndex(TargetIndex Index, std::int64_t Offset = 0,
                                              std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addMCSymbol(std::string Name, std::int64_t Offset = 0,
                                           std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addCFIInstruction(CFIInstructionIndex Index,
                                                 std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addIntrinsic(IntrinsicID ID, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addIntegerPredicate(IntegerPredicate Predicate,
                                                   std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addFloatingPredicate(FloatingPredicate Predicate,
                                                    std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addShuffleMask(std::vector<std::int32_t> Elements,
                                              std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addDebugInstrRef(std::uint64_t InstructionNumber,
                                                std::uint64_t OperandNumber,
                                                std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addLaneMask(LaneMask Mask, std::uint32_t TargetFlags = 0);

  [[nodiscard]] Expected<void> tieOperands(std::size_t DefOperand, std::size_t UseOperand);
  [[nodiscard]] Expected<void> addMemOperand(const MachineMemOperand &MemOperand);

  [[nodiscard]] Expected<void> setFlags(MachineInstrFlags NewFlags);

  [[nodiscard]] Expected<void> setSourceLocation(SourceLocation NewLocation);
  [[nodiscard]] Expected<void> clearSourceLocation();
  [[nodiscard]] Expected<void> setDebugLocation(MachineDebugLocation NewLocation);
  [[nodiscard]] Expected<void> clearDebugLocation();

  [[nodiscard]] Expected<void> setOrigin(OriginID NewOrigin);
  [[nodiscard]] Expected<void> clearOrigin();

  [[nodiscard]] Expected<MachineInstr *> build();

private:
  MachineInstrBuilder(MachineFunction &Function, const TargetDescription &Target,
                      const InstructionDescriptor &Descriptor, MachineBasicBlock &InsertBlock,
                      MachineInstr *InsertBefore) noexcept;

  [[nodiscard]] Expected<void> ensurePending() const;
  [[nodiscard]] Expected<void> validateInsertionPoint() const;
  [[nodiscard]] Expected<void> validateFrameReference(FrameIndex Index) const;
  [[nodiscard]] Expected<void> validateConstantPoolReference(ConstantPoolIndex Index) const;
  [[nodiscard]] Expected<void> validateJumpTableReference(JumpTableIndex Index) const;
  [[nodiscard]] Expected<void> validateCFIReference(CFIInstructionIndex Index) const;
  [[nodiscard]] Expected<void> validateOperandOwnership(const MachineOperand &Operand) const;
  [[nodiscard]] std::vector<MachineOperandTie> collectOperandTies() const;
  [[nodiscard]] Expected<void> appendRegister(Register Reg, RegisterOperandFlags Flags,
                                              SubRegIndex SubReg, std::uint32_t TargetFlags,
                                              bool IsImplicit);

  MachineFunction *Function;
  const TargetDescription *Target;
  const InstructionDescriptor *Descriptor;
  MachineBasicBlock *InsertBlock;
  MachineInstr *InsertBefore;
  std::vector<MachineOperand> ExplicitOperands;
  std::vector<MachineOperand> ExtraImplicitOperands;
  std::vector<MachineOperandTie> RequestedTies;
  std::vector<const MachineMemOperand *> MemOperands;
  MachineInstrFlags Flags;
  std::optional<SourceLocation> Location;
  std::optional<MachineDebugLocation> DebugLocation;
  std::optional<OriginID> Origin;
  bool IsBuilt = false;

  friend class MachineIRBuilder;
};

class MachineIRBuilderResult final {
public:
  enum class Kind : std::uint8_t {
    Register,
    Type,
  };

  MachineIRBuilderResult(Register Reg) noexcept : Reg(Reg), ResultKind(Kind::Register) {}
  MachineIRBuilderResult(LowLevelType Type) noexcept : Type(Type), ResultKind(Kind::Type) {}

  [[nodiscard]] Kind kind() const noexcept {
    return ResultKind;
  }

  [[nodiscard]] Register reg() const noexcept;
  [[nodiscard]] LowLevelType type() const noexcept;
  [[nodiscard]] Expected<LowLevelType> type(const MachineRegisterInfo &MRI) const;
  [[nodiscard]] Expected<void> addDef(MachineRegisterInfo &MRI, MachineInstrBuilder &Builder) const;

private:
  Register Reg;
  LowLevelType Type;
  Kind ResultKind;
};

class MachineIRBuilder {
public:
  MachineIRBuilder(MachineFunction &Function, const TargetDescription &Target) noexcept
      : Function(&Function), Target(&Target) {}
  MachineIRBuilder(const MachineIRBuilder &) = delete;
  MachineIRBuilder &operator=(const MachineIRBuilder &) = delete;
  MachineIRBuilder(MachineIRBuilder &&) = delete;
  MachineIRBuilder &operator=(MachineIRBuilder &&) = delete;
  virtual ~MachineIRBuilder() = default;

  [[nodiscard]] MachineFunction &function() noexcept {
    return *Function;
  }

  [[nodiscard]] MachineFunction &function() const noexcept {
    return *Function;
  }

  [[nodiscard]] const TargetDescription &target() const noexcept {
    return *Target;
  }

  [[nodiscard]] MachineBasicBlock *insertionBlock() const noexcept {
    return InsertBlock;
  }

  [[nodiscard]] MachineInstr *insertionInstruction() const noexcept {
    return InsertBefore;
  }

  [[nodiscard]] GISelCSEInfo *getCSEInfo() noexcept {
    return CSEInfo;
  }

  [[nodiscard]] GISelCSEInfo *getCSEInfo() const noexcept {
    return CSEInfo;
  }

  void setCSEInfo(GISelCSEInfo *Info) noexcept {
    CSEInfo = Info;
  }

  [[nodiscard]] Expected<void> verifyInsertPoint() const;

  [[nodiscard]] Expected<void> setInsertPoint(MachineBasicBlock &Block);
  [[nodiscard]] Expected<void> setInsertPointBefore(MachineInstr &Instruction);
  [[nodiscard]] Expected<void> setInsertPointAfter(MachineInstr &Instruction);
  void clearInsertPoint() noexcept {
    InsertBlock = nullptr;
    InsertBefore = nullptr;
  }

  [[nodiscard]] Expected<MachineInstrBuilder> buildInstr(Opcode InstructionOpcode) const;
  [[nodiscard]] Expected<MachineInstr *>
  buildInstr(Opcode InstructionOpcode, std::initializer_list<MachineIRBuilderResult> Destinations,
             std::initializer_list<Register> Sources) const;
  [[nodiscard]] virtual Expected<MachineInstr *>
  buildInstr(Opcode InstructionOpcode, std::span<const MachineIRBuilderResult> Destinations,
             std::span<const Register> Sources) const;
  [[nodiscard]] Expected<MachineInstr *> buildTrunc(MachineIRBuilderResult Destination,
                                                    Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildCopy(MachineIRBuilderResult Destination,
                                                   Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildImplicitDef(MachineIRBuilderResult Destination) const;
  [[nodiscard]] Expected<MachineInstr *> buildAnyExt(MachineIRBuilderResult Destination,
                                                     Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildZExt(MachineIRBuilderResult Destination,
                                                   Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildSExt(MachineIRBuilderResult Destination,
                                                   Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildUndef(MachineIRBuilderResult Destination) const;
  [[nodiscard]] Expected<MachineInstr *> buildAnyExtOrTrunc(MachineIRBuilderResult Destination,
                                                            Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildZExtOrTrunc(MachineIRBuilderResult Destination,
                                                          Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildSExtOrTrunc(MachineIRBuilderResult Destination,
                                                          Register Source) const;
  [[nodiscard]] virtual Expected<MachineInstr *> buildConstant(MachineIRBuilderResult Destination,
                                                               APInt Value) const;
  [[nodiscard]] Expected<MachineInstr *> buildConstant(MachineIRBuilderResult Destination,
                                                       std::int64_t Value) const;
  [[nodiscard]] virtual Expected<MachineInstr *>
  buildSExtInReg(MachineIRBuilderResult Destination, Register Source, std::uint32_t Width) const;
  [[nodiscard]] Expected<MachineInstr *> buildAnd(MachineIRBuilderResult Destination, Register Left,
                                                  Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildOr(MachineIRBuilderResult Destination, Register Left,
                                                 Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildXor(MachineIRBuilderResult Destination, Register Left,
                                                  Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildAdd(MachineIRBuilderResult Destination, Register Left,
                                                  Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildSub(MachineIRBuilderResult Destination, Register Left,
                                                  Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildShl(MachineIRBuilderResult Destination, Register Left,
                                                  Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildLShr(MachineIRBuilderResult Destination,
                                                   Register Left, Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildAShr(MachineIRBuilderResult Destination,
                                                   Register Left, Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildPtrToInt(MachineIRBuilderResult Destination,
                                                       Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildIntToPtr(MachineIRBuilderResult Destination,
                                                       Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildICmp(IntegerPredicate Predicate,
                                                   MachineIRBuilderResult Destination,
                                                   Register Left, Register Right) const;
  [[nodiscard]] Expected<MachineInstr *>
  buildMergeLikeInstr(MachineIRBuilderResult Destination,
                      const std::vector<Register> &Sources) const;
  [[nodiscard]] Expected<MachineInstr *>
  buildMergeValues(MachineIRBuilderResult Destination, const std::vector<Register> &Sources) const;
  [[nodiscard]] Expected<MachineInstr *>
  buildUnmergeValues(const std::vector<Register> &Destinations, Register Source) const;
  [[nodiscard]] virtual Expected<MachineInstr *>
  buildExtract(MachineIRBuilderResult Destination, Register Source, std::uint64_t Offset) const;
  [[nodiscard]] Expected<MachineInstr *> buildInsert(MachineIRBuilderResult Destination,
                                                     Register Source, Register Inserted,
                                                     std::uint64_t Offset) const;

private:
  [[nodiscard]] Expected<MachineInstr *>
  buildUnary(Opcode InstructionOpcode, MachineIRBuilderResult Destination, Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildBinary(Opcode InstructionOpcode,
                                                     MachineIRBuilderResult Destination,
                                                     Register Left, Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildExtOrTrunc(Opcode ExtensionOpcode,
                                                         MachineIRBuilderResult Destination,
                                                         Register Source) const;
  [[nodiscard]] Expected<LowLevelType> registerType(Register Reg) const;
  [[nodiscard]] Expected<void> validateBlock(const MachineBasicBlock &Block) const;
  [[nodiscard]] Expected<void> validateInstruction(const MachineInstr &Instruction) const;

  MachineFunction *Function;
  const TargetDescription *Target;
  MachineBasicBlock *InsertBlock = nullptr;
  MachineInstr *InsertBefore = nullptr;
  GISelCSEInfo *CSEInfo = nullptr;
};

} // namespace rcc

#endif // RCC_CODEGEN_MACHINEIRBUILDER_H