blob: 674db3fe15987f4187efb673d5d75e224b4b8d06 [file] [log] [blame]
Clement Courbet0e69e2d2018-05-17 10:52:18 +00001//===-- MCInstrDescView.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Clement Courbet0e69e2d2018-05-17 10:52:18 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "MCInstrDescView.h"
10
11#include <iterator>
12#include <map>
13#include <tuple>
14
15#include "llvm/ADT/STLExtras.h"
16
Fangrui Song32401af2018-10-22 17:10:47 +000017namespace llvm {
Clement Courbet0e69e2d2018-05-17 10:52:18 +000018namespace exegesis {
19
Guillaume Chatelet32d384c2019-12-18 12:08:38 +010020unsigned Variable::getIndex() const { return *Index; }
Guillaume Chateletfe5b0b42018-10-09 10:06:19 +000021
Guillaume Chatelet09c28392018-10-09 08:59:10 +000022unsigned Variable::getPrimaryOperandIndex() const {
23 assert(!TiedOperands.empty());
24 return TiedOperands[0];
25}
26
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +000027bool Variable::hasTiedOperands() const {
28 assert(TiedOperands.size() <= 2 &&
29 "No more than two operands can be tied together");
30 // By definition only Use and Def operands can be tied together.
31 // TiedOperands[0] is the Def operand (LLVM stores defs first).
32 // TiedOperands[1] is the Use operand.
33 return TiedOperands.size() > 1;
34}
Guillaume Chatelet09c28392018-10-09 08:59:10 +000035
Guillaume Chatelet32d384c2019-12-18 12:08:38 +010036unsigned Operand::getIndex() const { return *Index; }
Guillaume Chatelet09c28392018-10-09 08:59:10 +000037
38bool Operand::isExplicit() const { return Info; }
39
40bool Operand::isImplicit() const { return !Info; }
41
42bool Operand::isImplicitReg() const { return ImplicitReg; }
43
44bool Operand::isDef() const { return IsDef; }
45
46bool Operand::isUse() const { return !IsDef; }
47
48bool Operand::isReg() const { return Tracker; }
49
Guillaume Chatelet32d384c2019-12-18 12:08:38 +010050bool Operand::isTied() const { return TiedToIndex.hasValue(); }
Guillaume Chatelet09c28392018-10-09 08:59:10 +000051
Guillaume Chatelet32d384c2019-12-18 12:08:38 +010052bool Operand::isVariable() const { return VariableIndex.hasValue(); }
Guillaume Chatelet09c28392018-10-09 08:59:10 +000053
54bool Operand::isMemory() const {
55 return isExplicit() &&
Clement Courbet50cdd562019-10-09 11:58:42 +000056 getExplicitOperandInfo().OperandType == MCOI::OPERAND_MEMORY;
Guillaume Chatelet09c28392018-10-09 08:59:10 +000057}
58
59bool Operand::isImmediate() const {
60 return isExplicit() &&
Clement Courbet50cdd562019-10-09 11:58:42 +000061 getExplicitOperandInfo().OperandType == MCOI::OPERAND_IMMEDIATE;
Guillaume Chatelet09c28392018-10-09 08:59:10 +000062}
63
Guillaume Chatelet32d384c2019-12-18 12:08:38 +010064unsigned Operand::getTiedToIndex() const { return *TiedToIndex; }
Guillaume Chatelet09c28392018-10-09 08:59:10 +000065
Guillaume Chatelet32d384c2019-12-18 12:08:38 +010066unsigned Operand::getVariableIndex() const { return *VariableIndex; }
Guillaume Chatelet09c28392018-10-09 08:59:10 +000067
68unsigned Operand::getImplicitReg() const {
69 assert(ImplicitReg);
70 return *ImplicitReg;
71}
72
73const RegisterAliasingTracker &Operand::getRegisterAliasing() const {
74 assert(Tracker);
75 return *Tracker;
76}
77
Clement Courbet50cdd562019-10-09 11:58:42 +000078const MCOperandInfo &Operand::getExplicitOperandInfo() const {
Guillaume Chatelet09c28392018-10-09 08:59:10 +000079 assert(Info);
80 return *Info;
81}
82
Guillaume Chatelet32d384c2019-12-18 12:08:38 +010083const BitVector *BitVectorCache::getUnique(BitVector &&BV) const {
84 for (const auto &Entry : Cache)
85 if (*Entry == BV)
86 return Entry.get();
87 Cache.push_back(std::make_unique<BitVector>());
88 auto &Entry = Cache.back();
89 Entry->swap(BV);
90 return Entry.get();
91}
92
93Instruction::Instruction(const MCInstrDesc *Description, StringRef Name,
94 SmallVector<Operand, 8> Operands,
95 SmallVector<Variable, 4> Variables,
96 const BitVector *ImplDefRegs,
97 const BitVector *ImplUseRegs,
98 const BitVector *AllDefRegs,
99 const BitVector *AllUseRegs)
100 : Description(*Description), Name(Name), Operands(std::move(Operands)),
101 Variables(std::move(Variables)), ImplDefRegs(*ImplDefRegs),
102 ImplUseRegs(*ImplUseRegs), AllDefRegs(*AllDefRegs),
103 AllUseRegs(*AllUseRegs) {}
104
105std::unique_ptr<Instruction>
106Instruction::create(const MCInstrInfo &InstrInfo,
107 const RegisterAliasingTrackerCache &RATC,
108 const BitVectorCache &BVC, unsigned Opcode) {
109 const llvm::MCInstrDesc *const Description = &InstrInfo.get(Opcode);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000110 unsigned OpIndex = 0;
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100111 SmallVector<Operand, 8> Operands;
112 SmallVector<Variable, 4> Variables;
Guillaume Chateletee9c2a172018-10-10 14:22:48 +0000113 for (; OpIndex < Description->getNumOperands(); ++OpIndex) {
114 const auto &OpInfo = Description->opInfo_begin()[OpIndex];
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000115 Operand Operand;
116 Operand.Index = OpIndex;
Guillaume Chateletee9c2a172018-10-10 14:22:48 +0000117 Operand.IsDef = (OpIndex < Description->getNumDefs());
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000118 // TODO(gchatelet): Handle isLookupPtrRegClass.
119 if (OpInfo.RegClass >= 0)
120 Operand.Tracker = &RATC.getRegisterClass(OpInfo.RegClass);
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100121 int TiedToIndex = Description->getOperandConstraint(OpIndex, MCOI::TIED_TO);
Simon Pilgrim83d0db52020-02-04 21:23:50 +0000122 assert((TiedToIndex == -1 ||
123 (0 <= TiedToIndex &&
124 TiedToIndex < std::numeric_limits<uint8_t>::max())) &&
125 "Unknown Operand Constraint");
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100126 if (TiedToIndex >= 0)
127 Operand.TiedToIndex = TiedToIndex;
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000128 Operand.Info = &OpInfo;
129 Operands.push_back(Operand);
130 }
Clement Courbet50cdd562019-10-09 11:58:42 +0000131 for (const MCPhysReg *MCPhysReg = Description->getImplicitDefs();
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000132 MCPhysReg && *MCPhysReg; ++MCPhysReg, ++OpIndex) {
133 Operand Operand;
134 Operand.Index = OpIndex;
135 Operand.IsDef = true;
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000136 Operand.Tracker = &RATC.getRegister(*MCPhysReg);
137 Operand.ImplicitReg = MCPhysReg;
138 Operands.push_back(Operand);
139 }
Clement Courbet50cdd562019-10-09 11:58:42 +0000140 for (const MCPhysReg *MCPhysReg = Description->getImplicitUses();
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000141 MCPhysReg && *MCPhysReg; ++MCPhysReg, ++OpIndex) {
142 Operand Operand;
143 Operand.Index = OpIndex;
144 Operand.IsDef = false;
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000145 Operand.Tracker = &RATC.getRegister(*MCPhysReg);
146 Operand.ImplicitReg = MCPhysReg;
147 Operands.push_back(Operand);
148 }
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000149 Variables.reserve(Operands.size()); // Variables.size() <= Operands.size()
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100150 // Assigning Variables to non tied explicit operands.
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000151 for (auto &Op : Operands)
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000152 if (Op.isExplicit() && !Op.isTied()) {
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000153 const size_t VariableIndex = Variables.size();
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100154 assert(VariableIndex < std::numeric_limits<uint8_t>::max());
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000155 Op.VariableIndex = VariableIndex;
156 Variables.emplace_back();
157 Variables.back().Index = VariableIndex;
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000158 }
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000159 // Assigning Variables to tied operands.
160 for (auto &Op : Operands)
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100161 if (Op.isExplicit() && Op.isTied())
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000162 Op.VariableIndex = Operands[Op.getTiedToIndex()].getVariableIndex();
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000163 // Assigning Operands to Variables.
164 for (auto &Op : Operands)
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000165 if (Op.isVariable())
166 Variables[Op.getVariableIndex()].TiedOperands.push_back(Op.getIndex());
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000167 // Processing Aliasing.
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100168 BitVector ImplDefRegs = RATC.emptyRegisters();
169 BitVector ImplUseRegs = RATC.emptyRegisters();
170 BitVector AllDefRegs = RATC.emptyRegisters();
171 BitVector AllUseRegs = RATC.emptyRegisters();
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000172 for (const auto &Op : Operands) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000173 if (Op.isReg()) {
174 const auto &AliasingBits = Op.getRegisterAliasing().aliasedBits();
175 if (Op.isDef())
176 AllDefRegs |= AliasingBits;
177 if (Op.isUse())
178 AllUseRegs |= AliasingBits;
179 if (Op.isDef() && Op.isImplicit())
180 ImplDefRegs |= AliasingBits;
181 if (Op.isUse() && Op.isImplicit())
182 ImplUseRegs |= AliasingBits;
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000183 }
184 }
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100185 // Can't use make_unique because constructor is private.
186 return std::unique_ptr<Instruction>(new Instruction(
187 Description, InstrInfo.getName(Opcode), std::move(Operands),
188 std::move(Variables), BVC.getUnique(std::move(ImplDefRegs)),
189 BVC.getUnique(std::move(ImplUseRegs)),
190 BVC.getUnique(std::move(AllDefRegs)),
191 BVC.getUnique(std::move(AllUseRegs))));
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000192}
193
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000194const Operand &Instruction::getPrimaryOperand(const Variable &Var) const {
195 const auto PrimaryOperandIndex = Var.getPrimaryOperandIndex();
196 assert(PrimaryOperandIndex < Operands.size());
197 return Operands[PrimaryOperandIndex];
198}
199
Guillaume Chateletfb943542018-08-01 14:41:45 +0000200bool Instruction::hasMemoryOperands() const {
Fangrui Song2e83b2e2018-10-19 06:12:02 +0000201 return any_of(Operands, [](const Operand &Op) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000202 return Op.isReg() && Op.isExplicit() && Op.isMemory();
203 });
204}
205
206bool Instruction::hasAliasingImplicitRegisters() const {
207 return ImplDefRegs.anyCommon(ImplUseRegs);
208}
209
Clement Courbet8ef97e12019-09-27 08:04:10 +0000210// Returns true if there are registers that are both in `A` and `B` but not in
211// `Forbidden`.
212static bool anyCommonExcludingForbidden(const BitVector &A, const BitVector &B,
213 const BitVector &Forbidden) {
214 assert(A.size() == B.size() && B.size() == Forbidden.size());
215 const auto Size = A.size();
216 for (int AIndex = A.find_first(); AIndex != -1;) {
217 const int BIndex = B.find_first_in(AIndex, Size);
218 if (BIndex == -1)
219 return false;
220 if (AIndex == BIndex && !Forbidden.test(AIndex))
221 return true;
222 AIndex = A.find_first_in(BIndex + 1, Size);
223 }
224 return false;
225}
226
Guillaume Chatelet0c17cbf2018-10-10 09:12:36 +0000227bool Instruction::hasAliasingRegistersThrough(
Clement Courbet8ef97e12019-09-27 08:04:10 +0000228 const Instruction &OtherInstr, const BitVector &ForbiddenRegisters) const {
229 return anyCommonExcludingForbidden(AllDefRegs, OtherInstr.AllUseRegs,
230 ForbiddenRegisters) &&
231 anyCommonExcludingForbidden(OtherInstr.AllDefRegs, AllUseRegs,
232 ForbiddenRegisters);
Guillaume Chatelet0c17cbf2018-10-10 09:12:36 +0000233}
234
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000235bool Instruction::hasTiedRegisters() const {
Clement Courbet50cdd562019-10-09 11:58:42 +0000236 return any_of(Variables,
237 [](const Variable &Var) { return Var.hasTiedOperands(); });
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000238}
239
Clement Courbet8ef97e12019-09-27 08:04:10 +0000240bool Instruction::hasAliasingRegisters(
241 const BitVector &ForbiddenRegisters) const {
242 return anyCommonExcludingForbidden(AllDefRegs, AllUseRegs,
243 ForbiddenRegisters);
Guillaume Chateletfb943542018-08-01 14:41:45 +0000244}
245
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +0000246bool Instruction::hasOneUseOrOneDef() const {
247 return AllDefRegs.count() || AllUseRegs.count();
248}
249
Clement Courbet50cdd562019-10-09 11:58:42 +0000250void Instruction::dump(const MCRegisterInfo &RegInfo,
Clement Courbet8ef97e12019-09-27 08:04:10 +0000251 const RegisterAliasingTrackerCache &RATC,
Clement Courbet50cdd562019-10-09 11:58:42 +0000252 raw_ostream &Stream) const {
Guillaume Chatelet9b592382018-10-10 14:57:32 +0000253 Stream << "- " << Name << "\n";
Guillaume Chatelet547d2dd2018-10-09 14:51:29 +0000254 for (const auto &Op : Operands) {
255 Stream << "- Op" << Op.getIndex();
256 if (Op.isExplicit())
257 Stream << " Explicit";
258 if (Op.isImplicit())
259 Stream << " Implicit";
260 if (Op.isUse())
261 Stream << " Use";
262 if (Op.isDef())
263 Stream << " Def";
264 if (Op.isImmediate())
265 Stream << " Immediate";
266 if (Op.isMemory())
267 Stream << " Memory";
268 if (Op.isReg()) {
269 if (Op.isImplicitReg())
270 Stream << " Reg(" << RegInfo.getName(Op.getImplicitReg()) << ")";
271 else
272 Stream << " RegClass("
273 << RegInfo.getRegClassName(
274 &RegInfo.getRegClass(Op.Info->RegClass))
275 << ")";
276 }
277 if (Op.isTied())
278 Stream << " TiedToOp" << Op.getTiedToIndex();
279 Stream << "\n";
280 }
281 for (const auto &Var : Variables) {
282 Stream << "- Var" << Var.getIndex();
Guillaume Chatelet9b592382018-10-10 14:57:32 +0000283 Stream << " [";
284 bool IsFirst = true;
285 for (auto OperandIndex : Var.TiedOperands) {
286 if (!IsFirst)
287 Stream << ",";
Guillaume Chatelet0c17cbf2018-10-10 09:12:36 +0000288 Stream << "Op" << OperandIndex;
Guillaume Chatelet9b592382018-10-10 14:57:32 +0000289 IsFirst = false;
290 }
291 Stream << "]";
Guillaume Chatelet547d2dd2018-10-09 14:51:29 +0000292 Stream << "\n";
293 }
294 if (hasMemoryOperands())
295 Stream << "- hasMemoryOperands\n";
296 if (hasAliasingImplicitRegisters())
297 Stream << "- hasAliasingImplicitRegisters (execution is always serial)\n";
298 if (hasTiedRegisters())
299 Stream << "- hasTiedRegisters (execution is always serial)\n";
Clement Courbet8ef97e12019-09-27 08:04:10 +0000300 if (hasAliasingRegisters(RATC.emptyRegisters()))
Guillaume Chatelet547d2dd2018-10-09 14:51:29 +0000301 Stream << "- hasAliasingRegisters\n";
302}
303
Clement Courbet50cdd562019-10-09 11:58:42 +0000304InstructionsCache::InstructionsCache(const MCInstrInfo &InstrInfo,
Guillaume Chateletda11b852018-10-24 11:55:06 +0000305 const RegisterAliasingTrackerCache &RATC)
Jonas Devlieghere09db6e32020-01-13 17:31:07 -0800306 : InstrInfo(InstrInfo), RATC(RATC), BVC() {}
Guillaume Chateletda11b852018-10-24 11:55:06 +0000307
308const Instruction &InstructionsCache::getInstr(unsigned Opcode) const {
309 auto &Found = Instructions[Opcode];
310 if (!Found)
Guillaume Chatelet32d384c2019-12-18 12:08:38 +0100311 Found = Instruction::create(InstrInfo, RATC, BVC, Opcode);
Guillaume Chateletda11b852018-10-24 11:55:06 +0000312 return *Found;
313}
314
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000315bool RegisterOperandAssignment::
316operator==(const RegisterOperandAssignment &Other) const {
317 return std::tie(Op, Reg) == std::tie(Other.Op, Other.Reg);
318}
319
320bool AliasingRegisterOperands::
321operator==(const AliasingRegisterOperands &Other) const {
322 return std::tie(Defs, Uses) == std::tie(Other.Defs, Other.Uses);
323}
324
Clement Courbet50cdd562019-10-09 11:58:42 +0000325static void
326addOperandIfAlias(const MCPhysReg Reg, bool SelectDef,
327 ArrayRef<Operand> Operands,
328 SmallVectorImpl<RegisterOperandAssignment> &OperandValues) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000329 for (const auto &Op : Operands) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000330 if (Op.isReg() && Op.isDef() == SelectDef) {
331 const int SourceReg = Op.getRegisterAliasing().getOrigin(Reg);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000332 if (SourceReg >= 0)
333 OperandValues.emplace_back(&Op, SourceReg);
334 }
335 }
336}
337
338bool AliasingRegisterOperands::hasImplicitAliasing() const {
339 const auto HasImplicit = [](const RegisterOperandAssignment &ROV) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000340 return ROV.Op->isImplicit();
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000341 };
Clement Courbet50cdd562019-10-09 11:58:42 +0000342 return any_of(Defs, HasImplicit) && any_of(Uses, HasImplicit);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000343}
344
345bool AliasingConfigurations::empty() const { return Configurations.empty(); }
346
347bool AliasingConfigurations::hasImplicitAliasing() const {
Clement Courbet50cdd562019-10-09 11:58:42 +0000348 return any_of(Configurations, [](const AliasingRegisterOperands &ARO) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000349 return ARO.hasImplicitAliasing();
350 });
351}
352
353AliasingConfigurations::AliasingConfigurations(
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +0000354 const Instruction &DefInstruction, const Instruction &UseInstruction) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000355 if (UseInstruction.AllUseRegs.anyCommon(DefInstruction.AllDefRegs)) {
356 auto CommonRegisters = UseInstruction.AllUseRegs;
357 CommonRegisters &= DefInstruction.AllDefRegs;
Clement Courbet50cdd562019-10-09 11:58:42 +0000358 for (const MCPhysReg Reg : CommonRegisters.set_bits()) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000359 AliasingRegisterOperands ARO;
360 addOperandIfAlias(Reg, true, DefInstruction.Operands, ARO.Defs);
361 addOperandIfAlias(Reg, false, UseInstruction.Operands, ARO.Uses);
362 if (!ARO.Defs.empty() && !ARO.Uses.empty() &&
Clement Courbet50cdd562019-10-09 11:58:42 +0000363 !is_contained(Configurations, ARO))
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000364 Configurations.push_back(std::move(ARO));
365 }
366 }
367}
368
Clement Courbet50cdd562019-10-09 11:58:42 +0000369void DumpMCOperand(const MCRegisterInfo &MCRegisterInfo, const MCOperand &Op,
370 raw_ostream &OS) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000371 if (!Op.isValid())
372 OS << "Invalid";
373 else if (Op.isReg())
374 OS << MCRegisterInfo.getName(Op.getReg());
375 else if (Op.isImm())
376 OS << Op.getImm();
377 else if (Op.isFPImm())
378 OS << Op.getFPImm();
379 else if (Op.isExpr())
380 OS << "Expr";
381 else if (Op.isInst())
382 OS << "SubInst";
383}
384
Clement Courbet50cdd562019-10-09 11:58:42 +0000385void DumpMCInst(const MCRegisterInfo &MCRegisterInfo,
386 const MCInstrInfo &MCInstrInfo, const MCInst &MCInst,
387 raw_ostream &OS) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000388 OS << MCInstrInfo.getName(MCInst.getOpcode());
389 for (unsigned I = 0, E = MCInst.getNumOperands(); I < E; ++I) {
390 if (I > 0)
391 OS << ',';
392 OS << ' ';
393 DumpMCOperand(MCRegisterInfo, MCInst.getOperand(I), OS);
394 }
395}
396
397} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000398} // namespace llvm