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
|
//===-- MCInstrDescView.cpp -------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "MCInstrDescView.h"
#include <iterator>
#include <map>
#include <tuple>
#include "llvm/ADT/STLExtras.h"
namespace llvm {
namespace exegesis {
unsigned Variable::getIndex() const { return *Index; }
unsigned Variable::getPrimaryOperandIndex() const {
assert(!TiedOperands.empty());
return TiedOperands[0];
}
bool Variable::hasTiedOperands() const {
assert(TiedOperands.size() <= 2 &&
"No more than two operands can be tied together");
// By definition only Use and Def operands can be tied together.
// TiedOperands[0] is the Def operand (LLVM stores defs first).
// TiedOperands[1] is the Use operand.
return TiedOperands.size() > 1;
}
unsigned Operand::getIndex() const { return *Index; }
bool Operand::isExplicit() const { return Info; }
bool Operand::isImplicit() const { return !Info; }
bool Operand::isImplicitReg() const { return ImplicitReg; }
bool Operand::isDef() const { return IsDef; }
bool Operand::isUse() const { return !IsDef; }
bool Operand::isReg() const { return Tracker; }
bool Operand::isTied() const { return TiedToIndex.has_value(); }
bool Operand::isVariable() const { return VariableIndex.has_value(); }
bool Operand::isMemory() const {
return isExplicit() &&
getExplicitOperandInfo().OperandType == MCOI::OPERAND_MEMORY;
}
bool Operand::isImmediate() const {
return isExplicit() &&
getExplicitOperandInfo().OperandType == MCOI::OPERAND_IMMEDIATE;
}
unsigned Operand::getTiedToIndex() const { return *TiedToIndex; }
unsigned Operand::getVariableIndex() const { return *VariableIndex; }
unsigned Operand::getImplicitReg() const {
assert(ImplicitReg);
return ImplicitReg;
}
const RegisterAliasingTracker &Operand::getRegisterAliasing() const {
assert(Tracker);
return *Tracker;
}
const MCOperandInfo &Operand::getExplicitOperandInfo() const {
assert(Info);
return *Info;
}
const BitVector *BitVectorCache::getUnique(BitVector &&BV) const {
for (const auto &Entry : Cache)
if (*Entry == BV)
return Entry.get();
Cache.push_back(std::make_unique<BitVector>());
auto &Entry = Cache.back();
Entry->swap(BV);
return Entry.get();
}
Instruction::Instruction(const MCInstrDesc *Description, StringRef Name,
SmallVector<Operand, 8> Operands,
SmallVector<Variable, 4> Variables,
const BitVector *ImplDefRegs,
const BitVector *ImplUseRegs,
const BitVector *AllDefRegs,
const BitVector *AllUseRegs)
: Description(*Description), Name(Name), Operands(std::move(Operands)),
Variables(std::move(Variables)), ImplDefRegs(*ImplDefRegs),
ImplUseRegs(*ImplUseRegs), AllDefRegs(*AllDefRegs),
AllUseRegs(*AllUseRegs) {}
std::unique_ptr<Instruction>
Instruction::create(const MCInstrInfo &InstrInfo,
const RegisterAliasingTrackerCache &RATC,
const BitVectorCache &BVC, unsigned Opcode) {
const llvm::MCInstrDesc *const Description = &InstrInfo.get(Opcode);
unsigned OpIndex = 0;
SmallVector<Operand, 8> Operands;
SmallVector<Variable, 4> Variables;
for (; OpIndex < Description->getNumOperands(); ++OpIndex) {
const auto &OpInfo = Description->operands()[OpIndex];
Operand Operand;
Operand.Index = OpIndex;
Operand.IsDef = (OpIndex < Description->getNumDefs());
// TODO(gchatelet): Handle isLookupPtrRegClass.
if (OpInfo.RegClass >= 0)
Operand.Tracker = &RATC.getRegisterClass(OpInfo.RegClass);
int TiedToIndex = Description->getOperandConstraint(OpIndex, MCOI::TIED_TO);
assert((TiedToIndex == -1 ||
(0 <= TiedToIndex &&
TiedToIndex < std::numeric_limits<uint8_t>::max())) &&
"Unknown Operand Constraint");
if (TiedToIndex >= 0)
Operand.TiedToIndex = TiedToIndex;
Operand.Info = &OpInfo;
Operands.push_back(Operand);
}
for (MCPhysReg MCPhysReg : Description->implicit_defs()) {
Operand Operand;
Operand.Index = OpIndex++;
Operand.IsDef = true;
Operand.Tracker = &RATC.getRegister(MCPhysReg);
Operand.ImplicitReg = MCPhysReg;
Operands.push_back(Operand);
}
for (MCPhysReg MCPhysReg : Description->implicit_uses()) {
Operand Operand;
Operand.Index = OpIndex++;
Operand.IsDef = false;
Operand.Tracker = &RATC.getRegister(MCPhysReg);
Operand.ImplicitReg = MCPhysReg;
Operands.push_back(Operand);
}
Variables.reserve(Operands.size()); // Variables.size() <= Operands.size()
// Assigning Variables to non tied explicit operands.
for (auto &Op : Operands)
if (Op.isExplicit() && !Op.isTied()) {
const size_t VariableIndex = Variables.size();
assert(VariableIndex < std::numeric_limits<uint8_t>::max());
Op.VariableIndex = VariableIndex;
Variables.emplace_back();
Variables.back().Index = VariableIndex;
}
// Assigning Variables to tied operands.
for (auto &Op : Operands)
if (Op.isExplicit() && Op.isTied())
Op.VariableIndex = Operands[Op.getTiedToIndex()].getVariableIndex();
// Assigning Operands to Variables.
for (auto &Op : Operands)
if (Op.isVariable())
Variables[Op.getVariableIndex()].TiedOperands.push_back(Op.getIndex());
// Processing Aliasing.
BitVector ImplDefRegs = RATC.emptyRegisters();
BitVector ImplUseRegs = RATC.emptyRegisters();
BitVector AllDefRegs = RATC.emptyRegisters();
BitVector AllUseRegs = RATC.emptyRegisters();
for (const auto &Op : Operands) {
if (Op.isReg()) {
const auto &AliasingBits = Op.getRegisterAliasing().aliasedBits();
if (Op.isDef())
AllDefRegs |= AliasingBits;
if (Op.isUse())
AllUseRegs |= AliasingBits;
if (Op.isDef() && Op.isImplicit())
ImplDefRegs |= AliasingBits;
if (Op.isUse() && Op.isImplicit())
ImplUseRegs |= AliasingBits;
}
}
// Can't use make_unique because constructor is private.
return std::unique_ptr<Instruction>(new Instruction(
Description, InstrInfo.getName(Opcode), std::move(Operands),
std::move(Variables), BVC.getUnique(std::move(ImplDefRegs)),
BVC.getUnique(std::move(ImplUseRegs)),
BVC.getUnique(std::move(AllDefRegs)),
BVC.getUnique(std::move(AllUseRegs))));
}
const Operand &Instruction::getPrimaryOperand(const Variable &Var) const {
const auto PrimaryOperandIndex = Var.getPrimaryOperandIndex();
assert(PrimaryOperandIndex < Operands.size());
return Operands[PrimaryOperandIndex];
}
bool Instruction::hasMemoryOperands() const {
return any_of(Operands, [](const Operand &Op) {
return Op.isReg() && Op.isExplicit() && Op.isMemory();
});
}
bool Instruction::hasAliasingImplicitRegisters() const {
return ImplDefRegs.anyCommon(ImplUseRegs);
}
// Returns true if there are registers that are both in `A` and `B` but not in
// `Forbidden`.
static bool anyCommonExcludingForbidden(const BitVector &A, const BitVector &B,
const BitVector &Forbidden) {
assert(A.size() == B.size() && B.size() == Forbidden.size());
const auto Size = A.size();
for (int AIndex = A.find_first(); AIndex != -1;) {
const int BIndex = B.find_first_in(AIndex, Size);
if (BIndex == -1)
return false;
if (AIndex == BIndex && !Forbidden.test(AIndex))
return true;
AIndex = A.find_first_in(BIndex + 1, Size);
}
return false;
}
bool Instruction::hasAliasingRegistersThrough(
const Instruction &OtherInstr, const BitVector &ForbiddenRegisters) const {
return anyCommonExcludingForbidden(AllDefRegs, OtherInstr.AllUseRegs,
ForbiddenRegisters) &&
anyCommonExcludingForbidden(OtherInstr.AllDefRegs, AllUseRegs,
ForbiddenRegisters);
}
bool Instruction::hasTiedRegisters() const {
return any_of(Variables,
[](const Variable &Var) { return Var.hasTiedOperands(); });
}
bool Instruction::hasAliasingRegisters(
const BitVector &ForbiddenRegisters) const {
return anyCommonExcludingForbidden(AllDefRegs, AllUseRegs,
ForbiddenRegisters);
}
bool Instruction::hasOneUseOrOneDef() const {
return AllDefRegs.count() || AllUseRegs.count();
}
void Instruction::dump(const MCRegisterInfo &RegInfo,
const RegisterAliasingTrackerCache &RATC,
raw_ostream &Stream) const {
Stream << "- " << Name << "\n";
for (const auto &Op : Operands) {
Stream << "- Op" << Op.getIndex();
if (Op.isExplicit())
Stream << " Explicit";
if (Op.isImplicit())
Stream << " Implicit";
if (Op.isUse())
Stream << " Use";
if (Op.isDef())
Stream << " Def";
if (Op.isImmediate())
Stream << " Immediate";
if (Op.isMemory())
Stream << " Memory";
if (Op.isReg()) {
if (Op.isImplicitReg())
Stream << " Reg(" << RegInfo.getName(Op.getImplicitReg()) << ")";
else
Stream << " RegClass("
<< RegInfo.getRegClassName(
&RegInfo.getRegClass(Op.Info->RegClass))
<< ")";
}
if (Op.isTied())
Stream << " TiedToOp" << Op.getTiedToIndex();
Stream << "\n";
}
for (const auto &Var : Variables) {
Stream << "- Var" << Var.getIndex();
Stream << " [";
bool IsFirst = true;
for (auto OperandIndex : Var.TiedOperands) {
if (!IsFirst)
Stream << ",";
Stream << "Op" << OperandIndex;
IsFirst = false;
}
Stream << "]";
Stream << "\n";
}
if (hasMemoryOperands())
Stream << "- hasMemoryOperands\n";
if (hasAliasingImplicitRegisters())
Stream << "- hasAliasingImplicitRegisters (execution is always serial)\n";
if (hasTiedRegisters())
Stream << "- hasTiedRegisters (execution is always serial)\n";
if (hasAliasingRegisters(RATC.emptyRegisters()))
Stream << "- hasAliasingRegisters\n";
}
InstructionsCache::InstructionsCache(const MCInstrInfo &InstrInfo,
const RegisterAliasingTrackerCache &RATC)
: InstrInfo(InstrInfo), RATC(RATC), BVC() {}
const Instruction &InstructionsCache::getInstr(unsigned Opcode) const {
auto &Found = Instructions[Opcode];
if (!Found)
Found = Instruction::create(InstrInfo, RATC, BVC, Opcode);
return *Found;
}
bool RegisterOperandAssignment::
operator==(const RegisterOperandAssignment &Other) const {
return std::tie(Op, Reg) == std::tie(Other.Op, Other.Reg);
}
bool AliasingRegisterOperands::
operator==(const AliasingRegisterOperands &Other) const {
return std::tie(Defs, Uses) == std::tie(Other.Defs, Other.Uses);
}
static void
addOperandIfAlias(const MCPhysReg Reg, bool SelectDef,
ArrayRef<Operand> Operands,
SmallVectorImpl<RegisterOperandAssignment> &OperandValues) {
for (const auto &Op : Operands) {
if (Op.isReg() && Op.isDef() == SelectDef) {
const int SourceReg = Op.getRegisterAliasing().getOrigin(Reg);
if (SourceReg >= 0)
OperandValues.emplace_back(&Op, SourceReg);
}
}
}
bool AliasingRegisterOperands::hasImplicitAliasing() const {
const auto HasImplicit = [](const RegisterOperandAssignment &ROV) {
return ROV.Op->isImplicit();
};
return any_of(Defs, HasImplicit) && any_of(Uses, HasImplicit);
}
bool AliasingConfigurations::empty() const { return Configurations.empty(); }
bool AliasingConfigurations::hasImplicitAliasing() const {
return any_of(Configurations, [](const AliasingRegisterOperands &ARO) {
return ARO.hasImplicitAliasing();
});
}
AliasingConfigurations::AliasingConfigurations(
const Instruction &DefInstruction, const Instruction &UseInstruction,
const BitVector &ForbiddenRegisters) {
auto CommonRegisters = UseInstruction.AllUseRegs;
CommonRegisters &= DefInstruction.AllDefRegs;
CommonRegisters.reset(ForbiddenRegisters);
if (!CommonRegisters.empty()) {
for (const MCPhysReg Reg : CommonRegisters.set_bits()) {
AliasingRegisterOperands ARO;
addOperandIfAlias(Reg, true, DefInstruction.Operands, ARO.Defs);
addOperandIfAlias(Reg, false, UseInstruction.Operands, ARO.Uses);
if (!ARO.Defs.empty() && !ARO.Uses.empty() &&
!is_contained(Configurations, ARO))
Configurations.push_back(std::move(ARO));
}
}
}
void DumpMCOperand(const MCRegisterInfo &MCRegisterInfo, const MCOperand &Op,
raw_ostream &OS) {
if (!Op.isValid())
OS << "Invalid";
else if (Op.isReg())
OS << MCRegisterInfo.getName(Op.getReg());
else if (Op.isImm())
OS << Op.getImm();
else if (Op.isDFPImm())
OS << bit_cast<double>(Op.getDFPImm());
else if (Op.isSFPImm())
OS << bit_cast<float>(Op.getSFPImm());
else if (Op.isExpr())
OS << "Expr";
else if (Op.isInst())
OS << "SubInst";
}
void DumpMCInst(const MCRegisterInfo &MCRegisterInfo,
const MCInstrInfo &MCInstrInfo, const MCInst &MCInst,
raw_ostream &OS) {
OS << MCInstrInfo.getName(MCInst.getOpcode());
for (unsigned I = 0, E = MCInst.getNumOperands(); I < E; ++I) {
if (I > 0)
OS << ',';
OS << ' ';
DumpMCOperand(MCRegisterInfo, MCInst.getOperand(I), OS);
}
}
} // namespace exegesis
} // namespace llvm
|