10b57cec5SDimitry Andric //===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the CodeGenInstruction class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "CodeGenInstruction.h"
140b57cec5SDimitry Andric #include "CodeGenTarget.h"
150b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
160b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
170b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
180b57cec5SDimitry Andric #include "llvm/TableGen/Error.h"
190b57cec5SDimitry Andric #include "llvm/TableGen/Record.h"
200b57cec5SDimitry Andric #include <set>
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
240b57cec5SDimitry Andric // CGIOperandList Implementation
250b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
260b57cec5SDimitry Andric 
CGIOperandList(Record * R)270b57cec5SDimitry Andric CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
280b57cec5SDimitry Andric   isPredicable = false;
290b57cec5SDimitry Andric   hasOptionalDef = false;
300b57cec5SDimitry Andric   isVariadic = false;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric   DagInit *OutDI = R->getValueAsDag("OutOperandList");
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric   if (DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) {
350b57cec5SDimitry Andric     if (Init->getDef()->getName() != "outs")
360b57cec5SDimitry Andric       PrintFatalError(R->getLoc(),
370b57cec5SDimitry Andric                       R->getName() +
380b57cec5SDimitry Andric                           ": invalid def name for output list: use 'outs'");
390b57cec5SDimitry Andric   } else
400b57cec5SDimitry Andric     PrintFatalError(R->getLoc(),
410b57cec5SDimitry Andric                     R->getName() + ": invalid output list: use 'outs'");
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   NumDefs = OutDI->getNumArgs();
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   DagInit *InDI = R->getValueAsDag("InOperandList");
460b57cec5SDimitry Andric   if (DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) {
470b57cec5SDimitry Andric     if (Init->getDef()->getName() != "ins")
480b57cec5SDimitry Andric       PrintFatalError(R->getLoc(),
490b57cec5SDimitry Andric                       R->getName() +
500b57cec5SDimitry Andric                           ": invalid def name for input list: use 'ins'");
510b57cec5SDimitry Andric   } else
520b57cec5SDimitry Andric     PrintFatalError(R->getLoc(),
530b57cec5SDimitry Andric                     R->getName() + ": invalid input list: use 'ins'");
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   unsigned MIOperandNo = 0;
560b57cec5SDimitry Andric   std::set<std::string> OperandNames;
570b57cec5SDimitry Andric   unsigned e = InDI->getNumArgs() + OutDI->getNumArgs();
580b57cec5SDimitry Andric   OperandList.reserve(e);
595ffd83dbSDimitry Andric   bool VariadicOuts = false;
600b57cec5SDimitry Andric   for (unsigned i = 0; i != e; ++i){
610b57cec5SDimitry Andric     Init *ArgInit;
620b57cec5SDimitry Andric     StringRef ArgName;
630b57cec5SDimitry Andric     if (i < NumDefs) {
640b57cec5SDimitry Andric       ArgInit = OutDI->getArg(i);
650b57cec5SDimitry Andric       ArgName = OutDI->getArgNameStr(i);
660b57cec5SDimitry Andric     } else {
670b57cec5SDimitry Andric       ArgInit = InDI->getArg(i-NumDefs);
680b57cec5SDimitry Andric       ArgName = InDI->getArgNameStr(i-NumDefs);
690b57cec5SDimitry Andric     }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric     DefInit *Arg = dyn_cast<DefInit>(ArgInit);
720b57cec5SDimitry Andric     if (!Arg)
730b57cec5SDimitry Andric       PrintFatalError(R->getLoc(), "Illegal operand for the '" + R->getName() +
740b57cec5SDimitry Andric                                        "' instruction!");
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric     Record *Rec = Arg->getDef();
770b57cec5SDimitry Andric     std::string PrintMethod = "printOperand";
780b57cec5SDimitry Andric     std::string EncoderMethod;
790b57cec5SDimitry Andric     std::string OperandType = "OPERAND_UNKNOWN";
800b57cec5SDimitry Andric     std::string OperandNamespace = "MCOI";
810b57cec5SDimitry Andric     unsigned NumOps = 1;
820b57cec5SDimitry Andric     DagInit *MIOpInfo = nullptr;
830b57cec5SDimitry Andric     if (Rec->isSubClassOf("RegisterOperand")) {
845ffd83dbSDimitry Andric       PrintMethod = std::string(Rec->getValueAsString("PrintMethod"));
855ffd83dbSDimitry Andric       OperandType = std::string(Rec->getValueAsString("OperandType"));
865ffd83dbSDimitry Andric       OperandNamespace = std::string(Rec->getValueAsString("OperandNamespace"));
875ffd83dbSDimitry Andric       EncoderMethod = std::string(Rec->getValueAsString("EncoderMethod"));
880b57cec5SDimitry Andric     } else if (Rec->isSubClassOf("Operand")) {
895ffd83dbSDimitry Andric       PrintMethod = std::string(Rec->getValueAsString("PrintMethod"));
905ffd83dbSDimitry Andric       OperandType = std::string(Rec->getValueAsString("OperandType"));
915ffd83dbSDimitry Andric       OperandNamespace = std::string(Rec->getValueAsString("OperandNamespace"));
920b57cec5SDimitry Andric       // If there is an explicit encoder method, use it.
935ffd83dbSDimitry Andric       EncoderMethod = std::string(Rec->getValueAsString("EncoderMethod"));
940b57cec5SDimitry Andric       MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric       // Verify that MIOpInfo has an 'ops' root value.
970b57cec5SDimitry Andric       if (!isa<DefInit>(MIOpInfo->getOperator()) ||
980b57cec5SDimitry Andric           cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
990b57cec5SDimitry Andric         PrintFatalError(R->getLoc(),
1000b57cec5SDimitry Andric                         "Bad value for MIOperandInfo in operand '" +
1010b57cec5SDimitry Andric                             Rec->getName() + "'\n");
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric       // If we have MIOpInfo, then we have #operands equal to number of entries
1040b57cec5SDimitry Andric       // in MIOperandInfo.
1050b57cec5SDimitry Andric       if (unsigned NumArgs = MIOpInfo->getNumArgs())
1060b57cec5SDimitry Andric         NumOps = NumArgs;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric       if (Rec->isSubClassOf("PredicateOp"))
1090b57cec5SDimitry Andric         isPredicable = true;
1100b57cec5SDimitry Andric       else if (Rec->isSubClassOf("OptionalDefOperand"))
1110b57cec5SDimitry Andric         hasOptionalDef = true;
1120b57cec5SDimitry Andric     } else if (Rec->getName() == "variable_ops") {
1135ffd83dbSDimitry Andric       if (i < NumDefs)
1145ffd83dbSDimitry Andric         VariadicOuts = true;
1150b57cec5SDimitry Andric       isVariadic = true;
1160b57cec5SDimitry Andric       continue;
1170b57cec5SDimitry Andric     } else if (Rec->isSubClassOf("RegisterClass")) {
1180b57cec5SDimitry Andric       OperandType = "OPERAND_REGISTER";
1190b57cec5SDimitry Andric     } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
1200b57cec5SDimitry Andric                !Rec->isSubClassOf("unknown_class"))
1210b57cec5SDimitry Andric       PrintFatalError(R->getLoc(), "Unknown operand class '" + Rec->getName() +
1220b57cec5SDimitry Andric                                        "' in '" + R->getName() +
1230b57cec5SDimitry Andric                                        "' instruction!");
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric     // Check that the operand has a name and that it's unique.
1260b57cec5SDimitry Andric     if (ArgName.empty())
1270b57cec5SDimitry Andric       PrintFatalError(R->getLoc(), "In instruction '" + R->getName() +
1280b57cec5SDimitry Andric                                        "', operand #" + Twine(i) +
1290b57cec5SDimitry Andric                                        " has no name!");
1305ffd83dbSDimitry Andric     if (!OperandNames.insert(std::string(ArgName)).second)
1310b57cec5SDimitry Andric       PrintFatalError(R->getLoc(),
1320b57cec5SDimitry Andric                       "In instruction '" + R->getName() + "', operand #" +
1330b57cec5SDimitry Andric                           Twine(i) +
1340b57cec5SDimitry Andric                           " has the same name as a previous operand!");
1350b57cec5SDimitry Andric 
1365ffd83dbSDimitry Andric     OperandList.emplace_back(
1375ffd83dbSDimitry Andric         Rec, std::string(ArgName), std::string(PrintMethod),
1385ffd83dbSDimitry Andric         std::string(EncoderMethod), OperandNamespace + "::" + OperandType,
1395ffd83dbSDimitry Andric         MIOperandNo, NumOps, MIOpInfo);
1400b57cec5SDimitry Andric     MIOperandNo += NumOps;
1410b57cec5SDimitry Andric   }
1420b57cec5SDimitry Andric 
1435ffd83dbSDimitry Andric   if (VariadicOuts)
1445ffd83dbSDimitry Andric     --NumDefs;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   // Make sure the constraints list for each operand is large enough to hold
1470b57cec5SDimitry Andric   // constraint info, even if none is present.
1480b57cec5SDimitry Andric   for (OperandInfo &OpInfo : OperandList)
1490b57cec5SDimitry Andric     OpInfo.Constraints.resize(OpInfo.MINumOperands);
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric /// getOperandNamed - Return the index of the operand with the specified
1540b57cec5SDimitry Andric /// non-empty name.  If the instruction does not have an operand with the
1550b57cec5SDimitry Andric /// specified name, abort.
1560b57cec5SDimitry Andric ///
getOperandNamed(StringRef Name) const1570b57cec5SDimitry Andric unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
1580b57cec5SDimitry Andric   unsigned OpIdx;
1590b57cec5SDimitry Andric   if (hasOperandNamed(Name, OpIdx))
1600b57cec5SDimitry Andric     return OpIdx;
1610b57cec5SDimitry Andric   PrintFatalError(TheDef->getLoc(), "'" + TheDef->getName() +
1620b57cec5SDimitry Andric                                         "' does not have an operand named '$" +
1630b57cec5SDimitry Andric                                         Name + "'!");
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric /// hasOperandNamed - Query whether the instruction has an operand of the
1670b57cec5SDimitry Andric /// given name. If so, return true and set OpIdx to the index of the
1680b57cec5SDimitry Andric /// operand. Otherwise, return false.
hasOperandNamed(StringRef Name,unsigned & OpIdx) const1690b57cec5SDimitry Andric bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
1700b57cec5SDimitry Andric   assert(!Name.empty() && "Cannot search for operand with no name!");
1710b57cec5SDimitry Andric   for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
1720b57cec5SDimitry Andric     if (OperandList[i].Name == Name) {
1730b57cec5SDimitry Andric       OpIdx = i;
1740b57cec5SDimitry Andric       return true;
1750b57cec5SDimitry Andric     }
1760b57cec5SDimitry Andric   return false;
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric std::pair<unsigned,unsigned>
ParseOperandName(StringRef Op,bool AllowWholeOp)180*5f7ddb14SDimitry Andric CGIOperandList::ParseOperandName(StringRef Op, bool AllowWholeOp) {
1810b57cec5SDimitry Andric   if (Op.empty() || Op[0] != '$')
1820b57cec5SDimitry Andric     PrintFatalError(TheDef->getLoc(),
1830b57cec5SDimitry Andric                     TheDef->getName() + ": Illegal operand name: '" + Op + "'");
1840b57cec5SDimitry Andric 
185*5f7ddb14SDimitry Andric   StringRef OpName = Op.substr(1);
186*5f7ddb14SDimitry Andric   StringRef SubOpName;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   // Check to see if this is $foo.bar.
189*5f7ddb14SDimitry Andric   StringRef::size_type DotIdx = OpName.find_first_of('.');
190*5f7ddb14SDimitry Andric   if (DotIdx != StringRef::npos) {
1910b57cec5SDimitry Andric     SubOpName = OpName.substr(DotIdx+1);
1920b57cec5SDimitry Andric     if (SubOpName.empty())
1930b57cec5SDimitry Andric       PrintFatalError(TheDef->getLoc(),
1940b57cec5SDimitry Andric                       TheDef->getName() +
1950b57cec5SDimitry Andric                           ": illegal empty suboperand name in '" + Op + "'");
1960b57cec5SDimitry Andric     OpName = OpName.substr(0, DotIdx);
1970b57cec5SDimitry Andric   }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   unsigned OpIdx = getOperandNamed(OpName);
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   if (SubOpName.empty()) {  // If no suboperand name was specified:
2020b57cec5SDimitry Andric     // If one was needed, throw.
2030b57cec5SDimitry Andric     if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
2040b57cec5SDimitry Andric         SubOpName.empty())
2050b57cec5SDimitry Andric       PrintFatalError(TheDef->getLoc(),
2060b57cec5SDimitry Andric                       TheDef->getName() +
2070b57cec5SDimitry Andric                           ": Illegal to refer to"
2080b57cec5SDimitry Andric                           " whole operand part of complex operand '" +
2090b57cec5SDimitry Andric                           Op + "'");
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric     // Otherwise, return the operand.
2120b57cec5SDimitry Andric     return std::make_pair(OpIdx, 0U);
2130b57cec5SDimitry Andric   }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   // Find the suboperand number involved.
2160b57cec5SDimitry Andric   DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
2170b57cec5SDimitry Andric   if (!MIOpInfo)
2180b57cec5SDimitry Andric     PrintFatalError(TheDef->getLoc(), TheDef->getName() +
2190b57cec5SDimitry Andric                                           ": unknown suboperand name in '" +
2200b57cec5SDimitry Andric                                           Op + "'");
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   // Find the operand with the right name.
2230b57cec5SDimitry Andric   for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
2240b57cec5SDimitry Andric     if (MIOpInfo->getArgNameStr(i) == SubOpName)
2250b57cec5SDimitry Andric       return std::make_pair(OpIdx, i);
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric   // Otherwise, didn't find it!
2280b57cec5SDimitry Andric   PrintFatalError(TheDef->getLoc(), TheDef->getName() +
2290b57cec5SDimitry Andric                                         ": unknown suboperand name in '" + Op +
2300b57cec5SDimitry Andric                                         "'");
2310b57cec5SDimitry Andric   return std::make_pair(0U, 0U);
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric 
ParseConstraint(StringRef CStr,CGIOperandList & Ops,Record * Rec)234*5f7ddb14SDimitry Andric static void ParseConstraint(StringRef CStr, CGIOperandList &Ops,
2350b57cec5SDimitry Andric                             Record *Rec) {
2360b57cec5SDimitry Andric   // EARLY_CLOBBER: @early $reg
237*5f7ddb14SDimitry Andric   StringRef::size_type wpos = CStr.find_first_of(" \t");
238*5f7ddb14SDimitry Andric   StringRef::size_type start = CStr.find_first_not_of(" \t");
239*5f7ddb14SDimitry Andric   StringRef Tok = CStr.substr(start, wpos - start);
2400b57cec5SDimitry Andric   if (Tok == "@earlyclobber") {
241*5f7ddb14SDimitry Andric     StringRef Name = CStr.substr(wpos+1);
2420b57cec5SDimitry Andric     wpos = Name.find_first_not_of(" \t");
243*5f7ddb14SDimitry Andric     if (wpos == StringRef::npos)
2440b57cec5SDimitry Andric       PrintFatalError(
2450b57cec5SDimitry Andric         Rec->getLoc(), "Illegal format for @earlyclobber constraint in '" +
2460b57cec5SDimitry Andric         Rec->getName() + "': '" + CStr + "'");
2470b57cec5SDimitry Andric     Name = Name.substr(wpos);
2480b57cec5SDimitry Andric     std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric     // Build the string for the operand
2510b57cec5SDimitry Andric     if (!Ops[Op.first].Constraints[Op.second].isNone())
2520b57cec5SDimitry Andric       PrintFatalError(
2530b57cec5SDimitry Andric         Rec->getLoc(), "Operand '" + Name + "' of '" + Rec->getName() +
2540b57cec5SDimitry Andric         "' cannot have multiple constraints!");
2550b57cec5SDimitry Andric     Ops[Op.first].Constraints[Op.second] =
2560b57cec5SDimitry Andric     CGIOperandList::ConstraintInfo::getEarlyClobber();
2570b57cec5SDimitry Andric     return;
2580b57cec5SDimitry Andric   }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric   // Only other constraint is "TIED_TO" for now.
261*5f7ddb14SDimitry Andric   StringRef::size_type pos = CStr.find_first_of('=');
262*5f7ddb14SDimitry Andric   if (pos == StringRef::npos)
2630b57cec5SDimitry Andric     PrintFatalError(
2640b57cec5SDimitry Andric       Rec->getLoc(), "Unrecognized constraint '" + CStr +
2650b57cec5SDimitry Andric       "' in '" + Rec->getName() + "'");
2660b57cec5SDimitry Andric   start = CStr.find_first_not_of(" \t");
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   // TIED_TO: $src1 = $dst
2690b57cec5SDimitry Andric   wpos = CStr.find_first_of(" \t", start);
270*5f7ddb14SDimitry Andric   if (wpos == StringRef::npos || wpos > pos)
2710b57cec5SDimitry Andric     PrintFatalError(
2720b57cec5SDimitry Andric       Rec->getLoc(), "Illegal format for tied-to constraint in '" +
2730b57cec5SDimitry Andric       Rec->getName() + "': '" + CStr + "'");
274*5f7ddb14SDimitry Andric   StringRef LHSOpName = CStr.substr(start, wpos - start);
2750b57cec5SDimitry Andric   std::pair<unsigned,unsigned> LHSOp = Ops.ParseOperandName(LHSOpName, false);
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   wpos = CStr.find_first_not_of(" \t", pos + 1);
278*5f7ddb14SDimitry Andric   if (wpos == StringRef::npos)
2790b57cec5SDimitry Andric     PrintFatalError(
2800b57cec5SDimitry Andric       Rec->getLoc(), "Illegal format for tied-to constraint: '" + CStr + "'");
2810b57cec5SDimitry Andric 
282*5f7ddb14SDimitry Andric   StringRef RHSOpName = CStr.substr(wpos);
2830b57cec5SDimitry Andric   std::pair<unsigned,unsigned> RHSOp = Ops.ParseOperandName(RHSOpName, false);
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   // Sort the operands into order, which should put the output one
2860b57cec5SDimitry Andric   // first. But keep the original order, for use in diagnostics.
2870b57cec5SDimitry Andric   bool FirstIsDest = (LHSOp < RHSOp);
2880b57cec5SDimitry Andric   std::pair<unsigned,unsigned> DestOp = (FirstIsDest ? LHSOp : RHSOp);
2890b57cec5SDimitry Andric   StringRef DestOpName = (FirstIsDest ? LHSOpName : RHSOpName);
2900b57cec5SDimitry Andric   std::pair<unsigned,unsigned> SrcOp = (FirstIsDest ? RHSOp : LHSOp);
2910b57cec5SDimitry Andric   StringRef SrcOpName = (FirstIsDest ? RHSOpName : LHSOpName);
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   // Ensure one operand is a def and the other is a use.
2940b57cec5SDimitry Andric   if (DestOp.first >= Ops.NumDefs)
2950b57cec5SDimitry Andric     PrintFatalError(
2960b57cec5SDimitry Andric       Rec->getLoc(), "Input operands '" + LHSOpName + "' and '" + RHSOpName +
2970b57cec5SDimitry Andric       "' of '" + Rec->getName() + "' cannot be tied!");
2980b57cec5SDimitry Andric   if (SrcOp.first < Ops.NumDefs)
2990b57cec5SDimitry Andric     PrintFatalError(
3000b57cec5SDimitry Andric       Rec->getLoc(), "Output operands '" + LHSOpName + "' and '" + RHSOpName +
3010b57cec5SDimitry Andric       "' of '" + Rec->getName() + "' cannot be tied!");
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric   // The constraint has to go on the operand with higher index, i.e.
3040b57cec5SDimitry Andric   // the source one. Check there isn't another constraint there
3050b57cec5SDimitry Andric   // already.
3060b57cec5SDimitry Andric   if (!Ops[SrcOp.first].Constraints[SrcOp.second].isNone())
3070b57cec5SDimitry Andric     PrintFatalError(
3080b57cec5SDimitry Andric       Rec->getLoc(), "Operand '" + SrcOpName + "' of '" + Rec->getName() +
3090b57cec5SDimitry Andric       "' cannot have multiple constraints!");
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric   unsigned DestFlatOpNo = Ops.getFlattenedOperandNumber(DestOp);
3120b57cec5SDimitry Andric   auto NewConstraint = CGIOperandList::ConstraintInfo::getTied(DestFlatOpNo);
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   // Check that the earlier operand is not the target of another tie
3150b57cec5SDimitry Andric   // before making it the target of this one.
3160b57cec5SDimitry Andric   for (const CGIOperandList::OperandInfo &Op : Ops) {
3170b57cec5SDimitry Andric     for (unsigned i = 0; i < Op.MINumOperands; i++)
3180b57cec5SDimitry Andric       if (Op.Constraints[i] == NewConstraint)
3190b57cec5SDimitry Andric         PrintFatalError(
3200b57cec5SDimitry Andric           Rec->getLoc(), "Operand '" + DestOpName + "' of '" + Rec->getName() +
3210b57cec5SDimitry Andric           "' cannot have multiple operands tied to it!");
3220b57cec5SDimitry Andric   }
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   Ops[SrcOp.first].Constraints[SrcOp.second] = NewConstraint;
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric 
ParseConstraints(StringRef CStr,CGIOperandList & Ops,Record * Rec)327*5f7ddb14SDimitry Andric static void ParseConstraints(StringRef CStr, CGIOperandList &Ops, Record *Rec) {
3280b57cec5SDimitry Andric   if (CStr.empty()) return;
3290b57cec5SDimitry Andric 
330*5f7ddb14SDimitry Andric   StringRef delims(",");
331*5f7ddb14SDimitry Andric   StringRef::size_type bidx, eidx;
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   bidx = CStr.find_first_not_of(delims);
334*5f7ddb14SDimitry Andric   while (bidx != StringRef::npos) {
3350b57cec5SDimitry Andric     eidx = CStr.find_first_of(delims, bidx);
336*5f7ddb14SDimitry Andric     if (eidx == StringRef::npos)
337*5f7ddb14SDimitry Andric       eidx = CStr.size();
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric     ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops, Rec);
3400b57cec5SDimitry Andric     bidx = CStr.find_first_not_of(delims, eidx);
3410b57cec5SDimitry Andric   }
3420b57cec5SDimitry Andric }
3430b57cec5SDimitry Andric 
ProcessDisableEncoding(StringRef DisableEncoding)344*5f7ddb14SDimitry Andric void CGIOperandList::ProcessDisableEncoding(StringRef DisableEncoding) {
3450b57cec5SDimitry Andric   while (1) {
346*5f7ddb14SDimitry Andric     StringRef OpName;
347*5f7ddb14SDimitry Andric     std::tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
3480b57cec5SDimitry Andric     if (OpName.empty()) break;
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric     // Figure out which operand this is.
3510b57cec5SDimitry Andric     std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric     // Mark the operand as not-to-be encoded.
3540b57cec5SDimitry Andric     if (Op.second >= OperandList[Op.first].DoNotEncode.size())
3550b57cec5SDimitry Andric       OperandList[Op.first].DoNotEncode.resize(Op.second+1);
3560b57cec5SDimitry Andric     OperandList[Op.first].DoNotEncode[Op.second] = true;
3570b57cec5SDimitry Andric   }
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric }
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3620b57cec5SDimitry Andric // CodeGenInstruction Implementation
3630b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3640b57cec5SDimitry Andric 
CodeGenInstruction(Record * R)3650b57cec5SDimitry Andric CodeGenInstruction::CodeGenInstruction(Record *R)
3660b57cec5SDimitry Andric   : TheDef(R), Operands(R), InferredFrom(nullptr) {
3670b57cec5SDimitry Andric   Namespace = R->getValueAsString("Namespace");
3685ffd83dbSDimitry Andric   AsmString = std::string(R->getValueAsString("AsmString"));
3690b57cec5SDimitry Andric 
3708bcb0991SDimitry Andric   isPreISelOpcode = R->getValueAsBit("isPreISelOpcode");
3710b57cec5SDimitry Andric   isReturn     = R->getValueAsBit("isReturn");
3720b57cec5SDimitry Andric   isEHScopeReturn = R->getValueAsBit("isEHScopeReturn");
3730b57cec5SDimitry Andric   isBranch     = R->getValueAsBit("isBranch");
3740b57cec5SDimitry Andric   isIndirectBranch = R->getValueAsBit("isIndirectBranch");
3750b57cec5SDimitry Andric   isCompare    = R->getValueAsBit("isCompare");
3760b57cec5SDimitry Andric   isMoveImm    = R->getValueAsBit("isMoveImm");
3770b57cec5SDimitry Andric   isMoveReg    = R->getValueAsBit("isMoveReg");
3780b57cec5SDimitry Andric   isBitcast    = R->getValueAsBit("isBitcast");
3790b57cec5SDimitry Andric   isSelect     = R->getValueAsBit("isSelect");
3800b57cec5SDimitry Andric   isBarrier    = R->getValueAsBit("isBarrier");
3810b57cec5SDimitry Andric   isCall       = R->getValueAsBit("isCall");
3820b57cec5SDimitry Andric   isAdd        = R->getValueAsBit("isAdd");
3830b57cec5SDimitry Andric   isTrap       = R->getValueAsBit("isTrap");
3840b57cec5SDimitry Andric   canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
3850b57cec5SDimitry Andric   isPredicable = !R->getValueAsBit("isUnpredicable") && (
3860b57cec5SDimitry Andric       Operands.isPredicable || R->getValueAsBit("isPredicable"));
3870b57cec5SDimitry Andric   isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
3880b57cec5SDimitry Andric   isCommutable = R->getValueAsBit("isCommutable");
3890b57cec5SDimitry Andric   isTerminator = R->getValueAsBit("isTerminator");
3900b57cec5SDimitry Andric   isReMaterializable = R->getValueAsBit("isReMaterializable");
3910b57cec5SDimitry Andric   hasDelaySlot = R->getValueAsBit("hasDelaySlot");
3920b57cec5SDimitry Andric   usesCustomInserter = R->getValueAsBit("usesCustomInserter");
3930b57cec5SDimitry Andric   hasPostISelHook = R->getValueAsBit("hasPostISelHook");
3940b57cec5SDimitry Andric   hasCtrlDep   = R->getValueAsBit("hasCtrlDep");
3950b57cec5SDimitry Andric   isNotDuplicable = R->getValueAsBit("isNotDuplicable");
3960b57cec5SDimitry Andric   isRegSequence = R->getValueAsBit("isRegSequence");
3970b57cec5SDimitry Andric   isExtractSubreg = R->getValueAsBit("isExtractSubreg");
3980b57cec5SDimitry Andric   isInsertSubreg = R->getValueAsBit("isInsertSubreg");
3990b57cec5SDimitry Andric   isConvergent = R->getValueAsBit("isConvergent");
4000b57cec5SDimitry Andric   hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo");
4010b57cec5SDimitry Andric   FastISelShouldIgnore = R->getValueAsBit("FastISelShouldIgnore");
4020b57cec5SDimitry Andric   variadicOpsAreDefs = R->getValueAsBit("variadicOpsAreDefs");
403480093f4SDimitry Andric   isAuthenticated = R->getValueAsBit("isAuthenticated");
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric   bool Unset;
4060b57cec5SDimitry Andric   mayLoad      = R->getValueAsBitOrUnset("mayLoad", Unset);
4070b57cec5SDimitry Andric   mayLoad_Unset = Unset;
4080b57cec5SDimitry Andric   mayStore     = R->getValueAsBitOrUnset("mayStore", Unset);
4090b57cec5SDimitry Andric   mayStore_Unset = Unset;
4100b57cec5SDimitry Andric   mayRaiseFPException = R->getValueAsBit("mayRaiseFPException");
4110b57cec5SDimitry Andric   hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset);
4120b57cec5SDimitry Andric   hasSideEffects_Unset = Unset;
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric   isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
4150b57cec5SDimitry Andric   hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
4160b57cec5SDimitry Andric   hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
4170b57cec5SDimitry Andric   isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
4180b57cec5SDimitry Andric   isPseudo = R->getValueAsBit("isPseudo");
4190b57cec5SDimitry Andric   ImplicitDefs = R->getValueAsListOfDefs("Defs");
4200b57cec5SDimitry Andric   ImplicitUses = R->getValueAsListOfDefs("Uses");
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric   // This flag is only inferred from the pattern.
4230b57cec5SDimitry Andric   hasChain = false;
4240b57cec5SDimitry Andric   hasChain_Inferred = false;
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric   // Parse Constraints.
427*5f7ddb14SDimitry Andric   ParseConstraints(R->getValueAsString("Constraints"), Operands, R);
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric   // Parse the DisableEncoding field.
4305ffd83dbSDimitry Andric   Operands.ProcessDisableEncoding(
431*5f7ddb14SDimitry Andric       R->getValueAsString("DisableEncoding"));
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric   // First check for a ComplexDeprecationPredicate.
4340b57cec5SDimitry Andric   if (R->getValue("ComplexDeprecationPredicate")) {
4350b57cec5SDimitry Andric     HasComplexDeprecationPredicate = true;
4365ffd83dbSDimitry Andric     DeprecatedReason =
4375ffd83dbSDimitry Andric         std::string(R->getValueAsString("ComplexDeprecationPredicate"));
4380b57cec5SDimitry Andric   } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) {
4390b57cec5SDimitry Andric     // Check if we have a Subtarget feature mask.
4400b57cec5SDimitry Andric     HasComplexDeprecationPredicate = false;
4410b57cec5SDimitry Andric     DeprecatedReason = Dep->getValue()->getAsString();
4420b57cec5SDimitry Andric   } else {
4430b57cec5SDimitry Andric     // This instruction isn't deprecated.
4440b57cec5SDimitry Andric     HasComplexDeprecationPredicate = false;
4450b57cec5SDimitry Andric     DeprecatedReason = "";
4460b57cec5SDimitry Andric   }
4470b57cec5SDimitry Andric }
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
4500b57cec5SDimitry Andric /// implicit def and it has a known VT, return the VT, otherwise return
4510b57cec5SDimitry Andric /// MVT::Other.
4520b57cec5SDimitry Andric MVT::SimpleValueType CodeGenInstruction::
HasOneImplicitDefWithKnownVT(const CodeGenTarget & TargetInfo) const4530b57cec5SDimitry Andric HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
4540b57cec5SDimitry Andric   if (ImplicitDefs.empty()) return MVT::Other;
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric   // Check to see if the first implicit def has a resolvable type.
4570b57cec5SDimitry Andric   Record *FirstImplicitDef = ImplicitDefs[0];
4580b57cec5SDimitry Andric   assert(FirstImplicitDef->isSubClassOf("Register"));
4590b57cec5SDimitry Andric   const std::vector<ValueTypeByHwMode> &RegVTs =
4600b57cec5SDimitry Andric     TargetInfo.getRegisterVTs(FirstImplicitDef);
4610b57cec5SDimitry Andric   if (RegVTs.size() == 1 && RegVTs[0].isSimple())
4620b57cec5SDimitry Andric     return RegVTs[0].getSimple().SimpleTy;
4630b57cec5SDimitry Andric   return MVT::Other;
4640b57cec5SDimitry Andric }
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric /// FlattenAsmStringVariants - Flatten the specified AsmString to only
4680b57cec5SDimitry Andric /// include text from the specified variant, returning the new string.
4690b57cec5SDimitry Andric std::string CodeGenInstruction::
FlattenAsmStringVariants(StringRef Cur,unsigned Variant)4700b57cec5SDimitry Andric FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
471af732203SDimitry Andric   std::string Res;
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric   for (;;) {
4740b57cec5SDimitry Andric     // Find the start of the next variant string.
4750b57cec5SDimitry Andric     size_t VariantsStart = 0;
4760b57cec5SDimitry Andric     for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
4770b57cec5SDimitry Andric       if (Cur[VariantsStart] == '{' &&
4780b57cec5SDimitry Andric           (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
4790b57cec5SDimitry Andric                                   Cur[VariantsStart-1] != '\\')))
4800b57cec5SDimitry Andric         break;
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric     // Add the prefix to the result.
4830b57cec5SDimitry Andric     Res += Cur.slice(0, VariantsStart);
4840b57cec5SDimitry Andric     if (VariantsStart == Cur.size())
4850b57cec5SDimitry Andric       break;
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric     ++VariantsStart; // Skip the '{'.
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric     // Scan to the end of the variants string.
4900b57cec5SDimitry Andric     size_t VariantsEnd = VariantsStart;
4910b57cec5SDimitry Andric     unsigned NestedBraces = 1;
4920b57cec5SDimitry Andric     for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
4930b57cec5SDimitry Andric       if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
4940b57cec5SDimitry Andric         if (--NestedBraces == 0)
4950b57cec5SDimitry Andric           break;
4960b57cec5SDimitry Andric       } else if (Cur[VariantsEnd] == '{')
4970b57cec5SDimitry Andric         ++NestedBraces;
4980b57cec5SDimitry Andric     }
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric     // Select the Nth variant (or empty).
5010b57cec5SDimitry Andric     StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
5020b57cec5SDimitry Andric     for (unsigned i = 0; i != Variant; ++i)
5030b57cec5SDimitry Andric       Selection = Selection.split('|').second;
5040b57cec5SDimitry Andric     Res += Selection.split('|').first;
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric     assert(VariantsEnd != Cur.size() &&
5070b57cec5SDimitry Andric            "Unterminated variants in assembly string!");
5080b57cec5SDimitry Andric     Cur = Cur.substr(VariantsEnd + 1);
5090b57cec5SDimitry Andric   }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric   return Res;
5120b57cec5SDimitry Andric }
5130b57cec5SDimitry Andric 
isOperandImpl(unsigned i,StringRef PropertyName) const514480093f4SDimitry Andric bool CodeGenInstruction::isOperandImpl(unsigned i,
515480093f4SDimitry Andric                                        StringRef PropertyName) const {
516480093f4SDimitry Andric   DagInit *ConstraintList = TheDef->getValueAsDag("InOperandList");
517480093f4SDimitry Andric   if (!ConstraintList || i >= ConstraintList->getNumArgs())
5180b57cec5SDimitry Andric     return false;
519480093f4SDimitry Andric 
520480093f4SDimitry Andric   DefInit *Constraint = dyn_cast<DefInit>(ConstraintList->getArg(i));
521480093f4SDimitry Andric   if (!Constraint)
522480093f4SDimitry Andric     return false;
523480093f4SDimitry Andric 
524480093f4SDimitry Andric   return Constraint->getDef()->isSubClassOf("TypedOperand") &&
525480093f4SDimitry Andric          Constraint->getDef()->getValueAsBit(PropertyName);
5260b57cec5SDimitry Andric }
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5290b57cec5SDimitry Andric /// CodeGenInstAlias Implementation
5300b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
5330b57cec5SDimitry Andric /// constructor.  It checks if an argument in an InstAlias pattern matches
5340b57cec5SDimitry Andric /// the corresponding operand of the instruction.  It returns true on a
5350b57cec5SDimitry Andric /// successful match, with ResOp set to the result operand to be used.
tryAliasOpMatch(DagInit * Result,unsigned AliasOpNo,Record * InstOpRec,bool hasSubOps,ArrayRef<SMLoc> Loc,CodeGenTarget & T,ResultOperand & ResOp)5360b57cec5SDimitry Andric bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
5370b57cec5SDimitry Andric                                        Record *InstOpRec, bool hasSubOps,
5380b57cec5SDimitry Andric                                        ArrayRef<SMLoc> Loc, CodeGenTarget &T,
5390b57cec5SDimitry Andric                                        ResultOperand &ResOp) {
5400b57cec5SDimitry Andric   Init *Arg = Result->getArg(AliasOpNo);
5410b57cec5SDimitry Andric   DefInit *ADI = dyn_cast<DefInit>(Arg);
5420b57cec5SDimitry Andric   Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric   if (ADI && ADI->getDef() == InstOpRec) {
5450b57cec5SDimitry Andric     // If the operand is a record, it must have a name, and the record type
5460b57cec5SDimitry Andric     // must match up with the instruction's argument type.
5470b57cec5SDimitry Andric     if (!Result->getArgName(AliasOpNo))
5480b57cec5SDimitry Andric       PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
5490b57cec5SDimitry Andric                            " must have a name!");
5505ffd83dbSDimitry Andric     ResOp = ResultOperand(std::string(Result->getArgNameStr(AliasOpNo)),
5515ffd83dbSDimitry Andric                           ResultRecord);
5520b57cec5SDimitry Andric     return true;
5530b57cec5SDimitry Andric   }
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric   // For register operands, the source register class can be a subclass
5560b57cec5SDimitry Andric   // of the instruction register class, not just an exact match.
5570b57cec5SDimitry Andric   if (InstOpRec->isSubClassOf("RegisterOperand"))
5580b57cec5SDimitry Andric     InstOpRec = InstOpRec->getValueAsDef("RegClass");
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric   if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand"))
5610b57cec5SDimitry Andric     ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit();
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric   if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
5640b57cec5SDimitry Andric     if (!InstOpRec->isSubClassOf("RegisterClass"))
5650b57cec5SDimitry Andric       return false;
5660b57cec5SDimitry Andric     if (!T.getRegisterClass(InstOpRec)
5670b57cec5SDimitry Andric               .hasSubClass(&T.getRegisterClass(ADI->getDef())))
5680b57cec5SDimitry Andric       return false;
5695ffd83dbSDimitry Andric     ResOp = ResultOperand(std::string(Result->getArgNameStr(AliasOpNo)),
5705ffd83dbSDimitry Andric                           ResultRecord);
5710b57cec5SDimitry Andric     return true;
5720b57cec5SDimitry Andric   }
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric   // Handle explicit registers.
5750b57cec5SDimitry Andric   if (ADI && ADI->getDef()->isSubClassOf("Register")) {
5760b57cec5SDimitry Andric     if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
5770b57cec5SDimitry Andric       DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
5780b57cec5SDimitry Andric       // The operand info should only have a single (register) entry. We
5790b57cec5SDimitry Andric       // want the register class of it.
5800b57cec5SDimitry Andric       InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
5810b57cec5SDimitry Andric     }
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric     if (!InstOpRec->isSubClassOf("RegisterClass"))
5840b57cec5SDimitry Andric       return false;
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric     if (!T.getRegisterClass(InstOpRec)
5870b57cec5SDimitry Andric         .contains(T.getRegBank().getReg(ADI->getDef())))
5880b57cec5SDimitry Andric       PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
5890b57cec5SDimitry Andric                       " is not a member of the " + InstOpRec->getName() +
5900b57cec5SDimitry Andric                       " register class!");
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric     if (Result->getArgName(AliasOpNo))
5930b57cec5SDimitry Andric       PrintFatalError(Loc, "result fixed register argument must "
5940b57cec5SDimitry Andric                       "not have a name!");
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric     ResOp = ResultOperand(ResultRecord);
5970b57cec5SDimitry Andric     return true;
5980b57cec5SDimitry Andric   }
5990b57cec5SDimitry Andric 
6000b57cec5SDimitry Andric   // Handle "zero_reg" for optional def operands.
6010b57cec5SDimitry Andric   if (ADI && ADI->getDef()->getName() == "zero_reg") {
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric     // Check if this is an optional def.
6040b57cec5SDimitry Andric     // Tied operands where the source is a sub-operand of a complex operand
6050b57cec5SDimitry Andric     // need to represent both operands in the alias destination instruction.
6060b57cec5SDimitry Andric     // Allow zero_reg for the tied portion. This can and should go away once
6070b57cec5SDimitry Andric     // the MC representation of things doesn't use tied operands at all.
6080b57cec5SDimitry Andric     //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
6090b57cec5SDimitry Andric     //  throw TGError(Loc, "reg0 used for result that is not an "
6100b57cec5SDimitry Andric     //                "OptionalDefOperand!");
6110b57cec5SDimitry Andric 
6120b57cec5SDimitry Andric     ResOp = ResultOperand(static_cast<Record*>(nullptr));
6130b57cec5SDimitry Andric     return true;
6140b57cec5SDimitry Andric   }
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric   // Literal integers.
6170b57cec5SDimitry Andric   if (IntInit *II = dyn_cast<IntInit>(Arg)) {
6180b57cec5SDimitry Andric     if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
6190b57cec5SDimitry Andric       return false;
6200b57cec5SDimitry Andric     // Integer arguments can't have names.
6210b57cec5SDimitry Andric     if (Result->getArgName(AliasOpNo))
6220b57cec5SDimitry Andric       PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
6230b57cec5SDimitry Andric                       " must not have a name!");
6240b57cec5SDimitry Andric     ResOp = ResultOperand(II->getValue());
6250b57cec5SDimitry Andric     return true;
6260b57cec5SDimitry Andric   }
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric   // Bits<n> (also used for 0bxx literals)
6290b57cec5SDimitry Andric   if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
6300b57cec5SDimitry Andric     if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
6310b57cec5SDimitry Andric       return false;
6320b57cec5SDimitry Andric     if (!BI->isComplete())
6330b57cec5SDimitry Andric       return false;
6340b57cec5SDimitry Andric     // Convert the bits init to an integer and use that for the result.
6350b57cec5SDimitry Andric     IntInit *II =
6360b57cec5SDimitry Andric       dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get()));
6370b57cec5SDimitry Andric     if (!II)
6380b57cec5SDimitry Andric       return false;
6390b57cec5SDimitry Andric     ResOp = ResultOperand(II->getValue());
6400b57cec5SDimitry Andric     return true;
6410b57cec5SDimitry Andric   }
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric   // If both are Operands with the same MVT, allow the conversion. It's
6440b57cec5SDimitry Andric   // up to the user to make sure the values are appropriate, just like
6450b57cec5SDimitry Andric   // for isel Pat's.
6460b57cec5SDimitry Andric   if (InstOpRec->isSubClassOf("Operand") && ADI &&
6470b57cec5SDimitry Andric       ADI->getDef()->isSubClassOf("Operand")) {
6480b57cec5SDimitry Andric     // FIXME: What other attributes should we check here? Identical
6490b57cec5SDimitry Andric     // MIOperandInfo perhaps?
6500b57cec5SDimitry Andric     if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
6510b57cec5SDimitry Andric       return false;
6525ffd83dbSDimitry Andric     ResOp = ResultOperand(std::string(Result->getArgNameStr(AliasOpNo)),
6535ffd83dbSDimitry Andric                           ADI->getDef());
6540b57cec5SDimitry Andric     return true;
6550b57cec5SDimitry Andric   }
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric   return false;
6580b57cec5SDimitry Andric }
6590b57cec5SDimitry Andric 
getMINumOperands() const6600b57cec5SDimitry Andric unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
6610b57cec5SDimitry Andric   if (!isRecord())
6620b57cec5SDimitry Andric     return 1;
6630b57cec5SDimitry Andric 
6640b57cec5SDimitry Andric   Record *Rec = getRecord();
6650b57cec5SDimitry Andric   if (!Rec->isSubClassOf("Operand"))
6660b57cec5SDimitry Andric     return 1;
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric   DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
6690b57cec5SDimitry Andric   if (MIOpInfo->getNumArgs() == 0) {
6700b57cec5SDimitry Andric     // Unspecified, so it defaults to 1
6710b57cec5SDimitry Andric     return 1;
6720b57cec5SDimitry Andric   }
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric   return MIOpInfo->getNumArgs();
6750b57cec5SDimitry Andric }
6760b57cec5SDimitry Andric 
CodeGenInstAlias(Record * R,CodeGenTarget & T)6770b57cec5SDimitry Andric CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T)
6780b57cec5SDimitry Andric     : TheDef(R) {
6790b57cec5SDimitry Andric   Result = R->getValueAsDag("ResultInst");
6805ffd83dbSDimitry Andric   AsmString = std::string(R->getValueAsString("AsmString"));
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric   // Verify that the root of the result is an instruction.
6830b57cec5SDimitry Andric   DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
6840b57cec5SDimitry Andric   if (!DI || !DI->getDef()->isSubClassOf("Instruction"))
6850b57cec5SDimitry Andric     PrintFatalError(R->getLoc(),
6860b57cec5SDimitry Andric                     "result of inst alias should be an instruction");
6870b57cec5SDimitry Andric 
6880b57cec5SDimitry Andric   ResultInst = &T.getInstruction(DI->getDef());
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric   // NameClass - If argument names are repeated, we need to verify they have
6910b57cec5SDimitry Andric   // the same class.
6920b57cec5SDimitry Andric   StringMap<Record*> NameClass;
6930b57cec5SDimitry Andric   for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
6940b57cec5SDimitry Andric     DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
6950b57cec5SDimitry Andric     if (!ADI || !Result->getArgName(i))
6960b57cec5SDimitry Andric       continue;
6970b57cec5SDimitry Andric     // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
6980b57cec5SDimitry Andric     // $foo can exist multiple times in the result list, but it must have the
6990b57cec5SDimitry Andric     // same type.
7000b57cec5SDimitry Andric     Record *&Entry = NameClass[Result->getArgNameStr(i)];
7010b57cec5SDimitry Andric     if (Entry && Entry != ADI->getDef())
7020b57cec5SDimitry Andric       PrintFatalError(R->getLoc(), "result value $" + Result->getArgNameStr(i) +
7030b57cec5SDimitry Andric                       " is both " + Entry->getName() + " and " +
7040b57cec5SDimitry Andric                       ADI->getDef()->getName() + "!");
7050b57cec5SDimitry Andric     Entry = ADI->getDef();
7060b57cec5SDimitry Andric   }
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric   // Decode and validate the arguments of the result.
7090b57cec5SDimitry Andric   unsigned AliasOpNo = 0;
7100b57cec5SDimitry Andric   for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
7110b57cec5SDimitry Andric 
7120b57cec5SDimitry Andric     // Tied registers don't have an entry in the result dag unless they're part
7130b57cec5SDimitry Andric     // of a complex operand, in which case we include them anyways, as we
7140b57cec5SDimitry Andric     // don't have any other way to specify the whole operand.
7150b57cec5SDimitry Andric     if (ResultInst->Operands[i].MINumOperands == 1 &&
7160b57cec5SDimitry Andric         ResultInst->Operands[i].getTiedRegister() != -1) {
7170b57cec5SDimitry Andric       // Tied operands of different RegisterClass should be explicit within an
7180b57cec5SDimitry Andric       // instruction's syntax and so cannot be skipped.
7190b57cec5SDimitry Andric       int TiedOpNum = ResultInst->Operands[i].getTiedRegister();
7200b57cec5SDimitry Andric       if (ResultInst->Operands[i].Rec->getName() ==
7210b57cec5SDimitry Andric           ResultInst->Operands[TiedOpNum].Rec->getName())
7220b57cec5SDimitry Andric         continue;
7230b57cec5SDimitry Andric     }
7240b57cec5SDimitry Andric 
7250b57cec5SDimitry Andric     if (AliasOpNo >= Result->getNumArgs())
7260b57cec5SDimitry Andric       PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric     Record *InstOpRec = ResultInst->Operands[i].Rec;
7290b57cec5SDimitry Andric     unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
7300b57cec5SDimitry Andric     ResultOperand ResOp(static_cast<int64_t>(0));
7310b57cec5SDimitry Andric     if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
7320b57cec5SDimitry Andric                         R->getLoc(), T, ResOp)) {
7330b57cec5SDimitry Andric       // If this is a simple operand, or a complex operand with a custom match
7340b57cec5SDimitry Andric       // class, then we can match is verbatim.
7350b57cec5SDimitry Andric       if (NumSubOps == 1 ||
7360b57cec5SDimitry Andric           (InstOpRec->getValue("ParserMatchClass") &&
7370b57cec5SDimitry Andric            InstOpRec->getValueAsDef("ParserMatchClass")
7380b57cec5SDimitry Andric              ->getValueAsString("Name") != "Imm")) {
7390b57cec5SDimitry Andric         ResultOperands.push_back(ResOp);
7400b57cec5SDimitry Andric         ResultInstOperandIndex.push_back(std::make_pair(i, -1));
7410b57cec5SDimitry Andric         ++AliasOpNo;
7420b57cec5SDimitry Andric 
7430b57cec5SDimitry Andric       // Otherwise, we need to match each of the suboperands individually.
7440b57cec5SDimitry Andric       } else {
7450b57cec5SDimitry Andric          DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
7460b57cec5SDimitry Andric          for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
7470b57cec5SDimitry Andric           Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
7480b57cec5SDimitry Andric 
7490b57cec5SDimitry Andric           // Take care to instantiate each of the suboperands with the correct
7500b57cec5SDimitry Andric           // nomenclature: $foo.bar
7510b57cec5SDimitry Andric           ResultOperands.emplace_back(
7520b57cec5SDimitry Andric             Result->getArgName(AliasOpNo)->getAsUnquotedString() + "." +
7530b57cec5SDimitry Andric             MIOI->getArgName(SubOp)->getAsUnquotedString(), SubRec);
7540b57cec5SDimitry Andric           ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
7550b57cec5SDimitry Andric          }
7560b57cec5SDimitry Andric          ++AliasOpNo;
7570b57cec5SDimitry Andric       }
7580b57cec5SDimitry Andric       continue;
7590b57cec5SDimitry Andric     }
7600b57cec5SDimitry Andric 
7610b57cec5SDimitry Andric     // If the argument did not match the instruction operand, and the operand
7620b57cec5SDimitry Andric     // is composed of multiple suboperands, try matching the suboperands.
7630b57cec5SDimitry Andric     if (NumSubOps > 1) {
7640b57cec5SDimitry Andric       DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
7650b57cec5SDimitry Andric       for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
7660b57cec5SDimitry Andric         if (AliasOpNo >= Result->getNumArgs())
7670b57cec5SDimitry Andric           PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
7680b57cec5SDimitry Andric         Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
7690b57cec5SDimitry Andric         if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
7700b57cec5SDimitry Andric                             R->getLoc(), T, ResOp)) {
7710b57cec5SDimitry Andric           ResultOperands.push_back(ResOp);
7720b57cec5SDimitry Andric           ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
7730b57cec5SDimitry Andric           ++AliasOpNo;
7740b57cec5SDimitry Andric         } else {
7750b57cec5SDimitry Andric           PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
7760b57cec5SDimitry Andric                         " does not match instruction operand class " +
7770b57cec5SDimitry Andric                         (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
7780b57cec5SDimitry Andric         }
7790b57cec5SDimitry Andric       }
7800b57cec5SDimitry Andric       continue;
7810b57cec5SDimitry Andric     }
7820b57cec5SDimitry Andric     PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
7830b57cec5SDimitry Andric                     " does not match instruction operand class " +
7840b57cec5SDimitry Andric                     InstOpRec->getName());
7850b57cec5SDimitry Andric   }
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric   if (AliasOpNo != Result->getNumArgs())
7880b57cec5SDimitry Andric     PrintFatalError(R->getLoc(), "too many operands for instruction!");
7890b57cec5SDimitry Andric }
790