CodeGenInstruction.cpp revision 5b55ff0c1555031c1c85f88d67c3b566750a9319
1//===- 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 "CodeGenTarget.h"
16#include "Record.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/ADT/STLExtras.h"
19#include <set>
20using namespace llvm;
21
22static 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);
35
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();
41    return;
42  }
43
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);
49
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);
56
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 + "'";
61
62  std::pair<unsigned,unsigned> SrcOp =
63  I->ParseOperandName(Name.substr(wpos), false);
64  if (SrcOp > DestOp)
65    throw "Illegal tied-to operand constraint '" + CStr + "'";
66
67
68  unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
69
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);
74}
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.
79  for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
80    I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
81
82  if (CStr.empty()) return;
83
84  const std::string delims(",");
85  std::string::size_type bidx, eidx;
86
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)
91      eidx = CStr.length();
92
93    ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
94    bidx = CStr.find_first_not_of(delims, eidx);
95  }
96}
97
98CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
99  : TheDef(R), AsmString(AsmStr) {
100  Namespace = R->getValueAsString("Namespace");
101
102  isReturn     = R->getValueAsBit("isReturn");
103  isBranch     = R->getValueAsBit("isBranch");
104  isIndirectBranch = R->getValueAsBit("isIndirectBranch");
105  isBarrier    = R->getValueAsBit("isBarrier");
106  isCall       = R->getValueAsBit("isCall");
107  canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
108  mayLoad      = R->getValueAsBit("mayLoad");
109  mayStore     = R->getValueAsBit("mayStore");
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");
116  usesCustomInserter = R->getValueAsBit("usesCustomInserter");
117  hasCtrlDep   = R->getValueAsBit("hasCtrlDep");
118  isNotDuplicable = R->getValueAsBit("isNotDuplicable");
119  hasSideEffects = R->getValueAsBit("hasSideEffects");
120  neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
121  isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
122  hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
123  hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
124  hasOptionalDef = false;
125  isVariadic = false;
126  ImplicitDefs = R->getValueAsListOfDefs("Defs");
127  ImplicitUses = R->getValueAsListOfDefs("Uses");
128
129  if (neverHasSideEffects + hasSideEffects > 1)
130    throw R->getName() + ": multiple conflicting side-effect flags set!";
131
132  DagInit *OutDI = R->getValueAsDag("OutOperandList");
133
134  if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
135    if (Init->getDef()->getName() != "outs")
136      throw R->getName() + ": invalid def name for output list: use 'outs'";
137  } else
138    throw R->getName() + ": invalid output list: use 'outs'";
139
140  NumDefs = OutDI->getNumArgs();
141
142  DagInit *InDI = R->getValueAsDag("InOperandList");
143  if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
144    if (Init->getDef()->getName() != "ins")
145      throw R->getName() + ": invalid def name for input list: use 'ins'";
146  } else
147    throw R->getName() + ": invalid input list: use 'ins'";
148
149  unsigned MIOperandNo = 0;
150  std::set<std::string> OperandNames;
151  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);
163    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");
173
174      // 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") {
191      isVariadic = true;
192      continue;
193    } else if (!Rec->isSubClassOf("RegisterClass") &&
194               Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
195      throw "Unknown operand class '" + Rec->getName() +
196            "' in '" + R->getName() + "' instruction!";
197
198    // Check that the operand has a name and that it's unique.
199    if (ArgName.empty())
200      throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
201        " has no name!";
202    if (!OperandNames.insert(ArgName).second)
203      throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
204        " has the same name as a previous operand!";
205
206    OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod,
207                                      MIOperandNo, NumOps, MIOpInfo));
208    MIOperandNo += NumOps;
209  }
210
211  // Parse Constraints.
212  ParseConstraints(R->getValueAsString("Constraints"), this);
213
214  // Parse the DisableEncoding field.
215  std::string DisableEncoding = R->getValueAsString("DisableEncoding");
216  while (1) {
217    std::string OpName;
218    tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
219    if (OpName.empty()) break;
220
221    // Figure out which operand this is.
222    std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
223
224    // Mark the operand as not-to-be encoded.
225    if (Op.second >= OperandList[Op.first].DoNotEncode.size())
226      OperandList[Op.first].DoNotEncode.resize(Op.second+1);
227    OperandList[Op.first].DoNotEncode[Op.second] = true;
228  }
229}
230
231/// getOperandNamed - Return the index of the operand with the specified
232/// non-empty name.  If the instruction does not have an operand with the
233/// specified name, throw an exception.
234///
235unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
236  assert(!Name.empty() && "Cannot search for operand with no name!");
237  for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
238    if (OperandList[i].Name == Name) return i;
239  throw "Instruction '" + TheDef->getName() +
240        "' does not have an operand named '$" + Name + "'!";
241}
242
243std::pair<unsigned,unsigned>
244CodeGenInstruction::ParseOperandName(const std::string &Op,
245                                     bool AllowWholeOp) {
246  if (Op.empty() || Op[0] != '$')
247    throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
248
249  std::string OpName = Op.substr(1);
250  std::string SubOpName;
251
252  // Check to see if this is $foo.bar.
253  std::string::size_type DotIdx = OpName.find_first_of(".");
254  if (DotIdx != std::string::npos) {
255    SubOpName = OpName.substr(DotIdx+1);
256    if (SubOpName.empty())
257      throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
258    OpName = OpName.substr(0, DotIdx);
259  }
260
261  unsigned OpIdx = getOperandNamed(OpName);
262
263  if (SubOpName.empty()) {  // If no suboperand name was specified:
264    // If one was needed, throw.
265    if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
266        SubOpName.empty())
267      throw TheDef->getName() + ": Illegal to refer to"
268            " whole operand part of complex operand '" + Op + "'";
269
270    // Otherwise, return the operand.
271    return std::make_pair(OpIdx, 0U);
272  }
273
274  // Find the suboperand number involved.
275  DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
276  if (MIOpInfo == 0)
277    throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
278
279  // Find the operand with the right name.
280  for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
281    if (MIOpInfo->getArgName(i) == SubOpName)
282      return std::make_pair(OpIdx, i);
283
284  // Otherwise, didn't find it!
285  throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
286}
287
288
289/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
290/// implicit def and it has a known VT, return the VT, otherwise return
291/// MVT::Other.
292MVT::SimpleValueType CodeGenInstruction::
293HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
294  if (ImplicitDefs.empty()) return MVT::Other;
295
296  // Check to see if the first implicit def has a resolvable type.
297  Record *FirstImplicitDef = ImplicitDefs[0];
298  assert(FirstImplicitDef->isSubClassOf("Register"));
299  const std::vector<MVT::SimpleValueType> &RegVTs =
300    TargetInfo.getRegisterVTs(FirstImplicitDef);
301  if (RegVTs.size() == 1)
302    return RegVTs[0];
303  return MVT::Other;
304}
305
306