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