1//===----------------------------------------------------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
9#include "InstructionEncoding.h"
10#include "CodeGenInstruction.h"
11#include "VarLenCodeEmitterGen.h"
12#include "llvm/Support/FormatVariadic.h"
13#include "llvm/TableGen/Error.h"
14
15using namespace llvm;
16
17std::pair<std::string, bool>
18InstructionEncoding::findOperandDecoderMethod(const Record *Record) {
19 std::string Decoder;
20
21 const RecordVal *DecoderString = Record->getValue(Name: "DecoderMethod");
22 const StringInit *String =
23 DecoderString ? dyn_cast<StringInit>(Val: DecoderString->getValue()) : nullptr;
24 if (String) {
25 Decoder = String->getValue().str();
26 if (!Decoder.empty())
27 return {Decoder, false};
28 }
29
30 if (Record->isSubClassOf(Name: "RegisterOperand"))
31 // Allows use of a DecoderMethod in referenced RegisterClass if set.
32 return findOperandDecoderMethod(Record: Record->getValueAsDef(FieldName: "RegClass"));
33
34 if (Record->isSubClassOf(Name: "RegisterClass")) {
35 Decoder = "Decode" + Record->getName().str() + "RegisterClass";
36 } else if (Record->isSubClassOf(Name: "RegClassByHwMode")) {
37 Decoder = "Decode" + Record->getName().str() + "RegClassByHwMode";
38 }
39
40 return {Decoder, true};
41}
42
43OperandInfo InstructionEncoding::getOpInfo(const Record *TypeRecord) {
44 const RecordVal *HasCompleteDecoderVal =
45 TypeRecord->getValue(Name: "hasCompleteDecoder");
46 const BitInit *HasCompleteDecoderBit =
47 HasCompleteDecoderVal
48 ? dyn_cast<BitInit>(Val: HasCompleteDecoderVal->getValue())
49 : nullptr;
50 bool HasCompleteDecoder =
51 HasCompleteDecoderBit ? HasCompleteDecoderBit->getValue() : true;
52
53 return OperandInfo(findOperandDecoderMethod(Record: TypeRecord).first,
54 HasCompleteDecoder);
55}
56
57void InstructionEncoding::parseVarLenEncoding(const VarLenInst &VLI) {
58 InstBits = KnownBits(VLI.size());
59 SoftFailMask = APInt(VLI.size(), 0);
60
61 // Parse Inst field.
62 unsigned I = 0;
63 for (const EncodingSegment &S : VLI) {
64 if (const auto *SegmentBits = dyn_cast<BitsInit>(Val: S.Value)) {
65 for (const Init *V : SegmentBits->getBits()) {
66 if (const auto *B = dyn_cast<BitInit>(Val: V)) {
67 if (B->getValue())
68 InstBits.One.setBit(I);
69 else
70 InstBits.Zero.setBit(I);
71 }
72 ++I;
73 }
74 } else if (const auto *B = dyn_cast<BitInit>(Val: S.Value)) {
75 if (B->getValue())
76 InstBits.One.setBit(I);
77 else
78 InstBits.Zero.setBit(I);
79 ++I;
80 } else {
81 I += S.BitWidth;
82 }
83 }
84 assert(I == VLI.size());
85}
86
87void InstructionEncoding::parseFixedLenEncoding(
88 const BitsInit &RecordInstBits) {
89 // For fixed length instructions, sometimes the `Inst` field specifies more
90 // bits than the actual size of the instruction, which is specified in `Size`.
91 // In such cases, we do some basic validation and drop the upper bits.
92 unsigned BitWidth = EncodingDef->getValueAsInt(FieldName: "Size") * 8;
93 unsigned InstNumBits = RecordInstBits.getNumBits();
94
95 // Returns true if all bits in `Bits` are zero or unset.
96 auto CheckAllZeroOrUnset = [&](ArrayRef<const Init *> Bits,
97 const RecordVal *Field) {
98 bool AllZeroOrUnset = llvm::all_of(Range&: Bits, P: [](const Init *Bit) {
99 if (const auto *BI = dyn_cast<BitInit>(Val: Bit))
100 return !BI->getValue();
101 return isa<UnsetInit>(Val: Bit);
102 });
103 if (AllZeroOrUnset)
104 return;
105 PrintNote(PrintMsg: [Field](raw_ostream &OS) { Field->print(OS); });
106 PrintFatalError(Rec: EncodingDef, Msg: Twine(Name) + ": Size is " + Twine(BitWidth) +
107 " bits, but " + Field->getName() +
108 " bits beyond that are not zero/unset");
109 };
110
111 if (InstNumBits < BitWidth)
112 PrintFatalError(Rec: EncodingDef, Msg: Twine(Name) + ": Size is " + Twine(BitWidth) +
113 " bits, but Inst specifies only " +
114 Twine(InstNumBits) + " bits");
115
116 if (InstNumBits > BitWidth) {
117 // Ensure that all the bits beyond 'Size' are 0 or unset (i.e., carry no
118 // actual encoding).
119 ArrayRef<const Init *> UpperBits =
120 RecordInstBits.getBits().drop_front(N: BitWidth);
121 const RecordVal *InstField = EncodingDef->getValue(Name: "Inst");
122 CheckAllZeroOrUnset(UpperBits, InstField);
123 }
124
125 ArrayRef<const Init *> ActiveInstBits =
126 RecordInstBits.getBits().take_front(N: BitWidth);
127 InstBits = KnownBits(BitWidth);
128 SoftFailMask = APInt(BitWidth, 0);
129
130 // Parse Inst field.
131 for (auto [I, V] : enumerate(First&: ActiveInstBits)) {
132 if (const auto *B = dyn_cast<BitInit>(Val: V)) {
133 if (B->getValue())
134 InstBits.One.setBit(I);
135 else
136 InstBits.Zero.setBit(I);
137 }
138 }
139
140 // Parse SoftFail field.
141 const RecordVal *SoftFailField = EncodingDef->getValue(Name: "SoftFail");
142 if (!SoftFailField)
143 return;
144
145 const auto *SFBits = dyn_cast<BitsInit>(Val: SoftFailField->getValue());
146 if (!SFBits || SFBits->getNumBits() != InstNumBits) {
147 PrintNote(NoteLoc: EncodingDef->getLoc(), Msg: "in record");
148 PrintFatalError(RecVal: SoftFailField,
149 Msg: formatv(Fmt: "SoftFail field, if defined, must be "
150 "of the same type as Inst, which is bits<{}>",
151 Vals&: InstNumBits));
152 }
153
154 if (InstNumBits > BitWidth) {
155 // Ensure that all upper bits of `SoftFail` are 0 or unset.
156 ArrayRef<const Init *> UpperBits = SFBits->getBits().drop_front(N: BitWidth);
157 CheckAllZeroOrUnset(UpperBits, SoftFailField);
158 }
159
160 ArrayRef<const Init *> ActiveSFBits = SFBits->getBits().take_front(N: BitWidth);
161 for (auto [I, V] : enumerate(First&: ActiveSFBits)) {
162 if (const auto *B = dyn_cast<BitInit>(Val: V); B && B->getValue()) {
163 if (!InstBits.Zero[I] && !InstBits.One[I]) {
164 PrintNote(NoteLoc: EncodingDef->getLoc(), Msg: "in record");
165 PrintError(RecVal: SoftFailField,
166 Msg: formatv(Fmt: "SoftFail{{{0}} = 1 requires Inst{{{0}} "
167 "to be fully defined (0 or 1, not '?')",
168 Vals&: I));
169 }
170 SoftFailMask.setBit(I);
171 }
172 }
173}
174
175void InstructionEncoding::parseVarLenOperands(const VarLenInst &VLI) {
176 SmallVector<int> TiedTo;
177
178 for (const auto &[Idx, Op] : enumerate(First: Inst->Operands)) {
179 if (Op.MIOperandInfo && Op.MIOperandInfo->getNumArgs() > 0)
180 for (auto *Arg : Op.MIOperandInfo->getArgs())
181 Operands.push_back(Elt: getOpInfo(TypeRecord: cast<DefInit>(Val: Arg)->getDef()));
182 else
183 Operands.push_back(Elt: getOpInfo(TypeRecord: Op.Rec));
184
185 int TiedReg = Op.getTiedRegister();
186 TiedTo.push_back(Elt: -1);
187 if (TiedReg != -1) {
188 TiedTo[Idx] = TiedReg;
189 TiedTo[TiedReg] = Idx;
190 }
191 }
192
193 unsigned CurrBitPos = 0;
194 for (const auto &EncodingSegment : VLI) {
195 unsigned Offset = 0;
196 StringRef OpName;
197
198 if (const StringInit *SI = dyn_cast<StringInit>(Val: EncodingSegment.Value)) {
199 OpName = SI->getValue();
200 } else if (const DagInit *DI = dyn_cast<DagInit>(Val: EncodingSegment.Value)) {
201 OpName = cast<StringInit>(Val: DI->getArg(Num: 0))->getValue();
202 Offset = cast<IntInit>(Val: DI->getArg(Num: 2))->getValue();
203 }
204
205 if (!OpName.empty()) {
206 auto OpSubOpPair = Inst->Operands.parseOperandName(Op: OpName);
207 unsigned OpIdx = Inst->Operands.getFlattenedOperandNumber(Op: OpSubOpPair);
208 Operands[OpIdx].Fields.emplace_back(args&: CurrBitPos, args: EncodingSegment.BitWidth,
209 args&: Offset);
210 if (!EncodingSegment.CustomDecoder.empty())
211 Operands[OpIdx].Decoder = EncodingSegment.CustomDecoder.str();
212
213 int TiedReg = TiedTo[OpSubOpPair.first];
214 if (TiedReg != -1) {
215 unsigned OpIdx = Inst->Operands.getFlattenedOperandNumber(
216 Op: {TiedReg, OpSubOpPair.second});
217 Operands[OpIdx].Fields.emplace_back(args&: CurrBitPos,
218 args: EncodingSegment.BitWidth, args&: Offset);
219 }
220 }
221
222 CurrBitPos += EncodingSegment.BitWidth;
223 }
224}
225
226static void debugDumpRecord(const Record &Rec) {
227 // Dump the record, so we can see what's going on.
228 PrintNote(PrintMsg: [&Rec](raw_ostream &OS) {
229 OS << "Dumping record for previous error:\n";
230 OS << Rec;
231 });
232}
233
234/// For an operand field named OpName: populate OpInfo.InitValue with the
235/// constant-valued bit values, and OpInfo.Fields with the ranges of bits to
236/// insert from the decoded instruction.
237static void addOneOperandFields(const Record *EncodingDef,
238 const BitsInit &InstBits,
239 std::map<StringRef, StringRef> &TiedNames,
240 const Record *OpRec, StringRef OpName,
241 OperandInfo &OpInfo) {
242 OpInfo.Name = OpName;
243
244 // Find a field with the operand's name.
245 const RecordVal *OpEncodingField = EncodingDef->getValue(Name: OpName);
246
247 // If there is no such field, try tied operand's name.
248 if (!OpEncodingField) {
249 if (auto I = TiedNames.find(x: OpName); I != TiedNames.end())
250 OpEncodingField = EncodingDef->getValue(Name: I->second);
251
252 // If still no luck, we're done with this operand.
253 if (!OpEncodingField) {
254 OpInfo.HasNoEncoding = true;
255 return;
256 }
257 }
258
259 // Some or all bits of the operand may be required to be 0 or 1 depending
260 // on the instruction's encoding. Collect those bits.
261 if (const auto *OpBit = dyn_cast<BitInit>(Val: OpEncodingField->getValue())) {
262 OpInfo.InitValue = OpBit->getValue();
263 return;
264 }
265 if (const auto *OpBits = dyn_cast<BitsInit>(Val: OpEncodingField->getValue())) {
266 if (OpBits->getNumBits() == 0) {
267 if (OpInfo.Decoder.empty()) {
268 PrintError(ErrorLoc: EncodingDef->getLoc(), Msg: "operand '" + OpName + "' of type '" +
269 OpRec->getName() +
270 "' must have a decoder method");
271 }
272 return;
273 }
274 for (unsigned I = 0; I < OpBits->getNumBits(); ++I) {
275 if (const auto *OpBit = dyn_cast<BitInit>(Val: OpBits->getBit(Bit: I)))
276 OpInfo.InitValue = OpInfo.InitValue.value_or(u: 0) |
277 static_cast<uint64_t>(OpBit->getValue()) << I;
278 }
279 }
280
281 // Find out where the variable bits of the operand are encoded. The bits don't
282 // have to be consecutive or in ascending order. For example, an operand could
283 // be encoded as follows:
284 //
285 // 7 6 5 4 3 2 1 0
286 // {1, op{5}, op{2}, op{1}, 0, op{4}, op{3}, ?}
287 //
288 // In this example the operand is encoded in three segments:
289 //
290 // Base Width Offset
291 // op{2...1} 4 2 1
292 // op{4...3} 1 2 3
293 // op{5} 6 1 5
294 //
295 for (unsigned I = 0, J = 0; I != InstBits.getNumBits(); I = J) {
296 const VarInit *Var;
297 unsigned Offset = 0;
298 for (; J != InstBits.getNumBits(); ++J) {
299 const Init *BitJ = InstBits.getBit(Bit: J);
300 if (const auto *VBI = dyn_cast<VarBitInit>(Val: BitJ)) {
301 Var = dyn_cast<VarInit>(Val: VBI->getBitVar());
302 if (I == J)
303 Offset = VBI->getBitNum();
304 else if (VBI->getBitNum() != Offset + J - I)
305 break;
306 } else {
307 Var = dyn_cast<VarInit>(Val: BitJ);
308 }
309 if (!Var ||
310 (Var->getName() != OpName && Var->getName() != TiedNames[OpName]))
311 break;
312 }
313 if (I == J)
314 ++J;
315 else
316 OpInfo.Fields.emplace_back(args&: I, args: J - I, args&: Offset);
317 }
318
319 if (!OpInfo.InitValue && OpInfo.Fields.empty()) {
320 // We found a field in InstructionEncoding record that corresponds to the
321 // named operand, but that field has no constant bits and doesn't contribute
322 // to the Inst field. For now, treat that field as if it didn't exist.
323 // TODO: Remove along with IgnoreNonDecodableOperands.
324 OpInfo.HasNoEncoding = true;
325 }
326}
327
328void InstructionEncoding::parseFixedLenOperands(const BitsInit &Bits) {
329 // Search for tied operands, so that we can correctly instantiate
330 // operands that are not explicitly represented in the encoding.
331 std::map<StringRef, StringRef> TiedNames;
332 for (const auto &Op : Inst->Operands) {
333 for (const auto &[J, CI] : enumerate(First: Op.Constraints)) {
334 if (!CI.isTied())
335 continue;
336 std::pair<unsigned, unsigned> SO =
337 Inst->Operands.getSubOperandNumber(Op: CI.getTiedOperand());
338 StringRef TiedName = Inst->Operands[SO.first].SubOpNames[SO.second];
339 if (TiedName.empty())
340 TiedName = Inst->Operands[SO.first].Name;
341 StringRef MyName = Op.SubOpNames[J];
342 if (MyName.empty())
343 MyName = Op.Name;
344
345 TiedNames[MyName] = TiedName;
346 TiedNames[TiedName] = MyName;
347 }
348 }
349
350 // For each operand, see if we can figure out where it is encoded.
351 for (const CGIOperandList::OperandInfo &Op : Inst->Operands) {
352 // Lookup the decoder method and construct a new OperandInfo to hold our
353 // result.
354 OperandInfo OpInfo = getOpInfo(TypeRecord: Op.Rec);
355
356 // If we have named sub-operands...
357 if (Op.MIOperandInfo && !Op.SubOpNames[0].empty()) {
358 // Then there should not be a custom decoder specified on the top-level
359 // type.
360 if (!OpInfo.Decoder.empty()) {
361 PrintError(Rec: EncodingDef,
362 Msg: "DecoderEmitter: operand \"" + Op.Name + "\" has type \"" +
363 Op.Rec->getName() +
364 "\" with a custom DecoderMethod, but also named "
365 "sub-operands.");
366 continue;
367 }
368
369 // Decode each of the sub-ops separately.
370 for (auto [SubOpName, SubOp] :
371 zip_equal(t: Op.SubOpNames, u: Op.MIOperandInfo->getArgs())) {
372 const Record *SubOpRec = cast<DefInit>(Val: SubOp)->getDef();
373 OperandInfo SubOpInfo = getOpInfo(TypeRecord: SubOpRec);
374 addOneOperandFields(EncodingDef, InstBits: Bits, TiedNames, OpRec: SubOpRec, OpName: SubOpName,
375 OpInfo&: SubOpInfo);
376 Operands.push_back(Elt: std::move(SubOpInfo));
377 }
378 continue;
379 }
380
381 // Otherwise, if we have an operand with sub-operands, but they aren't
382 // named...
383 if (Op.MIOperandInfo && OpInfo.Decoder.empty()) {
384 // If we have sub-ops, we'd better have a custom decoder.
385 // (Otherwise we don't know how to populate them properly...)
386 if (Op.MIOperandInfo->getNumArgs()) {
387 PrintError(Rec: EncodingDef,
388 Msg: "DecoderEmitter: operand \"" + Op.Name +
389 "\" has non-empty MIOperandInfo, but doesn't "
390 "have a custom decoder!");
391 debugDumpRecord(Rec: *EncodingDef);
392 continue;
393 }
394 }
395
396 addOneOperandFields(EncodingDef, InstBits: Bits, TiedNames, OpRec: Op.Rec, OpName: Op.Name, OpInfo);
397 Operands.push_back(Elt: std::move(OpInfo));
398 }
399}
400
401InstructionEncoding::InstructionEncoding(const Record *EncodingDef,
402 const CodeGenInstruction *Inst)
403 : EncodingDef(EncodingDef), Inst(Inst) {
404 const Record *InstDef = Inst->TheDef;
405
406 // Give this encoding a name.
407 if (EncodingDef != InstDef)
408 Name = (EncodingDef->getName() + Twine(':')).str();
409 Name.append(svt: InstDef->getName());
410
411 DecoderNamespace = EncodingDef->getValueAsString(FieldName: "DecoderNamespace");
412 DecoderMethod = EncodingDef->getValueAsString(FieldName: "DecoderMethod");
413 if (!DecoderMethod.empty())
414 HasCompleteDecoder = EncodingDef->getValueAsBit(FieldName: "hasCompleteDecoder");
415
416 const RecordVal *InstField = EncodingDef->getValue(Name: "Inst");
417 if (const auto *DI = dyn_cast<DagInit>(Val: InstField->getValue())) {
418 VarLenInst VLI(DI, InstField);
419 parseVarLenEncoding(VLI);
420 // If the encoding has a custom decoder, don't bother parsing the operands.
421 if (DecoderMethod.empty())
422 parseVarLenOperands(VLI);
423 } else {
424 const auto *BI = cast<BitsInit>(Val: InstField->getValue());
425 parseFixedLenEncoding(RecordInstBits: *BI);
426 // If the encoding has a custom decoder, don't bother parsing the operands.
427 if (DecoderMethod.empty())
428 parseFixedLenOperands(Bits: *BI);
429 }
430
431 if (DecoderMethod.empty()) {
432 // A generated decoder is always successful if none of the operand
433 // decoders can fail (all are always successful).
434 HasCompleteDecoder = all_of(Range&: Operands, P: [](const OperandInfo &Op) {
435 // By default, a generated operand decoder is assumed to always succeed.
436 // This can be overridden by the user.
437 return Op.Decoder.empty() || Op.HasCompleteDecoder;
438 });
439 }
440}
441