1 //===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CodeGenInstruction class.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenInstruction.h"
15 #include "CodeGenTarget.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/STLExtras.h"
22 static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
23 // 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);
36 // Build the string for the operand
37 if (!I->OperandList[Op.first].Constraints[Op.second].isNone())
38 throw "Operand '" + Name + "' cannot have multiple constraints!";
39 I->OperandList[Op.first].Constraints[Op.second] =
40 CodeGenInstruction::ConstraintInfo::getEarlyClobber();
44 // Only other constraint is "TIED_TO" for now.
45 std::string::size_type pos = CStr.find_first_of('=');
46 assert(pos != std::string::npos && "Unrecognized constraint");
47 start = CStr.find_first_not_of(" \t");
48 std::string Name = CStr.substr(start, pos - start);
50 // TIED_TO: $src1 = $dst
51 wpos = Name.find_first_of(" \t");
52 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);
57 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 + "'";
62 std::pair<unsigned,unsigned> SrcOp =
63 I->ParseOperandName(Name.substr(wpos), false);
65 throw "Illegal tied-to operand constraint '" + CStr + "'";
68 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
70 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].isNone())
71 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
72 I->OperandList[DestOp.first].Constraints[DestOp.second] =
73 CodeGenInstruction::ConstraintInfo::getTied(FlatOpNo);
76 static 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.
79 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
80 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
82 if (CStr.empty()) return;
84 const std::string delims(",");
85 std::string::size_type bidx, eidx;
87 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)
93 ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
94 bidx = CStr.find_first_not_of(delims, eidx);
98 CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R) {
99 Namespace = R->getValueAsString("Namespace");
100 AsmString = R->getValueAsString("AsmString");
102 isReturn = R->getValueAsBit("isReturn");
103 isBranch = R->getValueAsBit("isBranch");
104 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
105 isCompare = R->getValueAsBit("isCompare");
106 isBarrier = R->getValueAsBit("isBarrier");
107 isCall = R->getValueAsBit("isCall");
108 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
109 mayLoad = R->getValueAsBit("mayLoad");
110 mayStore = R->getValueAsBit("mayStore");
111 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");
117 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
118 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
119 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
120 hasSideEffects = R->getValueAsBit("hasSideEffects");
121 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
122 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
123 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
124 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
125 hasOptionalDef = false;
127 ImplicitDefs = R->getValueAsListOfDefs("Defs");
128 ImplicitUses = R->getValueAsListOfDefs("Uses");
130 if (neverHasSideEffects + hasSideEffects > 1)
131 throw R->getName() + ": multiple conflicting side-effect flags set!";
133 DagInit *OutDI = R->getValueAsDag("OutOperandList");
135 if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
136 if (Init->getDef()->getName() != "outs")
137 throw R->getName() + ": invalid def name for output list: use 'outs'";
139 throw R->getName() + ": invalid output list: use 'outs'";
141 NumDefs = OutDI->getNumArgs();
143 DagInit *InDI = R->getValueAsDag("InOperandList");
144 if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
145 if (Init->getDef()->getName() != "ins")
146 throw R->getName() + ": invalid def name for input list: use 'ins'";
148 throw R->getName() + ": invalid input list: use 'ins'";
150 unsigned MIOperandNo = 0;
151 std::set<std::string> OperandNames;
152 for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
156 ArgInit = OutDI->getArg(i);
157 ArgName = OutDI->getArgName(i);
159 ArgInit = InDI->getArg(i-NumDefs);
160 ArgName = InDI->getArgName(i-NumDefs);
163 DefInit *Arg = dynamic_cast<DefInit*>(ArgInit);
165 throw "Illegal operand for the '" + R->getName() + "' instruction!";
167 Record *Rec = Arg->getDef();
168 std::string PrintMethod = "printOperand";
169 std::string EncoderMethod;
171 DagInit *MIOpInfo = 0;
172 if (Rec->isSubClassOf("Operand")) {
173 PrintMethod = Rec->getValueAsString("PrintMethod");
174 // If there is an explicit encoder method, use it.
175 if (Rec->getValue("EncoderMethod"))
176 EncoderMethod = Rec->getValueAsString("EncoderMethod");
177 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
179 // 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() +
186 // If we have MIOpInfo, then we have #operands equal to number of entries
188 if (unsigned NumArgs = MIOpInfo->getNumArgs())
191 if (Rec->isSubClassOf("PredicateOperand"))
193 else if (Rec->isSubClassOf("OptionalDefOperand"))
194 hasOptionalDef = true;
195 } else if (Rec->getName() == "variable_ops") {
198 } else if (!Rec->isSubClassOf("RegisterClass") &&
199 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
200 throw "Unknown operand class '" + Rec->getName() +
201 "' in '" + R->getName() + "' instruction!";
203 // Check that the operand has a name and that it's unique.
205 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
207 if (!OperandNames.insert(ArgName).second)
208 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
209 " has the same name as a previous operand!";
211 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
212 MIOperandNo, NumOps, MIOpInfo));
213 MIOperandNo += NumOps;
216 // Parse Constraints.
217 ParseConstraints(R->getValueAsString("Constraints"), this);
219 // Parse the DisableEncoding field.
220 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
223 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
224 if (OpName.empty()) break;
226 // Figure out which operand this is.
227 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
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;
236 /// 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.
240 unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
242 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
243 throw "Instruction '" + TheDef->getName() +
244 "' does not have an operand named '$" + Name + "'!";
247 /// 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.
250 bool 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) {
261 std::pair<unsigned,unsigned>
262 CodeGenInstruction::ParseOperandName(const std::string &Op,
264 if (Op.empty() || Op[0] != '$')
265 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
267 std::string OpName = Op.substr(1);
268 std::string SubOpName;
270 // 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);
279 unsigned OpIdx = getOperandNamed(OpName);
281 if (SubOpName.empty()) { // If no suboperand name was specified:
282 // If one was needed, throw.
283 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
285 throw TheDef->getName() + ": Illegal to refer to"
286 " whole operand part of complex operand '" + Op + "'";
288 // Otherwise, return the operand.
289 return std::make_pair(OpIdx, 0U);
292 // Find the suboperand number involved.
293 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
295 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
297 // 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);
302 // Otherwise, didn't find it!
303 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
307 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
308 /// implicit def and it has a known VT, return the VT, otherwise return
310 MVT::SimpleValueType CodeGenInstruction::
311 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
312 if (ImplicitDefs.empty()) return MVT::Other;
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)
325 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
326 /// include text from the specified variant, returning the new string.
327 std::string CodeGenInstruction::
328 FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
329 std::string Res = "";
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] != '\\')))
340 // Add the prefix to the result.
341 Res += Cur.slice(0, VariantsStart);
342 if (VariantsStart == Cur.size())
345 ++VariantsStart; // Skip the '{'.
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)
354 } else if (Cur[VariantsEnd] == '{')
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;
364 assert(VariantsEnd != Cur.size() &&
365 "Unterminated variants in assembly string!");
366 Cur = Cur.substr(VariantsEnd + 1);