CodeGenInstruction.cpp revision 201360
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 "Record.h"
16#include "llvm/ADT/StringExtras.h"
17#include <set>
18using namespace llvm;
19
20static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
21  // EARLY_CLOBBER: @early $reg
22  std::string::size_type wpos = CStr.find_first_of(" \t");
23  std::string::size_type start = CStr.find_first_not_of(" \t");
24  std::string Tok = CStr.substr(start, wpos - start);
25  if (Tok == "@earlyclobber") {
26    std::string Name = CStr.substr(wpos+1);
27    wpos = Name.find_first_not_of(" \t");
28    if (wpos == std::string::npos)
29      throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
30    Name = Name.substr(wpos);
31    std::pair<unsigned,unsigned> Op =
32      I->ParseOperandName(Name, false);
33
34    // Build the string for the operand
35    std::string OpConstraint = "(1 << TOI::EARLY_CLOBBER)";
36    if (!I->OperandList[Op.first].Constraints[Op.second].empty())
37      throw "Operand '" + Name + "' cannot have multiple constraints!";
38    I->OperandList[Op.first].Constraints[Op.second] = OpConstraint;
39    return;
40  }
41
42  // Only other constraint is "TIED_TO" for now.
43  std::string::size_type pos = CStr.find_first_of('=');
44  assert(pos != std::string::npos && "Unrecognized constraint");
45  start = CStr.find_first_not_of(" \t");
46  std::string Name = CStr.substr(start, pos - start);
47
48  // TIED_TO: $src1 = $dst
49  wpos = Name.find_first_of(" \t");
50  if (wpos == std::string::npos)
51    throw "Illegal format for tied-to constraint: '" + CStr + "'";
52  std::string DestOpName = Name.substr(0, wpos);
53  std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
54
55  Name = CStr.substr(pos+1);
56  wpos = Name.find_first_not_of(" \t");
57  if (wpos == std::string::npos)
58    throw "Illegal format for tied-to constraint: '" + CStr + "'";
59
60  std::pair<unsigned,unsigned> SrcOp =
61  I->ParseOperandName(Name.substr(wpos), false);
62  if (SrcOp > DestOp)
63    throw "Illegal tied-to operand constraint '" + CStr + "'";
64
65
66  unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
67  // Build the string for the operand.
68  std::string OpConstraint =
69  "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
70
71  if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
72    throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
73  I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
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  bool isTwoAddress = R->getValueAsBit("isTwoAddress");
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  mayHaveSideEffects = R->getValueAsBit("mayHaveSideEffects");
122  neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
123  isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
124  hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
125  hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
126  hasOptionalDef = false;
127  isVariadic = false;
128
129  if (mayHaveSideEffects + neverHasSideEffects + hasSideEffects > 1)
130    throw R->getName() + ": multiple conflicting side-effect flags set!";
131
132  DagInit *DI;
133  try {
134    DI = R->getValueAsDag("OutOperandList");
135  } catch (...) {
136    // Error getting operand list, just ignore it (sparcv9).
137    AsmString.clear();
138    OperandList.clear();
139    return;
140  }
141  NumDefs = DI->getNumArgs();
142
143  DagInit *IDI;
144  try {
145    IDI = R->getValueAsDag("InOperandList");
146  } catch (...) {
147    // Error getting operand list, just ignore it (sparcv9).
148    AsmString.clear();
149    OperandList.clear();
150    return;
151  }
152  DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI, new DagRecTy))->Fold(R, 0);
153
154  unsigned MIOperandNo = 0;
155  std::set<std::string> OperandNames;
156  for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
157    DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
158    if (!Arg)
159      throw "Illegal operand for the '" + R->getName() + "' instruction!";
160
161    Record *Rec = Arg->getDef();
162    std::string PrintMethod = "printOperand";
163    unsigned NumOps = 1;
164    DagInit *MIOpInfo = 0;
165    if (Rec->isSubClassOf("Operand")) {
166      PrintMethod = Rec->getValueAsString("PrintMethod");
167      MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
168
169      // Verify that MIOpInfo has an 'ops' root value.
170      if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
171          dynamic_cast<DefInit*>(MIOpInfo->getOperator())
172               ->getDef()->getName() != "ops")
173        throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
174              "'\n";
175
176      // If we have MIOpInfo, then we have #operands equal to number of entries
177      // in MIOperandInfo.
178      if (unsigned NumArgs = MIOpInfo->getNumArgs())
179        NumOps = NumArgs;
180
181      if (Rec->isSubClassOf("PredicateOperand"))
182        isPredicable = true;
183      else if (Rec->isSubClassOf("OptionalDefOperand"))
184        hasOptionalDef = true;
185    } else if (Rec->getName() == "variable_ops") {
186      isVariadic = true;
187      continue;
188    } else if (!Rec->isSubClassOf("RegisterClass") &&
189               Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
190      throw "Unknown operand class '" + Rec->getName() +
191            "' in '" + R->getName() + "' instruction!";
192
193    // Check that the operand has a name and that it's unique.
194    if (DI->getArgName(i).empty())
195      throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
196        " has no name!";
197    if (!OperandNames.insert(DI->getArgName(i)).second)
198      throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
199        " has the same name as a previous operand!";
200
201    OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
202                                      MIOperandNo, NumOps, MIOpInfo));
203    MIOperandNo += NumOps;
204  }
205
206  // Parse Constraints.
207  ParseConstraints(R->getValueAsString("Constraints"), this);
208
209  // For backward compatibility: isTwoAddress means operand 1 is tied to
210  // operand 0.
211  if (isTwoAddress) {
212    if (!OperandList[1].Constraints[0].empty())
213      throw R->getName() + ": cannot use isTwoAddress property: instruction "
214            "already has constraint set!";
215    OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
216  }
217
218  // Any operands with unset constraints get 0 as their constraint.
219  for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
220    for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
221      if (OperandList[op].Constraints[j].empty())
222        OperandList[op].Constraints[j] = "0";
223
224  // Parse the DisableEncoding field.
225  std::string DisableEncoding = R->getValueAsString("DisableEncoding");
226  while (1) {
227    std::string OpName = getToken(DisableEncoding, " ,\t");
228    if (OpName.empty()) break;
229
230    // Figure out which operand this is.
231    std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
232
233    // Mark the operand as not-to-be encoded.
234    if (Op.second >= OperandList[Op.first].DoNotEncode.size())
235      OperandList[Op.first].DoNotEncode.resize(Op.second+1);
236    OperandList[Op.first].DoNotEncode[Op.second] = true;
237  }
238}
239
240/// getOperandNamed - Return the index of the operand with the specified
241/// non-empty name.  If the instruction does not have an operand with the
242/// specified name, throw an exception.
243///
244unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
245  assert(!Name.empty() && "Cannot search for operand with no name!");
246  for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
247    if (OperandList[i].Name == Name) return i;
248  throw "Instruction '" + TheDef->getName() +
249        "' does not have an operand named '$" + Name + "'!";
250}
251
252std::pair<unsigned,unsigned>
253CodeGenInstruction::ParseOperandName(const std::string &Op,
254                                     bool AllowWholeOp) {
255  if (Op.empty() || Op[0] != '$')
256    throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
257
258  std::string OpName = Op.substr(1);
259  std::string SubOpName;
260
261  // Check to see if this is $foo.bar.
262  std::string::size_type DotIdx = OpName.find_first_of(".");
263  if (DotIdx != std::string::npos) {
264    SubOpName = OpName.substr(DotIdx+1);
265    if (SubOpName.empty())
266      throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
267    OpName = OpName.substr(0, DotIdx);
268  }
269
270  unsigned OpIdx = getOperandNamed(OpName);
271
272  if (SubOpName.empty()) {  // If no suboperand name was specified:
273    // If one was needed, throw.
274    if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
275        SubOpName.empty())
276      throw TheDef->getName() + ": Illegal to refer to"
277            " whole operand part of complex operand '" + Op + "'";
278
279    // Otherwise, return the operand.
280    return std::make_pair(OpIdx, 0U);
281  }
282
283  // Find the suboperand number involved.
284  DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
285  if (MIOpInfo == 0)
286    throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
287
288  // Find the operand with the right name.
289  for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
290    if (MIOpInfo->getArgName(i) == SubOpName)
291      return std::make_pair(OpIdx, i);
292
293  // Otherwise, didn't find it!
294  throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
295}
296