aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/MachineBasicBlock.h
blob: d82477a8d4e8d35c2ced6c33f4a4fe812b45f388 (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
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_MACHINEBASICBLOCK_H
#define RCC_CODEGEN_MACHINEBASICBLOCK_H

#include "rcc/CodeGen/MachineBasicBlockAttributes.h"
#include "rcc/CodeGen/MachineIRTypes.h"
#include "rcc/CodeGen/Register.h"
#include "rcc/CodeGen/StableID.h"
#include "rcc/Support/Error.h"

#include <cstddef>
#include <cstdint>
#include <optional>
#include <span>
#include <vector>

namespace rcc {

class MachineBasicBlock;
class MachineCFGUpdater;
class MachineFunction;
class MachineInstr;

struct MachineSuccessor final {
  MachineBasicBlock *Block = nullptr;
  BranchProbability Probability;

  friend bool operator==(const MachineSuccessor &Left, const MachineSuccessor &Right) = default;
};

struct MachineBasicBlockLiveIn final {
  PhysicalRegister PhysReg;
  LaneMask Lanes = LaneMask::all();

  friend bool operator==(const MachineBasicBlockLiveIn &Left,
                         const MachineBasicBlockLiveIn &Right) = default;
};

class MachineBasicBlock final {
public:
  MachineBasicBlock(const MachineBasicBlock &) = delete;
  MachineBasicBlock &operator=(const MachineBasicBlock &) = delete;
  MachineBasicBlock(MachineBasicBlock &&) = delete;
  MachineBasicBlock &operator=(MachineBasicBlock &&) = delete;
  ~MachineBasicBlock() = default;

  [[nodiscard]] MachineBlockStableID stableID() const noexcept {
    return StableID;
  }

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

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

  [[nodiscard]] bool isLinked() const noexcept {
    return IsLinked;
  }

  [[nodiscard]] MachineBasicBlock *previous() noexcept {
    return PreviousBlock;
  }

  [[nodiscard]] const MachineBasicBlock *previous() const noexcept {
    return PreviousBlock;
  }

  [[nodiscard]] MachineBasicBlock *next() noexcept {
    return NextBlock;
  }

  [[nodiscard]] const MachineBasicBlock *next() const noexcept {
    return NextBlock;
  }

  [[nodiscard]] MachineInstr *firstInstruction() noexcept {
    return FirstInstruction;
  }

  [[nodiscard]] const MachineInstr *firstInstruction() const noexcept {
    return FirstInstruction;
  }

  [[nodiscard]] MachineInstr *lastInstruction() noexcept {
    return LastInstruction;
  }

  [[nodiscard]] const MachineInstr *lastInstruction() const noexcept {
    return LastInstruction;
  }

  [[nodiscard]] std::size_t instructionCount() const noexcept;
  [[nodiscard]] Expected<void> appendInstruction(MachineInstr &Instruction);
  [[nodiscard]] Expected<void> insertInstructionBefore(MachineInstr &Position,
                                                       MachineInstr &Instruction);
  [[nodiscard]] Expected<void> insertInstructionAfter(MachineInstr &Position,
                                                      MachineInstr &Instruction);
  [[nodiscard]] Expected<void> removeInstruction(MachineInstr &Instruction);
  void clearInstructions() noexcept;

  [[nodiscard]] std::span<MachineBasicBlock *const> predecessors() const noexcept {
    return Predecessors;
  }

  [[nodiscard]] std::span<const MachineSuccessor> successors() const noexcept {
    return Successors;
  }

  [[nodiscard]] Expected<void> addLiveIn(PhysicalRegister PhysReg,
                                         LaneMask Lanes = LaneMask::all());
  [[nodiscard]] Expected<void> removeLiveIn(PhysicalRegister PhysReg);
  [[nodiscard]] std::span<const MachineBasicBlockLiveIn> liveIns() const noexcept {
    return LiveIns;
  }

  [[nodiscard]] std::optional<std::uint32_t> denseNumber() const noexcept {
    return DenseNumber;
  }

  [[nodiscard]] std::optional<std::uint32_t> layoutIndex() const noexcept {
    return LayoutIndex;
  }

  [[nodiscard]] MachineBasicBlockAttributes &attributes() noexcept {
    return Attributes;
  }

  [[nodiscard]] const MachineBasicBlockAttributes &attributes() const noexcept {
    return Attributes;
  }

private:
  MachineBasicBlock(MachineFunction &OwnerFunction, MachineBlockStableID StableID)
      : OwnerFunction(&OwnerFunction), StableID(StableID) {}

  MachineFunction *OwnerFunction;
  MachineBlockStableID StableID;
  bool IsLinked = false;
  MachineBasicBlock *PreviousBlock = nullptr;
  MachineBasicBlock *NextBlock = nullptr;
  MachineInstr *FirstInstruction = nullptr;
  MachineInstr *LastInstruction = nullptr;
  std::vector<MachineBasicBlock *> Predecessors;
  std::vector<MachineSuccessor> Successors;
  std::vector<MachineBasicBlockLiveIn> LiveIns;
  MachineBasicBlockAttributes Attributes;
  std::optional<std::uint32_t> DenseNumber;
  std::optional<std::uint32_t> LayoutIndex;

  friend class MachineArena;
  friend class MachineCFGUpdater;
  friend class MachineFunction;
};

} // namespace rcc

#endif // RCC_CODEGEN_MACHINEBASICBLOCK_H