1f22ef01cSRoman Divacky //===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This file implements the CodeGenInstruction class.
11f22ef01cSRoman Divacky //
12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
13f22ef01cSRoman Divacky
14f22ef01cSRoman Divacky #include "CodeGenInstruction.h"
15f22ef01cSRoman Divacky #include "CodeGenTarget.h"
16139f7f9bSDimitry Andric #include "llvm/ADT/STLExtras.h"
17f22ef01cSRoman Divacky #include "llvm/ADT/StringExtras.h"
182754fe60SDimitry Andric #include "llvm/ADT/StringMap.h"
19139f7f9bSDimitry Andric #include "llvm/TableGen/Error.h"
20139f7f9bSDimitry Andric #include "llvm/TableGen/Record.h"
21f22ef01cSRoman Divacky #include <set>
22f22ef01cSRoman Divacky using namespace llvm;
23f22ef01cSRoman Divacky
242754fe60SDimitry Andric //===----------------------------------------------------------------------===//
252754fe60SDimitry Andric // CGIOperandList Implementation
262754fe60SDimitry Andric //===----------------------------------------------------------------------===//
27f22ef01cSRoman Divacky
CGIOperandList(Record * R)282754fe60SDimitry Andric CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
292754fe60SDimitry Andric isPredicable = false;
30f22ef01cSRoman Divacky hasOptionalDef = false;
31f22ef01cSRoman Divacky isVariadic = false;
32f22ef01cSRoman Divacky
33f22ef01cSRoman Divacky DagInit *OutDI = R->getValueAsDag("OutOperandList");
34f22ef01cSRoman Divacky
353861d79fSDimitry Andric if (DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) {
36f22ef01cSRoman Divacky if (Init->getDef()->getName() != "outs")
373861d79fSDimitry Andric PrintFatalError(R->getName() + ": invalid def name for output list: use 'outs'");
38f22ef01cSRoman Divacky } else
393861d79fSDimitry Andric PrintFatalError(R->getName() + ": invalid output list: use 'outs'");
40f22ef01cSRoman Divacky
41f22ef01cSRoman Divacky NumDefs = OutDI->getNumArgs();
42f22ef01cSRoman Divacky
43f22ef01cSRoman Divacky DagInit *InDI = R->getValueAsDag("InOperandList");
443861d79fSDimitry Andric if (DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) {
45f22ef01cSRoman Divacky if (Init->getDef()->getName() != "ins")
463861d79fSDimitry Andric PrintFatalError(R->getName() + ": invalid def name for input list: use 'ins'");
47f22ef01cSRoman Divacky } else
483861d79fSDimitry Andric PrintFatalError(R->getName() + ": invalid input list: use 'ins'");
49f22ef01cSRoman Divacky
50f22ef01cSRoman Divacky unsigned MIOperandNo = 0;
51f22ef01cSRoman Divacky std::set<std::string> OperandNames;
523ca95b02SDimitry Andric unsigned e = InDI->getNumArgs() + OutDI->getNumArgs();
533ca95b02SDimitry Andric OperandList.reserve(e);
543ca95b02SDimitry Andric for (unsigned i = 0; i != e; ++i){
55f22ef01cSRoman Divacky Init *ArgInit;
56d88c1a5aSDimitry Andric StringRef ArgName;
57f22ef01cSRoman Divacky if (i < NumDefs) {
58f22ef01cSRoman Divacky ArgInit = OutDI->getArg(i);
59d88c1a5aSDimitry Andric ArgName = OutDI->getArgNameStr(i);
60f22ef01cSRoman Divacky } else {
61f22ef01cSRoman Divacky ArgInit = InDI->getArg(i-NumDefs);
62d88c1a5aSDimitry Andric ArgName = InDI->getArgNameStr(i-NumDefs);
63f22ef01cSRoman Divacky }
64f22ef01cSRoman Divacky
653861d79fSDimitry Andric DefInit *Arg = dyn_cast<DefInit>(ArgInit);
66f22ef01cSRoman Divacky if (!Arg)
673861d79fSDimitry Andric PrintFatalError("Illegal operand for the '" + R->getName() + "' instruction!");
68f22ef01cSRoman Divacky
69f22ef01cSRoman Divacky Record *Rec = Arg->getDef();
70f22ef01cSRoman Divacky std::string PrintMethod = "printOperand";
712754fe60SDimitry Andric std::string EncoderMethod;
7217a519f9SDimitry Andric std::string OperandType = "OPERAND_UNKNOWN";
7339d628a0SDimitry Andric std::string OperandNamespace = "MCOI";
74f22ef01cSRoman Divacky unsigned NumOps = 1;
7591bc56edSDimitry Andric DagInit *MIOpInfo = nullptr;
7617a519f9SDimitry Andric if (Rec->isSubClassOf("RegisterOperand")) {
77f22ef01cSRoman Divacky PrintMethod = Rec->getValueAsString("PrintMethod");
7839d628a0SDimitry Andric OperandType = Rec->getValueAsString("OperandType");
7939d628a0SDimitry Andric OperandNamespace = Rec->getValueAsString("OperandNamespace");
805517e702SDimitry Andric EncoderMethod = Rec->getValueAsString("EncoderMethod");
8117a519f9SDimitry Andric } else if (Rec->isSubClassOf("Operand")) {
8217a519f9SDimitry Andric PrintMethod = Rec->getValueAsString("PrintMethod");
8317a519f9SDimitry Andric OperandType = Rec->getValueAsString("OperandType");
847d523365SDimitry Andric OperandNamespace = Rec->getValueAsString("OperandNamespace");
852754fe60SDimitry Andric // If there is an explicit encoder method, use it.
862754fe60SDimitry Andric EncoderMethod = Rec->getValueAsString("EncoderMethod");
87f22ef01cSRoman Divacky MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
88f22ef01cSRoman Divacky
89f22ef01cSRoman Divacky // Verify that MIOpInfo has an 'ops' root value.
903861d79fSDimitry Andric if (!isa<DefInit>(MIOpInfo->getOperator()) ||
913861d79fSDimitry Andric cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
923861d79fSDimitry Andric PrintFatalError("Bad value for MIOperandInfo in operand '" + Rec->getName() +
933861d79fSDimitry Andric "'\n");
94f22ef01cSRoman Divacky
95f22ef01cSRoman Divacky // If we have MIOpInfo, then we have #operands equal to number of entries
96f22ef01cSRoman Divacky // in MIOperandInfo.
97f22ef01cSRoman Divacky if (unsigned NumArgs = MIOpInfo->getNumArgs())
98f22ef01cSRoman Divacky NumOps = NumArgs;
99f22ef01cSRoman Divacky
100f785676fSDimitry Andric if (Rec->isSubClassOf("PredicateOp"))
101f22ef01cSRoman Divacky isPredicable = true;
102f22ef01cSRoman Divacky else if (Rec->isSubClassOf("OptionalDefOperand"))
103f22ef01cSRoman Divacky hasOptionalDef = true;
104f22ef01cSRoman Divacky } else if (Rec->getName() == "variable_ops") {
105f22ef01cSRoman Divacky isVariadic = true;
106f22ef01cSRoman Divacky continue;
10717a519f9SDimitry Andric } else if (Rec->isSubClassOf("RegisterClass")) {
10817a519f9SDimitry Andric OperandType = "OPERAND_REGISTER";
10917a519f9SDimitry Andric } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
1103861d79fSDimitry Andric !Rec->isSubClassOf("unknown_class"))
1113861d79fSDimitry Andric PrintFatalError("Unknown operand class '" + Rec->getName() +
1123861d79fSDimitry Andric "' in '" + R->getName() + "' instruction!");
113f22ef01cSRoman Divacky
114f22ef01cSRoman Divacky // Check that the operand has a name and that it's unique.
115f22ef01cSRoman Divacky if (ArgName.empty())
11691bc56edSDimitry Andric PrintFatalError("In instruction '" + R->getName() + "', operand #" +
11791bc56edSDimitry Andric Twine(i) + " has no name!");
118f22ef01cSRoman Divacky if (!OperandNames.insert(ArgName).second)
11991bc56edSDimitry Andric PrintFatalError("In instruction '" + R->getName() + "', operand #" +
12091bc56edSDimitry Andric Twine(i) + " has the same name as a previous operand!");
121f22ef01cSRoman Divacky
12297bc6c73SDimitry Andric OperandList.emplace_back(Rec, ArgName, PrintMethod, EncoderMethod,
12397bc6c73SDimitry Andric OperandNamespace + "::" + OperandType, MIOperandNo,
12497bc6c73SDimitry Andric NumOps, MIOpInfo);
125f22ef01cSRoman Divacky MIOperandNo += NumOps;
126f22ef01cSRoman Divacky }
127f22ef01cSRoman Divacky
128f22ef01cSRoman Divacky
1292754fe60SDimitry Andric // Make sure the constraints list for each operand is large enough to hold
1302754fe60SDimitry Andric // constraint info, even if none is present.
1312cab237bSDimitry Andric for (OperandInfo &OpInfo : OperandList)
1322cab237bSDimitry Andric OpInfo.Constraints.resize(OpInfo.MINumOperands);
133f22ef01cSRoman Divacky }
1342754fe60SDimitry Andric
135f22ef01cSRoman Divacky
136f22ef01cSRoman Divacky /// getOperandNamed - Return the index of the operand with the specified
137f22ef01cSRoman Divacky /// non-empty name. If the instruction does not have an operand with the
1383861d79fSDimitry Andric /// specified name, abort.
139f22ef01cSRoman Divacky ///
getOperandNamed(StringRef Name) const1402754fe60SDimitry Andric unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
1412754fe60SDimitry Andric unsigned OpIdx;
1422754fe60SDimitry Andric if (hasOperandNamed(Name, OpIdx)) return OpIdx;
14391bc56edSDimitry Andric PrintFatalError("'" + TheDef->getName() +
14491bc56edSDimitry Andric "' does not have an operand named '$" + Name + "'!");
1452754fe60SDimitry Andric }
1462754fe60SDimitry Andric
1472754fe60SDimitry Andric /// hasOperandNamed - Query whether the instruction has an operand of the
1482754fe60SDimitry Andric /// given name. If so, return true and set OpIdx to the index of the
1492754fe60SDimitry Andric /// operand. Otherwise, return false.
hasOperandNamed(StringRef Name,unsigned & OpIdx) const1502754fe60SDimitry Andric bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
151f22ef01cSRoman Divacky assert(!Name.empty() && "Cannot search for operand with no name!");
152f22ef01cSRoman Divacky for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
1532754fe60SDimitry Andric if (OperandList[i].Name == Name) {
1542754fe60SDimitry Andric OpIdx = i;
1552754fe60SDimitry Andric return true;
1562754fe60SDimitry Andric }
1572754fe60SDimitry Andric return false;
158f22ef01cSRoman Divacky }
159f22ef01cSRoman Divacky
160f22ef01cSRoman Divacky std::pair<unsigned,unsigned>
ParseOperandName(const std::string & Op,bool AllowWholeOp)1612754fe60SDimitry Andric CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
162f22ef01cSRoman Divacky if (Op.empty() || Op[0] != '$')
1633861d79fSDimitry Andric PrintFatalError(TheDef->getName() + ": Illegal operand name: '" + Op + "'");
164f22ef01cSRoman Divacky
165f22ef01cSRoman Divacky std::string OpName = Op.substr(1);
166f22ef01cSRoman Divacky std::string SubOpName;
167f22ef01cSRoman Divacky
168f22ef01cSRoman Divacky // Check to see if this is $foo.bar.
169d88c1a5aSDimitry Andric std::string::size_type DotIdx = OpName.find_first_of('.');
170f22ef01cSRoman Divacky if (DotIdx != std::string::npos) {
171f22ef01cSRoman Divacky SubOpName = OpName.substr(DotIdx+1);
172f22ef01cSRoman Divacky if (SubOpName.empty())
1733861d79fSDimitry Andric PrintFatalError(TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'");
174f22ef01cSRoman Divacky OpName = OpName.substr(0, DotIdx);
175f22ef01cSRoman Divacky }
176f22ef01cSRoman Divacky
177f22ef01cSRoman Divacky unsigned OpIdx = getOperandNamed(OpName);
178f22ef01cSRoman Divacky
179f22ef01cSRoman Divacky if (SubOpName.empty()) { // If no suboperand name was specified:
180f22ef01cSRoman Divacky // If one was needed, throw.
181f22ef01cSRoman Divacky if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
182f22ef01cSRoman Divacky SubOpName.empty())
1833861d79fSDimitry Andric PrintFatalError(TheDef->getName() + ": Illegal to refer to"
1843861d79fSDimitry Andric " whole operand part of complex operand '" + Op + "'");
185f22ef01cSRoman Divacky
186f22ef01cSRoman Divacky // Otherwise, return the operand.
187f22ef01cSRoman Divacky return std::make_pair(OpIdx, 0U);
188f22ef01cSRoman Divacky }
189f22ef01cSRoman Divacky
190f22ef01cSRoman Divacky // Find the suboperand number involved.
191f22ef01cSRoman Divacky DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
19291bc56edSDimitry Andric if (!MIOpInfo)
1933861d79fSDimitry Andric PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
194f22ef01cSRoman Divacky
195f22ef01cSRoman Divacky // Find the operand with the right name.
196f22ef01cSRoman Divacky for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
197d88c1a5aSDimitry Andric if (MIOpInfo->getArgNameStr(i) == SubOpName)
198f22ef01cSRoman Divacky return std::make_pair(OpIdx, i);
199f22ef01cSRoman Divacky
200f22ef01cSRoman Divacky // Otherwise, didn't find it!
2013861d79fSDimitry Andric PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
202f785676fSDimitry Andric return std::make_pair(0U, 0U);
203f22ef01cSRoman Divacky }
204f22ef01cSRoman Divacky
ParseConstraint(const std::string & CStr,CGIOperandList & Ops,Record * Rec)205*b5893f02SDimitry Andric static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops,
206*b5893f02SDimitry Andric Record *Rec) {
2072754fe60SDimitry Andric // EARLY_CLOBBER: @early $reg
2082754fe60SDimitry Andric std::string::size_type wpos = CStr.find_first_of(" \t");
2092754fe60SDimitry Andric std::string::size_type start = CStr.find_first_not_of(" \t");
2102754fe60SDimitry Andric std::string Tok = CStr.substr(start, wpos - start);
2112754fe60SDimitry Andric if (Tok == "@earlyclobber") {
2122754fe60SDimitry Andric std::string Name = CStr.substr(wpos+1);
2132754fe60SDimitry Andric wpos = Name.find_first_not_of(" \t");
2142754fe60SDimitry Andric if (wpos == std::string::npos)
215*b5893f02SDimitry Andric PrintFatalError(
216*b5893f02SDimitry Andric Rec->getLoc(), "Illegal format for @earlyclobber constraint in '" +
217*b5893f02SDimitry Andric Rec->getName() + "': '" + CStr + "'");
2182754fe60SDimitry Andric Name = Name.substr(wpos);
2192754fe60SDimitry Andric std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
2202754fe60SDimitry Andric
2212754fe60SDimitry Andric // Build the string for the operand
2222754fe60SDimitry Andric if (!Ops[Op.first].Constraints[Op.second].isNone())
223*b5893f02SDimitry Andric PrintFatalError(
224*b5893f02SDimitry Andric Rec->getLoc(), "Operand '" + Name + "' of '" + Rec->getName() +
225*b5893f02SDimitry Andric "' cannot have multiple constraints!");
2262754fe60SDimitry Andric Ops[Op.first].Constraints[Op.second] =
2272754fe60SDimitry Andric CGIOperandList::ConstraintInfo::getEarlyClobber();
2282754fe60SDimitry Andric return;
2292754fe60SDimitry Andric }
2302754fe60SDimitry Andric
2312754fe60SDimitry Andric // Only other constraint is "TIED_TO" for now.
2322754fe60SDimitry Andric std::string::size_type pos = CStr.find_first_of('=');
233*b5893f02SDimitry Andric if (pos == std::string::npos)
234*b5893f02SDimitry Andric PrintFatalError(
235*b5893f02SDimitry Andric Rec->getLoc(), "Unrecognized constraint '" + CStr +
236*b5893f02SDimitry Andric "' in '" + Rec->getName() + "'");
2372754fe60SDimitry Andric start = CStr.find_first_not_of(" \t");
2382754fe60SDimitry Andric
2392754fe60SDimitry Andric // TIED_TO: $src1 = $dst
240*b5893f02SDimitry Andric wpos = CStr.find_first_of(" \t", start);
241*b5893f02SDimitry Andric if (wpos == std::string::npos || wpos > pos)
242*b5893f02SDimitry Andric PrintFatalError(
243*b5893f02SDimitry Andric Rec->getLoc(), "Illegal format for tied-to constraint in '" +
244*b5893f02SDimitry Andric Rec->getName() + "': '" + CStr + "'");
245*b5893f02SDimitry Andric std::string LHSOpName = StringRef(CStr).substr(start, wpos - start);
246*b5893f02SDimitry Andric std::pair<unsigned,unsigned> LHSOp = Ops.ParseOperandName(LHSOpName, false);
247*b5893f02SDimitry Andric
248*b5893f02SDimitry Andric wpos = CStr.find_first_not_of(" \t", pos + 1);
2492754fe60SDimitry Andric if (wpos == std::string::npos)
250*b5893f02SDimitry Andric PrintFatalError(
251*b5893f02SDimitry Andric Rec->getLoc(), "Illegal format for tied-to constraint: '" + CStr + "'");
2522754fe60SDimitry Andric
253*b5893f02SDimitry Andric std::string RHSOpName = StringRef(CStr).substr(wpos);
254*b5893f02SDimitry Andric std::pair<unsigned,unsigned> RHSOp = Ops.ParseOperandName(RHSOpName, false);
2552754fe60SDimitry Andric
256*b5893f02SDimitry Andric // Sort the operands into order, which should put the output one
257*b5893f02SDimitry Andric // first. But keep the original order, for use in diagnostics.
258*b5893f02SDimitry Andric bool FirstIsDest = (LHSOp < RHSOp);
259*b5893f02SDimitry Andric std::pair<unsigned,unsigned> DestOp = (FirstIsDest ? LHSOp : RHSOp);
260*b5893f02SDimitry Andric StringRef DestOpName = (FirstIsDest ? LHSOpName : RHSOpName);
261*b5893f02SDimitry Andric std::pair<unsigned,unsigned> SrcOp = (FirstIsDest ? RHSOp : LHSOp);
262*b5893f02SDimitry Andric StringRef SrcOpName = (FirstIsDest ? RHSOpName : LHSOpName);
2632754fe60SDimitry Andric
264*b5893f02SDimitry Andric // Ensure one operand is a def and the other is a use.
265*b5893f02SDimitry Andric if (DestOp.first >= Ops.NumDefs)
266*b5893f02SDimitry Andric PrintFatalError(
267*b5893f02SDimitry Andric Rec->getLoc(), "Input operands '" + LHSOpName + "' and '" + RHSOpName +
268*b5893f02SDimitry Andric "' of '" + Rec->getName() + "' cannot be tied!");
269*b5893f02SDimitry Andric if (SrcOp.first < Ops.NumDefs)
270*b5893f02SDimitry Andric PrintFatalError(
271*b5893f02SDimitry Andric Rec->getLoc(), "Output operands '" + LHSOpName + "' and '" + RHSOpName +
272*b5893f02SDimitry Andric "' of '" + Rec->getName() + "' cannot be tied!");
2732754fe60SDimitry Andric
274*b5893f02SDimitry Andric // The constraint has to go on the operand with higher index, i.e.
275*b5893f02SDimitry Andric // the source one. Check there isn't another constraint there
276*b5893f02SDimitry Andric // already.
277*b5893f02SDimitry Andric if (!Ops[SrcOp.first].Constraints[SrcOp.second].isNone())
278*b5893f02SDimitry Andric PrintFatalError(
279*b5893f02SDimitry Andric Rec->getLoc(), "Operand '" + SrcOpName + "' of '" + Rec->getName() +
2803861d79fSDimitry Andric "' cannot have multiple constraints!");
281*b5893f02SDimitry Andric
282*b5893f02SDimitry Andric unsigned DestFlatOpNo = Ops.getFlattenedOperandNumber(DestOp);
283*b5893f02SDimitry Andric auto NewConstraint = CGIOperandList::ConstraintInfo::getTied(DestFlatOpNo);
284*b5893f02SDimitry Andric
285*b5893f02SDimitry Andric // Check that the earlier operand is not the target of another tie
286*b5893f02SDimitry Andric // before making it the target of this one.
287*b5893f02SDimitry Andric for (const CGIOperandList::OperandInfo &Op : Ops) {
288*b5893f02SDimitry Andric for (unsigned i = 0; i < Op.MINumOperands; i++)
289*b5893f02SDimitry Andric if (Op.Constraints[i] == NewConstraint)
290*b5893f02SDimitry Andric PrintFatalError(
291*b5893f02SDimitry Andric Rec->getLoc(), "Operand '" + DestOpName + "' of '" + Rec->getName() +
292*b5893f02SDimitry Andric "' cannot have multiple operands tied to it!");
2932754fe60SDimitry Andric }
2942754fe60SDimitry Andric
295*b5893f02SDimitry Andric Ops[SrcOp.first].Constraints[SrcOp.second] = NewConstraint;
296*b5893f02SDimitry Andric }
297*b5893f02SDimitry Andric
ParseConstraints(const std::string & CStr,CGIOperandList & Ops,Record * Rec)298*b5893f02SDimitry Andric static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops,
299*b5893f02SDimitry Andric Record *Rec) {
3002754fe60SDimitry Andric if (CStr.empty()) return;
3012754fe60SDimitry Andric
3022754fe60SDimitry Andric const std::string delims(",");
3032754fe60SDimitry Andric std::string::size_type bidx, eidx;
3042754fe60SDimitry Andric
3052754fe60SDimitry Andric bidx = CStr.find_first_not_of(delims);
3062754fe60SDimitry Andric while (bidx != std::string::npos) {
3072754fe60SDimitry Andric eidx = CStr.find_first_of(delims, bidx);
3082754fe60SDimitry Andric if (eidx == std::string::npos)
3092754fe60SDimitry Andric eidx = CStr.length();
3102754fe60SDimitry Andric
311*b5893f02SDimitry Andric ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops, Rec);
3122754fe60SDimitry Andric bidx = CStr.find_first_not_of(delims, eidx);
3132754fe60SDimitry Andric }
3142754fe60SDimitry Andric }
3152754fe60SDimitry Andric
ProcessDisableEncoding(std::string DisableEncoding)3162754fe60SDimitry Andric void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
3172754fe60SDimitry Andric while (1) {
3186122f3e6SDimitry Andric std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
3196122f3e6SDimitry Andric std::string OpName = P.first;
3206122f3e6SDimitry Andric DisableEncoding = P.second;
3212754fe60SDimitry Andric if (OpName.empty()) break;
3222754fe60SDimitry Andric
3232754fe60SDimitry Andric // Figure out which operand this is.
3242754fe60SDimitry Andric std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
3252754fe60SDimitry Andric
3262754fe60SDimitry Andric // Mark the operand as not-to-be encoded.
3272754fe60SDimitry Andric if (Op.second >= OperandList[Op.first].DoNotEncode.size())
3282754fe60SDimitry Andric OperandList[Op.first].DoNotEncode.resize(Op.second+1);
3292754fe60SDimitry Andric OperandList[Op.first].DoNotEncode[Op.second] = true;
3302754fe60SDimitry Andric }
3312754fe60SDimitry Andric
3322754fe60SDimitry Andric }
3332754fe60SDimitry Andric
3342754fe60SDimitry Andric //===----------------------------------------------------------------------===//
3352754fe60SDimitry Andric // CodeGenInstruction Implementation
3362754fe60SDimitry Andric //===----------------------------------------------------------------------===//
3372754fe60SDimitry Andric
CodeGenInstruction(Record * R)3383861d79fSDimitry Andric CodeGenInstruction::CodeGenInstruction(Record *R)
33991bc56edSDimitry Andric : TheDef(R), Operands(R), InferredFrom(nullptr) {
3402754fe60SDimitry Andric Namespace = R->getValueAsString("Namespace");
3412754fe60SDimitry Andric AsmString = R->getValueAsString("AsmString");
3422754fe60SDimitry Andric
3432754fe60SDimitry Andric isReturn = R->getValueAsBit("isReturn");
344*b5893f02SDimitry Andric isEHScopeReturn = R->getValueAsBit("isEHScopeReturn");
3452754fe60SDimitry Andric isBranch = R->getValueAsBit("isBranch");
3462754fe60SDimitry Andric isIndirectBranch = R->getValueAsBit("isIndirectBranch");
3472754fe60SDimitry Andric isCompare = R->getValueAsBit("isCompare");
3482754fe60SDimitry Andric isMoveImm = R->getValueAsBit("isMoveImm");
3494ba319b5SDimitry Andric isMoveReg = R->getValueAsBit("isMoveReg");
3503b0f4066SDimitry Andric isBitcast = R->getValueAsBit("isBitcast");
3517ae0e2c9SDimitry Andric isSelect = R->getValueAsBit("isSelect");
3522754fe60SDimitry Andric isBarrier = R->getValueAsBit("isBarrier");
3532754fe60SDimitry Andric isCall = R->getValueAsBit("isCall");
354d88c1a5aSDimitry Andric isAdd = R->getValueAsBit("isAdd");
3554ba319b5SDimitry Andric isTrap = R->getValueAsBit("isTrap");
3562754fe60SDimitry Andric canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
3572754fe60SDimitry Andric isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
3582754fe60SDimitry Andric isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
3592754fe60SDimitry Andric isCommutable = R->getValueAsBit("isCommutable");
3602754fe60SDimitry Andric isTerminator = R->getValueAsBit("isTerminator");
3612754fe60SDimitry Andric isReMaterializable = R->getValueAsBit("isReMaterializable");
3622754fe60SDimitry Andric hasDelaySlot = R->getValueAsBit("hasDelaySlot");
3632754fe60SDimitry Andric usesCustomInserter = R->getValueAsBit("usesCustomInserter");
3646122f3e6SDimitry Andric hasPostISelHook = R->getValueAsBit("hasPostISelHook");
3652754fe60SDimitry Andric hasCtrlDep = R->getValueAsBit("hasCtrlDep");
3662754fe60SDimitry Andric isNotDuplicable = R->getValueAsBit("isNotDuplicable");
36739d628a0SDimitry Andric isRegSequence = R->getValueAsBit("isRegSequence");
36839d628a0SDimitry Andric isExtractSubreg = R->getValueAsBit("isExtractSubreg");
36939d628a0SDimitry Andric isInsertSubreg = R->getValueAsBit("isInsertSubreg");
37097bc6c73SDimitry Andric isConvergent = R->getValueAsBit("isConvergent");
3713ca95b02SDimitry Andric hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo");
3724ba319b5SDimitry Andric FastISelShouldIgnore = R->getValueAsBit("FastISelShouldIgnore");
373*b5893f02SDimitry Andric variadicOpsAreDefs = R->getValueAsBit("variadicOpsAreDefs");
3743861d79fSDimitry Andric
37591bc56edSDimitry Andric bool Unset;
37691bc56edSDimitry Andric mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset);
37791bc56edSDimitry Andric mayLoad_Unset = Unset;
37891bc56edSDimitry Andric mayStore = R->getValueAsBitOrUnset("mayStore", Unset);
37991bc56edSDimitry Andric mayStore_Unset = Unset;
38091bc56edSDimitry Andric hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset);
38191bc56edSDimitry Andric hasSideEffects_Unset = Unset;
3823861d79fSDimitry Andric
3832754fe60SDimitry Andric isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
3842754fe60SDimitry Andric hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
3852754fe60SDimitry Andric hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
38617a519f9SDimitry Andric isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
38717a519f9SDimitry Andric isPseudo = R->getValueAsBit("isPseudo");
3882754fe60SDimitry Andric ImplicitDefs = R->getValueAsListOfDefs("Defs");
3892754fe60SDimitry Andric ImplicitUses = R->getValueAsListOfDefs("Uses");
3902754fe60SDimitry Andric
3914ba319b5SDimitry Andric // This flag is only inferred from the pattern.
3924ba319b5SDimitry Andric hasChain = false;
3934ba319b5SDimitry Andric hasChain_Inferred = false;
3944ba319b5SDimitry Andric
3952754fe60SDimitry Andric // Parse Constraints.
396*b5893f02SDimitry Andric ParseConstraints(R->getValueAsString("Constraints"), Operands, R);
3972754fe60SDimitry Andric
3982754fe60SDimitry Andric // Parse the DisableEncoding field.
3992754fe60SDimitry Andric Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
400f785676fSDimitry Andric
401f785676fSDimitry Andric // First check for a ComplexDeprecationPredicate.
402f785676fSDimitry Andric if (R->getValue("ComplexDeprecationPredicate")) {
403f785676fSDimitry Andric HasComplexDeprecationPredicate = true;
404f785676fSDimitry Andric DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate");
405f785676fSDimitry Andric } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) {
406f785676fSDimitry Andric // Check if we have a Subtarget feature mask.
407f785676fSDimitry Andric HasComplexDeprecationPredicate = false;
408f785676fSDimitry Andric DeprecatedReason = Dep->getValue()->getAsString();
409f785676fSDimitry Andric } else {
410f785676fSDimitry Andric // This instruction isn't deprecated.
411f785676fSDimitry Andric HasComplexDeprecationPredicate = false;
412f785676fSDimitry Andric DeprecatedReason = "";
413f785676fSDimitry Andric }
4142754fe60SDimitry Andric }
415f22ef01cSRoman Divacky
416f22ef01cSRoman Divacky /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
417f22ef01cSRoman Divacky /// implicit def and it has a known VT, return the VT, otherwise return
418f22ef01cSRoman Divacky /// MVT::Other.
419f22ef01cSRoman Divacky MVT::SimpleValueType CodeGenInstruction::
HasOneImplicitDefWithKnownVT(const CodeGenTarget & TargetInfo) const420f22ef01cSRoman Divacky HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
421f22ef01cSRoman Divacky if (ImplicitDefs.empty()) return MVT::Other;
422f22ef01cSRoman Divacky
423f22ef01cSRoman Divacky // Check to see if the first implicit def has a resolvable type.
424f22ef01cSRoman Divacky Record *FirstImplicitDef = ImplicitDefs[0];
425f22ef01cSRoman Divacky assert(FirstImplicitDef->isSubClassOf("Register"));
4262cab237bSDimitry Andric const std::vector<ValueTypeByHwMode> &RegVTs =
427f22ef01cSRoman Divacky TargetInfo.getRegisterVTs(FirstImplicitDef);
4282cab237bSDimitry Andric if (RegVTs.size() == 1 && RegVTs[0].isSimple())
4292cab237bSDimitry Andric return RegVTs[0].getSimple().SimpleTy;
430f22ef01cSRoman Divacky return MVT::Other;
431f22ef01cSRoman Divacky }
432f22ef01cSRoman Divacky
4332754fe60SDimitry Andric
4342754fe60SDimitry Andric /// FlattenAsmStringVariants - Flatten the specified AsmString to only
4352754fe60SDimitry Andric /// include text from the specified variant, returning the new string.
4362754fe60SDimitry Andric std::string CodeGenInstruction::
FlattenAsmStringVariants(StringRef Cur,unsigned Variant)4372754fe60SDimitry Andric FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
4382754fe60SDimitry Andric std::string Res = "";
4392754fe60SDimitry Andric
4402754fe60SDimitry Andric for (;;) {
4412754fe60SDimitry Andric // Find the start of the next variant string.
4422754fe60SDimitry Andric size_t VariantsStart = 0;
4432754fe60SDimitry Andric for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
4442754fe60SDimitry Andric if (Cur[VariantsStart] == '{' &&
4452754fe60SDimitry Andric (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
4462754fe60SDimitry Andric Cur[VariantsStart-1] != '\\')))
4472754fe60SDimitry Andric break;
4482754fe60SDimitry Andric
4492754fe60SDimitry Andric // Add the prefix to the result.
4502754fe60SDimitry Andric Res += Cur.slice(0, VariantsStart);
4512754fe60SDimitry Andric if (VariantsStart == Cur.size())
4522754fe60SDimitry Andric break;
4532754fe60SDimitry Andric
4542754fe60SDimitry Andric ++VariantsStart; // Skip the '{'.
4552754fe60SDimitry Andric
4562754fe60SDimitry Andric // Scan to the end of the variants string.
4572754fe60SDimitry Andric size_t VariantsEnd = VariantsStart;
4582754fe60SDimitry Andric unsigned NestedBraces = 1;
4592754fe60SDimitry Andric for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
4602754fe60SDimitry Andric if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
4612754fe60SDimitry Andric if (--NestedBraces == 0)
4622754fe60SDimitry Andric break;
4632754fe60SDimitry Andric } else if (Cur[VariantsEnd] == '{')
4642754fe60SDimitry Andric ++NestedBraces;
4652754fe60SDimitry Andric }
4662754fe60SDimitry Andric
4672754fe60SDimitry Andric // Select the Nth variant (or empty).
4682754fe60SDimitry Andric StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
4692754fe60SDimitry Andric for (unsigned i = 0; i != Variant; ++i)
4702754fe60SDimitry Andric Selection = Selection.split('|').second;
4712754fe60SDimitry Andric Res += Selection.split('|').first;
4722754fe60SDimitry Andric
4732754fe60SDimitry Andric assert(VariantsEnd != Cur.size() &&
4742754fe60SDimitry Andric "Unterminated variants in assembly string!");
4752754fe60SDimitry Andric Cur = Cur.substr(VariantsEnd + 1);
4762754fe60SDimitry Andric }
4772754fe60SDimitry Andric
4782754fe60SDimitry Andric return Res;
4792754fe60SDimitry Andric }
4802754fe60SDimitry Andric
isOperandAPointer(unsigned i) const4812cab237bSDimitry Andric bool CodeGenInstruction::isOperandAPointer(unsigned i) const {
4822cab237bSDimitry Andric if (DagInit *ConstraintList = TheDef->getValueAsDag("InOperandList")) {
4832cab237bSDimitry Andric if (i < ConstraintList->getNumArgs()) {
4842cab237bSDimitry Andric if (DefInit *Constraint = dyn_cast<DefInit>(ConstraintList->getArg(i))) {
4852cab237bSDimitry Andric return Constraint->getDef()->isSubClassOf("TypedOperand") &&
4862cab237bSDimitry Andric Constraint->getDef()->getValueAsBit("IsPointer");
4872cab237bSDimitry Andric }
4882cab237bSDimitry Andric }
4892cab237bSDimitry Andric }
4902cab237bSDimitry Andric return false;
4912cab237bSDimitry Andric }
4922754fe60SDimitry Andric
4932754fe60SDimitry Andric //===----------------------------------------------------------------------===//
4942754fe60SDimitry Andric /// CodeGenInstAlias Implementation
4952754fe60SDimitry Andric //===----------------------------------------------------------------------===//
4962754fe60SDimitry Andric
4972754fe60SDimitry Andric /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
4982754fe60SDimitry Andric /// constructor. It checks if an argument in an InstAlias pattern matches
4992754fe60SDimitry Andric /// the corresponding operand of the instruction. It returns true on a
5002754fe60SDimitry 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)5012754fe60SDimitry Andric bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
5022754fe60SDimitry Andric Record *InstOpRec, bool hasSubOps,
5033861d79fSDimitry Andric ArrayRef<SMLoc> Loc, CodeGenTarget &T,
5042754fe60SDimitry Andric ResultOperand &ResOp) {
5052754fe60SDimitry Andric Init *Arg = Result->getArg(AliasOpNo);
5063861d79fSDimitry Andric DefInit *ADI = dyn_cast<DefInit>(Arg);
50791bc56edSDimitry Andric Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
5082754fe60SDimitry Andric
5092754fe60SDimitry Andric if (ADI && ADI->getDef() == InstOpRec) {
5102754fe60SDimitry Andric // If the operand is a record, it must have a name, and the record type
5112754fe60SDimitry Andric // must match up with the instruction's argument type.
512d88c1a5aSDimitry Andric if (!Result->getArgName(AliasOpNo))
51391bc56edSDimitry Andric PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
5142754fe60SDimitry Andric " must have a name!");
515d88c1a5aSDimitry Andric ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
5162754fe60SDimitry Andric return true;
5172754fe60SDimitry Andric }
5182754fe60SDimitry Andric
519dff0c46cSDimitry Andric // For register operands, the source register class can be a subclass
520dff0c46cSDimitry Andric // of the instruction register class, not just an exact match.
52191bc56edSDimitry Andric if (InstOpRec->isSubClassOf("RegisterOperand"))
52291bc56edSDimitry Andric InstOpRec = InstOpRec->getValueAsDef("RegClass");
52391bc56edSDimitry Andric
52491bc56edSDimitry Andric if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand"))
52591bc56edSDimitry Andric ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit();
52691bc56edSDimitry Andric
527dff0c46cSDimitry Andric if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
528dff0c46cSDimitry Andric if (!InstOpRec->isSubClassOf("RegisterClass"))
529dff0c46cSDimitry Andric return false;
530dff0c46cSDimitry Andric if (!T.getRegisterClass(InstOpRec)
531dff0c46cSDimitry Andric .hasSubClass(&T.getRegisterClass(ADI->getDef())))
532dff0c46cSDimitry Andric return false;
533d88c1a5aSDimitry Andric ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
534dff0c46cSDimitry Andric return true;
535dff0c46cSDimitry Andric }
536dff0c46cSDimitry Andric
5372754fe60SDimitry Andric // Handle explicit registers.
5382754fe60SDimitry Andric if (ADI && ADI->getDef()->isSubClassOf("Register")) {
5396122f3e6SDimitry Andric if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
5406122f3e6SDimitry Andric DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
5416122f3e6SDimitry Andric // The operand info should only have a single (register) entry. We
5426122f3e6SDimitry Andric // want the register class of it.
5433861d79fSDimitry Andric InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
5446122f3e6SDimitry Andric }
5456122f3e6SDimitry Andric
5462754fe60SDimitry Andric if (!InstOpRec->isSubClassOf("RegisterClass"))
5472754fe60SDimitry Andric return false;
5482754fe60SDimitry Andric
54917a519f9SDimitry Andric if (!T.getRegisterClass(InstOpRec)
55017a519f9SDimitry Andric .contains(T.getRegBank().getReg(ADI->getDef())))
5513861d79fSDimitry Andric PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
5526122f3e6SDimitry Andric " is not a member of the " + InstOpRec->getName() +
5532754fe60SDimitry Andric " register class!");
5542754fe60SDimitry Andric
555d88c1a5aSDimitry Andric if (Result->getArgName(AliasOpNo))
5563861d79fSDimitry Andric PrintFatalError(Loc, "result fixed register argument must "
5572754fe60SDimitry Andric "not have a name!");
5582754fe60SDimitry Andric
55991bc56edSDimitry Andric ResOp = ResultOperand(ResultRecord);
5602754fe60SDimitry Andric return true;
5612754fe60SDimitry Andric }
5622754fe60SDimitry Andric
5632754fe60SDimitry Andric // Handle "zero_reg" for optional def operands.
5642754fe60SDimitry Andric if (ADI && ADI->getDef()->getName() == "zero_reg") {
5652754fe60SDimitry Andric
5662754fe60SDimitry Andric // Check if this is an optional def.
567dff0c46cSDimitry Andric // Tied operands where the source is a sub-operand of a complex operand
568dff0c46cSDimitry Andric // need to represent both operands in the alias destination instruction.
569dff0c46cSDimitry Andric // Allow zero_reg for the tied portion. This can and should go away once
570dff0c46cSDimitry Andric // the MC representation of things doesn't use tied operands at all.
571dff0c46cSDimitry Andric //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
572dff0c46cSDimitry Andric // throw TGError(Loc, "reg0 used for result that is not an "
573dff0c46cSDimitry Andric // "OptionalDefOperand!");
5742754fe60SDimitry Andric
57591bc56edSDimitry Andric ResOp = ResultOperand(static_cast<Record*>(nullptr));
5762754fe60SDimitry Andric return true;
5772754fe60SDimitry Andric }
5782754fe60SDimitry Andric
579dff0c46cSDimitry Andric // Literal integers.
5803861d79fSDimitry Andric if (IntInit *II = dyn_cast<IntInit>(Arg)) {
5812754fe60SDimitry Andric if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
5822754fe60SDimitry Andric return false;
5832754fe60SDimitry Andric // Integer arguments can't have names.
584d88c1a5aSDimitry Andric if (Result->getArgName(AliasOpNo))
58591bc56edSDimitry Andric PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
5862754fe60SDimitry Andric " must not have a name!");
5872754fe60SDimitry Andric ResOp = ResultOperand(II->getValue());
5882754fe60SDimitry Andric return true;
5892754fe60SDimitry Andric }
5902754fe60SDimitry Andric
59139d628a0SDimitry Andric // Bits<n> (also used for 0bxx literals)
59239d628a0SDimitry Andric if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
59339d628a0SDimitry Andric if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
59439d628a0SDimitry Andric return false;
59539d628a0SDimitry Andric if (!BI->isComplete())
59639d628a0SDimitry Andric return false;
59739d628a0SDimitry Andric // Convert the bits init to an integer and use that for the result.
59839d628a0SDimitry Andric IntInit *II =
59939d628a0SDimitry Andric dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get()));
60039d628a0SDimitry Andric if (!II)
60139d628a0SDimitry Andric return false;
60239d628a0SDimitry Andric ResOp = ResultOperand(II->getValue());
60339d628a0SDimitry Andric return true;
60439d628a0SDimitry Andric }
60539d628a0SDimitry Andric
606dff0c46cSDimitry Andric // If both are Operands with the same MVT, allow the conversion. It's
607dff0c46cSDimitry Andric // up to the user to make sure the values are appropriate, just like
608dff0c46cSDimitry Andric // for isel Pat's.
60939d628a0SDimitry Andric if (InstOpRec->isSubClassOf("Operand") && ADI &&
610dff0c46cSDimitry Andric ADI->getDef()->isSubClassOf("Operand")) {
611dff0c46cSDimitry Andric // FIXME: What other attributes should we check here? Identical
612dff0c46cSDimitry Andric // MIOperandInfo perhaps?
613dff0c46cSDimitry Andric if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
614dff0c46cSDimitry Andric return false;
615d88c1a5aSDimitry Andric ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ADI->getDef());
616dff0c46cSDimitry Andric return true;
617dff0c46cSDimitry Andric }
618dff0c46cSDimitry Andric
6192754fe60SDimitry Andric return false;
6202754fe60SDimitry Andric }
6212754fe60SDimitry Andric
getMINumOperands() const62291bc56edSDimitry Andric unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
62391bc56edSDimitry Andric if (!isRecord())
62491bc56edSDimitry Andric return 1;
62591bc56edSDimitry Andric
62691bc56edSDimitry Andric Record *Rec = getRecord();
62791bc56edSDimitry Andric if (!Rec->isSubClassOf("Operand"))
62891bc56edSDimitry Andric return 1;
62991bc56edSDimitry Andric
63091bc56edSDimitry Andric DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
63191bc56edSDimitry Andric if (MIOpInfo->getNumArgs() == 0) {
63291bc56edSDimitry Andric // Unspecified, so it defaults to 1
63391bc56edSDimitry Andric return 1;
63491bc56edSDimitry Andric }
63591bc56edSDimitry Andric
63691bc56edSDimitry Andric return MIOpInfo->getNumArgs();
63791bc56edSDimitry Andric }
63891bc56edSDimitry Andric
CodeGenInstAlias(Record * R,CodeGenTarget & T)6394ba319b5SDimitry Andric CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T)
64091bc56edSDimitry Andric : TheDef(R) {
6412754fe60SDimitry Andric Result = R->getValueAsDag("ResultInst");
64291bc56edSDimitry Andric AsmString = R->getValueAsString("AsmString");
64391bc56edSDimitry Andric
6442754fe60SDimitry Andric
6452754fe60SDimitry Andric // Verify that the root of the result is an instruction.
6463861d79fSDimitry Andric DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
64791bc56edSDimitry Andric if (!DI || !DI->getDef()->isSubClassOf("Instruction"))
6483861d79fSDimitry Andric PrintFatalError(R->getLoc(),
6493861d79fSDimitry Andric "result of inst alias should be an instruction");
6502754fe60SDimitry Andric
6512754fe60SDimitry Andric ResultInst = &T.getInstruction(DI->getDef());
6522754fe60SDimitry Andric
6532754fe60SDimitry Andric // NameClass - If argument names are repeated, we need to verify they have
6542754fe60SDimitry Andric // the same class.
6552754fe60SDimitry Andric StringMap<Record*> NameClass;
6562754fe60SDimitry Andric for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
6573861d79fSDimitry Andric DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
658d88c1a5aSDimitry Andric if (!ADI || !Result->getArgName(i))
6592754fe60SDimitry Andric continue;
6602754fe60SDimitry Andric // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
6612754fe60SDimitry Andric // $foo can exist multiple times in the result list, but it must have the
6622754fe60SDimitry Andric // same type.
663d88c1a5aSDimitry Andric Record *&Entry = NameClass[Result->getArgNameStr(i)];
6642754fe60SDimitry Andric if (Entry && Entry != ADI->getDef())
665d88c1a5aSDimitry Andric PrintFatalError(R->getLoc(), "result value $" + Result->getArgNameStr(i) +
6662754fe60SDimitry Andric " is both " + Entry->getName() + " and " +
6672754fe60SDimitry Andric ADI->getDef()->getName() + "!");
6682754fe60SDimitry Andric Entry = ADI->getDef();
6692754fe60SDimitry Andric }
6702754fe60SDimitry Andric
6712754fe60SDimitry Andric // Decode and validate the arguments of the result.
6722754fe60SDimitry Andric unsigned AliasOpNo = 0;
6732754fe60SDimitry Andric for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
6742754fe60SDimitry Andric
675dff0c46cSDimitry Andric // Tied registers don't have an entry in the result dag unless they're part
676dff0c46cSDimitry Andric // of a complex operand, in which case we include them anyways, as we
677dff0c46cSDimitry Andric // don't have any other way to specify the whole operand.
678dff0c46cSDimitry Andric if (ResultInst->Operands[i].MINumOperands == 1 &&
6794ba319b5SDimitry Andric ResultInst->Operands[i].getTiedRegister() != -1) {
6804ba319b5SDimitry Andric // Tied operands of different RegisterClass should be explicit within an
6814ba319b5SDimitry Andric // instruction's syntax and so cannot be skipped.
6824ba319b5SDimitry Andric int TiedOpNum = ResultInst->Operands[i].getTiedRegister();
6834ba319b5SDimitry Andric if (ResultInst->Operands[i].Rec->getName() ==
6844ba319b5SDimitry Andric ResultInst->Operands[TiedOpNum].Rec->getName())
6852754fe60SDimitry Andric continue;
6864ba319b5SDimitry Andric }
6872754fe60SDimitry Andric
6882754fe60SDimitry Andric if (AliasOpNo >= Result->getNumArgs())
6893861d79fSDimitry Andric PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
6902754fe60SDimitry Andric
6912754fe60SDimitry Andric Record *InstOpRec = ResultInst->Operands[i].Rec;
6922754fe60SDimitry Andric unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
6932754fe60SDimitry Andric ResultOperand ResOp(static_cast<int64_t>(0));
6942754fe60SDimitry Andric if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
6952754fe60SDimitry Andric R->getLoc(), T, ResOp)) {
6967ae0e2c9SDimitry Andric // If this is a simple operand, or a complex operand with a custom match
6977ae0e2c9SDimitry Andric // class, then we can match is verbatim.
6987ae0e2c9SDimitry Andric if (NumSubOps == 1 ||
6997ae0e2c9SDimitry Andric (InstOpRec->getValue("ParserMatchClass") &&
7007ae0e2c9SDimitry Andric InstOpRec->getValueAsDef("ParserMatchClass")
7017ae0e2c9SDimitry Andric ->getValueAsString("Name") != "Imm")) {
7022754fe60SDimitry Andric ResultOperands.push_back(ResOp);
7032754fe60SDimitry Andric ResultInstOperandIndex.push_back(std::make_pair(i, -1));
7042754fe60SDimitry Andric ++AliasOpNo;
7057ae0e2c9SDimitry Andric
7067ae0e2c9SDimitry Andric // Otherwise, we need to match each of the suboperands individually.
7077ae0e2c9SDimitry Andric } else {
7087ae0e2c9SDimitry Andric DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
7097ae0e2c9SDimitry Andric for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
7103861d79fSDimitry Andric Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
7117ae0e2c9SDimitry Andric
7127ae0e2c9SDimitry Andric // Take care to instantiate each of the suboperands with the correct
7137ae0e2c9SDimitry Andric // nomenclature: $foo.bar
714d88c1a5aSDimitry Andric ResultOperands.emplace_back(
715d88c1a5aSDimitry Andric Result->getArgName(AliasOpNo)->getAsUnquotedString() + "." +
716d88c1a5aSDimitry Andric MIOI->getArgName(SubOp)->getAsUnquotedString(), SubRec);
7177ae0e2c9SDimitry Andric ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
7187ae0e2c9SDimitry Andric }
7197ae0e2c9SDimitry Andric ++AliasOpNo;
7207ae0e2c9SDimitry Andric }
7212754fe60SDimitry Andric continue;
7222754fe60SDimitry Andric }
7232754fe60SDimitry Andric
7242754fe60SDimitry Andric // If the argument did not match the instruction operand, and the operand
7252754fe60SDimitry Andric // is composed of multiple suboperands, try matching the suboperands.
7262754fe60SDimitry Andric if (NumSubOps > 1) {
7272754fe60SDimitry Andric DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
7282754fe60SDimitry Andric for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
7292754fe60SDimitry Andric if (AliasOpNo >= Result->getNumArgs())
7303861d79fSDimitry Andric PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
7313861d79fSDimitry Andric Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
7322754fe60SDimitry Andric if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
7332754fe60SDimitry Andric R->getLoc(), T, ResOp)) {
7342754fe60SDimitry Andric ResultOperands.push_back(ResOp);
7352754fe60SDimitry Andric ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
7362754fe60SDimitry Andric ++AliasOpNo;
7372754fe60SDimitry Andric } else {
73891bc56edSDimitry Andric PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
7392754fe60SDimitry Andric " does not match instruction operand class " +
7402754fe60SDimitry Andric (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
7412754fe60SDimitry Andric }
7422754fe60SDimitry Andric }
7432754fe60SDimitry Andric continue;
7442754fe60SDimitry Andric }
74591bc56edSDimitry Andric PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
7462754fe60SDimitry Andric " does not match instruction operand class " +
7472754fe60SDimitry Andric InstOpRec->getName());
7482754fe60SDimitry Andric }
7492754fe60SDimitry Andric
7502754fe60SDimitry Andric if (AliasOpNo != Result->getNumArgs())
7513861d79fSDimitry Andric PrintFatalError(R->getLoc(), "too many operands for instruction!");
7522754fe60SDimitry Andric }
753