blob: 99d2f173a87cbf74f94b1fef6d0f81c15487e430 [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
Sean Silva6cfc8062012-10-10 20:24:43 +000035 if (DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000036 if (Init->getDef()->getName() != "outs")
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");
Sean Silva6cfc8062012-10-10 20:24:43 +000044 if (DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000045 if (Init->getDef()->getName() != "ins")
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
Sean Silva6cfc8062012-10-10 20:24:43 +000063 DefInit *Arg = dyn_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.
Sean Silva3f7b7f82012-10-10 20:24:47 +000083 if (!isa<DefInit>(MIOpInfo->getOperator()) ||
84 cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
Chris Lattner6cc654b2008-01-06 01:35:39 +000085 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +000086 "'\n";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000087
Chris Lattner6cc654b2008-01-06 01:35:39 +000088 // If we have MIOpInfo, then we have #operands equal to number of entries
89 // in MIOperandInfo.
90 if (unsigned NumArgs = MIOpInfo->getNumArgs())
91 NumOps = NumArgs;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000092
Chris Lattner6cc654b2008-01-06 01:35:39 +000093 if (Rec->isSubClassOf("PredicateOperand"))
94 isPredicable = true;
95 else if (Rec->isSubClassOf("OptionalDefOperand"))
96 hasOptionalDef = true;
97 } else if (Rec->getName() == "variable_ops") {
Chris Lattner8f707e12008-01-07 05:19:29 +000098 isVariadic = true;
Chris Lattner6cc654b2008-01-06 01:35:39 +000099 continue;
Benjamin Kramer5196c122011-07-14 21:47:18 +0000100 } else if (Rec->isSubClassOf("RegisterClass")) {
101 OperandType = "OPERAND_REGISTER";
102 } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
Owen Anderson83c0eef2012-09-11 23:47:08 +0000103 !Rec->isSubClassOf("unknown_class"))
Chris Lattner6cc654b2008-01-06 01:35:39 +0000104 throw "Unknown operand class '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000105 "' in '" + R->getName() + "' instruction!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000106
Chris Lattner6cc654b2008-01-06 01:35:39 +0000107 // Check that the operand has a name and that it's unique.
Chris Lattnerf55eed22010-03-18 21:07:39 +0000108 if (ArgName.empty())
Chris Lattner6cc654b2008-01-06 01:35:39 +0000109 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000110 " has no name!";
Chris Lattnerf55eed22010-03-18 21:07:39 +0000111 if (!OperandNames.insert(ArgName).second)
Chris Lattner6cc654b2008-01-06 01:35:39 +0000112 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000113 " has the same name as a previous operand!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000114
Jim Grosbach5013f742010-10-12 22:21:57 +0000115 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
Benjamin Kramer5196c122011-07-14 21:47:18 +0000116 OperandType, MIOperandNo, NumOps,
117 MIOpInfo));
Chris Lattner6cc654b2008-01-06 01:35:39 +0000118 MIOperandNo += NumOps;
119 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000120
121
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000122 // Make sure the constraints list for each operand is large enough to hold
123 // constraint info, even if none is present.
124 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
125 OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000126}
127
Chris Lattnerc240bb02010-11-01 04:03:32 +0000128
Chris Lattner6cc654b2008-01-06 01:35:39 +0000129/// getOperandNamed - Return the index of the operand with the specified
130/// non-empty name. If the instruction does not have an operand with the
131/// specified name, throw an exception.
132///
Chris Lattnerc240bb02010-11-01 04:03:32 +0000133unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000134 unsigned OpIdx;
135 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000136 throw "'" + TheDef->getName() + "' does not have an operand named '$" +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000137 Name.str() + "'!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000138}
139
Jim Grosbach01855072010-10-11 18:25:51 +0000140/// hasOperandNamed - Query whether the instruction has an operand of the
141/// given name. If so, return true and set OpIdx to the index of the
142/// operand. Otherwise, return false.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000143bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000144 assert(!Name.empty() && "Cannot search for operand with no name!");
145 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
146 if (OperandList[i].Name == Name) {
147 OpIdx = i;
148 return true;
149 }
150 return false;
151}
152
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000153std::pair<unsigned,unsigned>
Chris Lattnerc240bb02010-11-01 04:03:32 +0000154CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattner6cc654b2008-01-06 01:35:39 +0000155 if (Op.empty() || Op[0] != '$')
156 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000157
Chris Lattner6cc654b2008-01-06 01:35:39 +0000158 std::string OpName = Op.substr(1);
159 std::string SubOpName;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000160
Chris Lattner6cc654b2008-01-06 01:35:39 +0000161 // Check to see if this is $foo.bar.
162 std::string::size_type DotIdx = OpName.find_first_of(".");
163 if (DotIdx != std::string::npos) {
164 SubOpName = OpName.substr(DotIdx+1);
165 if (SubOpName.empty())
166 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
167 OpName = OpName.substr(0, DotIdx);
168 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000169
Chris Lattner6cc654b2008-01-06 01:35:39 +0000170 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000171
Chris Lattner6cc654b2008-01-06 01:35:39 +0000172 if (SubOpName.empty()) { // If no suboperand name was specified:
173 // If one was needed, throw.
174 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
175 SubOpName.empty())
176 throw TheDef->getName() + ": Illegal to refer to"
Chris Lattnerc240bb02010-11-01 04:03:32 +0000177 " whole operand part of complex operand '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000178
Chris Lattner6cc654b2008-01-06 01:35:39 +0000179 // Otherwise, return the operand.
180 return std::make_pair(OpIdx, 0U);
181 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000182
Chris Lattner6cc654b2008-01-06 01:35:39 +0000183 // Find the suboperand number involved.
David Greene05bce0b2011-07-29 22:43:06 +0000184 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000185 if (MIOpInfo == 0)
186 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000187
Chris Lattner6cc654b2008-01-06 01:35:39 +0000188 // Find the operand with the right name.
189 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
190 if (MIOpInfo->getArgName(i) == SubOpName)
191 return std::make_pair(OpIdx, i);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000192
Chris Lattner6cc654b2008-01-06 01:35:39 +0000193 // Otherwise, didn't find it!
194 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
195}
Chris Lattner9414ae52010-03-27 20:09:24 +0000196
Chris Lattnerc240bb02010-11-01 04:03:32 +0000197static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
198 // EARLY_CLOBBER: @early $reg
199 std::string::size_type wpos = CStr.find_first_of(" \t");
200 std::string::size_type start = CStr.find_first_not_of(" \t");
201 std::string Tok = CStr.substr(start, wpos - start);
202 if (Tok == "@earlyclobber") {
203 std::string Name = CStr.substr(wpos+1);
204 wpos = Name.find_first_not_of(" \t");
205 if (wpos == std::string::npos)
206 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
207 Name = Name.substr(wpos);
208 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000209
Chris Lattnerc240bb02010-11-01 04:03:32 +0000210 // Build the string for the operand
211 if (!Ops[Op.first].Constraints[Op.second].isNone())
212 throw "Operand '" + Name + "' cannot have multiple constraints!";
213 Ops[Op.first].Constraints[Op.second] =
214 CGIOperandList::ConstraintInfo::getEarlyClobber();
215 return;
216 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000217
Chris Lattnerc240bb02010-11-01 04:03:32 +0000218 // Only other constraint is "TIED_TO" for now.
219 std::string::size_type pos = CStr.find_first_of('=');
220 assert(pos != std::string::npos && "Unrecognized constraint");
221 start = CStr.find_first_not_of(" \t");
222 std::string Name = CStr.substr(start, pos - start);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000223
Chris Lattnerc240bb02010-11-01 04:03:32 +0000224 // TIED_TO: $src1 = $dst
225 wpos = Name.find_first_of(" \t");
226 if (wpos == std::string::npos)
227 throw "Illegal format for tied-to constraint: '" + CStr + "'";
228 std::string DestOpName = Name.substr(0, wpos);
229 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000230
Chris Lattnerc240bb02010-11-01 04:03:32 +0000231 Name = CStr.substr(pos+1);
232 wpos = Name.find_first_not_of(" \t");
233 if (wpos == std::string::npos)
234 throw "Illegal format for tied-to constraint: '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000235
Chris Lattnerc240bb02010-11-01 04:03:32 +0000236 std::pair<unsigned,unsigned> SrcOp =
237 Ops.ParseOperandName(Name.substr(wpos), false);
238 if (SrcOp > DestOp)
239 throw "Illegal tied-to operand constraint '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000240
241
Chris Lattnerc240bb02010-11-01 04:03:32 +0000242 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000243
Chris Lattnerc240bb02010-11-01 04:03:32 +0000244 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
245 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
246 Ops[DestOp.first].Constraints[DestOp.second] =
Jim Grosbach2a8cd572011-11-15 01:05:12 +0000247 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000248}
249
250static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000251 if (CStr.empty()) return;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000252
Chris Lattnerc240bb02010-11-01 04:03:32 +0000253 const std::string delims(",");
254 std::string::size_type bidx, eidx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000255
Chris Lattnerc240bb02010-11-01 04:03:32 +0000256 bidx = CStr.find_first_not_of(delims);
257 while (bidx != std::string::npos) {
258 eidx = CStr.find_first_of(delims, bidx);
259 if (eidx == std::string::npos)
260 eidx = CStr.length();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000261
Chris Lattnerc240bb02010-11-01 04:03:32 +0000262 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
263 bidx = CStr.find_first_not_of(delims, eidx);
264 }
265}
266
267void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
268 while (1) {
Chris Lattnerc30a38f2011-07-21 06:21:31 +0000269 std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
270 std::string OpName = P.first;
271 DisableEncoding = P.second;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000272 if (OpName.empty()) break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000273
Chris Lattnerc240bb02010-11-01 04:03:32 +0000274 // Figure out which operand this is.
275 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000276
Chris Lattnerc240bb02010-11-01 04:03:32 +0000277 // Mark the operand as not-to-be encoded.
278 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
279 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
280 OperandList[Op.first].DoNotEncode[Op.second] = true;
281 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000282
Chris Lattnerc240bb02010-11-01 04:03:32 +0000283}
284
285//===----------------------------------------------------------------------===//
286// CodeGenInstruction Implementation
287//===----------------------------------------------------------------------===//
288
Jakob Stoklund Olesen912519a2012-08-24 00:31:16 +0000289CodeGenInstruction::CodeGenInstruction(Record *R)
290 : TheDef(R), Operands(R), InferredFrom(0) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000291 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");
Jakob Stoklund Olesenf2c64ef2012-08-16 23:11:47 +0000300 isSelect = R->getValueAsBit("isSelect");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000301 isBarrier = R->getValueAsBit("isBarrier");
302 isCall = R->getValueAsBit("isCall");
303 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000304 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
305 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
306 isCommutable = R->getValueAsBit("isCommutable");
307 isTerminator = R->getValueAsBit("isTerminator");
308 isReMaterializable = R->getValueAsBit("isReMaterializable");
309 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
310 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Andrew Trick83a80312011-09-20 18:22:31 +0000311 hasPostISelHook = R->getValueAsBit("hasPostISelHook");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000312 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
313 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Jakob Stoklund Olesenc1f10fd2012-08-23 19:34:46 +0000314
315 mayLoad = R->getValueAsBitOrUnset("mayLoad", mayLoad_Unset);
316 mayStore = R->getValueAsBitOrUnset("mayStore", mayStore_Unset);
317 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects",
318 hasSideEffects_Unset);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000319 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
Jakob Stoklund Olesenc1f10fd2012-08-23 19:34:46 +0000320
Chris Lattnerc240bb02010-11-01 04:03:32 +0000321 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
322 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
323 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Jim Grosbache727d672011-07-07 00:48:02 +0000324 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
Jim Grosbach806fcc02011-07-06 21:33:38 +0000325 isPseudo = R->getValueAsBit("isPseudo");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000326 ImplicitDefs = R->getValueAsListOfDefs("Defs");
327 ImplicitUses = R->getValueAsListOfDefs("Uses");
328
329 if (neverHasSideEffects + hasSideEffects > 1)
330 throw R->getName() + ": multiple conflicting side-effect flags set!";
331
332 // Parse Constraints.
333 ParseConstraints(R->getValueAsString("Constraints"), Operands);
334
335 // Parse the DisableEncoding field.
336 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
337}
Chris Lattner9414ae52010-03-27 20:09:24 +0000338
339/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
340/// implicit def and it has a known VT, return the VT, otherwise return
341/// MVT::Other.
342MVT::SimpleValueType CodeGenInstruction::
343HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
344 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000345
Chris Lattner9414ae52010-03-27 20:09:24 +0000346 // Check to see if the first implicit def has a resolvable type.
347 Record *FirstImplicitDef = ImplicitDefs[0];
348 assert(FirstImplicitDef->isSubClassOf("Register"));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000349 const std::vector<MVT::SimpleValueType> &RegVTs =
Chris Lattner9414ae52010-03-27 20:09:24 +0000350 TargetInfo.getRegisterVTs(FirstImplicitDef);
351 if (RegVTs.size() == 1)
352 return RegVTs[0];
353 return MVT::Other;
354}
355
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000356
357/// FlattenAsmStringVariants - Flatten the specified AsmString to only
358/// include text from the specified variant, returning the new string.
359std::string CodeGenInstruction::
360FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
361 std::string Res = "";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000362
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000363 for (;;) {
364 // Find the start of the next variant string.
365 size_t VariantsStart = 0;
366 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
367 if (Cur[VariantsStart] == '{' &&
368 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
369 Cur[VariantsStart-1] != '\\')))
370 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000371
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000372 // Add the prefix to the result.
373 Res += Cur.slice(0, VariantsStart);
374 if (VariantsStart == Cur.size())
375 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000376
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000377 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000378
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000379 // Scan to the end of the variants string.
380 size_t VariantsEnd = VariantsStart;
381 unsigned NestedBraces = 1;
382 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
383 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
384 if (--NestedBraces == 0)
385 break;
386 } else if (Cur[VariantsEnd] == '{')
387 ++NestedBraces;
388 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000389
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000390 // Select the Nth variant (or empty).
391 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
392 for (unsigned i = 0; i != Variant; ++i)
393 Selection = Selection.split('|').second;
394 Res += Selection.split('|').first;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000395
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000396 assert(VariantsEnd != Cur.size() &&
397 "Unterminated variants in assembly string!");
398 Cur = Cur.substr(VariantsEnd + 1);
399 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000400
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000401 return Res;
402}
403
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000404
405//===----------------------------------------------------------------------===//
406/// CodeGenInstAlias Implementation
407//===----------------------------------------------------------------------===//
408
Bob Wilsona49c7df2011-01-26 19:44:55 +0000409/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
410/// constructor. It checks if an argument in an InstAlias pattern matches
411/// the corresponding operand of the instruction. It returns true on a
412/// successful match, with ResOp set to the result operand to be used.
David Greene05bce0b2011-07-29 22:43:06 +0000413bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000414 Record *InstOpRec, bool hasSubOps,
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +0000415 ArrayRef<SMLoc> Loc, CodeGenTarget &T,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000416 ResultOperand &ResOp) {
David Greene05bce0b2011-07-29 22:43:06 +0000417 Init *Arg = Result->getArg(AliasOpNo);
Sean Silva6cfc8062012-10-10 20:24:43 +0000418 DefInit *ADI = dyn_cast<DefInit>(Arg);
Bob Wilsona49c7df2011-01-26 19:44:55 +0000419
420 if (ADI && ADI->getDef() == InstOpRec) {
421 // If the operand is a record, it must have a name, and the record type
422 // must match up with the instruction's argument type.
423 if (Result->getArgName(AliasOpNo).empty())
424 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
425 " must have a name!");
426 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
427 return true;
428 }
429
Jim Grosbachbe5d6bc2011-10-28 16:43:40 +0000430 // For register operands, the source register class can be a subclass
431 // of the instruction register class, not just an exact match.
432 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
433 if (!InstOpRec->isSubClassOf("RegisterClass"))
434 return false;
Jim Grosbach48c1f842011-10-28 22:32:53 +0000435 if (!T.getRegisterClass(InstOpRec)
436 .hasSubClass(&T.getRegisterClass(ADI->getDef())))
437 return false;
438 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
439 return true;
Jim Grosbachbe5d6bc2011-10-28 16:43:40 +0000440 }
441
Bob Wilsona49c7df2011-01-26 19:44:55 +0000442 // Handle explicit registers.
443 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
Jim Grosbachc68e9272011-08-19 20:33:06 +0000444 if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
445 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
446 // The operand info should only have a single (register) entry. We
447 // want the register class of it.
Sean Silva3f7b7f82012-10-10 20:24:47 +0000448 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
Jim Grosbachc68e9272011-08-19 20:33:06 +0000449 }
450
Owen Andersonbea6f612011-06-27 21:06:21 +0000451 if (InstOpRec->isSubClassOf("RegisterOperand"))
452 InstOpRec = InstOpRec->getValueAsDef("RegClass");
453
Bob Wilsona49c7df2011-01-26 19:44:55 +0000454 if (!InstOpRec->isSubClassOf("RegisterClass"))
455 return false;
456
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000457 if (!T.getRegisterClass(InstOpRec)
458 .contains(T.getRegBank().getReg(ADI->getDef())))
Jim Grosbachf2764c82011-08-19 19:53:51 +0000459 throw TGError(Loc, "fixed register " + ADI->getDef()->getName() +
460 " is not a member of the " + InstOpRec->getName() +
Bob Wilsona49c7df2011-01-26 19:44:55 +0000461 " register class!");
462
463 if (!Result->getArgName(AliasOpNo).empty())
464 throw TGError(Loc, "result fixed register argument must "
465 "not have a name!");
466
467 ResOp = ResultOperand(ADI->getDef());
468 return true;
469 }
470
471 // Handle "zero_reg" for optional def operands.
472 if (ADI && ADI->getDef()->getName() == "zero_reg") {
473
474 // Check if this is an optional def.
Jim Grosbachbfc94292011-11-15 01:46:57 +0000475 // Tied operands where the source is a sub-operand of a complex operand
476 // need to represent both operands in the alias destination instruction.
477 // Allow zero_reg for the tied portion. This can and should go away once
478 // the MC representation of things doesn't use tied operands at all.
479 //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
480 // throw TGError(Loc, "reg0 used for result that is not an "
481 // "OptionalDefOperand!");
Bob Wilsona49c7df2011-01-26 19:44:55 +0000482
483 ResOp = ResultOperand(static_cast<Record*>(0));
484 return true;
485 }
486
Jim Grosbach48c1f842011-10-28 22:32:53 +0000487 // Literal integers.
Sean Silva6cfc8062012-10-10 20:24:43 +0000488 if (IntInit *II = dyn_cast<IntInit>(Arg)) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000489 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
490 return false;
491 // Integer arguments can't have names.
492 if (!Result->getArgName(AliasOpNo).empty())
493 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
494 " must not have a name!");
495 ResOp = ResultOperand(II->getValue());
496 return true;
497 }
498
Jim Grosbach48c1f842011-10-28 22:32:53 +0000499 // If both are Operands with the same MVT, allow the conversion. It's
500 // up to the user to make sure the values are appropriate, just like
501 // for isel Pat's.
502 if (InstOpRec->isSubClassOf("Operand") &&
503 ADI->getDef()->isSubClassOf("Operand")) {
504 // FIXME: What other attributes should we check here? Identical
505 // MIOperandInfo perhaps?
506 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
507 return false;
508 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
509 return true;
510 }
511
Bob Wilsona49c7df2011-01-26 19:44:55 +0000512 return false;
513}
514
Chris Lattner662e5a32010-11-06 07:14:44 +0000515CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000516 AsmString = R->getValueAsString("AsmString");
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000517 Result = R->getValueAsDag("ResultInst");
Chris Lattner225549f2010-11-06 06:39:47 +0000518
519 // Verify that the root of the result is an instruction.
Sean Silva6cfc8062012-10-10 20:24:43 +0000520 DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
Chris Lattner225549f2010-11-06 06:39:47 +0000521 if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction"))
522 throw TGError(R->getLoc(), "result of inst alias should be an instruction");
523
524 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000525
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000526 // NameClass - If argument names are repeated, we need to verify they have
527 // the same class.
528 StringMap<Record*> NameClass;
Bob Wilson55931ab2011-01-20 18:38:10 +0000529 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000530 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
Bob Wilson55931ab2011-01-20 18:38:10 +0000531 if (!ADI || Result->getArgName(i).empty())
532 continue;
533 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
534 // $foo can exist multiple times in the result list, but it must have the
535 // same type.
536 Record *&Entry = NameClass[Result->getArgName(i)];
537 if (Entry && Entry != ADI->getDef())
538 throw TGError(R->getLoc(), "result value $" + Result->getArgName(i) +
539 " is both " + Entry->getName() + " and " +
540 ADI->getDef()->getName() + "!");
541 Entry = ADI->getDef();
542 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000543
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000544 // Decode and validate the arguments of the result.
Chris Lattner41409852010-11-06 07:31:43 +0000545 unsigned AliasOpNo = 0;
546 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000547
Jim Grosbachbfc94292011-11-15 01:46:57 +0000548 // Tied registers don't have an entry in the result dag unless they're part
549 // of a complex operand, in which case we include them anyways, as we
550 // don't have any other way to specify the whole operand.
551 if (ResultInst->Operands[i].MINumOperands == 1 &&
552 ResultInst->Operands[i].getTiedRegister() != -1)
Chris Lattner41409852010-11-06 07:31:43 +0000553 continue;
554
555 if (AliasOpNo >= Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000556 throw TGError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000557
Bob Wilsona49c7df2011-01-26 19:44:55 +0000558 Record *InstOpRec = ResultInst->Operands[i].Rec;
559 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
560 ResultOperand ResOp(static_cast<int64_t>(0));
561 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
562 R->getLoc(), T, ResOp)) {
Owen Anderson7e8921b2012-06-08 00:25:03 +0000563 // If this is a simple operand, or a complex operand with a custom match
564 // class, then we can match is verbatim.
565 if (NumSubOps == 1 ||
566 (InstOpRec->getValue("ParserMatchClass") &&
567 InstOpRec->getValueAsDef("ParserMatchClass")
568 ->getValueAsString("Name") != "Imm")) {
569 ResultOperands.push_back(ResOp);
570 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
571 ++AliasOpNo;
572
573 // Otherwise, we need to match each of the suboperands individually.
574 } else {
575 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
576 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
Sean Silva3f7b7f82012-10-10 20:24:47 +0000577 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Owen Anderson7e8921b2012-06-08 00:25:03 +0000578
579 // Take care to instantiate each of the suboperands with the correct
580 // nomenclature: $foo.bar
581 ResultOperands.push_back(
582 ResultOperand(Result->getArgName(AliasOpNo) + "." +
583 MIOI->getArgName(SubOp), SubRec));
584 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
585 }
586 ++AliasOpNo;
587 }
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000588 continue;
589 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000590
Bob Wilsona49c7df2011-01-26 19:44:55 +0000591 // If the argument did not match the instruction operand, and the operand
592 // is composed of multiple suboperands, try matching the suboperands.
593 if (NumSubOps > 1) {
David Greene05bce0b2011-07-29 22:43:06 +0000594 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000595 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
596 if (AliasOpNo >= Result->getNumArgs())
597 throw TGError(R->getLoc(), "not enough arguments for instruction!");
Sean Silva3f7b7f82012-10-10 20:24:47 +0000598 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Bob Wilsona49c7df2011-01-26 19:44:55 +0000599 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
600 R->getLoc(), T, ResOp)) {
601 ResultOperands.push_back(ResOp);
602 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
603 ++AliasOpNo;
604 } else {
605 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
606 " does not match instruction operand class " +
607 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
608 }
609 }
Chris Lattner98c870f2010-11-06 19:25:43 +0000610 continue;
611 }
Bob Wilsona49c7df2011-01-26 19:44:55 +0000612 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
613 " does not match instruction operand class " +
614 InstOpRec->getName());
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000615 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000616
Chris Lattner41409852010-11-06 07:31:43 +0000617 if (AliasOpNo != Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000618 throw TGError(R->getLoc(), "too many operands for instruction!");
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000619}