blob: e1bdb43ecd4d8a908679358182e48c74beb8a670 [file] [log] [blame]
Jim Grosbachbcb36be2011-07-08 17:36:35 +00001//===- 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
Jim Grosbachbcb36be2011-07-08 17:36:35 +000010#include "CodeGenInstruction.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000011#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"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000017#include "llvm/TableGen/Error.h"
18#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000019#include "llvm/TableGen/TableGenBackend.h"
Jim Grosbachbcb36be2011-07-08 17:36:35 +000020#include <vector>
21using namespace llvm;
22
Chandler Carruth97acce22014-04-22 03:06:00 +000023#define DEBUG_TYPE "pseudo-lowering"
24
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000025namespace {
26class 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);
61public:
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
Jim Grosbachbcb36be2011-07-08 17:36:35 +000069// 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
73unsigned PseudoLoweringEmitter::
David Greeneaf8ee2c2011-07-29 22:43:06 +000074addDagOperandMapping(Record *Rec, DagInit *Dag, CodeGenInstruction &Insn,
Jim Grosbachbcb36be2011-07-08 17:36:35 +000075 IndexedMap<OpData> &OperandMap, unsigned BaseIdx) {
76 unsigned OpsAdded = 0;
77 for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +000078 if (DefInit *DI = dyn_cast<DefInit>(Dag->getArg(i))) {
Jim Grosbachbcb36be2011-07-08 17:36:35 +000079 // 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)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000094 PrintFatalError(Rec->getLoc(),
Jim Grosbachbcb36be2011-07-08 17:36:35 +000095 "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;
Sean Silvafb509ed2012-10-10 20:24:43 +0000104 } else if (IntInit *II = dyn_cast<IntInit>(Dag->getArg(i))) {
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000105 OperandMap[BaseIdx + i].Kind = OpData::Imm;
106 OperandMap[BaseIdx + i].Data.Imm = II->getValue();
107 ++OpsAdded;
Sean Silvafb509ed2012-10-10 20:24:43 +0000108 } else if (DagInit *SubDag = dyn_cast<DagInit>(Dag->getArg(i))) {
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000109 // 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
Craig Topperc4965bc2012-02-05 07:21:30 +0000117 llvm_unreachable("Unhandled pseudo-expansion argument type!");
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000118 }
119 return OpsAdded;
120}
121
122void 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.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000127 DagInit *Dag = Rec->getValueAsDag("ResultInst");
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000128 assert(Dag && "Missing result instruction in pseudo expansion!");
129 DEBUG(dbgs() << " Result: " << *Dag << "\n");
130
Sean Silvafb509ed2012-10-10 20:24:43 +0000131 DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator());
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000132 if (!OpDef)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000133 PrintFatalError(Rec->getLoc(), Rec->getName() +
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000134 " has unexpected operator type!");
135 Record *Operator = OpDef->getDef();
136 if (!Operator->isSubClassOf("Instruction"))
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000137 PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
138 "' is not an instruction!");
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000139
140 CodeGenInstruction Insn(Operator);
141
142 if (Insn.isCodeGenOnly || Insn.isPseudo)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000143 PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
144 "' cannot be another pseudo instruction!");
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000145
146 if (Insn.Operands.size() != Dag->getNumArgs())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000147 PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
148 "' operand count mismatch");
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000149
Evan Cheng630a7f32012-03-20 21:07:51 +0000150 unsigned NumMIOperands = 0;
151 for (unsigned i = 0, e = Insn.Operands.size(); i != e; ++i)
152 NumMIOperands += Insn.Operands[i].MINumOperands;
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000153 IndexedMap<OpData> OperandMap;
Evan Cheng630a7f32012-03-20 21:07:51 +0000154 OperandMap.grow(NumMIOperands);
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000155
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,
Tom Stellardb7246a72012-09-06 14:15:52 +0000160 // Operands that are a sublass of OperandWithDefaultOp have default values.
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000161
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())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000183 PrintFatalError(Rec->getLoc(),
184 "Pseudo output operand '" + Dag->getArgName(i) +
185 "' has no matching source operand.");
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000186 // 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
198void PseudoLoweringEmitter::emitLoweringEmitter(raw_ostream &o) {
199 // Emit file header.
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000200 emitSourceFileHeader("Pseudo-instruction MC lowering Source Fragment", o);
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000201
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) {
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000229 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) {
David Peixotto6eecb282013-02-13 19:21:47 +0000256 MIOpNo = Source.Operands.size() + 1;
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000257 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 }
David Woodhousee6c13e42014-01-28 23:12:42 +0000263 o << " EmitToStreamer(OutStreamer, TmpInst);\n"
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000264 << " break;\n"
265 << " }\n";
266 }
267 o << " }\n return true;\n}\n\n";
268}
269
270void PseudoLoweringEmitter::run(raw_ostream &o) {
271 Record *ExpansionClass = Records.getClass("PseudoInstExpansion");
Michael Liaoebeedd02012-09-17 04:43:39 +0000272 Record *InstructionClass = Records.getClass("Instruction");
Jim Grosbachbcb36be2011-07-08 17:36:35 +0000273 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
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000293namespace llvm {
294
295void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS) {
296 PseudoLoweringEmitter(RK).run(OS);
297}
298
299} // End llvm namespace