blob: 2c7a91eea52b4c3f6e99a4befb6928a609a10716 [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 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +000069
Owen Andersonabb1f162008-08-26 01:22:59 +000070 const CodeGenRegisterClass *DstRC = 0;
71
Dan Gohmand1d2ee82008-08-19 20:56:30 +000072 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
73 TreePatternNode *Op = InstPatNode->getChild(i);
Eric Christopherbc168272010-07-28 01:52:23 +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;
Chris Lattnerd7349192010-03-19 21:37:09 +000080
81 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;
85
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;
Eric Christopherbc168272010-07-28 01:52:23 +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;
Eric Christopherecfa0792010-07-26 17:53:07 +0000216 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
217 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;
Eric Christopherbc168272010-07-28 01:52:23 +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;
299
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;
314
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;
Owen Anderson667d8f72008-08-29 17:45:56 +0000338
339 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 }
351
352 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 += "::";
359
360 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 }
368
369 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 // FIXME: Source location information for the diagnostic.
385 if (SimplePatterns[Operands][OpcodeName][VT][RetVT]
386 .count(PredicateCheck)) {
387 SmallString<128> PatText;
388 raw_svector_ostream OS(PatText);
389 Pattern.SrcPattern->print(OS);
390 throw "Duplicate record: " + OS.str().str();
391 }
Owen Andersonabb1f162008-08-26 01:22:59 +0000392 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000393 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000394}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000395
Daniel Dunbar1a551802009-07-03 00:10:29 +0000396void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000397 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000398 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000399 OE = SimplePatterns.end(); OI != OE; ++OI) {
400 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000401 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000402
Owen Anderson7b2e5792008-08-25 23:43:09 +0000403 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000404 I != E; ++I) {
405 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000406 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000407
408 OS << "// FastEmit functions for " << Opcode << ".\n";
409 OS << "\n";
410
411 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000412 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000413 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000415 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000416 if (RM.size() != 1) {
417 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
418 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000420 const PredMap &PM = RI->second;
421 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000422
Evan Chengc3f44b02008-09-03 00:03:49 +0000423 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000424 << getLegalCName(Opcode)
425 << "_" << getLegalCName(getName(VT))
426 << "_" << getLegalCName(getName(RetVT)) << "_";
427 Operands.PrintManglingSuffix(OS);
428 OS << "(";
429 Operands.PrintParameters(OS);
430 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000431
Owen Anderson71669e52008-08-26 00:42:26 +0000432 // Emit code for each possible instruction. There may be
433 // multiple if there are subtarget concerns.
434 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
435 PI != PE; ++PI) {
436 std::string PredicateCheck = PI->first;
437 const InstructionMemo &Memo = PI->second;
438
439 if (PredicateCheck.empty()) {
440 assert(!HasPred &&
441 "Multiple instructions match, at least one has "
442 "a predicate and at least one doesn't!");
443 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000444 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000445 OS << " ";
446 HasPred = true;
447 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000448
449 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
450 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000451 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
452 << "TII.get(TargetOpcode::COPY), "
453 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000454 }
455
Owen Anderson71669e52008-08-26 00:42:26 +0000456 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000457 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000458 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000459 OS << "(" << InstNS << Memo.Name << ", ";
460 OS << InstNS << Memo.RC->getName() << "RegisterClass";
461 if (!Operands.empty())
462 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000463 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000464 OS << ");\n";
465 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000466 OS << "extractsubreg(" << getName(RetVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000467 OS << ", Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000468 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000469 OS << ");\n";
470 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000471
472 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000473 OS << " }\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000474
Owen Anderson71669e52008-08-26 00:42:26 +0000475 }
476 // Return 0 if none of the predicates were satisfied.
477 if (HasPred)
478 OS << " return 0;\n";
479 OS << "}\n";
480 OS << "\n";
481 }
482
483 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000484 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000485 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000486 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000487 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000489 if (!Operands.empty())
490 OS << ", ";
491 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000493 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
494 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000496 OS << " case " << getName(RetVT) << ": return FastEmit_"
497 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
498 << "_" << getLegalCName(getName(RetVT)) << "_";
499 Operands.PrintManglingSuffix(OS);
500 OS << "(";
501 Operands.PrintArguments(OS);
502 OS << ");\n";
503 }
504 OS << " default: return 0;\n}\n}\n\n";
505
506 } else {
507 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000508 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000509 << getLegalCName(Opcode) << "_"
510 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000511 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000513 if (!Operands.empty())
514 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000515 Operands.PrintParameters(OS);
516 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000517
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000519 << ")\n return 0;\n";
520
Owen Anderson71669e52008-08-26 00:42:26 +0000521 const PredMap &PM = RM.begin()->second;
522 bool HasPred = false;
523
Owen Anderson7b2e5792008-08-25 23:43:09 +0000524 // Emit code for each possible instruction. There may be
525 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000526 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
527 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000528 std::string PredicateCheck = PI->first;
529 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000530
Owen Anderson7b2e5792008-08-25 23:43:09 +0000531 if (PredicateCheck.empty()) {
532 assert(!HasPred &&
533 "Multiple instructions match, at least one has "
534 "a predicate and at least one doesn't!");
535 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000536 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000537 OS << " ";
538 HasPred = true;
539 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000540
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000541 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
542 if ((*Memo.PhysRegs)[i] != "")
543 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
544 << "TII.get(TargetOpcode::COPY), "
545 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
546 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000547
Owen Anderson7b2e5792008-08-25 23:43:09 +0000548 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000549
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000550 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000551 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000552 OS << "(" << InstNS << Memo.Name << ", ";
553 OS << InstNS << Memo.RC->getName() << "RegisterClass";
554 if (!Operands.empty())
555 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000556 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000557 OS << ");\n";
558 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000559 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000560 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000561 OS << ");\n";
562 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000563
564 if (HasPred)
565 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000566 }
Owen Anderson71669e52008-08-26 00:42:26 +0000567
Owen Anderson7b2e5792008-08-25 23:43:09 +0000568 // Return 0 if none of the predicates were satisfied.
569 if (HasPred)
570 OS << " return 0;\n";
571 OS << "}\n";
572 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000573 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000574 }
575
576 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000577 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000578 << getLegalCName(Opcode) << "_";
579 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000581 if (!Operands.empty())
582 OS << ", ";
583 Operands.PrintParameters(OS);
584 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000586 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000587 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000589 std::string TypeName = getName(VT);
590 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000591 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
592 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000593 OS << "(RetVT";
594 if (!Operands.empty())
595 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000596 Operands.PrintArguments(OS);
597 OS << ");\n";
598 }
599 OS << " default: return 0;\n";
600 OS << " }\n";
601 OS << "}\n";
602 OS << "\n";
603 }
604
Dan Gohman0bfb7522008-08-22 00:28:15 +0000605 OS << "// Top-level FastEmit function.\n";
606 OS << "\n";
607
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000608 // Emit one function for the operand signature that demultiplexes based
609 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000610 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000611 Operands.PrintManglingSuffix(OS);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000612 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000613 if (!Operands.empty())
614 OS << ", ";
615 Operands.PrintParameters(OS);
616 OS << ") {\n";
617 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000618 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000619 I != E; ++I) {
620 const std::string &Opcode = I->first;
621
622 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000623 << getLegalCName(Opcode) << "_";
624 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000625 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000626 if (!Operands.empty())
627 OS << ", ";
628 Operands.PrintArguments(OS);
629 OS << ");\n";
630 }
631 OS << " default: return 0;\n";
632 OS << " }\n";
633 OS << "}\n";
634 OS << "\n";
635 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000636}
637
Daniel Dunbar1a551802009-07-03 00:10:29 +0000638void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000639 const CodeGenTarget &Target = CGP.getTargetInfo();
640
641 // Determine the target's namespace name.
642 std::string InstNS = Target.getInstNamespace() + "::";
643 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
644
645 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
646 Target.getName() + " target", OS);
647
Dan Gohman72d63af2008-08-26 21:21:20 +0000648 FastISelMap F(InstNS);
649 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000650 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000651}
652
653FastISelEmitter::FastISelEmitter(RecordKeeper &R)
654 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000655 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000656}
Dan Gohman72d63af2008-08-26 21:21:20 +0000657