blob: 8ec7682985faa2ea1eb86d06205176af62f78c0f [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"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000016#include "llvm/ADT/STLExtras.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000017#include "llvm/ADT/StringExtras.h"
Chris Lattner3f2c8e42010-11-06 07:06:09 +000018#include "llvm/ADT/StringMap.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000019#include "llvm/TableGen/Error.h"
20#include "llvm/TableGen/Record.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")
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000037 PrintFatalError(R->getName() + ": invalid def name for output list: use 'outs'");
Chris Lattnercedef1c2010-03-18 20:50:52 +000038 } else
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000039 PrintFatalError(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")
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000046 PrintFatalError(R->getName() + ": invalid def name for input list: use 'ins'");
Chris Lattnercedef1c2010-03-18 20:50:52 +000047 } else
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000048 PrintFatalError(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)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000065 PrintFatalError("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")
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000085 PrintFatalError("Bad value for MIOperandInfo in operand '" + Rec->getName() +
86 "'\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"))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000104 PrintFatalError("Unknown operand class '" + Rec->getName() +
105 "' 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())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000109 PrintFatalError("In instruction '" + R->getName() + "', operand #" + utostr(i) +
110 " has no name!");
Chris Lattnerf55eed22010-03-18 21:07:39 +0000111 if (!OperandNames.insert(ArgName).second)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000112 PrintFatalError("In instruction '" + R->getName() + "', operand #" + utostr(i) +
113 " 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
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000131/// specified name, abort.
Chris Lattner6cc654b2008-01-06 01:35:39 +0000132///
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;
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000136 PrintFatalError("'" + TheDef->getName() + "' does not have an operand named '$" +
137 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] != '$')
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000156 PrintFatalError(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())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000166 PrintFatalError(TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000167 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())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000176 PrintFatalError(TheDef->getName() + ": Illegal to refer to"
177 " 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)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000186 PrintFatalError(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!
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000194 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
Aaron Ballman33695992013-08-16 01:43:31 +0000195 return std::make_pair(0U, 0U);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000196}
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)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000207 PrintFatalError("Illegal format for @earlyclobber constraint: '" + CStr + "'");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000208 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())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000213 PrintFatalError("Operand '" + Name + "' cannot have multiple constraints!");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000214 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)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000228 PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000229 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)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000235 PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000236
Lang Hames64486732012-10-20 22:44:13 +0000237 std::string SrcOpName = Name.substr(wpos);
238 std::pair<unsigned,unsigned> SrcOp = Ops.ParseOperandName(SrcOpName, false);
239 if (SrcOp > DestOp) {
240 std::swap(SrcOp, DestOp);
241 std::swap(SrcOpName, DestOpName);
242 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000243
Chris Lattnerc240bb02010-11-01 04:03:32 +0000244 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000245
Chris Lattnerc240bb02010-11-01 04:03:32 +0000246 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000247 PrintFatalError("Operand '" + DestOpName +
248 "' cannot have multiple constraints!");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000249 Ops[DestOp.first].Constraints[DestOp.second] =
Jim Grosbach2a8cd572011-11-15 01:05:12 +0000250 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000251}
252
253static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000254 if (CStr.empty()) return;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000255
Chris Lattnerc240bb02010-11-01 04:03:32 +0000256 const std::string delims(",");
257 std::string::size_type bidx, eidx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000258
Chris Lattnerc240bb02010-11-01 04:03:32 +0000259 bidx = CStr.find_first_not_of(delims);
260 while (bidx != std::string::npos) {
261 eidx = CStr.find_first_of(delims, bidx);
262 if (eidx == std::string::npos)
263 eidx = CStr.length();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000264
Chris Lattnerc240bb02010-11-01 04:03:32 +0000265 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
266 bidx = CStr.find_first_not_of(delims, eidx);
267 }
268}
269
270void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
271 while (1) {
Chris Lattnerc30a38f2011-07-21 06:21:31 +0000272 std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
273 std::string OpName = P.first;
274 DisableEncoding = P.second;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000275 if (OpName.empty()) break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000276
Chris Lattnerc240bb02010-11-01 04:03:32 +0000277 // Figure out which operand this is.
278 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000279
Chris Lattnerc240bb02010-11-01 04:03:32 +0000280 // Mark the operand as not-to-be encoded.
281 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
282 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
283 OperandList[Op.first].DoNotEncode[Op.second] = true;
284 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000285
Chris Lattnerc240bb02010-11-01 04:03:32 +0000286}
287
288//===----------------------------------------------------------------------===//
289// CodeGenInstruction Implementation
290//===----------------------------------------------------------------------===//
291
Jakob Stoklund Olesen912519a2012-08-24 00:31:16 +0000292CodeGenInstruction::CodeGenInstruction(Record *R)
293 : TheDef(R), Operands(R), InferredFrom(0) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000294 Namespace = R->getValueAsString("Namespace");
295 AsmString = R->getValueAsString("AsmString");
296
297 isReturn = R->getValueAsBit("isReturn");
298 isBranch = R->getValueAsBit("isBranch");
299 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
300 isCompare = R->getValueAsBit("isCompare");
Evan Chengc4af4632010-11-17 20:13:28 +0000301 isMoveImm = R->getValueAsBit("isMoveImm");
Evan Cheng0f040a22011-03-15 05:09:26 +0000302 isBitcast = R->getValueAsBit("isBitcast");
Jakob Stoklund Olesenf2c64ef2012-08-16 23:11:47 +0000303 isSelect = R->getValueAsBit("isSelect");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000304 isBarrier = R->getValueAsBit("isBarrier");
305 isCall = R->getValueAsBit("isCall");
306 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000307 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
308 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
309 isCommutable = R->getValueAsBit("isCommutable");
310 isTerminator = R->getValueAsBit("isTerminator");
311 isReMaterializable = R->getValueAsBit("isReMaterializable");
312 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
313 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Andrew Trick83a80312011-09-20 18:22:31 +0000314 hasPostISelHook = R->getValueAsBit("hasPostISelHook");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000315 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
316 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Jakob Stoklund Olesenc1f10fd2012-08-23 19:34:46 +0000317
318 mayLoad = R->getValueAsBitOrUnset("mayLoad", mayLoad_Unset);
319 mayStore = R->getValueAsBitOrUnset("mayStore", mayStore_Unset);
320 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects",
321 hasSideEffects_Unset);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000322 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
Jakob Stoklund Olesenc1f10fd2012-08-23 19:34:46 +0000323
Chris Lattnerc240bb02010-11-01 04:03:32 +0000324 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
325 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
326 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Jim Grosbache727d672011-07-07 00:48:02 +0000327 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
Jim Grosbach806fcc02011-07-06 21:33:38 +0000328 isPseudo = R->getValueAsBit("isPseudo");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000329 ImplicitDefs = R->getValueAsListOfDefs("Defs");
330 ImplicitUses = R->getValueAsListOfDefs("Uses");
331
332 if (neverHasSideEffects + hasSideEffects > 1)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000333 PrintFatalError(R->getName() + ": multiple conflicting side-effect flags set!");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000334
335 // Parse Constraints.
336 ParseConstraints(R->getValueAsString("Constraints"), Operands);
337
338 // Parse the DisableEncoding field.
339 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
340}
Chris Lattner9414ae52010-03-27 20:09:24 +0000341
342/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
343/// implicit def and it has a known VT, return the VT, otherwise return
344/// MVT::Other.
345MVT::SimpleValueType CodeGenInstruction::
346HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
347 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000348
Chris Lattner9414ae52010-03-27 20:09:24 +0000349 // Check to see if the first implicit def has a resolvable type.
350 Record *FirstImplicitDef = ImplicitDefs[0];
351 assert(FirstImplicitDef->isSubClassOf("Register"));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000352 const std::vector<MVT::SimpleValueType> &RegVTs =
Chris Lattner9414ae52010-03-27 20:09:24 +0000353 TargetInfo.getRegisterVTs(FirstImplicitDef);
354 if (RegVTs.size() == 1)
355 return RegVTs[0];
356 return MVT::Other;
357}
358
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000359
360/// FlattenAsmStringVariants - Flatten the specified AsmString to only
361/// include text from the specified variant, returning the new string.
362std::string CodeGenInstruction::
363FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
364 std::string Res = "";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000365
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000366 for (;;) {
367 // Find the start of the next variant string.
368 size_t VariantsStart = 0;
369 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
370 if (Cur[VariantsStart] == '{' &&
371 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
372 Cur[VariantsStart-1] != '\\')))
373 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000374
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000375 // Add the prefix to the result.
376 Res += Cur.slice(0, VariantsStart);
377 if (VariantsStart == Cur.size())
378 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000379
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000380 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000381
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000382 // Scan to the end of the variants string.
383 size_t VariantsEnd = VariantsStart;
384 unsigned NestedBraces = 1;
385 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
386 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
387 if (--NestedBraces == 0)
388 break;
389 } else if (Cur[VariantsEnd] == '{')
390 ++NestedBraces;
391 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000392
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000393 // Select the Nth variant (or empty).
394 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
395 for (unsigned i = 0; i != Variant; ++i)
396 Selection = Selection.split('|').second;
397 Res += Selection.split('|').first;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000398
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000399 assert(VariantsEnd != Cur.size() &&
400 "Unterminated variants in assembly string!");
401 Cur = Cur.substr(VariantsEnd + 1);
402 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000403
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000404 return Res;
405}
406
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000407
408//===----------------------------------------------------------------------===//
409/// CodeGenInstAlias Implementation
410//===----------------------------------------------------------------------===//
411
Bob Wilsona49c7df2011-01-26 19:44:55 +0000412/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
413/// constructor. It checks if an argument in an InstAlias pattern matches
414/// the corresponding operand of the instruction. It returns true on a
415/// successful match, with ResOp set to the result operand to be used.
David Greene05bce0b2011-07-29 22:43:06 +0000416bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000417 Record *InstOpRec, bool hasSubOps,
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +0000418 ArrayRef<SMLoc> Loc, CodeGenTarget &T,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000419 ResultOperand &ResOp) {
David Greene05bce0b2011-07-29 22:43:06 +0000420 Init *Arg = Result->getArg(AliasOpNo);
Sean Silva6cfc8062012-10-10 20:24:43 +0000421 DefInit *ADI = dyn_cast<DefInit>(Arg);
Bob Wilsona49c7df2011-01-26 19:44:55 +0000422
423 if (ADI && ADI->getDef() == InstOpRec) {
424 // If the operand is a record, it must have a name, and the record type
425 // must match up with the instruction's argument type.
426 if (Result->getArgName(AliasOpNo).empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000427 PrintFatalError(Loc, "result argument #" + utostr(AliasOpNo) +
Bob Wilsona49c7df2011-01-26 19:44:55 +0000428 " must have a name!");
429 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
430 return true;
431 }
432
Jim Grosbachbe5d6bc2011-10-28 16:43:40 +0000433 // For register operands, the source register class can be a subclass
434 // of the instruction register class, not just an exact match.
435 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
436 if (!InstOpRec->isSubClassOf("RegisterClass"))
437 return false;
Jim Grosbach48c1f842011-10-28 22:32:53 +0000438 if (!T.getRegisterClass(InstOpRec)
439 .hasSubClass(&T.getRegisterClass(ADI->getDef())))
440 return false;
441 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
442 return true;
Jim Grosbachbe5d6bc2011-10-28 16:43:40 +0000443 }
444
Bob Wilsona49c7df2011-01-26 19:44:55 +0000445 // Handle explicit registers.
446 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
Jim Grosbachc68e9272011-08-19 20:33:06 +0000447 if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
448 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
449 // The operand info should only have a single (register) entry. We
450 // want the register class of it.
Sean Silva3f7b7f82012-10-10 20:24:47 +0000451 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
Jim Grosbachc68e9272011-08-19 20:33:06 +0000452 }
453
Owen Andersonbea6f612011-06-27 21:06:21 +0000454 if (InstOpRec->isSubClassOf("RegisterOperand"))
455 InstOpRec = InstOpRec->getValueAsDef("RegClass");
456
Bob Wilsona49c7df2011-01-26 19:44:55 +0000457 if (!InstOpRec->isSubClassOf("RegisterClass"))
458 return false;
459
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000460 if (!T.getRegisterClass(InstOpRec)
461 .contains(T.getRegBank().getReg(ADI->getDef())))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000462 PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
463 " is not a member of the " + InstOpRec->getName() +
464 " register class!");
Bob Wilsona49c7df2011-01-26 19:44:55 +0000465
466 if (!Result->getArgName(AliasOpNo).empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000467 PrintFatalError(Loc, "result fixed register argument must "
468 "not have a name!");
Bob Wilsona49c7df2011-01-26 19:44:55 +0000469
470 ResOp = ResultOperand(ADI->getDef());
471 return true;
472 }
473
474 // Handle "zero_reg" for optional def operands.
475 if (ADI && ADI->getDef()->getName() == "zero_reg") {
476
477 // Check if this is an optional def.
Jim Grosbachbfc94292011-11-15 01:46:57 +0000478 // Tied operands where the source is a sub-operand of a complex operand
479 // need to represent both operands in the alias destination instruction.
480 // Allow zero_reg for the tied portion. This can and should go away once
481 // the MC representation of things doesn't use tied operands at all.
482 //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
483 // throw TGError(Loc, "reg0 used for result that is not an "
484 // "OptionalDefOperand!");
Bob Wilsona49c7df2011-01-26 19:44:55 +0000485
486 ResOp = ResultOperand(static_cast<Record*>(0));
487 return true;
488 }
489
Jim Grosbach48c1f842011-10-28 22:32:53 +0000490 // Literal integers.
Sean Silva6cfc8062012-10-10 20:24:43 +0000491 if (IntInit *II = dyn_cast<IntInit>(Arg)) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000492 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
493 return false;
494 // Integer arguments can't have names.
495 if (!Result->getArgName(AliasOpNo).empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000496 PrintFatalError(Loc, "result argument #" + utostr(AliasOpNo) +
497 " must not have a name!");
Bob Wilsona49c7df2011-01-26 19:44:55 +0000498 ResOp = ResultOperand(II->getValue());
499 return true;
500 }
501
Jim Grosbach48c1f842011-10-28 22:32:53 +0000502 // If both are Operands with the same MVT, allow the conversion. It's
503 // up to the user to make sure the values are appropriate, just like
504 // for isel Pat's.
505 if (InstOpRec->isSubClassOf("Operand") &&
506 ADI->getDef()->isSubClassOf("Operand")) {
507 // FIXME: What other attributes should we check here? Identical
508 // MIOperandInfo perhaps?
509 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
510 return false;
511 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
512 return true;
513 }
514
Bob Wilsona49c7df2011-01-26 19:44:55 +0000515 return false;
516}
517
Chris Lattner662e5a32010-11-06 07:14:44 +0000518CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000519 AsmString = R->getValueAsString("AsmString");
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000520 Result = R->getValueAsDag("ResultInst");
Chris Lattner225549f2010-11-06 06:39:47 +0000521
522 // Verify that the root of the result is an instruction.
Sean Silva6cfc8062012-10-10 20:24:43 +0000523 DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
Chris Lattner225549f2010-11-06 06:39:47 +0000524 if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction"))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000525 PrintFatalError(R->getLoc(),
526 "result of inst alias should be an instruction");
Chris Lattner225549f2010-11-06 06:39:47 +0000527
528 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000529
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000530 // NameClass - If argument names are repeated, we need to verify they have
531 // the same class.
532 StringMap<Record*> NameClass;
Bob Wilson55931ab2011-01-20 18:38:10 +0000533 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000534 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
Bob Wilson55931ab2011-01-20 18:38:10 +0000535 if (!ADI || Result->getArgName(i).empty())
536 continue;
537 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
538 // $foo can exist multiple times in the result list, but it must have the
539 // same type.
540 Record *&Entry = NameClass[Result->getArgName(i)];
541 if (Entry && Entry != ADI->getDef())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000542 PrintFatalError(R->getLoc(), "result value $" + Result->getArgName(i) +
543 " is both " + Entry->getName() + " and " +
544 ADI->getDef()->getName() + "!");
Bob Wilson55931ab2011-01-20 18:38:10 +0000545 Entry = ADI->getDef();
546 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000547
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000548 // Decode and validate the arguments of the result.
Chris Lattner41409852010-11-06 07:31:43 +0000549 unsigned AliasOpNo = 0;
550 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000551
Jim Grosbachbfc94292011-11-15 01:46:57 +0000552 // Tied registers don't have an entry in the result dag unless they're part
553 // of a complex operand, in which case we include them anyways, as we
554 // don't have any other way to specify the whole operand.
555 if (ResultInst->Operands[i].MINumOperands == 1 &&
556 ResultInst->Operands[i].getTiedRegister() != -1)
Chris Lattner41409852010-11-06 07:31:43 +0000557 continue;
558
559 if (AliasOpNo >= Result->getNumArgs())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000560 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000561
Bob Wilsona49c7df2011-01-26 19:44:55 +0000562 Record *InstOpRec = ResultInst->Operands[i].Rec;
563 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
564 ResultOperand ResOp(static_cast<int64_t>(0));
565 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
566 R->getLoc(), T, ResOp)) {
Owen Anderson7e8921b2012-06-08 00:25:03 +0000567 // If this is a simple operand, or a complex operand with a custom match
568 // class, then we can match is verbatim.
569 if (NumSubOps == 1 ||
570 (InstOpRec->getValue("ParserMatchClass") &&
571 InstOpRec->getValueAsDef("ParserMatchClass")
572 ->getValueAsString("Name") != "Imm")) {
573 ResultOperands.push_back(ResOp);
574 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
575 ++AliasOpNo;
576
577 // Otherwise, we need to match each of the suboperands individually.
578 } else {
579 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
580 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
Sean Silva3f7b7f82012-10-10 20:24:47 +0000581 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Owen Anderson7e8921b2012-06-08 00:25:03 +0000582
583 // Take care to instantiate each of the suboperands with the correct
584 // nomenclature: $foo.bar
585 ResultOperands.push_back(
586 ResultOperand(Result->getArgName(AliasOpNo) + "." +
587 MIOI->getArgName(SubOp), SubRec));
588 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
589 }
590 ++AliasOpNo;
591 }
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000592 continue;
593 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000594
Bob Wilsona49c7df2011-01-26 19:44:55 +0000595 // If the argument did not match the instruction operand, and the operand
596 // is composed of multiple suboperands, try matching the suboperands.
597 if (NumSubOps > 1) {
David Greene05bce0b2011-07-29 22:43:06 +0000598 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000599 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
600 if (AliasOpNo >= Result->getNumArgs())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000601 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
Sean Silva3f7b7f82012-10-10 20:24:47 +0000602 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
Bob Wilsona49c7df2011-01-26 19:44:55 +0000603 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
604 R->getLoc(), T, ResOp)) {
605 ResultOperands.push_back(ResOp);
606 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
607 ++AliasOpNo;
608 } else {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000609 PrintFatalError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
Bob Wilsona49c7df2011-01-26 19:44:55 +0000610 " does not match instruction operand class " +
611 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
612 }
613 }
Chris Lattner98c870f2010-11-06 19:25:43 +0000614 continue;
615 }
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000616 PrintFatalError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
617 " does not match instruction operand class " +
618 InstOpRec->getName());
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000619 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000620
Chris Lattner41409852010-11-06 07:31:43 +0000621 if (AliasOpNo != Result->getNumArgs())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000622 PrintFatalError(R->getLoc(), "too many operands for instruction!");
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000623}