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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineMemOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include <cstddef>
#include <functional>
#include <optional>
#include <utility>
namespace rcc {
MachineInstr::MachineInstr(MachineFunction &OwnerFunction, Opcode InstructionOpcode,
MachineInstrStableID StableID)
: OwnerFunction(&OwnerFunction), InstructionOpcode(InstructionOpcode), StableID(StableID) {}
MachineInstr::~MachineInstr() {
for (MachineOperand &Operand : Operands)
Operand.detachFromInstruction();
}
Expected<void> MachineInstr::setOpcode(Opcode NewOpcode) {
if (!NewOpcode.isValid())
return makeUnexpected(ErrorCode::InvalidArgument, "machine opcode must be nonzero");
InstructionOpcode = NewOpcode;
return {};
}
MachineOperand *MachineInstr::operandAt(std::size_t Index) noexcept {
if (Index >= Operands.size())
return nullptr;
return &Operands[Index];
}
const MachineOperand *MachineInstr::operandAt(std::size_t Index) const noexcept {
if (Index >= Operands.size())
return nullptr;
return &Operands[Index];
}
void MachineInstr::reserveOperands(std::size_t Capacity) {
Operands.reserve(Capacity);
}
Expected<void> MachineInstr::addOperand(MachineOperand &&Operand) {
return insertOperand(Operands.size(), std::move(Operand));
}
Expected<void> MachineInstr::insertOperand(std::size_t Index, MachineOperand &&Operand) {
if (Index > Operands.size())
return makeUnexpected(ErrorCode::InvalidArgument, "operand insertion index is out of range");
if (Operand.Parent != nullptr)
return makeUnexpected(ErrorCode::InvalidArgument, "operand already belongs to an instruction");
if (Operand.TiedOperandIndex)
return makeUnexpected(ErrorCode::InvalidArgument,
"detached operand cannot carry a tied operand index");
if (const auto *BlockOperand = Operand.getIf<BasicBlockOperand>();
BlockOperand != nullptr && &BlockOperand->Block->function() != OwnerFunction) {
return makeUnexpected(ErrorCode::InvalidArgument,
"basic block operand belongs to another machine function");
}
if (const Register *Reg = Operand.getIf<Register>();
Reg != nullptr && Reg->isVirtual() &&
!OwnerFunction->registerInfo().isKnownVirtualRegister(*Reg)) {
return makeUnexpected(ErrorCode::InvalidArgument,
"register operand references an unknown virtual register");
}
Operands.insert(Operands.begin() + static_cast<std::ptrdiff_t>(Index), std::move(Operand));
adjustTiesForInsertion(Index);
Operands[Index].attachTo(*this, linkedRegisterInfo());
return {};
}
Expected<void> MachineInstr::eraseOperand(std::size_t Index) {
if (Index >= Operands.size())
return makeUnexpected(ErrorCode::InvalidArgument, "operand erase index is out of range");
adjustTiesForErasure(Index);
Operands.erase(Operands.begin() + static_cast<std::ptrdiff_t>(Index));
return {};
}
Expected<void> MachineInstr::tieOperands(std::size_t DefIndex, std::size_t UseIndex) {
if (DefIndex >= Operands.size() || UseIndex >= Operands.size() || DefIndex == UseIndex) {
return makeUnexpected(ErrorCode::InvalidArgument, "tied operand indices are invalid");
}
MachineOperand &DefOperand = Operands[DefIndex];
MachineOperand &UseOperand = Operands[UseIndex];
if (!DefOperand.isRegister() || !UseOperand.isRegister() || !DefOperand.isDef() ||
DefOperand.isUse() || !UseOperand.isUse() || UseOperand.isDef()) {
return makeUnexpected(ErrorCode::InvalidArgument,
"tie must connect a pure register definition and use");
}
if (DefOperand.TiedOperandIndex || UseOperand.TiedOperandIndex) {
return makeUnexpected(ErrorCode::InvalidArgument,
"an operand cannot participate in more than one tie");
}
DefOperand.setTiedOperandIndex(UseIndex);
UseOperand.setTiedOperandIndex(DefIndex);
return {};
}
Expected<void> MachineInstr::untieOperand(std::size_t Index) {
if (Index >= Operands.size())
return makeUnexpected(ErrorCode::InvalidArgument, "operand tie index is out of range");
const auto OtherIndex = Operands[Index].TiedOperandIndex;
if (!OtherIndex)
return makeUnexpected(ErrorCode::InvalidArgument, "operand is not tied");
if (*OtherIndex >= Operands.size() || Operands[*OtherIndex].TiedOperandIndex != Index) {
return makeUnexpected(ErrorCode::VerificationError, "operand tie relation is not reciprocal");
}
Operands[*OtherIndex].setTiedOperandIndex(std::nullopt);
Operands[Index].setTiedOperandIndex(std::nullopt);
return {};
}
Expected<void> MachineInstr::addMemOperand(const MachineMemOperand &MemOperand) {
if (!OwnerFunction->ownsMemOperand(MemOperand)) {
return makeUnexpected(ErrorCode::InvalidArgument,
"machine memory operand belongs to another function");
}
MemOperands.push_back(&MemOperand);
return {};
}
Expected<void> MachineInstr::eraseMemOperand(std::size_t Index) {
if (Index >= MemOperands.size()) {
return makeUnexpected(ErrorCode::InvalidArgument,
"machine memory operand index is out of range");
}
MemOperands.erase(MemOperands.begin() + static_cast<std::ptrdiff_t>(Index));
return {};
}
MachineRegisterInfo *MachineInstr::linkedRegisterInfo() noexcept {
if (!isLinked())
return nullptr;
return &OwnerFunction->registerInfo();
}
Expected<void> MachineInstr::attachToBlock(MachineBasicBlock &Block) {
if (ParentBlock != nullptr) {
return makeUnexpected(ErrorCode::InvalidArgument,
"instruction is already linked to a basic block");
}
MachineRegisterInfo &RegInfo = OwnerFunction->registerInfo();
for (const MachineOperand &Operand : Operands) {
const Register *Reg = Operand.getIf<Register>();
if (Reg != nullptr && Reg->isVirtual() && !RegInfo.isKnownVirtualRegister(*Reg)) {
return makeUnexpected(ErrorCode::InvalidArgument,
"instruction references an unknown virtual register");
}
}
ParentBlock = &Block;
for (MachineOperand &Operand : Operands) {
const Register *Reg = Operand.getIf<Register>();
if (Reg != nullptr && !Reg->isNoRegister())
Operand.linkToRegInfo(RegInfo);
}
return {};
}
void MachineInstr::detachFromBlock() noexcept {
for (MachineOperand &Operand : Operands)
Operand.unlinkFromRegInfo();
ParentBlock = nullptr;
PreviousInstruction = nullptr;
NextInstruction = nullptr;
}
void MachineInstr::adjustTiesForInsertion(std::size_t Index) noexcept {
for (MachineOperand &Operand : Operands) {
if (Operand.TiedOperandIndex && *Operand.TiedOperandIndex >= Index)
++*Operand.TiedOperandIndex;
}
}
void MachineInstr::adjustTiesForErasure(std::size_t Index) noexcept {
for (MachineOperand &Operand : Operands) {
if (!Operand.TiedOperandIndex)
continue;
if (*Operand.TiedOperandIndex == Index)
Operand.TiedOperandIndex.reset();
else if (*Operand.TiedOperandIndex > Index)
--*Operand.TiedOperandIndex;
}
}
} // namespace rcc
|