blob: 33381e956905a49f3ada5c065a57caa0fc321af2 [file] [log] [blame]
Chris Lattner6cc654b2008-01-06 01:35:39 +00001//===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CodeGenInstruction class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenInstruction.h"
Chris Lattner9414ae52010-03-27 20:09:24 +000015#include "CodeGenTarget.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000016#include "llvm/TableGen/Error.h"
17#include "llvm/TableGen/Record.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattner3f2c8e42010-11-06 07:06:09 +000019#include "llvm/ADT/StringMap.h"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000021#include <set>
22using namespace llvm;
23
Chris Lattnerc240bb02010-11-01 04:03:32 +000024//===----------------------------------------------------------------------===//
25// CGIOperandList Implementation
26//===----------------------------------------------------------------------===//
Jim Grosbach06801722009-12-16 19:43:02 +000027
Chris Lattnerc240bb02010-11-01 04:03:32 +000028CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
29 isPredicable = false;
Chris Lattner6cc654b2008-01-06 01:35:39 +000030 hasOptionalDef = false;
Chris Lattner8f707e12008-01-07 05:19:29 +000031 isVariadic = false;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000032
David Greene05bce0b2011-07-29 22:43:06 +000033 DagInit *OutDI = R->getValueAsDag("OutOperandList");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000034
David Greene05bce0b2011-07-29 22:43:06 +000035 if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000036 if (Init->getDef()->getName() != "outs")
Chris Lattnercedef1c2010-03-18 20:50:52 +000037 throw R->getName() + ": invalid def name for output list: use 'outs'";
38 } else
39 throw R->getName() + ": invalid output list: use 'outs'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000040
Chris Lattnerf55eed22010-03-18 21:07:39 +000041 NumDefs = OutDI->getNumArgs();
Chris Lattnerc240bb02010-11-01 04:03:32 +000042
David Greene05bce0b2011-07-29 22:43:06 +000043 DagInit *InDI = R->getValueAsDag("InOperandList");
44 if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000045 if (Init->getDef()->getName() != "ins")
Chris Lattnercedef1c2010-03-18 20:50:52 +000046 throw R->getName() + ": invalid def name for input list: use 'ins'";
47 } else
48 throw R->getName() + ": invalid input list: use 'ins'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000049
Chris Lattner6cc654b2008-01-06 01:35:39 +000050 unsigned MIOperandNo = 0;
51 std::set<std::string> OperandNames;
Chris Lattnerf55eed22010-03-18 21:07:39 +000052 for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
David Greene05bce0b2011-07-29 22:43:06 +000053 Init *ArgInit;
Chris Lattnerf55eed22010-03-18 21:07:39 +000054 std::string ArgName;
55 if (i < NumDefs) {
56 ArgInit = OutDI->getArg(i);
57 ArgName = OutDI->getArgName(i);
58 } else {
59 ArgInit = InDI->getArg(i-NumDefs);
60 ArgName = InDI->getArgName(i-NumDefs);
61 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000062
David Greene05bce0b2011-07-29 22:43:06 +000063 DefInit *Arg = dynamic_cast<DefInit*>(ArgInit);
Chris Lattner6cc654b2008-01-06 01:35:39 +000064 if (!Arg)
65 throw "Illegal operand for the '" + R->getName() + "' instruction!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000066
Chris Lattner6cc654b2008-01-06 01:35:39 +000067 Record *Rec = Arg->getDef();
68 std::string PrintMethod = "printOperand";
Jim Grosbach5013f742010-10-12 22:21:57 +000069 std::string EncoderMethod;
Benjamin Kramer5196c122011-07-14 21:47:18 +000070 std::string OperandType = "OPERAND_UNKNOWN";
Chris Lattner6cc654b2008-01-06 01:35:39 +000071 unsigned NumOps = 1;
David Greene05bce0b2011-07-29 22:43:06 +000072 DagInit *MIOpInfo = 0;
Owen Andersonbea6f612011-06-27 21:06:21 +000073 if (Rec->isSubClassOf("RegisterOperand")) {
74 PrintMethod = Rec->getValueAsString("PrintMethod");
75 } else if (Rec->isSubClassOf("Operand")) {
Chris Lattner6cc654b2008-01-06 01:35:39 +000076 PrintMethod = Rec->getValueAsString("PrintMethod");
Benjamin Kramer5196c122011-07-14 21:47:18 +000077 OperandType = Rec->getValueAsString("OperandType");
Jim Grosbach5013f742010-10-12 22:21:57 +000078 // If there is an explicit encoder method, use it.
Chris Lattner2ac19022010-11-15 05:19:05 +000079 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattner6cc654b2008-01-06 01:35:39 +000080 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000081
Chris Lattner6cc654b2008-01-06 01:35:39 +000082 // Verify that MIOpInfo has an 'ops' root value.
David Greene05bce0b2011-07-29 22:43:06 +000083 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
84 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
Chris Lattnerc240bb02010-11-01 04:03:32 +000085 ->getDef()->getName() != "ops")
Chris Lattner6cc654b2008-01-06 01:35:39 +000086 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +000087 "'\n";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000088
Chris Lattner6cc654b2008-01-06 01:35:39 +000089 // If we have MIOpInfo, then we have #operands equal to number of entries
90 // in MIOperandInfo.
91 if (unsigned NumArgs = MIOpInfo->getNumArgs())
92 NumOps = NumArgs;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000093
Chris Lattner6cc654b2008-01-06 01:35:39 +000094 if (Rec->isSubClassOf("PredicateOperand"))
95 isPredicable = true;
96 else if (Rec->isSubClassOf("OptionalDefOperand"))
97 hasOptionalDef = true;
98 } else if (Rec->getName() == "variable_ops") {
Chris Lattner8f707e12008-01-07 05:19:29 +000099 isVariadic = true;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000100 continue;
Benjamin Kramer5196c122011-07-14 21:47:18 +0000101 } else if (Rec->isSubClassOf("RegisterClass")) {
102 OperandType = "OPERAND_REGISTER";
103 } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
NAKAMURA Takumi36c3bc42011-01-26 02:03:48 +0000104 Rec->getName() != "unknown")
Chris Lattner6cc654b2008-01-06 01:35:39 +0000105 throw "Unknown operand class '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000106 "' in '" + R->getName() + "' instruction!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000107
Chris Lattner6cc654b2008-01-06 01:35:39 +0000108 // Check that the operand has a name and that it's unique.
Chris Lattnerf55eed22010-03-18 21:07:39 +0000109 if (ArgName.empty())
Chris Lattner6cc654b2008-01-06 01:35:39 +0000110 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000111 " has no name!";
Chris Lattnerf55eed22010-03-18 21:07:39 +0000112 if (!OperandNames.insert(ArgName).second)
Chris Lattner6cc654b2008-01-06 01:35:39 +0000113 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000114 " has the same name as a previous operand!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000115
Jim Grosbach5013f742010-10-12 22:21:57 +0000116 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
Benjamin Kramer5196c122011-07-14 21:47:18 +0000117 OperandType, MIOperandNo, NumOps,
118 MIOpInfo));
Chris Lattner6cc654b2008-01-06 01:35:39 +0000119 MIOperandNo += NumOps;
120 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000121
122
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000123 // Make sure the constraints list for each operand is large enough to hold
124 // constraint info, even if none is present.
125 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
126 OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000127}
128
Chris Lattnerc240bb02010-11-01 04:03:32 +0000129
Chris Lattner6cc654b2008-01-06 01:35:39 +0000130/// getOperandNamed - Return the index of the operand with the specified
131/// non-empty name. If the instruction does not have an operand with the
132/// specified name, throw an exception.
133///
Chris Lattnerc240bb02010-11-01 04:03:32 +0000134unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000135 unsigned OpIdx;
136 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000137 throw "'" + TheDef->getName() + "' does not have an operand named '$" +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000138 Name.str() + "'!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000139}
140
Jim Grosbach01855072010-10-11 18:25:51 +0000141/// hasOperandNamed - Query whether the instruction has an operand of the
142/// given name. If so, return true and set OpIdx to the index of the
143/// operand. Otherwise, return false.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000144bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000145 assert(!Name.empty() && "Cannot search for operand with no name!");
146 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
147 if (OperandList[i].Name == Name) {
148 OpIdx = i;
149 return true;
150 }
151 return false;
152}
153
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000154std::pair<unsigned,unsigned>
Chris Lattnerc240bb02010-11-01 04:03:32 +0000155CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattner6cc654b2008-01-06 01:35:39 +0000156 if (Op.empty() || Op[0] != '$')
157 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000158
Chris Lattner6cc654b2008-01-06 01:35:39 +0000159 std::string OpName = Op.substr(1);
160 std::string SubOpName;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Chris Lattner6cc654b2008-01-06 01:35:39 +0000162 // Check to see if this is $foo.bar.
163 std::string::size_type DotIdx = OpName.find_first_of(".");
164 if (DotIdx != std::string::npos) {
165 SubOpName = OpName.substr(DotIdx+1);
166 if (SubOpName.empty())
167 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
168 OpName = OpName.substr(0, DotIdx);
169 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000170
Chris Lattner6cc654b2008-01-06 01:35:39 +0000171 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000172
Chris Lattner6cc654b2008-01-06 01:35:39 +0000173 if (SubOpName.empty()) { // If no suboperand name was specified:
174 // If one was needed, throw.
175 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
176 SubOpName.empty())
177 throw TheDef->getName() + ": Illegal to refer to"
Chris Lattnerc240bb02010-11-01 04:03:32 +0000178 " whole operand part of complex operand '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000179
Chris Lattner6cc654b2008-01-06 01:35:39 +0000180 // Otherwise, return the operand.
181 return std::make_pair(OpIdx, 0U);
182 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000183
Chris Lattner6cc654b2008-01-06 01:35:39 +0000184 // Find the suboperand number involved.
David Greene05bce0b2011-07-29 22:43:06 +0000185 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000186 if (MIOpInfo == 0)
187 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000188
Chris Lattner6cc654b2008-01-06 01:35:39 +0000189 // Find the operand with the right name.
190 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
191 if (MIOpInfo->getArgName(i) == SubOpName)
192 return std::make_pair(OpIdx, i);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000193
Chris Lattner6cc654b2008-01-06 01:35:39 +0000194 // Otherwise, didn't find it!
195 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
196}
Chris Lattner9414ae52010-03-27 20:09:24 +0000197
Chris Lattnerc240bb02010-11-01 04:03:32 +0000198static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
199 // EARLY_CLOBBER: @early $reg
200 std::string::size_type wpos = CStr.find_first_of(" \t");
201 std::string::size_type start = CStr.find_first_not_of(" \t");
202 std::string Tok = CStr.substr(start, wpos - start);
203 if (Tok == "@earlyclobber") {
204 std::string Name = CStr.substr(wpos+1);
205 wpos = Name.find_first_not_of(" \t");
206 if (wpos == std::string::npos)
207 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
208 Name = Name.substr(wpos);
209 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000210
Chris Lattnerc240bb02010-11-01 04:03:32 +0000211 // Build the string for the operand
212 if (!Ops[Op.first].Constraints[Op.second].isNone())
213 throw "Operand '" + Name + "' cannot have multiple constraints!";
214 Ops[Op.first].Constraints[Op.second] =
215 CGIOperandList::ConstraintInfo::getEarlyClobber();
216 return;
217 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000218
Chris Lattnerc240bb02010-11-01 04:03:32 +0000219 // Only other constraint is "TIED_TO" for now.
220 std::string::size_type pos = CStr.find_first_of('=');
221 assert(pos != std::string::npos && "Unrecognized constraint");
222 start = CStr.find_first_not_of(" \t");
223 std::string Name = CStr.substr(start, pos - start);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000224
Chris Lattnerc240bb02010-11-01 04:03:32 +0000225 // TIED_TO: $src1 = $dst
226 wpos = Name.find_first_of(" \t");
227 if (wpos == std::string::npos)
228 throw "Illegal format for tied-to constraint: '" + CStr + "'";
229 std::string DestOpName = Name.substr(0, wpos);
230 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000231
Chris Lattnerc240bb02010-11-01 04:03:32 +0000232 Name = CStr.substr(pos+1);
233 wpos = Name.find_first_not_of(" \t");
234 if (wpos == std::string::npos)
235 throw "Illegal format for tied-to constraint: '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000236
Chris Lattnerc240bb02010-11-01 04:03:32 +0000237 std::pair<unsigned,unsigned> SrcOp =
238 Ops.ParseOperandName(Name.substr(wpos), false);
239 if (SrcOp > DestOp)
240 throw "Illegal tied-to operand constraint '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000241
242
Chris Lattnerc240bb02010-11-01 04:03:32 +0000243 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000244
Chris Lattnerc240bb02010-11-01 04:03:32 +0000245 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
246 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
247 Ops[DestOp.first].Constraints[DestOp.second] =
Jim Grosbach2a8cd572011-11-15 01:05:12 +0000248 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000249}
250
251static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000252 if (CStr.empty()) return;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000253
Chris Lattnerc240bb02010-11-01 04:03:32 +0000254 const std::string delims(",");
255 std::string::size_type bidx, eidx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000256
Chris Lattnerc240bb02010-11-01 04:03:32 +0000257 bidx = CStr.find_first_not_of(delims);
258 while (bidx != std::string::npos) {
259 eidx = CStr.find_first_of(delims, bidx);
260 if (eidx == std::string::npos)
261 eidx = CStr.length();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000262
Chris Lattnerc240bb02010-11-01 04:03:32 +0000263 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
264 bidx = CStr.find_first_not_of(delims, eidx);
265 }
266}
267
268void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
269 while (1) {
Chris Lattnerc30a38f2011-07-21 06:21:31 +0000270 std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
271 std::string OpName = P.first;
272 DisableEncoding = P.second;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000273 if (OpName.empty()) break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000274
Chris Lattnerc240bb02010-11-01 04:03:32 +0000275 // Figure out which operand this is.
276 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000277
Chris Lattnerc240bb02010-11-01 04:03:32 +0000278 // Mark the operand as not-to-be encoded.
279 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
280 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
281 OperandList[Op.first].DoNotEncode[Op.second] = true;
282 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000283
Chris Lattnerc240bb02010-11-01 04:03:32 +0000284}
285
286//===----------------------------------------------------------------------===//
287// CodeGenInstruction Implementation
288//===----------------------------------------------------------------------===//
289
290CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R), Operands(R) {
291 Namespace = R->getValueAsString("Namespace");
292 AsmString = R->getValueAsString("AsmString");
293
294 isReturn = R->getValueAsBit("isReturn");
295 isBranch = R->getValueAsBit("isBranch");
296 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
297 isCompare = R->getValueAsBit("isCompare");
Evan Chengc4af4632010-11-17 20:13:28 +0000298 isMoveImm = R->getValueAsBit("isMoveImm");
Evan Cheng0f040a22011-03-15 05:09:26 +0000299 isBitcast = R->getValueAsBit("isBitcast");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000300 isBarrier = R->getValueAsBit("isBarrier");
301 isCall = R->getValueAsBit("isCall");
302 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
303 mayLoad = R->getValueAsBit("mayLoad");
304 mayStore = R->getValueAsBit("mayStore");
305 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
306 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
307 isCommutable = R->getValueAsBit("isCommutable");
308 isTerminator = R->getValueAsBit("isTerminator");
309 isReMaterializable = R->getValueAsBit("isReMaterializable");
310 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
311 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Andrew Trick83a80312011-09-20 18:22:31 +0000312 hasPostISelHook = R->getValueAsBit("hasPostISelHook");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000313 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
314 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
315 hasSideEffects = R->getValueAsBit("hasSideEffects");
316 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
317 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
318 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
319 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Jim Grosbache727d672011-07-07 00:48:02 +0000320 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
Jim Grosbach806fcc02011-07-06 21:33:38 +0000321 isPseudo = R->getValueAsBit("isPseudo");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000322 ImplicitDefs = R->getValueAsListOfDefs("Defs");
323 ImplicitUses = R->getValueAsListOfDefs("Uses");
324
325 if (neverHasSideEffects + hasSideEffects > 1)
326 throw R->getName() + ": multiple conflicting side-effect flags set!";
327
328 // Parse Constraints.
329 ParseConstraints(R->getValueAsString("Constraints"), Operands);
330
331 // Parse the DisableEncoding field.
332 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
333}
Chris Lattner9414ae52010-03-27 20:09:24 +0000334
335/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
336/// implicit def and it has a known VT, return the VT, otherwise return
337/// MVT::Other.
338MVT::SimpleValueType CodeGenInstruction::
339HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
340 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000341
Chris Lattner9414ae52010-03-27 20:09:24 +0000342 // Check to see if the first implicit def has a resolvable type.
343 Record *FirstImplicitDef = ImplicitDefs[0];
344 assert(FirstImplicitDef->isSubClassOf("Register"));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000345 const std::vector<MVT::SimpleValueType> &RegVTs =
Chris Lattner9414ae52010-03-27 20:09:24 +0000346 TargetInfo.getRegisterVTs(FirstImplicitDef);
347 if (RegVTs.size() == 1)
348 return RegVTs[0];
349 return MVT::Other;
350}
351
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000352
353/// FlattenAsmStringVariants - Flatten the specified AsmString to only
354/// include text from the specified variant, returning the new string.
355std::string CodeGenInstruction::
356FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
357 std::string Res = "";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000359 for (;;) {
360 // Find the start of the next variant string.
361 size_t VariantsStart = 0;
362 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
363 if (Cur[VariantsStart] == '{' &&
364 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
365 Cur[VariantsStart-1] != '\\')))
366 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000367
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000368 // Add the prefix to the result.
369 Res += Cur.slice(0, VariantsStart);
370 if (VariantsStart == Cur.size())
371 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000372
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000373 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000374
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000375 // Scan to the end of the variants string.
376 size_t VariantsEnd = VariantsStart;
377 unsigned NestedBraces = 1;
378 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
379 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
380 if (--NestedBraces == 0)
381 break;
382 } else if (Cur[VariantsEnd] == '{')
383 ++NestedBraces;
384 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000385
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000386 // Select the Nth variant (or empty).
387 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
388 for (unsigned i = 0; i != Variant; ++i)
389 Selection = Selection.split('|').second;
390 Res += Selection.split('|').first;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000391
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000392 assert(VariantsEnd != Cur.size() &&
393 "Unterminated variants in assembly string!");
394 Cur = Cur.substr(VariantsEnd + 1);
395 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000396
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000397 return Res;
398}
399
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000400
401//===----------------------------------------------------------------------===//
402/// CodeGenInstAlias Implementation
403//===----------------------------------------------------------------------===//
404
Bob Wilsona49c7df2011-01-26 19:44:55 +0000405/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
406/// constructor. It checks if an argument in an InstAlias pattern matches
407/// the corresponding operand of the instruction. It returns true on a
408/// successful match, with ResOp set to the result operand to be used.
David Greene05bce0b2011-07-29 22:43:06 +0000409bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000410 Record *InstOpRec, bool hasSubOps,
411 SMLoc Loc, CodeGenTarget &T,
412 ResultOperand &ResOp) {
David Greene05bce0b2011-07-29 22:43:06 +0000413 Init *Arg = Result->getArg(AliasOpNo);
414 DefInit *ADI = dynamic_cast<DefInit*>(Arg);
Bob Wilsona49c7df2011-01-26 19:44:55 +0000415
416 if (ADI && ADI->getDef() == InstOpRec) {
417 // If the operand is a record, it must have a name, and the record type
418 // must match up with the instruction's argument type.
419 if (Result->getArgName(AliasOpNo).empty())
420 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
421 " must have a name!");
422 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
423 return true;
424 }
425
Jim Grosbachbe5d6bc2011-10-28 16:43:40 +0000426 // For register operands, the source register class can be a subclass
427 // of the instruction register class, not just an exact match.
428 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
429 if (!InstOpRec->isSubClassOf("RegisterClass"))
430 return false;
Jim Grosbach48c1f842011-10-28 22:32:53 +0000431 if (!T.getRegisterClass(InstOpRec)
432 .hasSubClass(&T.getRegisterClass(ADI->getDef())))
433 return false;
434 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
435 return true;
Jim Grosbachbe5d6bc2011-10-28 16:43:40 +0000436 }
437
Bob Wilsona49c7df2011-01-26 19:44:55 +0000438 // Handle explicit registers.
439 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
Jim Grosbachc68e9272011-08-19 20:33:06 +0000440 if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
441 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
442 // The operand info should only have a single (register) entry. We
443 // want the register class of it.
444 InstOpRec = dynamic_cast<DefInit*>(DI->getArg(0))->getDef();
445 }
446
Owen Andersonbea6f612011-06-27 21:06:21 +0000447 if (InstOpRec->isSubClassOf("RegisterOperand"))
448 InstOpRec = InstOpRec->getValueAsDef("RegClass");
449
Bob Wilsona49c7df2011-01-26 19:44:55 +0000450 if (!InstOpRec->isSubClassOf("RegisterClass"))
451 return false;
452
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000453 if (!T.getRegisterClass(InstOpRec)
454 .contains(T.getRegBank().getReg(ADI->getDef())))
Jim Grosbachf2764c82011-08-19 19:53:51 +0000455 throw TGError(Loc, "fixed register " + ADI->getDef()->getName() +
456 " is not a member of the " + InstOpRec->getName() +
Bob Wilsona49c7df2011-01-26 19:44:55 +0000457 " register class!");
458
459 if (!Result->getArgName(AliasOpNo).empty())
460 throw TGError(Loc, "result fixed register argument must "
461 "not have a name!");
462
463 ResOp = ResultOperand(ADI->getDef());
464 return true;
465 }
466
467 // Handle "zero_reg" for optional def operands.
468 if (ADI && ADI->getDef()->getName() == "zero_reg") {
469
470 // Check if this is an optional def.
Jim Grosbachbfc94292011-11-15 01:46:57 +0000471 // Tied operands where the source is a sub-operand of a complex operand
472 // need to represent both operands in the alias destination instruction.
473 // Allow zero_reg for the tied portion. This can and should go away once
474 // the MC representation of things doesn't use tied operands at all.
475 //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
476 // throw TGError(Loc, "reg0 used for result that is not an "
477 // "OptionalDefOperand!");
Bob Wilsona49c7df2011-01-26 19:44:55 +0000478
479 ResOp = ResultOperand(static_cast<Record*>(0));
480 return true;
481 }
482
Jim Grosbach48c1f842011-10-28 22:32:53 +0000483 // Literal integers.
David Greene05bce0b2011-07-29 22:43:06 +0000484 if (IntInit *II = dynamic_cast<IntInit*>(Arg)) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000485 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
486 return false;
487 // Integer arguments can't have names.
488 if (!Result->getArgName(AliasOpNo).empty())
489 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
490 " must not have a name!");
491 ResOp = ResultOperand(II->getValue());
492 return true;
493 }
494
Jim Grosbach48c1f842011-10-28 22:32:53 +0000495 // If both are Operands with the same MVT, allow the conversion. It's
496 // up to the user to make sure the values are appropriate, just like
497 // for isel Pat's.
498 if (InstOpRec->isSubClassOf("Operand") &&
499 ADI->getDef()->isSubClassOf("Operand")) {
500 // FIXME: What other attributes should we check here? Identical
501 // MIOperandInfo perhaps?
502 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
503 return false;
504 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
505 return true;
506 }
507
Bob Wilsona49c7df2011-01-26 19:44:55 +0000508 return false;
509}
510
Chris Lattner662e5a32010-11-06 07:14:44 +0000511CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000512 AsmString = R->getValueAsString("AsmString");
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000513 Result = R->getValueAsDag("ResultInst");
Chris Lattner225549f2010-11-06 06:39:47 +0000514
515 // Verify that the root of the result is an instruction.
David Greene05bce0b2011-07-29 22:43:06 +0000516 DefInit *DI = dynamic_cast<DefInit*>(Result->getOperator());
Chris Lattner225549f2010-11-06 06:39:47 +0000517 if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction"))
518 throw TGError(R->getLoc(), "result of inst alias should be an instruction");
519
520 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000521
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000522 // NameClass - If argument names are repeated, we need to verify they have
523 // the same class.
524 StringMap<Record*> NameClass;
Bob Wilson55931ab2011-01-20 18:38:10 +0000525 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +0000526 DefInit *ADI = dynamic_cast<DefInit*>(Result->getArg(i));
Bob Wilson55931ab2011-01-20 18:38:10 +0000527 if (!ADI || Result->getArgName(i).empty())
528 continue;
529 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
530 // $foo can exist multiple times in the result list, but it must have the
531 // same type.
532 Record *&Entry = NameClass[Result->getArgName(i)];
533 if (Entry && Entry != ADI->getDef())
534 throw TGError(R->getLoc(), "result value $" + Result->getArgName(i) +
535 " is both " + Entry->getName() + " and " +
536 ADI->getDef()->getName() + "!");
537 Entry = ADI->getDef();
538 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000539
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000540 // Decode and validate the arguments of the result.
Chris Lattner41409852010-11-06 07:31:43 +0000541 unsigned AliasOpNo = 0;
542 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000543
Jim Grosbachbfc94292011-11-15 01:46:57 +0000544 // Tied registers don't have an entry in the result dag unless they're part
545 // of a complex operand, in which case we include them anyways, as we
546 // don't have any other way to specify the whole operand.
547 if (ResultInst->Operands[i].MINumOperands == 1 &&
548 ResultInst->Operands[i].getTiedRegister() != -1)
Chris Lattner41409852010-11-06 07:31:43 +0000549 continue;
550
551 if (AliasOpNo >= Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000552 throw TGError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000553
Bob Wilsona49c7df2011-01-26 19:44:55 +0000554 Record *InstOpRec = ResultInst->Operands[i].Rec;
555 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
556 ResultOperand ResOp(static_cast<int64_t>(0));
557 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
558 R->getLoc(), T, ResOp)) {
Owen Anderson7e8921b2012-06-08 00:25:03 +0000559 // If this is a simple operand, or a complex operand with a custom match
560 // class, then we can match is verbatim.
561 if (NumSubOps == 1 ||
562 (InstOpRec->getValue("ParserMatchClass") &&
563 InstOpRec->getValueAsDef("ParserMatchClass")
564 ->getValueAsString("Name") != "Imm")) {
565 ResultOperands.push_back(ResOp);
566 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
567 ++AliasOpNo;
568
569 // Otherwise, we need to match each of the suboperands individually.
570 } else {
571 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
572 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
573 Record *SubRec = dynamic_cast<DefInit*>(MIOI->getArg(SubOp))->getDef();
574
575 // Take care to instantiate each of the suboperands with the correct
576 // nomenclature: $foo.bar
577 ResultOperands.push_back(
578 ResultOperand(Result->getArgName(AliasOpNo) + "." +
579 MIOI->getArgName(SubOp), SubRec));
580 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
581 }
582 ++AliasOpNo;
583 }
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000584 continue;
585 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000586
Bob Wilsona49c7df2011-01-26 19:44:55 +0000587 // If the argument did not match the instruction operand, and the operand
588 // is composed of multiple suboperands, try matching the suboperands.
589 if (NumSubOps > 1) {
David Greene05bce0b2011-07-29 22:43:06 +0000590 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000591 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
592 if (AliasOpNo >= Result->getNumArgs())
593 throw TGError(R->getLoc(), "not enough arguments for instruction!");
David Greene05bce0b2011-07-29 22:43:06 +0000594 Record *SubRec = dynamic_cast<DefInit*>(MIOI->getArg(SubOp))->getDef();
Bob Wilsona49c7df2011-01-26 19:44:55 +0000595 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
596 R->getLoc(), T, ResOp)) {
597 ResultOperands.push_back(ResOp);
598 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
599 ++AliasOpNo;
600 } else {
601 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
602 " does not match instruction operand class " +
603 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
604 }
605 }
Chris Lattner98c870f2010-11-06 19:25:43 +0000606 continue;
607 }
Bob Wilsona49c7df2011-01-26 19:44:55 +0000608 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
609 " does not match instruction operand class " +
610 InstOpRec->getName());
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000611 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000612
Chris Lattner41409852010-11-06 07:31:43 +0000613 if (AliasOpNo != Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000614 throw TGError(R->getLoc(), "too many operands for instruction!");
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000615}