blob: bb2ec2a64e49e04360d2f23cdb3ef84f9bc35319 [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");
Owen Andersona84be6c2011-06-27 21:06:21 +000080 } else if (Rec->isSubClassOf("Operand")) {
Chris Lattnerea2d52d2008-01-06 01:35:39 +000081 PrintMethod = Rec->getValueAsString("PrintMethod");
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +000082 OperandType = Rec->getValueAsString("OperandType");
Dan Gohmandb70d012015-12-22 23:37:37 +000083 OperandNamespace = Rec->getValueAsString("OperandNamespace");
Jim Grosbach51a12eb2010-10-12 22:21:57 +000084 // If there is an explicit encoder method, use it.
Chris Lattner63274cb2010-11-15 05:19:05 +000085 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattnerea2d52d2008-01-06 01:35:39 +000086 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000087
Chris Lattnerea2d52d2008-01-06 01:35:39 +000088 // Verify that MIOpInfo has an 'ops' root value.
Sean Silva88eb8dd2012-10-10 20:24:47 +000089 if (!isa<DefInit>(MIOpInfo->getOperator()) ||
90 cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000091 PrintFatalError("Bad value for MIOperandInfo in operand '" + Rec->getName() +
92 "'\n");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000093
Chris Lattnerea2d52d2008-01-06 01:35:39 +000094 // If we have MIOpInfo, then we have #operands equal to number of entries
95 // in MIOperandInfo.
96 if (unsigned NumArgs = MIOpInfo->getNumArgs())
97 NumOps = NumArgs;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000098
Tim Northover42180442013-08-22 09:57:11 +000099 if (Rec->isSubClassOf("PredicateOp"))
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000100 isPredicable = true;
101 else if (Rec->isSubClassOf("OptionalDefOperand"))
102 hasOptionalDef = true;
103 } else if (Rec->getName() == "variable_ops") {
Chris Lattnerf376c992008-01-07 05:19:29 +0000104 isVariadic = true;
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000105 continue;
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +0000106 } else if (Rec->isSubClassOf("RegisterClass")) {
107 OperandType = "OPERAND_REGISTER";
108 } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
Owen Anderson16ba4b22012-09-11 23:47:08 +0000109 !Rec->isSubClassOf("unknown_class"))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000110 PrintFatalError("Unknown operand class '" + Rec->getName() +
111 "' in '" + R->getName() + "' instruction!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000112
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000113 // Check that the operand has a name and that it's unique.
Chris Lattner1f22d272010-03-18 21:07:39 +0000114 if (ArgName.empty())
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000115 PrintFatalError("In instruction '" + R->getName() + "', operand #" +
116 Twine(i) + " has no name!");
Chris Lattner1f22d272010-03-18 21:07:39 +0000117 if (!OperandNames.insert(ArgName).second)
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000118 PrintFatalError("In instruction '" + R->getName() + "', operand #" +
119 Twine(i) + " has the same name as a previous operand!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000120
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000121 OperandList.emplace_back(Rec, ArgName, PrintMethod, EncoderMethod,
122 OperandNamespace + "::" + OperandType, MIOperandNo,
123 NumOps, MIOpInfo);
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000124 MIOperandNo += NumOps;
125 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000126
127
Chris Lattnerdd3b09c2010-11-01 05:34:34 +0000128 // Make sure the constraints list for each operand is large enough to hold
129 // constraint info, even if none is present.
130 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
131 OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000132}
133
Chris Lattnerd8adec72010-11-01 04:03:32 +0000134
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000135/// getOperandNamed - Return the index of the operand with the specified
136/// non-empty name. If the instruction does not have an operand with the
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000137/// specified name, abort.
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000138///
Chris Lattnerd8adec72010-11-01 04:03:32 +0000139unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000140 unsigned OpIdx;
141 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000142 PrintFatalError("'" + TheDef->getName() +
143 "' does not have an operand named '$" + Name + "'!");
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000144}
145
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000146/// hasOperandNamed - Query whether the instruction has an operand of the
147/// given name. If so, return true and set OpIdx to the index of the
148/// operand. Otherwise, return false.
Chris Lattnerd8adec72010-11-01 04:03:32 +0000149bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000150 assert(!Name.empty() && "Cannot search for operand with no name!");
151 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
152 if (OperandList[i].Name == Name) {
153 OpIdx = i;
154 return true;
155 }
156 return false;
157}
158
Jim Grosbach38c4f4b2009-12-15 19:28:13 +0000159std::pair<unsigned,unsigned>
Chris Lattnerd8adec72010-11-01 04:03:32 +0000160CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000161 if (Op.empty() || Op[0] != '$')
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000162 PrintFatalError(TheDef->getName() + ": Illegal operand name: '" + Op + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000163
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000164 std::string OpName = Op.substr(1);
165 std::string SubOpName;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000166
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000167 // Check to see if this is $foo.bar.
Benjamin Kramere6ba5ef2016-11-30 10:01:11 +0000168 std::string::size_type DotIdx = OpName.find_first_of('.');
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000169 if (DotIdx != std::string::npos) {
170 SubOpName = OpName.substr(DotIdx+1);
171 if (SubOpName.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000172 PrintFatalError(TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'");
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000173 OpName = OpName.substr(0, DotIdx);
174 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000175
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000176 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000177
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000178 if (SubOpName.empty()) { // If no suboperand name was specified:
179 // If one was needed, throw.
180 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
181 SubOpName.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000182 PrintFatalError(TheDef->getName() + ": Illegal to refer to"
183 " whole operand part of complex operand '" + Op + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000184
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000185 // Otherwise, return the operand.
186 return std::make_pair(OpIdx, 0U);
187 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000188
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000189 // Find the suboperand number involved.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000190 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
Craig Topper24064772014-04-15 07:20:03 +0000191 if (!MIOpInfo)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000192 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000193
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000194 // Find the operand with the right name.
195 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
Matthias Braunbb053162016-12-05 06:00:46 +0000196 if (MIOpInfo->getArgNameStr(i) == SubOpName)
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000197 return std::make_pair(OpIdx, i);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000198
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000199 // Otherwise, didn't find it!
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000200 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
Aaron Ballman414a0cd2013-08-16 01:43:31 +0000201 return std::make_pair(0U, 0U);
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000202}
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000203
Chris Lattnerd8adec72010-11-01 04:03:32 +0000204static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
205 // EARLY_CLOBBER: @early $reg
206 std::string::size_type wpos = CStr.find_first_of(" \t");
207 std::string::size_type start = CStr.find_first_not_of(" \t");
208 std::string Tok = CStr.substr(start, wpos - start);
209 if (Tok == "@earlyclobber") {
210 std::string Name = CStr.substr(wpos+1);
211 wpos = Name.find_first_not_of(" \t");
212 if (wpos == std::string::npos)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000213 PrintFatalError("Illegal format for @earlyclobber constraint: '" + CStr + "'");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000214 Name = Name.substr(wpos);
215 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000216
Chris Lattnerd8adec72010-11-01 04:03:32 +0000217 // Build the string for the operand
218 if (!Ops[Op.first].Constraints[Op.second].isNone())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000219 PrintFatalError("Operand '" + Name + "' cannot have multiple constraints!");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000220 Ops[Op.first].Constraints[Op.second] =
221 CGIOperandList::ConstraintInfo::getEarlyClobber();
222 return;
223 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000224
Chris Lattnerd8adec72010-11-01 04:03:32 +0000225 // Only other constraint is "TIED_TO" for now.
226 std::string::size_type pos = CStr.find_first_of('=');
227 assert(pos != std::string::npos && "Unrecognized constraint");
228 start = CStr.find_first_not_of(" \t");
229 std::string Name = CStr.substr(start, pos - start);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000230
Chris Lattnerd8adec72010-11-01 04:03:32 +0000231 // TIED_TO: $src1 = $dst
232 wpos = Name.find_first_of(" \t");
233 if (wpos == std::string::npos)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000234 PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000235 std::string DestOpName = Name.substr(0, wpos);
236 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000237
Chris Lattnerd8adec72010-11-01 04:03:32 +0000238 Name = CStr.substr(pos+1);
239 wpos = Name.find_first_not_of(" \t");
240 if (wpos == std::string::npos)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000241 PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000242
Lang Hamescdd40bd2012-10-20 22:44:13 +0000243 std::string SrcOpName = Name.substr(wpos);
244 std::pair<unsigned,unsigned> SrcOp = Ops.ParseOperandName(SrcOpName, false);
245 if (SrcOp > DestOp) {
246 std::swap(SrcOp, DestOp);
247 std::swap(SrcOpName, DestOpName);
248 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000249
Chris Lattnerd8adec72010-11-01 04:03:32 +0000250 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000251
Chris Lattnerd8adec72010-11-01 04:03:32 +0000252 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000253 PrintFatalError("Operand '" + DestOpName +
254 "' cannot have multiple constraints!");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000255 Ops[DestOp.first].Constraints[DestOp.second] =
Jim Grosbach7b03fbd2011-11-15 01:05:12 +0000256 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
Chris Lattnerd8adec72010-11-01 04:03:32 +0000257}
258
259static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000260 if (CStr.empty()) return;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000261
Chris Lattnerd8adec72010-11-01 04:03:32 +0000262 const std::string delims(",");
263 std::string::size_type bidx, eidx;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000264
Chris Lattnerd8adec72010-11-01 04:03:32 +0000265 bidx = CStr.find_first_not_of(delims);
266 while (bidx != std::string::npos) {
267 eidx = CStr.find_first_of(delims, bidx);
268 if (eidx == std::string::npos)
269 eidx = CStr.length();
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000270
Chris Lattnerd8adec72010-11-01 04:03:32 +0000271 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
272 bidx = CStr.find_first_not_of(delims, eidx);
273 }
274}
275
276void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
277 while (1) {
Chris Lattner5cf753c2011-07-21 06:21:31 +0000278 std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
279 std::string OpName = P.first;
280 DisableEncoding = P.second;
Chris Lattnerd8adec72010-11-01 04:03:32 +0000281 if (OpName.empty()) break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000282
Chris Lattnerd8adec72010-11-01 04:03:32 +0000283 // Figure out which operand this is.
284 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000285
Chris Lattnerd8adec72010-11-01 04:03:32 +0000286 // Mark the operand as not-to-be encoded.
287 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
288 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
289 OperandList[Op.first].DoNotEncode[Op.second] = true;
290 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000291
Chris Lattnerd8adec72010-11-01 04:03:32 +0000292}
293
294//===----------------------------------------------------------------------===//
295// CodeGenInstruction Implementation
296//===----------------------------------------------------------------------===//
297
Jakob Stoklund Olesen94ed4d42012-08-24 00:31:16 +0000298CodeGenInstruction::CodeGenInstruction(Record *R)
Craig Topper24064772014-04-15 07:20:03 +0000299 : TheDef(R), Operands(R), InferredFrom(nullptr) {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000300 Namespace = R->getValueAsString("Namespace");
301 AsmString = R->getValueAsString("AsmString");
302
303 isReturn = R->getValueAsBit("isReturn");
304 isBranch = R->getValueAsBit("isBranch");
305 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
306 isCompare = R->getValueAsBit("isCompare");
Evan Cheng7f8ab6e2010-11-17 20:13:28 +0000307 isMoveImm = R->getValueAsBit("isMoveImm");
Evan Cheng880e299d2011-03-15 05:09:26 +0000308 isBitcast = R->getValueAsBit("isBitcast");
Jakob Stoklund Olesen2382d322012-08-16 23:11:47 +0000309 isSelect = R->getValueAsBit("isSelect");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000310 isBarrier = R->getValueAsBit("isBarrier");
311 isCall = R->getValueAsBit("isCall");
Sjoerd Meijer724023a2016-09-14 08:20:03 +0000312 isAdd = R->getValueAsBit("isAdd");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000313 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000314 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
315 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
316 isCommutable = R->getValueAsBit("isCommutable");
317 isTerminator = R->getValueAsBit("isTerminator");
318 isReMaterializable = R->getValueAsBit("isReMaterializable");
319 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
320 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Andrew Trick52363bd2011-09-20 18:22:31 +0000321 hasPostISelHook = R->getValueAsBit("hasPostISelHook");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000322 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
323 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Quentin Colombetd533cdf2014-08-11 22:17:14 +0000324 isRegSequence = R->getValueAsBit("isRegSequence");
Quentin Colombet7e75cba2014-08-20 21:51:26 +0000325 isExtractSubreg = R->getValueAsBit("isExtractSubreg");
Quentin Colombet7e3da662014-08-20 23:49:36 +0000326 isInsertSubreg = R->getValueAsBit("isInsertSubreg");
Owen Andersonabaa5232015-05-28 18:33:39 +0000327 isConvergent = R->getValueAsBit("isConvergent");
Matthias Braun8e0a7342016-03-01 20:03:11 +0000328 hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo");
Jakob Stoklund Olesenaf507bf2012-08-23 19:34:46 +0000329
Craig Topperbc9486b2014-02-05 09:10:40 +0000330 bool Unset;
331 mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset);
332 mayLoad_Unset = Unset;
333 mayStore = R->getValueAsBitOrUnset("mayStore", Unset);
334 mayStore_Unset = Unset;
335 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset);
336 hasSideEffects_Unset = Unset;
Jakob Stoklund Olesenaf507bf2012-08-23 19:34:46 +0000337
Chris Lattnerd8adec72010-11-01 04:03:32 +0000338 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
339 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
340 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Jim Grosbach4c08a9f2011-07-07 00:48:02 +0000341 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000342 isPseudo = R->getValueAsBit("isPseudo");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000343 ImplicitDefs = R->getValueAsListOfDefs("Defs");
344 ImplicitUses = R->getValueAsListOfDefs("Uses");
345
Chris Lattnerd8adec72010-11-01 04:03:32 +0000346 // Parse Constraints.
347 ParseConstraints(R->getValueAsString("Constraints"), Operands);
348
349 // Parse the DisableEncoding field.
350 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
Joey Gouly0e76fa72013-09-12 10:28:05 +0000351
352 // First check for a ComplexDeprecationPredicate.
353 if (R->getValue("ComplexDeprecationPredicate")) {
354 HasComplexDeprecationPredicate = true;
355 DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate");
356 } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) {
357 // Check if we have a Subtarget feature mask.
358 HasComplexDeprecationPredicate = false;
359 DeprecatedReason = Dep->getValue()->getAsString();
360 } else {
361 // This instruction isn't deprecated.
362 HasComplexDeprecationPredicate = false;
363 DeprecatedReason = "";
364 }
Chris Lattnerd8adec72010-11-01 04:03:32 +0000365}
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000366
367/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
368/// implicit def and it has a known VT, return the VT, otherwise return
369/// MVT::Other.
370MVT::SimpleValueType CodeGenInstruction::
371HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
372 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000373
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000374 // Check to see if the first implicit def has a resolvable type.
375 Record *FirstImplicitDef = ImplicitDefs[0];
376 assert(FirstImplicitDef->isSubClassOf("Register"));
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000377 const std::vector<MVT::SimpleValueType> &RegVTs =
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000378 TargetInfo.getRegisterVTs(FirstImplicitDef);
379 if (RegVTs.size() == 1)
380 return RegVTs[0];
381 return MVT::Other;
382}
383
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000384
385/// FlattenAsmStringVariants - Flatten the specified AsmString to only
386/// include text from the specified variant, returning the new string.
387std::string CodeGenInstruction::
388FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
389 std::string Res = "";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000390
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000391 for (;;) {
392 // Find the start of the next variant string.
393 size_t VariantsStart = 0;
394 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
395 if (Cur[VariantsStart] == '{' &&
396 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
397 Cur[VariantsStart-1] != '\\')))
398 break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000399
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000400 // Add the prefix to the result.
401 Res += Cur.slice(0, VariantsStart);
402 if (VariantsStart == Cur.size())
403 break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000404
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000405 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000406
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000407 // Scan to the end of the variants string.
408 size_t VariantsEnd = VariantsStart;
409 unsigned NestedBraces = 1;
410 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
411 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
412 if (--NestedBraces == 0)
413 break;
414 } else if (Cur[VariantsEnd] == '{')
415 ++NestedBraces;
416 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000417
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000418 // Select the Nth variant (or empty).
419 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
420 for (unsigned i = 0; i != Variant; ++i)
421 Selection = Selection.split('|').second;
422 Res += Selection.split('|').first;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000423
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000424 assert(VariantsEnd != Cur.size() &&
425 "Unterminated variants in assembly string!");
426 Cur = Cur.substr(VariantsEnd + 1);
427 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000428
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000429 return Res;
430}
431
Chris Lattner488c2012010-11-01 04:05:41 +0000432
433//===----------------------------------------------------------------------===//
434/// CodeGenInstAlias Implementation
435//===----------------------------------------------------------------------===//
436
Bob Wilsonb9b24222011-01-26 19:44:55 +0000437/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
438/// constructor. It checks if an argument in an InstAlias pattern matches
439/// the corresponding operand of the instruction. It returns true on a
440/// successful match, with ResOp set to the result operand to be used.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000441bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
Bob Wilsonb9b24222011-01-26 19:44:55 +0000442 Record *InstOpRec, bool hasSubOps,
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +0000443 ArrayRef<SMLoc> Loc, CodeGenTarget &T,
Bob Wilsonb9b24222011-01-26 19:44:55 +0000444 ResultOperand &ResOp) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000445 Init *Arg = Result->getArg(AliasOpNo);
Sean Silvafb509ed2012-10-10 20:24:43 +0000446 DefInit *ADI = dyn_cast<DefInit>(Arg);
Craig Topper24064772014-04-15 07:20:03 +0000447 Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
Bob Wilsonb9b24222011-01-26 19:44:55 +0000448
449 if (ADI && ADI->getDef() == InstOpRec) {
450 // If the operand is a record, it must have a name, and the record type
451 // must match up with the instruction's argument type.
Matthias Braunbb053162016-12-05 06:00:46 +0000452 if (!Result->getArgName(AliasOpNo))
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000453 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
454 " must have a name!");
Matthias Braunbb053162016-12-05 06:00:46 +0000455 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
Bob Wilsonb9b24222011-01-26 19:44:55 +0000456 return true;
457 }
458
Jim Grosbach6acb1482011-10-28 16:43:40 +0000459 // For register operands, the source register class can be a subclass
460 // of the instruction register class, not just an exact match.
Tim Northover3e38d292014-03-29 09:03:22 +0000461 if (InstOpRec->isSubClassOf("RegisterOperand"))
462 InstOpRec = InstOpRec->getValueAsDef("RegClass");
463
464 if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand"))
465 ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit();
466
Jim Grosbach6acb1482011-10-28 16:43:40 +0000467 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
468 if (!InstOpRec->isSubClassOf("RegisterClass"))
469 return false;
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000470 if (!T.getRegisterClass(InstOpRec)
471 .hasSubClass(&T.getRegisterClass(ADI->getDef())))
472 return false;
Matthias Braunbb053162016-12-05 06:00:46 +0000473 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000474 return true;
Jim Grosbach6acb1482011-10-28 16:43:40 +0000475 }
476
Bob Wilsonb9b24222011-01-26 19:44:55 +0000477 // Handle explicit registers.
478 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
Jim Grosbacha7b2d442011-08-19 20:33:06 +0000479 if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
480 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
481 // The operand info should only have a single (register) entry. We
482 // want the register class of it.
Sean Silva88eb8dd2012-10-10 20:24:47 +0000483 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
Jim Grosbacha7b2d442011-08-19 20:33:06 +0000484 }
485
Bob Wilsonb9b24222011-01-26 19:44:55 +0000486 if (!InstOpRec->isSubClassOf("RegisterClass"))
487 return false;
488
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000489 if (!T.getRegisterClass(InstOpRec)
490 .contains(T.getRegBank().getReg(ADI->getDef())))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000491 PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
492 " is not a member of the " + InstOpRec->getName() +
493 " register class!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000494
Matthias Braunbb053162016-12-05 06:00:46 +0000495 if (Result->getArgName(AliasOpNo))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000496 PrintFatalError(Loc, "result fixed register argument must "
497 "not have a name!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000498
Tim Northover3e38d292014-03-29 09:03:22 +0000499 ResOp = ResultOperand(ResultRecord);
Bob Wilsonb9b24222011-01-26 19:44:55 +0000500 return true;
501 }
502
503 // Handle "zero_reg" for optional def operands.
504 if (ADI && ADI->getDef()->getName() == "zero_reg") {
505
506 // Check if this is an optional def.
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000507 // Tied operands where the source is a sub-operand of a complex operand
508 // need to represent both operands in the alias destination instruction.
509 // Allow zero_reg for the tied portion. This can and should go away once
510 // the MC representation of things doesn't use tied operands at all.
511 //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
512 // throw TGError(Loc, "reg0 used for result that is not an "
513 // "OptionalDefOperand!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000514
Craig Topper24064772014-04-15 07:20:03 +0000515 ResOp = ResultOperand(static_cast<Record*>(nullptr));
Bob Wilsonb9b24222011-01-26 19:44:55 +0000516 return true;
517 }
518
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000519 // Literal integers.
Sean Silvafb509ed2012-10-10 20:24:43 +0000520 if (IntInit *II = dyn_cast<IntInit>(Arg)) {
Bob Wilsonb9b24222011-01-26 19:44:55 +0000521 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
522 return false;
523 // Integer arguments can't have names.
Matthias Braunbb053162016-12-05 06:00:46 +0000524 if (Result->getArgName(AliasOpNo))
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000525 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000526 " must not have a name!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000527 ResOp = ResultOperand(II->getValue());
528 return true;
529 }
530
Pete Cooper2cfdfe52014-08-07 05:47:04 +0000531 // Bits<n> (also used for 0bxx literals)
532 if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
533 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
534 return false;
535 if (!BI->isComplete())
536 return false;
537 // Convert the bits init to an integer and use that for the result.
538 IntInit *II =
539 dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get()));
540 if (!II)
541 return false;
542 ResOp = ResultOperand(II->getValue());
543 return true;
544 }
545
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000546 // If both are Operands with the same MVT, allow the conversion. It's
547 // up to the user to make sure the values are appropriate, just like
548 // for isel Pat's.
Michael Ilseman5be22a12014-12-12 21:48:03 +0000549 if (InstOpRec->isSubClassOf("Operand") && ADI &&
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000550 ADI->getDef()->isSubClassOf("Operand")) {
551 // FIXME: What other attributes should we check here? Identical
552 // MIOperandInfo perhaps?
553 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
554 return false;
Matthias Braunbb053162016-12-05 06:00:46 +0000555 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ADI->getDef());
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000556 return true;
557 }
558
Bob Wilsonb9b24222011-01-26 19:44:55 +0000559 return false;
560}
561
Tim Northover60091cf2014-05-15 13:36:01 +0000562unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
563 if (!isRecord())
564 return 1;
565
566 Record *Rec = getRecord();
567 if (!Rec->isSubClassOf("Operand"))
568 return 1;
569
570 DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
571 if (MIOpInfo->getNumArgs() == 0) {
572 // Unspecified, so it defaults to 1
573 return 1;
574 }
575
576 return MIOpInfo->getNumArgs();
577}
578
Tim Northoverd8d65a62014-05-15 11:16:32 +0000579CodeGenInstAlias::CodeGenInstAlias(Record *R, unsigned Variant,
580 CodeGenTarget &T)
581 : TheDef(R) {
Chris Lattnerdd3b09c2010-11-01 05:34:34 +0000582 Result = R->getValueAsDag("ResultInst");
Tim Northoverd8d65a62014-05-15 11:16:32 +0000583 AsmString = R->getValueAsString("AsmString");
584 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant);
585
Chris Lattner8ffd1292010-11-06 06:39:47 +0000586
587 // Verify that the root of the result is an instruction.
Sean Silvafb509ed2012-10-10 20:24:43 +0000588 DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
Craig Topper24064772014-04-15 07:20:03 +0000589 if (!DI || !DI->getDef()->isSubClassOf("Instruction"))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000590 PrintFatalError(R->getLoc(),
591 "result of inst alias should be an instruction");
Chris Lattner8ffd1292010-11-06 06:39:47 +0000592
593 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000594
Chris Lattnerb625dd22010-11-06 07:06:09 +0000595 // NameClass - If argument names are repeated, we need to verify they have
596 // the same class.
597 StringMap<Record*> NameClass;
Bob Wilsona48eacc2011-01-20 18:38:10 +0000598 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000599 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
Matthias Braunbb053162016-12-05 06:00:46 +0000600 if (!ADI || !Result->getArgName(i))
Bob Wilsona48eacc2011-01-20 18:38:10 +0000601 continue;
602 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
603 // $foo can exist multiple times in the result list, but it must have the
604 // same type.
Matthias Braunbb053162016-12-05 06:00:46 +0000605 Record *&Entry = NameClass[Result->getArgNameStr(i)];
Bob Wilsona48eacc2011-01-20 18:38:10 +0000606 if (Entry && Entry != ADI->getDef())
Matthias Braunbb053162016-12-05 06:00:46 +0000607 PrintFatalError(R->getLoc(), "result value $" + Result->getArgNameStr(i) +
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000608 " is both " + Entry->getName() + " and " +
609 ADI->getDef()->getName() + "!");
Bob Wilsona48eacc2011-01-20 18:38:10 +0000610 Entry = ADI->getDef();
611 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000612
Chris Lattner76030522010-11-06 06:54:38 +0000613 // Decode and validate the arguments of the result.
Chris Lattner8188fb22010-11-06 07:31:43 +0000614 unsigned AliasOpNo = 0;
615 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsonb9b24222011-01-26 19:44:55 +0000616
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000617 // Tied registers don't have an entry in the result dag unless they're part
618 // of a complex operand, in which case we include them anyways, as we
619 // don't have any other way to specify the whole operand.
620 if (ResultInst->Operands[i].MINumOperands == 1 &&
621 ResultInst->Operands[i].getTiedRegister() != -1)
Chris Lattner8188fb22010-11-06 07:31:43 +0000622 continue;
623
624 if (AliasOpNo >= Result->getNumArgs())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000625 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000626
Bob Wilsonb9b24222011-01-26 19:44:55 +0000627 Record *InstOpRec = ResultInst->Operands[i].Rec;
628 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
629 ResultOperand ResOp(static_cast<int64_t>(0));
630 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
631 R->getLoc(), T, ResOp)) {
Owen Andersonda6bd3e2012-06-08 00:25:03 +0000632 // If this is a simple operand, or a complex operand with a custom match
633 // class, then we can match is verbatim.
634 if (NumSubOps == 1 ||
635 (InstOpRec->getValue("ParserMatchClass") &&
636 InstOpRec->getValueAsDef("ParserMatchClass")
637 ->getValueAsString("Name") != "Imm")) {
638 ResultOperands.push_back(ResOp);
639 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
640 ++AliasOpNo;
641
642 // Otherwise, we need to match each of the suboperands individually.
643 } else {
644 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
645 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000646 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Owen Andersonda6bd3e2012-06-08 00:25:03 +0000647
648 // Take care to instantiate each of the suboperands with the correct
649 // nomenclature: $foo.bar
Matthias Braunbb053162016-12-05 06:00:46 +0000650 ResultOperands.emplace_back(
651 Result->getArgName(AliasOpNo)->getAsUnquotedString() + "." +
652 MIOI->getArgName(SubOp)->getAsUnquotedString(), SubRec);
Owen Andersonda6bd3e2012-06-08 00:25:03 +0000653 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
654 }
655 ++AliasOpNo;
656 }
Chris Lattner76030522010-11-06 06:54:38 +0000657 continue;
658 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000659
Bob Wilsonb9b24222011-01-26 19:44:55 +0000660 // If the argument did not match the instruction operand, and the operand
661 // is composed of multiple suboperands, try matching the suboperands.
662 if (NumSubOps > 1) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000663 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
Bob Wilsonb9b24222011-01-26 19:44:55 +0000664 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
665 if (AliasOpNo >= Result->getNumArgs())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000666 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
Sean Silva88eb8dd2012-10-10 20:24:47 +0000667 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Bob Wilsonb9b24222011-01-26 19:44:55 +0000668 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
669 R->getLoc(), T, ResOp)) {
670 ResultOperands.push_back(ResOp);
671 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
672 ++AliasOpNo;
673 } else {
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000674 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
Bob Wilsonb9b24222011-01-26 19:44:55 +0000675 " does not match instruction operand class " +
676 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
677 }
678 }
Chris Lattnerb6f8e822010-11-06 19:25:43 +0000679 continue;
680 }
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000681 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000682 " does not match instruction operand class " +
683 InstOpRec->getName());
Chris Lattner76030522010-11-06 06:54:38 +0000684 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000685
Chris Lattner8188fb22010-11-06 07:31:43 +0000686 if (AliasOpNo != Result->getNumArgs())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000687 PrintFatalError(R->getLoc(), "too many operands for instruction!");
Chris Lattner488c2012010-11-01 04:05:41 +0000688}