blob: fde946d065891f5b7f2ebbd2cb5a444b10809575 [file] [log] [blame]
Chris Lattnerea2d52d2008-01-06 01:35:39 +00001//===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerea2d52d2008-01-06 01:35:39 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the CodeGenInstruction class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CodeGenInstruction.h"
Chris Lattner7bc5d9b2010-03-27 20:09:24 +000014#include "CodeGenTarget.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000015#include "llvm/ADT/STLExtras.h"
Chris Lattnerea2d52d2008-01-06 01:35:39 +000016#include "llvm/ADT/StringExtras.h"
Chris Lattnerb625dd22010-11-06 07:06:09 +000017#include "llvm/ADT/StringMap.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000018#include "llvm/TableGen/Error.h"
19#include "llvm/TableGen/Record.h"
Chris Lattnerea2d52d2008-01-06 01:35:39 +000020#include <set>
21using namespace llvm;
22
Chris Lattnerd8adec72010-11-01 04:03:32 +000023//===----------------------------------------------------------------------===//
24// CGIOperandList Implementation
25//===----------------------------------------------------------------------===//
Jim Grosbach2a282f22009-12-16 19:43:02 +000026
Chris Lattnerd8adec72010-11-01 04:03:32 +000027CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
28 isPredicable = false;
Chris Lattnerea2d52d2008-01-06 01:35:39 +000029 hasOptionalDef = false;
Chris Lattnerf376c992008-01-07 05:19:29 +000030 isVariadic = false;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000031
David Greeneaf8ee2c2011-07-29 22:43:06 +000032 DagInit *OutDI = R->getValueAsDag("OutOperandList");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000033
Sean Silvafb509ed2012-10-10 20:24:43 +000034 if (DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) {
Chris Lattner0f037472010-03-18 20:56:35 +000035 if (Init->getDef()->getName() != "outs")
Daniel Sandersdff673b2019-02-12 17:36:57 +000036 PrintFatalError(R->getLoc(),
37 R->getName() +
38 ": invalid def name for output list: use 'outs'");
Chris Lattner5f418ea2010-03-18 20:50:52 +000039 } else
Daniel Sandersdff673b2019-02-12 17:36:57 +000040 PrintFatalError(R->getLoc(),
41 R->getName() + ": invalid output list: use 'outs'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000042
Chris Lattner1f22d272010-03-18 21:07:39 +000043 NumDefs = OutDI->getNumArgs();
Chris Lattnerd8adec72010-11-01 04:03:32 +000044
David Greeneaf8ee2c2011-07-29 22:43:06 +000045 DagInit *InDI = R->getValueAsDag("InOperandList");
Sean Silvafb509ed2012-10-10 20:24:43 +000046 if (DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) {
Chris Lattner0f037472010-03-18 20:56:35 +000047 if (Init->getDef()->getName() != "ins")
Daniel Sandersdff673b2019-02-12 17:36:57 +000048 PrintFatalError(R->getLoc(),
49 R->getName() +
50 ": invalid def name for input list: use 'ins'");
Chris Lattner5f418ea2010-03-18 20:50:52 +000051 } else
Daniel Sandersdff673b2019-02-12 17:36:57 +000052 PrintFatalError(R->getLoc(),
53 R->getName() + ": invalid input list: use 'ins'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000054
Chris Lattnerea2d52d2008-01-06 01:35:39 +000055 unsigned MIOperandNo = 0;
56 std::set<std::string> OperandNames;
Reid Kleckner45b61592016-02-03 19:34:28 +000057 unsigned e = InDI->getNumArgs() + OutDI->getNumArgs();
58 OperandList.reserve(e);
59 for (unsigned i = 0; i != e; ++i){
David Greeneaf8ee2c2011-07-29 22:43:06 +000060 Init *ArgInit;
Matthias Braunbb053162016-12-05 06:00:46 +000061 StringRef ArgName;
Chris Lattner1f22d272010-03-18 21:07:39 +000062 if (i < NumDefs) {
63 ArgInit = OutDI->getArg(i);
Matthias Braunbb053162016-12-05 06:00:46 +000064 ArgName = OutDI->getArgNameStr(i);
Chris Lattner1f22d272010-03-18 21:07:39 +000065 } else {
66 ArgInit = InDI->getArg(i-NumDefs);
Matthias Braunbb053162016-12-05 06:00:46 +000067 ArgName = InDI->getArgNameStr(i-NumDefs);
Chris Lattner1f22d272010-03-18 21:07:39 +000068 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000069
Sean Silvafb509ed2012-10-10 20:24:43 +000070 DefInit *Arg = dyn_cast<DefInit>(ArgInit);
Chris Lattnerea2d52d2008-01-06 01:35:39 +000071 if (!Arg)
Daniel Sandersdff673b2019-02-12 17:36:57 +000072 PrintFatalError(R->getLoc(), "Illegal operand for the '" + R->getName() +
73 "' instruction!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000074
Chris Lattnerea2d52d2008-01-06 01:35:39 +000075 Record *Rec = Arg->getDef();
76 std::string PrintMethod = "printOperand";
Jim Grosbach51a12eb2010-10-12 22:21:57 +000077 std::string EncoderMethod;
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +000078 std::string OperandType = "OPERAND_UNKNOWN";
Tom Stellard89b26102015-01-12 19:33:09 +000079 std::string OperandNamespace = "MCOI";
Chris Lattnerea2d52d2008-01-06 01:35:39 +000080 unsigned NumOps = 1;
Craig Topper24064772014-04-15 07:20:03 +000081 DagInit *MIOpInfo = nullptr;
Owen Andersona84be6c2011-06-27 21:06:21 +000082 if (Rec->isSubClassOf("RegisterOperand")) {
83 PrintMethod = Rec->getValueAsString("PrintMethod");
Tom Stellard89b26102015-01-12 19:33:09 +000084 OperandType = Rec->getValueAsString("OperandType");
85 OperandNamespace = Rec->getValueAsString("OperandNamespace");
Sam Kolton1a5a5e62017-05-15 10:13:07 +000086 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Owen Andersona84be6c2011-06-27 21:06:21 +000087 } else if (Rec->isSubClassOf("Operand")) {
Chris Lattnerea2d52d2008-01-06 01:35:39 +000088 PrintMethod = Rec->getValueAsString("PrintMethod");
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +000089 OperandType = Rec->getValueAsString("OperandType");
Dan Gohmandb70d012015-12-22 23:37:37 +000090 OperandNamespace = Rec->getValueAsString("OperandNamespace");
Jim Grosbach51a12eb2010-10-12 22:21:57 +000091 // If there is an explicit encoder method, use it.
Chris Lattner63274cb2010-11-15 05:19:05 +000092 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattnerea2d52d2008-01-06 01:35:39 +000093 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000094
Chris Lattnerea2d52d2008-01-06 01:35:39 +000095 // Verify that MIOpInfo has an 'ops' root value.
Sean Silva88eb8dd2012-10-10 20:24:47 +000096 if (!isa<DefInit>(MIOpInfo->getOperator()) ||
97 cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
Daniel Sandersdff673b2019-02-12 17:36:57 +000098 PrintFatalError(R->getLoc(),
99 "Bad value for MIOperandInfo in operand '" +
100 Rec->getName() + "'\n");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000101
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000102 // If we have MIOpInfo, then we have #operands equal to number of entries
103 // in MIOperandInfo.
104 if (unsigned NumArgs = MIOpInfo->getNumArgs())
105 NumOps = NumArgs;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000106
Tim Northover42180442013-08-22 09:57:11 +0000107 if (Rec->isSubClassOf("PredicateOp"))
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000108 isPredicable = true;
109 else if (Rec->isSubClassOf("OptionalDefOperand"))
110 hasOptionalDef = true;
111 } else if (Rec->getName() == "variable_ops") {
Chris Lattnerf376c992008-01-07 05:19:29 +0000112 isVariadic = true;
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000113 continue;
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +0000114 } else if (Rec->isSubClassOf("RegisterClass")) {
115 OperandType = "OPERAND_REGISTER";
116 } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
Owen Anderson16ba4b22012-09-11 23:47:08 +0000117 !Rec->isSubClassOf("unknown_class"))
Daniel Sandersdff673b2019-02-12 17:36:57 +0000118 PrintFatalError(R->getLoc(), "Unknown operand class '" + Rec->getName() +
119 "' in '" + R->getName() +
120 "' instruction!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000121
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000122 // Check that the operand has a name and that it's unique.
Chris Lattner1f22d272010-03-18 21:07:39 +0000123 if (ArgName.empty())
Daniel Sandersdff673b2019-02-12 17:36:57 +0000124 PrintFatalError(R->getLoc(), "In instruction '" + R->getName() +
125 "', operand #" + Twine(i) +
126 " has no name!");
Chris Lattner1f22d272010-03-18 21:07:39 +0000127 if (!OperandNames.insert(ArgName).second)
Daniel Sandersdff673b2019-02-12 17:36:57 +0000128 PrintFatalError(R->getLoc(),
129 "In instruction '" + R->getName() + "', operand #" +
130 Twine(i) +
131 " has the same name as a previous operand!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000132
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000133 OperandList.emplace_back(Rec, ArgName, PrintMethod, EncoderMethod,
134 OperandNamespace + "::" + OperandType, MIOperandNo,
135 NumOps, MIOpInfo);
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000136 MIOperandNo += NumOps;
137 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000138
139
Chris Lattnerdd3b09c2010-11-01 05:34:34 +0000140 // Make sure the constraints list for each operand is large enough to hold
141 // constraint info, even if none is present.
Javed Absar41705e92017-10-06 09:32:45 +0000142 for (OperandInfo &OpInfo : OperandList)
143 OpInfo.Constraints.resize(OpInfo.MINumOperands);
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000144}
145
Chris Lattnerd8adec72010-11-01 04:03:32 +0000146
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000147/// getOperandNamed - Return the index of the operand with the specified
148/// non-empty name. If the instruction does not have an operand with the
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000149/// specified name, abort.
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000150///
Chris Lattnerd8adec72010-11-01 04:03:32 +0000151unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000152 unsigned OpIdx;
Daniel Sandersdff673b2019-02-12 17:36:57 +0000153 if (hasOperandNamed(Name, OpIdx))
154 return OpIdx;
155 PrintFatalError(TheDef->getLoc(), "'" + TheDef->getName() +
156 "' does not have an operand named '$" +
157 Name + "'!");
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000158}
159
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000160/// hasOperandNamed - Query whether the instruction has an operand of the
161/// given name. If so, return true and set OpIdx to the index of the
162/// operand. Otherwise, return false.
Chris Lattnerd8adec72010-11-01 04:03:32 +0000163bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000164 assert(!Name.empty() && "Cannot search for operand with no name!");
165 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
166 if (OperandList[i].Name == Name) {
167 OpIdx = i;
168 return true;
169 }
170 return false;
171}
172
Jim Grosbach38c4f4b2009-12-15 19:28:13 +0000173std::pair<unsigned,unsigned>
Chris Lattnerd8adec72010-11-01 04:03:32 +0000174CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000175 if (Op.empty() || Op[0] != '$')
Daniel Sandersdff673b2019-02-12 17:36:57 +0000176 PrintFatalError(TheDef->getLoc(),
177 TheDef->getName() + ": Illegal operand name: '" + Op + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000178
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000179 std::string OpName = Op.substr(1);
180 std::string SubOpName;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000181
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000182 // Check to see if this is $foo.bar.
Benjamin Kramere6ba5ef2016-11-30 10:01:11 +0000183 std::string::size_type DotIdx = OpName.find_first_of('.');
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000184 if (DotIdx != std::string::npos) {
185 SubOpName = OpName.substr(DotIdx+1);
186 if (SubOpName.empty())
Daniel Sandersdff673b2019-02-12 17:36:57 +0000187 PrintFatalError(TheDef->getLoc(),
188 TheDef->getName() +
189 ": illegal empty suboperand name in '" + Op + "'");
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000190 OpName = OpName.substr(0, DotIdx);
191 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000192
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000193 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000194
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000195 if (SubOpName.empty()) { // If no suboperand name was specified:
196 // If one was needed, throw.
197 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
198 SubOpName.empty())
Daniel Sandersdff673b2019-02-12 17:36:57 +0000199 PrintFatalError(TheDef->getLoc(),
200 TheDef->getName() +
201 ": Illegal to refer to"
202 " whole operand part of complex operand '" +
203 Op + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000204
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000205 // Otherwise, return the operand.
206 return std::make_pair(OpIdx, 0U);
207 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000208
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000209 // Find the suboperand number involved.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000210 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
Craig Topper24064772014-04-15 07:20:03 +0000211 if (!MIOpInfo)
Daniel Sandersdff673b2019-02-12 17:36:57 +0000212 PrintFatalError(TheDef->getLoc(), TheDef->getName() +
213 ": unknown suboperand name in '" +
214 Op + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000215
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000216 // Find the operand with the right name.
217 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
Matthias Braunbb053162016-12-05 06:00:46 +0000218 if (MIOpInfo->getArgNameStr(i) == SubOpName)
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000219 return std::make_pair(OpIdx, i);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000220
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000221 // Otherwise, didn't find it!
Daniel Sandersdff673b2019-02-12 17:36:57 +0000222 PrintFatalError(TheDef->getLoc(), TheDef->getName() +
223 ": unknown suboperand name in '" + Op +
224 "'");
Aaron Ballman414a0cd2013-08-16 01:43:31 +0000225 return std::make_pair(0U, 0U);
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000226}
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000227
Simon Tatham34860552018-11-28 11:43:49 +0000228static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops,
229 Record *Rec) {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000230 // EARLY_CLOBBER: @early $reg
231 std::string::size_type wpos = CStr.find_first_of(" \t");
232 std::string::size_type start = CStr.find_first_not_of(" \t");
233 std::string Tok = CStr.substr(start, wpos - start);
234 if (Tok == "@earlyclobber") {
235 std::string Name = CStr.substr(wpos+1);
236 wpos = Name.find_first_not_of(" \t");
237 if (wpos == std::string::npos)
Simon Tatham34860552018-11-28 11:43:49 +0000238 PrintFatalError(
239 Rec->getLoc(), "Illegal format for @earlyclobber constraint in '" +
240 Rec->getName() + "': '" + CStr + "'");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000241 Name = Name.substr(wpos);
242 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000243
Chris Lattnerd8adec72010-11-01 04:03:32 +0000244 // Build the string for the operand
245 if (!Ops[Op.first].Constraints[Op.second].isNone())
Simon Tatham34860552018-11-28 11:43:49 +0000246 PrintFatalError(
247 Rec->getLoc(), "Operand '" + Name + "' of '" + Rec->getName() +
248 "' cannot have multiple constraints!");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000249 Ops[Op.first].Constraints[Op.second] =
250 CGIOperandList::ConstraintInfo::getEarlyClobber();
251 return;
252 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000253
Chris Lattnerd8adec72010-11-01 04:03:32 +0000254 // Only other constraint is "TIED_TO" for now.
255 std::string::size_type pos = CStr.find_first_of('=');
Simon Tatham34860552018-11-28 11:43:49 +0000256 if (pos == std::string::npos)
257 PrintFatalError(
258 Rec->getLoc(), "Unrecognized constraint '" + CStr +
259 "' in '" + Rec->getName() + "'");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000260 start = CStr.find_first_not_of(" \t");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000261
Chris Lattnerd8adec72010-11-01 04:03:32 +0000262 // TIED_TO: $src1 = $dst
Simon Tatham34860552018-11-28 11:43:49 +0000263 wpos = CStr.find_first_of(" \t", start);
264 if (wpos == std::string::npos || wpos > pos)
265 PrintFatalError(
266 Rec->getLoc(), "Illegal format for tied-to constraint in '" +
267 Rec->getName() + "': '" + CStr + "'");
268 std::string LHSOpName = StringRef(CStr).substr(start, wpos - start);
269 std::pair<unsigned,unsigned> LHSOp = Ops.ParseOperandName(LHSOpName, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000270
Simon Tatham34860552018-11-28 11:43:49 +0000271 wpos = CStr.find_first_not_of(" \t", pos + 1);
Chris Lattnerd8adec72010-11-01 04:03:32 +0000272 if (wpos == std::string::npos)
Simon Tatham34860552018-11-28 11:43:49 +0000273 PrintFatalError(
274 Rec->getLoc(), "Illegal format for tied-to constraint: '" + CStr + "'");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000275
Simon Tatham34860552018-11-28 11:43:49 +0000276 std::string RHSOpName = StringRef(CStr).substr(wpos);
277 std::pair<unsigned,unsigned> RHSOp = Ops.ParseOperandName(RHSOpName, false);
278
279 // Sort the operands into order, which should put the output one
280 // first. But keep the original order, for use in diagnostics.
281 bool FirstIsDest = (LHSOp < RHSOp);
282 std::pair<unsigned,unsigned> DestOp = (FirstIsDest ? LHSOp : RHSOp);
283 StringRef DestOpName = (FirstIsDest ? LHSOpName : RHSOpName);
284 std::pair<unsigned,unsigned> SrcOp = (FirstIsDest ? RHSOp : LHSOp);
285 StringRef SrcOpName = (FirstIsDest ? RHSOpName : LHSOpName);
286
287 // Ensure one operand is a def and the other is a use.
288 if (DestOp.first >= Ops.NumDefs)
289 PrintFatalError(
290 Rec->getLoc(), "Input operands '" + LHSOpName + "' and '" + RHSOpName +
291 "' of '" + Rec->getName() + "' cannot be tied!");
292 if (SrcOp.first < Ops.NumDefs)
293 PrintFatalError(
294 Rec->getLoc(), "Output operands '" + LHSOpName + "' and '" + RHSOpName +
295 "' of '" + Rec->getName() + "' cannot be tied!");
296
297 // The constraint has to go on the operand with higher index, i.e.
298 // the source one. Check there isn't another constraint there
299 // already.
300 if (!Ops[SrcOp.first].Constraints[SrcOp.second].isNone())
301 PrintFatalError(
302 Rec->getLoc(), "Operand '" + SrcOpName + "' of '" + Rec->getName() +
303 "' cannot have multiple constraints!");
304
305 unsigned DestFlatOpNo = Ops.getFlattenedOperandNumber(DestOp);
306 auto NewConstraint = CGIOperandList::ConstraintInfo::getTied(DestFlatOpNo);
307
308 // Check that the earlier operand is not the target of another tie
309 // before making it the target of this one.
310 for (const CGIOperandList::OperandInfo &Op : Ops) {
311 for (unsigned i = 0; i < Op.MINumOperands; i++)
312 if (Op.Constraints[i] == NewConstraint)
313 PrintFatalError(
314 Rec->getLoc(), "Operand '" + DestOpName + "' of '" + Rec->getName() +
315 "' cannot have multiple operands tied to it!");
Lang Hamescdd40bd2012-10-20 22:44:13 +0000316 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000317
Simon Tatham34860552018-11-28 11:43:49 +0000318 Ops[SrcOp.first].Constraints[SrcOp.second] = NewConstraint;
Chris Lattnerd8adec72010-11-01 04:03:32 +0000319}
320
Simon Tatham34860552018-11-28 11:43:49 +0000321static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops,
322 Record *Rec) {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000323 if (CStr.empty()) return;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000324
Chris Lattnerd8adec72010-11-01 04:03:32 +0000325 const std::string delims(",");
326 std::string::size_type bidx, eidx;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000327
Chris Lattnerd8adec72010-11-01 04:03:32 +0000328 bidx = CStr.find_first_not_of(delims);
329 while (bidx != std::string::npos) {
330 eidx = CStr.find_first_of(delims, bidx);
331 if (eidx == std::string::npos)
332 eidx = CStr.length();
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000333
Simon Tatham34860552018-11-28 11:43:49 +0000334 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops, Rec);
Chris Lattnerd8adec72010-11-01 04:03:32 +0000335 bidx = CStr.find_first_not_of(delims, eidx);
336 }
337}
338
339void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
340 while (1) {
Chris Lattner5cf753c2011-07-21 06:21:31 +0000341 std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
342 std::string OpName = P.first;
343 DisableEncoding = P.second;
Chris Lattnerd8adec72010-11-01 04:03:32 +0000344 if (OpName.empty()) break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000345
Chris Lattnerd8adec72010-11-01 04:03:32 +0000346 // Figure out which operand this is.
347 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000348
Chris Lattnerd8adec72010-11-01 04:03:32 +0000349 // Mark the operand as not-to-be encoded.
350 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
351 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
352 OperandList[Op.first].DoNotEncode[Op.second] = true;
353 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000354
Chris Lattnerd8adec72010-11-01 04:03:32 +0000355}
356
357//===----------------------------------------------------------------------===//
358// CodeGenInstruction Implementation
359//===----------------------------------------------------------------------===//
360
Jakob Stoklund Olesen94ed4d42012-08-24 00:31:16 +0000361CodeGenInstruction::CodeGenInstruction(Record *R)
Craig Topper24064772014-04-15 07:20:03 +0000362 : TheDef(R), Operands(R), InferredFrom(nullptr) {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000363 Namespace = R->getValueAsString("Namespace");
364 AsmString = R->getValueAsString("AsmString");
365
Matt Arsenault27269052019-10-07 18:43:29 +0000366 isPreISelOpcode = R->getValueAsBit("isPreISelOpcode");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000367 isReturn = R->getValueAsBit("isReturn");
Heejin Ahned5e06b2018-08-21 19:44:11 +0000368 isEHScopeReturn = R->getValueAsBit("isEHScopeReturn");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000369 isBranch = R->getValueAsBit("isBranch");
370 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
371 isCompare = R->getValueAsBit("isCompare");
Evan Cheng7f8ab6e2010-11-17 20:13:28 +0000372 isMoveImm = R->getValueAsBit("isMoveImm");
Petar Jovanovicc0510002018-05-23 15:28:28 +0000373 isMoveReg = R->getValueAsBit("isMoveReg");
Evan Cheng880e299d2011-03-15 05:09:26 +0000374 isBitcast = R->getValueAsBit("isBitcast");
Jakob Stoklund Olesen2382d322012-08-16 23:11:47 +0000375 isSelect = R->getValueAsBit("isSelect");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000376 isBarrier = R->getValueAsBit("isBarrier");
377 isCall = R->getValueAsBit("isCall");
Sjoerd Meijer724023a2016-09-14 08:20:03 +0000378 isAdd = R->getValueAsBit("isAdd");
Joel Galenson06e7e572018-07-13 15:19:33 +0000379 isTrap = R->getValueAsBit("isTrap");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000380 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
Simon Tathamb70fc0c2019-02-25 10:39:53 +0000381 isPredicable = !R->getValueAsBit("isUnpredicable") && (
382 Operands.isPredicable || R->getValueAsBit("isPredicable"));
Chris Lattnerd8adec72010-11-01 04:03:32 +0000383 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
384 isCommutable = R->getValueAsBit("isCommutable");
385 isTerminator = R->getValueAsBit("isTerminator");
386 isReMaterializable = R->getValueAsBit("isReMaterializable");
387 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
388 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Andrew Trick52363bd2011-09-20 18:22:31 +0000389 hasPostISelHook = R->getValueAsBit("hasPostISelHook");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000390 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
391 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Quentin Colombetd533cdf2014-08-11 22:17:14 +0000392 isRegSequence = R->getValueAsBit("isRegSequence");
Quentin Colombet7e75cba2014-08-20 21:51:26 +0000393 isExtractSubreg = R->getValueAsBit("isExtractSubreg");
Quentin Colombet7e3da662014-08-20 23:49:36 +0000394 isInsertSubreg = R->getValueAsBit("isInsertSubreg");
Owen Andersonabaa5232015-05-28 18:33:39 +0000395 isConvergent = R->getValueAsBit("isConvergent");
Matthias Braun8e0a7342016-03-01 20:03:11 +0000396 hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo");
Simon Dardis13de5552018-05-22 14:36:58 +0000397 FastISelShouldIgnore = R->getValueAsBit("FastISelShouldIgnore");
Oliver Stannard4cf35b42018-12-03 10:32:42 +0000398 variadicOpsAreDefs = R->getValueAsBit("variadicOpsAreDefs");
Jakob Stoklund Olesenaf507bf2012-08-23 19:34:46 +0000399
Craig Topperbc9486b2014-02-05 09:10:40 +0000400 bool Unset;
401 mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset);
402 mayLoad_Unset = Unset;
403 mayStore = R->getValueAsBitOrUnset("mayStore", Unset);
404 mayStore_Unset = Unset;
Ulrich Weigand6c5d5ce2019-06-05 22:33:10 +0000405 mayRaiseFPException = R->getValueAsBit("mayRaiseFPException");
Craig Topperbc9486b2014-02-05 09:10:40 +0000406 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset);
407 hasSideEffects_Unset = Unset;
Jakob Stoklund Olesenaf507bf2012-08-23 19:34:46 +0000408
Chris Lattnerd8adec72010-11-01 04:03:32 +0000409 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
410 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
411 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Jim Grosbach4c08a9f2011-07-07 00:48:02 +0000412 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000413 isPseudo = R->getValueAsBit("isPseudo");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000414 ImplicitDefs = R->getValueAsListOfDefs("Defs");
415 ImplicitUses = R->getValueAsListOfDefs("Uses");
416
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000417 // This flag is only inferred from the pattern.
418 hasChain = false;
419 hasChain_Inferred = false;
420
Chris Lattnerd8adec72010-11-01 04:03:32 +0000421 // Parse Constraints.
Simon Tatham34860552018-11-28 11:43:49 +0000422 ParseConstraints(R->getValueAsString("Constraints"), Operands, R);
Chris Lattnerd8adec72010-11-01 04:03:32 +0000423
424 // Parse the DisableEncoding field.
425 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
Joey Gouly0e76fa72013-09-12 10:28:05 +0000426
427 // First check for a ComplexDeprecationPredicate.
428 if (R->getValue("ComplexDeprecationPredicate")) {
429 HasComplexDeprecationPredicate = true;
430 DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate");
431 } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) {
432 // Check if we have a Subtarget feature mask.
433 HasComplexDeprecationPredicate = false;
434 DeprecatedReason = Dep->getValue()->getAsString();
435 } else {
436 // This instruction isn't deprecated.
437 HasComplexDeprecationPredicate = false;
438 DeprecatedReason = "";
439 }
Chris Lattnerd8adec72010-11-01 04:03:32 +0000440}
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000441
442/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
443/// implicit def and it has a known VT, return the VT, otherwise return
444/// MVT::Other.
445MVT::SimpleValueType CodeGenInstruction::
446HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
447 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000448
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000449 // Check to see if the first implicit def has a resolvable type.
450 Record *FirstImplicitDef = ImplicitDefs[0];
451 assert(FirstImplicitDef->isSubClassOf("Register"));
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000452 const std::vector<ValueTypeByHwMode> &RegVTs =
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000453 TargetInfo.getRegisterVTs(FirstImplicitDef);
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000454 if (RegVTs.size() == 1 && RegVTs[0].isSimple())
455 return RegVTs[0].getSimple().SimpleTy;
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000456 return MVT::Other;
457}
458
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000459
460/// FlattenAsmStringVariants - Flatten the specified AsmString to only
461/// include text from the specified variant, returning the new string.
462std::string CodeGenInstruction::
463FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
464 std::string Res = "";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000465
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000466 for (;;) {
467 // Find the start of the next variant string.
468 size_t VariantsStart = 0;
469 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
470 if (Cur[VariantsStart] == '{' &&
471 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
472 Cur[VariantsStart-1] != '\\')))
473 break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000474
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000475 // Add the prefix to the result.
476 Res += Cur.slice(0, VariantsStart);
477 if (VariantsStart == Cur.size())
478 break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000479
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000480 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000481
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000482 // Scan to the end of the variants string.
483 size_t VariantsEnd = VariantsStart;
484 unsigned NestedBraces = 1;
485 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
486 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
487 if (--NestedBraces == 0)
488 break;
489 } else if (Cur[VariantsEnd] == '{')
490 ++NestedBraces;
491 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000492
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000493 // Select the Nth variant (or empty).
494 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
495 for (unsigned i = 0; i != Variant; ++i)
496 Selection = Selection.split('|').second;
497 Res += Selection.split('|').first;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000498
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000499 assert(VariantsEnd != Cur.size() &&
500 "Unterminated variants in assembly string!");
501 Cur = Cur.substr(VariantsEnd + 1);
502 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000503
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000504 return Res;
505}
506
Daniel Sandersc54aa9c2017-11-18 00:16:44 +0000507bool CodeGenInstruction::isOperandAPointer(unsigned i) const {
508 if (DagInit *ConstraintList = TheDef->getValueAsDag("InOperandList")) {
509 if (i < ConstraintList->getNumArgs()) {
510 if (DefInit *Constraint = dyn_cast<DefInit>(ConstraintList->getArg(i))) {
511 return Constraint->getDef()->isSubClassOf("TypedOperand") &&
512 Constraint->getDef()->getValueAsBit("IsPointer");
513 }
514 }
515 }
516 return false;
517}
Chris Lattner488c2012010-11-01 04:05:41 +0000518
519//===----------------------------------------------------------------------===//
520/// CodeGenInstAlias Implementation
521//===----------------------------------------------------------------------===//
522
Bob Wilsonb9b24222011-01-26 19:44:55 +0000523/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
524/// constructor. It checks if an argument in an InstAlias pattern matches
525/// the corresponding operand of the instruction. It returns true on a
526/// successful match, with ResOp set to the result operand to be used.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000527bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
Bob Wilsonb9b24222011-01-26 19:44:55 +0000528 Record *InstOpRec, bool hasSubOps,
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +0000529 ArrayRef<SMLoc> Loc, CodeGenTarget &T,
Bob Wilsonb9b24222011-01-26 19:44:55 +0000530 ResultOperand &ResOp) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000531 Init *Arg = Result->getArg(AliasOpNo);
Sean Silvafb509ed2012-10-10 20:24:43 +0000532 DefInit *ADI = dyn_cast<DefInit>(Arg);
Craig Topper24064772014-04-15 07:20:03 +0000533 Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
Bob Wilsonb9b24222011-01-26 19:44:55 +0000534
535 if (ADI && ADI->getDef() == InstOpRec) {
536 // If the operand is a record, it must have a name, and the record type
537 // must match up with the instruction's argument type.
Matthias Braunbb053162016-12-05 06:00:46 +0000538 if (!Result->getArgName(AliasOpNo))
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000539 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
540 " must have a name!");
Matthias Braunbb053162016-12-05 06:00:46 +0000541 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
Bob Wilsonb9b24222011-01-26 19:44:55 +0000542 return true;
543 }
544
Jim Grosbach6acb1482011-10-28 16:43:40 +0000545 // For register operands, the source register class can be a subclass
546 // of the instruction register class, not just an exact match.
Tim Northover3e38d292014-03-29 09:03:22 +0000547 if (InstOpRec->isSubClassOf("RegisterOperand"))
548 InstOpRec = InstOpRec->getValueAsDef("RegClass");
549
550 if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand"))
551 ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit();
552
Jim Grosbach6acb1482011-10-28 16:43:40 +0000553 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
554 if (!InstOpRec->isSubClassOf("RegisterClass"))
555 return false;
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000556 if (!T.getRegisterClass(InstOpRec)
557 .hasSubClass(&T.getRegisterClass(ADI->getDef())))
558 return false;
Matthias Braunbb053162016-12-05 06:00:46 +0000559 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000560 return true;
Jim Grosbach6acb1482011-10-28 16:43:40 +0000561 }
562
Bob Wilsonb9b24222011-01-26 19:44:55 +0000563 // Handle explicit registers.
564 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
Jim Grosbacha7b2d442011-08-19 20:33:06 +0000565 if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
566 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
567 // The operand info should only have a single (register) entry. We
568 // want the register class of it.
Sean Silva88eb8dd2012-10-10 20:24:47 +0000569 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
Jim Grosbacha7b2d442011-08-19 20:33:06 +0000570 }
571
Bob Wilsonb9b24222011-01-26 19:44:55 +0000572 if (!InstOpRec->isSubClassOf("RegisterClass"))
573 return false;
574
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000575 if (!T.getRegisterClass(InstOpRec)
576 .contains(T.getRegBank().getReg(ADI->getDef())))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000577 PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
578 " is not a member of the " + InstOpRec->getName() +
579 " register class!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000580
Matthias Braunbb053162016-12-05 06:00:46 +0000581 if (Result->getArgName(AliasOpNo))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000582 PrintFatalError(Loc, "result fixed register argument must "
583 "not have a name!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000584
Tim Northover3e38d292014-03-29 09:03:22 +0000585 ResOp = ResultOperand(ResultRecord);
Bob Wilsonb9b24222011-01-26 19:44:55 +0000586 return true;
587 }
588
589 // Handle "zero_reg" for optional def operands.
590 if (ADI && ADI->getDef()->getName() == "zero_reg") {
591
592 // Check if this is an optional def.
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000593 // Tied operands where the source is a sub-operand of a complex operand
594 // need to represent both operands in the alias destination instruction.
595 // Allow zero_reg for the tied portion. This can and should go away once
596 // the MC representation of things doesn't use tied operands at all.
597 //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
598 // throw TGError(Loc, "reg0 used for result that is not an "
599 // "OptionalDefOperand!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000600
Craig Topper24064772014-04-15 07:20:03 +0000601 ResOp = ResultOperand(static_cast<Record*>(nullptr));
Bob Wilsonb9b24222011-01-26 19:44:55 +0000602 return true;
603 }
604
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000605 // Literal integers.
Sean Silvafb509ed2012-10-10 20:24:43 +0000606 if (IntInit *II = dyn_cast<IntInit>(Arg)) {
Bob Wilsonb9b24222011-01-26 19:44:55 +0000607 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
608 return false;
609 // Integer arguments can't have names.
Matthias Braunbb053162016-12-05 06:00:46 +0000610 if (Result->getArgName(AliasOpNo))
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000611 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000612 " must not have a name!");
Bob Wilsonb9b24222011-01-26 19:44:55 +0000613 ResOp = ResultOperand(II->getValue());
614 return true;
615 }
616
Pete Cooper2cfdfe52014-08-07 05:47:04 +0000617 // Bits<n> (also used for 0bxx literals)
618 if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
619 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
620 return false;
621 if (!BI->isComplete())
622 return false;
623 // Convert the bits init to an integer and use that for the result.
624 IntInit *II =
625 dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get()));
626 if (!II)
627 return false;
628 ResOp = ResultOperand(II->getValue());
629 return true;
630 }
631
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000632 // If both are Operands with the same MVT, allow the conversion. It's
633 // up to the user to make sure the values are appropriate, just like
634 // for isel Pat's.
Michael Ilseman5be22a12014-12-12 21:48:03 +0000635 if (InstOpRec->isSubClassOf("Operand") && ADI &&
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000636 ADI->getDef()->isSubClassOf("Operand")) {
637 // FIXME: What other attributes should we check here? Identical
638 // MIOperandInfo perhaps?
639 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
640 return false;
Matthias Braunbb053162016-12-05 06:00:46 +0000641 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ADI->getDef());
Jim Grosbachd1f1b792011-10-28 22:32:53 +0000642 return true;
643 }
644
Bob Wilsonb9b24222011-01-26 19:44:55 +0000645 return false;
646}
647
Tim Northover60091cf2014-05-15 13:36:01 +0000648unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
649 if (!isRecord())
650 return 1;
651
652 Record *Rec = getRecord();
653 if (!Rec->isSubClassOf("Operand"))
654 return 1;
655
656 DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
657 if (MIOpInfo->getNumArgs() == 0) {
658 // Unspecified, so it defaults to 1
659 return 1;
660 }
661
662 return MIOpInfo->getNumArgs();
663}
664
Craig Topper2be74392018-06-18 01:28:01 +0000665CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T)
Tim Northoverd8d65a62014-05-15 11:16:32 +0000666 : TheDef(R) {
Chris Lattnerdd3b09c2010-11-01 05:34:34 +0000667 Result = R->getValueAsDag("ResultInst");
Tim Northoverd8d65a62014-05-15 11:16:32 +0000668 AsmString = R->getValueAsString("AsmString");
Tim Northoverd8d65a62014-05-15 11:16:32 +0000669
Chris Lattner8ffd1292010-11-06 06:39:47 +0000670
671 // Verify that the root of the result is an instruction.
Sean Silvafb509ed2012-10-10 20:24:43 +0000672 DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
Craig Topper24064772014-04-15 07:20:03 +0000673 if (!DI || !DI->getDef()->isSubClassOf("Instruction"))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000674 PrintFatalError(R->getLoc(),
675 "result of inst alias should be an instruction");
Chris Lattner8ffd1292010-11-06 06:39:47 +0000676
677 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000678
Chris Lattnerb625dd22010-11-06 07:06:09 +0000679 // NameClass - If argument names are repeated, we need to verify they have
680 // the same class.
681 StringMap<Record*> NameClass;
Bob Wilsona48eacc2011-01-20 18:38:10 +0000682 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000683 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
Matthias Braunbb053162016-12-05 06:00:46 +0000684 if (!ADI || !Result->getArgName(i))
Bob Wilsona48eacc2011-01-20 18:38:10 +0000685 continue;
686 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
687 // $foo can exist multiple times in the result list, but it must have the
688 // same type.
Matthias Braunbb053162016-12-05 06:00:46 +0000689 Record *&Entry = NameClass[Result->getArgNameStr(i)];
Bob Wilsona48eacc2011-01-20 18:38:10 +0000690 if (Entry && Entry != ADI->getDef())
Matthias Braunbb053162016-12-05 06:00:46 +0000691 PrintFatalError(R->getLoc(), "result value $" + Result->getArgNameStr(i) +
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000692 " is both " + Entry->getName() + " and " +
693 ADI->getDef()->getName() + "!");
Bob Wilsona48eacc2011-01-20 18:38:10 +0000694 Entry = ADI->getDef();
695 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000696
Chris Lattner76030522010-11-06 06:54:38 +0000697 // Decode and validate the arguments of the result.
Chris Lattner8188fb22010-11-06 07:31:43 +0000698 unsigned AliasOpNo = 0;
699 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsonb9b24222011-01-26 19:44:55 +0000700
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000701 // Tied registers don't have an entry in the result dag unless they're part
702 // of a complex operand, in which case we include them anyways, as we
703 // don't have any other way to specify the whole operand.
704 if (ResultInst->Operands[i].MINumOperands == 1 &&
Sander de Smalen118099a2018-06-18 13:39:29 +0000705 ResultInst->Operands[i].getTiedRegister() != -1) {
706 // Tied operands of different RegisterClass should be explicit within an
707 // instruction's syntax and so cannot be skipped.
708 int TiedOpNum = ResultInst->Operands[i].getTiedRegister();
709 if (ResultInst->Operands[i].Rec->getName() ==
710 ResultInst->Operands[TiedOpNum].Rec->getName())
711 continue;
712 }
Chris Lattner8188fb22010-11-06 07:31:43 +0000713
714 if (AliasOpNo >= Result->getNumArgs())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000715 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000716
Bob Wilsonb9b24222011-01-26 19:44:55 +0000717 Record *InstOpRec = ResultInst->Operands[i].Rec;
718 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
719 ResultOperand ResOp(static_cast<int64_t>(0));
720 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
721 R->getLoc(), T, ResOp)) {
Owen Andersonda6bd3e2012-06-08 00:25:03 +0000722 // If this is a simple operand, or a complex operand with a custom match
723 // class, then we can match is verbatim.
724 if (NumSubOps == 1 ||
725 (InstOpRec->getValue("ParserMatchClass") &&
726 InstOpRec->getValueAsDef("ParserMatchClass")
727 ->getValueAsString("Name") != "Imm")) {
728 ResultOperands.push_back(ResOp);
729 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
730 ++AliasOpNo;
731
732 // Otherwise, we need to match each of the suboperands individually.
733 } else {
734 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
735 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
Sean Silva88eb8dd2012-10-10 20:24:47 +0000736 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Owen Andersonda6bd3e2012-06-08 00:25:03 +0000737
738 // Take care to instantiate each of the suboperands with the correct
739 // nomenclature: $foo.bar
Matthias Braunbb053162016-12-05 06:00:46 +0000740 ResultOperands.emplace_back(
741 Result->getArgName(AliasOpNo)->getAsUnquotedString() + "." +
742 MIOI->getArgName(SubOp)->getAsUnquotedString(), SubRec);
Owen Andersonda6bd3e2012-06-08 00:25:03 +0000743 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
744 }
745 ++AliasOpNo;
746 }
Chris Lattner76030522010-11-06 06:54:38 +0000747 continue;
748 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000749
Bob Wilsonb9b24222011-01-26 19:44:55 +0000750 // If the argument did not match the instruction operand, and the operand
751 // is composed of multiple suboperands, try matching the suboperands.
752 if (NumSubOps > 1) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000753 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
Bob Wilsonb9b24222011-01-26 19:44:55 +0000754 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
755 if (AliasOpNo >= Result->getNumArgs())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000756 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
Sean Silva88eb8dd2012-10-10 20:24:47 +0000757 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Bob Wilsonb9b24222011-01-26 19:44:55 +0000758 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
759 R->getLoc(), T, ResOp)) {
760 ResultOperands.push_back(ResOp);
761 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
762 ++AliasOpNo;
763 } else {
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000764 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
Bob Wilsonb9b24222011-01-26 19:44:55 +0000765 " does not match instruction operand class " +
766 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
767 }
768 }
Chris Lattnerb6f8e822010-11-06 19:25:43 +0000769 continue;
770 }
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000771 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000772 " does not match instruction operand class " +
773 InstOpRec->getName());
Chris Lattner76030522010-11-06 06:54:38 +0000774 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000775
Chris Lattner8188fb22010-11-06 07:31:43 +0000776 if (AliasOpNo != Result->getNumArgs())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000777 PrintFatalError(R->getLoc(), "too many operands for instruction!");
Chris Lattner488c2012010-11-01 04:05:41 +0000778}