aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/MachineOperand.cpp
blob: d1d152e13d3a638590622decee1b47cdc1ff03a9 (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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/MachineOperand.h"

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

#include <algorithm>
#include <array>
#include <cstdint>
#include <limits>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace rcc {
namespace {

[[nodiscard]] constexpr std::size_t integerWordCount(std::uint32_t BitWidth) noexcept {
  return (static_cast<std::size_t>(BitWidth) + 63U) / 64U;
}

[[nodiscard]] constexpr std::uint64_t integerHighWordMask(std::uint32_t BitWidth) noexcept {
  const std::uint32_t UsedBits = BitWidth % 64U;
  if (UsedBits == 0)
    return std::numeric_limits<std::uint64_t>::max();
  return (std::uint64_t{1} << UsedBits) - 1U;
}

[[nodiscard]] Expected<void> verifyIntegerImmediate(std::uint32_t BitWidth,
                                                    std::span<const std::uint64_t> Words,
                                                    ErrorCode Code) {
  if (BitWidth == 0 || BitWidth > IntegerImmediate::MaxBitWidth) {
    return makeUnexpected(Code, "integer immediate bit width is outside the supported range");
  }
  if (Words.size() != integerWordCount(BitWidth)) {
    return makeUnexpected(Code, "integer immediate bit pattern has the wrong word count");
  }
  if ((Words.back() & ~integerHighWordMask(BitWidth)) != 0) {
    return makeUnexpected(Code, "integer immediate has nonzero bits above its declared width");
  }
  return {};
}

} // namespace

Expected<IntegerImmediate> IntegerImmediate::create(std::uint32_t BitWidth,
                                                    std::span<const std::uint64_t> Words) {
  auto Verification = verifyIntegerImmediate(BitWidth, Words, ErrorCode::InvalidArgument);
  if (!Verification)
    return std::unexpected(Verification.error());
  return IntegerImmediate(BitWidth, std::vector<std::uint64_t>(Words.begin(), Words.end()));
}

Expected<IntegerImmediate> IntegerImmediate::fromUnsigned(std::uint32_t BitWidth,
                                                          std::uint64_t Value) {
  if (BitWidth == 0 || BitWidth > MaxBitWidth) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "integer immediate bit width is outside the supported range");
  }
  if (BitWidth < 64U && (Value >> BitWidth) != 0) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "unsigned value does not fit the integer immediate bit width");
  }

  std::vector<std::uint64_t> Words(integerWordCount(BitWidth), 0);
  Words.front() = Value;
  return create(BitWidth, Words);
}

Expected<IntegerImmediate> IntegerImmediate::fromSigned(std::uint32_t BitWidth,
                                                        std::int64_t Value) {
  if (BitWidth == 0 || BitWidth > MaxBitWidth) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "integer immediate bit width is outside the supported range");
  }
  if (BitWidth < 64U) {
    const std::uint32_t MagnitudeBits = BitWidth - 1U;
    const std::int64_t Minimum = -(std::int64_t{1} << MagnitudeBits);
    const std::int64_t Maximum = (std::int64_t{1} << MagnitudeBits) - 1;
    if (Value < Minimum || Value > Maximum) {
      return makeUnexpected(ErrorCode::InvalidArgument,
                            "signed value does not fit the integer immediate bit width");
    }
  }

  const std::uint64_t Fill =
      Value < 0 ? std::numeric_limits<std::uint64_t>::max() : std::uint64_t{0};
  std::vector<std::uint64_t> Words(integerWordCount(BitWidth), Fill);
  Words.front() = static_cast<std::uint64_t>(Value);
  Words.back() &= integerHighWordMask(BitWidth);
  return create(BitWidth, Words);
}

Expected<IntegerImmediate> IntegerImmediate::parseDecimal(std::uint32_t BitWidth,
                                                          std::string_view Text) {
  if (BitWidth == 0 || BitWidth > MaxBitWidth) {
    return makeUnexpected(ErrorCode::ParseError,
                          "integer immediate bit width is outside the supported range");
  }
  const bool Negative = Text.starts_with('-');
  if (Negative)
    Text.remove_prefix(1);
  if (Text.empty())
    return makeUnexpected(ErrorCode::ParseError, "integer immediate has no decimal digits");

  std::vector<std::uint64_t> Words(integerWordCount(BitWidth), 0);
  for (const char Character : Text) {
    if (Character < '0' || Character > '9') {
      return makeUnexpected(ErrorCode::ParseError,
                            "integer immediate contains a non-decimal digit");
    }
    std::uint64_t Carry = static_cast<unsigned>(Character - '0');
    for (std::uint64_t &Word : Words) {
      const unsigned __int128 Product = static_cast<unsigned __int128>(Word) * 10U + Carry;
      Word = static_cast<std::uint64_t>(Product);
      Carry = static_cast<std::uint64_t>(Product >> 64U);
    }
    if (Carry != 0 || (Words.back() & ~integerHighWordMask(BitWidth)) != 0) {
      return makeUnexpected(ErrorCode::ParseError,
                            "integer immediate does not fit its declared bit width");
    }
  }

  if (Negative) {
    if (std::ranges::all_of(Words, [](std::uint64_t Word) { return Word == 0; })) {
      return makeUnexpected(ErrorCode::ParseError,
                            "negative zero is not a canonical integer immediate");
    }
    const std::uint32_t SignBit = BitWidth - 1U;
    const std::size_t SignWord = SignBit / 64U;
    const std::uint64_t SignMask = std::uint64_t{1} << (SignBit % 64U);
    bool Fits = Words[SignWord] <= SignMask;
    for (std::size_t Index = SignWord + 1; Index < Words.size(); ++Index)
      Fits = Fits && Words[Index] == 0;
    if (Words[SignWord] == SignMask) {
      for (std::size_t Index = 0; Index < SignWord; ++Index)
        Fits = Fits && Words[Index] == 0;
    }
    if (!Fits) {
      return makeUnexpected(ErrorCode::ParseError,
                            "negative integer immediate does not fit its declared bit width");
    }
    for (std::uint64_t &Word : Words)
      Word = ~Word;
    std::uint64_t Carry = 1;
    for (std::uint64_t &Word : Words) {
      const std::uint64_t Previous = Word;
      Word += Carry;
      Carry = Carry != 0 && Word < Previous ? 1 : 0;
    }
    Words.back() &= integerHighWordMask(BitWidth);
  }
  return create(BitWidth, Words);
}

bool IntegerImmediate::isZero() const noexcept {
  return std::ranges::all_of(Words, [](std::uint64_t Word) { return Word == 0; });
}

bool IntegerImmediate::isNegative() const noexcept {
  const std::uint32_t SignBit = BitWidth - 1U;
  return (Words[SignBit / 64U] & (std::uint64_t{1} << (SignBit % 64U))) != 0;
}

std::string IntegerImmediate::toUnsignedDecimal() const {
  if (isZero())
    return "0";
  std::vector<std::uint64_t> Remaining = Words;
  std::string Digits;
  while (!std::ranges::all_of(Remaining, [](std::uint64_t Word) { return Word == 0; })) {
    unsigned __int128 Remainder = 0;
    for (auto Iterator = Remaining.rbegin(); Iterator != Remaining.rend(); ++Iterator) {
      const unsigned __int128 Dividend = (Remainder << 64U) | *Iterator;
      *Iterator = static_cast<std::uint64_t>(Dividend / 10U);
      Remainder = Dividend % 10U;
    }
    Digits.push_back(static_cast<char>('0' + static_cast<unsigned>(Remainder)));
  }
  std::ranges::reverse(Digits);
  return Digits;
}

Expected<void> IntegerImmediate::verify() const {
  return verifyIntegerImmediate(BitWidth, Words, ErrorCode::VerificationError);
}

Expected<FloatingPointBits> FloatingPointBits::create(FloatSemantics Semantics,
                                                      std::span<const std::uint64_t> Words) {
  const auto BitWidth = getFloatSemanticsBitWidth(Semantics);
  if (!BitWidth) {
    return makeUnexpected(ErrorCode::InvalidArgument, "floating immediate has unknown semantics");
  }

  const std::size_t RequiredWords = (*BitWidth + 63U) / 64U;
  if (RequiredWords > 2 || Words.size() != RequiredWords) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "floating immediate bit pattern has the wrong word count");
  }

  const std::uint32_t UsedBitsInLastWord = *BitWidth % 64U;
  if (UsedBitsInLastWord != 0 && (Words.back() >> UsedBitsInLastWord) != 0) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "floating immediate has nonzero bits above its semantics width");
  }

  std::array<std::uint64_t, 2> StoredWords{};
  std::ranges::copy(Words, StoredWords.begin());
  return FloatingPointBits(Semantics, StoredWords, static_cast<std::uint8_t>(RequiredWords));
}

RegisterOperandFlags::RegisterOperandFlags(
    std::initializer_list<RegisterOperandFlag> Flags) noexcept {
  for (const RegisterOperandFlag Flag : Flags)
    set(Flag);
}

Expected<void> PredicateOperand::verify() const {
  if (const auto *Integer = integerPredicate();
      Integer != nullptr &&
      static_cast<std::uint8_t>(*Integer) < static_cast<std::uint8_t>(IntegerPredicate::Count)) {
    return {};
  }
  if (const auto *Floating = floatingPredicate();
      Floating != nullptr &&
      static_cast<std::uint8_t>(*Floating) < static_cast<std::uint8_t>(FloatingPredicate::Count)) {
    return {};
  }
  return makeUnexpected(ErrorCode::VerificationError, "predicate operand contains an invalid kind");
}

Expected<void> MachineOperand::verifyRegisterFlags(RegisterOperandFlags Flags) {
  const bool IsDef = Flags.has(RegisterOperandFlag::Define);
  const bool IsUse = Flags.has(RegisterOperandFlag::Use);
  if (!IsDef && !IsUse) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "register operand must be a definition or use");
  }
  if (Flags.has(RegisterOperandFlag::Kill) && !IsUse) {
    return makeUnexpected(ErrorCode::InvalidArgument, "kill flag requires a register use");
  }
  if (Flags.has(RegisterOperandFlag::Dead) && !IsDef) {
    return makeUnexpected(ErrorCode::InvalidArgument, "dead flag requires a register definition");
  }
  if (Flags.has(RegisterOperandFlag::Undef) && !IsUse) {
    return makeUnexpected(ErrorCode::InvalidArgument, "undef flag requires a register use");
  }
  if (Flags.has(RegisterOperandFlag::EarlyClobber) && !IsDef) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "early-clobber flag requires a register definition");
  }
  if (Flags.has(RegisterOperandFlag::InternalRead) && (!IsDef || !IsUse)) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "internal-read flag requires a register def-use operand");
  }
  if (IsDef && IsUse && !Flags.has(RegisterOperandFlag::InternalRead)) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "register def-use operand requires internal-read");
  }
  if (Flags.has(RegisterOperandFlag::Debug) && (!IsUse || IsDef)) {
    return makeUnexpected(ErrorCode::InvalidArgument, "debug register operand must be a use");
  }
  return {};
}

Expected<MachineOperand> MachineOperand::registerOperand(Register Reg, RegisterOperandFlags Flags,
                                                         SubRegIndex SubReg,
                                                         std::uint32_t TargetFlags) {
  const auto Validation = verifyRegisterFlags(Flags);
  if (!Validation)
    return std::unexpected(Validation.error());

  MachineOperand Operand(Kind::Register, Reg, TargetFlags);
  Operand.RegFlags = Flags;
  Operand.RegSubReg = SubReg;
  return Operand;
}

MachineOperand MachineOperand::immediate(std::int64_t Value, std::uint32_t TargetFlags) {
  return MachineOperand(Kind::Immediate, Value, TargetFlags);
}

MachineOperand MachineOperand::integerImmediate(IntegerImmediate Value, std::uint32_t TargetFlags) {
  return MachineOperand(Kind::IntegerImmediate, IntegerImmediateOperand{std::move(Value)},
                        TargetFlags);
}

MachineOperand MachineOperand::floatingImmediate(FloatingPointBits Bits,
                                                 std::uint32_t TargetFlags) {
  return MachineOperand(Kind::FloatingImmediate, FloatingImmediateOperand{Bits}, TargetFlags);
}

MachineOperand MachineOperand::basicBlock(MachineBasicBlock &Block, std::uint32_t TargetFlags) {
  return MachineOperand(Kind::BasicBlock, BasicBlockOperand{&Block}, TargetFlags);
}

MachineOperand MachineOperand::frameIndex(FrameIndex Index, std::int64_t Offset,
                                          std::uint32_t TargetFlags) {
  return MachineOperand(Kind::FrameIndex, FrameIndexOperand{Index, Offset}, TargetFlags);
}

MachineOperand MachineOperand::constantPool(ConstantPoolIndex Index, std::int64_t Offset,
                                            std::uint32_t TargetFlags) {
  return MachineOperand(Kind::ConstantPoolIndex, ConstantPoolOperand{Index, Offset}, TargetFlags);
}

MachineOperand MachineOperand::jumpTable(JumpTableIndex Index, std::uint32_t TargetFlags) {
  return MachineOperand(Kind::JumpTableIndex, JumpTableOperand{Index}, TargetFlags);
}

Expected<MachineOperand> MachineOperand::globalSymbol(std::string Name, std::int64_t Offset,
                                                      std::uint32_t TargetFlags) {
  if (Name.empty()) {
    return makeUnexpected(ErrorCode::InvalidArgument, "global symbol operand name cannot be empty");
  }
  return MachineOperand(Kind::GlobalSymbol, GlobalSymbolOperand{std::move(Name), Offset},
                        TargetFlags);
}

Expected<MachineOperand> MachineOperand::externalSymbol(std::string Name, std::int64_t Offset,
                                                        std::uint32_t TargetFlags) {
  if (Name.empty()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "external symbol operand name cannot be empty");
  }
  return MachineOperand(Kind::ExternalSymbol, ExternalSymbolOperand{std::move(Name), Offset},
                        TargetFlags);
}

Expected<MachineOperand> MachineOperand::blockAddress(std::string FunctionName,
                                                      std::string BlockName, std::int64_t Offset,
                                                      std::uint32_t TargetFlags) {
  if (FunctionName.empty() || BlockName.empty()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "block address operand requires function and block names");
  }
  return MachineOperand(Kind::BlockAddress,
                        BlockAddressOperand{std::move(FunctionName), std::move(BlockName), Offset},
                        TargetFlags);
}

Expected<MachineOperand> MachineOperand::registerMask(std::shared_ptr<const RegisterMask> Mask,
                                                      std::uint32_t TargetFlags) {
  if (!Mask) {
    return makeUnexpected(ErrorCode::InvalidArgument, "register mask operand cannot be null");
  }
  return MachineOperand(Kind::RegisterMask, RegisterMaskOperand{std::move(Mask)}, TargetFlags);
}

Expected<MachineOperand>
MachineOperand::liveOutRegisterMask(std::shared_ptr<const RegisterMask> Mask,
                                    std::uint32_t TargetFlags) {
  if (!Mask) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "live-out register-mask operand cannot be null");
  }
  return MachineOperand(Kind::LiveOutRegisterMask, LiveOutRegisterMaskOperand{std::move(Mask)},
                        TargetFlags);
}

Expected<MachineOperand> MachineOperand::metadata(MachineMetadataValue Value,
                                                  std::uint32_t TargetFlags) {
  if (!Value.isValid()) {
    return makeUnexpected(ErrorCode::InvalidArgument, "metadata operand value must be valid");
  }
  return MachineOperand(Kind::Metadata, MetadataOperand{std::move(Value)}, TargetFlags);
}

MachineOperand MachineOperand::targetIndex(TargetIndex Index, std::int64_t Offset,
                                           std::uint32_t TargetFlags) {
  return MachineOperand(Kind::TargetIndex, TargetIndexOperand{Index, Offset}, TargetFlags);
}

Expected<MachineOperand> MachineOperand::mcSymbol(std::string Name, std::int64_t Offset,
                                                  std::uint32_t TargetFlags) {
  if (Name.empty()) {
    return makeUnexpected(ErrorCode::InvalidArgument, "MC symbol operand name cannot be empty");
  }
  return MachineOperand(Kind::MCSymbol, MCSymbolOperand{std::move(Name), Offset}, TargetFlags);
}

MachineOperand MachineOperand::cfiIndex(CFIInstructionIndex Index, std::uint32_t TargetFlags) {
  return MachineOperand(Kind::CFIIndex, CFIIndexOperand{Index}, TargetFlags);
}

Expected<MachineOperand> MachineOperand::intrinsic(IntrinsicID ID, std::uint32_t TargetFlags) {
  if (ID.value() == 0) {
    return makeUnexpected(ErrorCode::InvalidArgument, "intrinsic operand ID must be nonzero");
  }
  return MachineOperand(Kind::Intrinsic, IntrinsicOperand{ID}, TargetFlags);
}

Expected<MachineOperand> MachineOperand::integerPredicate(IntegerPredicate Predicate,
                                                          std::uint32_t TargetFlags) {
  PredicateOperand Value(Predicate);
  auto Verification = Value.verify();
  if (!Verification)
    return makeUnexpected(ErrorCode::InvalidArgument, "integer predicate operand is invalid");
  return MachineOperand(Kind::Predicate, Value, TargetFlags);
}

Expected<MachineOperand> MachineOperand::floatingPredicate(FloatingPredicate Predicate,
                                                           std::uint32_t TargetFlags) {
  PredicateOperand Value(Predicate);
  auto Verification = Value.verify();
  if (!Verification)
    return makeUnexpected(ErrorCode::InvalidArgument, "floating predicate operand is invalid");
  return MachineOperand(Kind::Predicate, Value, TargetFlags);
}

Expected<MachineOperand> MachineOperand::shuffleMask(std::vector<std::int32_t> Elements,
                                                     std::uint32_t TargetFlags) {
  if (Elements.size() < 2) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "shuffle-mask operand requires at least two elements");
  }
  return MachineOperand(Kind::ShuffleMask, ShuffleMaskOperand{std::move(Elements)}, TargetFlags);
}

Expected<MachineOperand> MachineOperand::debugInstrRef(std::uint64_t InstructionNumber,
                                                       std::uint64_t OperandNumber,
                                                       std::uint32_t TargetFlags) {
  if (InstructionNumber > std::numeric_limits<std::uint32_t>::max()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "debug instruction reference instruction number must fit in uint32");
  }
  if (OperandNumber > std::numeric_limits<std::uint32_t>::max()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "debug instruction reference operand number must fit in uint32");
  }
  return MachineOperand(Kind::DebugInstrRef,
                        DebugInstrRefOperand{static_cast<std::uint32_t>(InstructionNumber),
                                             static_cast<std::uint32_t>(OperandNumber)},
                        TargetFlags);
}

MachineOperand MachineOperand::laneMask(LaneMask Mask, std::uint32_t TargetFlags) {
  return MachineOperand(Kind::LaneMask, LaneMaskOperand{Mask}, TargetFlags);
}

MachineOperand::MachineOperand(MachineOperand &&Other) noexcept
    : OperandKind(Other.OperandKind), Storage(std::move(Other.Storage)), RegFlags(Other.RegFlags),
      RegSubReg(Other.RegSubReg), TiedOperandIndex(Other.TiedOperandIndex),
      TargetFlags(Other.TargetFlags), Parent(Other.Parent) {
  takeRegLinkFrom(Other);
  Other.Parent = nullptr;
  Other.TiedOperandIndex.reset();
}

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

  unlinkFromRegInfo();
  OperandKind = Other.OperandKind;
  Storage = std::move(Other.Storage);
  RegFlags = Other.RegFlags;
  RegSubReg = Other.RegSubReg;
  TiedOperandIndex = Other.TiedOperandIndex;
  TargetFlags = Other.TargetFlags;
  Parent = Other.Parent;
  takeRegLinkFrom(Other);
  Other.Parent = nullptr;
  Other.TiedOperandIndex.reset();
  return *this;
}

MachineOperand::~MachineOperand() {
  unlinkFromRegInfo();
}

std::optional<Register> MachineOperand::registerValue() const noexcept {
  if (!isRegister())
    return std::nullopt;
  return std::get<Register>(Storage);
}

bool MachineOperand::isDef() const noexcept {
  return isRegister() && RegFlags.has(RegisterOperandFlag::Define);
}

bool MachineOperand::isUse() const noexcept {
  return isRegister() && RegFlags.has(RegisterOperandFlag::Use);
}

bool MachineOperand::isDebugUse() const noexcept {
  return isUse() && RegFlags.has(RegisterOperandFlag::Debug);
}

bool MachineOperand::isImplicit() const noexcept {
  return isRegister() && RegFlags.has(RegisterOperandFlag::Implicit);
}

Expected<void> MachineOperand::setRegister(Register Reg) {
  if (!isRegister()) {
    return makeUnexpected(ErrorCode::InvalidArgument, "setRegister requires a register operand");
  }

  MachineRegisterInfo *RegInfo = LinkedRegInfo;
  if (RegInfo == nullptr && Parent != nullptr)
    RegInfo = &Parent->function().registerInfo();
  if (RegInfo != nullptr && Reg.isVirtual() && !RegInfo->isKnownVirtualRegister(Reg)) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "register operand references an unknown virtual register");
  }

  const bool WasLinked = LinkedRegInfo != nullptr;
  unlinkFromRegInfo();
  Storage = Reg;
  if (WasLinked && !Reg.isNoRegister())
    linkToRegInfo(*RegInfo);
  return {};
}

Expected<void> MachineOperand::setRegisterFlags(RegisterOperandFlags Flags) {
  if (!isRegister()) {
    return makeUnexpected(ErrorCode::InvalidArgument, "register flags require a register operand");
  }
  const auto Validation = verifyRegisterFlags(Flags);
  if (!Validation)
    return std::unexpected(Validation.error());
  RegFlags = Flags;
  return {};
}

Expected<void> MachineOperand::setSubReg(SubRegIndex SubReg) {
  if (!isRegister()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "subregister index requires a register operand");
  }
  RegSubReg = SubReg;
  return {};
}

Expected<void> MachineOperand::setBasicBlock(MachineBasicBlock &Block) {
  if (OperandKind != Kind::BasicBlock)
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "setBasicBlock requires a basic-block operand");
  if (Parent != nullptr && &Block.function() != &Parent->function())
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "basic-block operand references another machine function");
  std::get<BasicBlockOperand>(Storage).Block = &Block;
  return {};
}

Expected<void> MachineOperand::setFrameIndex(FrameIndex Index) {
  if (OperandKind != Kind::FrameIndex)
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "setFrameIndex requires a frame-index operand");
  if (Parent != nullptr && Parent->function().frameInfo().object(Index) == nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "frame-index operand references an unknown stack object");
  }
  std::get<FrameIndexOperand>(Storage).Index = Index;
  return {};
}

MachineOperand MachineOperand::cloneDetached() const {
  MachineOperand Result(OperandKind, Storage, TargetFlags);
  Result.RegFlags = RegFlags;
  Result.RegSubReg = RegSubReg;
  return Result;
}

void MachineOperand::attachTo(MachineInstr &Instruction, MachineRegisterInfo *RegInfo) {
  Parent = &Instruction;
  if (RegInfo != nullptr && isRegister() && !std::get<Register>(Storage).isNoRegister()) {
    linkToRegInfo(*RegInfo);
  }
}

void MachineOperand::detachFromInstruction() noexcept {
  unlinkFromRegInfo();
  Parent = nullptr;
}

void MachineOperand::linkToRegInfo(MachineRegisterInfo &RegInfo) {
  RegInfo.linkOperand(*this);
}

void MachineOperand::unlinkFromRegInfo() noexcept {
  if (PreviousRegLink == nullptr)
    return;

  *PreviousRegLink = NextRegOperand;
  if (NextRegOperand != nullptr)
    NextRegOperand->PreviousRegLink = PreviousRegLink;

  PreviousRegLink = nullptr;
  NextRegOperand = nullptr;
  LinkedRegInfo = nullptr;
}

void MachineOperand::takeRegLinkFrom(MachineOperand &Other) noexcept {
  LinkedRegInfo = Other.LinkedRegInfo;
  PreviousRegLink = Other.PreviousRegLink;
  NextRegOperand = Other.NextRegOperand;

  if (PreviousRegLink != nullptr)
    *PreviousRegLink = this;
  if (NextRegOperand != nullptr)
    NextRegOperand->PreviousRegLink = &NextRegOperand;

  Other.LinkedRegInfo = nullptr;
  Other.PreviousRegLink = nullptr;
  Other.NextRegOperand = nullptr;
}

} // namespace rcc