aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/MachineBasicBlock.cpp
blob: a8d69f66f2b2ceb49a1154dc4fb5b9703a73498c (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/MachineBasicBlock.h"

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

#include <algorithm>
#include <cstddef>

namespace rcc {

std::size_t MachineBasicBlock::instructionCount() const noexcept {
  std::size_t Count = 0;
  for (const MachineInstr *Instruction = FirstInstruction; Instruction != nullptr;
       Instruction = Instruction->next()) {
    ++Count;
  }
  return Count;
}

Expected<void> MachineBasicBlock::appendInstruction(MachineInstr &Instruction) {
  if (&Instruction.function() != OwnerFunction) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "instruction belongs to another machine function");
  }
  if (Instruction.isLinked()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "instruction is already linked to a basic block");
  }

  auto AttachResult = Instruction.attachToBlock(*this);
  if (!AttachResult)
    return std::unexpected(AttachResult.error());

  Instruction.PreviousInstruction = LastInstruction;
  Instruction.NextInstruction = nullptr;
  if (LastInstruction != nullptr)
    LastInstruction->NextInstruction = &Instruction;
  else
    FirstInstruction = &Instruction;
  LastInstruction = &Instruction;
  return {};
}

Expected<void> MachineBasicBlock::insertInstructionBefore(MachineInstr &Position,
                                                          MachineInstr &Instruction) {
  if (Position.parent() != this) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "instruction insertion position is not in this block");
  }
  if (&Instruction.function() != OwnerFunction || Instruction.isLinked()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "inserted instruction is linked or belongs to another function");
  }

  auto AttachResult = Instruction.attachToBlock(*this);
  if (!AttachResult)
    return std::unexpected(AttachResult.error());

  Instruction.PreviousInstruction = Position.PreviousInstruction;
  Instruction.NextInstruction = &Position;
  if (Position.PreviousInstruction != nullptr)
    Position.PreviousInstruction->NextInstruction = &Instruction;
  else
    FirstInstruction = &Instruction;
  Position.PreviousInstruction = &Instruction;
  return {};
}

Expected<void> MachineBasicBlock::insertInstructionAfter(MachineInstr &Position,
                                                         MachineInstr &Instruction) {
  if (Position.parent() != this) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "instruction insertion position is not in this block");
  }
  if (&Instruction.function() != OwnerFunction || Instruction.isLinked()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "inserted instruction is linked or belongs to another function");
  }

  auto AttachResult = Instruction.attachToBlock(*this);
  if (!AttachResult)
    return std::unexpected(AttachResult.error());

  Instruction.PreviousInstruction = &Position;
  Instruction.NextInstruction = Position.NextInstruction;
  if (Position.NextInstruction != nullptr)
    Position.NextInstruction->PreviousInstruction = &Instruction;
  else
    LastInstruction = &Instruction;
  Position.NextInstruction = &Instruction;
  return {};
}

Expected<void> MachineBasicBlock::removeInstruction(MachineInstr &Instruction) {
  if (Instruction.parent() != this) {
    return makeUnexpected(ErrorCode::InvalidArgument, "instruction does not belong to this block");
  }

  if (Instruction.PreviousInstruction != nullptr)
    Instruction.PreviousInstruction->NextInstruction = Instruction.NextInstruction;
  else
    FirstInstruction = Instruction.NextInstruction;

  if (Instruction.NextInstruction != nullptr)
    Instruction.NextInstruction->PreviousInstruction = Instruction.PreviousInstruction;
  else
    LastInstruction = Instruction.PreviousInstruction;

  Instruction.detachFromBlock();
  return {};
}

void MachineBasicBlock::clearInstructions() noexcept {
  MachineInstr *Instruction = FirstInstruction;
  while (Instruction != nullptr) {
    MachineInstr *NextInstruction = Instruction->NextInstruction;
    Instruction->detachFromBlock();
    Instruction = NextInstruction;
  }
  FirstInstruction = nullptr;
  LastInstruction = nullptr;
}

Expected<void> MachineBasicBlock::addLiveIn(PhysicalRegister PhysReg, LaneMask Lanes) {
  if (!PhysReg.isValid() || Lanes.empty()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "block live-in requires a physical register and nonempty lanes");
  }

  const auto Position =
      std::ranges::find_if(LiveIns, [PhysReg](const MachineBasicBlockLiveIn &LiveIn) {
        return LiveIn.PhysReg == PhysReg;
      });
  if (Position != LiveIns.end()) {
    Position->Lanes = Position->Lanes | Lanes;
    return {};
  }

  LiveIns.push_back(MachineBasicBlockLiveIn{PhysReg, Lanes});
  return {};
}

Expected<void> MachineBasicBlock::removeLiveIn(PhysicalRegister PhysReg) {
  const auto Position =
      std::ranges::find_if(LiveIns, [PhysReg](const MachineBasicBlockLiveIn &LiveIn) {
        return LiveIn.PhysReg == PhysReg;
      });
  if (Position == LiveIns.end()) {
    return makeUnexpected(ErrorCode::InvalidArgument, "block physical live-in is not present");
  }
  LiveIns.erase(Position);
  return {};
}

} // namespace rcc