aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/MachineIRBuilder.cpp
blob: 74ca55b918e547ffcab62cf183bd0a985e7706d5 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/MachineIRBuilder.h"

#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFrameInfo.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineMemOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

namespace rcc {

MachineInstrBuilder::MachineInstrBuilder(MachineFunction &Function,
                                         const InstructionSet &Instructions,
                                         const InstructionDescriptor &Descriptor,
                                         MachineBasicBlock &InsertBlock,
                                         MachineInstr *InsertBefore) noexcept
    : Function(&Function), Instructions(&Instructions), Descriptor(&Descriptor),
      InsertBlock(&InsertBlock), InsertBefore(InsertBefore) {}

MachineInstrBuilder::MachineInstrBuilder(MachineInstrBuilder &&Other) noexcept
    : Function(Other.Function), Instructions(Other.Instructions), Descriptor(Other.Descriptor),
      InsertBlock(Other.InsertBlock), InsertBefore(Other.InsertBefore),
      ExplicitOperands(std::move(Other.ExplicitOperands)),
      ExtraImplicitOperands(std::move(Other.ExtraImplicitOperands)),
      RequestedTies(std::move(Other.RequestedTies)), MemOperands(std::move(Other.MemOperands)),
      Flags(Other.Flags), Location(std::move(Other.Location)), Origin(Other.Origin),
      IsBuilt(Other.IsBuilt) {
  Other.Function = nullptr;
  Other.Instructions = nullptr;
  Other.Descriptor = nullptr;
  Other.InsertBlock = nullptr;
  Other.InsertBefore = nullptr;
  Other.IsBuilt = true;
}

MachineInstrBuilder &MachineInstrBuilder::operator=(MachineInstrBuilder &&Other) noexcept {
  if (this == &Other)
    return *this;

  Function = Other.Function;
  Instructions = Other.Instructions;
  Descriptor = Other.Descriptor;
  InsertBlock = Other.InsertBlock;
  InsertBefore = Other.InsertBefore;
  ExplicitOperands = std::move(Other.ExplicitOperands);
  ExtraImplicitOperands = std::move(Other.ExtraImplicitOperands);
  RequestedTies = std::move(Other.RequestedTies);
  MemOperands = std::move(Other.MemOperands);
  Flags = Other.Flags;
  Location = std::move(Other.Location);
  Origin = Other.Origin;
  IsBuilt = Other.IsBuilt;

  Other.Function = nullptr;
  Other.Instructions = nullptr;
  Other.Descriptor = nullptr;
  Other.InsertBlock = nullptr;
  Other.InsertBefore = nullptr;
  Other.IsBuilt = true;
  return *this;
}

Expected<void> MachineInstrBuilder::ensurePending() const {
  if (IsBuilt) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "machine instruction builder was already consumed");
  }
  return {};
}

Expected<void> MachineInstrBuilder::addOperand(MachineOperand &&Operand) {
  auto Pending = ensurePending();
  if (!Pending)
    return std::unexpected(Pending.error());
  if (Operand.parent() != nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder operand already belongs to an instruction");
  }
  if (Operand.isImplicit()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "implicit register requires addImplicitOperand");
  }
  ExplicitOperands.push_back(std::move(Operand));
  return {};
}

Expected<void> MachineInstrBuilder::addImplicitOperand(MachineOperand &&Operand) {
  auto Pending = ensurePending();
  if (!Pending)
    return std::unexpected(Pending.error());
  if (Operand.parent() != nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder operand already belongs to an instruction");
  }
  if (!Operand.isRegister() || !Operand.isImplicit()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "dynamic implicit operand must be an implicit register");
  }
  ExtraImplicitOperands.push_back(std::move(Operand));
  return {};
}

Expected<void> MachineInstrBuilder::appendRegister(Register Reg, RegisterOperandFlags RegisterFlags,
                                                   SubRegIndex SubReg, std::uint32_t TargetFlags,
                                                   bool IsImplicit) {
  auto Operand = MachineOperand::registerOperand(Reg, RegisterFlags, SubReg, TargetFlags);
  if (!Operand)
    return std::unexpected(Operand.error());
  if (IsImplicit)
    return addImplicitOperand(std::move(*Operand));
  return addOperand(std::move(*Operand));
}

Expected<void> MachineInstrBuilder::addDef(Register Reg, RegisterOperandFlags AdditionalFlags,
                                           SubRegIndex SubReg, std::uint32_t TargetFlags) {
  AdditionalFlags.set(RegisterOperandFlag::Define);
  return appendRegister(Reg, AdditionalFlags, SubReg, TargetFlags, false);
}

Expected<void> MachineInstrBuilder::addUse(Register Reg, RegisterOperandFlags AdditionalFlags,
                                           SubRegIndex SubReg, std::uint32_t TargetFlags) {
  AdditionalFlags.set(RegisterOperandFlag::Use);
  return appendRegister(Reg, AdditionalFlags, SubReg, TargetFlags, false);
}

Expected<void> MachineInstrBuilder::addDefUse(Register Reg, RegisterOperandFlags AdditionalFlags,
                                              SubRegIndex SubReg, std::uint32_t TargetFlags) {
  AdditionalFlags.set(RegisterOperandFlag::Define);
  AdditionalFlags.set(RegisterOperandFlag::Use);
  AdditionalFlags.set(RegisterOperandFlag::InternalRead);
  return appendRegister(Reg, AdditionalFlags, SubReg, TargetFlags, false);
}

Expected<void> MachineInstrBuilder::addImplicitDef(Register Reg,
                                                   RegisterOperandFlags AdditionalFlags,
                                                   SubRegIndex SubReg, std::uint32_t TargetFlags) {
  AdditionalFlags.set(RegisterOperandFlag::Define);
  AdditionalFlags.set(RegisterOperandFlag::Implicit);
  return appendRegister(Reg, AdditionalFlags, SubReg, TargetFlags, true);
}

Expected<void> MachineInstrBuilder::addImplicitUse(Register Reg,
                                                   RegisterOperandFlags AdditionalFlags,
                                                   SubRegIndex SubReg, std::uint32_t TargetFlags) {
  AdditionalFlags.set(RegisterOperandFlag::Use);
  AdditionalFlags.set(RegisterOperandFlag::Implicit);
  return appendRegister(Reg, AdditionalFlags, SubReg, TargetFlags, true);
}

Expected<void> MachineInstrBuilder::addImmediate(std::int64_t Value, std::uint32_t TargetFlags) {
  return addOperand(MachineOperand::immediate(Value, TargetFlags));
}

Expected<void> MachineInstrBuilder::addFloatingImmediate(FloatingPointBits Bits,
                                                         std::uint32_t TargetFlags) {
  return addOperand(MachineOperand::floatingImmediate(Bits, TargetFlags));
}

Expected<void> MachineInstrBuilder::addBasicBlock(MachineBasicBlock &Block,
                                                  std::uint32_t TargetFlags) {
  return addOperand(MachineOperand::basicBlock(Block, TargetFlags));
}

Expected<void> MachineInstrBuilder::addFrameIndex(FrameIndex Index, std::int64_t Offset,
                                                  std::uint32_t TargetFlags) {
  return addOperand(MachineOperand::frameIndex(Index, Offset, TargetFlags));
}

Expected<void> MachineInstrBuilder::addConstantPool(ConstantPoolIndex Index, std::int64_t Offset,
                                                    std::uint32_t TargetFlags) {
  return addOperand(MachineOperand::constantPool(Index, Offset, TargetFlags));
}

Expected<void> MachineInstrBuilder::addJumpTable(JumpTableIndex Index, std::uint32_t TargetFlags) {
  return addOperand(MachineOperand::jumpTable(Index, TargetFlags));
}

Expected<void> MachineInstrBuilder::addGlobalSymbol(std::string Name, std::int64_t Offset,
                                                    std::uint32_t TargetFlags) {
  auto Operand = MachineOperand::globalSymbol(std::move(Name), Offset, TargetFlags);
  if (!Operand)
    return std::unexpected(Operand.error());
  return addOperand(std::move(*Operand));
}

Expected<void> MachineInstrBuilder::addExternalSymbol(std::string Name, std::int64_t Offset,
                                                      std::uint32_t TargetFlags) {
  auto Operand = MachineOperand::externalSymbol(std::move(Name), Offset, TargetFlags);
  if (!Operand)
    return std::unexpected(Operand.error());
  return addOperand(std::move(*Operand));
}

Expected<void> MachineInstrBuilder::addBlockAddress(std::string FunctionName, std::string BlockName,
                                                    std::int64_t Offset,
                                                    std::uint32_t TargetFlags) {
  auto Operand = MachineOperand::blockAddress(std::move(FunctionName), std::move(BlockName), Offset,
                                              TargetFlags);
  if (!Operand)
    return std::unexpected(Operand.error());
  return addOperand(std::move(*Operand));
}

Expected<void> MachineInstrBuilder::addRegisterMask(std::shared_ptr<const RegisterMask> Mask,
                                                    std::uint32_t TargetFlags) {
  auto Operand = MachineOperand::registerMask(std::move(Mask), TargetFlags);
  if (!Operand)
    return std::unexpected(Operand.error());
  return addOperand(std::move(*Operand));
}

Expected<void> MachineInstrBuilder::addMetadata(std::string Marker, std::uint32_t TargetFlags) {
  auto Operand = MachineOperand::metadata(std::move(Marker), TargetFlags);
  if (!Operand)
    return std::unexpected(Operand.error());
  return addOperand(std::move(*Operand));
}

Expected<void> MachineInstrBuilder::addTargetIndex(TargetIndex Index, std::int64_t Offset,
                                                   std::uint32_t TargetFlags) {
  return addOperand(MachineOperand::targetIndex(Index, Offset, TargetFlags));
}

Expected<void> MachineInstrBuilder::addMCSymbol(std::string Name, std::int64_t Offset,
                                                std::uint32_t TargetFlags) {
  auto Operand = MachineOperand::mcSymbol(std::move(Name), Offset, TargetFlags);
  if (!Operand)
    return std::unexpected(Operand.error());
  return addOperand(std::move(*Operand));
}

Expected<void> MachineInstrBuilder::tieOperands(std::size_t DefOperand, std::size_t UseOperand) {
  auto Pending = ensurePending();
  if (!Pending)
    return std::unexpected(Pending.error());
  if (DefOperand >= ExplicitOperands.size() || UseOperand >= ExplicitOperands.size() ||
      DefOperand == UseOperand) {
    return makeUnexpected(ErrorCode::InvalidArgument, "builder operand tie indices are invalid");
  }

  const MachineOperand &Def = ExplicitOperands[DefOperand];
  const MachineOperand &Use = ExplicitOperands[UseOperand];
  if (!Def.isRegister() || !Use.isRegister() || !Def.isDef() || Def.isUse() || !Use.isUse() ||
      Use.isDef()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder tie must connect a pure register def and use");
  }

  const MachineOperandTie Tie{DefOperand, UseOperand};
  if (std::ranges::find(RequestedTies, Tie) == RequestedTies.end())
    RequestedTies.push_back(Tie);
  return {};
}

Expected<void> MachineInstrBuilder::addMemOperand(const MachineMemOperand &MemOperand) {
  auto Pending = ensurePending();
  if (!Pending)
    return std::unexpected(Pending.error());
  if (!Function->ownsMemOperand(MemOperand)) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder memory operand belongs to another function");
  }
  MemOperands.push_back(&MemOperand);
  return {};
}

Expected<void> MachineInstrBuilder::setFlags(MachineInstrFlags NewFlags) {
  auto Pending = ensurePending();
  if (!Pending)
    return std::unexpected(Pending.error());
  Flags = NewFlags;
  return {};
}

Expected<void> MachineInstrBuilder::setSourceLocation(SourceLocation NewLocation) {
  auto Pending = ensurePending();
  if (!Pending)
    return std::unexpected(Pending.error());
  if (!NewLocation.isValid()) {
    return makeUnexpected(ErrorCode::InvalidArgument, "builder source location is incomplete");
  }
  Location = std::move(NewLocation);
  return {};
}

Expected<void> MachineInstrBuilder::clearSourceLocation() {
  auto Pending = ensurePending();
  if (!Pending)
    return std::unexpected(Pending.error());
  Location.reset();
  return {};
}

Expected<void> MachineInstrBuilder::setOrigin(OriginID NewOrigin) {
  auto Pending = ensurePending();
  if (!Pending)
    return std::unexpected(Pending.error());
  if (NewOrigin.value() == 0) {
    return makeUnexpected(ErrorCode::InvalidArgument, "builder origin ID must be nonzero");
  }
  Origin = NewOrigin;
  return {};
}

Expected<void> MachineInstrBuilder::clearOrigin() {
  auto Pending = ensurePending();
  if (!Pending)
    return std::unexpected(Pending.error());
  Origin.reset();
  return {};
}

Expected<void> MachineInstrBuilder::validateInsertionPoint() const {
  if (InsertBlock == nullptr || &InsertBlock->function() != Function || !InsertBlock->isLinked()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder insertion block is not linked to its function");
  }
  if (InsertBefore != nullptr && InsertBefore->parent() != InsertBlock) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder insertion cursor is no longer in its block");
  }
  return {};
}

Expected<void> MachineInstrBuilder::validateFrameReference(FrameIndex Index) const {
  const StackObject *Object = Function->frameInfo().object(Index);
  if (Object == nullptr || Object->description()->IsDead) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder references an unknown or dead stack object");
  }
  return {};
}

Expected<void> MachineInstrBuilder::validateConstantPoolReference(ConstantPoolIndex Index) const {
  if (Function->constantPool().entry(Index) == nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder references an unknown constant pool entry");
  }
  return {};
}

Expected<void> MachineInstrBuilder::validateJumpTableReference(JumpTableIndex Index) const {
  if (Function->jumpTableInfo().table(Index) == nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument, "builder references an unknown jump table");
  }
  return {};
}

Expected<void> MachineInstrBuilder::validateOperandOwnership(const MachineOperand &Operand) const {
  if (Operand.parent() != nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder operand already belongs to an instruction");
  }
  if (const auto *Block = Operand.getIf<BasicBlockOperand>();
      Block != nullptr && (Block->Block == nullptr || &Block->Block->function() != Function ||
                           !Block->Block->isLinked())) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder basic block operand is not linked to its function");
  }
  if (const Register *Reg = Operand.getIf<Register>();
      Reg != nullptr && Reg->isVirtual() &&
      !Function->registerInfo().isKnownVirtualRegister(*Reg)) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder operand references an unknown virtual register");
  }
  if (const auto *Frame = Operand.getIf<FrameIndexOperand>(); Frame != nullptr) {
    auto FrameResult = validateFrameReference(Frame->Index);
    if (!FrameResult)
      return FrameResult;
  }
  if (const auto *Constant = Operand.getIf<ConstantPoolOperand>(); Constant != nullptr) {
    auto ConstantResult = validateConstantPoolReference(Constant->Index);
    if (!ConstantResult)
      return ConstantResult;
  }
  if (const auto *JumpTable = Operand.getIf<JumpTableOperand>(); JumpTable != nullptr) {
    auto JumpTableResult = validateJumpTableReference(JumpTable->Index);
    if (!JumpTableResult)
      return JumpTableResult;
  }
  return {};
}

std::vector<MachineOperandTie> MachineInstrBuilder::collectOperandTies() const {
  std::vector<MachineOperandTie> Result;
  for (std::size_t Index = 0; Index < Descriptor->Operands.size(); ++Index) {
    const OperandDescriptor &Operand = Descriptor->Operands[Index];
    if (!Operand.DefaultTiedOperand || Index > *Operand.DefaultTiedOperand)
      continue;
    const std::size_t Other = *Operand.DefaultTiedOperand;
    const bool IndexIsDef = Operand.RegisterRole == RegisterOperandRole::Def;
    Result.push_back(IndexIsDef ? MachineOperandTie{Index, Other}
                                : MachineOperandTie{Other, Index});
  }
  for (const MachineOperandTie Tie : RequestedTies) {
    if (std::ranges::find(Result, Tie) == Result.end())
      Result.push_back(Tie);
  }
  return Result;
}

Expected<MachineInstr *> MachineInstrBuilder::build() {
  auto Pending = ensurePending();
  if (!Pending)
    return std::unexpected(Pending.error());

  auto InsertionValidation = validateInsertionPoint();
  if (!InsertionValidation)
    return std::unexpected(InsertionValidation.error());

  for (const MachineOperand &Operand : ExplicitOperands) {
    auto Ownership = validateOperandOwnership(Operand);
    if (!Ownership)
      return std::unexpected(Ownership.error());
  }
  for (const MachineOperand &Operand : ExtraImplicitOperands) {
    auto Ownership = validateOperandOwnership(Operand);
    if (!Ownership)
      return std::unexpected(Ownership.error());
  }
  for (const MachineMemOperand *MemOperand : MemOperands) {
    const MemoryBase &Base = MemOperand->description().Base;
    if (const auto Frame = Base.frameIndex()) {
      auto FrameResult = validateFrameReference(*Frame);
      if (!FrameResult)
        return std::unexpected(FrameResult.error());
    }
    if (const auto Constant = Base.constantPoolIndex()) {
      auto ConstantResult = validateConstantPoolReference(*Constant);
      if (!ConstantResult)
        return std::unexpected(ConstantResult.error());
    }
    if (const auto JumpTable = Base.jumpTableIndex()) {
      auto JumpTableResult = validateJumpTableReference(*JumpTable);
      if (!JumpTableResult)
        return std::unexpected(JumpTableResult.error());
    }
  }

  auto ExplicitValidation = Instructions->validateExplicitOperands(
      Descriptor->InstructionOpcode, ExplicitOperands, Function->registerInfo());
  if (!ExplicitValidation)
    return std::unexpected(ExplicitValidation.error());

  auto DefaultImplicit = Instructions->createDefaultImplicitOperands(*Descriptor);
  if (!DefaultImplicit)
    return std::unexpected(DefaultImplicit.error());

  std::vector<const MachineOperand *> ImplicitOperands;
  ImplicitOperands.reserve(DefaultImplicit->size() + ExtraImplicitOperands.size());
  for (const MachineOperand &Operand : *DefaultImplicit)
    ImplicitOperands.push_back(&Operand);
  for (const MachineOperand &Operand : ExtraImplicitOperands)
    ImplicitOperands.push_back(&Operand);
  auto ImplicitValidation =
      Instructions->validateImplicitOperands(Descriptor->InstructionOpcode, ImplicitOperands);
  if (!ImplicitValidation)
    return std::unexpected(ImplicitValidation.error());

  const std::vector<MachineOperandTie> Ties = collectOperandTies();
  auto TieValidation =
      Instructions->validateOperandTies(Descriptor->InstructionOpcode, ExplicitOperands, Ties);
  if (!TieValidation)
    return std::unexpected(TieValidation.error());

  auto MemoryValidation =
      Instructions->validateMemOperands(Descriptor->InstructionOpcode, MemOperands);
  if (!MemoryValidation)
    return std::unexpected(MemoryValidation.error());

  auto Instruction = Function->createInstruction(Descriptor->InstructionOpcode);
  if (!Instruction)
    return std::unexpected(Instruction.error());
  MachineInstr *Result = *Instruction;
  Result->reserveOperands(ExplicitOperands.size() + DefaultImplicit->size() +
                          ExtraImplicitOperands.size());

  for (MachineOperand &Operand : ExplicitOperands) {
    auto AddResult = Result->addOperand(std::move(Operand));
    if (!AddResult)
      return std::unexpected(AddResult.error());
  }
  for (MachineOperand &Operand : *DefaultImplicit) {
    auto AddResult = Result->addOperand(std::move(Operand));
    if (!AddResult)
      return std::unexpected(AddResult.error());
  }
  for (MachineOperand &Operand : ExtraImplicitOperands) {
    auto AddResult = Result->addOperand(std::move(Operand));
    if (!AddResult)
      return std::unexpected(AddResult.error());
  }
  for (const MachineOperandTie Tie : Ties) {
    auto TieResult = Result->tieOperands(Tie.DefOperand, Tie.UseOperand);
    if (!TieResult)
      return std::unexpected(TieResult.error());
  }
  for (const MachineMemOperand *MemOperand : MemOperands) {
    auto AddResult = Result->addMemOperand(*MemOperand);
    if (!AddResult)
      return std::unexpected(AddResult.error());
  }

  Result->setFlags(Flags);
  if (Location)
    Result->setSourceLocation(std::move(*Location));
  if (Origin)
    Result->setOrigin(*Origin);

  Expected<void> InsertResult;
  if (InsertBefore == nullptr)
    InsertResult = InsertBlock->appendInstruction(*Result);
  else
    InsertResult = InsertBlock->insertInstructionBefore(*InsertBefore, *Result);
  if (!InsertResult)
    return std::unexpected(InsertResult.error());

  IsBuilt = true;
  return Result;
}

Expected<void> MachineIRBuilder::validateBlock(const MachineBasicBlock &Block) const {
  if (&Block.function() != Function || !Block.isLinked()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder insertion block is not linked to its function");
  }
  return {};
}

Expected<void> MachineIRBuilder::validateInstruction(const MachineInstr &Instruction) const {
  if (&Instruction.function() != Function || Instruction.parent() == nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "builder insertion instruction is not linked to its function");
  }
  return {};
}

Expected<void> MachineIRBuilder::setInsertPoint(MachineBasicBlock &Block) {
  auto Validation = validateBlock(Block);
  if (!Validation)
    return std::unexpected(Validation.error());
  InsertBlock = &Block;
  InsertBefore = nullptr;
  return {};
}

Expected<void> MachineIRBuilder::setInsertPointBefore(MachineInstr &Instruction) {
  auto Validation = validateInstruction(Instruction);
  if (!Validation)
    return std::unexpected(Validation.error());
  InsertBlock = Instruction.parent();
  InsertBefore = &Instruction;
  return {};
}

Expected<void> MachineIRBuilder::setInsertPointAfter(MachineInstr &Instruction) {
  auto Validation = validateInstruction(Instruction);
  if (!Validation)
    return std::unexpected(Validation.error());
  InsertBlock = Instruction.parent();
  InsertBefore = Instruction.next();
  return {};
}

Expected<MachineInstrBuilder> MachineIRBuilder::buildInstr(Opcode InstructionOpcode) const {
  if (InsertBlock == nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument, "machine IR builder has no insertion point");
  }
  const InstructionDescriptor *Descriptor = Instructions->descriptor(InstructionOpcode);
  if (Descriptor == nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "machine IR builder opcode is not registered");
  }
  auto BlockValidation = validateBlock(*InsertBlock);
  if (!BlockValidation)
    return std::unexpected(BlockValidation.error());
  if (InsertBefore != nullptr && InsertBefore->parent() != InsertBlock) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "machine IR builder insertion cursor is stale");
  }
  return MachineInstrBuilder(*Function, *Instructions, *Descriptor, *InsertBlock, InsertBefore);
}

} // namespace rcc