blob: f01de1dcfce600a9f86730ab06d4260443d6c480 [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 {
43 std::vector<std::string> Operands;
44
45 bool operator<(const OperandsSignature &O) const {
46 return Operands < O.Operands;
47 }
48
49 bool empty() const { return Operands.empty(); }
50
Dan Gohmand1d2ee82008-08-19 20:56:30 +000051 /// initialize - Examine the given pattern and initialize the contents
52 /// of the Operands array accordingly. Return true if all the operands
53 /// are supported, false otherwise.
54 ///
55 bool initialize(TreePatternNode *InstPatNode,
56 const CodeGenTarget &Target,
Owen Anderson825b72b2009-08-11 20:47:22 +000057 MVT::SimpleValueType VT) {
Eric Christopherbc168272010-07-28 01:52:23 +000058
Dan Gohman098d3a42010-05-27 16:25:05 +000059 if (!InstPatNode->isLeaf()) {
60 if (InstPatNode->getOperator()->getName() == "imm") {
61 Operands.push_back("i");
62 return true;
63 }
64 if (InstPatNode->getOperator()->getName() == "fpimm") {
65 Operands.push_back("f");
66 return true;
67 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000068 }
Jim Grosbach45258f52010-12-07 19:36:07 +000069
Owen Andersonabb1f162008-08-26 01:22:59 +000070 const CodeGenRegisterClass *DstRC = 0;
Jim Grosbach45258f52010-12-07 19:36:07 +000071
Dan Gohmand1d2ee82008-08-19 20:56:30 +000072 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
73 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach45258f52010-12-07 19:36:07 +000074
Dan Gohmand1d2ee82008-08-19 20:56:30 +000075 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +000076 // For now, filter out any operand with multiple values.
Chris Lattnerd7349192010-03-19 21:37:09 +000077 if (!Op->getPredicateFns().empty() ||
78 Op->getNumTypes() != 1)
Dan Gohmand5fe57d2008-08-21 01:41:07 +000079 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +000080
Chris Lattnerd7349192010-03-19 21:37:09 +000081 assert(Op->hasTypeSet(0) && "Type infererence not done?");
82 // For now, all the operands must have the same type.
83 if (Op->getType(0) != VT)
84 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +000085
Dan Gohmand5fe57d2008-08-21 01:41:07 +000086 if (!Op->isLeaf()) {
87 if (Op->getOperator()->getName() == "imm") {
88 Operands.push_back("i");
Dale Johannesenedc87742009-05-21 22:25:49 +000089 continue;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000090 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000091 if (Op->getOperator()->getName() == "fpimm") {
92 Operands.push_back("f");
Dale Johannesenedc87742009-05-21 22:25:49 +000093 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +000094 }
Dan Gohman833ddf82008-08-27 16:18:22 +000095 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +000096 return false;
97 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +000098 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
99 if (!OpDI)
100 return false;
101 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000102 // For now, the only other thing we accept is register operands.
Evan Cheng98d2d072008-09-08 08:39:33 +0000103
Owen Anderson667d8f72008-08-29 17:45:56 +0000104 const CodeGenRegisterClass *RC = 0;
105 if (OpLeafRec->isSubClassOf("RegisterClass"))
106 RC = &Target.getRegisterClass(OpLeafRec);
107 else if (OpLeafRec->isSubClassOf("Register"))
108 RC = Target.getRegisterClassForRegister(OpLeafRec);
109 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000110 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000111
Eric Christopher2cfcad92010-08-24 23:21:59 +0000112 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000113 if (!RC)
114 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000115
Eric Christopher53452602010-08-25 04:58:56 +0000116 // For now, all the operands must have the same register class or be
117 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000118 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000119 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000120 return false;
121 } else
122 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000123 Operands.push_back("r");
124 }
125 return true;
126 }
127
Daniel Dunbar1a551802009-07-03 00:10:29 +0000128 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000129 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
130 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000131 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000132 } else if (Operands[i] == "i") {
133 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000134 } else if (Operands[i] == "f") {
135 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000136 } else {
137 assert("Unknown operand kind!");
138 abort();
139 }
140 if (i + 1 != e)
141 OS << ", ";
142 }
143 }
144
Daniel Dunbar1a551802009-07-03 00:10:29 +0000145 void PrintArguments(raw_ostream &OS,
Owen Anderson667d8f72008-08-29 17:45:56 +0000146 const std::vector<std::string>& PR) const {
147 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000148 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000149 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000150 if (PR[i] != "")
151 // Implicit physical register operand.
152 continue;
153
154 if (PrintedArg)
155 OS << ", ";
156 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000157 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000158 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000159 } else if (Operands[i] == "i") {
160 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000161 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000162 } else if (Operands[i] == "f") {
163 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000164 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000165 } else {
166 assert("Unknown operand kind!");
167 abort();
168 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000169 }
170 }
171
Daniel Dunbar1a551802009-07-03 00:10:29 +0000172 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000173 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
174 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000175 OS << "Op" << i << ", Op" << i << "IsKill";
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000176 } else if (Operands[i] == "i") {
177 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000178 } else if (Operands[i] == "f") {
179 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000180 } else {
181 assert("Unknown operand kind!");
182 abort();
183 }
184 if (i + 1 != e)
185 OS << ", ";
186 }
187 }
188
Owen Anderson667d8f72008-08-29 17:45:56 +0000189
Daniel Dunbar1a551802009-07-03 00:10:29 +0000190 void PrintManglingSuffix(raw_ostream &OS,
Evan Cheng98d2d072008-09-08 08:39:33 +0000191 const std::vector<std::string>& PR) const {
192 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
193 if (PR[i] != "")
194 // Implicit physical register operand. e.g. Instruction::Mul expect to
195 // select to a binary op. On x86, mul may take a single operand with
196 // the other operand being implicit. We must emit something that looks
197 // like a binary instruction except for the very inner FastEmitInst_*
198 // call.
199 continue;
200 OS << Operands[i];
201 }
202 }
203
Daniel Dunbar1a551802009-07-03 00:10:29 +0000204 void PrintManglingSuffix(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000205 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
206 OS << Operands[i];
207 }
208 }
209};
210
Dan Gohman72d63af2008-08-26 21:21:20 +0000211class FastISelMap {
212 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
214 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000215 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach45258f52010-12-07 19:36:07 +0000216 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopherecfa0792010-07-26 17:53:07 +0000217 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000218
219 OperandsOpcodeTypeRetPredMap SimplePatterns;
220
221 std::string InstNS;
222
223public:
224 explicit FastISelMap(std::string InstNS);
225
226 void CollectPatterns(CodeGenDAGPatterns &CGP);
Daniel Dunbar1a551802009-07-03 00:10:29 +0000227 void PrintFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000228};
229
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000230}
231
232static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
233 return CGP.getSDNodeInfo(Op).getEnumName();
234}
235
236static std::string getLegalCName(std::string OpName) {
237 std::string::size_type pos = OpName.find("::");
238 if (pos != std::string::npos)
239 OpName.replace(pos, 2, "_");
240 return OpName;
241}
242
Dan Gohman72d63af2008-08-26 21:21:20 +0000243FastISelMap::FastISelMap(std::string instns)
244 : InstNS(instns) {
245}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000246
Dan Gohman72d63af2008-08-26 21:21:20 +0000247void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
248 const CodeGenTarget &Target = CGP.getTargetInfo();
249
250 // Determine the target's namespace name.
251 InstNS = Target.getInstNamespace() + "::";
252 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000253
Dan Gohman0bfb7522008-08-22 00:28:15 +0000254 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000255 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
256 E = CGP.ptm_end(); I != E; ++I) {
257 const PatternToMatch &Pattern = *I;
258
259 // For now, just look at Instructions, so that we don't have to worry
260 // about emitting multiple instructions for a pattern.
261 TreePatternNode *Dst = Pattern.getDstPattern();
262 if (Dst->isLeaf()) continue;
263 Record *Op = Dst->getOperator();
264 if (!Op->isSubClassOf("Instruction"))
265 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000266 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000267 if (II.Operands.size() == 0)
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000268 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000269
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000270 // For now, ignore multi-instruction patterns.
271 bool MultiInsts = false;
272 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
273 TreePatternNode *ChildOp = Dst->getChild(i);
274 if (ChildOp->isLeaf())
275 continue;
276 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
277 MultiInsts = true;
278 break;
279 }
280 }
281 if (MultiInsts)
282 continue;
283
Dan Gohman379cad42008-08-19 20:36:33 +0000284 // For now, ignore instructions where the first operand is not an
285 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000286 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000287 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000288 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000289 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000290 if (!Op0Rec->isSubClassOf("RegisterClass"))
291 continue;
292 DstRC = &Target.getRegisterClass(Op0Rec);
293 if (!DstRC)
294 continue;
295 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000296 // If this isn't a leaf, then continue since the register classes are
297 // a bit too complicated for now.
298 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000299
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000300 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
301 if (SR)
302 SubRegNo = getQualifiedName(SR->getDef());
303 else
304 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000305 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000306
307 // Inspect the pattern.
308 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
309 if (!InstPatNode) continue;
310 if (InstPatNode->isLeaf()) continue;
311
Chris Lattner084df622010-03-24 00:41:19 +0000312 // Ignore multiple result nodes for now.
313 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000314
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000315 Record *InstPatOp = InstPatNode->getOperator();
316 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000317 MVT::SimpleValueType RetVT = MVT::isVoid;
318 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000320 if (InstPatNode->getNumChildren()) {
321 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
322 VT = InstPatNode->getChild(0)->getType(0);
323 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000324
325 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000326 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000327 if (InstPatOp->isSubClassOf("Operand"))
328 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000329
330 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000331 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000332 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000333
Dan Gohman379cad42008-08-19 20:36:33 +0000334 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000335 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000336 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000337 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000338
Owen Anderson667d8f72008-08-29 17:45:56 +0000339 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
340 if (!InstPatNode->isLeaf() &&
341 (InstPatNode->getOperator()->getName() == "imm" ||
342 InstPatNode->getOperator()->getName() == "fpimmm"))
343 PhysRegInputs->push_back("");
344 else if (!InstPatNode->isLeaf()) {
345 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
346 TreePatternNode *Op = InstPatNode->getChild(i);
347 if (!Op->isLeaf()) {
348 PhysRegInputs->push_back("");
349 continue;
350 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000351
Owen Anderson667d8f72008-08-29 17:45:56 +0000352 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
353 Record *OpLeafRec = OpDI->getDef();
354 std::string PhysReg;
355 if (OpLeafRec->isSubClassOf("Register")) {
356 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
357 "Namespace")->getValue())->getValue();
358 PhysReg += "::";
Jim Grosbach45258f52010-12-07 19:36:07 +0000359
Owen Anderson667d8f72008-08-29 17:45:56 +0000360 std::vector<CodeGenRegister> Regs = Target.getRegisters();
361 for (unsigned i = 0; i < Regs.size(); ++i) {
362 if (Regs[i].TheDef == OpLeafRec) {
363 PhysReg += Regs[i].getName();
364 break;
365 }
366 }
367 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000368
Owen Anderson667d8f72008-08-29 17:45:56 +0000369 PhysRegInputs->push_back(PhysReg);
370 }
371 } else
372 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000373
Dan Gohman22bb3112008-08-22 00:20:26 +0000374 // Get the predicate that guards this pattern.
375 std::string PredicateCheck = Pattern.getPredicateCheck();
376
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000377 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000378 InstructionMemo Memo = {
379 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000380 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000381 SubRegNo,
382 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000383 };
Jim Grosbach76612b52010-12-07 19:35:36 +0000384 if (SimplePatterns[Operands][OpcodeName][VT][RetVT]
Jim Grosbach997759a2010-12-07 23:05:49 +0000385 .count(PredicateCheck))
386 throw TGError(Pattern.getSrcRecord()->getLoc(), "Duplicate record!");
387
Owen Andersonabb1f162008-08-26 01:22:59 +0000388 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000389 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000390}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000391
Daniel Dunbar1a551802009-07-03 00:10:29 +0000392void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000393 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000394 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000395 OE = SimplePatterns.end(); OI != OE; ++OI) {
396 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000397 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000398
Owen Anderson7b2e5792008-08-25 23:43:09 +0000399 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000400 I != E; ++I) {
401 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000402 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000403
404 OS << "// FastEmit functions for " << Opcode << ".\n";
405 OS << "\n";
406
407 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000408 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000409 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000411 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000412 if (RM.size() != 1) {
413 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
414 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000416 const PredMap &PM = RI->second;
417 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000418
Evan Chengc3f44b02008-09-03 00:03:49 +0000419 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000420 << getLegalCName(Opcode)
421 << "_" << getLegalCName(getName(VT))
422 << "_" << getLegalCName(getName(RetVT)) << "_";
423 Operands.PrintManglingSuffix(OS);
424 OS << "(";
425 Operands.PrintParameters(OS);
426 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000427
Owen Anderson71669e52008-08-26 00:42:26 +0000428 // Emit code for each possible instruction. There may be
429 // multiple if there are subtarget concerns.
430 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
431 PI != PE; ++PI) {
432 std::string PredicateCheck = PI->first;
433 const InstructionMemo &Memo = PI->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000434
Owen Anderson71669e52008-08-26 00:42:26 +0000435 if (PredicateCheck.empty()) {
436 assert(!HasPred &&
437 "Multiple instructions match, at least one has "
438 "a predicate and at least one doesn't!");
439 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000440 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000441 OS << " ";
442 HasPred = true;
443 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000444
Owen Anderson667d8f72008-08-29 17:45:56 +0000445 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
446 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000447 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
448 << "TII.get(TargetOpcode::COPY), "
449 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000450 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000451
Owen Anderson71669e52008-08-26 00:42:26 +0000452 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000453 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000454 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000455 OS << "(" << InstNS << Memo.Name << ", ";
456 OS << InstNS << Memo.RC->getName() << "RegisterClass";
457 if (!Operands.empty())
458 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000459 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000460 OS << ");\n";
461 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000462 OS << "extractsubreg(" << getName(RetVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000463 OS << ", Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000464 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000465 OS << ");\n";
466 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000467
Owen Anderson667d8f72008-08-29 17:45:56 +0000468 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000469 OS << " }\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000470
Owen Anderson71669e52008-08-26 00:42:26 +0000471 }
472 // Return 0 if none of the predicates were satisfied.
473 if (HasPred)
474 OS << " return 0;\n";
475 OS << "}\n";
476 OS << "\n";
477 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000478
Owen Anderson71669e52008-08-26 00:42:26 +0000479 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000480 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000481 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000482 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000483 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000485 if (!Operands.empty())
486 OS << ", ";
487 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000489 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
490 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000492 OS << " case " << getName(RetVT) << ": return FastEmit_"
493 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
494 << "_" << getLegalCName(getName(RetVT)) << "_";
495 Operands.PrintManglingSuffix(OS);
496 OS << "(";
497 Operands.PrintArguments(OS);
498 OS << ");\n";
499 }
500 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000501
Owen Anderson71669e52008-08-26 00:42:26 +0000502 } else {
503 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000504 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000505 << getLegalCName(Opcode) << "_"
506 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000507 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000509 if (!Operands.empty())
510 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000511 Operands.PrintParameters(OS);
512 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000513
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000515 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000516
Owen Anderson71669e52008-08-26 00:42:26 +0000517 const PredMap &PM = RM.begin()->second;
518 bool HasPred = false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000519
Owen Anderson7b2e5792008-08-25 23:43:09 +0000520 // Emit code for each possible instruction. There may be
521 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000522 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
523 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000524 std::string PredicateCheck = PI->first;
525 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000526
Owen Anderson7b2e5792008-08-25 23:43:09 +0000527 if (PredicateCheck.empty()) {
528 assert(!HasPred &&
529 "Multiple instructions match, at least one has "
530 "a predicate and at least one doesn't!");
531 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000532 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000533 OS << " ";
534 HasPred = true;
535 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000536
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000537 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
538 if ((*Memo.PhysRegs)[i] != "")
539 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
540 << "TII.get(TargetOpcode::COPY), "
541 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
542 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000543
Owen Anderson7b2e5792008-08-25 23:43:09 +0000544 OS << " return FastEmitInst_";
Jim Grosbach45258f52010-12-07 19:36:07 +0000545
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000546 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000547 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000548 OS << "(" << InstNS << Memo.Name << ", ";
549 OS << InstNS << Memo.RC->getName() << "RegisterClass";
550 if (!Operands.empty())
551 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000552 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000553 OS << ");\n";
554 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000555 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000556 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000557 OS << ");\n";
558 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000559
Owen Anderson667d8f72008-08-29 17:45:56 +0000560 if (HasPred)
561 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000562 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000563
Owen Anderson7b2e5792008-08-25 23:43:09 +0000564 // Return 0 if none of the predicates were satisfied.
565 if (HasPred)
566 OS << " return 0;\n";
567 OS << "}\n";
568 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000569 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000570 }
571
572 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000573 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000574 << getLegalCName(Opcode) << "_";
575 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000577 if (!Operands.empty())
578 OS << ", ";
579 Operands.PrintParameters(OS);
580 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000582 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000583 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000585 std::string TypeName = getName(VT);
586 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000587 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
588 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000589 OS << "(RetVT";
590 if (!Operands.empty())
591 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000592 Operands.PrintArguments(OS);
593 OS << ");\n";
594 }
595 OS << " default: return 0;\n";
596 OS << " }\n";
597 OS << "}\n";
598 OS << "\n";
599 }
600
Dan Gohman0bfb7522008-08-22 00:28:15 +0000601 OS << "// Top-level FastEmit function.\n";
602 OS << "\n";
603
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000604 // Emit one function for the operand signature that demultiplexes based
605 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000606 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000607 Operands.PrintManglingSuffix(OS);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000608 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000609 if (!Operands.empty())
610 OS << ", ";
611 Operands.PrintParameters(OS);
612 OS << ") {\n";
613 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000614 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000615 I != E; ++I) {
616 const std::string &Opcode = I->first;
617
618 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000619 << getLegalCName(Opcode) << "_";
620 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000621 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000622 if (!Operands.empty())
623 OS << ", ";
624 Operands.PrintArguments(OS);
625 OS << ");\n";
626 }
627 OS << " default: return 0;\n";
628 OS << " }\n";
629 OS << "}\n";
630 OS << "\n";
631 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000632}
633
Daniel Dunbar1a551802009-07-03 00:10:29 +0000634void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000635 const CodeGenTarget &Target = CGP.getTargetInfo();
636
637 // Determine the target's namespace name.
638 std::string InstNS = Target.getInstNamespace() + "::";
639 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
640
641 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
642 Target.getName() + " target", OS);
643
Dan Gohman72d63af2008-08-26 21:21:20 +0000644 FastISelMap F(InstNS);
645 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000646 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000647}
648
649FastISelEmitter::FastISelEmitter(RecordKeeper &R)
650 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000651 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000652}
Dan Gohman72d63af2008-08-26 21:21:20 +0000653