llvm.org GIT mirror llvm / ea6408f
llvm-mc/AsmParser: Implement automatic classification of RegisterClass operands. - This drops us to 123 ambiguous instructions (previously ~500) on X86. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78636 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 10 years ago
2 changed file(s) with 277 addition(s) and 79 deletion(s). Raw diff Collapse all Expand all
2929 subl $256, %eax
3030
3131 // FIXME: Check that this matches XOR64ri8
32 // RUN: grep {MCInst(opcode=.*, operands=.reg:19, reg:0, val:1.)} %t
33 xorq $1, %eax
32 // RUN: grep {MCInst(opcode=.*, operands=.reg:80, reg:0, val:1.)} %t
33 xorq $1, %rax
3434
3535 // FIXME: Check that this matches XOR64ri32
36 // RUN: grep {MCInst(opcode=.*, operands=.reg:19, reg:0, val:256.)} %t
37 xorq $256, %eax
36 // RUN: grep {MCInst(opcode=.*, operands=.reg:80, reg:0, val:256.)} %t
37 xorq $256, %rax
38
39 // FIXME: Check that this matches SUB8rr
40 // RUN: grep {MCInst(opcode=.*, operands=.reg:5, reg:0, reg:2.)} %t
41 subb %al, %bl
42
43 // FIXME: Check that this matches SUB16rr
44 // RUN: grep {MCInst(opcode=.*, operands=.reg:8, reg:0, reg:3.)} %t
45 subw %ax, %bx
46
47 // FIXME: Check that this matches SUB32rr
48 // RUN: grep {MCInst(opcode=.*, operands=.reg:21, reg:0, reg:19.)} %t
49 subl %eax, %ebx
50
281281 /// class of operands which can be matched.
282282 struct ClassInfo {
283283 enum ClassInfoKind {
284 Invalid = 0, ///< Invalid kind, for use as a sentinel value.
285 Token, ///< The class for a particular token.
286 Register, ///< A register class.
287 UserClass0 ///< The (first) user defined class, subsequent user defined
288 /// classes are UserClass0+1, and so on.
284 /// Invalid kind, for use as a sentinel value.
285 Invalid = 0,
286
287 /// The class for a particular token.
288 Token,
289
290 /// The (first) register class, subsequent register classes are
291 /// RegisterClass0+1, and so on.
292 RegisterClass0,
293
294 /// The (first) user defined class, subsequent user defined classes are
295 /// UserClass0+1, and so on.
296 UserClass0 = 1<<16
289297 };
290298
291299 /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
292300 /// N) for the Nth user defined class.
293301 unsigned Kind;
294302
295 /// SuperClass - The super class, or 0.
296 ClassInfo *SuperClass;
303 /// SuperClasses - The super classes of this class. Note that for simplicities
304 /// sake user operands only record their immediate super class, while register
305 /// operands include all superclasses.
306 std::vector SuperClasses;
297307
298308 /// Name - The full class name, suitable for use in an enum.
299309 std::string Name;
307317 std::string ValueName;
308318
309319 /// PredicateMethod - The name of the operand method to test whether the
310 /// operand matches this class; this is not valid for Token kinds.
320 /// operand matches this class; this is not valid for Token or register kinds.
311321 std::string PredicateMethod;
312322
313323 /// RenderMethod - The name of the operand method to add this operand to an
314 /// MCInst; this is not valid for Token kinds.
324 /// MCInst; this is not valid for Token or register kinds.
315325 std::string RenderMethod;
326
327 /// isRegisterClass() - Check if this is a register class.
328 bool isRegisterClass() const {
329 return Kind >= RegisterClass0 && Kind < UserClass0;
330 }
316331
317332 /// isUserClass() - Check if this is a user defined class.
318333 bool isUserClass() const {
319334 return Kind >= UserClass0;
320335 }
321336
322 /// getRootClass - Return the root class of this one.
323 const ClassInfo *getRootClass() const {
324 const ClassInfo *CI = this;
325 while (CI->SuperClass)
326 CI = CI->SuperClass;
327 return CI;
337 /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes
338 /// are related if they are in the same class hierarchy.
339 bool isRelatedTo(const ClassInfo &RHS) const {
340 // Tokens are only related to tokens.
341 if (Kind == Token || RHS.Kind == Token)
342 return Kind == Token && RHS.Kind == Token;
343
344 // Registers are only related to registers.
345 if (isRegisterClass() || RHS.isRegisterClass())
346 return isRegisterClass() && RHS.isRegisterClass();
347
348 // Otherwise we have two users operands; they are related if they are in the
349 // same class hierarchy.
350 assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
351 const ClassInfo *Root = this;
352 while (!Root->SuperClasses.empty())
353 Root = Root->SuperClasses.front();
354
355 const ClassInfo *RHSRoot = this;
356 while (!RHSRoot->SuperClasses.empty())
357 RHSRoot = RHSRoot->SuperClasses.front();
358
359 return Root == RHSRoot;
360 }
361
362 /// isSubsetOf - Test whether this class is a subset of \arg RHS;
363 bool isSubsetOf(const ClassInfo &RHS) const {
364 // This is a subset of RHS if it is the same class...
365 if (this == &RHS)
366 return true;
367
368 // ... or if any of its super classes are a subset of RHS.
369 for (std::vector::const_iterator it = SuperClasses.begin(),
370 ie = SuperClasses.end(); it != ie; ++it)
371 if ((*it)->isSubsetOf(RHS))
372 return true;
373
374 return false;
328375 }
329376
330377 /// operator< - Compare two classes.
331378 bool operator<(const ClassInfo &RHS) const {
332 // Incompatible kinds are comparable for classes in disjoint hierarchies.
333 if (Kind != RHS.Kind && getRootClass() != RHS.getRootClass())
379 // Unrelated classes can be ordered by kind.
380 if (!isRelatedTo(RHS))
334381 return Kind < RHS.Kind;
335382
336383 switch (Kind) {
343390 return ValueName < RHS.ValueName;
344391
345392 default:
346 // This class preceeds the RHS if the RHS is a super class.
347 for (ClassInfo *Parent = SuperClass; Parent; Parent = Parent->SuperClass)
348 if (Parent == &RHS)
349 return true;
350
351 return false;
393 // This class preceeds the RHS if it is a proper subset of the RHS.
394 return this != &RHS && isSubsetOf(RHS);
352395 }
353396 }
354397 };
445488 /// The information on the instruction to match.
446489 std::vector Instructions;
447490
491 /// Map of Register records to their class information.
492 std::map RegisterClasses;
493
448494 private:
449495 /// Map of token to class information which has already been constructed.
450496 std::map TokenClasses;
451497
452 /// The ClassInfo instance for registers.
453 ClassInfo *TheRegisterClass;
498 /// Map of RegisterClass records to their class information.
499 std::map RegisterClassClasses;
454500
455501 /// Map of AsmOperandClass records to their class information.
456502 std::map AsmOperandClasses;
462508 /// getOperandClass - Lookup or create the class for the given operand.
463509 ClassInfo *getOperandClass(const StringRef &Token,
464510 const CodeGenInstruction::OperandInfo &OI);
511
512 /// BuildRegisterClasses - Build the ClassInfo* instances for register
513 /// classes.
514 void BuildRegisterClasses(CodeGenTarget &Target);
515
516 /// BuildOperandClasses - Build the ClassInfo* instances for user defined
517 /// operand classes.
518 void BuildOperandClasses(CodeGenTarget &Target);
465519
466520 public:
467521 /// BuildInfo - Construct the various tables used during matching.
521575 if (!Entry) {
522576 Entry = new ClassInfo();
523577 Entry->Kind = ClassInfo::Token;
524 Entry->SuperClass = 0;
525578 Entry->ClassName = "Token";
526579 Entry->Name = "MCK_" + getEnumNameForToken(Token);
527580 Entry->ValueName = Token;
536589 ClassInfo *
537590 AsmMatcherInfo::getOperandClass(const StringRef &Token,
538591 const CodeGenInstruction::OperandInfo &OI) {
539 if (OI.Rec->isSubClassOf("RegisterClass"))
540 return TheRegisterClass;
592 if (OI.Rec->isSubClassOf("RegisterClass")) {
593 ClassInfo *CI = RegisterClassClasses[OI.Rec];
594
595 if (!CI) {
596 PrintError(OI.Rec->getLoc(), "register class has no class info!");
597 throw std::string("ERROR: Missing register class!");
598 }
599
600 return CI;
601 }
541602
542603 assert(OI.Rec->isSubClassOf("Operand") && "Unexpected operand!");
543604 Record *MatchClass = OI.Rec->getValueAsDef("ParserMatchClass");
551612 return CI;
552613 }
553614
554 void AsmMatcherInfo::BuildInfo(CodeGenTarget &Target) {
555 // Build the assembly match class information.
556
557 // Construct the "Reg" class.
558 //
559 // FIXME: This needs to dice up the RegisterClass instances.
560 ClassInfo *RegClass = TheRegisterClass = new ClassInfo();
561 RegClass->Kind = ClassInfo::Register;
562 RegClass->SuperClass = 0;
563 RegClass->ClassName = "Reg";
564 RegClass->Name = "MCK_Reg";
565 RegClass->ValueName = "";
566 RegClass->PredicateMethod = "isReg";
567 RegClass->RenderMethod = "addRegOperands";
568 Classes.push_back(RegClass);
569
570 // Build info for the user defined assembly operand classes.
615 void AsmMatcherInfo::BuildRegisterClasses(CodeGenTarget &Target) {
616 std::vector RegisterClasses;
617 std::vector Registers;
618
619 RegisterClasses = Target.getRegisterClasses();
620 Registers = Target.getRegisters();
621
622 // The register sets used for matching.
623 std::set< std::set > RegisterSets;
624
625 // Gather the defined sets.
626 for (std::vector::iterator it = RegisterClasses.begin(),
627 ie = RegisterClasses.end(); it != ie; ++it)
628 RegisterSets.insert(std::set(it->Elements.begin(),
629 it->Elements.end()));
630
631 // Introduce derived sets where necessary (when a register does not determine
632 // a unique register set class), and build the mapping of registers to the set
633 // they should classify to.
634 std::map > RegisterMap;
635 for (std::vector::iterator it = Registers.begin(),
636 ie = Registers.end(); it != ie; ++it) {
637 CodeGenRegister &CGR = *it;
638 // Compute the intersection of all sets containing this register.
639 std::set ContainingSet;
640
641 for (std::set< std::set >::iterator it = RegisterSets.begin(),
642 ie = RegisterSets.end(); it != ie; ++it) {
643 if (!it->count(CGR.TheDef))
644 continue;
645
646 if (ContainingSet.empty()) {
647 ContainingSet = *it;
648 } else {
649 std::set Tmp;
650 std::swap(Tmp, ContainingSet);
651 std::insert_iterator< std::set > II(ContainingSet,
652 ContainingSet.begin());
653 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(),
654 II);
655 }
656 }
657
658 if (!ContainingSet.empty()) {
659 RegisterSets.insert(ContainingSet);
660 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
661 }
662 }
663
664 // Construct the register classes.
665 std::map, ClassInfo*> RegisterSetClasses;
666 unsigned Index = 0;
667 for (std::set< std::set >::iterator it = RegisterSets.begin(),
668 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
669 ClassInfo *CI = new ClassInfo();
670 CI->Kind = ClassInfo::RegisterClass0 + Index;
671 CI->ClassName = "Reg" + utostr(Index);
672 CI->Name = "MCK_Reg" + utostr(Index);
673 CI->ValueName = "";
674 CI->PredicateMethod = ""; // unused
675 CI->RenderMethod = "addRegOperands";
676 Classes.push_back(CI);
677 RegisterSetClasses.insert(std::make_pair(*it, CI));
678 }
679
680 // Find the superclasses; we could compute only the subgroup lattice edges,
681 // but there isn't really a point.
682 for (std::set< std::set >::iterator it = RegisterSets.begin(),
683 ie = RegisterSets.end(); it != ie; ++it) {
684 ClassInfo *CI = RegisterSetClasses[*it];
685 for (std::set< std::set >::iterator it2 = RegisterSets.begin(),
686 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
687 if (*it != *it2 &&
688 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
689 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
690 }
691
692 // Name the register classes which correspond to a user defined RegisterClass.
693 for (std::vector::iterator it = RegisterClasses.begin(),
694 ie = RegisterClasses.end(); it != ie; ++it) {
695 ClassInfo *CI = RegisterSetClasses[std::set(it->Elements.begin(),
696 it->Elements.end())];
697 if (CI->ValueName.empty()) {
698 CI->ClassName = it->getName();
699 CI->Name = "MCK_" + it->getName();
700 CI->ValueName = it->getName();
701 } else
702 CI->ValueName = CI->ValueName + "," + it->getName();
703
704 RegisterClassClasses.insert(std::make_pair(it->TheDef, CI));
705 }
706
707 // Populate the map for individual registers.
708 for (std::map >::iterator it = RegisterMap.begin(),
709 ie = RegisterMap.end(); it != ie; ++it)
710 this->RegisterClasses[it->first] = RegisterSetClasses[it->second];
711 }
712
713 void AsmMatcherInfo::BuildOperandClasses(CodeGenTarget &Target) {
571714 std::vector AsmOperands;
572715 AsmOperands = Records.getAllDerivedDefinitions("AsmOperandClass");
573716 unsigned Index = 0;
578721
579722 Init *Super = (*it)->getValueInit("SuperClass");
580723 if (DefInit *DI = dynamic_cast(Super)) {
581 CI->SuperClass = AsmOperandClasses[DI->getDef()];
582 if (!CI->SuperClass)
724 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
725 if (!SC)
583726 PrintError((*it)->getLoc(), "Invalid super class reference!");
727 else
728 CI->SuperClasses.push_back(SC);
584729 } else {
585730 assert(dynamic_cast(Super) && "Unexpected SuperClass field!");
586 CI->SuperClass = 0;
587731 }
588732 CI->ClassName = (*it)->getValueAsString("Name");
589733 CI->Name = "MCK_" + CI->ClassName;
612756 AsmOperandClasses[*it] = CI;
613757 Classes.push_back(CI);
614758 }
759 }
760
761 void AsmMatcherInfo::BuildInfo(CodeGenTarget &Target) {
762 // Build info for the register classes.
763 BuildRegisterClasses(Target);
764
765 // Build info for the user defined assembly operand classes.
766 BuildOperandClasses(Target);
615767
616768 // Build the instruction information.
617769 for (std::map::const_iterator
745897 for (; CurIndex != Op.OperandInfo->MIOperandNo; ++CurIndex)
746898 Signature += "Imp";
747899
748 Signature += Op.Class->ClassName;
900 // Registers are always converted the same, don't duplicate the conversion
901 // function based on them.
902 //
903 // FIXME: We could generalize this based on the render method, if it
904 // mattered.
905 if (Op.Class->isRegisterClass())
906 Signature += "Reg";
907 else
908 Signature += Op.Class->ClassName;
749909 Signature += utostr(Op.OperandInfo->MINumOperands);
750910 Signature += "_" + utostr(MIOperandList[i].second);
751911
819979 OS << " " << CI.Name << ", // ";
820980 if (CI.Kind == ClassInfo::Token) {
821981 OS << "'" << CI.ValueName << "'\n";
822 } else if (CI.Kind == ClassInfo::Register) {
982 } else if (CI.isRegisterClass()) {
823983 if (!CI.ValueName.empty())
824984 OS << "register class '" << CI.ValueName << "'\n";
825985 else
836996
837997 /// EmitClassifyOperand - Emit the function to classify an operand.
838998 static void EmitClassifyOperand(CodeGenTarget &Target,
839 std::vector &Infos,
999 AsmMatcherInfo &Info,
8401000 raw_ostream &OS) {
8411001 OS << "static MatchClassKind ClassifyOperand("
8421002 << Target.getName() << "Operand &Operand) {\n";
1003
1004 // Classify tokens.
8431005 OS << " if (Operand.isToken())\n";
8441006 OS << " return MatchTokenString(Operand.getToken());\n\n";
845 for (std::vector::iterator it = Infos.begin(),
846 ie = Infos.end(); it != ie; ++it) {
1007
1008 // Classify registers.
1009 //
1010 // FIXME: Don't hardcode isReg, getReg.
1011 OS << " if (Operand.isReg()) {\n";
1012 OS << " switch (Operand.getReg()) {\n";
1013 OS << " default: return InvalidMatchClass;\n";
1014 for (std::map::iterator
1015 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1016 it != ie; ++it)
1017 OS << " case " << Target.getName() << "::"
1018 << it->first->getName() << ": return " << it->second->Name << ";\n";
1019 OS << " }\n";
1020 OS << " }\n\n";
1021
1022 // Classify user defined operands.
1023 for (std::vector::iterator it = Info.Classes.begin(),
1024 ie = Info.Classes.end(); it != ie; ++it) {
8471025 ClassInfo &CI = **it;
8481026
849 if (CI.Kind != ClassInfo::Token) {
850 OS << " // '" << CI.ClassName << "' class";
851 if (CI.SuperClass) {
852 OS << ", subclass of '" << CI.SuperClass->ClassName << "'";
853 assert(CI < *CI.SuperClass && "Invalid class relation!");
1027 if (!CI.isUserClass())
1028 continue;
1029
1030 OS << " // '" << CI.ClassName << "' class";
1031 if (!CI.SuperClasses.empty()) {
1032 OS << ", subclass of ";
1033 for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i) {
1034 if (i) OS << ", ";
1035 OS << "'" << CI.SuperClasses[i]->ClassName << "'";
1036 assert(CI < *CI.SuperClasses[i] && "Invalid class relation!");
8541037 }
855 OS << "\n";
856
857 OS << " if (Operand." << CI.PredicateMethod << "()) {\n";
1038 }
1039 OS << "\n";
1040
1041 OS << " if (Operand." << CI.PredicateMethod << "()) {\n";
8581042
859 // Validate subclass relationships.
860 if (CI.SuperClass)
861 OS << " assert(Operand." << CI.SuperClass->PredicateMethod
1043 // Validate subclass relationships.
1044 if (!CI.SuperClasses.empty()) {
1045 for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i)
1046 OS << " assert(Operand." << CI.SuperClasses[i]->PredicateMethod
8621047 << "() && \"Invalid class relationship!\");\n";
863
864 OS << " return " << CI.Name << ";\n";
865 OS << " }\n\n";
866 }
1048 }
1049
1050 OS << " return " << CI.Name << ";\n";
1051 OS << " }\n\n";
8671052 }
8681053 OS << " return InvalidMatchClass;\n";
8691054 OS << "}\n\n";
8911076 ie = Infos.end(); it != ie; ++it) {
8921077 ClassInfo &B = **it;
8931078
894 if (&A != &B && A.getRootClass() == B.getRootClass() && A < B)
1079 if (&A != &B && A.isSubsetOf(B))
8951080 SuperClasses.push_back(B.Name);
8961081 }
8971082
9011086 OS << "\n case " << A.Name << ":\n";
9021087
9031088 if (SuperClasses.size() == 1) {
904 OS << " return B == " << SuperClasses.back() << ";\n\n";
1089 OS << " return B == " << SuperClasses.back() << ";\n";
9051090 continue;
9061091 }
9071092
908 OS << " switch (B) {\n";
909 OS << " default: return false;\n";
1093 OS << " switch (B) {\n";
1094 OS << " default: return false;\n";
9101095 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
911 OS << " case " << SuperClasses[i] << ": return true;\n";
912 OS << " }\n\n";
1096 OS << " case " << SuperClasses[i] << ": return true;\n";
1097 OS << " }\n";
9131098 }
9141099 }
9151100 OS << " }\n";
11531338 EmitMatchTokenString(Target, Info.Classes, OS);
11541339
11551340 // Emit the routine to classify an operand.
1156 EmitClassifyOperand(Target, Info.Classes, OS);
1341 EmitClassifyOperand(Target, Info, OS);
11571342
11581343 // Emit the subclass predicate routine.
11591344 EmitIsSubclass(Target, Info.Classes, OS);