blob: c723c36617e6c0fe5f5f08c2000dbb06579d70f5 [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"
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000016#include "Error.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000017#include "Record.h"
18#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 Greened4a90662011-07-11 18:25:51 +000033 const DagInit *OutDI = R->getValueAsDag("OutOperandList");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000034
David Greened4a90662011-07-11 18:25:51 +000035 if (const DefInit *Init =
36 dynamic_cast<const DefInit*>(OutDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000037 if (Init->getDef()->getName() != "outs")
Chris Lattnercedef1c2010-03-18 20:50:52 +000038 throw R->getName() + ": invalid def name for output list: use 'outs'";
39 } else
40 throw R->getName() + ": invalid output list: use 'outs'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000041
Chris Lattnerf55eed22010-03-18 21:07:39 +000042 NumDefs = OutDI->getNumArgs();
Chris Lattnerc240bb02010-11-01 04:03:32 +000043
David Greened4a90662011-07-11 18:25:51 +000044 const DagInit *InDI = R->getValueAsDag("InOperandList");
45 if (const DefInit *Init = dynamic_cast<const DefInit*>(InDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000046 if (Init->getDef()->getName() != "ins")
Chris Lattnercedef1c2010-03-18 20:50:52 +000047 throw R->getName() + ": invalid def name for input list: use 'ins'";
48 } else
49 throw R->getName() + ": invalid input list: use 'ins'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000050
Chris Lattner6cc654b2008-01-06 01:35:39 +000051 unsigned MIOperandNo = 0;
52 std::set<std::string> OperandNames;
Chris Lattnerf55eed22010-03-18 21:07:39 +000053 for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
David Greened4a90662011-07-11 18:25:51 +000054 const Init *ArgInit;
Chris Lattnerf55eed22010-03-18 21:07:39 +000055 std::string ArgName;
56 if (i < NumDefs) {
57 ArgInit = OutDI->getArg(i);
58 ArgName = OutDI->getArgName(i);
59 } else {
60 ArgInit = InDI->getArg(i-NumDefs);
61 ArgName = InDI->getArgName(i-NumDefs);
62 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000063
David Greened4a90662011-07-11 18:25:51 +000064 const DefInit *Arg = dynamic_cast<const DefInit*>(ArgInit);
Chris Lattner6cc654b2008-01-06 01:35:39 +000065 if (!Arg)
66 throw "Illegal operand for the '" + R->getName() + "' instruction!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000067
Chris Lattner6cc654b2008-01-06 01:35:39 +000068 Record *Rec = Arg->getDef();
69 std::string PrintMethod = "printOperand";
Jim Grosbach5013f742010-10-12 22:21:57 +000070 std::string EncoderMethod;
Chris Lattner6cc654b2008-01-06 01:35:39 +000071 unsigned NumOps = 1;
David Greened4a90662011-07-11 18:25:51 +000072 const 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");
Jim Grosbach5013f742010-10-12 22:21:57 +000077 // If there is an explicit encoder method, use it.
Chris Lattner2ac19022010-11-15 05:19:05 +000078 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattner6cc654b2008-01-06 01:35:39 +000079 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000080
Chris Lattner6cc654b2008-01-06 01:35:39 +000081 // Verify that MIOpInfo has an 'ops' root value.
David Greened4a90662011-07-11 18:25:51 +000082 if (!dynamic_cast<const DefInit*>(MIOpInfo->getOperator()) ||
83 dynamic_cast<const DefInit*>(MIOpInfo->getOperator())
Chris Lattnerc240bb02010-11-01 04:03:32 +000084 ->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;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000100 } else if (!Rec->isSubClassOf("RegisterClass") &&
NAKAMURA Takumi36c3bc42011-01-26 02:03:48 +0000101 !Rec->isSubClassOf("PointerLikeRegClass") &&
102 Rec->getName() != "unknown")
Chris Lattner6cc654b2008-01-06 01:35:39 +0000103 throw "Unknown operand class '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000104 "' in '" + R->getName() + "' instruction!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000105
Chris Lattner6cc654b2008-01-06 01:35:39 +0000106 // Check that the operand has a name and that it's unique.
Chris Lattnerf55eed22010-03-18 21:07:39 +0000107 if (ArgName.empty())
Chris Lattner6cc654b2008-01-06 01:35:39 +0000108 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000109 " has no name!";
Chris Lattnerf55eed22010-03-18 21:07:39 +0000110 if (!OperandNames.insert(ArgName).second)
Chris Lattner6cc654b2008-01-06 01:35:39 +0000111 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000112 " has the same name as a previous operand!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000113
Jim Grosbach5013f742010-10-12 22:21:57 +0000114 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
Chris Lattner6cc654b2008-01-06 01:35:39 +0000115 MIOperandNo, NumOps, MIOpInfo));
116 MIOperandNo += NumOps;
117 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000118
119
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000120 // Make sure the constraints list for each operand is large enough to hold
121 // constraint info, even if none is present.
122 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
123 OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000124}
125
Chris Lattnerc240bb02010-11-01 04:03:32 +0000126
Chris Lattner6cc654b2008-01-06 01:35:39 +0000127/// getOperandNamed - Return the index of the operand with the specified
128/// non-empty name. If the instruction does not have an operand with the
129/// specified name, throw an exception.
130///
Chris Lattnerc240bb02010-11-01 04:03:32 +0000131unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000132 unsigned OpIdx;
133 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000134 throw "'" + TheDef->getName() + "' does not have an operand named '$" +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000135 Name.str() + "'!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000136}
137
Jim Grosbach01855072010-10-11 18:25:51 +0000138/// hasOperandNamed - Query whether the instruction has an operand of the
139/// given name. If so, return true and set OpIdx to the index of the
140/// operand. Otherwise, return false.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000141bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000142 assert(!Name.empty() && "Cannot search for operand with no name!");
143 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
144 if (OperandList[i].Name == Name) {
145 OpIdx = i;
146 return true;
147 }
148 return false;
149}
150
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000151std::pair<unsigned,unsigned>
Chris Lattnerc240bb02010-11-01 04:03:32 +0000152CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattner6cc654b2008-01-06 01:35:39 +0000153 if (Op.empty() || Op[0] != '$')
154 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000155
Chris Lattner6cc654b2008-01-06 01:35:39 +0000156 std::string OpName = Op.substr(1);
157 std::string SubOpName;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000158
Chris Lattner6cc654b2008-01-06 01:35:39 +0000159 // Check to see if this is $foo.bar.
160 std::string::size_type DotIdx = OpName.find_first_of(".");
161 if (DotIdx != std::string::npos) {
162 SubOpName = OpName.substr(DotIdx+1);
163 if (SubOpName.empty())
164 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
165 OpName = OpName.substr(0, DotIdx);
166 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000167
Chris Lattner6cc654b2008-01-06 01:35:39 +0000168 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000169
Chris Lattner6cc654b2008-01-06 01:35:39 +0000170 if (SubOpName.empty()) { // If no suboperand name was specified:
171 // If one was needed, throw.
172 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
173 SubOpName.empty())
174 throw TheDef->getName() + ": Illegal to refer to"
Chris Lattnerc240bb02010-11-01 04:03:32 +0000175 " whole operand part of complex operand '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000176
Chris Lattner6cc654b2008-01-06 01:35:39 +0000177 // Otherwise, return the operand.
178 return std::make_pair(OpIdx, 0U);
179 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000180
Chris Lattner6cc654b2008-01-06 01:35:39 +0000181 // Find the suboperand number involved.
David Greened4a90662011-07-11 18:25:51 +0000182 const DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000183 if (MIOpInfo == 0)
184 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000185
Chris Lattner6cc654b2008-01-06 01:35:39 +0000186 // Find the operand with the right name.
187 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
188 if (MIOpInfo->getArgName(i) == SubOpName)
189 return std::make_pair(OpIdx, i);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000190
Chris Lattner6cc654b2008-01-06 01:35:39 +0000191 // Otherwise, didn't find it!
192 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
193}
Chris Lattner9414ae52010-03-27 20:09:24 +0000194
Chris Lattnerc240bb02010-11-01 04:03:32 +0000195static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
196 // EARLY_CLOBBER: @early $reg
197 std::string::size_type wpos = CStr.find_first_of(" \t");
198 std::string::size_type start = CStr.find_first_not_of(" \t");
199 std::string Tok = CStr.substr(start, wpos - start);
200 if (Tok == "@earlyclobber") {
201 std::string Name = CStr.substr(wpos+1);
202 wpos = Name.find_first_not_of(" \t");
203 if (wpos == std::string::npos)
204 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
205 Name = Name.substr(wpos);
206 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000207
Chris Lattnerc240bb02010-11-01 04:03:32 +0000208 // Build the string for the operand
209 if (!Ops[Op.first].Constraints[Op.second].isNone())
210 throw "Operand '" + Name + "' cannot have multiple constraints!";
211 Ops[Op.first].Constraints[Op.second] =
212 CGIOperandList::ConstraintInfo::getEarlyClobber();
213 return;
214 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000215
Chris Lattnerc240bb02010-11-01 04:03:32 +0000216 // Only other constraint is "TIED_TO" for now.
217 std::string::size_type pos = CStr.find_first_of('=');
218 assert(pos != std::string::npos && "Unrecognized constraint");
219 start = CStr.find_first_not_of(" \t");
220 std::string Name = CStr.substr(start, pos - start);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000221
Chris Lattnerc240bb02010-11-01 04:03:32 +0000222 // TIED_TO: $src1 = $dst
223 wpos = Name.find_first_of(" \t");
224 if (wpos == std::string::npos)
225 throw "Illegal format for tied-to constraint: '" + CStr + "'";
226 std::string DestOpName = Name.substr(0, wpos);
227 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000228
Chris Lattnerc240bb02010-11-01 04:03:32 +0000229 Name = CStr.substr(pos+1);
230 wpos = Name.find_first_not_of(" \t");
231 if (wpos == std::string::npos)
232 throw "Illegal format for tied-to constraint: '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000233
Chris Lattnerc240bb02010-11-01 04:03:32 +0000234 std::pair<unsigned,unsigned> SrcOp =
235 Ops.ParseOperandName(Name.substr(wpos), false);
236 if (SrcOp > DestOp)
237 throw "Illegal tied-to operand constraint '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000238
239
Chris Lattnerc240bb02010-11-01 04:03:32 +0000240 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000241
Chris Lattnerc240bb02010-11-01 04:03:32 +0000242 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
243 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
244 Ops[DestOp.first].Constraints[DestOp.second] =
245 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
246}
247
248static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000249 if (CStr.empty()) return;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000250
Chris Lattnerc240bb02010-11-01 04:03:32 +0000251 const std::string delims(",");
252 std::string::size_type bidx, eidx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000253
Chris Lattnerc240bb02010-11-01 04:03:32 +0000254 bidx = CStr.find_first_not_of(delims);
255 while (bidx != std::string::npos) {
256 eidx = CStr.find_first_of(delims, bidx);
257 if (eidx == std::string::npos)
258 eidx = CStr.length();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000259
Chris Lattnerc240bb02010-11-01 04:03:32 +0000260 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
261 bidx = CStr.find_first_not_of(delims, eidx);
262 }
263}
264
265void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
266 while (1) {
267 std::string OpName;
268 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
269 if (OpName.empty()) break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000270
Chris Lattnerc240bb02010-11-01 04:03:32 +0000271 // Figure out which operand this is.
272 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000273
Chris Lattnerc240bb02010-11-01 04:03:32 +0000274 // Mark the operand as not-to-be encoded.
275 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
276 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
277 OperandList[Op.first].DoNotEncode[Op.second] = true;
278 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000279
Chris Lattnerc240bb02010-11-01 04:03:32 +0000280}
281
282//===----------------------------------------------------------------------===//
283// CodeGenInstruction Implementation
284//===----------------------------------------------------------------------===//
285
286CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R), Operands(R) {
287 Namespace = R->getValueAsString("Namespace");
288 AsmString = R->getValueAsString("AsmString");
289
290 isReturn = R->getValueAsBit("isReturn");
291 isBranch = R->getValueAsBit("isBranch");
292 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
293 isCompare = R->getValueAsBit("isCompare");
Evan Chengc4af4632010-11-17 20:13:28 +0000294 isMoveImm = R->getValueAsBit("isMoveImm");
Evan Cheng0f040a22011-03-15 05:09:26 +0000295 isBitcast = R->getValueAsBit("isBitcast");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000296 isBarrier = R->getValueAsBit("isBarrier");
297 isCall = R->getValueAsBit("isCall");
298 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
299 mayLoad = R->getValueAsBit("mayLoad");
300 mayStore = R->getValueAsBit("mayStore");
301 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
302 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
303 isCommutable = R->getValueAsBit("isCommutable");
304 isTerminator = R->getValueAsBit("isTerminator");
305 isReMaterializable = R->getValueAsBit("isReMaterializable");
306 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
307 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
308 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
309 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
310 hasSideEffects = R->getValueAsBit("hasSideEffects");
311 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
312 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
313 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
314 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Jim Grosbache727d672011-07-07 00:48:02 +0000315 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
Jim Grosbach806fcc02011-07-06 21:33:38 +0000316 isPseudo = R->getValueAsBit("isPseudo");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000317 ImplicitDefs = R->getValueAsListOfDefs("Defs");
318 ImplicitUses = R->getValueAsListOfDefs("Uses");
319
320 if (neverHasSideEffects + hasSideEffects > 1)
321 throw R->getName() + ": multiple conflicting side-effect flags set!";
322
323 // Parse Constraints.
324 ParseConstraints(R->getValueAsString("Constraints"), Operands);
325
326 // Parse the DisableEncoding field.
327 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
328}
Chris Lattner9414ae52010-03-27 20:09:24 +0000329
330/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
331/// implicit def and it has a known VT, return the VT, otherwise return
332/// MVT::Other.
333MVT::SimpleValueType CodeGenInstruction::
334HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
335 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000336
Chris Lattner9414ae52010-03-27 20:09:24 +0000337 // Check to see if the first implicit def has a resolvable type.
338 Record *FirstImplicitDef = ImplicitDefs[0];
339 assert(FirstImplicitDef->isSubClassOf("Register"));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000340 const std::vector<MVT::SimpleValueType> &RegVTs =
Chris Lattner9414ae52010-03-27 20:09:24 +0000341 TargetInfo.getRegisterVTs(FirstImplicitDef);
342 if (RegVTs.size() == 1)
343 return RegVTs[0];
344 return MVT::Other;
345}
346
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000347
348/// FlattenAsmStringVariants - Flatten the specified AsmString to only
349/// include text from the specified variant, returning the new string.
350std::string CodeGenInstruction::
351FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
352 std::string Res = "";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000353
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000354 for (;;) {
355 // Find the start of the next variant string.
356 size_t VariantsStart = 0;
357 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
358 if (Cur[VariantsStart] == '{' &&
359 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
360 Cur[VariantsStart-1] != '\\')))
361 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000362
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000363 // Add the prefix to the result.
364 Res += Cur.slice(0, VariantsStart);
365 if (VariantsStart == Cur.size())
366 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000367
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000368 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000369
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000370 // Scan to the end of the variants string.
371 size_t VariantsEnd = VariantsStart;
372 unsigned NestedBraces = 1;
373 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
374 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
375 if (--NestedBraces == 0)
376 break;
377 } else if (Cur[VariantsEnd] == '{')
378 ++NestedBraces;
379 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000380
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000381 // Select the Nth variant (or empty).
382 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
383 for (unsigned i = 0; i != Variant; ++i)
384 Selection = Selection.split('|').second;
385 Res += Selection.split('|').first;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000386
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000387 assert(VariantsEnd != Cur.size() &&
388 "Unterminated variants in assembly string!");
389 Cur = Cur.substr(VariantsEnd + 1);
390 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000391
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000392 return Res;
393}
394
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000395
396//===----------------------------------------------------------------------===//
397/// CodeGenInstAlias Implementation
398//===----------------------------------------------------------------------===//
399
Bob Wilsona49c7df2011-01-26 19:44:55 +0000400/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
401/// constructor. It checks if an argument in an InstAlias pattern matches
402/// the corresponding operand of the instruction. It returns true on a
403/// successful match, with ResOp set to the result operand to be used.
David Greened4a90662011-07-11 18:25:51 +0000404bool CodeGenInstAlias::tryAliasOpMatch(const DagInit *Result,
405 unsigned AliasOpNo,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000406 Record *InstOpRec, bool hasSubOps,
407 SMLoc Loc, CodeGenTarget &T,
408 ResultOperand &ResOp) {
David Greened4a90662011-07-11 18:25:51 +0000409 const Init *Arg = Result->getArg(AliasOpNo);
410 const DefInit *ADI = dynamic_cast<const DefInit*>(Arg);
Bob Wilsona49c7df2011-01-26 19:44:55 +0000411
412 if (ADI && ADI->getDef() == InstOpRec) {
413 // If the operand is a record, it must have a name, and the record type
414 // must match up with the instruction's argument type.
415 if (Result->getArgName(AliasOpNo).empty())
416 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
417 " must have a name!");
418 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
419 return true;
420 }
421
422 // Handle explicit registers.
423 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000424 if (InstOpRec->isSubClassOf("RegisterOperand"))
425 InstOpRec = InstOpRec->getValueAsDef("RegClass");
426
Bob Wilsona49c7df2011-01-26 19:44:55 +0000427 if (!InstOpRec->isSubClassOf("RegisterClass"))
428 return false;
429
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000430 if (!T.getRegisterClass(InstOpRec)
431 .contains(T.getRegBank().getReg(ADI->getDef())))
Bob Wilsona49c7df2011-01-26 19:44:55 +0000432 throw TGError(Loc, "fixed register " +ADI->getDef()->getName()
433 + " is not a member of the " + InstOpRec->getName() +
434 " register class!");
435
436 if (!Result->getArgName(AliasOpNo).empty())
437 throw TGError(Loc, "result fixed register argument must "
438 "not have a name!");
439
440 ResOp = ResultOperand(ADI->getDef());
441 return true;
442 }
443
444 // Handle "zero_reg" for optional def operands.
445 if (ADI && ADI->getDef()->getName() == "zero_reg") {
446
447 // Check if this is an optional def.
448 if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
449 throw TGError(Loc, "reg0 used for result that is not an "
450 "OptionalDefOperand!");
451
452 ResOp = ResultOperand(static_cast<Record*>(0));
453 return true;
454 }
455
David Greened4a90662011-07-11 18:25:51 +0000456 if (const IntInit *II = dynamic_cast<const IntInit*>(Arg)) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000457 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
458 return false;
459 // Integer arguments can't have names.
460 if (!Result->getArgName(AliasOpNo).empty())
461 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
462 " must not have a name!");
463 ResOp = ResultOperand(II->getValue());
464 return true;
465 }
466
467 return false;
468}
469
Chris Lattner662e5a32010-11-06 07:14:44 +0000470CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000471 AsmString = R->getValueAsString("AsmString");
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000472 Result = R->getValueAsDag("ResultInst");
Chris Lattner225549f2010-11-06 06:39:47 +0000473
474 // Verify that the root of the result is an instruction.
David Greened4a90662011-07-11 18:25:51 +0000475 const DefInit *DI = dynamic_cast<const DefInit*>(Result->getOperator());
Chris Lattner225549f2010-11-06 06:39:47 +0000476 if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction"))
477 throw TGError(R->getLoc(), "result of inst alias should be an instruction");
478
479 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000480
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000481 // NameClass - If argument names are repeated, we need to verify they have
482 // the same class.
483 StringMap<Record*> NameClass;
Bob Wilson55931ab2011-01-20 18:38:10 +0000484 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
David Greened4a90662011-07-11 18:25:51 +0000485 const DefInit *ADI = dynamic_cast<const DefInit*>(Result->getArg(i));
Bob Wilson55931ab2011-01-20 18:38:10 +0000486 if (!ADI || Result->getArgName(i).empty())
487 continue;
488 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
489 // $foo can exist multiple times in the result list, but it must have the
490 // same type.
491 Record *&Entry = NameClass[Result->getArgName(i)];
492 if (Entry && Entry != ADI->getDef())
493 throw TGError(R->getLoc(), "result value $" + Result->getArgName(i) +
494 " is both " + Entry->getName() + " and " +
495 ADI->getDef()->getName() + "!");
496 Entry = ADI->getDef();
497 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000498
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000499 // Decode and validate the arguments of the result.
Chris Lattner41409852010-11-06 07:31:43 +0000500 unsigned AliasOpNo = 0;
501 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000502
Chris Lattner41409852010-11-06 07:31:43 +0000503 // Tied registers don't have an entry in the result dag.
504 if (ResultInst->Operands[i].getTiedRegister() != -1)
505 continue;
506
507 if (AliasOpNo >= Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000508 throw TGError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000509
Bob Wilsona49c7df2011-01-26 19:44:55 +0000510 Record *InstOpRec = ResultInst->Operands[i].Rec;
511 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
512 ResultOperand ResOp(static_cast<int64_t>(0));
513 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
514 R->getLoc(), T, ResOp)) {
515 ResultOperands.push_back(ResOp);
516 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
Chris Lattner41409852010-11-06 07:31:43 +0000517 ++AliasOpNo;
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000518 continue;
519 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000520
Bob Wilsona49c7df2011-01-26 19:44:55 +0000521 // If the argument did not match the instruction operand, and the operand
522 // is composed of multiple suboperands, try matching the suboperands.
523 if (NumSubOps > 1) {
David Greened4a90662011-07-11 18:25:51 +0000524 const DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000525 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
526 if (AliasOpNo >= Result->getNumArgs())
527 throw TGError(R->getLoc(), "not enough arguments for instruction!");
David Greened4a90662011-07-11 18:25:51 +0000528 Record *SubRec =
529 dynamic_cast<const DefInit*>(MIOI->getArg(SubOp))->getDef();
Bob Wilsona49c7df2011-01-26 19:44:55 +0000530 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
531 R->getLoc(), T, ResOp)) {
532 ResultOperands.push_back(ResOp);
533 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
534 ++AliasOpNo;
535 } else {
536 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
537 " does not match instruction operand class " +
538 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
539 }
540 }
Chris Lattner98c870f2010-11-06 19:25:43 +0000541 continue;
542 }
Bob Wilsona49c7df2011-01-26 19:44:55 +0000543 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
544 " does not match instruction operand class " +
545 InstOpRec->getName());
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000546 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000547
Chris Lattner41409852010-11-06 07:31:43 +0000548 if (AliasOpNo != Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000549 throw TGError(R->getLoc(), "too many operands for instruction!");
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000550}