blob: eea55618721e51d7bbb79aa85cf2e7c5a0cd925f [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"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000018#include <set>
19using namespace llvm;
20
21static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
Jim Grosbach06801722009-12-16 19:43:02 +000022 // EARLY_CLOBBER: @early $reg
23 std::string::size_type wpos = CStr.find_first_of(" \t");
24 std::string::size_type start = CStr.find_first_not_of(" \t");
25 std::string Tok = CStr.substr(start, wpos - start);
26 if (Tok == "@earlyclobber") {
27 std::string Name = CStr.substr(wpos+1);
28 wpos = Name.find_first_not_of(" \t");
29 if (wpos == std::string::npos)
30 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
31 Name = Name.substr(wpos);
32 std::pair<unsigned,unsigned> Op =
33 I->ParseOperandName(Name, false);
34
35 // Build the string for the operand
Chris Lattnera7d479c2010-02-10 01:45:28 +000036 if (!I->OperandList[Op.first].Constraints[Op.second].isNone())
Jim Grosbach06801722009-12-16 19:43:02 +000037 throw "Operand '" + Name + "' cannot have multiple constraints!";
Chris Lattnera7d479c2010-02-10 01:45:28 +000038 I->OperandList[Op.first].Constraints[Op.second] =
39 CodeGenInstruction::ConstraintInfo::getEarlyClobber();
Jim Grosbach06801722009-12-16 19:43:02 +000040 return;
41 }
42
43 // Only other constraint is "TIED_TO" for now.
Chris Lattner6cc654b2008-01-06 01:35:39 +000044 std::string::size_type pos = CStr.find_first_of('=');
45 assert(pos != std::string::npos && "Unrecognized constraint");
Jim Grosbach06801722009-12-16 19:43:02 +000046 start = CStr.find_first_not_of(" \t");
Bob Wilsonfd87e6a2009-08-26 22:50:39 +000047 std::string Name = CStr.substr(start, pos - start);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000048
Chris Lattner6cc654b2008-01-06 01:35:39 +000049 // TIED_TO: $src1 = $dst
Jim Grosbach06801722009-12-16 19:43:02 +000050 wpos = Name.find_first_of(" \t");
Chris Lattner6cc654b2008-01-06 01:35:39 +000051 if (wpos == std::string::npos)
52 throw "Illegal format for tied-to constraint: '" + CStr + "'";
53 std::string DestOpName = Name.substr(0, wpos);
54 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000055
Chris Lattner6cc654b2008-01-06 01:35:39 +000056 Name = CStr.substr(pos+1);
57 wpos = Name.find_first_not_of(" \t");
58 if (wpos == std::string::npos)
59 throw "Illegal format for tied-to constraint: '" + CStr + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000060
Chris Lattner6cc654b2008-01-06 01:35:39 +000061 std::pair<unsigned,unsigned> SrcOp =
62 I->ParseOperandName(Name.substr(wpos), false);
63 if (SrcOp > DestOp)
64 throw "Illegal tied-to operand constraint '" + CStr + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000065
66
Chris Lattner6cc654b2008-01-06 01:35:39 +000067 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000068
Chris Lattnera7d479c2010-02-10 01:45:28 +000069 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].isNone())
Chris Lattner6cc654b2008-01-06 01:35:39 +000070 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
Chris Lattnera7d479c2010-02-10 01:45:28 +000071 I->OperandList[DestOp.first].Constraints[DestOp.second] =
72 CodeGenInstruction::ConstraintInfo::getTied(FlatOpNo);
Chris Lattner6cc654b2008-01-06 01:35:39 +000073}
74
75static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
76 // Make sure the constraints list for each operand is large enough to hold
77 // constraint info, even if none is present.
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000078 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
Chris Lattner6cc654b2008-01-06 01:35:39 +000079 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000080
Chris Lattner6cc654b2008-01-06 01:35:39 +000081 if (CStr.empty()) return;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000082
Chris Lattner6cc654b2008-01-06 01:35:39 +000083 const std::string delims(",");
84 std::string::size_type bidx, eidx;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000085
Chris Lattner6cc654b2008-01-06 01:35:39 +000086 bidx = CStr.find_first_not_of(delims);
87 while (bidx != std::string::npos) {
88 eidx = CStr.find_first_of(delims, bidx);
89 if (eidx == std::string::npos)
90 eidx = CStr.length();
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000091
Bob Wilsonfd87e6a2009-08-26 22:50:39 +000092 ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
Chris Lattner6cc654b2008-01-06 01:35:39 +000093 bidx = CStr.find_first_not_of(delims, eidx);
94 }
95}
96
97CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
98 : TheDef(R), AsmString(AsmStr) {
Chris Lattner6cc654b2008-01-06 01:35:39 +000099 Namespace = R->getValueAsString("Namespace");
100
101 isReturn = R->getValueAsBit("isReturn");
102 isBranch = R->getValueAsBit("isBranch");
103 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
104 isBarrier = R->getValueAsBit("isBarrier");
105 isCall = R->getValueAsBit("isCall");
Dan Gohman15511cf2008-12-03 18:15:48 +0000106 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
Chris Lattnerdcc8b4f2008-01-08 18:05:21 +0000107 mayLoad = R->getValueAsBit("mayLoad");
Chris Lattner2e48a702008-01-06 08:36:04 +0000108 mayStore = R->getValueAsBit("mayStore");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000109 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
110 isPredicable = R->getValueAsBit("isPredicable");
111 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
112 isCommutable = R->getValueAsBit("isCommutable");
113 isTerminator = R->getValueAsBit("isTerminator");
114 isReMaterializable = R->getValueAsBit("isReMaterializable");
115 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Dan Gohman533297b2009-10-29 18:10:34 +0000116 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000117 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
118 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Chris Lattnerba7e7562008-01-10 07:59:24 +0000119 hasSideEffects = R->getValueAsBit("hasSideEffects");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000120 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
Bill Wendling8370d382008-05-28 22:54:52 +0000121 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
Evan Cheng799d6972009-10-01 08:21:18 +0000122 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
123 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000124 hasOptionalDef = false;
Chris Lattner8f707e12008-01-07 05:19:29 +0000125 isVariadic = false;
Chris Lattnerf506b6b2010-03-18 21:42:03 +0000126 ImplicitDefs = R->getValueAsListOfDefs("Defs");
127 ImplicitUses = R->getValueAsListOfDefs("Uses");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000128
Dan Gohmanbc9d98b2010-02-27 23:47:46 +0000129 if (neverHasSideEffects + hasSideEffects > 1)
Chris Lattnerba7e7562008-01-10 07:59:24 +0000130 throw R->getName() + ": multiple conflicting side-effect flags set!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000131
Chris Lattnerf55eed22010-03-18 21:07:39 +0000132 DagInit *OutDI = R->getValueAsDag("OutOperandList");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000133
Chris Lattnerf55eed22010-03-18 21:07:39 +0000134 if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +0000135 if (Init->getDef()->getName() != "outs")
Chris Lattnercedef1c2010-03-18 20:50:52 +0000136 throw R->getName() + ": invalid def name for output list: use 'outs'";
137 } else
138 throw R->getName() + ": invalid output list: use 'outs'";
139
Chris Lattnerf55eed22010-03-18 21:07:39 +0000140 NumDefs = OutDI->getNumArgs();
Chris Lattnercedef1c2010-03-18 20:50:52 +0000141
Chris Lattnerf55eed22010-03-18 21:07:39 +0000142 DagInit *InDI = R->getValueAsDag("InOperandList");
143 if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +0000144 if (Init->getDef()->getName() != "ins")
Chris Lattnercedef1c2010-03-18 20:50:52 +0000145 throw R->getName() + ": invalid def name for input list: use 'ins'";
146 } else
147 throw R->getName() + ": invalid input list: use 'ins'";
148
Chris Lattner6cc654b2008-01-06 01:35:39 +0000149 unsigned MIOperandNo = 0;
150 std::set<std::string> OperandNames;
Chris Lattnerf55eed22010-03-18 21:07:39 +0000151 for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
152 Init *ArgInit;
153 std::string ArgName;
154 if (i < NumDefs) {
155 ArgInit = OutDI->getArg(i);
156 ArgName = OutDI->getArgName(i);
157 } else {
158 ArgInit = InDI->getArg(i-NumDefs);
159 ArgName = InDI->getArgName(i-NumDefs);
160 }
161
162 DefInit *Arg = dynamic_cast<DefInit*>(ArgInit);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000163 if (!Arg)
164 throw "Illegal operand for the '" + R->getName() + "' instruction!";
165
166 Record *Rec = Arg->getDef();
167 std::string PrintMethod = "printOperand";
168 unsigned NumOps = 1;
169 DagInit *MIOpInfo = 0;
170 if (Rec->isSubClassOf("Operand")) {
171 PrintMethod = Rec->getValueAsString("PrintMethod");
172 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000173
Chris Lattner6cc654b2008-01-06 01:35:39 +0000174 // Verify that MIOpInfo has an 'ops' root value.
175 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
176 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
177 ->getDef()->getName() != "ops")
178 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
179 "'\n";
180
181 // If we have MIOpInfo, then we have #operands equal to number of entries
182 // in MIOperandInfo.
183 if (unsigned NumArgs = MIOpInfo->getNumArgs())
184 NumOps = NumArgs;
185
186 if (Rec->isSubClassOf("PredicateOperand"))
187 isPredicable = true;
188 else if (Rec->isSubClassOf("OptionalDefOperand"))
189 hasOptionalDef = true;
190 } else if (Rec->getName() == "variable_ops") {
Chris Lattner8f707e12008-01-07 05:19:29 +0000191 isVariadic = true;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000192 continue;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000193 } else if (!Rec->isSubClassOf("RegisterClass") &&
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000194 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
Chris Lattner6cc654b2008-01-06 01:35:39 +0000195 throw "Unknown operand class '" + Rec->getName() +
Matthijs Kooijman677fbfa2008-10-27 15:59:43 +0000196 "' in '" + R->getName() + "' instruction!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000197
198 // Check that the operand has a name and that it's unique.
Chris Lattnerf55eed22010-03-18 21:07:39 +0000199 if (ArgName.empty())
Chris Lattner6cc654b2008-01-06 01:35:39 +0000200 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
201 " has no name!";
Chris Lattnerf55eed22010-03-18 21:07:39 +0000202 if (!OperandNames.insert(ArgName).second)
Chris Lattner6cc654b2008-01-06 01:35:39 +0000203 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
204 " has the same name as a previous operand!";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000205
Chris Lattnerf55eed22010-03-18 21:07:39 +0000206 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod,
Chris Lattner6cc654b2008-01-06 01:35:39 +0000207 MIOperandNo, NumOps, MIOpInfo));
208 MIOperandNo += NumOps;
209 }
210
211 // Parse Constraints.
212 ParseConstraints(R->getValueAsString("Constraints"), this);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000213
Chris Lattner6cc654b2008-01-06 01:35:39 +0000214 // For backward compatibility: isTwoAddress means operand 1 is tied to
215 // operand 0.
216 if (isTwoAddress) {
Chris Lattnera7d479c2010-02-10 01:45:28 +0000217 if (!OperandList[1].Constraints[0].isNone())
Chris Lattner6cc654b2008-01-06 01:35:39 +0000218 throw R->getName() + ": cannot use isTwoAddress property: instruction "
219 "already has constraint set!";
Chris Lattnera7d479c2010-02-10 01:45:28 +0000220 OperandList[1].Constraints[0] =
221 CodeGenInstruction::ConstraintInfo::getTied(0);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000222 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000223
Chris Lattner6cc654b2008-01-06 01:35:39 +0000224 // Parse the DisableEncoding field.
225 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
226 while (1) {
Benjamin Kramerd4f19592010-01-11 18:03:24 +0000227 std::string OpName;
228 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000229 if (OpName.empty()) break;
230
231 // Figure out which operand this is.
232 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
233
234 // Mark the operand as not-to-be encoded.
235 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
236 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
237 OperandList[Op.first].DoNotEncode[Op.second] = true;
238 }
239}
240
Chris Lattner6cc654b2008-01-06 01:35:39 +0000241/// getOperandNamed - Return the index of the operand with the specified
242/// non-empty name. If the instruction does not have an operand with the
243/// specified name, throw an exception.
244///
245unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
246 assert(!Name.empty() && "Cannot search for operand with no name!");
247 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
248 if (OperandList[i].Name == Name) return i;
249 throw "Instruction '" + TheDef->getName() +
250 "' does not have an operand named '$" + Name + "'!";
251}
252
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000253std::pair<unsigned,unsigned>
Chris Lattner6cc654b2008-01-06 01:35:39 +0000254CodeGenInstruction::ParseOperandName(const std::string &Op,
255 bool AllowWholeOp) {
256 if (Op.empty() || Op[0] != '$')
257 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000258
Chris Lattner6cc654b2008-01-06 01:35:39 +0000259 std::string OpName = Op.substr(1);
260 std::string SubOpName;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000261
Chris Lattner6cc654b2008-01-06 01:35:39 +0000262 // Check to see if this is $foo.bar.
263 std::string::size_type DotIdx = OpName.find_first_of(".");
264 if (DotIdx != std::string::npos) {
265 SubOpName = OpName.substr(DotIdx+1);
266 if (SubOpName.empty())
267 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
268 OpName = OpName.substr(0, DotIdx);
269 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000270
Chris Lattner6cc654b2008-01-06 01:35:39 +0000271 unsigned OpIdx = getOperandNamed(OpName);
272
273 if (SubOpName.empty()) { // If no suboperand name was specified:
274 // If one was needed, throw.
275 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
276 SubOpName.empty())
277 throw TheDef->getName() + ": Illegal to refer to"
278 " whole operand part of complex operand '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000279
Chris Lattner6cc654b2008-01-06 01:35:39 +0000280 // Otherwise, return the operand.
281 return std::make_pair(OpIdx, 0U);
282 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000283
Chris Lattner6cc654b2008-01-06 01:35:39 +0000284 // Find the suboperand number involved.
285 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
286 if (MIOpInfo == 0)
287 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000288
Chris Lattner6cc654b2008-01-06 01:35:39 +0000289 // Find the operand with the right name.
290 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
291 if (MIOpInfo->getArgName(i) == SubOpName)
292 return std::make_pair(OpIdx, i);
293
294 // Otherwise, didn't find it!
295 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
296}