blob: c57696979d5d9b7ff49a2a8c7108c80d4b45d16f [file] [log] [blame]
Clement Courbet0e69e2d2018-05-17 10:52:18 +00001//===-- MCInstrDescView.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "MCInstrDescView.h"
11
12#include <iterator>
13#include <map>
14#include <tuple>
15
16#include "llvm/ADT/STLExtras.h"
17
18namespace exegesis {
19
Clement Courbet0e69e2d2018-05-17 10:52:18 +000020Instruction::Instruction(const llvm::MCInstrDesc &MCInstrDesc,
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000021 const RegisterAliasingTrackerCache &RATC)
Clement Courbet0e69e2d2018-05-17 10:52:18 +000022 : Description(MCInstrDesc) {
23 unsigned OpIndex = 0;
24 for (; OpIndex < MCInstrDesc.getNumOperands(); ++OpIndex) {
25 const auto &OpInfo = MCInstrDesc.opInfo_begin()[OpIndex];
26 Operand Operand;
27 Operand.Index = OpIndex;
28 Operand.IsDef = (OpIndex < MCInstrDesc.getNumDefs());
29 Operand.IsExplicit = true;
30 // TODO(gchatelet): Handle isLookupPtrRegClass.
31 if (OpInfo.RegClass >= 0)
32 Operand.Tracker = &RATC.getRegisterClass(OpInfo.RegClass);
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000033 Operand.TiedToIndex =
34 MCInstrDesc.getOperandConstraint(OpIndex, llvm::MCOI::TIED_TO);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000035 Operand.Info = &OpInfo;
36 Operands.push_back(Operand);
37 }
38 for (const llvm::MCPhysReg *MCPhysReg = MCInstrDesc.getImplicitDefs();
39 MCPhysReg && *MCPhysReg; ++MCPhysReg, ++OpIndex) {
40 Operand Operand;
41 Operand.Index = OpIndex;
42 Operand.IsDef = true;
43 Operand.IsExplicit = false;
44 Operand.Tracker = &RATC.getRegister(*MCPhysReg);
45 Operand.ImplicitReg = MCPhysReg;
46 Operands.push_back(Operand);
47 }
48 for (const llvm::MCPhysReg *MCPhysReg = MCInstrDesc.getImplicitUses();
49 MCPhysReg && *MCPhysReg; ++MCPhysReg, ++OpIndex) {
50 Operand Operand;
51 Operand.Index = OpIndex;
52 Operand.IsDef = false;
53 Operand.IsExplicit = false;
54 Operand.Tracker = &RATC.getRegister(*MCPhysReg);
55 Operand.ImplicitReg = MCPhysReg;
56 Operands.push_back(Operand);
57 }
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000058 // Assigning Variables to non tied explicit operands.
59 Variables.reserve(Operands.size()); // Variables.size() <= Operands.size()
60 for (auto &Op : Operands)
61 if (Op.IsExplicit && Op.TiedToIndex < 0) {
62 const size_t VariableIndex = Variables.size();
63 Op.VariableIndex = VariableIndex;
64 Variables.emplace_back();
65 Variables.back().Index = VariableIndex;
Clement Courbet0e69e2d2018-05-17 10:52:18 +000066 }
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000067 // Assigning Variables to tied operands.
68 for (auto &Op : Operands)
69 if (Op.TiedToIndex >= 0)
70 Op.VariableIndex = Operands[Op.TiedToIndex].VariableIndex;
71 // Assigning Operands to Variables.
72 for (auto &Op : Operands)
73 if (Op.VariableIndex >= 0)
74 Variables[Op.VariableIndex].TiedOperands.push_back(&Op);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000075 // Processing Aliasing.
76 DefRegisters = RATC.emptyRegisters();
77 UseRegisters = RATC.emptyRegisters();
78 for (const auto &Op : Operands) {
79 if (Op.Tracker) {
80 auto &Registers = Op.IsDef ? DefRegisters : UseRegisters;
81 Registers |= Op.Tracker->aliasedBits();
82 }
83 }
84}
85
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000086InstructionInstance::InstructionInstance(const Instruction &Instr)
87 : Instr(Instr), VariableValues(Instr.Variables.size()) {}
88
89llvm::MCOperand &InstructionInstance::getValueFor(const Variable &Var) {
90 return VariableValues[Var.Index];
91}
92
93llvm::MCOperand &InstructionInstance::getValueFor(const Operand &Op) {
94 assert(Op.VariableIndex >= 0);
95 return getValueFor(Instr.Variables[Op.VariableIndex]);
96}
97
98// forward declaration.
99static void randomize(const Variable &Var, llvm::MCOperand &AssignedValue);
100
101llvm::MCInst InstructionInstance::randomizeUnsetVariablesAndBuild() {
102 for (const Variable &Var : Instr.Variables) {
103 llvm::MCOperand &AssignedValue = getValueFor(Var);
104 if (!AssignedValue.isValid())
105 randomize(Var, AssignedValue);
106 }
107 llvm::MCInst Result;
108 Result.setOpcode(Instr.Description.Opcode);
109 for (const auto &Op : Instr.Operands)
110 if (Op.IsExplicit)
111 Result.addOperand(getValueFor(Op));
112 return Result;
113}
114
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000115bool RegisterOperandAssignment::
116operator==(const RegisterOperandAssignment &Other) const {
117 return std::tie(Op, Reg) == std::tie(Other.Op, Other.Reg);
118}
119
120bool AliasingRegisterOperands::
121operator==(const AliasingRegisterOperands &Other) const {
122 return std::tie(Defs, Uses) == std::tie(Other.Defs, Other.Uses);
123}
124
125static void addOperandIfAlias(
126 const llvm::MCPhysReg Reg, bool SelectDef, llvm::ArrayRef<Operand> Operands,
127 llvm::SmallVectorImpl<RegisterOperandAssignment> &OperandValues) {
128 for (const auto &Op : Operands) {
129 if (Op.Tracker && Op.IsDef == SelectDef) {
130 const int SourceReg = Op.Tracker->getOrigin(Reg);
131 if (SourceReg >= 0)
132 OperandValues.emplace_back(&Op, SourceReg);
133 }
134 }
135}
136
137bool AliasingRegisterOperands::hasImplicitAliasing() const {
138 const auto HasImplicit = [](const RegisterOperandAssignment &ROV) {
139 return !ROV.Op->IsExplicit;
140 };
141 return llvm::any_of(Defs, HasImplicit) && llvm::any_of(Uses, HasImplicit);
142}
143
144bool AliasingConfigurations::empty() const { return Configurations.empty(); }
145
146bool AliasingConfigurations::hasImplicitAliasing() const {
147 return llvm::any_of(Configurations, [](const AliasingRegisterOperands &ARO) {
148 return ARO.hasImplicitAliasing();
149 });
150}
151
152AliasingConfigurations::AliasingConfigurations(
153 const Instruction &DefInstruction, const Instruction &UseInstruction)
154 : DefInstruction(DefInstruction), UseInstruction(UseInstruction) {
155 if (UseInstruction.UseRegisters.anyCommon(DefInstruction.DefRegisters)) {
156 auto CommonRegisters = UseInstruction.UseRegisters;
157 CommonRegisters &= DefInstruction.DefRegisters;
158 for (const llvm::MCPhysReg Reg : CommonRegisters.set_bits()) {
159 AliasingRegisterOperands ARO;
160 addOperandIfAlias(Reg, true, DefInstruction.Operands, ARO.Defs);
161 addOperandIfAlias(Reg, false, UseInstruction.Operands, ARO.Uses);
162 if (!ARO.Defs.empty() && !ARO.Uses.empty() &&
163 !llvm::is_contained(Configurations, ARO))
164 Configurations.push_back(std::move(ARO));
165 }
166 }
167}
168
169std::mt19937 &randomGenerator() {
170 static std::random_device RandomDevice;
171 static std::mt19937 RandomGenerator(RandomDevice());
172 return RandomGenerator;
173}
174
175static size_t randomIndex(size_t Size) {
176 assert(Size > 0);
177 std::uniform_int_distribution<> Distribution(0, Size - 1);
178 return Distribution(randomGenerator());
179}
180
181template <typename C>
182static auto randomElement(const C &Container) -> decltype(Container[0]) {
183 return Container[randomIndex(Container.size())];
184}
185
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000186static void randomize(const Variable &Var, llvm::MCOperand &AssignedValue) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000187 assert(!Var.TiedOperands.empty());
188 assert(Var.TiedOperands.front() != nullptr);
189 const Operand &Op = *Var.TiedOperands.front();
190 assert(Op.Info != nullptr);
191 const auto &OpInfo = *Op.Info;
192 switch (OpInfo.OperandType) {
193 case llvm::MCOI::OperandType::OPERAND_IMMEDIATE:
194 // FIXME: explore immediate values too.
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000195 AssignedValue = llvm::MCOperand::createImm(1);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000196 break;
197 case llvm::MCOI::OperandType::OPERAND_REGISTER: {
198 assert(Op.Tracker);
199 const auto &Registers = Op.Tracker->sourceBits();
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000200 AssignedValue = llvm::MCOperand::createReg(randomBit(Registers));
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000201 break;
202 }
203 default:
204 break;
205 }
206}
207
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000208static void setRegisterOperandValue(const RegisterOperandAssignment &ROV,
209 InstructionInstance &II) {
210 assert(ROV.Op);
211 assert(ROV.Op->IsExplicit);
212 auto &AssignedValue = II.getValueFor(*ROV.Op);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000213 if (AssignedValue.isValid()) {
214 assert(AssignedValue.isReg() && AssignedValue.getReg() == ROV.Reg);
215 return;
216 }
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000217 AssignedValue = llvm::MCOperand::createReg(ROV.Reg);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000218}
219
220size_t randomBit(const llvm::BitVector &Vector) {
221 assert(Vector.any());
222 auto Itr = Vector.set_bits_begin();
223 for (size_t I = randomIndex(Vector.count()); I != 0; --I)
224 ++Itr;
225 return *Itr;
226}
227
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000228void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
229 InstructionInstance &DefII, InstructionInstance &UseII) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000230 assert(!AliasingConfigurations.empty());
231 assert(!AliasingConfigurations.hasImplicitAliasing());
232 const auto &RandomConf = randomElement(AliasingConfigurations.Configurations);
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000233 setRegisterOperandValue(randomElement(RandomConf.Defs), DefII);
234 setRegisterOperandValue(randomElement(RandomConf.Uses), UseII);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000235}
236
237void DumpMCOperand(const llvm::MCRegisterInfo &MCRegisterInfo,
238 const llvm::MCOperand &Op, llvm::raw_ostream &OS) {
239 if (!Op.isValid())
240 OS << "Invalid";
241 else if (Op.isReg())
242 OS << MCRegisterInfo.getName(Op.getReg());
243 else if (Op.isImm())
244 OS << Op.getImm();
245 else if (Op.isFPImm())
246 OS << Op.getFPImm();
247 else if (Op.isExpr())
248 OS << "Expr";
249 else if (Op.isInst())
250 OS << "SubInst";
251}
252
253void DumpMCInst(const llvm::MCRegisterInfo &MCRegisterInfo,
254 const llvm::MCInstrInfo &MCInstrInfo,
255 const llvm::MCInst &MCInst, llvm::raw_ostream &OS) {
256 OS << MCInstrInfo.getName(MCInst.getOpcode());
257 for (unsigned I = 0, E = MCInst.getNumOperands(); I < E; ++I) {
258 if (I > 0)
259 OS << ',';
260 OS << ' ';
261 DumpMCOperand(MCRegisterInfo, MCInst.getOperand(I), OS);
262 }
263}
264
265} // namespace exegesis