blob: 6d06ba2c8b6768e6a1b651d3dfa9964776916143 [file] [log] [blame]
Chris Lattner6cc654b2008-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 Lattner9414ae52010-03-27 20:09:24 +000015#include "CodeGenTarget.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000016#include "llvm/ADT/STLExtras.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000017#include "llvm/ADT/StringExtras.h"
Chris Lattner3f2c8e42010-11-06 07:06:09 +000018#include "llvm/ADT/StringMap.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000019#include "llvm/TableGen/Error.h"
20#include "llvm/TableGen/Record.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000021#include <set>
22using namespace llvm;
23
Chris Lattnerc240bb02010-11-01 04:03:32 +000024//===----------------------------------------------------------------------===//
25// CGIOperandList Implementation
26//===----------------------------------------------------------------------===//
Jim Grosbach06801722009-12-16 19:43:02 +000027
Chris Lattnerc240bb02010-11-01 04:03:32 +000028CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
29 isPredicable = false;
Chris Lattner6cc654b2008-01-06 01:35:39 +000030 hasOptionalDef = false;
Chris Lattner8f707e12008-01-07 05:19:29 +000031 isVariadic = false;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000032
David Greene05bce0b2011-07-29 22:43:06 +000033 DagInit *OutDI = R->getValueAsDag("OutOperandList");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000034
Sean Silva6cfc8062012-10-10 20:24:43 +000035 if (DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000036 if (Init->getDef()->getName() != "outs")
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000037 PrintFatalError(R->getName() + ": invalid def name for output list: use 'outs'");
Chris Lattnercedef1c2010-03-18 20:50:52 +000038 } else
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000039 PrintFatalError(R->getName() + ": invalid output list: use 'outs'");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000040
Chris Lattnerf55eed22010-03-18 21:07:39 +000041 NumDefs = OutDI->getNumArgs();
Chris Lattnerc240bb02010-11-01 04:03:32 +000042
David Greene05bce0b2011-07-29 22:43:06 +000043 DagInit *InDI = R->getValueAsDag("InOperandList");
Sean Silva6cfc8062012-10-10 20:24:43 +000044 if (DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000045 if (Init->getDef()->getName() != "ins")
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000046 PrintFatalError(R->getName() + ": invalid def name for input list: use 'ins'");
Chris Lattnercedef1c2010-03-18 20:50:52 +000047 } else
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000048 PrintFatalError(R->getName() + ": invalid input list: use 'ins'");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000049
Chris Lattner6cc654b2008-01-06 01:35:39 +000050 unsigned MIOperandNo = 0;
51 std::set<std::string> OperandNames;
Reid Kleckner6ebd4e62016-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 Greene05bce0b2011-07-29 22:43:06 +000055 Init *ArgInit;
Matthias Braunddbd6db2016-12-05 06:00:46 +000056 StringRef ArgName;
Chris Lattnerf55eed22010-03-18 21:07:39 +000057 if (i < NumDefs) {
58 ArgInit = OutDI->getArg(i);
Matthias Braunddbd6db2016-12-05 06:00:46 +000059 ArgName = OutDI->getArgNameStr(i);
Chris Lattnerf55eed22010-03-18 21:07:39 +000060 } else {
61 ArgInit = InDI->getArg(i-NumDefs);
Matthias Braunddbd6db2016-12-05 06:00:46 +000062 ArgName = InDI->getArgNameStr(i-NumDefs);
Chris Lattnerf55eed22010-03-18 21:07:39 +000063 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000064
Sean Silva6cfc8062012-10-10 20:24:43 +000065 DefInit *Arg = dyn_cast<DefInit>(ArgInit);
Chris Lattner6cc654b2008-01-06 01:35:39 +000066 if (!Arg)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000067 PrintFatalError("Illegal operand for the '" + R->getName() + "' instruction!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000068
Chris Lattner6cc654b2008-01-06 01:35:39 +000069 Record *Rec = Arg->getDef();
70 std::string PrintMethod = "printOperand";
Jim Grosbach5013f742010-10-12 22:21:57 +000071 std::string EncoderMethod;
Benjamin Kramer5196c122011-07-14 21:47:18 +000072 std::string OperandType = "OPERAND_UNKNOWN";
Tom Stellardb461e832015-01-12 19:33:09 +000073 std::string OperandNamespace = "MCOI";
Chris Lattner6cc654b2008-01-06 01:35:39 +000074 unsigned NumOps = 1;
Craig Topper095734c2014-04-15 07:20:03 +000075 DagInit *MIOpInfo = nullptr;
Owen Andersonbea6f612011-06-27 21:06:21 +000076 if (Rec->isSubClassOf("RegisterOperand")) {
77 PrintMethod = Rec->getValueAsString("PrintMethod");
Tom Stellardb461e832015-01-12 19:33:09 +000078 OperandType = Rec->getValueAsString("OperandType");
79 OperandNamespace = Rec->getValueAsString("OperandNamespace");
Sam Kolton5188cfa2017-05-15 10:13:07 +000080 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Owen Andersonbea6f612011-06-27 21:06:21 +000081 } else if (Rec->isSubClassOf("Operand")) {
Chris Lattner6cc654b2008-01-06 01:35:39 +000082 PrintMethod = Rec->getValueAsString("PrintMethod");
Benjamin Kramer5196c122011-07-14 21:47:18 +000083 OperandType = Rec->getValueAsString("OperandType");
Dan Gohmanee083502015-12-22 23:37:37 +000084 OperandNamespace = Rec->getValueAsString("OperandNamespace");
Jim Grosbach5013f742010-10-12 22:21:57 +000085 // If there is an explicit encoder method, use it.
Chris Lattner2ac19022010-11-15 05:19:05 +000086 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattner6cc654b2008-01-06 01:35:39 +000087 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000088
Chris Lattner6cc654b2008-01-06 01:35:39 +000089 // Verify that MIOpInfo has an 'ops' root value.
Sean Silva3f7b7f82012-10-10 20:24:47 +000090 if (!isa<DefInit>(MIOpInfo->getOperator()) ||
91 cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000092 PrintFatalError("Bad value for MIOperandInfo in operand '" + Rec->getName() +
93 "'\n");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000094
Chris Lattner6cc654b2008-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 Takumie5fffe92011-01-26 02:03:37 +000099
Tim Northoverf7ab3a82013-08-22 09:57:11 +0000100 if (Rec->isSubClassOf("PredicateOp"))
Chris Lattner6cc654b2008-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 Lattner8f707e12008-01-07 05:19:29 +0000105 isVariadic = true;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000106 continue;
Benjamin Kramer5196c122011-07-14 21:47:18 +0000107 } else if (Rec->isSubClassOf("RegisterClass")) {
108 OperandType = "OPERAND_REGISTER";
109 } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
Owen Anderson83c0eef2012-09-11 23:47:08 +0000110 !Rec->isSubClassOf("unknown_class"))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000111 PrintFatalError("Unknown operand class '" + Rec->getName() +
112 "' in '" + R->getName() + "' instruction!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000113
Chris Lattner6cc654b2008-01-06 01:35:39 +0000114 // Check that the operand has a name and that it's unique.
Chris Lattnerf55eed22010-03-18 21:07:39 +0000115 if (ArgName.empty())
Benjamin Kramerabe43b52014-03-29 17:17:15 +0000116 PrintFatalError("In instruction '" + R->getName() + "', operand #" +
117 Twine(i) + " has no name!");
Chris Lattnerf55eed22010-03-18 21:07:39 +0000118 if (!OperandNames.insert(ArgName).second)
Benjamin Kramerabe43b52014-03-29 17:17:15 +0000119 PrintFatalError("In instruction '" + R->getName() + "', operand #" +
120 Twine(i) + " has the same name as a previous operand!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000121
Benjamin Kramer9589ff82015-05-29 19:43:39 +0000122 OperandList.emplace_back(Rec, ArgName, PrintMethod, EncoderMethod,
123 OperandNamespace + "::" + OperandType, MIOperandNo,
124 NumOps, MIOpInfo);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000125 MIOperandNo += NumOps;
126 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000127
128
Chris Lattnerb501d4f2010-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 Absarf7351882017-10-06 09:32:45 +0000131 for (OperandInfo &OpInfo : OperandList)
132 OpInfo.Constraints.resize(OpInfo.MINumOperands);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000133}
134
Chris Lattnerc240bb02010-11-01 04:03:32 +0000135
Chris Lattner6cc654b2008-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 Sonnenberger61131ab2012-10-25 20:33:17 +0000138/// specified name, abort.
Chris Lattner6cc654b2008-01-06 01:35:39 +0000139///
Chris Lattnerc240bb02010-11-01 04:03:32 +0000140unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000141 unsigned OpIdx;
142 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
Benjamin Kramerabe43b52014-03-29 17:17:15 +0000143 PrintFatalError("'" + TheDef->getName() +
144 "' does not have an operand named '$" + Name + "'!");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000145}
146
Jim Grosbach01855072010-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 Lattnerc240bb02010-11-01 04:03:32 +0000150bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach01855072010-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 Grosbachf0a4fad2009-12-15 19:28:13 +0000160std::pair<unsigned,unsigned>
Chris Lattnerc240bb02010-11-01 04:03:32 +0000161CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattner6cc654b2008-01-06 01:35:39 +0000162 if (Op.empty() || Op[0] != '$')
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000163 PrintFatalError(TheDef->getName() + ": Illegal operand name: '" + Op + "'");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000164
Chris Lattner6cc654b2008-01-06 01:35:39 +0000165 std::string OpName = Op.substr(1);
166 std::string SubOpName;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000167
Chris Lattner6cc654b2008-01-06 01:35:39 +0000168 // Check to see if this is $foo.bar.
Benjamin Kramerbf60ce02016-11-30 10:01:11 +0000169 std::string::size_type DotIdx = OpName.find_first_of('.');
Chris Lattner6cc654b2008-01-06 01:35:39 +0000170 if (DotIdx != std::string::npos) {
171 SubOpName = OpName.substr(DotIdx+1);
172 if (SubOpName.empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000173 PrintFatalError(TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000174 OpName = OpName.substr(0, DotIdx);
175 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000176
Chris Lattner6cc654b2008-01-06 01:35:39 +0000177 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000178
Chris Lattner6cc654b2008-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 Sonnenberger61131ab2012-10-25 20:33:17 +0000183 PrintFatalError(TheDef->getName() + ": Illegal to refer to"
184 " whole operand part of complex operand '" + Op + "'");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000185
Chris Lattner6cc654b2008-01-06 01:35:39 +0000186 // Otherwise, return the operand.
187 return std::make_pair(OpIdx, 0U);
188 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000189
Chris Lattner6cc654b2008-01-06 01:35:39 +0000190 // Find the suboperand number involved.
David Greene05bce0b2011-07-29 22:43:06 +0000191 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
Craig Topper095734c2014-04-15 07:20:03 +0000192 if (!MIOpInfo)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000193 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000194
Chris Lattner6cc654b2008-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 Braunddbd6db2016-12-05 06:00:46 +0000197 if (MIOpInfo->getArgNameStr(i) == SubOpName)
Chris Lattner6cc654b2008-01-06 01:35:39 +0000198 return std::make_pair(OpIdx, i);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000199
Chris Lattner6cc654b2008-01-06 01:35:39 +0000200 // Otherwise, didn't find it!
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000201 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
Aaron Ballman33695992013-08-16 01:43:31 +0000202 return std::make_pair(0U, 0U);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000203}
Chris Lattner9414ae52010-03-27 20:09:24 +0000204
Simon Tatham4b88bad2018-11-28 11:43:49 +0000205static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops,
206 Record *Rec) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000207 // EARLY_CLOBBER: @early $reg
208 std::string::size_type wpos = CStr.find_first_of(" \t");
209 std::string::size_type start = CStr.find_first_not_of(" \t");
210 std::string Tok = CStr.substr(start, wpos - start);
211 if (Tok == "@earlyclobber") {
212 std::string Name = CStr.substr(wpos+1);
213 wpos = Name.find_first_not_of(" \t");
214 if (wpos == std::string::npos)
Simon Tatham4b88bad2018-11-28 11:43:49 +0000215 PrintFatalError(
216 Rec->getLoc(), "Illegal format for @earlyclobber constraint in '" +
217 Rec->getName() + "': '" + CStr + "'");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000218 Name = Name.substr(wpos);
219 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000220
Chris Lattnerc240bb02010-11-01 04:03:32 +0000221 // Build the string for the operand
222 if (!Ops[Op.first].Constraints[Op.second].isNone())
Simon Tatham4b88bad2018-11-28 11:43:49 +0000223 PrintFatalError(
224 Rec->getLoc(), "Operand '" + Name + "' of '" + Rec->getName() +
225 "' cannot have multiple constraints!");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000226 Ops[Op.first].Constraints[Op.second] =
227 CGIOperandList::ConstraintInfo::getEarlyClobber();
228 return;
229 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000230
Chris Lattnerc240bb02010-11-01 04:03:32 +0000231 // Only other constraint is "TIED_TO" for now.
232 std::string::size_type pos = CStr.find_first_of('=');
Simon Tatham4b88bad2018-11-28 11:43:49 +0000233 if (pos == std::string::npos)
234 PrintFatalError(
235 Rec->getLoc(), "Unrecognized constraint '" + CStr +
236 "' in '" + Rec->getName() + "'");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000237 start = CStr.find_first_not_of(" \t");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000238
Chris Lattnerc240bb02010-11-01 04:03:32 +0000239 // TIED_TO: $src1 = $dst
Simon Tatham4b88bad2018-11-28 11:43:49 +0000240 wpos = CStr.find_first_of(" \t", start);
241 if (wpos == std::string::npos || wpos > pos)
242 PrintFatalError(
243 Rec->getLoc(), "Illegal format for tied-to constraint in '" +
244 Rec->getName() + "': '" + CStr + "'");
245 std::string LHSOpName = StringRef(CStr).substr(start, wpos - start);
246 std::pair<unsigned,unsigned> LHSOp = Ops.ParseOperandName(LHSOpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000247
Simon Tatham4b88bad2018-11-28 11:43:49 +0000248 wpos = CStr.find_first_not_of(" \t", pos + 1);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000249 if (wpos == std::string::npos)
Simon Tatham4b88bad2018-11-28 11:43:49 +0000250 PrintFatalError(
251 Rec->getLoc(), "Illegal format for tied-to constraint: '" + CStr + "'");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000252
Simon Tatham4b88bad2018-11-28 11:43:49 +0000253 std::string RHSOpName = StringRef(CStr).substr(wpos);
254 std::pair<unsigned,unsigned> RHSOp = Ops.ParseOperandName(RHSOpName, false);
255
256 // Sort the operands into order, which should put the output one
257 // first. But keep the original order, for use in diagnostics.
258 bool FirstIsDest = (LHSOp < RHSOp);
259 std::pair<unsigned,unsigned> DestOp = (FirstIsDest ? LHSOp : RHSOp);
260 StringRef DestOpName = (FirstIsDest ? LHSOpName : RHSOpName);
261 std::pair<unsigned,unsigned> SrcOp = (FirstIsDest ? RHSOp : LHSOp);
262 StringRef SrcOpName = (FirstIsDest ? RHSOpName : LHSOpName);
263
264 // Ensure one operand is a def and the other is a use.
265 if (DestOp.first >= Ops.NumDefs)
266 PrintFatalError(
267 Rec->getLoc(), "Input operands '" + LHSOpName + "' and '" + RHSOpName +
268 "' of '" + Rec->getName() + "' cannot be tied!");
269 if (SrcOp.first < Ops.NumDefs)
270 PrintFatalError(
271 Rec->getLoc(), "Output operands '" + LHSOpName + "' and '" + RHSOpName +
272 "' of '" + Rec->getName() + "' cannot be tied!");
273
274 // The constraint has to go on the operand with higher index, i.e.
275 // the source one. Check there isn't another constraint there
276 // already.
277 if (!Ops[SrcOp.first].Constraints[SrcOp.second].isNone())
278 PrintFatalError(
279 Rec->getLoc(), "Operand '" + SrcOpName + "' of '" + Rec->getName() +
280 "' cannot have multiple constraints!");
281
282 unsigned DestFlatOpNo = Ops.getFlattenedOperandNumber(DestOp);
283 auto NewConstraint = CGIOperandList::ConstraintInfo::getTied(DestFlatOpNo);
284
285 // Check that the earlier operand is not the target of another tie
286 // before making it the target of this one.
287 for (const CGIOperandList::OperandInfo &Op : Ops) {
288 for (unsigned i = 0; i < Op.MINumOperands; i++)
289 if (Op.Constraints[i] == NewConstraint)
290 PrintFatalError(
291 Rec->getLoc(), "Operand '" + DestOpName + "' of '" + Rec->getName() +
292 "' cannot have multiple operands tied to it!");
Lang Hames64486732012-10-20 22:44:13 +0000293 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000294
Simon Tatham4b88bad2018-11-28 11:43:49 +0000295 Ops[SrcOp.first].Constraints[SrcOp.second] = NewConstraint;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000296}
297
Simon Tatham4b88bad2018-11-28 11:43:49 +0000298static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops,
299 Record *Rec) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000300 if (CStr.empty()) return;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000301
Chris Lattnerc240bb02010-11-01 04:03:32 +0000302 const std::string delims(",");
303 std::string::size_type bidx, eidx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000304
Chris Lattnerc240bb02010-11-01 04:03:32 +0000305 bidx = CStr.find_first_not_of(delims);
306 while (bidx != std::string::npos) {
307 eidx = CStr.find_first_of(delims, bidx);
308 if (eidx == std::string::npos)
309 eidx = CStr.length();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000310
Simon Tatham4b88bad2018-11-28 11:43:49 +0000311 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops, Rec);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000312 bidx = CStr.find_first_not_of(delims, eidx);
313 }
314}
315
316void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
317 while (1) {
Chris Lattnerc30a38f2011-07-21 06:21:31 +0000318 std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
319 std::string OpName = P.first;
320 DisableEncoding = P.second;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000321 if (OpName.empty()) break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000322
Chris Lattnerc240bb02010-11-01 04:03:32 +0000323 // Figure out which operand this is.
324 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000325
Chris Lattnerc240bb02010-11-01 04:03:32 +0000326 // Mark the operand as not-to-be encoded.
327 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
328 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
329 OperandList[Op.first].DoNotEncode[Op.second] = true;
330 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000331
Chris Lattnerc240bb02010-11-01 04:03:32 +0000332}
333
334//===----------------------------------------------------------------------===//
335// CodeGenInstruction Implementation
336//===----------------------------------------------------------------------===//
337
Jakob Stoklund Olesen912519a2012-08-24 00:31:16 +0000338CodeGenInstruction::CodeGenInstruction(Record *R)
Craig Topper095734c2014-04-15 07:20:03 +0000339 : TheDef(R), Operands(R), InferredFrom(nullptr) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000340 Namespace = R->getValueAsString("Namespace");
341 AsmString = R->getValueAsString("AsmString");
342
343 isReturn = R->getValueAsBit("isReturn");
Heejin Ahn408053d2018-08-21 19:44:11 +0000344 isEHScopeReturn = R->getValueAsBit("isEHScopeReturn");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000345 isBranch = R->getValueAsBit("isBranch");
346 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
347 isCompare = R->getValueAsBit("isCompare");
Evan Chengc4af4632010-11-17 20:13:28 +0000348 isMoveImm = R->getValueAsBit("isMoveImm");
Petar Jovanovic4d979512018-05-23 15:28:28 +0000349 isMoveReg = R->getValueAsBit("isMoveReg");
Evan Cheng0f040a22011-03-15 05:09:26 +0000350 isBitcast = R->getValueAsBit("isBitcast");
Jakob Stoklund Olesenf2c64ef2012-08-16 23:11:47 +0000351 isSelect = R->getValueAsBit("isSelect");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000352 isBarrier = R->getValueAsBit("isBarrier");
353 isCall = R->getValueAsBit("isCall");
Sjoerd Meijer82d457b2016-09-14 08:20:03 +0000354 isAdd = R->getValueAsBit("isAdd");
Joel Galenson83529882018-07-13 15:19:33 +0000355 isTrap = R->getValueAsBit("isTrap");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000356 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000357 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
358 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
359 isCommutable = R->getValueAsBit("isCommutable");
360 isTerminator = R->getValueAsBit("isTerminator");
361 isReMaterializable = R->getValueAsBit("isReMaterializable");
362 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
363 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Andrew Trick83a80312011-09-20 18:22:31 +0000364 hasPostISelHook = R->getValueAsBit("hasPostISelHook");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000365 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
366 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Quentin Colombet1b425402014-08-11 22:17:14 +0000367 isRegSequence = R->getValueAsBit("isRegSequence");
Quentin Colombetdac67642014-08-20 21:51:26 +0000368 isExtractSubreg = R->getValueAsBit("isExtractSubreg");
Quentin Colombet0d152132014-08-20 23:49:36 +0000369 isInsertSubreg = R->getValueAsBit("isInsertSubreg");
Owen Anderson2f6ca832015-05-28 18:33:39 +0000370 isConvergent = R->getValueAsBit("isConvergent");
Matthias Braun279476d2016-03-01 20:03:11 +0000371 hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo");
Simon Dardisf690e9b2018-05-22 14:36:58 +0000372 FastISelShouldIgnore = R->getValueAsBit("FastISelShouldIgnore");
Oliver Stannard9639b462018-12-03 10:32:42 +0000373 variadicOpsAreDefs = R->getValueAsBit("variadicOpsAreDefs");
Jakob Stoklund Olesenc1f10fd2012-08-23 19:34:46 +0000374
Craig Topper51dd7652014-02-05 09:10:40 +0000375 bool Unset;
376 mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset);
377 mayLoad_Unset = Unset;
378 mayStore = R->getValueAsBitOrUnset("mayStore", Unset);
379 mayStore_Unset = Unset;
380 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset);
381 hasSideEffects_Unset = Unset;
Jakob Stoklund Olesenc1f10fd2012-08-23 19:34:46 +0000382
Chris Lattnerc240bb02010-11-01 04:03:32 +0000383 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
384 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
385 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Jim Grosbache727d672011-07-07 00:48:02 +0000386 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
Jim Grosbach806fcc02011-07-06 21:33:38 +0000387 isPseudo = R->getValueAsBit("isPseudo");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000388 ImplicitDefs = R->getValueAsListOfDefs("Defs");
389 ImplicitUses = R->getValueAsListOfDefs("Uses");
390
Ulrich Weigand3a904262018-07-13 13:18:00 +0000391 // This flag is only inferred from the pattern.
392 hasChain = false;
393 hasChain_Inferred = false;
394
Chris Lattnerc240bb02010-11-01 04:03:32 +0000395 // Parse Constraints.
Simon Tatham4b88bad2018-11-28 11:43:49 +0000396 ParseConstraints(R->getValueAsString("Constraints"), Operands, R);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000397
398 // Parse the DisableEncoding field.
399 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
Joey Gouly715d98d2013-09-12 10:28:05 +0000400
401 // First check for a ComplexDeprecationPredicate.
402 if (R->getValue("ComplexDeprecationPredicate")) {
403 HasComplexDeprecationPredicate = true;
404 DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate");
405 } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) {
406 // Check if we have a Subtarget feature mask.
407 HasComplexDeprecationPredicate = false;
408 DeprecatedReason = Dep->getValue()->getAsString();
409 } else {
410 // This instruction isn't deprecated.
411 HasComplexDeprecationPredicate = false;
412 DeprecatedReason = "";
413 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000414}
Chris Lattner9414ae52010-03-27 20:09:24 +0000415
416/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
417/// implicit def and it has a known VT, return the VT, otherwise return
418/// MVT::Other.
419MVT::SimpleValueType CodeGenInstruction::
420HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
421 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000422
Chris Lattner9414ae52010-03-27 20:09:24 +0000423 // Check to see if the first implicit def has a resolvable type.
424 Record *FirstImplicitDef = ImplicitDefs[0];
425 assert(FirstImplicitDef->isSubClassOf("Register"));
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +0000426 const std::vector<ValueTypeByHwMode> &RegVTs =
Chris Lattner9414ae52010-03-27 20:09:24 +0000427 TargetInfo.getRegisterVTs(FirstImplicitDef);
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +0000428 if (RegVTs.size() == 1 && RegVTs[0].isSimple())
429 return RegVTs[0].getSimple().SimpleTy;
Chris Lattner9414ae52010-03-27 20:09:24 +0000430 return MVT::Other;
431}
432
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000433
434/// FlattenAsmStringVariants - Flatten the specified AsmString to only
435/// include text from the specified variant, returning the new string.
436std::string CodeGenInstruction::
437FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
438 std::string Res = "";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000439
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000440 for (;;) {
441 // Find the start of the next variant string.
442 size_t VariantsStart = 0;
443 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
444 if (Cur[VariantsStart] == '{' &&
445 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
446 Cur[VariantsStart-1] != '\\')))
447 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000448
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000449 // Add the prefix to the result.
450 Res += Cur.slice(0, VariantsStart);
451 if (VariantsStart == Cur.size())
452 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000453
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000454 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000455
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000456 // Scan to the end of the variants string.
457 size_t VariantsEnd = VariantsStart;
458 unsigned NestedBraces = 1;
459 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
460 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
461 if (--NestedBraces == 0)
462 break;
463 } else if (Cur[VariantsEnd] == '{')
464 ++NestedBraces;
465 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000466
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000467 // Select the Nth variant (or empty).
468 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
469 for (unsigned i = 0; i != Variant; ++i)
470 Selection = Selection.split('|').second;
471 Res += Selection.split('|').first;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000472
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000473 assert(VariantsEnd != Cur.size() &&
474 "Unterminated variants in assembly string!");
475 Cur = Cur.substr(VariantsEnd + 1);
476 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000477
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000478 return Res;
479}
480
Daniel Sanders603ba132017-11-18 00:16:44 +0000481bool CodeGenInstruction::isOperandAPointer(unsigned i) const {
482 if (DagInit *ConstraintList = TheDef->getValueAsDag("InOperandList")) {
483 if (i < ConstraintList->getNumArgs()) {
484 if (DefInit *Constraint = dyn_cast<DefInit>(ConstraintList->getArg(i))) {
485 return Constraint->getDef()->isSubClassOf("TypedOperand") &&
486 Constraint->getDef()->getValueAsBit("IsPointer");
487 }
488 }
489 }
490 return false;
491}
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000492
493//===----------------------------------------------------------------------===//
494/// CodeGenInstAlias Implementation
495//===----------------------------------------------------------------------===//
496
Bob Wilsona49c7df2011-01-26 19:44:55 +0000497/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
498/// constructor. It checks if an argument in an InstAlias pattern matches
499/// the corresponding operand of the instruction. It returns true on a
500/// successful match, with ResOp set to the result operand to be used.
David Greene05bce0b2011-07-29 22:43:06 +0000501bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000502 Record *InstOpRec, bool hasSubOps,
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +0000503 ArrayRef<SMLoc> Loc, CodeGenTarget &T,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000504 ResultOperand &ResOp) {
David Greene05bce0b2011-07-29 22:43:06 +0000505 Init *Arg = Result->getArg(AliasOpNo);
Sean Silva6cfc8062012-10-10 20:24:43 +0000506 DefInit *ADI = dyn_cast<DefInit>(Arg);
Craig Topper095734c2014-04-15 07:20:03 +0000507 Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000508
509 if (ADI && ADI->getDef() == InstOpRec) {
510 // If the operand is a record, it must have a name, and the record type
511 // must match up with the instruction's argument type.
Matthias Braunddbd6db2016-12-05 06:00:46 +0000512 if (!Result->getArgName(AliasOpNo))
Benjamin Kramerabe43b52014-03-29 17:17:15 +0000513 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
514 " must have a name!");
Matthias Braunddbd6db2016-12-05 06:00:46 +0000515 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
Bob Wilsona49c7df2011-01-26 19:44:55 +0000516 return true;
517 }
518
Jim Grosbachbe5d6bc2011-10-28 16:43:40 +0000519 // For register operands, the source register class can be a subclass
520 // of the instruction register class, not just an exact match.
Tim Northover69bd9572014-03-29 09:03:22 +0000521 if (InstOpRec->isSubClassOf("RegisterOperand"))
522 InstOpRec = InstOpRec->getValueAsDef("RegClass");
523
524 if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand"))
525 ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit();
526
Jim Grosbachbe5d6bc2011-10-28 16:43:40 +0000527 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
528 if (!InstOpRec->isSubClassOf("RegisterClass"))
529 return false;
Jim Grosbach48c1f842011-10-28 22:32:53 +0000530 if (!T.getRegisterClass(InstOpRec)
531 .hasSubClass(&T.getRegisterClass(ADI->getDef())))
532 return false;
Matthias Braunddbd6db2016-12-05 06:00:46 +0000533 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
Jim Grosbach48c1f842011-10-28 22:32:53 +0000534 return true;
Jim Grosbachbe5d6bc2011-10-28 16:43:40 +0000535 }
536
Bob Wilsona49c7df2011-01-26 19:44:55 +0000537 // Handle explicit registers.
538 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
Jim Grosbachc68e9272011-08-19 20:33:06 +0000539 if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
540 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
541 // The operand info should only have a single (register) entry. We
542 // want the register class of it.
Sean Silva3f7b7f82012-10-10 20:24:47 +0000543 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
Jim Grosbachc68e9272011-08-19 20:33:06 +0000544 }
545
Bob Wilsona49c7df2011-01-26 19:44:55 +0000546 if (!InstOpRec->isSubClassOf("RegisterClass"))
547 return false;
548
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000549 if (!T.getRegisterClass(InstOpRec)
550 .contains(T.getRegBank().getReg(ADI->getDef())))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000551 PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
552 " is not a member of the " + InstOpRec->getName() +
553 " register class!");
Bob Wilsona49c7df2011-01-26 19:44:55 +0000554
Matthias Braunddbd6db2016-12-05 06:00:46 +0000555 if (Result->getArgName(AliasOpNo))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000556 PrintFatalError(Loc, "result fixed register argument must "
557 "not have a name!");
Bob Wilsona49c7df2011-01-26 19:44:55 +0000558
Tim Northover69bd9572014-03-29 09:03:22 +0000559 ResOp = ResultOperand(ResultRecord);
Bob Wilsona49c7df2011-01-26 19:44:55 +0000560 return true;
561 }
562
563 // Handle "zero_reg" for optional def operands.
564 if (ADI && ADI->getDef()->getName() == "zero_reg") {
565
566 // Check if this is an optional def.
Jim Grosbachbfc94292011-11-15 01:46:57 +0000567 // Tied operands where the source is a sub-operand of a complex operand
568 // need to represent both operands in the alias destination instruction.
569 // Allow zero_reg for the tied portion. This can and should go away once
570 // the MC representation of things doesn't use tied operands at all.
571 //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
572 // throw TGError(Loc, "reg0 used for result that is not an "
573 // "OptionalDefOperand!");
Bob Wilsona49c7df2011-01-26 19:44:55 +0000574
Craig Topper095734c2014-04-15 07:20:03 +0000575 ResOp = ResultOperand(static_cast<Record*>(nullptr));
Bob Wilsona49c7df2011-01-26 19:44:55 +0000576 return true;
577 }
578
Jim Grosbach48c1f842011-10-28 22:32:53 +0000579 // Literal integers.
Sean Silva6cfc8062012-10-10 20:24:43 +0000580 if (IntInit *II = dyn_cast<IntInit>(Arg)) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000581 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
582 return false;
583 // Integer arguments can't have names.
Matthias Braunddbd6db2016-12-05 06:00:46 +0000584 if (Result->getArgName(AliasOpNo))
Benjamin Kramerabe43b52014-03-29 17:17:15 +0000585 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000586 " must not have a name!");
Bob Wilsona49c7df2011-01-26 19:44:55 +0000587 ResOp = ResultOperand(II->getValue());
588 return true;
589 }
590
Pete Cooper2093e2c2014-08-07 05:47:04 +0000591 // Bits<n> (also used for 0bxx literals)
592 if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
593 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
594 return false;
595 if (!BI->isComplete())
596 return false;
597 // Convert the bits init to an integer and use that for the result.
598 IntInit *II =
599 dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get()));
600 if (!II)
601 return false;
602 ResOp = ResultOperand(II->getValue());
603 return true;
604 }
605
Jim Grosbach48c1f842011-10-28 22:32:53 +0000606 // If both are Operands with the same MVT, allow the conversion. It's
607 // up to the user to make sure the values are appropriate, just like
608 // for isel Pat's.
Michael Ilseman3f0e8832014-12-12 21:48:03 +0000609 if (InstOpRec->isSubClassOf("Operand") && ADI &&
Jim Grosbach48c1f842011-10-28 22:32:53 +0000610 ADI->getDef()->isSubClassOf("Operand")) {
611 // FIXME: What other attributes should we check here? Identical
612 // MIOperandInfo perhaps?
613 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
614 return false;
Matthias Braunddbd6db2016-12-05 06:00:46 +0000615 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ADI->getDef());
Jim Grosbach48c1f842011-10-28 22:32:53 +0000616 return true;
617 }
618
Bob Wilsona49c7df2011-01-26 19:44:55 +0000619 return false;
620}
621
Tim Northoverd0e93f22014-05-15 13:36:01 +0000622unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
623 if (!isRecord())
624 return 1;
625
626 Record *Rec = getRecord();
627 if (!Rec->isSubClassOf("Operand"))
628 return 1;
629
630 DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
631 if (MIOpInfo->getNumArgs() == 0) {
632 // Unspecified, so it defaults to 1
633 return 1;
634 }
635
636 return MIOpInfo->getNumArgs();
637}
638
Craig Topper50ea5bb2018-06-18 01:28:01 +0000639CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T)
Tim Northoverf61a4672014-05-15 11:16:32 +0000640 : TheDef(R) {
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000641 Result = R->getValueAsDag("ResultInst");
Tim Northoverf61a4672014-05-15 11:16:32 +0000642 AsmString = R->getValueAsString("AsmString");
Tim Northoverf61a4672014-05-15 11:16:32 +0000643
Chris Lattner225549f2010-11-06 06:39:47 +0000644
645 // Verify that the root of the result is an instruction.
Sean Silva6cfc8062012-10-10 20:24:43 +0000646 DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
Craig Topper095734c2014-04-15 07:20:03 +0000647 if (!DI || !DI->getDef()->isSubClassOf("Instruction"))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000648 PrintFatalError(R->getLoc(),
649 "result of inst alias should be an instruction");
Chris Lattner225549f2010-11-06 06:39:47 +0000650
651 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000652
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000653 // NameClass - If argument names are repeated, we need to verify they have
654 // the same class.
655 StringMap<Record*> NameClass;
Bob Wilson55931ab2011-01-20 18:38:10 +0000656 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000657 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
Matthias Braunddbd6db2016-12-05 06:00:46 +0000658 if (!ADI || !Result->getArgName(i))
Bob Wilson55931ab2011-01-20 18:38:10 +0000659 continue;
660 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
661 // $foo can exist multiple times in the result list, but it must have the
662 // same type.
Matthias Braunddbd6db2016-12-05 06:00:46 +0000663 Record *&Entry = NameClass[Result->getArgNameStr(i)];
Bob Wilson55931ab2011-01-20 18:38:10 +0000664 if (Entry && Entry != ADI->getDef())
Matthias Braunddbd6db2016-12-05 06:00:46 +0000665 PrintFatalError(R->getLoc(), "result value $" + Result->getArgNameStr(i) +
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000666 " is both " + Entry->getName() + " and " +
667 ADI->getDef()->getName() + "!");
Bob Wilson55931ab2011-01-20 18:38:10 +0000668 Entry = ADI->getDef();
669 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000670
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000671 // Decode and validate the arguments of the result.
Chris Lattner41409852010-11-06 07:31:43 +0000672 unsigned AliasOpNo = 0;
673 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000674
Jim Grosbachbfc94292011-11-15 01:46:57 +0000675 // Tied registers don't have an entry in the result dag unless they're part
676 // of a complex operand, in which case we include them anyways, as we
677 // don't have any other way to specify the whole operand.
678 if (ResultInst->Operands[i].MINumOperands == 1 &&
Sander de Smalenb0c87382018-06-18 13:39:29 +0000679 ResultInst->Operands[i].getTiedRegister() != -1) {
680 // Tied operands of different RegisterClass should be explicit within an
681 // instruction's syntax and so cannot be skipped.
682 int TiedOpNum = ResultInst->Operands[i].getTiedRegister();
683 if (ResultInst->Operands[i].Rec->getName() ==
684 ResultInst->Operands[TiedOpNum].Rec->getName())
685 continue;
686 }
Chris Lattner41409852010-11-06 07:31:43 +0000687
688 if (AliasOpNo >= Result->getNumArgs())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000689 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000690
Bob Wilsona49c7df2011-01-26 19:44:55 +0000691 Record *InstOpRec = ResultInst->Operands[i].Rec;
692 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
693 ResultOperand ResOp(static_cast<int64_t>(0));
694 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
695 R->getLoc(), T, ResOp)) {
Owen Anderson7e8921b2012-06-08 00:25:03 +0000696 // If this is a simple operand, or a complex operand with a custom match
697 // class, then we can match is verbatim.
698 if (NumSubOps == 1 ||
699 (InstOpRec->getValue("ParserMatchClass") &&
700 InstOpRec->getValueAsDef("ParserMatchClass")
701 ->getValueAsString("Name") != "Imm")) {
702 ResultOperands.push_back(ResOp);
703 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
704 ++AliasOpNo;
705
706 // Otherwise, we need to match each of the suboperands individually.
707 } else {
708 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
709 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
Sean Silva3f7b7f82012-10-10 20:24:47 +0000710 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Owen Anderson7e8921b2012-06-08 00:25:03 +0000711
712 // Take care to instantiate each of the suboperands with the correct
713 // nomenclature: $foo.bar
Matthias Braunddbd6db2016-12-05 06:00:46 +0000714 ResultOperands.emplace_back(
715 Result->getArgName(AliasOpNo)->getAsUnquotedString() + "." +
716 MIOI->getArgName(SubOp)->getAsUnquotedString(), SubRec);
Owen Anderson7e8921b2012-06-08 00:25:03 +0000717 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
718 }
719 ++AliasOpNo;
720 }
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000721 continue;
722 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000723
Bob Wilsona49c7df2011-01-26 19:44:55 +0000724 // If the argument did not match the instruction operand, and the operand
725 // is composed of multiple suboperands, try matching the suboperands.
726 if (NumSubOps > 1) {
David Greene05bce0b2011-07-29 22:43:06 +0000727 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000728 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
729 if (AliasOpNo >= Result->getNumArgs())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000730 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
Sean Silva3f7b7f82012-10-10 20:24:47 +0000731 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Bob Wilsona49c7df2011-01-26 19:44:55 +0000732 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
733 R->getLoc(), T, ResOp)) {
734 ResultOperands.push_back(ResOp);
735 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
736 ++AliasOpNo;
737 } else {
Benjamin Kramerabe43b52014-03-29 17:17:15 +0000738 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
Bob Wilsona49c7df2011-01-26 19:44:55 +0000739 " does not match instruction operand class " +
740 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
741 }
742 }
Chris Lattner98c870f2010-11-06 19:25:43 +0000743 continue;
744 }
Benjamin Kramerabe43b52014-03-29 17:17:15 +0000745 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000746 " does not match instruction operand class " +
747 InstOpRec->getName());
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000748 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000749
Chris Lattner41409852010-11-06 07:31:43 +0000750 if (AliasOpNo != Result->getNumArgs())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000751 PrintFatalError(R->getLoc(), "too many operands for instruction!");
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000752}