blob: 6c16fcfaa8a27aa621a869b36dd402b554274b33 [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"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000023#include "llvm/ADT/VectorExtras.h"
24using namespace llvm;
25
26namespace {
27
Owen Anderson667d8f72008-08-29 17:45:56 +000028/// InstructionMemo - This class holds additional information about an
29/// instruction needed to emit code for it.
30///
31struct InstructionMemo {
32 std::string Name;
33 const CodeGenRegisterClass *RC;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +000034 std::string SubRegNo;
Owen Anderson667d8f72008-08-29 17:45:56 +000035 std::vector<std::string>* PhysRegs;
36};
37
Dan Gohman04b7dfb2008-08-19 18:06:12 +000038/// OperandsSignature - This class holds a description of a list of operand
39/// types. It has utility methods for emitting text based on the operands.
40///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000041struct OperandsSignature {
42 std::vector<std::string> Operands;
43
44 bool operator<(const OperandsSignature &O) const {
45 return Operands < O.Operands;
46 }
47
48 bool empty() const { return Operands.empty(); }
49
Dan Gohmand1d2ee82008-08-19 20:56:30 +000050 /// initialize - Examine the given pattern and initialize the contents
51 /// of the Operands array accordingly. Return true if all the operands
52 /// are supported, false otherwise.
53 ///
54 bool initialize(TreePatternNode *InstPatNode,
55 const CodeGenTarget &Target,
Owen Anderson825b72b2009-08-11 20:47:22 +000056 MVT::SimpleValueType VT) {
Eric Christopherbc168272010-07-28 01:52:23 +000057
Dan Gohman098d3a42010-05-27 16:25:05 +000058 if (!InstPatNode->isLeaf()) {
59 if (InstPatNode->getOperator()->getName() == "imm") {
60 Operands.push_back("i");
61 return true;
62 }
63 if (InstPatNode->getOperator()->getName() == "fpimm") {
64 Operands.push_back("f");
65 return true;
66 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000067 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +000068
Owen Andersonabb1f162008-08-26 01:22:59 +000069 const CodeGenRegisterClass *DstRC = 0;
70
Dan Gohmand1d2ee82008-08-19 20:56:30 +000071 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
72 TreePatternNode *Op = InstPatNode->getChild(i);
Eric Christopherbc168272010-07-28 01:52:23 +000073
Dan Gohmand1d2ee82008-08-19 20:56:30 +000074 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +000075 // For now, filter out any operand with multiple values.
Chris Lattnerd7349192010-03-19 21:37:09 +000076 if (!Op->getPredicateFns().empty() ||
77 Op->getNumTypes() != 1)
Dan Gohmand5fe57d2008-08-21 01:41:07 +000078 return false;
Chris Lattnerd7349192010-03-19 21:37:09 +000079
80 assert(Op->hasTypeSet(0) && "Type infererence not done?");
81 // For now, all the operands must have the same type.
82 if (Op->getType(0) != VT)
83 return false;
84
Dan Gohmand5fe57d2008-08-21 01:41:07 +000085 if (!Op->isLeaf()) {
86 if (Op->getOperator()->getName() == "imm") {
87 Operands.push_back("i");
Dale Johannesenedc87742009-05-21 22:25:49 +000088 continue;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000089 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000090 if (Op->getOperator()->getName() == "fpimm") {
91 Operands.push_back("f");
Dale Johannesenedc87742009-05-21 22:25:49 +000092 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +000093 }
Dan Gohman833ddf82008-08-27 16:18:22 +000094 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +000095 return false;
96 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +000097 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
98 if (!OpDI)
99 return false;
100 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000101 // For now, the only other thing we accept is register operands.
Evan Cheng98d2d072008-09-08 08:39:33 +0000102
Owen Anderson667d8f72008-08-29 17:45:56 +0000103 const CodeGenRegisterClass *RC = 0;
104 if (OpLeafRec->isSubClassOf("RegisterClass"))
105 RC = &Target.getRegisterClass(OpLeafRec);
106 else if (OpLeafRec->isSubClassOf("Register"))
107 RC = Target.getRegisterClassForRegister(OpLeafRec);
108 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000109 return false;
Eric Christopherbc168272010-07-28 01:52:23 +0000110
Eric Christopher2cfcad92010-08-24 23:21:59 +0000111 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000112 if (!RC)
113 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000114
Eric Christopher53452602010-08-25 04:58:56 +0000115 // For now, all the operands must have the same register class or be
116 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000117 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000118 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000119 return false;
120 } else
121 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000122 Operands.push_back("r");
123 }
124 return true;
125 }
126
Daniel Dunbar1a551802009-07-03 00:10:29 +0000127 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000128 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
129 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000130 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000131 } else if (Operands[i] == "i") {
132 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000133 } else if (Operands[i] == "f") {
134 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000135 } else {
136 assert("Unknown operand kind!");
137 abort();
138 }
139 if (i + 1 != e)
140 OS << ", ";
141 }
142 }
143
Daniel Dunbar1a551802009-07-03 00:10:29 +0000144 void PrintArguments(raw_ostream &OS,
Owen Anderson667d8f72008-08-29 17:45:56 +0000145 const std::vector<std::string>& PR) const {
146 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000147 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000148 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000149 if (PR[i] != "")
150 // Implicit physical register operand.
151 continue;
152
153 if (PrintedArg)
154 OS << ", ";
155 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000156 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000157 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000158 } else if (Operands[i] == "i") {
159 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000160 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000161 } else if (Operands[i] == "f") {
162 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000163 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000164 } else {
165 assert("Unknown operand kind!");
166 abort();
167 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000168 }
169 }
170
Daniel Dunbar1a551802009-07-03 00:10:29 +0000171 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000172 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
173 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000174 OS << "Op" << i << ", Op" << i << "IsKill";
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000175 } else if (Operands[i] == "i") {
176 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000177 } else if (Operands[i] == "f") {
178 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000179 } else {
180 assert("Unknown operand kind!");
181 abort();
182 }
183 if (i + 1 != e)
184 OS << ", ";
185 }
186 }
187
Owen Anderson667d8f72008-08-29 17:45:56 +0000188
Daniel Dunbar1a551802009-07-03 00:10:29 +0000189 void PrintManglingSuffix(raw_ostream &OS,
Evan Cheng98d2d072008-09-08 08:39:33 +0000190 const std::vector<std::string>& PR) const {
191 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
192 if (PR[i] != "")
193 // Implicit physical register operand. e.g. Instruction::Mul expect to
194 // select to a binary op. On x86, mul may take a single operand with
195 // the other operand being implicit. We must emit something that looks
196 // like a binary instruction except for the very inner FastEmitInst_*
197 // call.
198 continue;
199 OS << Operands[i];
200 }
201 }
202
Daniel Dunbar1a551802009-07-03 00:10:29 +0000203 void PrintManglingSuffix(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000204 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
205 OS << Operands[i];
206 }
207 }
208};
209
Dan Gohman72d63af2008-08-26 21:21:20 +0000210class FastISelMap {
211 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
213 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000214 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Eric Christopherecfa0792010-07-26 17:53:07 +0000215 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
216 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000217
218 OperandsOpcodeTypeRetPredMap SimplePatterns;
219
220 std::string InstNS;
221
222public:
223 explicit FastISelMap(std::string InstNS);
224
225 void CollectPatterns(CodeGenDAGPatterns &CGP);
Daniel Dunbar1a551802009-07-03 00:10:29 +0000226 void PrintFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000227};
228
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000229}
230
231static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
232 return CGP.getSDNodeInfo(Op).getEnumName();
233}
234
235static std::string getLegalCName(std::string OpName) {
236 std::string::size_type pos = OpName.find("::");
237 if (pos != std::string::npos)
238 OpName.replace(pos, 2, "_");
239 return OpName;
240}
241
Dan Gohman72d63af2008-08-26 21:21:20 +0000242FastISelMap::FastISelMap(std::string instns)
243 : InstNS(instns) {
244}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000245
Dan Gohman72d63af2008-08-26 21:21:20 +0000246void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
247 const CodeGenTarget &Target = CGP.getTargetInfo();
248
249 // Determine the target's namespace name.
250 InstNS = Target.getInstNamespace() + "::";
251 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000252
Dan Gohman0bfb7522008-08-22 00:28:15 +0000253 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000254 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
255 E = CGP.ptm_end(); I != E; ++I) {
256 const PatternToMatch &Pattern = *I;
257
258 // For now, just look at Instructions, so that we don't have to worry
259 // about emitting multiple instructions for a pattern.
260 TreePatternNode *Dst = Pattern.getDstPattern();
261 if (Dst->isLeaf()) continue;
262 Record *Op = Dst->getOperator();
263 if (!Op->isSubClassOf("Instruction"))
264 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000265 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000266 if (II.OperandList.empty())
267 continue;
Eric Christopherbc168272010-07-28 01:52:23 +0000268
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000269 // For now, ignore multi-instruction patterns.
270 bool MultiInsts = false;
271 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
272 TreePatternNode *ChildOp = Dst->getChild(i);
273 if (ChildOp->isLeaf())
274 continue;
275 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
276 MultiInsts = true;
277 break;
278 }
279 }
280 if (MultiInsts)
281 continue;
282
Dan Gohman379cad42008-08-19 20:36:33 +0000283 // For now, ignore instructions where the first operand is not an
284 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000285 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000286 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000287 if (Op->getName() != "EXTRACT_SUBREG") {
288 Record *Op0Rec = II.OperandList[0].Rec;
289 if (!Op0Rec->isSubClassOf("RegisterClass"))
290 continue;
291 DstRC = &Target.getRegisterClass(Op0Rec);
292 if (!DstRC)
293 continue;
294 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000295 // If this isn't a leaf, then continue since the register classes are
296 // a bit too complicated for now.
297 if (!Dst->getChild(1)->isLeaf()) continue;
298
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000299 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
300 if (SR)
301 SubRegNo = getQualifiedName(SR->getDef());
302 else
303 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000304 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000305
306 // Inspect the pattern.
307 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
308 if (!InstPatNode) continue;
309 if (InstPatNode->isLeaf()) continue;
310
Chris Lattner084df622010-03-24 00:41:19 +0000311 // Ignore multiple result nodes for now.
312 if (InstPatNode->getNumTypes() > 1) continue;
313
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000314 Record *InstPatOp = InstPatNode->getOperator();
315 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000316 MVT::SimpleValueType RetVT = MVT::isVoid;
317 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000319 if (InstPatNode->getNumChildren()) {
320 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
321 VT = InstPatNode->getChild(0)->getType(0);
322 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000323
324 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000325 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000326 if (InstPatOp->isSubClassOf("Operand"))
327 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000328
329 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000330 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000331 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000332
Dan Gohman379cad42008-08-19 20:36:33 +0000333 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000334 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000335 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000336 continue;
Owen Anderson667d8f72008-08-29 17:45:56 +0000337
338 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
339 if (!InstPatNode->isLeaf() &&
340 (InstPatNode->getOperator()->getName() == "imm" ||
341 InstPatNode->getOperator()->getName() == "fpimmm"))
342 PhysRegInputs->push_back("");
343 else if (!InstPatNode->isLeaf()) {
344 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
345 TreePatternNode *Op = InstPatNode->getChild(i);
346 if (!Op->isLeaf()) {
347 PhysRegInputs->push_back("");
348 continue;
349 }
350
351 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
352 Record *OpLeafRec = OpDI->getDef();
353 std::string PhysReg;
354 if (OpLeafRec->isSubClassOf("Register")) {
355 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
356 "Namespace")->getValue())->getValue();
357 PhysReg += "::";
358
359 std::vector<CodeGenRegister> Regs = Target.getRegisters();
360 for (unsigned i = 0; i < Regs.size(); ++i) {
361 if (Regs[i].TheDef == OpLeafRec) {
362 PhysReg += Regs[i].getName();
363 break;
364 }
365 }
366 }
367
368 PhysRegInputs->push_back(PhysReg);
369 }
370 } else
371 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000372
Dan Gohman22bb3112008-08-22 00:20:26 +0000373 // Get the predicate that guards this pattern.
374 std::string PredicateCheck = Pattern.getPredicateCheck();
375
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000376 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000377 InstructionMemo Memo = {
378 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000379 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000380 SubRegNo,
381 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000382 };
Eric Christopherecfa0792010-07-26 17:53:07 +0000383 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT]
384 .count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000385 "Duplicate pattern!");
Owen Andersonabb1f162008-08-26 01:22:59 +0000386 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000387 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000388}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000389
Daniel Dunbar1a551802009-07-03 00:10:29 +0000390void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000391 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000392 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000393 OE = SimplePatterns.end(); OI != OE; ++OI) {
394 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000395 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000396
Owen Anderson7b2e5792008-08-25 23:43:09 +0000397 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000398 I != E; ++I) {
399 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000400 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000401
402 OS << "// FastEmit functions for " << Opcode << ".\n";
403 OS << "\n";
404
405 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000406 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000407 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000409 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000410 if (RM.size() != 1) {
411 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
412 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000414 const PredMap &PM = RI->second;
415 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000416
Evan Chengc3f44b02008-09-03 00:03:49 +0000417 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000418 << getLegalCName(Opcode)
419 << "_" << getLegalCName(getName(VT))
420 << "_" << getLegalCName(getName(RetVT)) << "_";
421 Operands.PrintManglingSuffix(OS);
422 OS << "(";
423 Operands.PrintParameters(OS);
424 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000425
Owen Anderson71669e52008-08-26 00:42:26 +0000426 // Emit code for each possible instruction. There may be
427 // multiple if there are subtarget concerns.
428 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
429 PI != PE; ++PI) {
430 std::string PredicateCheck = PI->first;
431 const InstructionMemo &Memo = PI->second;
432
433 if (PredicateCheck.empty()) {
434 assert(!HasPred &&
435 "Multiple instructions match, at least one has "
436 "a predicate and at least one doesn't!");
437 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000438 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000439 OS << " ";
440 HasPred = true;
441 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000442
443 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
444 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000445 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
446 << "TII.get(TargetOpcode::COPY), "
447 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000448 }
449
Owen Anderson71669e52008-08-26 00:42:26 +0000450 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000451 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000452 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000453 OS << "(" << InstNS << Memo.Name << ", ";
454 OS << InstNS << Memo.RC->getName() << "RegisterClass";
455 if (!Operands.empty())
456 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000457 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000458 OS << ");\n";
459 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000460 OS << "extractsubreg(" << getName(RetVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000461 OS << ", Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000462 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000463 OS << ");\n";
464 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000465
466 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000467 OS << " }\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000468
Owen Anderson71669e52008-08-26 00:42:26 +0000469 }
470 // Return 0 if none of the predicates were satisfied.
471 if (HasPred)
472 OS << " return 0;\n";
473 OS << "}\n";
474 OS << "\n";
475 }
476
477 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000478 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000479 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000480 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000481 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000483 if (!Operands.empty())
484 OS << ", ";
485 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000487 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
488 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000490 OS << " case " << getName(RetVT) << ": return FastEmit_"
491 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
492 << "_" << getLegalCName(getName(RetVT)) << "_";
493 Operands.PrintManglingSuffix(OS);
494 OS << "(";
495 Operands.PrintArguments(OS);
496 OS << ");\n";
497 }
498 OS << " default: return 0;\n}\n}\n\n";
499
500 } else {
501 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000502 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000503 << getLegalCName(Opcode) << "_"
504 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000505 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000507 if (!Operands.empty())
508 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000509 Operands.PrintParameters(OS);
510 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000511
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000513 << ")\n return 0;\n";
514
Owen Anderson71669e52008-08-26 00:42:26 +0000515 const PredMap &PM = RM.begin()->second;
516 bool HasPred = false;
517
Owen Anderson7b2e5792008-08-25 23:43:09 +0000518 // Emit code for each possible instruction. There may be
519 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000520 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
521 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000522 std::string PredicateCheck = PI->first;
523 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000524
Owen Anderson7b2e5792008-08-25 23:43:09 +0000525 if (PredicateCheck.empty()) {
526 assert(!HasPred &&
527 "Multiple instructions match, at least one has "
528 "a predicate and at least one doesn't!");
529 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000530 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000531 OS << " ";
532 HasPred = true;
533 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000534
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000535 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
536 if ((*Memo.PhysRegs)[i] != "")
537 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
538 << "TII.get(TargetOpcode::COPY), "
539 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
540 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000541
Owen Anderson7b2e5792008-08-25 23:43:09 +0000542 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000543
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000544 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000545 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000546 OS << "(" << InstNS << Memo.Name << ", ";
547 OS << InstNS << Memo.RC->getName() << "RegisterClass";
548 if (!Operands.empty())
549 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000550 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000551 OS << ");\n";
552 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000553 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000554 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000555 OS << ");\n";
556 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000557
558 if (HasPred)
559 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000560 }
Owen Anderson71669e52008-08-26 00:42:26 +0000561
Owen Anderson7b2e5792008-08-25 23:43:09 +0000562 // Return 0 if none of the predicates were satisfied.
563 if (HasPred)
564 OS << " return 0;\n";
565 OS << "}\n";
566 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000567 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000568 }
569
570 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000571 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000572 << getLegalCName(Opcode) << "_";
573 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000575 if (!Operands.empty())
576 OS << ", ";
577 Operands.PrintParameters(OS);
578 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000580 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000581 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000583 std::string TypeName = getName(VT);
584 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000585 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
586 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000587 OS << "(RetVT";
588 if (!Operands.empty())
589 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000590 Operands.PrintArguments(OS);
591 OS << ");\n";
592 }
593 OS << " default: return 0;\n";
594 OS << " }\n";
595 OS << "}\n";
596 OS << "\n";
597 }
598
Dan Gohman0bfb7522008-08-22 00:28:15 +0000599 OS << "// Top-level FastEmit function.\n";
600 OS << "\n";
601
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000602 // Emit one function for the operand signature that demultiplexes based
603 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000604 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000605 Operands.PrintManglingSuffix(OS);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000606 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000607 if (!Operands.empty())
608 OS << ", ";
609 Operands.PrintParameters(OS);
610 OS << ") {\n";
611 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000612 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000613 I != E; ++I) {
614 const std::string &Opcode = I->first;
615
616 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000617 << getLegalCName(Opcode) << "_";
618 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000619 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000620 if (!Operands.empty())
621 OS << ", ";
622 Operands.PrintArguments(OS);
623 OS << ");\n";
624 }
625 OS << " default: return 0;\n";
626 OS << " }\n";
627 OS << "}\n";
628 OS << "\n";
629 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000630}
631
Daniel Dunbar1a551802009-07-03 00:10:29 +0000632void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000633 const CodeGenTarget &Target = CGP.getTargetInfo();
634
635 // Determine the target's namespace name.
636 std::string InstNS = Target.getInstNamespace() + "::";
637 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
638
639 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
640 Target.getName() + " target", OS);
641
Dan Gohman72d63af2008-08-26 21:21:20 +0000642 FastISelMap F(InstNS);
643 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000644 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000645}
646
647FastISelEmitter::FastISelEmitter(RecordKeeper &R)
648 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000649 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000650}
Dan Gohman72d63af2008-08-26 21:21:20 +0000651