blob: 41a27cea3ddf557b3cb2494de6657535cf01f225 [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"
Chris Lattner6cc654b2008-01-06 01:35:39 +000016#include "Record.h"
17#include "llvm/ADT/StringExtras.h"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000018#include "llvm/ADT/STLExtras.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000019#include <set>
20using namespace llvm;
21
22static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
Jim Grosbach06801722009-12-16 19:43:02 +000023 // EARLY_CLOBBER: @early $reg
24 std::string::size_type wpos = CStr.find_first_of(" \t");
25 std::string::size_type start = CStr.find_first_not_of(" \t");
26 std::string Tok = CStr.substr(start, wpos - start);
27 if (Tok == "@earlyclobber") {
28 std::string Name = CStr.substr(wpos+1);
29 wpos = Name.find_first_not_of(" \t");
30 if (wpos == std::string::npos)
31 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
32 Name = Name.substr(wpos);
33 std::pair<unsigned,unsigned> Op =
34 I->ParseOperandName(Name, false);
35
36 // Build the string for the operand
Chris Lattnera7d479c2010-02-10 01:45:28 +000037 if (!I->OperandList[Op.first].Constraints[Op.second].isNone())
Jim Grosbach06801722009-12-16 19:43:02 +000038 throw "Operand '" + Name + "' cannot have multiple constraints!";
Chris Lattnera7d479c2010-02-10 01:45:28 +000039 I->OperandList[Op.first].Constraints[Op.second] =
40 CodeGenInstruction::ConstraintInfo::getEarlyClobber();
Jim Grosbach06801722009-12-16 19:43:02 +000041 return;
42 }
43
44 // Only other constraint is "TIED_TO" for now.
Chris Lattner6cc654b2008-01-06 01:35:39 +000045 std::string::size_type pos = CStr.find_first_of('=');
46 assert(pos != std::string::npos && "Unrecognized constraint");
Jim Grosbach06801722009-12-16 19:43:02 +000047 start = CStr.find_first_not_of(" \t");
Bob Wilsonfd87e6a2009-08-26 22:50:39 +000048 std::string Name = CStr.substr(start, pos - start);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000049
Chris Lattner6cc654b2008-01-06 01:35:39 +000050 // TIED_TO: $src1 = $dst
Jim Grosbach06801722009-12-16 19:43:02 +000051 wpos = Name.find_first_of(" \t");
Chris Lattner6cc654b2008-01-06 01:35:39 +000052 if (wpos == std::string::npos)
53 throw "Illegal format for tied-to constraint: '" + CStr + "'";
54 std::string DestOpName = Name.substr(0, wpos);
55 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000056
Chris Lattner6cc654b2008-01-06 01:35:39 +000057 Name = CStr.substr(pos+1);
58 wpos = Name.find_first_not_of(" \t");
59 if (wpos == std::string::npos)
60 throw "Illegal format for tied-to constraint: '" + CStr + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000061
Chris Lattner6cc654b2008-01-06 01:35:39 +000062 std::pair<unsigned,unsigned> SrcOp =
63 I->ParseOperandName(Name.substr(wpos), false);
64 if (SrcOp > DestOp)
65 throw "Illegal tied-to operand constraint '" + CStr + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000066
67
Chris Lattner6cc654b2008-01-06 01:35:39 +000068 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000069
Chris Lattnera7d479c2010-02-10 01:45:28 +000070 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].isNone())
Chris Lattner6cc654b2008-01-06 01:35:39 +000071 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
Chris Lattnera7d479c2010-02-10 01:45:28 +000072 I->OperandList[DestOp.first].Constraints[DestOp.second] =
73 CodeGenInstruction::ConstraintInfo::getTied(FlatOpNo);
Chris Lattner6cc654b2008-01-06 01:35:39 +000074}
75
76static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
77 // Make sure the constraints list for each operand is large enough to hold
78 // constraint info, even if none is present.
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000079 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
Chris Lattner6cc654b2008-01-06 01:35:39 +000080 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000081
Chris Lattner6cc654b2008-01-06 01:35:39 +000082 if (CStr.empty()) return;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000083
Chris Lattner6cc654b2008-01-06 01:35:39 +000084 const std::string delims(",");
85 std::string::size_type bidx, eidx;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000086
Chris Lattner6cc654b2008-01-06 01:35:39 +000087 bidx = CStr.find_first_not_of(delims);
88 while (bidx != std::string::npos) {
89 eidx = CStr.find_first_of(delims, bidx);
90 if (eidx == std::string::npos)
91 eidx = CStr.length();
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000092
Bob Wilsonfd87e6a2009-08-26 22:50:39 +000093 ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
Chris Lattner6cc654b2008-01-06 01:35:39 +000094 bidx = CStr.find_first_not_of(delims, eidx);
95 }
96}
97
Chris Lattnerf7808112010-11-01 02:15:23 +000098CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R) {
Chris Lattner6cc654b2008-01-06 01:35:39 +000099 Namespace = R->getValueAsString("Namespace");
Chris Lattnerf7808112010-11-01 02:15:23 +0000100 AsmString = R->getValueAsString("AsmString");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000101
102 isReturn = R->getValueAsBit("isReturn");
103 isBranch = R->getValueAsBit("isBranch");
104 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
Bill Wendling73739d02010-08-08 01:49:35 +0000105 isCompare = R->getValueAsBit("isCompare");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000106 isBarrier = R->getValueAsBit("isBarrier");
107 isCall = R->getValueAsBit("isCall");
Dan Gohman15511cf2008-12-03 18:15:48 +0000108 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
Chris Lattnerdcc8b4f2008-01-08 18:05:21 +0000109 mayLoad = R->getValueAsBit("mayLoad");
Chris Lattner2e48a702008-01-06 08:36:04 +0000110 mayStore = R->getValueAsBit("mayStore");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000111 isPredicable = R->getValueAsBit("isPredicable");
112 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
113 isCommutable = R->getValueAsBit("isCommutable");
114 isTerminator = R->getValueAsBit("isTerminator");
115 isReMaterializable = R->getValueAsBit("isReMaterializable");
116 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Dan Gohman533297b2009-10-29 18:10:34 +0000117 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000118 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
119 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Chris Lattnerba7e7562008-01-10 07:59:24 +0000120 hasSideEffects = R->getValueAsBit("hasSideEffects");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000121 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
Bill Wendling8370d382008-05-28 22:54:52 +0000122 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
Evan Cheng799d6972009-10-01 08:21:18 +0000123 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
124 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000125 hasOptionalDef = false;
Chris Lattner8f707e12008-01-07 05:19:29 +0000126 isVariadic = false;
Chris Lattnerf506b6b2010-03-18 21:42:03 +0000127 ImplicitDefs = R->getValueAsListOfDefs("Defs");
128 ImplicitUses = R->getValueAsListOfDefs("Uses");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000129
Dan Gohmanbc9d98b2010-02-27 23:47:46 +0000130 if (neverHasSideEffects + hasSideEffects > 1)
Chris Lattnerba7e7562008-01-10 07:59:24 +0000131 throw R->getName() + ": multiple conflicting side-effect flags set!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000132
Chris Lattnerf55eed22010-03-18 21:07:39 +0000133 DagInit *OutDI = R->getValueAsDag("OutOperandList");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000134
Chris Lattnerf55eed22010-03-18 21:07:39 +0000135 if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +0000136 if (Init->getDef()->getName() != "outs")
Chris Lattnercedef1c2010-03-18 20:50:52 +0000137 throw R->getName() + ": invalid def name for output list: use 'outs'";
138 } else
139 throw R->getName() + ": invalid output list: use 'outs'";
140
Chris Lattnerf55eed22010-03-18 21:07:39 +0000141 NumDefs = OutDI->getNumArgs();
Chris Lattnercedef1c2010-03-18 20:50:52 +0000142
Chris Lattnerf55eed22010-03-18 21:07:39 +0000143 DagInit *InDI = R->getValueAsDag("InOperandList");
144 if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +0000145 if (Init->getDef()->getName() != "ins")
Chris Lattnercedef1c2010-03-18 20:50:52 +0000146 throw R->getName() + ": invalid def name for input list: use 'ins'";
147 } else
148 throw R->getName() + ": invalid input list: use 'ins'";
149
Chris Lattner6cc654b2008-01-06 01:35:39 +0000150 unsigned MIOperandNo = 0;
151 std::set<std::string> OperandNames;
Chris Lattnerf55eed22010-03-18 21:07:39 +0000152 for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
153 Init *ArgInit;
154 std::string ArgName;
155 if (i < NumDefs) {
156 ArgInit = OutDI->getArg(i);
157 ArgName = OutDI->getArgName(i);
158 } else {
159 ArgInit = InDI->getArg(i-NumDefs);
160 ArgName = InDI->getArgName(i-NumDefs);
161 }
162
163 DefInit *Arg = dynamic_cast<DefInit*>(ArgInit);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000164 if (!Arg)
165 throw "Illegal operand for the '" + R->getName() + "' instruction!";
166
167 Record *Rec = Arg->getDef();
168 std::string PrintMethod = "printOperand";
Jim Grosbach5013f742010-10-12 22:21:57 +0000169 std::string EncoderMethod;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000170 unsigned NumOps = 1;
171 DagInit *MIOpInfo = 0;
172 if (Rec->isSubClassOf("Operand")) {
173 PrintMethod = Rec->getValueAsString("PrintMethod");
Jim Grosbach5013f742010-10-12 22:21:57 +0000174 // If there is an explicit encoder method, use it.
175 if (Rec->getValue("EncoderMethod"))
176 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000177 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000178
Chris Lattner6cc654b2008-01-06 01:35:39 +0000179 // Verify that MIOpInfo has an 'ops' root value.
180 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
181 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
182 ->getDef()->getName() != "ops")
183 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
184 "'\n";
185
186 // If we have MIOpInfo, then we have #operands equal to number of entries
187 // in MIOperandInfo.
188 if (unsigned NumArgs = MIOpInfo->getNumArgs())
189 NumOps = NumArgs;
190
191 if (Rec->isSubClassOf("PredicateOperand"))
192 isPredicable = true;
193 else if (Rec->isSubClassOf("OptionalDefOperand"))
194 hasOptionalDef = true;
195 } else if (Rec->getName() == "variable_ops") {
Chris Lattner8f707e12008-01-07 05:19:29 +0000196 isVariadic = true;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000197 continue;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000198 } else if (!Rec->isSubClassOf("RegisterClass") &&
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000199 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
Chris Lattner6cc654b2008-01-06 01:35:39 +0000200 throw "Unknown operand class '" + Rec->getName() +
Matthijs Kooijman677fbfa2008-10-27 15:59:43 +0000201 "' in '" + R->getName() + "' instruction!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000202
203 // Check that the operand has a name and that it's unique.
Chris Lattnerf55eed22010-03-18 21:07:39 +0000204 if (ArgName.empty())
Chris Lattner6cc654b2008-01-06 01:35:39 +0000205 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
206 " has no name!";
Chris Lattnerf55eed22010-03-18 21:07:39 +0000207 if (!OperandNames.insert(ArgName).second)
Chris Lattner6cc654b2008-01-06 01:35:39 +0000208 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
209 " has the same name as a previous operand!";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000210
Jim Grosbach5013f742010-10-12 22:21:57 +0000211 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
Chris Lattner6cc654b2008-01-06 01:35:39 +0000212 MIOperandNo, NumOps, MIOpInfo));
213 MIOperandNo += NumOps;
214 }
215
216 // Parse Constraints.
217 ParseConstraints(R->getValueAsString("Constraints"), this);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000218
Chris Lattner6cc654b2008-01-06 01:35:39 +0000219 // Parse the DisableEncoding field.
220 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
221 while (1) {
Benjamin Kramerd4f19592010-01-11 18:03:24 +0000222 std::string OpName;
223 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000224 if (OpName.empty()) break;
225
226 // Figure out which operand this is.
227 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
228
229 // Mark the operand as not-to-be encoded.
230 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
231 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
232 OperandList[Op.first].DoNotEncode[Op.second] = true;
233 }
234}
235
Chris Lattner6cc654b2008-01-06 01:35:39 +0000236/// getOperandNamed - Return the index of the operand with the specified
237/// non-empty name. If the instruction does not have an operand with the
238/// specified name, throw an exception.
239///
240unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000241 unsigned OpIdx;
242 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000243 throw "Instruction '" + TheDef->getName() +
244 "' does not have an operand named '$" + Name + "'!";
245}
246
Jim Grosbach01855072010-10-11 18:25:51 +0000247/// hasOperandNamed - Query whether the instruction has an operand of the
248/// given name. If so, return true and set OpIdx to the index of the
249/// operand. Otherwise, return false.
250bool CodeGenInstruction::hasOperandNamed(const std::string &Name,
251 unsigned &OpIdx) const {
252 assert(!Name.empty() && "Cannot search for operand with no name!");
253 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
254 if (OperandList[i].Name == Name) {
255 OpIdx = i;
256 return true;
257 }
258 return false;
259}
260
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000261std::pair<unsigned,unsigned>
Chris Lattner6cc654b2008-01-06 01:35:39 +0000262CodeGenInstruction::ParseOperandName(const std::string &Op,
263 bool AllowWholeOp) {
264 if (Op.empty() || Op[0] != '$')
265 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000266
Chris Lattner6cc654b2008-01-06 01:35:39 +0000267 std::string OpName = Op.substr(1);
268 std::string SubOpName;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000269
Chris Lattner6cc654b2008-01-06 01:35:39 +0000270 // Check to see if this is $foo.bar.
271 std::string::size_type DotIdx = OpName.find_first_of(".");
272 if (DotIdx != std::string::npos) {
273 SubOpName = OpName.substr(DotIdx+1);
274 if (SubOpName.empty())
275 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
276 OpName = OpName.substr(0, DotIdx);
277 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000278
Chris Lattner6cc654b2008-01-06 01:35:39 +0000279 unsigned OpIdx = getOperandNamed(OpName);
280
281 if (SubOpName.empty()) { // If no suboperand name was specified:
282 // If one was needed, throw.
283 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
284 SubOpName.empty())
285 throw TheDef->getName() + ": Illegal to refer to"
286 " whole operand part of complex operand '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000287
Chris Lattner6cc654b2008-01-06 01:35:39 +0000288 // Otherwise, return the operand.
289 return std::make_pair(OpIdx, 0U);
290 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000291
Chris Lattner6cc654b2008-01-06 01:35:39 +0000292 // Find the suboperand number involved.
293 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
294 if (MIOpInfo == 0)
295 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000296
Chris Lattner6cc654b2008-01-06 01:35:39 +0000297 // Find the operand with the right name.
298 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
299 if (MIOpInfo->getArgName(i) == SubOpName)
300 return std::make_pair(OpIdx, i);
301
302 // Otherwise, didn't find it!
303 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
304}
Chris Lattner9414ae52010-03-27 20:09:24 +0000305
306
307/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
308/// implicit def and it has a known VT, return the VT, otherwise return
309/// MVT::Other.
310MVT::SimpleValueType CodeGenInstruction::
311HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
312 if (ImplicitDefs.empty()) return MVT::Other;
313
314 // Check to see if the first implicit def has a resolvable type.
315 Record *FirstImplicitDef = ImplicitDefs[0];
316 assert(FirstImplicitDef->isSubClassOf("Register"));
317 const std::vector<MVT::SimpleValueType> &RegVTs =
318 TargetInfo.getRegisterVTs(FirstImplicitDef);
319 if (RegVTs.size() == 1)
320 return RegVTs[0];
321 return MVT::Other;
322}
323
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000324
325/// FlattenAsmStringVariants - Flatten the specified AsmString to only
326/// include text from the specified variant, returning the new string.
327std::string CodeGenInstruction::
328FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
329 std::string Res = "";
330
331 for (;;) {
332 // Find the start of the next variant string.
333 size_t VariantsStart = 0;
334 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
335 if (Cur[VariantsStart] == '{' &&
336 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
337 Cur[VariantsStart-1] != '\\')))
338 break;
339
340 // Add the prefix to the result.
341 Res += Cur.slice(0, VariantsStart);
342 if (VariantsStart == Cur.size())
343 break;
344
345 ++VariantsStart; // Skip the '{'.
346
347 // Scan to the end of the variants string.
348 size_t VariantsEnd = VariantsStart;
349 unsigned NestedBraces = 1;
350 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
351 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
352 if (--NestedBraces == 0)
353 break;
354 } else if (Cur[VariantsEnd] == '{')
355 ++NestedBraces;
356 }
357
358 // Select the Nth variant (or empty).
359 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
360 for (unsigned i = 0; i != Variant; ++i)
361 Selection = Selection.split('|').second;
362 Res += Selection.split('|').first;
363
364 assert(VariantsEnd != Cur.size() &&
365 "Unterminated variants in assembly string!");
366 Cur = Cur.substr(VariantsEnd + 1);
367 }
368
369 return Res;
370}
371
372