[LCG] Switch the primary node iterator to be a *much* more normal C++
[oota-llvm.git] / utils / TableGen / PseudoLoweringEmitter.cpp
1 //===- PseudoLoweringEmitter.cpp - PseudoLowering Generator -----*- C++ -*-===//
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 #include "CodeGenInstruction.h"
11 #include "CodeGenTarget.h"
12 #include "llvm/ADT/IndexedMap.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/TableGen/Error.h"
18 #include "llvm/TableGen/Record.h"
19 #include "llvm/TableGen/TableGenBackend.h"
20 #include <vector>
21 using namespace llvm;
22
23 #define DEBUG_TYPE "pseudo-lowering"
24
25 namespace {
26 class PseudoLoweringEmitter {
27   struct OpData {
28     enum MapKind { Operand, Imm, Reg };
29     MapKind Kind;
30     union {
31       unsigned Operand;   // Operand number mapped to.
32       uint64_t Imm;       // Integer immedate value.
33       Record *Reg;        // Physical register.
34     } Data;
35   };
36   struct PseudoExpansion {
37     CodeGenInstruction Source;  // The source pseudo instruction definition.
38     CodeGenInstruction Dest;    // The destination instruction to lower to.
39     IndexedMap<OpData> OperandMap;
40
41     PseudoExpansion(CodeGenInstruction &s, CodeGenInstruction &d,
42                     IndexedMap<OpData> &m) :
43       Source(s), Dest(d), OperandMap(m) {}
44   };
45
46   RecordKeeper &Records;
47
48   // It's overkill to have an instance of the full CodeGenTarget object,
49   // but it loads everything on demand, not in the constructor, so it's
50   // lightweight in performance, so it works out OK.
51   CodeGenTarget Target;
52
53   SmallVector<PseudoExpansion, 64> Expansions;
54
55   unsigned addDagOperandMapping(Record *Rec, DagInit *Dag,
56                                 CodeGenInstruction &Insn,
57                                 IndexedMap<OpData> &OperandMap,
58                                 unsigned BaseIdx);
59   void evaluateExpansion(Record *Pseudo);
60   void emitLoweringEmitter(raw_ostream &o);
61 public:
62   PseudoLoweringEmitter(RecordKeeper &R) : Records(R), Target(R) {}
63
64   /// run - Output the pseudo-lowerings.
65   void run(raw_ostream &o);
66 };
67 } // End anonymous namespace
68
69 // FIXME: This pass currently can only expand a pseudo to a single instruction.
70 //        The pseudo expansion really should take a list of dags, not just
71 //        a single dag, so we can do fancier things.
72
73 unsigned PseudoLoweringEmitter::
74 addDagOperandMapping(Record *Rec, DagInit *Dag, CodeGenInstruction &Insn,
75                      IndexedMap<OpData> &OperandMap, unsigned BaseIdx) {
76   unsigned OpsAdded = 0;
77   for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) {
78     if (DefInit *DI = dyn_cast<DefInit>(Dag->getArg(i))) {
79       // Physical register reference. Explicit check for the special case
80       // "zero_reg" definition.
81       if (DI->getDef()->isSubClassOf("Register") ||
82           DI->getDef()->getName() == "zero_reg") {
83         OperandMap[BaseIdx + i].Kind = OpData::Reg;
84         OperandMap[BaseIdx + i].Data.Reg = DI->getDef();
85         ++OpsAdded;
86         continue;
87       }
88
89       // Normal operands should always have the same type, or we have a
90       // problem.
91       // FIXME: We probably shouldn't ever get a non-zero BaseIdx here.
92       assert(BaseIdx == 0 && "Named subargument in pseudo expansion?!");
93       if (DI->getDef() != Insn.Operands[BaseIdx + i].Rec)
94         PrintFatalError(Rec->getLoc(),
95                       "Pseudo operand type '" + DI->getDef()->getName() +
96                       "' does not match expansion operand type '" +
97                       Insn.Operands[BaseIdx + i].Rec->getName() + "'");
98       // Source operand maps to destination operand. The Data element
99       // will be filled in later, just set the Kind for now. Do it
100       // for each corresponding MachineInstr operand, not just the first.
101       for (unsigned I = 0, E = Insn.Operands[i].MINumOperands; I != E; ++I)
102         OperandMap[BaseIdx + i + I].Kind = OpData::Operand;
103       OpsAdded += Insn.Operands[i].MINumOperands;
104     } else if (IntInit *II = dyn_cast<IntInit>(Dag->getArg(i))) {
105       OperandMap[BaseIdx + i].Kind = OpData::Imm;
106       OperandMap[BaseIdx + i].Data.Imm = II->getValue();
107       ++OpsAdded;
108     } else if (DagInit *SubDag = dyn_cast<DagInit>(Dag->getArg(i))) {
109       // Just add the operands recursively. This is almost certainly
110       // a constant value for a complex operand (> 1 MI operand).
111       unsigned NewOps =
112         addDagOperandMapping(Rec, SubDag, Insn, OperandMap, BaseIdx + i);
113       OpsAdded += NewOps;
114       // Since we added more than one, we also need to adjust the base.
115       BaseIdx += NewOps - 1;
116     } else
117       llvm_unreachable("Unhandled pseudo-expansion argument type!");
118   }
119   return OpsAdded;
120 }
121
122 void PseudoLoweringEmitter::evaluateExpansion(Record *Rec) {
123   DEBUG(dbgs() << "Pseudo definition: " << Rec->getName() << "\n");
124
125   // Validate that the result pattern has the corrent number and types
126   // of arguments for the instruction it references.
127   DagInit *Dag = Rec->getValueAsDag("ResultInst");
128   assert(Dag && "Missing result instruction in pseudo expansion!");
129   DEBUG(dbgs() << "  Result: " << *Dag << "\n");
130
131   DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator());
132   if (!OpDef)
133     PrintFatalError(Rec->getLoc(), Rec->getName() +
134                   " has unexpected operator type!");
135   Record *Operator = OpDef->getDef();
136   if (!Operator->isSubClassOf("Instruction"))
137     PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
138                     "' is not an instruction!");
139
140   CodeGenInstruction Insn(Operator);
141
142   if (Insn.isCodeGenOnly || Insn.isPseudo)
143     PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
144                     "' cannot be another pseudo instruction!");
145
146   if (Insn.Operands.size() != Dag->getNumArgs())
147     PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
148                     "' operand count mismatch");
149
150   unsigned NumMIOperands = 0;
151   for (unsigned i = 0, e = Insn.Operands.size(); i != e; ++i)
152     NumMIOperands += Insn.Operands[i].MINumOperands;
153   IndexedMap<OpData> OperandMap;
154   OperandMap.grow(NumMIOperands);
155
156   addDagOperandMapping(Rec, Dag, Insn, OperandMap, 0);
157
158   // If there are more operands that weren't in the DAG, they have to
159   // be operands that have default values, or we have an error. Currently,
160   // Operands that are a sublass of OperandWithDefaultOp have default values.
161
162
163   // Validate that each result pattern argument has a matching (by name)
164   // argument in the source instruction, in either the (outs) or (ins) list.
165   // Also check that the type of the arguments match.
166   //
167   // Record the mapping of the source to result arguments for use by
168   // the lowering emitter.
169   CodeGenInstruction SourceInsn(Rec);
170   StringMap<unsigned> SourceOperands;
171   for (unsigned i = 0, e = SourceInsn.Operands.size(); i != e; ++i)
172     SourceOperands[SourceInsn.Operands[i].Name] = i;
173
174   DEBUG(dbgs() << "  Operand mapping:\n");
175   for (unsigned i = 0, e = Insn.Operands.size(); i != e; ++i) {
176     // We've already handled constant values. Just map instruction operands
177     // here.
178     if (OperandMap[Insn.Operands[i].MIOperandNo].Kind != OpData::Operand)
179       continue;
180     StringMap<unsigned>::iterator SourceOp =
181       SourceOperands.find(Dag->getArgName(i));
182     if (SourceOp == SourceOperands.end())
183       PrintFatalError(Rec->getLoc(),
184                       "Pseudo output operand '" + Dag->getArgName(i) +
185                       "' has no matching source operand.");
186     // Map the source operand to the destination operand index for each
187     // MachineInstr operand.
188     for (unsigned I = 0, E = Insn.Operands[i].MINumOperands; I != E; ++I)
189       OperandMap[Insn.Operands[i].MIOperandNo + I].Data.Operand =
190         SourceOp->getValue();
191
192     DEBUG(dbgs() << "    " << SourceOp->getValue() << " ==> " << i << "\n");
193   }
194
195   Expansions.push_back(PseudoExpansion(SourceInsn, Insn, OperandMap));
196 }
197
198 void PseudoLoweringEmitter::emitLoweringEmitter(raw_ostream &o) {
199   // Emit file header.
200   emitSourceFileHeader("Pseudo-instruction MC lowering Source Fragment", o);
201
202   o << "bool " << Target.getName() + "AsmPrinter" << "::\n"
203     << "emitPseudoExpansionLowering(MCStreamer &OutStreamer,\n"
204     << "                            const MachineInstr *MI) {\n"
205     << "  switch (MI->getOpcode()) {\n"
206     << "    default: return false;\n";
207   for (unsigned i = 0, e = Expansions.size(); i != e; ++i) {
208     PseudoExpansion &Expansion = Expansions[i];
209     CodeGenInstruction &Source = Expansion.Source;
210     CodeGenInstruction &Dest = Expansion.Dest;
211     o << "    case " << Source.Namespace << "::"
212       << Source.TheDef->getName() << ": {\n"
213       << "      MCInst TmpInst;\n"
214       << "      MCOperand MCOp;\n"
215       << "      TmpInst.setOpcode(" << Dest.Namespace << "::"
216       << Dest.TheDef->getName() << ");\n";
217
218     // Copy the operands from the source instruction.
219     // FIXME: Instruction operands with defaults values (predicates and cc_out
220     //        in ARM, for example shouldn't need explicit values in the
221     //        expansion DAG.
222     unsigned MIOpNo = 0;
223     for (unsigned OpNo = 0, E = Dest.Operands.size(); OpNo != E;
224          ++OpNo) {
225       o << "      // Operand: " << Dest.Operands[OpNo].Name << "\n";
226       for (unsigned i = 0, e = Dest.Operands[OpNo].MINumOperands;
227            i != e; ++i) {
228         switch (Expansion.OperandMap[MIOpNo + i].Kind) {
229         case OpData::Operand:
230           o << "      lowerOperand(MI->getOperand("
231             << Source.Operands[Expansion.OperandMap[MIOpNo].Data
232                 .Operand].MIOperandNo + i
233             << "), MCOp);\n"
234             << "      TmpInst.addOperand(MCOp);\n";
235           break;
236         case OpData::Imm:
237           o << "      TmpInst.addOperand(MCOperand::CreateImm("
238             << Expansion.OperandMap[MIOpNo + i].Data.Imm << "));\n";
239           break;
240         case OpData::Reg: {
241           Record *Reg = Expansion.OperandMap[MIOpNo + i].Data.Reg;
242           o << "      TmpInst.addOperand(MCOperand::CreateReg(";
243           // "zero_reg" is special.
244           if (Reg->getName() == "zero_reg")
245             o << "0";
246           else
247             o << Reg->getValueAsString("Namespace") << "::" << Reg->getName();
248           o << "));\n";
249           break;
250         }
251         }
252       }
253       MIOpNo += Dest.Operands[OpNo].MINumOperands;
254     }
255     if (Dest.Operands.isVariadic) {
256       MIOpNo = Source.Operands.size() + 1;
257       o << "      // variable_ops\n";
258       o << "      for (unsigned i = " << MIOpNo
259         << ", e = MI->getNumOperands(); i != e; ++i)\n"
260         << "        if (lowerOperand(MI->getOperand(i), MCOp))\n"
261         << "          TmpInst.addOperand(MCOp);\n";
262     }
263     o << "      EmitToStreamer(OutStreamer, TmpInst);\n"
264       << "      break;\n"
265       << "    }\n";
266   }
267   o << "  }\n  return true;\n}\n\n";
268 }
269
270 void PseudoLoweringEmitter::run(raw_ostream &o) {
271   Record *ExpansionClass = Records.getClass("PseudoInstExpansion");
272   Record *InstructionClass = Records.getClass("Instruction");
273   assert(ExpansionClass && "PseudoInstExpansion class definition missing!");
274   assert(InstructionClass && "Instruction class definition missing!");
275
276   std::vector<Record*> Insts;
277   for (std::map<std::string, Record*>::const_iterator I =
278          Records.getDefs().begin(), E = Records.getDefs().end(); I != E; ++I) {
279     if (I->second->isSubClassOf(ExpansionClass) &&
280         I->second->isSubClassOf(InstructionClass))
281       Insts.push_back(I->second);
282   }
283
284   // Process the pseudo expansion definitions, validating them as we do so.
285   for (unsigned i = 0, e = Insts.size(); i != e; ++i)
286     evaluateExpansion(Insts[i]);
287
288   // Generate expansion code to lower the pseudo to an MCInst of the real
289   // instruction.
290   emitLoweringEmitter(o);
291 }
292
293 namespace llvm {
294
295 void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS) {
296   PseudoLoweringEmitter(RK).run(OS);
297 }
298
299 } // End llvm namespace