blob: a6ca400914b49d1df4dd88e5d11189ac16d88924 [file] [log] [blame]
Chris Lattnerea2d52d2008-01-06 01:35:39 +00001//===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CodeGenInstruction class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenInstruction.h"
Chris Lattner7bc5d9b2010-03-27 20:09:24 +000015#include "CodeGenTarget.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000016#include "llvm/ADT/STLExtras.h"
Chris Lattnerea2d52d2008-01-06 01:35:39 +000017#include "llvm/ADT/StringExtras.h"
Chris Lattnerb625dd22010-11-06 07:06:09 +000018#include "llvm/ADT/StringMap.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000019#include "llvm/TableGen/Error.h"
20#include "llvm/TableGen/Record.h"
Chris Lattnerea2d52d2008-01-06 01:35:39 +000021#include <set>
22using namespace llvm;
23
Chris Lattnerd8adec72010-11-01 04:03:32 +000024//===----------------------------------------------------------------------===//
25// CGIOperandList Implementation
26//===----------------------------------------------------------------------===//
Jim Grosbach2a282f22009-12-16 19:43:02 +000027
Chris Lattnerd8adec72010-11-01 04:03:32 +000028CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
29 isPredicable = false;
Chris Lattnerea2d52d2008-01-06 01:35:39 +000030 hasOptionalDef = false;
Chris Lattnerf376c992008-01-07 05:19:29 +000031 isVariadic = false;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000032
David Greeneaf8ee2c2011-07-29 22:43:06 +000033 DagInit *OutDI = R->getValueAsDag("OutOperandList");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000034
Sean Silvafb509ed2012-10-10 20:24:43 +000035 if (DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) {
Chris Lattner0f037472010-03-18 20:56:35 +000036 if (Init->getDef()->getName() != "outs")
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000037 PrintFatalError(R->getName() + ": invalid def name for output list: use 'outs'");
Chris Lattner5f418ea2010-03-18 20:50:52 +000038 } else
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000039 PrintFatalError(R->getName() + ": invalid output list: use 'outs'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000040
Chris Lattner1f22d272010-03-18 21:07:39 +000041 NumDefs = OutDI->getNumArgs();
Chris Lattnerd8adec72010-11-01 04:03:32 +000042
David Greeneaf8ee2c2011-07-29 22:43:06 +000043 DagInit *InDI = R->getValueAsDag("InOperandList");
Sean Silvafb509ed2012-10-10 20:24:43 +000044 if (DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) {
Chris Lattner0f037472010-03-18 20:56:35 +000045 if (Init->getDef()->getName() != "ins")
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000046 PrintFatalError(R->getName() + ": invalid def name for input list: use 'ins'");
Chris Lattner5f418ea2010-03-18 20:50:52 +000047 } else
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000048 PrintFatalError(R->getName() + ": invalid input list: use 'ins'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000049
Chris Lattnerea2d52d2008-01-06 01:35:39 +000050 unsigned MIOperandNo = 0;
51 std::set<std::string> OperandNames;
Reid Kleckner45b61592016-02-03 19:34:28 +000052 unsigned e = InDI->getNumArgs() + OutDI->getNumArgs();
53 OperandList.reserve(e);
54 for (unsigned i = 0; i != e; ++i){
David Greeneaf8ee2c2011-07-29 22:43:06 +000055 Init *ArgInit;
Matthias Braunbb053162016-12-05 06:00:46 +000056 StringRef ArgName;
Chris Lattner1f22d272010-03-18 21:07:39 +000057 if (i < NumDefs) {
58 ArgInit = OutDI->getArg(i);
Matthias Braunbb053162016-12-05 06:00:46 +000059 ArgName = OutDI->getArgNameStr(i);
Chris Lattner1f22d272010-03-18 21:07:39 +000060 } else {
61 ArgInit = InDI->getArg(i-NumDefs);
Matthias Braunbb053162016-12-05 06:00:46 +000062 ArgName = InDI->getArgNameStr(i-NumDefs);
Chris Lattner1f22d272010-03-18 21:07:39 +000063 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000064
Sean Silvafb509ed2012-10-10 20:24:43 +000065 DefInit *Arg = dyn_cast<DefInit>(ArgInit);
Chris Lattnerea2d52d2008-01-06 01:35:39 +000066 if (!Arg)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000067 PrintFatalError("Illegal operand for the '" + R->getName() + "' instruction!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000068
Chris Lattnerea2d52d2008-01-06 01:35:39 +000069 Record *Rec = Arg->getDef();
70 std::string PrintMethod = "printOperand";
Jim Grosbach51a12eb2010-10-12 22:21:57 +000071 std::string EncoderMethod;
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +000072 std::string OperandType = "OPERAND_UNKNOWN";
Tom Stellard89b26102015-01-12 19:33:09 +000073 std::string OperandNamespace = "MCOI";
Chris Lattnerea2d52d2008-01-06 01:35:39 +000074 unsigned NumOps = 1;
Craig Topper24064772014-04-15 07:20:03 +000075 DagInit *MIOpInfo = nullptr;
Owen Andersona84be6c2011-06-27 21:06:21 +000076 if (Rec->isSubClassOf("RegisterOperand")) {
77 PrintMethod = Rec->getValueAsString("PrintMethod");
Tom Stellard89b26102015-01-12 19:33:09 +000078 OperandType = Rec->getValueAsString("OperandType");
79 OperandNamespace = Rec->getValueAsString("OperandNamespace");
Sam Kolton1a5a5e62017-05-15 10:13:07 +000080 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Owen Andersona84be6c2011-06-27 21:06:21 +000081 } else if (Rec->isSubClassOf("Operand")) {
Chris Lattnerea2d52d2008-01-06 01:35:39 +000082 PrintMethod = Rec->getValueAsString("PrintMethod");
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +000083 OperandType = Rec->getValueAsString("OperandType");
Dan Gohmandb70d012015-12-22 23:37:37 +000084 OperandNamespace = Rec->getValueAsString("OperandNamespace");
Jim Grosbach51a12eb2010-10-12 22:21:57 +000085 // If there is an explicit encoder method, use it.
Chris Lattner63274cb2010-11-15 05:19:05 +000086 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattnerea2d52d2008-01-06 01:35:39 +000087 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000088
Chris Lattnerea2d52d2008-01-06 01:35:39 +000089 // Verify that MIOpInfo has an 'ops' root value.
Sean Silva88eb8dd2012-10-10 20:24:47 +000090 if (!isa<DefInit>(MIOpInfo->getOperator()) ||
91 cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000092 PrintFatalError("Bad value for MIOperandInfo in operand '" + Rec->getName() +
93 "'\n");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000094
Chris Lattnerea2d52d2008-01-06 01:35:39 +000095 // If we have MIOpInfo, then we have #operands equal to number of entries
96 // in MIOperandInfo.
97 if (unsigned NumArgs = MIOpInfo->getNumArgs())
98 NumOps = NumArgs;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000099
Tim Northover42180442013-08-22 09:57:11 +0000100 if (Rec->isSubClassOf("PredicateOp"))
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000101 isPredicable = true;
102 else if (Rec->isSubClassOf("OptionalDefOperand"))
103 hasOptionalDef = true;
104 } else if (Rec->getName() == "variable_ops") {
Chris Lattnerf376c992008-01-07 05:19:29 +0000105 isVariadic = true;
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000106 continue;
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +0000107 } else if (Rec->isSubClassOf("RegisterClass")) {
108 OperandType = "OPERAND_REGISTER";
109 } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
Owen Anderson16ba4b22012-09-11 23:47:08 +0000110 !Rec->isSubClassOf("unknown_class"))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000111 PrintFatalError("Unknown operand class '" + Rec->getName() +
112 "' in '" + R->getName() + "' instruction!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000113
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000114 // Check that the operand has a name and that it's unique.
Chris Lattner1f22d272010-03-18 21:07:39 +0000115 if (ArgName.empty())
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000116 PrintFatalError("In instruction '" + R->getName() + "', operand #" +
117 Twine(i) + " has no name!");
Chris Lattner1f22d272010-03-18 21:07:39 +0000118 if (!OperandNames.insert(ArgName).second)
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000119 PrintFatalError("In instruction '" + R->getName() + "', operand #" +
120 Twine(i) + " has the same name as a previous operand!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000121
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000122 OperandList.emplace_back(Rec, ArgName, PrintMethod, EncoderMethod,
123 OperandNamespace + "::" + OperandType, MIOperandNo,
124 NumOps, MIOpInfo);
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000125 MIOperandNo += NumOps;
126 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000127
128
Chris Lattnerdd3b09c2010-11-01 05:34:34 +0000129 // Make sure the constraints list for each operand is large enough to hold
130 // constraint info, even if none is present.
Javed Absar41705e92017-10-06 09:32:45 +0000131 for (OperandInfo &OpInfo : OperandList)
132 OpInfo.Constraints.resize(OpInfo.MINumOperands);
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000133}
134
Chris Lattnerd8adec72010-11-01 04:03:32 +0000135
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000136/// getOperandNamed - Return the index of the operand with the specified
137/// non-empty name. If the instruction does not have an operand with the
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000138/// specified name, abort.
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000139///
Chris Lattnerd8adec72010-11-01 04:03:32 +0000140unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000141 unsigned OpIdx;
142 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000143 PrintFatalError("'" + TheDef->getName() +
144 "' does not have an operand named '$" + Name + "'!");
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000145}
146
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000147/// hasOperandNamed - Query whether the instruction has an operand of the
148/// given name. If so, return true and set OpIdx to the index of the
149/// operand. Otherwise, return false.
Chris Lattnerd8adec72010-11-01 04:03:32 +0000150bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000151 assert(!Name.empty() && "Cannot search for operand with no name!");
152 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
153 if (OperandList[i].Name == Name) {
154 OpIdx = i;
155 return true;
156 }
157 return false;
158}
159
Jim Grosbach38c4f4b2009-12-15 19:28:13 +0000160std::pair<unsigned,unsigned>
Chris Lattnerd8adec72010-11-01 04:03:32 +0000161CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000162 if (Op.empty() || Op[0] != '$')
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000163 PrintFatalError(TheDef->getName() + ": Illegal operand name: '" + Op + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000164
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000165 std::string OpName = Op.substr(1);
166 std::string SubOpName;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000167
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000168 // Check to see if this is $foo.bar.
Benjamin Kramere6ba5ef2016-11-30 10:01:11 +0000169 std::string::size_type DotIdx = OpName.find_first_of('.');
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000170 if (DotIdx != std::string::npos) {
171 SubOpName = OpName.substr(DotIdx+1);
172 if (SubOpName.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000173 PrintFatalError(TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'");
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000174 OpName = OpName.substr(0, DotIdx);
175 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000176
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000177 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000178
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000179 if (SubOpName.empty()) { // If no suboperand name was specified:
180 // If one was needed, throw.
181 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
182 SubOpName.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000183 PrintFatalError(TheDef->getName() + ": Illegal to refer to"
184 " whole operand part of complex operand '" + Op + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000185
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000186 // Otherwise, return the operand.
187 return std::make_pair(OpIdx, 0U);
188 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000189
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000190 // Find the suboperand number involved.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000191 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
Craig Topper24064772014-04-15 07:20:03 +0000192 if (!MIOpInfo)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000193 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000194
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000195 // Find the operand with the right name.
196 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
Matthias Braunbb053162016-12-05 06:00:46 +0000197 if (MIOpInfo->getArgNameStr(i) == SubOpName)
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000198 return std::make_pair(OpIdx, i);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000199
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000200 // Otherwise, didn't find it!
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000201 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
Aaron Ballman414a0cd2013-08-16 01:43:31 +0000202 return std::make_pair(0U, 0U);
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000203}
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000204
Chris Lattnerd8adec72010-11-01 04:03:32 +0000205static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
206 // EARLY_CLOBBER: @early $reg
207 std::string::size_type wpos = CStr.find_first_of(" \t");
208 std::string::size_type start = CStr.find_first_not_of(" \t");
209 std::string Tok = CStr.substr(start, wpos - start);
210 if (Tok == "@earlyclobber") {
211 std::string Name = CStr.substr(wpos+1);
212 wpos = Name.find_first_not_of(" \t");
213 if (wpos == std::string::npos)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000214 PrintFatalError("Illegal format for @earlyclobber constraint: '" + CStr + "'");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000215 Name = Name.substr(wpos);
216 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000217
Chris Lattnerd8adec72010-11-01 04:03:32 +0000218 // Build the string for the operand
219 if (!Ops[Op.first].Constraints[Op.second].isNone())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000220 PrintFatalError("Operand '" + Name + "' cannot have multiple constraints!");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000221 Ops[Op.first].Constraints[Op.second] =
222 CGIOperandList::ConstraintInfo::getEarlyClobber();
223 return;
224 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000225
Chris Lattnerd8adec72010-11-01 04:03:32 +0000226 // Only other constraint is "TIED_TO" for now.
227 std::string::size_type pos = CStr.find_first_of('=');
228 assert(pos != std::string::npos && "Unrecognized constraint");
229 start = CStr.find_first_not_of(" \t");
230 std::string Name = CStr.substr(start, pos - start);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000231
Chris Lattnerd8adec72010-11-01 04:03:32 +0000232 // TIED_TO: $src1 = $dst
233 wpos = Name.find_first_of(" \t");
234 if (wpos == std::string::npos)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000235 PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000236 std::string DestOpName = Name.substr(0, wpos);
237 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000238
Chris Lattnerd8adec72010-11-01 04:03:32 +0000239 Name = CStr.substr(pos+1);
240 wpos = Name.find_first_not_of(" \t");
241 if (wpos == std::string::npos)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000242 PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000243
Lang Hamescdd40bd2012-10-20 22:44:13 +0000244 std::string SrcOpName = Name.substr(wpos);
245 std::pair<unsigned,unsigned> SrcOp = Ops.ParseOperandName(SrcOpName, false);
246 if (SrcOp > DestOp) {
247 std::swap(SrcOp, DestOp);
248 std::swap(SrcOpName, DestOpName);
249 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000250
Chris Lattnerd8adec72010-11-01 04:03:32 +0000251 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000252
Chris Lattnerd8adec72010-11-01 04:03:32 +0000253 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000254 PrintFatalError("Operand '" + DestOpName +
255 "' cannot have multiple constraints!");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000256 Ops[DestOp.first].Constraints[DestOp.second] =
Jim Grosbach7b03fbd2011-11-15 01:05:12 +0000257 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
Chris Lattnerd8adec72010-11-01 04:03:32 +0000258}
259
260static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000261 if (CStr.empty()) return;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000262
Chris Lattnerd8adec72010-11-01 04:03:32 +0000263 const std::string delims(",");
264 std::string::size_type bidx, eidx;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000265
Chris Lattnerd8adec72010-11-01 04:03:32 +0000266 bidx = CStr.find_first_not_of(delims);
267 while (bidx != std::string::npos) {
268 eidx = CStr.find_first_of(delims, bidx);
269 if (eidx == std::string::npos)
270 eidx = CStr.length();
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000271
Chris Lattnerd8adec72010-11-01 04:03:32 +0000272 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
273 bidx = CStr.find_first_not_of(delims, eidx);
274 }
275}
276
277void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
278 while (1) {
Chris Lattner5cf753c2011-07-21 06:21:31 +0000279 std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
280 std::string OpName = P.first;
281 DisableEncoding = P.second;
Chris Lattnerd8adec72010-11-01 04:03:32 +0000282 if (OpName.empty()) break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000283
Chris Lattnerd8adec72010-11-01 04:03:32 +0000284 // Figure out which operand this is.
285 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000286
Chris Lattnerd8adec72010-11-01 04:03:32 +0000287 // Mark the operand as not-to-be encoded.
288 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
289 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
290 OperandList[Op.first].DoNotEncode[Op.second] = true;
291 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000292
Chris Lattnerd8adec72010-11-01 04:03:32 +0000293}
294
295//===----------------------------------------------------------------------===//
296// CodeGenInstruction Implementation
297//===----------------------------------------------------------------------===//
298
Jakob Stoklund Olesen94ed4d42012-08-24 00:31:16 +0000299CodeGenInstruction::CodeGenInstruction(Record *R)
Craig Topper24064772014-04-15 07:20:03 +0000300 : TheDef(R), Operands(R), InferredFrom(nullptr) {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000301 Namespace = R->getValueAsString("Namespace");
302 AsmString = R->getValueAsString("AsmString");
303
304 isReturn = R->getValueAsBit("isReturn");
Heejin Ahned5e06b2018-08-21 19:44:11 +0000305 isEHScopeReturn = R->getValueAsBit("isEHScopeReturn");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000306 isBranch = R->getValueAsBit("isBranch");
307 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
308 isCompare = R->getValueAsBit("isCompare");
Evan Cheng7f8ab6e2010-11-17 20:13:28 +0000309 isMoveImm = R->getValueAsBit("isMoveImm");
Petar Jovanovicc0510002018-05-23 15:28:28 +0000310 isMoveReg = R->getValueAsBit("isMoveReg");
Evan Cheng880e299d2011-03-15 05:09:26 +0000311 isBitcast = R->getValueAsBit("isBitcast");
Jakob Stoklund Olesen2382d322012-08-16 23:11:47 +0000312 isSelect = R->getValueAsBit("isSelect");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000313 isBarrier = R->getValueAsBit("isBarrier");
314 isCall = R->getValueAsBit("isCall");
Sjoerd Meijer724023a2016-09-14 08:20:03 +0000315 isAdd = R->getValueAsBit("isAdd");
Joel Galenson06e7e572018-07-13 15:19:33 +0000316 isTrap = R->getValueAsBit("isTrap");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000317 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000318 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
319 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
320 isCommutable = R->getValueAsBit("isCommutable");
321 isTerminator = R->getValueAsBit("isTerminator");
322 isReMaterializable = R->getValueAsBit("isReMaterializable");
323 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
324 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Andrew Trick52363bd2011-09-20 18:22:31 +0000325 hasPostISelHook = R->getValueAsBit("hasPostISelHook");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000326 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
327 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Quentin Colombetd533cdf2014-08-11 22:17:14 +0000328 isRegSequence = R->getValueAsBit("isRegSequence");
Quentin Colombet7e75cba2014-08-20 21:51:26 +0000329 isExtractSubreg = R->getValueAsBit("isExtractSubreg");
Quentin Colombet7e3da662014-08-20 23:49:36 +0000330 isInsertSubreg = R->getValueAsBit("isInsertSubreg");
Owen Andersonabaa5232015-05-28 18:33:39 +0000331 isConvergent = R->getValueAsBit("isConvergent");
Matthias Braun8e0a7342016-03-01 20:03:11 +0000332 hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo");
Simon Dardis13de5552018-05-22 14:36:58 +0000333 FastISelShouldIgnore = R->getValueAsBit("FastISelShouldIgnore");
Jakob Stoklund Olesenaf507bf2012-08-23 19:34:46 +0000334
Craig Topperbc9486b2014-02-05 09:10:40 +0000335 bool Unset;
336 mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset);
337 mayLoad_Unset = Unset;
338 mayStore = R->getValueAsBitOrUnset("mayStore", Unset);
339 mayStore_Unset = Unset;
340 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset);
341 hasSideEffects_Unset = Unset;
Jakob Stoklund Olesenaf507bf2012-08-23 19:34:46 +0000342
Chris Lattnerd8adec72010-11-01 04:03:32 +0000343 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
344 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
345 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Jim Grosbach4c08a9f2011-07-07 00:48:02 +0000346 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000347 isPseudo = R->getValueAsBit("isPseudo");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000348 ImplicitDefs = R->getValueAsListOfDefs("Defs");
349 ImplicitUses = R->getValueAsListOfDefs("Uses");
350
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000351 // This flag is only inferred from the pattern.
352 hasChain = false;
353 hasChain_Inferred = false;
354
Chris Lattnerd8adec72010-11-01 04:03:32 +0000355 // Parse Constraints.
356 ParseConstraints(R->getValueAsString("Constraints"), Operands);
357
358 // Parse the DisableEncoding field.
359 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
Joey Gouly0e76fa72013-09-12 10:28:05 +0000360
361 // First check for a ComplexDeprecationPredicate.
362 if (R->getValue("ComplexDeprecationPredicate")) {
363 HasComplexDeprecationPredicate = true;
364 DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate");
365 } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) {
366 // Check if we have a Subtarget feature mask.
367 HasComplexDeprecationPredicate = false;
368 DeprecatedReason = Dep->getValue()->getAsString();
369 } else {
370 // This instruction isn't deprecated.
371 HasComplexDeprecationPredicate = false;
372 DeprecatedReason = "";
373 }
Chris Lattnerd8adec72010-11-01 04:03:32 +0000374}
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000375
376/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
377/// implicit def and it has a known VT, return the VT, otherwise return
378/// MVT::Other.
379MVT::SimpleValueType CodeGenInstruction::
380HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
381 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000382
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000383 // Check to see if the first implicit def has a resolvable type.
384 Record *FirstImplicitDef = ImplicitDefs[0];
385 assert(FirstImplicitDef->isSubClassOf("Register"));
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000386 const std::vector<ValueTypeByHwMode> &RegVTs =
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000387 TargetInfo.getRegisterVTs(FirstImplicitDef);
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000388 if (RegVTs.size() == 1 && RegVTs[0].isSimple())
389 return RegVTs[0].getSimple().SimpleTy;
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000390 return MVT::Other;
391}
392
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000393
394/// FlattenAsmStringVariants - Flatten the specified AsmString to only
395/// include text from the specified variant, returning the new string.
396std::string CodeGenInstruction::
397FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
398 std::string Res = "";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000399
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000400 for (;;) {
401 // Find the start of the next variant string.
402 size_t VariantsStart = 0;
403 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
404 if (Cur[VariantsStart] == '{' &&
405 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
406 Cur[VariantsStart-1] != '\\')))
407 break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000408
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000409 // Add the prefix to the result.
410 Res += Cur.slice(0, VariantsStart);
411 if (VariantsStart == Cur.size())
412 break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000413
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000414 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000415
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000416 // Scan to the end of the variants string.
417 size_t VariantsEnd = VariantsStart;
418 unsigned NestedBraces = 1;
419 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
420 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
421 if (--NestedBraces == 0)
422 break;
423 } else if (Cur[VariantsEnd] == '{')
424 ++NestedBraces;
425 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000426
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000427 // Select the Nth variant (or empty).
428 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
429 for (unsigned i = 0; i != Variant; ++i)
430 Selection = Selection.split('|').second;
431 Res += Selection.split('|').first;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000432
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000433 assert(VariantsEnd != Cur.size() &&
434 "Unterminated variants in assembly string!");
435 Cur = Cur.substr(VariantsEnd + 1);
436 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000437
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000438 return Res;
439}
440
Daniel Sandersc54aa9c2017-11-18 00:16:44 +0000441bool CodeGenInstruction::isOperandAPointer(unsigned i) const {
442 if (DagInit *ConstraintList = TheDef->getValueAsDag("InOperandList")) {
443 if (i < ConstraintList->getNumArgs()) {
444 if (DefInit *Constraint = dyn_cast<DefInit>(ConstraintList->getArg(i))) {
445 return Constraint->getDef()->isSubClassOf("TypedOperand") &&
446 Constraint->getDef()->getValueAsBit("IsPointer");
447 }
448 }
449 }
450 return false;
451}
Chris Lattner488c2012010-11-01 04:05:41 +0000452
453//===----------------------------------------------------------------------===//
454/// CodeGenInstAlias Implementation
455//===----------------------------------------------------------------------===//
456
Bob Wilsonb9b24222011-01-26 19:44:55 +0000457/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
458/// constructor. It checks if an argument in an InstAlias pattern matches
459/// the corresponding operand of the instruction. It returns true on a
460/// successful match, with ResOp set to the result operand to be used.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000461bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
Bob Wilsonb9b24222011-01-26 19:44:55 +0000462 Record *InstOpRec, bool hasSubOps,
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +0000463 ArrayRef<SMLoc> Loc, CodeGenTarget &T,
Bob Wilsonb9b24222011-01-26 19:44:55 +0000464 ResultOperand &ResOp) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000465 Init *Arg = Result->getArg(AliasOpNo);
Sean Silvafb509ed2012-10-10 20:24:43 +0000466 DefInit *ADI = dyn_cast<DefInit>(Arg);
Craig Topper24064772014-04-15 07:20:03 +0000467 Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
Bob Wilsonb9b24222011-01-26 19:44:55 +0000468
469 if (ADI && ADI->getDef() == InstOpRec) {
470 // If the operand is a record, it must have a name, and the record type
471 // must match up with the instruction's argument type.
Matthias Braunbb053162016-12-05 06:00:46 +0000472 if (!Result->getArgName(AliasOpNo))
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000473 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
474 " must have a name!");
Matthias Braunbb053162016-12-05 06:00:46 +0000475 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
Bob Wilsonb9b24222011-01-26 19:44:55 +0000476 return true;
477 }
478
Jim Grosbach6acb1482011-10-28 16:43:40 +0000479 // For register operands, the source register class can be a subclass
480 // of the instruction register class, not just an exact match.
Tim Northover3e38d292014-03-29 09:03:22 +0000481 if (InstOpRec->isSubClassOf("RegisterOperand"))
482 InstOpRec = InstOpRec->getValueAsDef("RegClass");
483
484 if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand"))
485 ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit();
486
Jim Grosbach6acb1482011-10-28 16:43:40 +0000487 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
488 if (!InstOpRec->isSubClassOf("RegisterClass"))
489 return false;
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000490 if (!T.getRegisterClass(InstOpRec)
491 .hasSubClass(&T.getRegisterClass(ADI->getDef())))
492 return false;
Matthias Braunbb053162016-12-05 06:00:46 +0000493 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000494 return true;
Jim Grosbach6acb1482011-10-28 16:43:40 +0000495 }
496
Bob Wilsonb9b24222011-01-26 19:44:55 +0000497 // Handle explicit registers.
498 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
Jim Grosbacha7b2d442011-08-19 20:33:06 +0000499 if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
500 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
501 // The operand info should only have a single (register) entry. We
502 // want the register class of it.
Sean Silva88eb8dd2012-10-10 20:24:47 +0000503 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
Jim Grosbacha7b2d442011-08-19 20:33:06 +0000504 }
505
Bob Wilsonb9b24222011-01-26 19:44:55 +0000506 if (!InstOpRec->isSubClassOf("RegisterClass"))
507 return false;
508
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000509 if (!T.getRegisterClass(InstOpRec)
510 .contains(T.getRegBank().getReg(ADI->getDef())))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000511 PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
512 " is not a member of the " + InstOpRec->getName() +
513 " register class!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000514
Matthias Braunbb053162016-12-05 06:00:46 +0000515 if (Result->getArgName(AliasOpNo))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000516 PrintFatalError(Loc, "result fixed register argument must "
517 "not have a name!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000518
Tim Northover3e38d292014-03-29 09:03:22 +0000519 ResOp = ResultOperand(ResultRecord);
Bob Wilsonb9b24222011-01-26 19:44:55 +0000520 return true;
521 }
522
523 // Handle "zero_reg" for optional def operands.
524 if (ADI && ADI->getDef()->getName() == "zero_reg") {
525
526 // Check if this is an optional def.
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000527 // Tied operands where the source is a sub-operand of a complex operand
528 // need to represent both operands in the alias destination instruction.
529 // Allow zero_reg for the tied portion. This can and should go away once
530 // the MC representation of things doesn't use tied operands at all.
531 //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
532 // throw TGError(Loc, "reg0 used for result that is not an "
533 // "OptionalDefOperand!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000534
Craig Topper24064772014-04-15 07:20:03 +0000535 ResOp = ResultOperand(static_cast<Record*>(nullptr));
Bob Wilsonb9b24222011-01-26 19:44:55 +0000536 return true;
537 }
538
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000539 // Literal integers.
Sean Silvafb509ed2012-10-10 20:24:43 +0000540 if (IntInit *II = dyn_cast<IntInit>(Arg)) {
Bob Wilsonb9b24222011-01-26 19:44:55 +0000541 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
542 return false;
543 // Integer arguments can't have names.
Matthias Braunbb053162016-12-05 06:00:46 +0000544 if (Result->getArgName(AliasOpNo))
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000545 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000546 " must not have a name!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000547 ResOp = ResultOperand(II->getValue());
548 return true;
549 }
550
Pete Cooper2cfdfe52014-08-07 05:47:04 +0000551 // Bits<n> (also used for 0bxx literals)
552 if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
553 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
554 return false;
555 if (!BI->isComplete())
556 return false;
557 // Convert the bits init to an integer and use that for the result.
558 IntInit *II =
559 dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get()));
560 if (!II)
561 return false;
562 ResOp = ResultOperand(II->getValue());
563 return true;
564 }
565
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000566 // If both are Operands with the same MVT, allow the conversion. It's
567 // up to the user to make sure the values are appropriate, just like
568 // for isel Pat's.
Michael Ilseman5be22a12014-12-12 21:48:03 +0000569 if (InstOpRec->isSubClassOf("Operand") && ADI &&
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000570 ADI->getDef()->isSubClassOf("Operand")) {
571 // FIXME: What other attributes should we check here? Identical
572 // MIOperandInfo perhaps?
573 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
574 return false;
Matthias Braunbb053162016-12-05 06:00:46 +0000575 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ADI->getDef());
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000576 return true;
577 }
578
Bob Wilsonb9b24222011-01-26 19:44:55 +0000579 return false;
580}
581
Tim Northover60091cf2014-05-15 13:36:01 +0000582unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
583 if (!isRecord())
584 return 1;
585
586 Record *Rec = getRecord();
587 if (!Rec->isSubClassOf("Operand"))
588 return 1;
589
590 DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
591 if (MIOpInfo->getNumArgs() == 0) {
592 // Unspecified, so it defaults to 1
593 return 1;
594 }
595
596 return MIOpInfo->getNumArgs();
597}
598
Craig Topper2be74392018-06-18 01:28:01 +0000599CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T)
Tim Northoverd8d65a62014-05-15 11:16:32 +0000600 : TheDef(R) {
Chris Lattnerdd3b09c2010-11-01 05:34:34 +0000601 Result = R->getValueAsDag("ResultInst");
Tim Northoverd8d65a62014-05-15 11:16:32 +0000602 AsmString = R->getValueAsString("AsmString");
Tim Northoverd8d65a62014-05-15 11:16:32 +0000603
Chris Lattner8ffd1292010-11-06 06:39:47 +0000604
605 // Verify that the root of the result is an instruction.
Sean Silvafb509ed2012-10-10 20:24:43 +0000606 DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
Craig Topper24064772014-04-15 07:20:03 +0000607 if (!DI || !DI->getDef()->isSubClassOf("Instruction"))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000608 PrintFatalError(R->getLoc(),
609 "result of inst alias should be an instruction");
Chris Lattner8ffd1292010-11-06 06:39:47 +0000610
611 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000612
Chris Lattnerb625dd22010-11-06 07:06:09 +0000613 // NameClass - If argument names are repeated, we need to verify they have
614 // the same class.
615 StringMap<Record*> NameClass;
Bob Wilsona48eacc2011-01-20 18:38:10 +0000616 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000617 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
Matthias Braunbb053162016-12-05 06:00:46 +0000618 if (!ADI || !Result->getArgName(i))
Bob Wilsona48eacc2011-01-20 18:38:10 +0000619 continue;
620 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
621 // $foo can exist multiple times in the result list, but it must have the
622 // same type.
Matthias Braunbb053162016-12-05 06:00:46 +0000623 Record *&Entry = NameClass[Result->getArgNameStr(i)];
Bob Wilsona48eacc2011-01-20 18:38:10 +0000624 if (Entry && Entry != ADI->getDef())
Matthias Braunbb053162016-12-05 06:00:46 +0000625 PrintFatalError(R->getLoc(), "result value $" + Result->getArgNameStr(i) +
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000626 " is both " + Entry->getName() + " and " +
627 ADI->getDef()->getName() + "!");
Bob Wilsona48eacc2011-01-20 18:38:10 +0000628 Entry = ADI->getDef();
629 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000630
Chris Lattner76030522010-11-06 06:54:38 +0000631 // Decode and validate the arguments of the result.
Chris Lattner8188fb22010-11-06 07:31:43 +0000632 unsigned AliasOpNo = 0;
633 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsonb9b24222011-01-26 19:44:55 +0000634
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000635 // Tied registers don't have an entry in the result dag unless they're part
636 // of a complex operand, in which case we include them anyways, as we
637 // don't have any other way to specify the whole operand.
638 if (ResultInst->Operands[i].MINumOperands == 1 &&
Sander de Smalen118099a2018-06-18 13:39:29 +0000639 ResultInst->Operands[i].getTiedRegister() != -1) {
640 // Tied operands of different RegisterClass should be explicit within an
641 // instruction's syntax and so cannot be skipped.
642 int TiedOpNum = ResultInst->Operands[i].getTiedRegister();
643 if (ResultInst->Operands[i].Rec->getName() ==
644 ResultInst->Operands[TiedOpNum].Rec->getName())
645 continue;
646 }
Chris Lattner8188fb22010-11-06 07:31:43 +0000647
648 if (AliasOpNo >= Result->getNumArgs())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000649 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000650
Bob Wilsonb9b24222011-01-26 19:44:55 +0000651 Record *InstOpRec = ResultInst->Operands[i].Rec;
652 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
653 ResultOperand ResOp(static_cast<int64_t>(0));
654 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
655 R->getLoc(), T, ResOp)) {
Owen Andersonda6bd3e2012-06-08 00:25:03 +0000656 // If this is a simple operand, or a complex operand with a custom match
657 // class, then we can match is verbatim.
658 if (NumSubOps == 1 ||
659 (InstOpRec->getValue("ParserMatchClass") &&
660 InstOpRec->getValueAsDef("ParserMatchClass")
661 ->getValueAsString("Name") != "Imm")) {
662 ResultOperands.push_back(ResOp);
663 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
664 ++AliasOpNo;
665
666 // Otherwise, we need to match each of the suboperands individually.
667 } else {
668 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
669 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000670 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Owen Andersonda6bd3e2012-06-08 00:25:03 +0000671
672 // Take care to instantiate each of the suboperands with the correct
673 // nomenclature: $foo.bar
Matthias Braunbb053162016-12-05 06:00:46 +0000674 ResultOperands.emplace_back(
675 Result->getArgName(AliasOpNo)->getAsUnquotedString() + "." +
676 MIOI->getArgName(SubOp)->getAsUnquotedString(), SubRec);
Owen Andersonda6bd3e2012-06-08 00:25:03 +0000677 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
678 }
679 ++AliasOpNo;
680 }
Chris Lattner76030522010-11-06 06:54:38 +0000681 continue;
682 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000683
Bob Wilsonb9b24222011-01-26 19:44:55 +0000684 // If the argument did not match the instruction operand, and the operand
685 // is composed of multiple suboperands, try matching the suboperands.
686 if (NumSubOps > 1) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000687 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
Bob Wilsonb9b24222011-01-26 19:44:55 +0000688 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
689 if (AliasOpNo >= Result->getNumArgs())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000690 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
Sean Silva88eb8dd2012-10-10 20:24:47 +0000691 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Bob Wilsonb9b24222011-01-26 19:44:55 +0000692 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
693 R->getLoc(), T, ResOp)) {
694 ResultOperands.push_back(ResOp);
695 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
696 ++AliasOpNo;
697 } else {
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000698 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
Bob Wilsonb9b24222011-01-26 19:44:55 +0000699 " does not match instruction operand class " +
700 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
701 }
702 }
Chris Lattnerb6f8e822010-11-06 19:25:43 +0000703 continue;
704 }
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000705 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000706 " does not match instruction operand class " +
707 InstOpRec->getName());
Chris Lattner76030522010-11-06 06:54:38 +0000708 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000709
Chris Lattner8188fb22010-11-06 07:31:43 +0000710 if (AliasOpNo != Result->getNumArgs())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000711 PrintFatalError(R->getLoc(), "too many operands for instruction!");
Chris Lattner488c2012010-11-01 04:05:41 +0000712}