1009b1c42SEd Schouten //===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // This file implements the CodeGenInstruction class.
10009b1c42SEd Schouten //
11009b1c42SEd Schouten //===----------------------------------------------------------------------===//
12009b1c42SEd Schouten
13009b1c42SEd Schouten #include "CodeGenInstruction.h"
14104bd817SRoman Divacky #include "CodeGenTarget.h"
15009b1c42SEd Schouten #include "llvm/ADT/StringExtras.h"
164a16efa3SDimitry Andric #include "llvm/TableGen/Error.h"
174a16efa3SDimitry Andric #include "llvm/TableGen/Record.h"
18009b1c42SEd Schouten #include <set>
19009b1c42SEd Schouten using namespace llvm;
20009b1c42SEd Schouten
21cf099d11SDimitry Andric //===----------------------------------------------------------------------===//
22cf099d11SDimitry Andric // CGIOperandList Implementation
23cf099d11SDimitry Andric //===----------------------------------------------------------------------===//
241e7804dbSRoman Divacky
CGIOperandList(Record * R)25cf099d11SDimitry Andric CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
26cf099d11SDimitry Andric isPredicable = false;
27009b1c42SEd Schouten hasOptionalDef = false;
28009b1c42SEd Schouten isVariadic = false;
29009b1c42SEd Schouten
302f12f10aSRoman Divacky DagInit *OutDI = R->getValueAsDag("OutOperandList");
31009b1c42SEd Schouten
32522600a2SDimitry Andric if (DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) {
332f12f10aSRoman Divacky if (Init->getDef()->getName() != "outs")
34e6d15924SDimitry Andric PrintFatalError(R->getLoc(),
35e6d15924SDimitry Andric R->getName() +
36e6d15924SDimitry Andric ": invalid def name for output list: use 'outs'");
372f12f10aSRoman Divacky } else
38e6d15924SDimitry Andric PrintFatalError(R->getLoc(),
39e6d15924SDimitry Andric R->getName() + ": invalid output list: use 'outs'");
402f12f10aSRoman Divacky
412f12f10aSRoman Divacky NumDefs = OutDI->getNumArgs();
422f12f10aSRoman Divacky
432f12f10aSRoman Divacky DagInit *InDI = R->getValueAsDag("InOperandList");
44522600a2SDimitry Andric if (DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) {
452f12f10aSRoman Divacky if (Init->getDef()->getName() != "ins")
46e6d15924SDimitry Andric PrintFatalError(R->getLoc(),
47e6d15924SDimitry Andric R->getName() +
48e6d15924SDimitry Andric ": invalid def name for input list: use 'ins'");
492f12f10aSRoman Divacky } else
50e6d15924SDimitry Andric PrintFatalError(R->getLoc(),
51e6d15924SDimitry Andric R->getName() + ": invalid input list: use 'ins'");
52009b1c42SEd Schouten
53009b1c42SEd Schouten unsigned MIOperandNo = 0;
54009b1c42SEd Schouten std::set<std::string> OperandNames;
5501095a5dSDimitry Andric unsigned e = InDI->getNumArgs() + OutDI->getNumArgs();
5601095a5dSDimitry Andric OperandList.reserve(e);
57cfca06d7SDimitry Andric bool VariadicOuts = false;
5801095a5dSDimitry Andric for (unsigned i = 0; i != e; ++i) {
592f12f10aSRoman Divacky Init *ArgInit;
60b915e9e0SDimitry Andric StringRef ArgName;
612f12f10aSRoman Divacky if (i < NumDefs) {
622f12f10aSRoman Divacky ArgInit = OutDI->getArg(i);
63b915e9e0SDimitry Andric ArgName = OutDI->getArgNameStr(i);
642f12f10aSRoman Divacky } else {
652f12f10aSRoman Divacky ArgInit = InDI->getArg(i - NumDefs);
66b915e9e0SDimitry Andric ArgName = InDI->getArgNameStr(i - NumDefs);
672f12f10aSRoman Divacky }
682f12f10aSRoman Divacky
69e3b55780SDimitry Andric DagInit *SubArgDag = dyn_cast<DagInit>(ArgInit);
70e3b55780SDimitry Andric if (SubArgDag)
71e3b55780SDimitry Andric ArgInit = SubArgDag->getOperator();
72e3b55780SDimitry Andric
73522600a2SDimitry Andric DefInit *Arg = dyn_cast<DefInit>(ArgInit);
74009b1c42SEd Schouten if (!Arg)
75e6d15924SDimitry Andric PrintFatalError(R->getLoc(), "Illegal operand for the '" + R->getName() +
76e6d15924SDimitry Andric "' instruction!");
77009b1c42SEd Schouten
78009b1c42SEd Schouten Record *Rec = Arg->getDef();
79009b1c42SEd Schouten std::string PrintMethod = "printOperand";
80cf099d11SDimitry Andric std::string EncoderMethod;
81411bd29eSDimitry Andric std::string OperandType = "OPERAND_UNKNOWN";
8267c32a98SDimitry Andric std::string OperandNamespace = "MCOI";
83009b1c42SEd Schouten unsigned NumOps = 1;
845ca98fd9SDimitry Andric DagInit *MIOpInfo = nullptr;
85411bd29eSDimitry Andric if (Rec->isSubClassOf("RegisterOperand")) {
86cfca06d7SDimitry Andric PrintMethod = std::string(Rec->getValueAsString("PrintMethod"));
87cfca06d7SDimitry Andric OperandType = std::string(Rec->getValueAsString("OperandType"));
88cfca06d7SDimitry Andric OperandNamespace = std::string(Rec->getValueAsString("OperandNamespace"));
89cfca06d7SDimitry Andric EncoderMethod = std::string(Rec->getValueAsString("EncoderMethod"));
90411bd29eSDimitry Andric } else if (Rec->isSubClassOf("Operand")) {
91cfca06d7SDimitry Andric PrintMethod = std::string(Rec->getValueAsString("PrintMethod"));
92cfca06d7SDimitry Andric OperandType = std::string(Rec->getValueAsString("OperandType"));
93cfca06d7SDimitry Andric OperandNamespace = std::string(Rec->getValueAsString("OperandNamespace"));
94cf099d11SDimitry Andric // If there is an explicit encoder method, use it.
95cfca06d7SDimitry Andric EncoderMethod = std::string(Rec->getValueAsString("EncoderMethod"));
96009b1c42SEd Schouten MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
97009b1c42SEd Schouten
98009b1c42SEd Schouten // Verify that MIOpInfo has an 'ops' root value.
99522600a2SDimitry Andric if (!isa<DefInit>(MIOpInfo->getOperator()) ||
100522600a2SDimitry Andric cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
101e6d15924SDimitry Andric PrintFatalError(R->getLoc(),
102e6d15924SDimitry Andric "Bad value for MIOperandInfo in operand '" +
103e6d15924SDimitry Andric Rec->getName() + "'\n");
104009b1c42SEd Schouten
105009b1c42SEd Schouten // If we have MIOpInfo, then we have #operands equal to number of entries
106009b1c42SEd Schouten // in MIOperandInfo.
107009b1c42SEd Schouten if (unsigned NumArgs = MIOpInfo->getNumArgs())
108009b1c42SEd Schouten NumOps = NumArgs;
109009b1c42SEd Schouten
110f8af5cf6SDimitry Andric if (Rec->isSubClassOf("PredicateOp"))
111009b1c42SEd Schouten isPredicable = true;
112009b1c42SEd Schouten else if (Rec->isSubClassOf("OptionalDefOperand"))
113009b1c42SEd Schouten hasOptionalDef = true;
114009b1c42SEd Schouten } else if (Rec->getName() == "variable_ops") {
115cfca06d7SDimitry Andric if (i < NumDefs)
116cfca06d7SDimitry Andric VariadicOuts = true;
117009b1c42SEd Schouten isVariadic = true;
118009b1c42SEd Schouten continue;
119411bd29eSDimitry Andric } else if (Rec->isSubClassOf("RegisterClass")) {
120411bd29eSDimitry Andric OperandType = "OPERAND_REGISTER";
121411bd29eSDimitry Andric } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
122e3b55780SDimitry Andric !Rec->isSubClassOf("unknown_class")) {
123e6d15924SDimitry Andric PrintFatalError(R->getLoc(), "Unknown operand class '" + Rec->getName() +
124e6d15924SDimitry Andric "' in '" + R->getName() +
125e6d15924SDimitry Andric "' instruction!");
126e3b55780SDimitry Andric }
127009b1c42SEd Schouten
128009b1c42SEd Schouten // Check that the operand has a name and that it's unique.
1292f12f10aSRoman Divacky if (ArgName.empty())
130e6d15924SDimitry Andric PrintFatalError(R->getLoc(), "In instruction '" + R->getName() +
131e6d15924SDimitry Andric "', operand #" + Twine(i) +
132e6d15924SDimitry Andric " has no name!");
133cfca06d7SDimitry Andric if (!OperandNames.insert(std::string(ArgName)).second)
134e6d15924SDimitry Andric PrintFatalError(R->getLoc(),
135e6d15924SDimitry Andric "In instruction '" + R->getName() + "', operand #" +
136e6d15924SDimitry Andric Twine(i) +
137e6d15924SDimitry Andric " has the same name as a previous operand!");
138009b1c42SEd Schouten
139e3b55780SDimitry Andric OperandInfo &OpInfo = OperandList.emplace_back(
140cfca06d7SDimitry Andric Rec, std::string(ArgName), std::string(PrintMethod),
141e3b55780SDimitry Andric OperandNamespace + "::" + OperandType, MIOperandNo, NumOps, MIOpInfo);
142e3b55780SDimitry Andric
143e3b55780SDimitry Andric if (SubArgDag) {
144e3b55780SDimitry Andric if (SubArgDag->getNumArgs() != NumOps) {
145e3b55780SDimitry Andric PrintFatalError(R->getLoc(), "In instruction '" + R->getName() +
146e3b55780SDimitry Andric "', operand #" + Twine(i) + " has " +
147e3b55780SDimitry Andric Twine(SubArgDag->getNumArgs()) +
148e3b55780SDimitry Andric " sub-arg names, expected " +
149e3b55780SDimitry Andric Twine(NumOps) + ".");
150e3b55780SDimitry Andric }
151e3b55780SDimitry Andric
152e3b55780SDimitry Andric for (unsigned j = 0; j < NumOps; ++j) {
153e3b55780SDimitry Andric if (!isa<UnsetInit>(SubArgDag->getArg(j)))
154e3b55780SDimitry Andric PrintFatalError(R->getLoc(),
155e3b55780SDimitry Andric "In instruction '" + R->getName() + "', operand #" +
156e3b55780SDimitry Andric Twine(i) + " sub-arg #" + Twine(j) +
157e3b55780SDimitry Andric " has unexpected operand (expected only $name).");
158e3b55780SDimitry Andric
159e3b55780SDimitry Andric StringRef SubArgName = SubArgDag->getArgNameStr(j);
160e3b55780SDimitry Andric if (SubArgName.empty())
161e3b55780SDimitry Andric PrintFatalError(R->getLoc(), "In instruction '" + R->getName() +
162e3b55780SDimitry Andric "', operand #" + Twine(i) +
163e3b55780SDimitry Andric " has no name!");
164e3b55780SDimitry Andric if (!OperandNames.insert(std::string(SubArgName)).second)
165e3b55780SDimitry Andric PrintFatalError(R->getLoc(),
166e3b55780SDimitry Andric "In instruction '" + R->getName() + "', operand #" +
167e3b55780SDimitry Andric Twine(i) + " sub-arg #" + Twine(j) +
168e3b55780SDimitry Andric " has the same name as a previous operand!");
169e3b55780SDimitry Andric
170e3b55780SDimitry Andric if (auto MaybeEncoderMethod =
171e3b55780SDimitry Andric cast<DefInit>(MIOpInfo->getArg(j))
172e3b55780SDimitry Andric ->getDef()
173e3b55780SDimitry Andric ->getValueAsOptionalString("EncoderMethod")) {
174e3b55780SDimitry Andric OpInfo.EncoderMethodNames[j] = *MaybeEncoderMethod;
175e3b55780SDimitry Andric }
176e3b55780SDimitry Andric
177e3b55780SDimitry Andric OpInfo.SubOpNames[j] = SubArgName;
178ac9a064cSDimitry Andric SubOpAliases[SubArgName] = std::pair(i, j);
179e3b55780SDimitry Andric }
180e3b55780SDimitry Andric } else if (!EncoderMethod.empty()) {
181e3b55780SDimitry Andric // If we have no explicit sub-op dag, but have an top-level encoder
182e3b55780SDimitry Andric // method, the single encoder will multiple sub-ops, itself.
183e3b55780SDimitry Andric OpInfo.EncoderMethodNames[0] = EncoderMethod;
184e3b55780SDimitry Andric for (unsigned j = 1; j < NumOps; ++j)
185e3b55780SDimitry Andric OpInfo.DoNotEncode[j] = true;
186e3b55780SDimitry Andric }
187e3b55780SDimitry Andric
188009b1c42SEd Schouten MIOperandNo += NumOps;
189009b1c42SEd Schouten }
190009b1c42SEd Schouten
191cfca06d7SDimitry Andric if (VariadicOuts)
192cfca06d7SDimitry Andric --NumDefs;
193009b1c42SEd Schouten }
194cf099d11SDimitry Andric
195009b1c42SEd Schouten /// getOperandNamed - Return the index of the operand with the specified
196009b1c42SEd Schouten /// non-empty name. If the instruction does not have an operand with the
197522600a2SDimitry Andric /// specified name, abort.
198009b1c42SEd Schouten ///
getOperandNamed(StringRef Name) const199cf099d11SDimitry Andric unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
200cf099d11SDimitry Andric unsigned OpIdx;
201e6d15924SDimitry Andric if (hasOperandNamed(Name, OpIdx))
202e6d15924SDimitry Andric return OpIdx;
203e6d15924SDimitry Andric PrintFatalError(TheDef->getLoc(), "'" + TheDef->getName() +
204e6d15924SDimitry Andric "' does not have an operand named '$" +
205e6d15924SDimitry Andric Name + "'!");
206cf099d11SDimitry Andric }
207cf099d11SDimitry Andric
208cf099d11SDimitry Andric /// hasOperandNamed - Query whether the instruction has an operand of the
209cf099d11SDimitry Andric /// given name. If so, return true and set OpIdx to the index of the
210cf099d11SDimitry Andric /// operand. Otherwise, return false.
hasOperandNamed(StringRef Name,unsigned & OpIdx) const211cf099d11SDimitry Andric bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
212009b1c42SEd Schouten assert(!Name.empty() && "Cannot search for operand with no name!");
213009b1c42SEd Schouten for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
214cf099d11SDimitry Andric if (OperandList[i].Name == Name) {
215cf099d11SDimitry Andric OpIdx = i;
216cf099d11SDimitry Andric return true;
217cf099d11SDimitry Andric }
218cf099d11SDimitry Andric return false;
219009b1c42SEd Schouten }
220009b1c42SEd Schouten
hasSubOperandAlias(StringRef Name,std::pair<unsigned,unsigned> & SubOp) const221e3b55780SDimitry Andric bool CGIOperandList::hasSubOperandAlias(
222e3b55780SDimitry Andric StringRef Name, std::pair<unsigned, unsigned> &SubOp) const {
223e3b55780SDimitry Andric assert(!Name.empty() && "Cannot search for operand with no name!");
224e3b55780SDimitry Andric auto SubOpIter = SubOpAliases.find(Name);
225e3b55780SDimitry Andric if (SubOpIter != SubOpAliases.end()) {
226e3b55780SDimitry Andric SubOp = SubOpIter->second;
227e3b55780SDimitry Andric return true;
228e3b55780SDimitry Andric }
229e3b55780SDimitry Andric return false;
230e3b55780SDimitry Andric }
231e3b55780SDimitry Andric
232009b1c42SEd Schouten std::pair<unsigned, unsigned>
ParseOperandName(StringRef Op,bool AllowWholeOp)233344a3780SDimitry Andric CGIOperandList::ParseOperandName(StringRef Op, bool AllowWholeOp) {
234ac9a064cSDimitry Andric if (!Op.starts_with("$"))
235e6d15924SDimitry Andric PrintFatalError(TheDef->getLoc(),
236e6d15924SDimitry Andric TheDef->getName() + ": Illegal operand name: '" + Op + "'");
237009b1c42SEd Schouten
238344a3780SDimitry Andric StringRef OpName = Op.substr(1);
239344a3780SDimitry Andric StringRef SubOpName;
240009b1c42SEd Schouten
241009b1c42SEd Schouten // Check to see if this is $foo.bar.
242344a3780SDimitry Andric StringRef::size_type DotIdx = OpName.find_first_of('.');
243344a3780SDimitry Andric if (DotIdx != StringRef::npos) {
244009b1c42SEd Schouten SubOpName = OpName.substr(DotIdx + 1);
245009b1c42SEd Schouten if (SubOpName.empty())
246e6d15924SDimitry Andric PrintFatalError(TheDef->getLoc(),
247e6d15924SDimitry Andric TheDef->getName() +
248e6d15924SDimitry Andric ": illegal empty suboperand name in '" + Op + "'");
249009b1c42SEd Schouten OpName = OpName.substr(0, DotIdx);
250009b1c42SEd Schouten }
251009b1c42SEd Schouten
252e3b55780SDimitry Andric unsigned OpIdx;
253e3b55780SDimitry Andric
254e3b55780SDimitry Andric if (std::pair<unsigned, unsigned> SubOp; hasSubOperandAlias(OpName, SubOp)) {
255e3b55780SDimitry Andric // Found a name for a piece of an operand, just return it directly.
256e3b55780SDimitry Andric if (!SubOpName.empty()) {
257e3b55780SDimitry Andric PrintFatalError(
258e3b55780SDimitry Andric TheDef->getLoc(),
259e3b55780SDimitry Andric TheDef->getName() +
260e3b55780SDimitry Andric ": Cannot use dotted suboperand name within suboperand '" +
261e3b55780SDimitry Andric OpName + "'");
262e3b55780SDimitry Andric }
263e3b55780SDimitry Andric return SubOp;
264e3b55780SDimitry Andric }
265e3b55780SDimitry Andric
266e3b55780SDimitry Andric OpIdx = getOperandNamed(OpName);
267009b1c42SEd Schouten
268009b1c42SEd Schouten if (SubOpName.empty()) { // If no suboperand name was specified:
269009b1c42SEd Schouten // If one was needed, throw.
270009b1c42SEd Schouten if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
271009b1c42SEd Schouten SubOpName.empty())
272e6d15924SDimitry Andric PrintFatalError(TheDef->getLoc(),
273e6d15924SDimitry Andric TheDef->getName() +
274e6d15924SDimitry Andric ": Illegal to refer to"
275e6d15924SDimitry Andric " whole operand part of complex operand '" +
276e6d15924SDimitry Andric Op + "'");
277009b1c42SEd Schouten
278009b1c42SEd Schouten // Otherwise, return the operand.
279ac9a064cSDimitry Andric return std::pair(OpIdx, 0U);
280009b1c42SEd Schouten }
281009b1c42SEd Schouten
282009b1c42SEd Schouten // Find the suboperand number involved.
283009b1c42SEd Schouten DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
2845ca98fd9SDimitry Andric if (!MIOpInfo)
285e6d15924SDimitry Andric PrintFatalError(TheDef->getLoc(), TheDef->getName() +
286e6d15924SDimitry Andric ": unknown suboperand name in '" +
287e6d15924SDimitry Andric Op + "'");
288009b1c42SEd Schouten
289009b1c42SEd Schouten // Find the operand with the right name.
290009b1c42SEd Schouten for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
291b915e9e0SDimitry Andric if (MIOpInfo->getArgNameStr(i) == SubOpName)
292ac9a064cSDimitry Andric return std::pair(OpIdx, i);
293009b1c42SEd Schouten
294009b1c42SEd Schouten // Otherwise, didn't find it!
295e6d15924SDimitry Andric PrintFatalError(TheDef->getLoc(), TheDef->getName() +
296e6d15924SDimitry Andric ": unknown suboperand name in '" + Op +
297e6d15924SDimitry Andric "'");
298ac9a064cSDimitry Andric return std::pair(0U, 0U);
299009b1c42SEd Schouten }
300104bd817SRoman Divacky
ParseConstraint(StringRef CStr,CGIOperandList & Ops,Record * Rec)301ac9a064cSDimitry Andric static void ParseConstraint(StringRef CStr, CGIOperandList &Ops, Record *Rec) {
302cf099d11SDimitry Andric // EARLY_CLOBBER: @early $reg
303344a3780SDimitry Andric StringRef::size_type wpos = CStr.find_first_of(" \t");
304344a3780SDimitry Andric StringRef::size_type start = CStr.find_first_not_of(" \t");
305344a3780SDimitry Andric StringRef Tok = CStr.substr(start, wpos - start);
306cf099d11SDimitry Andric if (Tok == "@earlyclobber") {
307344a3780SDimitry Andric StringRef Name = CStr.substr(wpos + 1);
308cf099d11SDimitry Andric wpos = Name.find_first_not_of(" \t");
309344a3780SDimitry Andric if (wpos == StringRef::npos)
310ac9a064cSDimitry Andric PrintFatalError(Rec->getLoc(),
311ac9a064cSDimitry Andric "Illegal format for @earlyclobber constraint in '" +
312d8e91e46SDimitry Andric Rec->getName() + "': '" + CStr + "'");
313cf099d11SDimitry Andric Name = Name.substr(wpos);
314cf099d11SDimitry Andric std::pair<unsigned, unsigned> Op = Ops.ParseOperandName(Name, false);
315cf099d11SDimitry Andric
316cf099d11SDimitry Andric // Build the string for the operand
317cf099d11SDimitry Andric if (!Ops[Op.first].Constraints[Op.second].isNone())
318ac9a064cSDimitry Andric PrintFatalError(Rec->getLoc(), "Operand '" + Name + "' of '" +
319ac9a064cSDimitry Andric Rec->getName() +
320d8e91e46SDimitry Andric "' cannot have multiple constraints!");
321cf099d11SDimitry Andric Ops[Op.first].Constraints[Op.second] =
322cf099d11SDimitry Andric CGIOperandList::ConstraintInfo::getEarlyClobber();
323cf099d11SDimitry Andric return;
324cf099d11SDimitry Andric }
325cf099d11SDimitry Andric
326cf099d11SDimitry Andric // Only other constraint is "TIED_TO" for now.
327344a3780SDimitry Andric StringRef::size_type pos = CStr.find_first_of('=');
328ac9a064cSDimitry Andric if (pos == StringRef::npos || pos == 0 ||
329ac9a064cSDimitry Andric CStr.find_first_of(" \t", pos) != (pos + 1) ||
330ac9a064cSDimitry Andric CStr.find_last_of(" \t", pos) != (pos - 1))
331ac9a064cSDimitry Andric PrintFatalError(Rec->getLoc(), "Unrecognized constraint '" + CStr +
332d8e91e46SDimitry Andric "' in '" + Rec->getName() + "'");
333cf099d11SDimitry Andric start = CStr.find_first_not_of(" \t");
334cf099d11SDimitry Andric
335cf099d11SDimitry Andric // TIED_TO: $src1 = $dst
336d8e91e46SDimitry Andric wpos = CStr.find_first_of(" \t", start);
337344a3780SDimitry Andric if (wpos == StringRef::npos || wpos > pos)
338ac9a064cSDimitry Andric PrintFatalError(Rec->getLoc(),
339ac9a064cSDimitry Andric "Illegal format for tied-to constraint in '" +
340d8e91e46SDimitry Andric Rec->getName() + "': '" + CStr + "'");
341344a3780SDimitry Andric StringRef LHSOpName = CStr.substr(start, wpos - start);
342d8e91e46SDimitry Andric std::pair<unsigned, unsigned> LHSOp = Ops.ParseOperandName(LHSOpName, false);
343d8e91e46SDimitry Andric
344d8e91e46SDimitry Andric wpos = CStr.find_first_not_of(" \t", pos + 1);
345344a3780SDimitry Andric if (wpos == StringRef::npos)
346ac9a064cSDimitry Andric PrintFatalError(Rec->getLoc(),
347ac9a064cSDimitry Andric "Illegal format for tied-to constraint: '" + CStr + "'");
348cf099d11SDimitry Andric
349344a3780SDimitry Andric StringRef RHSOpName = CStr.substr(wpos);
350d8e91e46SDimitry Andric std::pair<unsigned, unsigned> RHSOp = Ops.ParseOperandName(RHSOpName, false);
351cf099d11SDimitry Andric
352d8e91e46SDimitry Andric // Sort the operands into order, which should put the output one
353d8e91e46SDimitry Andric // first. But keep the original order, for use in diagnostics.
354d8e91e46SDimitry Andric bool FirstIsDest = (LHSOp < RHSOp);
355d8e91e46SDimitry Andric std::pair<unsigned, unsigned> DestOp = (FirstIsDest ? LHSOp : RHSOp);
356d8e91e46SDimitry Andric StringRef DestOpName = (FirstIsDest ? LHSOpName : RHSOpName);
357d8e91e46SDimitry Andric std::pair<unsigned, unsigned> SrcOp = (FirstIsDest ? RHSOp : LHSOp);
358d8e91e46SDimitry Andric StringRef SrcOpName = (FirstIsDest ? RHSOpName : LHSOpName);
359cf099d11SDimitry Andric
360d8e91e46SDimitry Andric // Ensure one operand is a def and the other is a use.
361d8e91e46SDimitry Andric if (DestOp.first >= Ops.NumDefs)
362ac9a064cSDimitry Andric PrintFatalError(Rec->getLoc(), "Input operands '" + LHSOpName + "' and '" +
363ac9a064cSDimitry Andric RHSOpName + "' of '" + Rec->getName() +
364ac9a064cSDimitry Andric "' cannot be tied!");
365d8e91e46SDimitry Andric if (SrcOp.first < Ops.NumDefs)
366ac9a064cSDimitry Andric PrintFatalError(Rec->getLoc(), "Output operands '" + LHSOpName + "' and '" +
367ac9a064cSDimitry Andric RHSOpName + "' of '" + Rec->getName() +
368ac9a064cSDimitry Andric "' cannot be tied!");
369cf099d11SDimitry Andric
370d8e91e46SDimitry Andric // The constraint has to go on the operand with higher index, i.e.
371d8e91e46SDimitry Andric // the source one. Check there isn't another constraint there
372d8e91e46SDimitry Andric // already.
373d8e91e46SDimitry Andric if (!Ops[SrcOp.first].Constraints[SrcOp.second].isNone())
374ac9a064cSDimitry Andric PrintFatalError(Rec->getLoc(), "Operand '" + SrcOpName + "' of '" +
375ac9a064cSDimitry Andric Rec->getName() +
376522600a2SDimitry Andric "' cannot have multiple constraints!");
377d8e91e46SDimitry Andric
378d8e91e46SDimitry Andric unsigned DestFlatOpNo = Ops.getFlattenedOperandNumber(DestOp);
379d8e91e46SDimitry Andric auto NewConstraint = CGIOperandList::ConstraintInfo::getTied(DestFlatOpNo);
380d8e91e46SDimitry Andric
381d8e91e46SDimitry Andric // Check that the earlier operand is not the target of another tie
382d8e91e46SDimitry Andric // before making it the target of this one.
383d8e91e46SDimitry Andric for (const CGIOperandList::OperandInfo &Op : Ops) {
384d8e91e46SDimitry Andric for (unsigned i = 0; i < Op.MINumOperands; i++)
385d8e91e46SDimitry Andric if (Op.Constraints[i] == NewConstraint)
386ac9a064cSDimitry Andric PrintFatalError(Rec->getLoc(),
387ac9a064cSDimitry Andric "Operand '" + DestOpName + "' of '" + Rec->getName() +
388d8e91e46SDimitry Andric "' cannot have multiple operands tied to it!");
389cf099d11SDimitry Andric }
390cf099d11SDimitry Andric
391d8e91e46SDimitry Andric Ops[SrcOp.first].Constraints[SrcOp.second] = NewConstraint;
392d8e91e46SDimitry Andric }
393d8e91e46SDimitry Andric
ParseConstraints(StringRef CStr,CGIOperandList & Ops,Record * Rec)394344a3780SDimitry Andric static void ParseConstraints(StringRef CStr, CGIOperandList &Ops, Record *Rec) {
395ac9a064cSDimitry Andric if (CStr.empty())
396ac9a064cSDimitry Andric return;
397cf099d11SDimitry Andric
398344a3780SDimitry Andric StringRef delims(",");
399344a3780SDimitry Andric StringRef::size_type bidx, eidx;
400cf099d11SDimitry Andric
401cf099d11SDimitry Andric bidx = CStr.find_first_not_of(delims);
402344a3780SDimitry Andric while (bidx != StringRef::npos) {
403cf099d11SDimitry Andric eidx = CStr.find_first_of(delims, bidx);
404344a3780SDimitry Andric if (eidx == StringRef::npos)
405344a3780SDimitry Andric eidx = CStr.size();
406cf099d11SDimitry Andric
407d8e91e46SDimitry Andric ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops, Rec);
408cf099d11SDimitry Andric bidx = CStr.find_first_not_of(delims, eidx);
409cf099d11SDimitry Andric }
410cf099d11SDimitry Andric }
411cf099d11SDimitry Andric
ProcessDisableEncoding(StringRef DisableEncoding)412344a3780SDimitry Andric void CGIOperandList::ProcessDisableEncoding(StringRef DisableEncoding) {
4136f8fc217SDimitry Andric while (true) {
414344a3780SDimitry Andric StringRef OpName;
415344a3780SDimitry Andric std::tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
416ac9a064cSDimitry Andric if (OpName.empty())
417ac9a064cSDimitry Andric break;
418cf099d11SDimitry Andric
419cf099d11SDimitry Andric // Figure out which operand this is.
420cf099d11SDimitry Andric std::pair<unsigned, unsigned> Op = ParseOperandName(OpName, false);
421cf099d11SDimitry Andric
422cf099d11SDimitry Andric // Mark the operand as not-to-be encoded.
423cf099d11SDimitry Andric OperandList[Op.first].DoNotEncode[Op.second] = true;
424cf099d11SDimitry Andric }
425cf099d11SDimitry Andric }
426cf099d11SDimitry Andric
427cf099d11SDimitry Andric //===----------------------------------------------------------------------===//
428cf099d11SDimitry Andric // CodeGenInstruction Implementation
429cf099d11SDimitry Andric //===----------------------------------------------------------------------===//
430cf099d11SDimitry Andric
CodeGenInstruction(Record * R)431522600a2SDimitry Andric CodeGenInstruction::CodeGenInstruction(Record *R)
4325ca98fd9SDimitry Andric : TheDef(R), Operands(R), InferredFrom(nullptr) {
433cf099d11SDimitry Andric Namespace = R->getValueAsString("Namespace");
434cfca06d7SDimitry Andric AsmString = std::string(R->getValueAsString("AsmString"));
435cf099d11SDimitry Andric
4361d5ae102SDimitry Andric isPreISelOpcode = R->getValueAsBit("isPreISelOpcode");
437cf099d11SDimitry Andric isReturn = R->getValueAsBit("isReturn");
438d8e91e46SDimitry Andric isEHScopeReturn = R->getValueAsBit("isEHScopeReturn");
439cf099d11SDimitry Andric isBranch = R->getValueAsBit("isBranch");
440cf099d11SDimitry Andric isIndirectBranch = R->getValueAsBit("isIndirectBranch");
441cf099d11SDimitry Andric isCompare = R->getValueAsBit("isCompare");
442cf099d11SDimitry Andric isMoveImm = R->getValueAsBit("isMoveImm");
443eb11fae6SDimitry Andric isMoveReg = R->getValueAsBit("isMoveReg");
4446b943ff3SDimitry Andric isBitcast = R->getValueAsBit("isBitcast");
445902a7b52SDimitry Andric isSelect = R->getValueAsBit("isSelect");
446cf099d11SDimitry Andric isBarrier = R->getValueAsBit("isBarrier");
447cf099d11SDimitry Andric isCall = R->getValueAsBit("isCall");
448b915e9e0SDimitry Andric isAdd = R->getValueAsBit("isAdd");
449eb11fae6SDimitry Andric isTrap = R->getValueAsBit("isTrap");
450cf099d11SDimitry Andric canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
451ac9a064cSDimitry Andric isPredicable = !R->getValueAsBit("isUnpredicable") &&
452ac9a064cSDimitry Andric (Operands.isPredicable || R->getValueAsBit("isPredicable"));
453cf099d11SDimitry Andric isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
454cf099d11SDimitry Andric isCommutable = R->getValueAsBit("isCommutable");
455cf099d11SDimitry Andric isTerminator = R->getValueAsBit("isTerminator");
456cf099d11SDimitry Andric isReMaterializable = R->getValueAsBit("isReMaterializable");
457cf099d11SDimitry Andric hasDelaySlot = R->getValueAsBit("hasDelaySlot");
458cf099d11SDimitry Andric usesCustomInserter = R->getValueAsBit("usesCustomInserter");
45930815c53SDimitry Andric hasPostISelHook = R->getValueAsBit("hasPostISelHook");
460cf099d11SDimitry Andric hasCtrlDep = R->getValueAsBit("hasCtrlDep");
461cf099d11SDimitry Andric isNotDuplicable = R->getValueAsBit("isNotDuplicable");
46267c32a98SDimitry Andric isRegSequence = R->getValueAsBit("isRegSequence");
46367c32a98SDimitry Andric isExtractSubreg = R->getValueAsBit("isExtractSubreg");
46467c32a98SDimitry Andric isInsertSubreg = R->getValueAsBit("isInsertSubreg");
46585d8b2bbSDimitry Andric isConvergent = R->getValueAsBit("isConvergent");
46601095a5dSDimitry Andric hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo");
467eb11fae6SDimitry Andric FastISelShouldIgnore = R->getValueAsBit("FastISelShouldIgnore");
468d8e91e46SDimitry Andric variadicOpsAreDefs = R->getValueAsBit("variadicOpsAreDefs");
469706b4fc4SDimitry Andric isAuthenticated = R->getValueAsBit("isAuthenticated");
470522600a2SDimitry Andric
4715ca98fd9SDimitry Andric bool Unset;
4725ca98fd9SDimitry Andric mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset);
4735ca98fd9SDimitry Andric mayLoad_Unset = Unset;
4745ca98fd9SDimitry Andric mayStore = R->getValueAsBitOrUnset("mayStore", Unset);
4755ca98fd9SDimitry Andric mayStore_Unset = Unset;
476e6d15924SDimitry Andric mayRaiseFPException = R->getValueAsBit("mayRaiseFPException");
4775ca98fd9SDimitry Andric hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset);
4785ca98fd9SDimitry Andric hasSideEffects_Unset = Unset;
479522600a2SDimitry Andric
480cf099d11SDimitry Andric isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
481cf099d11SDimitry Andric hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
482cf099d11SDimitry Andric hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
483411bd29eSDimitry Andric isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
484411bd29eSDimitry Andric isPseudo = R->getValueAsBit("isPseudo");
485145449b1SDimitry Andric isMeta = R->getValueAsBit("isMeta");
486cf099d11SDimitry Andric ImplicitDefs = R->getValueAsListOfDefs("Defs");
487cf099d11SDimitry Andric ImplicitUses = R->getValueAsListOfDefs("Uses");
488cf099d11SDimitry Andric
489eb11fae6SDimitry Andric // This flag is only inferred from the pattern.
490eb11fae6SDimitry Andric hasChain = false;
491eb11fae6SDimitry Andric hasChain_Inferred = false;
492eb11fae6SDimitry Andric
493cf099d11SDimitry Andric // Parse Constraints.
494344a3780SDimitry Andric ParseConstraints(R->getValueAsString("Constraints"), Operands, R);
495cf099d11SDimitry Andric
496cf099d11SDimitry Andric // Parse the DisableEncoding field.
497ac9a064cSDimitry Andric Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
498f8af5cf6SDimitry Andric
499f8af5cf6SDimitry Andric // First check for a ComplexDeprecationPredicate.
500f8af5cf6SDimitry Andric if (R->getValue("ComplexDeprecationPredicate")) {
501f8af5cf6SDimitry Andric HasComplexDeprecationPredicate = true;
502cfca06d7SDimitry Andric DeprecatedReason =
503cfca06d7SDimitry Andric std::string(R->getValueAsString("ComplexDeprecationPredicate"));
504f8af5cf6SDimitry Andric } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) {
505f8af5cf6SDimitry Andric // Check if we have a Subtarget feature mask.
506f8af5cf6SDimitry Andric HasComplexDeprecationPredicate = false;
507f8af5cf6SDimitry Andric DeprecatedReason = Dep->getValue()->getAsString();
508f8af5cf6SDimitry Andric } else {
509f8af5cf6SDimitry Andric // This instruction isn't deprecated.
510f8af5cf6SDimitry Andric HasComplexDeprecationPredicate = false;
511f8af5cf6SDimitry Andric DeprecatedReason = "";
512f8af5cf6SDimitry Andric }
513cf099d11SDimitry Andric }
514104bd817SRoman Divacky
515104bd817SRoman Divacky /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
516104bd817SRoman Divacky /// implicit def and it has a known VT, return the VT, otherwise return
517104bd817SRoman Divacky /// MVT::Other.
HasOneImplicitDefWithKnownVT(const CodeGenTarget & TargetInfo) const518ac9a064cSDimitry Andric MVT::SimpleValueType CodeGenInstruction::HasOneImplicitDefWithKnownVT(
519ac9a064cSDimitry Andric const CodeGenTarget &TargetInfo) const {
520ac9a064cSDimitry Andric if (ImplicitDefs.empty())
521ac9a064cSDimitry Andric return MVT::Other;
522104bd817SRoman Divacky
523104bd817SRoman Divacky // Check to see if the first implicit def has a resolvable type.
524104bd817SRoman Divacky Record *FirstImplicitDef = ImplicitDefs[0];
525104bd817SRoman Divacky assert(FirstImplicitDef->isSubClassOf("Register"));
526044eb2f6SDimitry Andric const std::vector<ValueTypeByHwMode> &RegVTs =
527104bd817SRoman Divacky TargetInfo.getRegisterVTs(FirstImplicitDef);
528044eb2f6SDimitry Andric if (RegVTs.size() == 1 && RegVTs[0].isSimple())
529044eb2f6SDimitry Andric return RegVTs[0].getSimple().SimpleTy;
530104bd817SRoman Divacky return MVT::Other;
531104bd817SRoman Divacky }
532104bd817SRoman Divacky
533cf099d11SDimitry Andric /// FlattenAsmStringVariants - Flatten the specified AsmString to only
534cf099d11SDimitry Andric /// include text from the specified variant, returning the new string.
FlattenAsmStringVariants(StringRef Cur,unsigned Variant)535ac9a064cSDimitry Andric std::string CodeGenInstruction::FlattenAsmStringVariants(StringRef Cur,
536ac9a064cSDimitry Andric unsigned Variant) {
537b60736ecSDimitry Andric std::string Res;
538cf099d11SDimitry Andric
539cf099d11SDimitry Andric for (;;) {
540cf099d11SDimitry Andric // Find the start of the next variant string.
541cf099d11SDimitry Andric size_t VariantsStart = 0;
542cf099d11SDimitry Andric for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
543cf099d11SDimitry Andric if (Cur[VariantsStart] == '{' &&
544ac9a064cSDimitry Andric (VariantsStart == 0 ||
545ac9a064cSDimitry Andric (Cur[VariantsStart - 1] != '$' && Cur[VariantsStart - 1] != '\\')))
546cf099d11SDimitry Andric break;
547cf099d11SDimitry Andric
548cf099d11SDimitry Andric // Add the prefix to the result.
549cf099d11SDimitry Andric Res += Cur.slice(0, VariantsStart);
550cf099d11SDimitry Andric if (VariantsStart == Cur.size())
551cf099d11SDimitry Andric break;
552cf099d11SDimitry Andric
553cf099d11SDimitry Andric ++VariantsStart; // Skip the '{'.
554cf099d11SDimitry Andric
555cf099d11SDimitry Andric // Scan to the end of the variants string.
556cf099d11SDimitry Andric size_t VariantsEnd = VariantsStart;
557cf099d11SDimitry Andric unsigned NestedBraces = 1;
558cf099d11SDimitry Andric for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
559cf099d11SDimitry Andric if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd - 1] != '\\') {
560cf099d11SDimitry Andric if (--NestedBraces == 0)
561cf099d11SDimitry Andric break;
562cf099d11SDimitry Andric } else if (Cur[VariantsEnd] == '{')
563cf099d11SDimitry Andric ++NestedBraces;
564cf099d11SDimitry Andric }
565cf099d11SDimitry Andric
566cf099d11SDimitry Andric // Select the Nth variant (or empty).
567cf099d11SDimitry Andric StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
568cf099d11SDimitry Andric for (unsigned i = 0; i != Variant; ++i)
569cf099d11SDimitry Andric Selection = Selection.split('|').second;
570cf099d11SDimitry Andric Res += Selection.split('|').first;
571cf099d11SDimitry Andric
572cf099d11SDimitry Andric assert(VariantsEnd != Cur.size() &&
573cf099d11SDimitry Andric "Unterminated variants in assembly string!");
574cf099d11SDimitry Andric Cur = Cur.substr(VariantsEnd + 1);
575cf099d11SDimitry Andric }
576cf099d11SDimitry Andric
577cf099d11SDimitry Andric return Res;
578cf099d11SDimitry Andric }
579cf099d11SDimitry Andric
isOperandImpl(StringRef OpListName,unsigned i,StringRef PropertyName) const580e3b55780SDimitry Andric bool CodeGenInstruction::isOperandImpl(StringRef OpListName, unsigned i,
581706b4fc4SDimitry Andric StringRef PropertyName) const {
582e3b55780SDimitry Andric DagInit *ConstraintList = TheDef->getValueAsDag(OpListName);
583706b4fc4SDimitry Andric if (!ConstraintList || i >= ConstraintList->getNumArgs())
584044eb2f6SDimitry Andric return false;
585706b4fc4SDimitry Andric
586706b4fc4SDimitry Andric DefInit *Constraint = dyn_cast<DefInit>(ConstraintList->getArg(i));
587706b4fc4SDimitry Andric if (!Constraint)
588706b4fc4SDimitry Andric return false;
589706b4fc4SDimitry Andric
590706b4fc4SDimitry Andric return Constraint->getDef()->isSubClassOf("TypedOperand") &&
591706b4fc4SDimitry Andric Constraint->getDef()->getValueAsBit(PropertyName);
592044eb2f6SDimitry Andric }
593