Home | History | Annotate | Download | only in TableGen
      1 //===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
      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 // This file implements the CodeGenInstruction class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "CodeGenInstruction.h"
     15 #include "CodeGenTarget.h"
     16 #include "llvm/TableGen/Error.h"
     17 #include "llvm/TableGen/Record.h"
     18 #include "llvm/ADT/StringExtras.h"
     19 #include "llvm/ADT/StringMap.h"
     20 #include "llvm/ADT/STLExtras.h"
     21 #include <set>
     22 using namespace llvm;
     23 
     24 //===----------------------------------------------------------------------===//
     25 // CGIOperandList Implementation
     26 //===----------------------------------------------------------------------===//
     27 
     28 CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
     29   isPredicable = false;
     30   hasOptionalDef = false;
     31   isVariadic = false;
     32 
     33   DagInit *OutDI = R->getValueAsDag("OutOperandList");
     34 
     35   if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
     36     if (Init->getDef()->getName() != "outs")
     37       throw R->getName() + ": invalid def name for output list: use 'outs'";
     38   } else
     39     throw R->getName() + ": invalid output list: use 'outs'";
     40 
     41   NumDefs = OutDI->getNumArgs();
     42 
     43   DagInit *InDI = R->getValueAsDag("InOperandList");
     44   if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
     45     if (Init->getDef()->getName() != "ins")
     46       throw R->getName() + ": invalid def name for input list: use 'ins'";
     47   } else
     48     throw R->getName() + ": invalid input list: use 'ins'";
     49 
     50   unsigned MIOperandNo = 0;
     51   std::set<std::string> OperandNames;
     52   for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
     53     Init *ArgInit;
     54     std::string ArgName;
     55     if (i < NumDefs) {
     56       ArgInit = OutDI->getArg(i);
     57       ArgName = OutDI->getArgName(i);
     58     } else {
     59       ArgInit = InDI->getArg(i-NumDefs);
     60       ArgName = InDI->getArgName(i-NumDefs);
     61     }
     62 
     63     DefInit *Arg = dynamic_cast<DefInit*>(ArgInit);
     64     if (!Arg)
     65       throw "Illegal operand for the '" + R->getName() + "' instruction!";
     66 
     67     Record *Rec = Arg->getDef();
     68     std::string PrintMethod = "printOperand";
     69     std::string EncoderMethod;
     70     std::string OperandType = "OPERAND_UNKNOWN";
     71     unsigned NumOps = 1;
     72     DagInit *MIOpInfo = 0;
     73     if (Rec->isSubClassOf("RegisterOperand")) {
     74       PrintMethod = Rec->getValueAsString("PrintMethod");
     75     } else if (Rec->isSubClassOf("Operand")) {
     76       PrintMethod = Rec->getValueAsString("PrintMethod");
     77       OperandType = Rec->getValueAsString("OperandType");
     78       // If there is an explicit encoder method, use it.
     79       EncoderMethod = Rec->getValueAsString("EncoderMethod");
     80       MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
     81 
     82       // Verify that MIOpInfo has an 'ops' root value.
     83       if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
     84           dynamic_cast<DefInit*>(MIOpInfo->getOperator())
     85           ->getDef()->getName() != "ops")
     86         throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
     87         "'\n";
     88 
     89       // If we have MIOpInfo, then we have #operands equal to number of entries
     90       // in MIOperandInfo.
     91       if (unsigned NumArgs = MIOpInfo->getNumArgs())
     92         NumOps = NumArgs;
     93 
     94       if (Rec->isSubClassOf("PredicateOperand"))
     95         isPredicable = true;
     96       else if (Rec->isSubClassOf("OptionalDefOperand"))
     97         hasOptionalDef = true;
     98     } else if (Rec->getName() == "variable_ops") {
     99       isVariadic = true;
    100       continue;
    101     } else if (Rec->isSubClassOf("RegisterClass")) {
    102       OperandType = "OPERAND_REGISTER";
    103     } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
    104                Rec->getName() != "unknown")
    105       throw "Unknown operand class '" + Rec->getName() +
    106       "' in '" + R->getName() + "' instruction!";
    107 
    108     // Check that the operand has a name and that it's unique.
    109     if (ArgName.empty())
    110       throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
    111       " has no name!";
    112     if (!OperandNames.insert(ArgName).second)
    113       throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
    114       " has the same name as a previous operand!";
    115 
    116     OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
    117                                       OperandType, MIOperandNo, NumOps,
    118                                       MIOpInfo));
    119     MIOperandNo += NumOps;
    120   }
    121 
    122 
    123   // Make sure the constraints list for each operand is large enough to hold
    124   // constraint info, even if none is present.
    125   for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
    126     OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
    127 }
    128 
    129 
    130 /// getOperandNamed - Return the index of the operand with the specified
    131 /// non-empty name.  If the instruction does not have an operand with the
    132 /// specified name, throw an exception.
    133 ///
    134 unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
    135   unsigned OpIdx;
    136   if (hasOperandNamed(Name, OpIdx)) return OpIdx;
    137   throw "'" + TheDef->getName() + "' does not have an operand named '$" +
    138     Name.str() + "'!";
    139 }
    140 
    141 /// hasOperandNamed - Query whether the instruction has an operand of the
    142 /// given name. If so, return true and set OpIdx to the index of the
    143 /// operand. Otherwise, return false.
    144 bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
    145   assert(!Name.empty() && "Cannot search for operand with no name!");
    146   for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
    147     if (OperandList[i].Name == Name) {
    148       OpIdx = i;
    149       return true;
    150     }
    151   return false;
    152 }
    153 
    154 std::pair<unsigned,unsigned>
    155 CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
    156   if (Op.empty() || Op[0] != '$')
    157     throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
    158 
    159   std::string OpName = Op.substr(1);
    160   std::string SubOpName;
    161 
    162   // Check to see if this is $foo.bar.
    163   std::string::size_type DotIdx = OpName.find_first_of(".");
    164   if (DotIdx != std::string::npos) {
    165     SubOpName = OpName.substr(DotIdx+1);
    166     if (SubOpName.empty())
    167       throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
    168     OpName = OpName.substr(0, DotIdx);
    169   }
    170 
    171   unsigned OpIdx = getOperandNamed(OpName);
    172 
    173   if (SubOpName.empty()) {  // If no suboperand name was specified:
    174     // If one was needed, throw.
    175     if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
    176         SubOpName.empty())
    177       throw TheDef->getName() + ": Illegal to refer to"
    178       " whole operand part of complex operand '" + Op + "'";
    179 
    180     // Otherwise, return the operand.
    181     return std::make_pair(OpIdx, 0U);
    182   }
    183 
    184   // Find the suboperand number involved.
    185   DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
    186   if (MIOpInfo == 0)
    187     throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
    188 
    189   // Find the operand with the right name.
    190   for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
    191     if (MIOpInfo->getArgName(i) == SubOpName)
    192       return std::make_pair(OpIdx, i);
    193 
    194   // Otherwise, didn't find it!
    195   throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
    196 }
    197 
    198 static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
    199   // EARLY_CLOBBER: @early $reg
    200   std::string::size_type wpos = CStr.find_first_of(" \t");
    201   std::string::size_type start = CStr.find_first_not_of(" \t");
    202   std::string Tok = CStr.substr(start, wpos - start);
    203   if (Tok == "@earlyclobber") {
    204     std::string Name = CStr.substr(wpos+1);
    205     wpos = Name.find_first_not_of(" \t");
    206     if (wpos == std::string::npos)
    207       throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
    208     Name = Name.substr(wpos);
    209     std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
    210 
    211     // Build the string for the operand
    212     if (!Ops[Op.first].Constraints[Op.second].isNone())
    213       throw "Operand '" + Name + "' cannot have multiple constraints!";
    214     Ops[Op.first].Constraints[Op.second] =
    215     CGIOperandList::ConstraintInfo::getEarlyClobber();
    216     return;
    217   }
    218 
    219   // Only other constraint is "TIED_TO" for now.
    220   std::string::size_type pos = CStr.find_first_of('=');
    221   assert(pos != std::string::npos && "Unrecognized constraint");
    222   start = CStr.find_first_not_of(" \t");
    223   std::string Name = CStr.substr(start, pos - start);
    224 
    225   // TIED_TO: $src1 = $dst
    226   wpos = Name.find_first_of(" \t");
    227   if (wpos == std::string::npos)
    228     throw "Illegal format for tied-to constraint: '" + CStr + "'";
    229   std::string DestOpName = Name.substr(0, wpos);
    230   std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
    231 
    232   Name = CStr.substr(pos+1);
    233   wpos = Name.find_first_not_of(" \t");
    234   if (wpos == std::string::npos)
    235     throw "Illegal format for tied-to constraint: '" + CStr + "'";
    236 
    237   std::pair<unsigned,unsigned> SrcOp =
    238   Ops.ParseOperandName(Name.substr(wpos), false);
    239   if (SrcOp > DestOp)
    240     throw "Illegal tied-to operand constraint '" + CStr + "'";
    241 
    242 
    243   unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
    244 
    245   if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
    246     throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
    247   Ops[DestOp.first].Constraints[DestOp.second] =
    248     CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
    249 }
    250 
    251 static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
    252   if (CStr.empty()) return;
    253 
    254   const std::string delims(",");
    255   std::string::size_type bidx, eidx;
    256 
    257   bidx = CStr.find_first_not_of(delims);
    258   while (bidx != std::string::npos) {
    259     eidx = CStr.find_first_of(delims, bidx);
    260     if (eidx == std::string::npos)
    261       eidx = CStr.length();
    262 
    263     ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
    264     bidx = CStr.find_first_not_of(delims, eidx);
    265   }
    266 }
    267 
    268 void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
    269   while (1) {
    270     std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
    271     std::string OpName = P.first;
    272     DisableEncoding = P.second;
    273     if (OpName.empty()) break;
    274 
    275     // Figure out which operand this is.
    276     std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
    277 
    278     // Mark the operand as not-to-be encoded.
    279     if (Op.second >= OperandList[Op.first].DoNotEncode.size())
    280       OperandList[Op.first].DoNotEncode.resize(Op.second+1);
    281     OperandList[Op.first].DoNotEncode[Op.second] = true;
    282   }
    283 
    284 }
    285 
    286 //===----------------------------------------------------------------------===//
    287 // CodeGenInstruction Implementation
    288 //===----------------------------------------------------------------------===//
    289 
    290 CodeGenInstruction::CodeGenInstruction(Record *R)
    291   : TheDef(R), Operands(R), InferredFrom(0) {
    292   Namespace = R->getValueAsString("Namespace");
    293   AsmString = R->getValueAsString("AsmString");
    294 
    295   isReturn     = R->getValueAsBit("isReturn");
    296   isBranch     = R->getValueAsBit("isBranch");
    297   isIndirectBranch = R->getValueAsBit("isIndirectBranch");
    298   isCompare    = R->getValueAsBit("isCompare");
    299   isMoveImm    = R->getValueAsBit("isMoveImm");
    300   isBitcast    = R->getValueAsBit("isBitcast");
    301   isSelect     = R->getValueAsBit("isSelect");
    302   isBarrier    = R->getValueAsBit("isBarrier");
    303   isCall       = R->getValueAsBit("isCall");
    304   canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
    305   isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
    306   isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
    307   isCommutable = R->getValueAsBit("isCommutable");
    308   isTerminator = R->getValueAsBit("isTerminator");
    309   isReMaterializable = R->getValueAsBit("isReMaterializable");
    310   hasDelaySlot = R->getValueAsBit("hasDelaySlot");
    311   usesCustomInserter = R->getValueAsBit("usesCustomInserter");
    312   hasPostISelHook = R->getValueAsBit("hasPostISelHook");
    313   hasCtrlDep   = R->getValueAsBit("hasCtrlDep");
    314   isNotDuplicable = R->getValueAsBit("isNotDuplicable");
    315 
    316   mayLoad      = R->getValueAsBitOrUnset("mayLoad", mayLoad_Unset);
    317   mayStore     = R->getValueAsBitOrUnset("mayStore", mayStore_Unset);
    318   hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects",
    319                                            hasSideEffects_Unset);
    320   neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
    321 
    322   isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
    323   hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
    324   hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
    325   isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
    326   isPseudo = R->getValueAsBit("isPseudo");
    327   ImplicitDefs = R->getValueAsListOfDefs("Defs");
    328   ImplicitUses = R->getValueAsListOfDefs("Uses");
    329 
    330   if (neverHasSideEffects + hasSideEffects > 1)
    331     throw R->getName() + ": multiple conflicting side-effect flags set!";
    332 
    333   // Parse Constraints.
    334   ParseConstraints(R->getValueAsString("Constraints"), Operands);
    335 
    336   // Parse the DisableEncoding field.
    337   Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
    338 }
    339 
    340 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
    341 /// implicit def and it has a known VT, return the VT, otherwise return
    342 /// MVT::Other.
    343 MVT::SimpleValueType CodeGenInstruction::
    344 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
    345   if (ImplicitDefs.empty()) return MVT::Other;
    346 
    347   // Check to see if the first implicit def has a resolvable type.
    348   Record *FirstImplicitDef = ImplicitDefs[0];
    349   assert(FirstImplicitDef->isSubClassOf("Register"));
    350   const std::vector<MVT::SimpleValueType> &RegVTs =
    351     TargetInfo.getRegisterVTs(FirstImplicitDef);
    352   if (RegVTs.size() == 1)
    353     return RegVTs[0];
    354   return MVT::Other;
    355 }
    356 
    357 
    358 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
    359 /// include text from the specified variant, returning the new string.
    360 std::string CodeGenInstruction::
    361 FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
    362   std::string Res = "";
    363 
    364   for (;;) {
    365     // Find the start of the next variant string.
    366     size_t VariantsStart = 0;
    367     for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
    368       if (Cur[VariantsStart] == '{' &&
    369           (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
    370                                   Cur[VariantsStart-1] != '\\')))
    371         break;
    372 
    373     // Add the prefix to the result.
    374     Res += Cur.slice(0, VariantsStart);
    375     if (VariantsStart == Cur.size())
    376       break;
    377 
    378     ++VariantsStart; // Skip the '{'.
    379 
    380     // Scan to the end of the variants string.
    381     size_t VariantsEnd = VariantsStart;
    382     unsigned NestedBraces = 1;
    383     for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
    384       if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
    385         if (--NestedBraces == 0)
    386           break;
    387       } else if (Cur[VariantsEnd] == '{')
    388         ++NestedBraces;
    389     }
    390 
    391     // Select the Nth variant (or empty).
    392     StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
    393     for (unsigned i = 0; i != Variant; ++i)
    394       Selection = Selection.split('|').second;
    395     Res += Selection.split('|').first;
    396 
    397     assert(VariantsEnd != Cur.size() &&
    398            "Unterminated variants in assembly string!");
    399     Cur = Cur.substr(VariantsEnd + 1);
    400   }
    401 
    402   return Res;
    403 }
    404 
    405 
    406 //===----------------------------------------------------------------------===//
    407 /// CodeGenInstAlias Implementation
    408 //===----------------------------------------------------------------------===//
    409 
    410 /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
    411 /// constructor.  It checks if an argument in an InstAlias pattern matches
    412 /// the corresponding operand of the instruction.  It returns true on a
    413 /// successful match, with ResOp set to the result operand to be used.
    414 bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
    415                                        Record *InstOpRec, bool hasSubOps,
    416                                        ArrayRef<SMLoc> Loc, CodeGenTarget &T,
    417                                        ResultOperand &ResOp) {
    418   Init *Arg = Result->getArg(AliasOpNo);
    419   DefInit *ADI = dynamic_cast<DefInit*>(Arg);
    420 
    421   if (ADI && ADI->getDef() == InstOpRec) {
    422     // If the operand is a record, it must have a name, and the record type
    423     // must match up with the instruction's argument type.
    424     if (Result->getArgName(AliasOpNo).empty())
    425       throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
    426                     " must have a name!");
    427     ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
    428     return true;
    429   }
    430 
    431   // For register operands, the source register class can be a subclass
    432   // of the instruction register class, not just an exact match.
    433   if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
    434     if (!InstOpRec->isSubClassOf("RegisterClass"))
    435       return false;
    436     if (!T.getRegisterClass(InstOpRec)
    437               .hasSubClass(&T.getRegisterClass(ADI->getDef())))
    438       return false;
    439     ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
    440     return true;
    441   }
    442 
    443   // Handle explicit registers.
    444   if (ADI && ADI->getDef()->isSubClassOf("Register")) {
    445     if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
    446       DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
    447       // The operand info should only have a single (register) entry. We
    448       // want the register class of it.
    449       InstOpRec = dynamic_cast<DefInit*>(DI->getArg(0))->getDef();
    450     }
    451 
    452     if (InstOpRec->isSubClassOf("RegisterOperand"))
    453       InstOpRec = InstOpRec->getValueAsDef("RegClass");
    454 
    455     if (!InstOpRec->isSubClassOf("RegisterClass"))
    456       return false;
    457 
    458     if (!T.getRegisterClass(InstOpRec)
    459         .contains(T.getRegBank().getReg(ADI->getDef())))
    460       throw TGError(Loc, "fixed register " + ADI->getDef()->getName() +
    461                     " is not a member of the " + InstOpRec->getName() +
    462                     " register class!");
    463 
    464     if (!Result->getArgName(AliasOpNo).empty())
    465       throw TGError(Loc, "result fixed register argument must "
    466                     "not have a name!");
    467 
    468     ResOp = ResultOperand(ADI->getDef());
    469     return true;
    470   }
    471 
    472   // Handle "zero_reg" for optional def operands.
    473   if (ADI && ADI->getDef()->getName() == "zero_reg") {
    474 
    475     // Check if this is an optional def.
    476     // Tied operands where the source is a sub-operand of a complex operand
    477     // need to represent both operands in the alias destination instruction.
    478     // Allow zero_reg for the tied portion. This can and should go away once
    479     // the MC representation of things doesn't use tied operands at all.
    480     //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
    481     //  throw TGError(Loc, "reg0 used for result that is not an "
    482     //                "OptionalDefOperand!");
    483 
    484     ResOp = ResultOperand(static_cast<Record*>(0));
    485     return true;
    486   }
    487 
    488   // Literal integers.
    489   if (IntInit *II = dynamic_cast<IntInit*>(Arg)) {
    490     if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
    491       return false;
    492     // Integer arguments can't have names.
    493     if (!Result->getArgName(AliasOpNo).empty())
    494       throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
    495                     " must not have a name!");
    496     ResOp = ResultOperand(II->getValue());
    497     return true;
    498   }
    499 
    500   // If both are Operands with the same MVT, allow the conversion. It's
    501   // up to the user to make sure the values are appropriate, just like
    502   // for isel Pat's.
    503   if (InstOpRec->isSubClassOf("Operand") &&
    504       ADI->getDef()->isSubClassOf("Operand")) {
    505     // FIXME: What other attributes should we check here? Identical
    506     // MIOperandInfo perhaps?
    507     if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
    508       return false;
    509     ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
    510     return true;
    511   }
    512 
    513   return false;
    514 }
    515 
    516 CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
    517   AsmString = R->getValueAsString("AsmString");
    518   Result = R->getValueAsDag("ResultInst");
    519 
    520   // Verify that the root of the result is an instruction.
    521   DefInit *DI = dynamic_cast<DefInit*>(Result->getOperator());
    522   if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction"))
    523     throw TGError(R->getLoc(), "result of inst alias should be an instruction");
    524 
    525   ResultInst = &T.getInstruction(DI->getDef());
    526 
    527   // NameClass - If argument names are repeated, we need to verify they have
    528   // the same class.
    529   StringMap<Record*> NameClass;
    530   for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
    531     DefInit *ADI = dynamic_cast<DefInit*>(Result->getArg(i));
    532     if (!ADI || Result->getArgName(i).empty())
    533       continue;
    534     // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
    535     // $foo can exist multiple times in the result list, but it must have the
    536     // same type.
    537     Record *&Entry = NameClass[Result->getArgName(i)];
    538     if (Entry && Entry != ADI->getDef())
    539       throw TGError(R->getLoc(), "result value $" + Result->getArgName(i) +
    540                     " is both " + Entry->getName() + " and " +
    541                     ADI->getDef()->getName() + "!");
    542     Entry = ADI->getDef();
    543   }
    544 
    545   // Decode and validate the arguments of the result.
    546   unsigned AliasOpNo = 0;
    547   for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
    548 
    549     // Tied registers don't have an entry in the result dag unless they're part
    550     // of a complex operand, in which case we include them anyways, as we
    551     // don't have any other way to specify the whole operand.
    552     if (ResultInst->Operands[i].MINumOperands == 1 &&
    553         ResultInst->Operands[i].getTiedRegister() != -1)
    554       continue;
    555 
    556     if (AliasOpNo >= Result->getNumArgs())
    557       throw TGError(R->getLoc(), "not enough arguments for instruction!");
    558 
    559     Record *InstOpRec = ResultInst->Operands[i].Rec;
    560     unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
    561     ResultOperand ResOp(static_cast<int64_t>(0));
    562     if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
    563                         R->getLoc(), T, ResOp)) {
    564       // If this is a simple operand, or a complex operand with a custom match
    565       // class, then we can match is verbatim.
    566       if (NumSubOps == 1 ||
    567           (InstOpRec->getValue("ParserMatchClass") &&
    568            InstOpRec->getValueAsDef("ParserMatchClass")
    569              ->getValueAsString("Name") != "Imm")) {
    570         ResultOperands.push_back(ResOp);
    571         ResultInstOperandIndex.push_back(std::make_pair(i, -1));
    572         ++AliasOpNo;
    573 
    574       // Otherwise, we need to match each of the suboperands individually.
    575       } else {
    576          DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
    577          for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
    578           Record *SubRec = dynamic_cast<DefInit*>(MIOI->getArg(SubOp))->getDef();
    579 
    580           // Take care to instantiate each of the suboperands with the correct
    581           // nomenclature: $foo.bar
    582           ResultOperands.push_back(
    583               ResultOperand(Result->getArgName(AliasOpNo) + "." +
    584                             MIOI->getArgName(SubOp), SubRec));
    585           ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
    586          }
    587          ++AliasOpNo;
    588       }
    589       continue;
    590     }
    591 
    592     // If the argument did not match the instruction operand, and the operand
    593     // is composed of multiple suboperands, try matching the suboperands.
    594     if (NumSubOps > 1) {
    595       DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
    596       for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
    597         if (AliasOpNo >= Result->getNumArgs())
    598           throw TGError(R->getLoc(), "not enough arguments for instruction!");
    599         Record *SubRec = dynamic_cast<DefInit*>(MIOI->getArg(SubOp))->getDef();
    600         if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
    601                             R->getLoc(), T, ResOp)) {
    602           ResultOperands.push_back(ResOp);
    603           ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
    604           ++AliasOpNo;
    605         } else {
    606           throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
    607                         " does not match instruction operand class " +
    608                         (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
    609         }
    610       }
    611       continue;
    612     }
    613     throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
    614                   " does not match instruction operand class " +
    615                   InstOpRec->getName());
    616   }
    617 
    618   if (AliasOpNo != Result->getNumArgs())
    619     throw TGError(R->getLoc(), "too many operands for instruction!");
    620 }
    621