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