blob: e4db96f0d1e119632ab706487cf020373ae7e97d [file] [log] [blame]
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001//===- FastISelEmitter.cpp - Generate an instruction selector -------------===//
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//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000010// This tablegen backend emits code for use by the "fast" instruction
11// selection algorithm. See the comments at the top of
12// lib/CodeGen/SelectionDAG/FastISel.cpp for background.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000013//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000014// This file scans through the target's tablegen instruction-info files
15// and extracts instructions with obvious-looking patterns, and it emits
16// code to look up these instructions by type and operator.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000017//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000018//===----------------------------------------------------------------------===//
19
20#include "FastISelEmitter.h"
21#include "Record.h"
22#include "llvm/Support/Debug.h"
Jim Grosbach76612b52010-12-07 19:35:36 +000023#include "llvm/ADT/SmallString.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000024#include "llvm/ADT/VectorExtras.h"
25using namespace llvm;
26
27namespace {
28
Owen Anderson667d8f72008-08-29 17:45:56 +000029/// InstructionMemo - This class holds additional information about an
30/// instruction needed to emit code for it.
31///
32struct InstructionMemo {
33 std::string Name;
34 const CodeGenRegisterClass *RC;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +000035 std::string SubRegNo;
Owen Anderson667d8f72008-08-29 17:45:56 +000036 std::vector<std::string>* PhysRegs;
37};
38
Dan Gohman04b7dfb2008-08-19 18:06:12 +000039/// OperandsSignature - This class holds a description of a list of operand
40/// types. It has utility methods for emitting text based on the operands.
41///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000042struct OperandsSignature {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000043 class OpKind {
44 enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 };
45 char Repr;
46 public:
47
48 OpKind() : Repr(OK_Invalid) {}
49
50 bool operator<(OpKind RHS) const { return Repr < RHS.Repr; }
51
52 static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; }
53 static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; }
54 static OpKind getImm() { OpKind K; K.Repr = OK_Imm; return K; }
55
56 bool isReg() const { return Repr == OK_Reg; }
57 bool isFP() const { return Repr == OK_FP; }
58 bool isImm() const { return Repr == OK_Imm; }
59
60 void printManglingSuffix(raw_ostream &OS) const {
61 if (isReg())
62 OS << 'r';
63 else if (isFP())
64 OS << 'f';
65 else
66 OS << 'i';
67 }
68 };
69
70 SmallVector<OpKind, 3> Operands;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000071
72 bool operator<(const OperandsSignature &O) const {
73 return Operands < O.Operands;
74 }
75
76 bool empty() const { return Operands.empty(); }
77
Dan Gohmand1d2ee82008-08-19 20:56:30 +000078 /// initialize - Examine the given pattern and initialize the contents
79 /// of the Operands array accordingly. Return true if all the operands
80 /// are supported, false otherwise.
81 ///
Chris Lattner602fc062011-04-17 20:23:29 +000082 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
Owen Anderson825b72b2009-08-11 20:47:22 +000083 MVT::SimpleValueType VT) {
Eric Christopherbc168272010-07-28 01:52:23 +000084
Dan Gohman098d3a42010-05-27 16:25:05 +000085 if (!InstPatNode->isLeaf()) {
86 if (InstPatNode->getOperator()->getName() == "imm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000087 Operands.push_back(OpKind::getImm());
Dan Gohman098d3a42010-05-27 16:25:05 +000088 return true;
89 }
90 if (InstPatNode->getOperator()->getName() == "fpimm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000091 Operands.push_back(OpKind::getFP());
Dan Gohman098d3a42010-05-27 16:25:05 +000092 return true;
93 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000094 }
Jim Grosbach45258f52010-12-07 19:36:07 +000095
Owen Andersonabb1f162008-08-26 01:22:59 +000096 const CodeGenRegisterClass *DstRC = 0;
Jim Grosbach45258f52010-12-07 19:36:07 +000097
Dan Gohmand1d2ee82008-08-19 20:56:30 +000098 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
99 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach45258f52010-12-07 19:36:07 +0000100
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000101 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000102 // For now, filter out any operand with multiple values.
Chris Lattner602fc062011-04-17 20:23:29 +0000103 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerd7349192010-03-19 21:37:09 +0000104 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000105
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000106 if (!Op->isLeaf()) {
107 if (Op->getOperator()->getName() == "imm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000108 Operands.push_back(OpKind::getImm());
Dale Johannesenedc87742009-05-21 22:25:49 +0000109 continue;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000110 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000111 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000112 Operands.push_back(OpKind::getFP());
Dale Johannesenedc87742009-05-21 22:25:49 +0000113 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000114 }
Dan Gohman833ddf82008-08-27 16:18:22 +0000115 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000116 return false;
117 }
Chris Lattner602fc062011-04-17 20:23:29 +0000118
119 assert(Op->hasTypeSet(0) && "Type infererence not done?");
120
121 // For now, all the operands must have the same type (if they aren't
122 // immediates). Note that this causes us to reject variable sized shifts
123 // on X86.
124 if (Op->getType(0) != VT)
125 return false;
126
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000127 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
128 if (!OpDI)
129 return false;
130 Record *OpLeafRec = OpDI->getDef();
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000131
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000132 // For now, the only other thing we accept is register operands.
Owen Anderson667d8f72008-08-29 17:45:56 +0000133 const CodeGenRegisterClass *RC = 0;
134 if (OpLeafRec->isSubClassOf("RegisterClass"))
135 RC = &Target.getRegisterClass(OpLeafRec);
136 else if (OpLeafRec->isSubClassOf("Register"))
137 RC = Target.getRegisterClassForRegister(OpLeafRec);
138 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000139 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000140
Eric Christopher2cfcad92010-08-24 23:21:59 +0000141 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000142 if (!RC)
143 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000144
Eric Christopher53452602010-08-25 04:58:56 +0000145 // For now, all the operands must have the same register class or be
146 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000147 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000148 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000149 return false;
150 } else
151 DstRC = RC;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000152 Operands.push_back(OpKind::getReg());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000153 }
154 return true;
155 }
156
Daniel Dunbar1a551802009-07-03 00:10:29 +0000157 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000158 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000159 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000160 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000161 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000162 OS << "uint64_t imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000163 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000164 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000165 } else {
166 assert("Unknown operand kind!");
167 abort();
168 }
169 if (i + 1 != e)
170 OS << ", ";
171 }
172 }
173
Daniel Dunbar1a551802009-07-03 00:10:29 +0000174 void PrintArguments(raw_ostream &OS,
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000175 const std::vector<std::string> &PR) const {
Owen Anderson667d8f72008-08-29 17:45:56 +0000176 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000177 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000178 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000179 if (PR[i] != "")
180 // Implicit physical register operand.
181 continue;
182
183 if (PrintedArg)
184 OS << ", ";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000185 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000186 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000187 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000188 } else if (Operands[i].isImm()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000189 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000190 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000191 } else if (Operands[i].isFP()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000192 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000193 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000194 } else {
195 assert("Unknown operand kind!");
196 abort();
197 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000198 }
199 }
200
Daniel Dunbar1a551802009-07-03 00:10:29 +0000201 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000202 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000203 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000204 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000205 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000206 OS << "imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000207 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000208 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000209 } else {
210 assert("Unknown operand kind!");
211 abort();
212 }
213 if (i + 1 != e)
214 OS << ", ";
215 }
216 }
217
Owen Anderson667d8f72008-08-29 17:45:56 +0000218
Daniel Dunbar1a551802009-07-03 00:10:29 +0000219 void PrintManglingSuffix(raw_ostream &OS,
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000220 const std::vector<std::string> &PR) const {
Evan Cheng98d2d072008-09-08 08:39:33 +0000221 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
222 if (PR[i] != "")
223 // Implicit physical register operand. e.g. Instruction::Mul expect to
224 // select to a binary op. On x86, mul may take a single operand with
225 // the other operand being implicit. We must emit something that looks
226 // like a binary instruction except for the very inner FastEmitInst_*
227 // call.
228 continue;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000229 Operands[i].printManglingSuffix(OS);
Evan Cheng98d2d072008-09-08 08:39:33 +0000230 }
231 }
232
Daniel Dunbar1a551802009-07-03 00:10:29 +0000233 void PrintManglingSuffix(raw_ostream &OS) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000234 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
235 Operands[i].printManglingSuffix(OS);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000236 }
237};
238
Dan Gohman72d63af2008-08-26 21:21:20 +0000239class FastISelMap {
240 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
242 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000243 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach45258f52010-12-07 19:36:07 +0000244 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopherecfa0792010-07-26 17:53:07 +0000245 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000246
247 OperandsOpcodeTypeRetPredMap SimplePatterns;
248
249 std::string InstNS;
250
251public:
252 explicit FastISelMap(std::string InstNS);
253
254 void CollectPatterns(CodeGenDAGPatterns &CGP);
Daniel Dunbar1a551802009-07-03 00:10:29 +0000255 void PrintFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000256};
257
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000258}
259
260static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
261 return CGP.getSDNodeInfo(Op).getEnumName();
262}
263
264static std::string getLegalCName(std::string OpName) {
265 std::string::size_type pos = OpName.find("::");
266 if (pos != std::string::npos)
267 OpName.replace(pos, 2, "_");
268 return OpName;
269}
270
Dan Gohman72d63af2008-08-26 21:21:20 +0000271FastISelMap::FastISelMap(std::string instns)
272 : InstNS(instns) {
273}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000274
Dan Gohman72d63af2008-08-26 21:21:20 +0000275void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
276 const CodeGenTarget &Target = CGP.getTargetInfo();
277
278 // Determine the target's namespace name.
279 InstNS = Target.getInstNamespace() + "::";
280 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000281
Dan Gohman0bfb7522008-08-22 00:28:15 +0000282 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000283 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
284 E = CGP.ptm_end(); I != E; ++I) {
285 const PatternToMatch &Pattern = *I;
286
287 // For now, just look at Instructions, so that we don't have to worry
288 // about emitting multiple instructions for a pattern.
289 TreePatternNode *Dst = Pattern.getDstPattern();
290 if (Dst->isLeaf()) continue;
291 Record *Op = Dst->getOperator();
292 if (!Op->isSubClassOf("Instruction"))
293 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000294 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnera90dbc12011-04-17 22:24:13 +0000295 if (II.Operands.empty())
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000296 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000297
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000298 // For now, ignore multi-instruction patterns.
299 bool MultiInsts = false;
300 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
301 TreePatternNode *ChildOp = Dst->getChild(i);
302 if (ChildOp->isLeaf())
303 continue;
304 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
305 MultiInsts = true;
306 break;
307 }
308 }
309 if (MultiInsts)
310 continue;
311
Dan Gohman379cad42008-08-19 20:36:33 +0000312 // For now, ignore instructions where the first operand is not an
313 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000314 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000315 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000316 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000317 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000318 if (!Op0Rec->isSubClassOf("RegisterClass"))
319 continue;
320 DstRC = &Target.getRegisterClass(Op0Rec);
321 if (!DstRC)
322 continue;
323 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000324 // If this isn't a leaf, then continue since the register classes are
325 // a bit too complicated for now.
326 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000327
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000328 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
329 if (SR)
330 SubRegNo = getQualifiedName(SR->getDef());
331 else
332 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000333 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000334
335 // Inspect the pattern.
336 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
337 if (!InstPatNode) continue;
338 if (InstPatNode->isLeaf()) continue;
339
Chris Lattner084df622010-03-24 00:41:19 +0000340 // Ignore multiple result nodes for now.
341 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000342
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000343 Record *InstPatOp = InstPatNode->getOperator();
344 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000345 MVT::SimpleValueType RetVT = MVT::isVoid;
346 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000348 if (InstPatNode->getNumChildren()) {
349 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
350 VT = InstPatNode->getChild(0)->getType(0);
351 }
Chris Lattner602fc062011-04-17 20:23:29 +0000352
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000353 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000354 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000355 if (InstPatOp->isSubClassOf("Operand"))
356 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000357
358 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000359 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000360 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000361
Dan Gohman379cad42008-08-19 20:36:33 +0000362 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000363 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000364 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000365 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000366
Owen Anderson667d8f72008-08-29 17:45:56 +0000367 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
368 if (!InstPatNode->isLeaf() &&
369 (InstPatNode->getOperator()->getName() == "imm" ||
370 InstPatNode->getOperator()->getName() == "fpimmm"))
371 PhysRegInputs->push_back("");
372 else if (!InstPatNode->isLeaf()) {
373 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
374 TreePatternNode *Op = InstPatNode->getChild(i);
375 if (!Op->isLeaf()) {
376 PhysRegInputs->push_back("");
377 continue;
378 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000379
Owen Anderson667d8f72008-08-29 17:45:56 +0000380 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
381 Record *OpLeafRec = OpDI->getDef();
382 std::string PhysReg;
383 if (OpLeafRec->isSubClassOf("Register")) {
384 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
385 "Namespace")->getValue())->getValue();
386 PhysReg += "::";
Jim Grosbach45258f52010-12-07 19:36:07 +0000387
Owen Anderson667d8f72008-08-29 17:45:56 +0000388 std::vector<CodeGenRegister> Regs = Target.getRegisters();
389 for (unsigned i = 0; i < Regs.size(); ++i) {
390 if (Regs[i].TheDef == OpLeafRec) {
391 PhysReg += Regs[i].getName();
392 break;
393 }
394 }
395 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000396
Owen Anderson667d8f72008-08-29 17:45:56 +0000397 PhysRegInputs->push_back(PhysReg);
398 }
399 } else
400 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000401
Dan Gohman22bb3112008-08-22 00:20:26 +0000402 // Get the predicate that guards this pattern.
403 std::string PredicateCheck = Pattern.getPredicateCheck();
404
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000405 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000406 InstructionMemo Memo = {
407 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000408 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000409 SubRegNo,
410 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000411 };
Jim Grosbach76612b52010-12-07 19:35:36 +0000412 if (SimplePatterns[Operands][OpcodeName][VT][RetVT]
Jim Grosbach997759a2010-12-07 23:05:49 +0000413 .count(PredicateCheck))
414 throw TGError(Pattern.getSrcRecord()->getLoc(), "Duplicate record!");
415
Owen Andersonabb1f162008-08-26 01:22:59 +0000416 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000417 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000418}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000419
Daniel Dunbar1a551802009-07-03 00:10:29 +0000420void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000421 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000422 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000423 OE = SimplePatterns.end(); OI != OE; ++OI) {
424 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000425 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000426
Owen Anderson7b2e5792008-08-25 23:43:09 +0000427 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000428 I != E; ++I) {
429 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000430 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000431
432 OS << "// FastEmit functions for " << Opcode << ".\n";
433 OS << "\n";
434
435 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000436 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000437 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000439 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000440 if (RM.size() != 1) {
441 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
442 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000444 const PredMap &PM = RI->second;
445 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000446
Evan Chengc3f44b02008-09-03 00:03:49 +0000447 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000448 << getLegalCName(Opcode)
449 << "_" << getLegalCName(getName(VT))
450 << "_" << getLegalCName(getName(RetVT)) << "_";
451 Operands.PrintManglingSuffix(OS);
452 OS << "(";
453 Operands.PrintParameters(OS);
454 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000455
Owen Anderson71669e52008-08-26 00:42:26 +0000456 // Emit code for each possible instruction. There may be
457 // multiple if there are subtarget concerns.
458 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
459 PI != PE; ++PI) {
460 std::string PredicateCheck = PI->first;
461 const InstructionMemo &Memo = PI->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000462
Owen Anderson71669e52008-08-26 00:42:26 +0000463 if (PredicateCheck.empty()) {
464 assert(!HasPred &&
465 "Multiple instructions match, at least one has "
466 "a predicate and at least one doesn't!");
467 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000468 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000469 OS << " ";
470 HasPred = true;
471 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000472
Owen Anderson667d8f72008-08-29 17:45:56 +0000473 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
474 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000475 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
476 << "TII.get(TargetOpcode::COPY), "
477 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000478 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000479
Owen Anderson71669e52008-08-26 00:42:26 +0000480 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000481 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000482 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000483 OS << "(" << InstNS << Memo.Name << ", ";
484 OS << InstNS << Memo.RC->getName() << "RegisterClass";
485 if (!Operands.empty())
486 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000487 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000488 OS << ");\n";
489 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000490 OS << "extractsubreg(" << getName(RetVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000491 OS << ", Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000492 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000493 OS << ");\n";
494 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000495
Owen Anderson667d8f72008-08-29 17:45:56 +0000496 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000497 OS << " }\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000498
Owen Anderson71669e52008-08-26 00:42:26 +0000499 }
500 // Return 0 if none of the predicates were satisfied.
501 if (HasPred)
502 OS << " return 0;\n";
503 OS << "}\n";
504 OS << "\n";
505 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000506
Owen Anderson71669e52008-08-26 00:42:26 +0000507 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000508 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000509 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000510 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000511 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000513 if (!Operands.empty())
514 OS << ", ";
515 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000517 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
518 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000520 OS << " case " << getName(RetVT) << ": return FastEmit_"
521 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
522 << "_" << getLegalCName(getName(RetVT)) << "_";
523 Operands.PrintManglingSuffix(OS);
524 OS << "(";
525 Operands.PrintArguments(OS);
526 OS << ");\n";
527 }
528 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000529
Owen Anderson71669e52008-08-26 00:42:26 +0000530 } else {
531 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000532 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000533 << getLegalCName(Opcode) << "_"
534 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000535 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000537 if (!Operands.empty())
538 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000539 Operands.PrintParameters(OS);
540 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000541
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000543 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000544
Owen Anderson71669e52008-08-26 00:42:26 +0000545 const PredMap &PM = RM.begin()->second;
546 bool HasPred = false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000547
Owen Anderson7b2e5792008-08-25 23:43:09 +0000548 // Emit code for each possible instruction. There may be
549 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000550 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
551 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000552 std::string PredicateCheck = PI->first;
553 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000554
Owen Anderson7b2e5792008-08-25 23:43:09 +0000555 if (PredicateCheck.empty()) {
556 assert(!HasPred &&
557 "Multiple instructions match, at least one has "
558 "a predicate and at least one doesn't!");
559 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000560 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000561 OS << " ";
562 HasPred = true;
563 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000564
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000565 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
566 if ((*Memo.PhysRegs)[i] != "")
567 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
568 << "TII.get(TargetOpcode::COPY), "
569 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
570 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000571
Owen Anderson7b2e5792008-08-25 23:43:09 +0000572 OS << " return FastEmitInst_";
Jim Grosbach45258f52010-12-07 19:36:07 +0000573
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000574 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000575 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000576 OS << "(" << InstNS << Memo.Name << ", ";
577 OS << InstNS << Memo.RC->getName() << "RegisterClass";
578 if (!Operands.empty())
579 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000580 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000581 OS << ");\n";
582 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000583 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000584 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000585 OS << ");\n";
586 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000587
Owen Anderson667d8f72008-08-29 17:45:56 +0000588 if (HasPred)
589 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000590 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000591
Owen Anderson7b2e5792008-08-25 23:43:09 +0000592 // Return 0 if none of the predicates were satisfied.
593 if (HasPred)
594 OS << " return 0;\n";
595 OS << "}\n";
596 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000597 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000598 }
599
600 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000601 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000602 << getLegalCName(Opcode) << "_";
603 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000605 if (!Operands.empty())
606 OS << ", ";
607 Operands.PrintParameters(OS);
608 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000610 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000611 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000613 std::string TypeName = getName(VT);
614 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000615 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
616 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000617 OS << "(RetVT";
618 if (!Operands.empty())
619 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000620 Operands.PrintArguments(OS);
621 OS << ");\n";
622 }
623 OS << " default: return 0;\n";
624 OS << " }\n";
625 OS << "}\n";
626 OS << "\n";
627 }
628
Dan Gohman0bfb7522008-08-22 00:28:15 +0000629 OS << "// Top-level FastEmit function.\n";
630 OS << "\n";
631
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000632 // Emit one function for the operand signature that demultiplexes based
633 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000634 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000635 Operands.PrintManglingSuffix(OS);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000636 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000637 if (!Operands.empty())
638 OS << ", ";
639 Operands.PrintParameters(OS);
640 OS << ") {\n";
641 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000642 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000643 I != E; ++I) {
644 const std::string &Opcode = I->first;
645
646 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000647 << getLegalCName(Opcode) << "_";
648 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000649 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000650 if (!Operands.empty())
651 OS << ", ";
652 Operands.PrintArguments(OS);
653 OS << ");\n";
654 }
655 OS << " default: return 0;\n";
656 OS << " }\n";
657 OS << "}\n";
658 OS << "\n";
659 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000660}
661
Daniel Dunbar1a551802009-07-03 00:10:29 +0000662void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000663 const CodeGenTarget &Target = CGP.getTargetInfo();
664
665 // Determine the target's namespace name.
666 std::string InstNS = Target.getInstNamespace() + "::";
667 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
668
669 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
670 Target.getName() + " target", OS);
671
Dan Gohman72d63af2008-08-26 21:21:20 +0000672 FastISelMap F(InstNS);
673 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000674 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000675}
676
677FastISelEmitter::FastISelEmitter(RecordKeeper &R)
678 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000679 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000680}
Dan Gohman72d63af2008-08-26 21:21:20 +0000681