blob: 70448ab92aead727d1015f04a446b07b61d4002e [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
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000111 // For now, require the register operands' register classes to all
112 // be the same.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000113 if (!RC)
114 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000115 // For now, all the operands must have the same register class.
Owen Andersonabb1f162008-08-26 01:22:59 +0000116 if (DstRC) {
117 if (DstRC != RC)
118 return false;
119 } else
120 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000121 Operands.push_back("r");
122 }
123 return true;
124 }
125
Daniel Dunbar1a551802009-07-03 00:10:29 +0000126 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000127 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
128 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000129 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000130 } else if (Operands[i] == "i") {
131 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000132 } else if (Operands[i] == "f") {
133 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000134 } else {
135 assert("Unknown operand kind!");
136 abort();
137 }
138 if (i + 1 != e)
139 OS << ", ";
140 }
141 }
142
Daniel Dunbar1a551802009-07-03 00:10:29 +0000143 void PrintArguments(raw_ostream &OS,
Owen Anderson667d8f72008-08-29 17:45:56 +0000144 const std::vector<std::string>& PR) const {
145 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000146 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000147 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000148 if (PR[i] != "")
149 // Implicit physical register operand.
150 continue;
151
152 if (PrintedArg)
153 OS << ", ";
154 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000155 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000156 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000157 } else if (Operands[i] == "i") {
158 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000159 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000160 } else if (Operands[i] == "f") {
161 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000162 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000163 } else {
164 assert("Unknown operand kind!");
165 abort();
166 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000167 }
168 }
169
Daniel Dunbar1a551802009-07-03 00:10:29 +0000170 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000171 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
172 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000173 OS << "Op" << i << ", Op" << i << "IsKill";
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000174 } else if (Operands[i] == "i") {
175 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000176 } else if (Operands[i] == "f") {
177 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000178 } else {
179 assert("Unknown operand kind!");
180 abort();
181 }
182 if (i + 1 != e)
183 OS << ", ";
184 }
185 }
186
Owen Anderson667d8f72008-08-29 17:45:56 +0000187
Daniel Dunbar1a551802009-07-03 00:10:29 +0000188 void PrintManglingSuffix(raw_ostream &OS,
Evan Cheng98d2d072008-09-08 08:39:33 +0000189 const std::vector<std::string>& PR) const {
190 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
191 if (PR[i] != "")
192 // Implicit physical register operand. e.g. Instruction::Mul expect to
193 // select to a binary op. On x86, mul may take a single operand with
194 // the other operand being implicit. We must emit something that looks
195 // like a binary instruction except for the very inner FastEmitInst_*
196 // call.
197 continue;
198 OS << Operands[i];
199 }
200 }
201
Daniel Dunbar1a551802009-07-03 00:10:29 +0000202 void PrintManglingSuffix(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000203 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
204 OS << Operands[i];
205 }
206 }
207};
208
Dan Gohman72d63af2008-08-26 21:21:20 +0000209class FastISelMap {
210 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
212 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000213 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Eric Christopherecfa0792010-07-26 17:53:07 +0000214 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
215 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000216
217 OperandsOpcodeTypeRetPredMap SimplePatterns;
218
219 std::string InstNS;
220
221public:
222 explicit FastISelMap(std::string InstNS);
223
224 void CollectPatterns(CodeGenDAGPatterns &CGP);
Daniel Dunbar1a551802009-07-03 00:10:29 +0000225 void PrintFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000226};
227
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000228}
229
230static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
231 return CGP.getSDNodeInfo(Op).getEnumName();
232}
233
234static std::string getLegalCName(std::string OpName) {
235 std::string::size_type pos = OpName.find("::");
236 if (pos != std::string::npos)
237 OpName.replace(pos, 2, "_");
238 return OpName;
239}
240
Dan Gohman72d63af2008-08-26 21:21:20 +0000241FastISelMap::FastISelMap(std::string instns)
242 : InstNS(instns) {
243}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000244
Dan Gohman72d63af2008-08-26 21:21:20 +0000245void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
246 const CodeGenTarget &Target = CGP.getTargetInfo();
247
248 // Determine the target's namespace name.
249 InstNS = Target.getInstNamespace() + "::";
250 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000251
Dan Gohman0bfb7522008-08-22 00:28:15 +0000252 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000253 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
254 E = CGP.ptm_end(); I != E; ++I) {
255 const PatternToMatch &Pattern = *I;
256
257 // For now, just look at Instructions, so that we don't have to worry
258 // about emitting multiple instructions for a pattern.
259 TreePatternNode *Dst = Pattern.getDstPattern();
260 if (Dst->isLeaf()) continue;
261 Record *Op = Dst->getOperator();
262 if (!Op->isSubClassOf("Instruction"))
263 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000264 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000265 if (II.OperandList.empty())
266 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000267
Eric Christopherbc168272010-07-28 01:52:23 +0000268 // For now ignore instructions that have predicate operands.
269 bool HasPredicate = false;
270 for (unsigned i = 0, e = II.OperandList.size(); i != e; ++i) {
271 if(II.OperandList[i].Rec->isSubClassOf("PredicateOperand"))
272 HasPredicate = true;
273 }
274 if (HasPredicate)
275 continue;
276
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000277 // For now, ignore multi-instruction patterns.
278 bool MultiInsts = false;
279 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
280 TreePatternNode *ChildOp = Dst->getChild(i);
281 if (ChildOp->isLeaf())
282 continue;
283 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
284 MultiInsts = true;
285 break;
286 }
287 }
288 if (MultiInsts)
289 continue;
290
Dan Gohman379cad42008-08-19 20:36:33 +0000291 // For now, ignore instructions where the first operand is not an
292 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000293 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000294 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000295 if (Op->getName() != "EXTRACT_SUBREG") {
296 Record *Op0Rec = II.OperandList[0].Rec;
297 if (!Op0Rec->isSubClassOf("RegisterClass"))
298 continue;
299 DstRC = &Target.getRegisterClass(Op0Rec);
300 if (!DstRC)
301 continue;
302 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000303 // If this isn't a leaf, then continue since the register classes are
304 // a bit too complicated for now.
305 if (!Dst->getChild(1)->isLeaf()) continue;
306
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000307 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
308 if (SR)
309 SubRegNo = getQualifiedName(SR->getDef());
310 else
311 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000312 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000313
314 // Inspect the pattern.
315 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
316 if (!InstPatNode) continue;
317 if (InstPatNode->isLeaf()) continue;
318
Chris Lattner084df622010-03-24 00:41:19 +0000319 // Ignore multiple result nodes for now.
320 if (InstPatNode->getNumTypes() > 1) continue;
321
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000322 Record *InstPatOp = InstPatNode->getOperator();
323 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000324 MVT::SimpleValueType RetVT = MVT::isVoid;
325 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000327 if (InstPatNode->getNumChildren()) {
328 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
329 VT = InstPatNode->getChild(0)->getType(0);
330 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000331
332 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000333 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000334 if (InstPatOp->isSubClassOf("Operand"))
335 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000336
337 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000338 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000339 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000340
Dan Gohman379cad42008-08-19 20:36:33 +0000341 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000342 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000343 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000344 continue;
Owen Anderson667d8f72008-08-29 17:45:56 +0000345
346 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
347 if (!InstPatNode->isLeaf() &&
348 (InstPatNode->getOperator()->getName() == "imm" ||
349 InstPatNode->getOperator()->getName() == "fpimmm"))
350 PhysRegInputs->push_back("");
351 else if (!InstPatNode->isLeaf()) {
352 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
353 TreePatternNode *Op = InstPatNode->getChild(i);
354 if (!Op->isLeaf()) {
355 PhysRegInputs->push_back("");
356 continue;
357 }
358
359 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
360 Record *OpLeafRec = OpDI->getDef();
361 std::string PhysReg;
362 if (OpLeafRec->isSubClassOf("Register")) {
363 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
364 "Namespace")->getValue())->getValue();
365 PhysReg += "::";
366
367 std::vector<CodeGenRegister> Regs = Target.getRegisters();
368 for (unsigned i = 0; i < Regs.size(); ++i) {
369 if (Regs[i].TheDef == OpLeafRec) {
370 PhysReg += Regs[i].getName();
371 break;
372 }
373 }
374 }
375
376 PhysRegInputs->push_back(PhysReg);
377 }
378 } else
379 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000380
Dan Gohman22bb3112008-08-22 00:20:26 +0000381 // Get the predicate that guards this pattern.
382 std::string PredicateCheck = Pattern.getPredicateCheck();
383
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000384 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000385 InstructionMemo Memo = {
386 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000387 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000388 SubRegNo,
389 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000390 };
Eric Christopherecfa0792010-07-26 17:53:07 +0000391 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT]
392 .count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000393 "Duplicate pattern!");
Owen Andersonabb1f162008-08-26 01:22:59 +0000394 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000395 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000396}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000397
Daniel Dunbar1a551802009-07-03 00:10:29 +0000398void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000399 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000400 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000401 OE = SimplePatterns.end(); OI != OE; ++OI) {
402 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000403 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000404
Owen Anderson7b2e5792008-08-25 23:43:09 +0000405 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000406 I != E; ++I) {
407 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000408 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000409
410 OS << "// FastEmit functions for " << Opcode << ".\n";
411 OS << "\n";
412
413 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000414 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000415 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000417 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000418 if (RM.size() != 1) {
419 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
420 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000422 const PredMap &PM = RI->second;
423 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000424
Evan Chengc3f44b02008-09-03 00:03:49 +0000425 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000426 << getLegalCName(Opcode)
427 << "_" << getLegalCName(getName(VT))
428 << "_" << getLegalCName(getName(RetVT)) << "_";
429 Operands.PrintManglingSuffix(OS);
430 OS << "(";
431 Operands.PrintParameters(OS);
432 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000433
Owen Anderson71669e52008-08-26 00:42:26 +0000434 // Emit code for each possible instruction. There may be
435 // multiple if there are subtarget concerns.
436 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
437 PI != PE; ++PI) {
438 std::string PredicateCheck = PI->first;
439 const InstructionMemo &Memo = PI->second;
440
441 if (PredicateCheck.empty()) {
442 assert(!HasPred &&
443 "Multiple instructions match, at least one has "
444 "a predicate and at least one doesn't!");
445 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000446 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000447 OS << " ";
448 HasPred = true;
449 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000450
451 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
452 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000453 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
454 << "TII.get(TargetOpcode::COPY), "
455 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000456 }
457
Owen Anderson71669e52008-08-26 00:42:26 +0000458 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000459 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000460 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000461 OS << "(" << InstNS << Memo.Name << ", ";
462 OS << InstNS << Memo.RC->getName() << "RegisterClass";
463 if (!Operands.empty())
464 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000465 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000466 OS << ");\n";
467 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000468 OS << "extractsubreg(" << getName(RetVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000469 OS << ", Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000470 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000471 OS << ");\n";
472 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000473
474 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000475 OS << " }\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000476
Owen Anderson71669e52008-08-26 00:42:26 +0000477 }
478 // Return 0 if none of the predicates were satisfied.
479 if (HasPred)
480 OS << " return 0;\n";
481 OS << "}\n";
482 OS << "\n";
483 }
484
485 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000486 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000487 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000488 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000489 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000491 if (!Operands.empty())
492 OS << ", ";
493 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000495 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
496 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000498 OS << " case " << getName(RetVT) << ": return FastEmit_"
499 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
500 << "_" << getLegalCName(getName(RetVT)) << "_";
501 Operands.PrintManglingSuffix(OS);
502 OS << "(";
503 Operands.PrintArguments(OS);
504 OS << ");\n";
505 }
506 OS << " default: return 0;\n}\n}\n\n";
507
508 } else {
509 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000510 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000511 << getLegalCName(Opcode) << "_"
512 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000513 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000515 if (!Operands.empty())
516 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000517 Operands.PrintParameters(OS);
518 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000519
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000521 << ")\n return 0;\n";
522
Owen Anderson71669e52008-08-26 00:42:26 +0000523 const PredMap &PM = RM.begin()->second;
524 bool HasPred = false;
525
Owen Anderson7b2e5792008-08-25 23:43:09 +0000526 // Emit code for each possible instruction. There may be
527 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000528 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
529 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000530 std::string PredicateCheck = PI->first;
531 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000532
Owen Anderson7b2e5792008-08-25 23:43:09 +0000533 if (PredicateCheck.empty()) {
534 assert(!HasPred &&
535 "Multiple instructions match, at least one has "
536 "a predicate and at least one doesn't!");
537 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000538 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000539 OS << " ";
540 HasPred = true;
541 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000542
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000543 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
544 if ((*Memo.PhysRegs)[i] != "")
545 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
546 << "TII.get(TargetOpcode::COPY), "
547 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
548 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000549
Owen Anderson7b2e5792008-08-25 23:43:09 +0000550 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000551
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000552 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000553 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000554 OS << "(" << InstNS << Memo.Name << ", ";
555 OS << InstNS << Memo.RC->getName() << "RegisterClass";
556 if (!Operands.empty())
557 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000558 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000559 OS << ");\n";
560 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000561 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000562 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000563 OS << ");\n";
564 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000565
566 if (HasPred)
567 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000568 }
Owen Anderson71669e52008-08-26 00:42:26 +0000569
Owen Anderson7b2e5792008-08-25 23:43:09 +0000570 // Return 0 if none of the predicates were satisfied.
571 if (HasPred)
572 OS << " return 0;\n";
573 OS << "}\n";
574 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000575 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000576 }
577
578 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000579 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000580 << getLegalCName(Opcode) << "_";
581 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000583 if (!Operands.empty())
584 OS << ", ";
585 Operands.PrintParameters(OS);
586 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000588 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000589 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000591 std::string TypeName = getName(VT);
592 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000593 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
594 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000595 OS << "(RetVT";
596 if (!Operands.empty())
597 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000598 Operands.PrintArguments(OS);
599 OS << ");\n";
600 }
601 OS << " default: return 0;\n";
602 OS << " }\n";
603 OS << "}\n";
604 OS << "\n";
605 }
606
Dan Gohman0bfb7522008-08-22 00:28:15 +0000607 OS << "// Top-level FastEmit function.\n";
608 OS << "\n";
609
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000610 // Emit one function for the operand signature that demultiplexes based
611 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000612 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000613 Operands.PrintManglingSuffix(OS);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000614 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000615 if (!Operands.empty())
616 OS << ", ";
617 Operands.PrintParameters(OS);
618 OS << ") {\n";
619 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000620 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000621 I != E; ++I) {
622 const std::string &Opcode = I->first;
623
624 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000625 << getLegalCName(Opcode) << "_";
626 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000627 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000628 if (!Operands.empty())
629 OS << ", ";
630 Operands.PrintArguments(OS);
631 OS << ");\n";
632 }
633 OS << " default: return 0;\n";
634 OS << " }\n";
635 OS << "}\n";
636 OS << "\n";
637 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000638}
639
Daniel Dunbar1a551802009-07-03 00:10:29 +0000640void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000641 const CodeGenTarget &Target = CGP.getTargetInfo();
642
643 // Determine the target's namespace name.
644 std::string InstNS = Target.getInstNamespace() + "::";
645 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
646
647 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
648 Target.getName() + " target", OS);
649
Dan Gohman72d63af2008-08-26 21:21:20 +0000650 FastISelMap F(InstNS);
651 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000652 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000653}
654
655FastISelEmitter::FastISelEmitter(RecordKeeper &R)
656 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000657 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000658}
Dan Gohman72d63af2008-08-26 21:21:20 +0000659