Rename isSimpleLoad to canFoldAsLoad, to better reflect its meaning.
[oota-llvm.git] / utils / TableGen / CodeGenInstruction.cpp
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>
18 using namespace llvm;
19
20 static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
21   // FIXME: Only supports TIED_TO for now.
22   std::string::size_type pos = CStr.find_first_of('=');
23   assert(pos != std::string::npos && "Unrecognized constraint");
24   std::string::size_type start = CStr.find_first_not_of(" \t");
25   std::string Name = CStr.substr(start, pos);
26   
27   // TIED_TO: $src1 = $dst
28   std::string::size_type wpos = Name.find_first_of(" \t");
29   if (wpos == std::string::npos)
30     throw "Illegal format for tied-to constraint: '" + CStr + "'";
31   std::string DestOpName = Name.substr(0, wpos);
32   std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
33   
34   Name = CStr.substr(pos+1);
35   wpos = Name.find_first_not_of(" \t");
36   if (wpos == std::string::npos)
37     throw "Illegal format for tied-to constraint: '" + CStr + "'";
38   
39   std::pair<unsigned,unsigned> SrcOp =
40   I->ParseOperandName(Name.substr(wpos), false);
41   if (SrcOp > DestOp)
42     throw "Illegal tied-to operand constraint '" + CStr + "'";
43   
44   
45   unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
46   // Build the string for the operand.
47   std::string OpConstraint =
48   "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
49   
50   
51   if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
52     throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
53   I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
54 }
55
56 static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
57   // Make sure the constraints list for each operand is large enough to hold
58   // constraint info, even if none is present.
59   for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i) 
60     I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
61   
62   if (CStr.empty()) return;
63   
64   const std::string delims(",");
65   std::string::size_type bidx, eidx;
66   
67   bidx = CStr.find_first_not_of(delims);
68   while (bidx != std::string::npos) {
69     eidx = CStr.find_first_of(delims, bidx);
70     if (eidx == std::string::npos)
71       eidx = CStr.length();
72     
73     ParseConstraint(CStr.substr(bidx, eidx), I);
74     bidx = CStr.find_first_not_of(delims, eidx);
75   }
76 }
77
78 CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
79   : TheDef(R), AsmString(AsmStr) {
80   Namespace = R->getValueAsString("Namespace");
81
82   isReturn     = R->getValueAsBit("isReturn");
83   isBranch     = R->getValueAsBit("isBranch");
84   isIndirectBranch = R->getValueAsBit("isIndirectBranch");
85   isBarrier    = R->getValueAsBit("isBarrier");
86   isCall       = R->getValueAsBit("isCall");
87   canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
88   mayLoad      = R->getValueAsBit("mayLoad");
89   mayStore     = R->getValueAsBit("mayStore");
90   bool isTwoAddress = R->getValueAsBit("isTwoAddress");
91   isPredicable = R->getValueAsBit("isPredicable");
92   isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
93   isCommutable = R->getValueAsBit("isCommutable");
94   isTerminator = R->getValueAsBit("isTerminator");
95   isReMaterializable = R->getValueAsBit("isReMaterializable");
96   hasDelaySlot = R->getValueAsBit("hasDelaySlot");
97   usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
98   hasCtrlDep   = R->getValueAsBit("hasCtrlDep");
99   isNotDuplicable = R->getValueAsBit("isNotDuplicable");
100   hasSideEffects = R->getValueAsBit("hasSideEffects");
101   mayHaveSideEffects = R->getValueAsBit("mayHaveSideEffects");
102   neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
103   isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
104   hasOptionalDef = false;
105   isVariadic = false;
106
107   if (mayHaveSideEffects + neverHasSideEffects + hasSideEffects > 1)
108     throw R->getName() + ": multiple conflicting side-effect flags set!";
109
110   DagInit *DI;
111   try {
112     DI = R->getValueAsDag("OutOperandList");
113   } catch (...) {
114     // Error getting operand list, just ignore it (sparcv9).
115     AsmString.clear();
116     OperandList.clear();
117     return;
118   }
119   NumDefs = DI->getNumArgs();
120
121   DagInit *IDI;
122   try {
123     IDI = R->getValueAsDag("InOperandList");
124   } catch (...) {
125     // Error getting operand list, just ignore it (sparcv9).
126     AsmString.clear();
127     OperandList.clear();
128     return;
129   }
130   DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI))->Fold();
131
132   unsigned MIOperandNo = 0;
133   std::set<std::string> OperandNames;
134   for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
135     DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
136     if (!Arg)
137       throw "Illegal operand for the '" + R->getName() + "' instruction!";
138
139     Record *Rec = Arg->getDef();
140     std::string PrintMethod = "printOperand";
141     unsigned NumOps = 1;
142     DagInit *MIOpInfo = 0;
143     if (Rec->isSubClassOf("Operand")) {
144       PrintMethod = Rec->getValueAsString("PrintMethod");
145       MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
146       
147       // Verify that MIOpInfo has an 'ops' root value.
148       if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
149           dynamic_cast<DefInit*>(MIOpInfo->getOperator())
150                ->getDef()->getName() != "ops")
151         throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
152               "'\n";
153
154       // If we have MIOpInfo, then we have #operands equal to number of entries
155       // in MIOperandInfo.
156       if (unsigned NumArgs = MIOpInfo->getNumArgs())
157         NumOps = NumArgs;
158
159       if (Rec->isSubClassOf("PredicateOperand"))
160         isPredicable = true;
161       else if (Rec->isSubClassOf("OptionalDefOperand"))
162         hasOptionalDef = true;
163     } else if (Rec->getName() == "variable_ops") {
164       isVariadic = true;
165       continue;
166     } else if (!Rec->isSubClassOf("RegisterClass") && 
167                Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
168       throw "Unknown operand class '" + Rec->getName() +
169             "' in '" + R->getName() + "' instruction!";
170
171     // Check that the operand has a name and that it's unique.
172     if (DI->getArgName(i).empty())
173       throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
174         " has no name!";
175     if (!OperandNames.insert(DI->getArgName(i)).second)
176       throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
177         " has the same name as a previous operand!";
178     
179     OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod, 
180                                       MIOperandNo, NumOps, MIOpInfo));
181     MIOperandNo += NumOps;
182   }
183
184   // Parse Constraints.
185   ParseConstraints(R->getValueAsString("Constraints"), this);
186   
187   // For backward compatibility: isTwoAddress means operand 1 is tied to
188   // operand 0.
189   if (isTwoAddress) {
190     if (!OperandList[1].Constraints[0].empty())
191       throw R->getName() + ": cannot use isTwoAddress property: instruction "
192             "already has constraint set!";
193     OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
194   }
195   
196   // Any operands with unset constraints get 0 as their constraint.
197   for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
198     for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
199       if (OperandList[op].Constraints[j].empty())
200         OperandList[op].Constraints[j] = "0";
201   
202   // Parse the DisableEncoding field.
203   std::string DisableEncoding = R->getValueAsString("DisableEncoding");
204   while (1) {
205     std::string OpName = getToken(DisableEncoding, " ,\t");
206     if (OpName.empty()) break;
207
208     // Figure out which operand this is.
209     std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
210
211     // Mark the operand as not-to-be encoded.
212     if (Op.second >= OperandList[Op.first].DoNotEncode.size())
213       OperandList[Op.first].DoNotEncode.resize(Op.second+1);
214     OperandList[Op.first].DoNotEncode[Op.second] = true;
215   }
216 }
217
218 /// getOperandNamed - Return the index of the operand with the specified
219 /// non-empty name.  If the instruction does not have an operand with the
220 /// specified name, throw an exception.
221 ///
222 unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
223   assert(!Name.empty() && "Cannot search for operand with no name!");
224   for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
225     if (OperandList[i].Name == Name) return i;
226   throw "Instruction '" + TheDef->getName() +
227         "' does not have an operand named '$" + Name + "'!";
228 }
229
230 std::pair<unsigned,unsigned> 
231 CodeGenInstruction::ParseOperandName(const std::string &Op,
232                                      bool AllowWholeOp) {
233   if (Op.empty() || Op[0] != '$')
234     throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
235   
236   std::string OpName = Op.substr(1);
237   std::string SubOpName;
238   
239   // Check to see if this is $foo.bar.
240   std::string::size_type DotIdx = OpName.find_first_of(".");
241   if (DotIdx != std::string::npos) {
242     SubOpName = OpName.substr(DotIdx+1);
243     if (SubOpName.empty())
244       throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
245     OpName = OpName.substr(0, DotIdx);
246   }
247   
248   unsigned OpIdx = getOperandNamed(OpName);
249
250   if (SubOpName.empty()) {  // If no suboperand name was specified:
251     // If one was needed, throw.
252     if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
253         SubOpName.empty())
254       throw TheDef->getName() + ": Illegal to refer to"
255             " whole operand part of complex operand '" + Op + "'";
256   
257     // Otherwise, return the operand.
258     return std::make_pair(OpIdx, 0U);
259   }
260   
261   // Find the suboperand number involved.
262   DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
263   if (MIOpInfo == 0)
264     throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
265   
266   // Find the operand with the right name.
267   for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
268     if (MIOpInfo->getArgName(i) == SubOpName)
269       return std::make_pair(OpIdx, i);
270
271   // Otherwise, didn't find it!
272   throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
273 }