llvm.org GIT mirror llvm / 9001929
Revert [tblgen][disasm] Allow multiple encodings to disassemble to the same instruction This reverts r363744 (git commit 9b2252123d1e79d2b3594097a9d9cc60072b83d9) This breaks many buildbots, e.g. http://lab.llvm.org:8011/builders/clang-atom-d525-fedora-rel/builds/203/steps/build%20stage%201/logs/stdio git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363747 91177308-0d34-0410-b5e6-96231b3b80d8 Jordan Rupprecht 3 months ago
2 changed file(s) with 80 addition(s) and 146 deletion(s). Raw diff Collapse all Expand all
397397
398398 class Predicate; // Forward def
399399
400 class InstructionEncoding {
401 // Size of encoded instruction.
402 int Size;
403
404 // The "namespace" in which this instruction exists, on targets like ARM
405 // which multiple ISA namespaces exist.
406 string DecoderNamespace = "";
407
408 // List of predicates which will be turned into isel matching code.
409 list Predicates = [];
410
411 string DecoderMethod = "";
412
413 // Is the instruction decoder method able to completely determine if the
414 // given instruction is valid or not. If the TableGen definition of the
415 // instruction specifies bitpattern A??B where A and B are static bits, the
416 // hasCompleteDecoder flag says whether the decoder method fully handles the
417 // ?? space, i.e. if it is a final arbiter for the instruction validity.
418 // If not then the decoder attempts to continue decoding when the decoder
419 // method fails.
420 //
421 // This allows to handle situations where the encoding is not fully
422 // orthogonal. Example:
423 // * InstA with bitpattern 0b0000????,
424 // * InstB with bitpattern 0b000000?? but the associated decoder method
425 // DecodeInstB() returns Fail when ?? is 0b00 or 0b11.
426 //
427 // The decoder tries to decode a bitpattern that matches both InstA and
428 // InstB bitpatterns first as InstB (because it is the most specific
429 // encoding). In the default case (hasCompleteDecoder = 1), when
430 // DecodeInstB() returns Fail the bitpattern gets rejected. By setting
431 // hasCompleteDecoder = 0 in InstB, the decoder is informed that
432 // DecodeInstB() is not able to determine if all possible values of ?? are
433 // valid or not. If DecodeInstB() returns Fail the decoder will attempt to
434 // decode the bitpattern as InstA too.
435 bit hasCompleteDecoder = 1;
436 }
437
438400 //===----------------------------------------------------------------------===//
439401 // Instruction set description - These classes correspond to the C++ classes in
440402 // the Target/TargetInstrInfo.h file.
441403 //
442 class Instruction : InstructionEncoding {
404 class Instruction {
443405 string Namespace = "";
444406
445407 dag OutOperandList; // An dag containing the MI def operand list.
463425 // Size - Size of encoded instruction, or zero if the size cannot be determined
464426 // from the opcode.
465427 int Size = 0;
428
429 // DecoderNamespace - The "namespace" in which this instruction exists, on
430 // targets like ARM which multiple ISA namespaces exist.
431 string DecoderNamespace = "";
466432
467433 // Code size, for instruction selection.
468434 // FIXME: What does this actually mean?
565531 string DisableEncoding = "";
566532
567533 string PostEncoderMethod = "";
534 string DecoderMethod = "";
535
536 // Is the instruction decoder method able to completely determine if the
537 // given instruction is valid or not. If the TableGen definition of the
538 // instruction specifies bitpattern A??B where A and B are static bits, the
539 // hasCompleteDecoder flag says whether the decoder method fully handles the
540 // ?? space, i.e. if it is a final arbiter for the instruction validity.
541 // If not then the decoder attempts to continue decoding when the decoder
542 // method fails.
543 //
544 // This allows to handle situations where the encoding is not fully
545 // orthogonal. Example:
546 // * InstA with bitpattern 0b0000????,
547 // * InstB with bitpattern 0b000000?? but the associated decoder method
548 // DecodeInstB() returns Fail when ?? is 0b00 or 0b11.
549 //
550 // The decoder tries to decode a bitpattern that matches both InstA and
551 // InstB bitpatterns first as InstB (because it is the most specific
552 // encoding). In the default case (hasCompleteDecoder = 1), when
553 // DecodeInstB() returns Fail the bitpattern gets rejected. By setting
554 // hasCompleteDecoder = 0 in InstB, the decoder is informed that
555 // DecodeInstB() is not able to determine if all possible values of ?? are
556 // valid or not. If DecodeInstB() returns Fail the decoder will attempt to
557 // decode the bitpattern as InstA too.
558 bit hasCompleteDecoder = 1;
568559
569560 /// Target-specific flags. This becomes the TSFlags field in TargetInstrDesc.
570561 bits<64> TSFlags = 0;
599590 /// instruction selection predicates. FastISel cannot handle such cases, but
600591 /// SelectionDAG can.
601592 bit FastISelShouldIgnore = 0;
602 }
603
604 /// Defines an additional encoding that disassembles to the given instruction
605 /// Like Instruction, the Inst and SoftFail fields are omitted to allow targets
606 // to specify their size.
607 class AdditionalEncoding : InstructionEncoding {
608 Instruction AliasOf = I;
609593 }
610594
611595 /// PseudoInstExpansion - Expansion information for a pseudo-instruction.
1515 #include "llvm/ADT/APInt.h"
1616 #include "llvm/ADT/ArrayRef.h"
1717 #include "llvm/ADT/CachedHashString.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SetVector.h"
1820 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/Statistic.h"
2221 #include "llvm/ADT/StringExtras.h"
2322 #include "llvm/ADT/StringRef.h"
2423 #include "llvm/MC/MCFixedLenDisassembler.h"
4746
4847 namespace {
4948
50 STATISTIC(NumEncodings, "Number of encodings considered");
51 STATISTIC(NumEncodingsLackingDisasm, "Number of encodings without disassembler info");
52 STATISTIC(NumInstructions, "Number of instructions considered");
53 STATISTIC(NumEncodingsSupported, "Number of encodings supported");
54 STATISTIC(NumEncodingsOmitted, "Number of encodings omitted");
55
5649 struct EncodingField {
5750 unsigned Base, Width, Offset;
5851 EncodingField(unsigned B, unsigned W, unsigned O)
10093 : EncodingDef(EncodingDef), Inst(Inst) {}
10194 };
10295
103 struct EncodingIDAndOpcode {
104 unsigned EncodingID;
105 unsigned Opcode;
106
107 EncodingIDAndOpcode() : EncodingID(0), Opcode(0) {}
108 EncodingIDAndOpcode(unsigned EncodingID, unsigned Opcode)
109 : EncodingID(EncodingID), Opcode(Opcode) {}
110 };
111
11296 raw_ostream &operator<<(raw_ostream &OS, const EncodingAndInst &Value) {
11397 if (Value.EncodingDef != Value.Inst->TheDef)
11498 OS << Value.EncodingDef->getName() << ":";
117101 }
118102
119103 class FixedLenDecoderEmitter {
120 RecordKeeper &RK;
121104 std::vector NumberedEncodings;
122105
123106 public:
129112 std::string ROK = "MCDisassembler::Success",
130113 std::string RFail = "MCDisassembler::Fail",
131114 std::string L = "")
132 : RK(R), Target(R), PredicateNamespace(std::move(PredicateNamespace)),
115 : Target(R), PredicateNamespace(std::move(PredicateNamespace)),
133116 GuardPrefix(std::move(GPrefix)), GuardPostfix(std::move(GPostfix)),
134117 ReturnOK(std::move(ROK)), ReturnFail(std::move(RFail)),
135118 Locals(std::move(L)) {}
267250 bool Mixed; // a mixed region contains both set and unset bits
268251
269252 // Map of well-known segment value to the set of uid's with that value.
270 std::map>
271 FilteredInstructions;
253 std::map> FilteredInstructions;
272254
273255 // Set of uid's with non-constant segment values.
274 std::vector<EncodingIDAndOpcode> VariableInstructions;
256 std::vector<unsigned> VariableInstructions;
275257
276258 // Map of well-known segment value to its delegate.
277259 std::map> FilterChooserMap;
280262 unsigned NumFiltered;
281263
282264 // Keeps track of the last opcode in the filtered bucket.
283 EncodingIDAndOpcode LastOpcFiltered;
265 unsigned LastOpcFiltered;
284266
285267 public:
286268 Filter(Filter &&f);
290272
291273 unsigned getNumFiltered() const { return NumFiltered; }
292274
293 EncodingIDAndOpcode getSingletonOpc() const {
275 unsigned getSingletonOpc() const {
294276 assert(NumFiltered == 1);
295277 return LastOpcFiltered;
296278 }
354336 friend class Filter;
355337
356338 // Vector of codegen instructions to choose our filter.
357 ArrayRef<const EncodingAndInst> AllInstructions;
339 ArrayRef<EncodingAndInst> AllInstructions;
358340
359341 // Vector of uid's for this filter chooser to work on.
360 // The first member of the pair is the opcode id being decoded, the second is
361 // the opcode id that should be emitted.
362 const std::vector<EncodingIDAndOpcode> &Opcodes;
342 const std::vector<unsigned> &Opcodes;
363343
364344 // Lookup table for the operand decoding of instructions.
365345 const std::map> &Operands;
384364 const FixedLenDecoderEmitter *Emitter;
385365
386366 public:
387 FilterChooser(ArrayRef Insts,
388 const std::vector &IDs,
367 FilterChooser(ArrayRef Insts,
368 const std::vector &IDs,
389369 const std::map> &Ops,
390370 unsigned BW, const FixedLenDecoderEmitter *E)
391371 : AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
394374 doFilter();
395375 }
396376
397 FilterChooser(ArrayRef Insts,
398 const std::vector &IDs,
377 FilterChooser(ArrayRef Insts,
378 const std::vector &IDs,
399379 const std::map> &Ops,
400380 const std::vector &ParentFilterBitValues,
401381 const FilterChooser &parent)
429409 else
430410 Insn.push_back(bitFromBits(Bits, i));
431411 }
432 }
433
434 // Emit the name of the encoding/instruction pair.
435 void emitNameWithID(raw_ostream &OS, unsigned Opcode) const {
436 const Record *EncodingDef = AllInstructions[Opcode].EncodingDef;
437 const Record *InstDef = AllInstructions[Opcode].Inst->TheDef;
438 if (EncodingDef != InstDef)
439 OS << EncodingDef->getName() << ":";
440 OS << InstDef->getName();
441412 }
442413
443414 // Populates the field of the insn given the start position and the number of
490461
491462 // Emits table entries to decode the singleton.
492463 void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
493 EncodingIDAndOpcode Opc) const;
464 unsigned Opc) const;
494465
495466 // Emits code to decode the singleton, and then to decode the rest.
496467 void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
551522 assert(StartBit + NumBits - 1 < Owner->BitWidth);
552523
553524 NumFiltered = 0;
554 LastOpcFiltered = {0, 0};
525 LastOpcFiltered = 0;
555526
556527 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
557528 insn_t Insn;
558529
559530 // Populates the insn given the uid.
560 Owner->insnWithID(Insn, Owner->Opcodes[i].EncodingID);
531 Owner->insnWithID(Insn, Owner->Opcodes[i]);
561532
562533 uint64_t Field;
563534 // Scans the segment for possibly well-specified encoding bits.
10531024 // 1: Water (the bit value does not affect decoding)
10541025 // 2: Island (well-known bit value needed for decoding)
10551026 int State = 0;
1056 int64_t Val = -1;
1027 int Val = -1;
10571028
10581029 for (unsigned i = 0; i < BitWidth; ++i) {
10591030 Val = Value(Insn[i]);
13411312
13421313 // Emits table entries to decode the singleton.
13431314 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1344 EncodingIDAndOpcode Opc) const {
1315 unsigned Opc) const {
13451316 std::vector StartBits;
13461317 std::vector EndBits;
13471318 std::vector FieldVals;
13481319 insn_t Insn;
1349 insnWithID(Insn, Opc.EncodingID);
1320 insnWithID(Insn, Opc);
13501321
13511322 // Look for islands of undecoded bits of the singleton.
13521323 getIslands(StartBits, EndBits, FieldVals, Insn);
13541325 unsigned Size = StartBits.size();
13551326
13561327 // Emit the predicate table entry if one is needed.
1357 emitPredicateTableEntry(TableInfo, Opc.EncodingID);
1328 emitPredicateTableEntry(TableInfo, Opc);
13581329
13591330 // Check any additional encoding fields needed.
13601331 for (unsigned I = Size; I != 0; --I) {
13781349 }
13791350
13801351 // Check for soft failure of the match.
1381 emitSoftFailTableEntry(TableInfo, Opc.EncodingID);
1352 emitSoftFailTableEntry(TableInfo, Opc);
13821353
13831354 bool HasCompleteDecoder;
1384 unsigned DIdx =
1385 getDecoderIndex(TableInfo.Decoders, Opc.EncodingID, HasCompleteDecoder);
1355 unsigned DIdx = getDecoderIndex(TableInfo.Decoders, Opc, HasCompleteDecoder);
13861356
13871357 // Produce OPC_Decode or OPC_TryDecode opcode based on the information
13881358 // whether the instruction decoder is complete or not. If it is complete
13951365 // can decode it.
13961366 TableInfo.Table.push_back(HasCompleteDecoder ? MCD::OPC_Decode :
13971367 MCD::OPC_TryDecode);
1398 NumEncodingsSupported++;
13991368 uint8_t Buffer[16], *p;
1400 encodeULEB128(Opc.Opcode, Buffer);
1369 encodeULEB128(Opc, Buffer);
14011370 for (p = Buffer; *p >= 128 ; ++p)
14021371 TableInfo.Table.push_back(*p);
14031372 TableInfo.Table.push_back(*p);
14231392 // Emits table entries to decode the singleton, and then to decode the rest.
14241393 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
14251394 const Filter &Best) const {
1426 EncodingIDAndOpcode Opc = Best.getSingletonOpc();
1395 unsigned Opc = Best.getSingletonOpc();
14271396
14281397 // complex singletons need predicate checks from the first singleton
14291398 // to refer forward to the variable filterchooser that follows.
14831452 std::vector FieldVals;
14841453 insn_t Insn;
14851454
1486 insnWithID(Insn, Opcodes[i].EncodingID);
1455 insnWithID(Insn, Opcodes[i]);
14871456
14881457 // Look for islands of undecoded bits of any instruction.
14891458 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
15271496 for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
15281497 insn_t insn;
15291498
1530 insnWithID(insn, Opcodes[InsnIndex].EncodingID);
1499 insnWithID(insn, Opcodes[InsnIndex]);
15311500
15321501 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
15331502 switch (bitAttrs[BitIndex]) {
17461715 dumpStack(errs(), "\t\t");
17471716
17481717 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1749 errs() << '\t';
1750 emitNameWithID(errs(), Opcodes[i].EncodingID);
1751 errs() << " ";
1752 dumpBits(
1753 errs(),
1754 getBitsField(*AllInstructions[Opcodes[i].EncodingID].EncodingDef, "Inst"));
1718 errs() << '\t' << AllInstructions[Opcodes[i]] << " ";
1719 dumpBits(errs(),
1720 getBitsField(*AllInstructions[Opcodes[i]].EncodingDef, "Inst"));
17551721 errs() << '\n';
17561722 }
17571723 }
17831749 return Decoder;
17841750 }
17851751
1786 static bool
1787 populateInstruction(CodeGenTarget &Target, const Record &EncodingDef,
1788 const CodeGenInstruction &CGI, unsigned Opc,
1789 std::map> &Operands) {
1752 static bool populateInstruction(CodeGenTarget &Target,
1753 const CodeGenInstruction &CGI, unsigned Opc,
1754 std::map> &Operands){
17901755 const Record &Def = *CGI.TheDef;
17911756 // If all the bit positions are not specified; do not decode this instruction.
17921757 // We are bound to fail! For proper disassembly, the well-known encoding bits
17931758 // of the instruction must be fully specified.
17941759
1795 BitsInit &Bits = getBitsField(EncodingDef, "Inst");
1760 BitsInit &Bits = getBitsField(Def, "Inst");
17961761 if (Bits.allInComplete()) return false;
17971762
17981763 std::vector InsnOperands;
17991764
18001765 // If the instruction has specified a custom decoding hook, use that instead
18011766 // of trying to auto-generate the decoder.
1802 StringRef InstDecoder = EncodingDef.getValueAsString("DecoderMethod");
1767 StringRef InstDecoder = Def.getValueAsString("DecoderMethod");
18031768 if (InstDecoder != "") {
1804 bool HasCompleteInstDecoder = EncodingDef.getValueAsBit("hasCompleteDecoder");
1769 bool HasCompleteInstDecoder = Def.getValueAsBit("hasCompleteDecoder");
18051770 InsnOperands.push_back(OperandInfo(InstDecoder, HasCompleteInstDecoder));
18061771 Operands[Opc] = InsnOperands;
18071772 return true;
21772142 << " const FeatureBitset& Bits = STI.getFeatureBits();\n"
21782143 << "\n"
21792144 << " const uint8_t *Ptr = DecodeTable;\n"
2180 << " InsnType CurFieldValue = 0;\n"
2145 << " uint32_t CurFieldValue = 0;\n"
21812146 << " DecodeStatus S = MCDisassembler::Success;\n"
21822147 << " while (true) {\n"
21832148 << " ptrdiff_t Loc = Ptr - DecodeTable;\n"
22222187 << " unsigned Len = *++Ptr;\n"
22232188 << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n"
22242189 << " // Decode the field value.\n"
2225 << " InsnType ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
2190 << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
22262191 << " Ptr += Len;\n"
22272192 << " // NumToSkip is a plain 24-bit integer.\n"
22282193 << " unsigned NumToSkip = *Ptr++;\n"
23692334 // Parameterize the decoders based on namespace and instruction width.
23702335 const auto &NumberedInstructions = Target.getInstructionsByEnumValue();
23712336 NumberedEncodings.reserve(NumberedInstructions.size());
2372 DenseMap IndexOfInstruction;
2373 for (const auto &NumberedInstruction : NumberedInstructions) {
2374 IndexOfInstruction[NumberedInstruction->TheDef] = NumberedEncodings.size();
2337 for (const auto &NumberedInstruction : NumberedInstructions)
23752338 NumberedEncodings.emplace_back(NumberedInstruction->TheDef, NumberedInstruction);
2376 }
2377 for (const auto &NumberedAlias : RK.getAllDerivedDefinitions("AdditionalEncoding"))
2378 NumberedEncodings.emplace_back(
2379 NumberedAlias,
2380 &Target.getInstruction(NumberedAlias->getValueAsDef("AliasOf")));
2381
2382 std::map, std::vector>
2383 OpcMap;
2339
2340 std::map,
2341 std::vector> OpcMap;
23842342 std::map> Operands;
23852343
23862344 for (unsigned i = 0; i < NumberedEncodings.size(); ++i) {
2387 const Record *EncodingDef = NumberedEncodings[i].EncodingDef;
23882345 const CodeGenInstruction *Inst = NumberedEncodings[i].Inst;
23892346 const Record *Def = Inst->TheDef;
2390 unsigned Size = EncodingDef->getValueAsInt("Size");
2347 unsigned Size = Def->getValueAsInt("Size");
23912348 if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
23922349 Def->getValueAsBit("isPseudo") ||
23932350 Def->getValueAsBit("isAsmParserOnly") ||
2394 Def->getValueAsBit("isCodeGenOnly")) {
2395 NumEncodingsLackingDisasm++;
2351 Def->getValueAsBit("isCodeGenOnly"))
23962352 continue;
2397 }
2398
2399 if (i < NumberedInstructions.size())
2400 NumInstructions++;
2401 NumEncodings++;
2402
2403 StringRef DecoderNamespace = EncodingDef->getValueAsString("DecoderNamespace");
2353
2354 StringRef DecoderNamespace = Def->getValueAsString("DecoderNamespace");
24042355
24052356 if (Size) {
2406 if (populateInstruction(Target, *EncodingDef, *Inst, i, Operands)) {
2407 OpcMap[std::make_pair(DecoderNamespace, Size)].emplace_back(i, IndexOfInstruction.find(Def)->second);
2408 } else
2409 NumEncodingsOmitted++;
2357 if (populateInstruction(Target, *Inst, i, Operands)) {
2358 OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i);
2359 }
24102360 }
24112361 }
24122362
24132363 DecoderTableInfo TableInfo;
24142364 for (const auto &Opc : OpcMap) {
24152365 // Emit the decoder for this namespace+width combination.
2416 ArrayRef NumberedEncodingsRef(
2417 NumberedEncodings.data(), NumberedEncodings.size());
2366 ArrayRef NumberedEncodingsRef(NumberedEncodings.data(),
2367 NumberedEncodings.size());
24182368 FilterChooser FC(NumberedEncodingsRef, Opc.second, Operands,
24192369 8 * Opc.first.second, this);
24202370