blob: 08fc139b5d104c5a8169a44ee87b2dcc5b9f380a [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) {
Dan Gohman098d3a42010-05-27 16:25:05 +000057 if (!InstPatNode->isLeaf()) {
58 if (InstPatNode->getOperator()->getName() == "imm") {
59 Operands.push_back("i");
60 return true;
61 }
62 if (InstPatNode->getOperator()->getName() == "fpimm") {
63 Operands.push_back("f");
64 return true;
65 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000066 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +000067
Owen Andersonabb1f162008-08-26 01:22:59 +000068 const CodeGenRegisterClass *DstRC = 0;
69
Dan Gohmand1d2ee82008-08-19 20:56:30 +000070 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
71 TreePatternNode *Op = InstPatNode->getChild(i);
Dan Gohmand1d2ee82008-08-19 20:56:30 +000072 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +000073 // For now, filter out any operand with multiple values.
Chris Lattnerd7349192010-03-19 21:37:09 +000074 if (!Op->getPredicateFns().empty() ||
75 Op->getNumTypes() != 1)
Dan Gohmand5fe57d2008-08-21 01:41:07 +000076 return false;
Chris Lattnerd7349192010-03-19 21:37:09 +000077
78 assert(Op->hasTypeSet(0) && "Type infererence not done?");
79 // For now, all the operands must have the same type.
80 if (Op->getType(0) != VT)
81 return false;
82
Dan Gohmand5fe57d2008-08-21 01:41:07 +000083 if (!Op->isLeaf()) {
84 if (Op->getOperator()->getName() == "imm") {
85 Operands.push_back("i");
Dale Johannesenedc87742009-05-21 22:25:49 +000086 continue;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000087 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000088 if (Op->getOperator()->getName() == "fpimm") {
89 Operands.push_back("f");
Dale Johannesenedc87742009-05-21 22:25:49 +000090 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +000091 }
Dan Gohman833ddf82008-08-27 16:18:22 +000092 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +000093 return false;
94 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +000095 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
96 if (!OpDI)
97 return false;
98 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +000099 // For now, the only other thing we accept is register operands.
Evan Cheng98d2d072008-09-08 08:39:33 +0000100
Owen Anderson667d8f72008-08-29 17:45:56 +0000101 const CodeGenRegisterClass *RC = 0;
102 if (OpLeafRec->isSubClassOf("RegisterClass"))
103 RC = &Target.getRegisterClass(OpLeafRec);
104 else if (OpLeafRec->isSubClassOf("Register"))
105 RC = Target.getRegisterClassForRegister(OpLeafRec);
106 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000107 return false;
108 // For now, require the register operands' register classes to all
109 // be the same.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000110 if (!RC)
111 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000112 // For now, all the operands must have the same register class.
Owen Andersonabb1f162008-08-26 01:22:59 +0000113 if (DstRC) {
114 if (DstRC != RC)
115 return false;
116 } else
117 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000118 Operands.push_back("r");
119 }
120 return true;
121 }
122
Daniel Dunbar1a551802009-07-03 00:10:29 +0000123 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000124 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
125 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000126 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000127 } else if (Operands[i] == "i") {
128 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000129 } else if (Operands[i] == "f") {
130 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000131 } else {
132 assert("Unknown operand kind!");
133 abort();
134 }
135 if (i + 1 != e)
136 OS << ", ";
137 }
138 }
139
Daniel Dunbar1a551802009-07-03 00:10:29 +0000140 void PrintArguments(raw_ostream &OS,
Owen Anderson667d8f72008-08-29 17:45:56 +0000141 const std::vector<std::string>& PR) const {
142 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000143 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000144 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000145 if (PR[i] != "")
146 // Implicit physical register operand.
147 continue;
148
149 if (PrintedArg)
150 OS << ", ";
151 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000152 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000153 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000154 } else if (Operands[i] == "i") {
155 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000156 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000157 } else if (Operands[i] == "f") {
158 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000159 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000160 } else {
161 assert("Unknown operand kind!");
162 abort();
163 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000164 }
165 }
166
Daniel Dunbar1a551802009-07-03 00:10:29 +0000167 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000168 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
169 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000170 OS << "Op" << i << ", Op" << i << "IsKill";
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000171 } else if (Operands[i] == "i") {
172 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000173 } else if (Operands[i] == "f") {
174 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000175 } else {
176 assert("Unknown operand kind!");
177 abort();
178 }
179 if (i + 1 != e)
180 OS << ", ";
181 }
182 }
183
Owen Anderson667d8f72008-08-29 17:45:56 +0000184
Daniel Dunbar1a551802009-07-03 00:10:29 +0000185 void PrintManglingSuffix(raw_ostream &OS,
Evan Cheng98d2d072008-09-08 08:39:33 +0000186 const std::vector<std::string>& PR) const {
187 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
188 if (PR[i] != "")
189 // Implicit physical register operand. e.g. Instruction::Mul expect to
190 // select to a binary op. On x86, mul may take a single operand with
191 // the other operand being implicit. We must emit something that looks
192 // like a binary instruction except for the very inner FastEmitInst_*
193 // call.
194 continue;
195 OS << Operands[i];
196 }
197 }
198
Daniel Dunbar1a551802009-07-03 00:10:29 +0000199 void PrintManglingSuffix(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000200 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
201 OS << Operands[i];
202 }
203 }
204};
205
Dan Gohman72d63af2008-08-26 21:21:20 +0000206class FastISelMap {
207 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
209 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000210 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
211 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> OperandsOpcodeTypeRetPredMap;
212
213 OperandsOpcodeTypeRetPredMap SimplePatterns;
214
215 std::string InstNS;
216
217public:
218 explicit FastISelMap(std::string InstNS);
219
220 void CollectPatterns(CodeGenDAGPatterns &CGP);
Daniel Dunbar1a551802009-07-03 00:10:29 +0000221 void PrintFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000222};
223
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000224}
225
226static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
227 return CGP.getSDNodeInfo(Op).getEnumName();
228}
229
230static std::string getLegalCName(std::string OpName) {
231 std::string::size_type pos = OpName.find("::");
232 if (pos != std::string::npos)
233 OpName.replace(pos, 2, "_");
234 return OpName;
235}
236
Dan Gohman72d63af2008-08-26 21:21:20 +0000237FastISelMap::FastISelMap(std::string instns)
238 : InstNS(instns) {
239}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000240
Dan Gohman72d63af2008-08-26 21:21:20 +0000241void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
242 const CodeGenTarget &Target = CGP.getTargetInfo();
243
244 // Determine the target's namespace name.
245 InstNS = Target.getInstNamespace() + "::";
246 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000247
Dan Gohman0bfb7522008-08-22 00:28:15 +0000248 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000249 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
250 E = CGP.ptm_end(); I != E; ++I) {
251 const PatternToMatch &Pattern = *I;
252
253 // For now, just look at Instructions, so that we don't have to worry
254 // about emitting multiple instructions for a pattern.
255 TreePatternNode *Dst = Pattern.getDstPattern();
256 if (Dst->isLeaf()) continue;
257 Record *Op = Dst->getOperator();
258 if (!Op->isSubClassOf("Instruction"))
259 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000260 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000261 if (II.OperandList.empty())
262 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000263
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000264 // For now, ignore multi-instruction patterns.
265 bool MultiInsts = false;
266 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
267 TreePatternNode *ChildOp = Dst->getChild(i);
268 if (ChildOp->isLeaf())
269 continue;
270 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
271 MultiInsts = true;
272 break;
273 }
274 }
275 if (MultiInsts)
276 continue;
277
Dan Gohman379cad42008-08-19 20:36:33 +0000278 // For now, ignore instructions where the first operand is not an
279 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000280 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000281 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000282 if (Op->getName() != "EXTRACT_SUBREG") {
283 Record *Op0Rec = II.OperandList[0].Rec;
284 if (!Op0Rec->isSubClassOf("RegisterClass"))
285 continue;
286 DstRC = &Target.getRegisterClass(Op0Rec);
287 if (!DstRC)
288 continue;
289 } else {
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000290 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
291 if (SR)
292 SubRegNo = getQualifiedName(SR->getDef());
293 else
294 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000295 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000296
297 // Inspect the pattern.
298 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
299 if (!InstPatNode) continue;
300 if (InstPatNode->isLeaf()) continue;
301
Chris Lattner084df622010-03-24 00:41:19 +0000302 // Ignore multiple result nodes for now.
303 if (InstPatNode->getNumTypes() > 1) continue;
304
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000305 Record *InstPatOp = InstPatNode->getOperator();
306 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000307 MVT::SimpleValueType RetVT = MVT::isVoid;
308 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000310 if (InstPatNode->getNumChildren()) {
311 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
312 VT = InstPatNode->getChild(0)->getType(0);
313 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000314
315 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000316 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000317 if (InstPatOp->isSubClassOf("Operand"))
318 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000319
320 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000321 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000322 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000323
Dan Gohman379cad42008-08-19 20:36:33 +0000324 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000325 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000326 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000327 continue;
Owen Anderson667d8f72008-08-29 17:45:56 +0000328
329 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
330 if (!InstPatNode->isLeaf() &&
331 (InstPatNode->getOperator()->getName() == "imm" ||
332 InstPatNode->getOperator()->getName() == "fpimmm"))
333 PhysRegInputs->push_back("");
334 else if (!InstPatNode->isLeaf()) {
335 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
336 TreePatternNode *Op = InstPatNode->getChild(i);
337 if (!Op->isLeaf()) {
338 PhysRegInputs->push_back("");
339 continue;
340 }
341
342 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
343 Record *OpLeafRec = OpDI->getDef();
344 std::string PhysReg;
345 if (OpLeafRec->isSubClassOf("Register")) {
346 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
347 "Namespace")->getValue())->getValue();
348 PhysReg += "::";
349
350 std::vector<CodeGenRegister> Regs = Target.getRegisters();
351 for (unsigned i = 0; i < Regs.size(); ++i) {
352 if (Regs[i].TheDef == OpLeafRec) {
353 PhysReg += Regs[i].getName();
354 break;
355 }
356 }
357 }
358
359 PhysRegInputs->push_back(PhysReg);
360 }
361 } else
362 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000363
Dan Gohman22bb3112008-08-22 00:20:26 +0000364 // Get the predicate that guards this pattern.
365 std::string PredicateCheck = Pattern.getPredicateCheck();
366
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000367 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000368 InstructionMemo Memo = {
369 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000370 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000371 SubRegNo,
372 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000373 };
Owen Andersonabb1f162008-08-26 01:22:59 +0000374 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000375 "Duplicate pattern!");
Owen Andersonabb1f162008-08-26 01:22:59 +0000376 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000377 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000378}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000379
Daniel Dunbar1a551802009-07-03 00:10:29 +0000380void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000381 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000382 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000383 OE = SimplePatterns.end(); OI != OE; ++OI) {
384 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000385 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000386
Owen Anderson7b2e5792008-08-25 23:43:09 +0000387 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000388 I != E; ++I) {
389 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000390 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000391
392 OS << "// FastEmit functions for " << Opcode << ".\n";
393 OS << "\n";
394
395 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000396 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000397 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000399 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000400 if (RM.size() != 1) {
401 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
402 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000404 const PredMap &PM = RI->second;
405 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000406
Evan Chengc3f44b02008-09-03 00:03:49 +0000407 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000408 << getLegalCName(Opcode)
409 << "_" << getLegalCName(getName(VT))
410 << "_" << getLegalCName(getName(RetVT)) << "_";
411 Operands.PrintManglingSuffix(OS);
412 OS << "(";
413 Operands.PrintParameters(OS);
414 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000415
Owen Anderson71669e52008-08-26 00:42:26 +0000416 // Emit code for each possible instruction. There may be
417 // multiple if there are subtarget concerns.
418 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
419 PI != PE; ++PI) {
420 std::string PredicateCheck = PI->first;
421 const InstructionMemo &Memo = PI->second;
422
423 if (PredicateCheck.empty()) {
424 assert(!HasPred &&
425 "Multiple instructions match, at least one has "
426 "a predicate and at least one doesn't!");
427 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000428 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000429 OS << " ";
430 HasPred = true;
431 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000432
433 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
434 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000435 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
436 << "TII.get(TargetOpcode::COPY), "
437 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000438 }
439
Owen Anderson71669e52008-08-26 00:42:26 +0000440 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000441 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000442 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000443 OS << "(" << InstNS << Memo.Name << ", ";
444 OS << InstNS << Memo.RC->getName() << "RegisterClass";
445 if (!Operands.empty())
446 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000447 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000448 OS << ");\n";
449 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000450 OS << "extractsubreg(" << getName(RetVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000451 OS << ", Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000452 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000453 OS << ");\n";
454 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000455
456 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000457 OS << " }\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000458
Owen Anderson71669e52008-08-26 00:42:26 +0000459 }
460 // Return 0 if none of the predicates were satisfied.
461 if (HasPred)
462 OS << " return 0;\n";
463 OS << "}\n";
464 OS << "\n";
465 }
466
467 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000468 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000469 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000470 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000471 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000473 if (!Operands.empty())
474 OS << ", ";
475 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000477 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
478 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000480 OS << " case " << getName(RetVT) << ": return FastEmit_"
481 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
482 << "_" << getLegalCName(getName(RetVT)) << "_";
483 Operands.PrintManglingSuffix(OS);
484 OS << "(";
485 Operands.PrintArguments(OS);
486 OS << ");\n";
487 }
488 OS << " default: return 0;\n}\n}\n\n";
489
490 } else {
491 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000492 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000493 << getLegalCName(Opcode) << "_"
494 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000495 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000497 if (!Operands.empty())
498 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000499 Operands.PrintParameters(OS);
500 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000501
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000503 << ")\n return 0;\n";
504
Owen Anderson71669e52008-08-26 00:42:26 +0000505 const PredMap &PM = RM.begin()->second;
506 bool HasPred = false;
507
Owen Anderson7b2e5792008-08-25 23:43:09 +0000508 // Emit code for each possible instruction. There may be
509 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000510 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
511 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000512 std::string PredicateCheck = PI->first;
513 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000514
Owen Anderson7b2e5792008-08-25 23:43:09 +0000515 if (PredicateCheck.empty()) {
516 assert(!HasPred &&
517 "Multiple instructions match, at least one has "
518 "a predicate and at least one doesn't!");
519 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000520 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000521 OS << " ";
522 HasPred = true;
523 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000524
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000525 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
526 if ((*Memo.PhysRegs)[i] != "")
527 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
528 << "TII.get(TargetOpcode::COPY), "
529 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
530 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000531
Owen Anderson7b2e5792008-08-25 23:43:09 +0000532 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000533
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000534 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000535 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000536 OS << "(" << InstNS << Memo.Name << ", ";
537 OS << InstNS << Memo.RC->getName() << "RegisterClass";
538 if (!Operands.empty())
539 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000540 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000541 OS << ");\n";
542 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000543 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000544 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000545 OS << ");\n";
546 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000547
548 if (HasPred)
549 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000550 }
Owen Anderson71669e52008-08-26 00:42:26 +0000551
Owen Anderson7b2e5792008-08-25 23:43:09 +0000552 // Return 0 if none of the predicates were satisfied.
553 if (HasPred)
554 OS << " return 0;\n";
555 OS << "}\n";
556 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000557 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000558 }
559
560 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000561 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000562 << getLegalCName(Opcode) << "_";
563 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000565 if (!Operands.empty())
566 OS << ", ";
567 Operands.PrintParameters(OS);
568 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000570 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000571 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000573 std::string TypeName = getName(VT);
574 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000575 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
576 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000577 OS << "(RetVT";
578 if (!Operands.empty())
579 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000580 Operands.PrintArguments(OS);
581 OS << ");\n";
582 }
583 OS << " default: return 0;\n";
584 OS << " }\n";
585 OS << "}\n";
586 OS << "\n";
587 }
588
Dan Gohman0bfb7522008-08-22 00:28:15 +0000589 OS << "// Top-level FastEmit function.\n";
590 OS << "\n";
591
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000592 // Emit one function for the operand signature that demultiplexes based
593 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000594 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000595 Operands.PrintManglingSuffix(OS);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000596 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000597 if (!Operands.empty())
598 OS << ", ";
599 Operands.PrintParameters(OS);
600 OS << ") {\n";
601 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000602 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000603 I != E; ++I) {
604 const std::string &Opcode = I->first;
605
606 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000607 << getLegalCName(Opcode) << "_";
608 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000609 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000610 if (!Operands.empty())
611 OS << ", ";
612 Operands.PrintArguments(OS);
613 OS << ");\n";
614 }
615 OS << " default: return 0;\n";
616 OS << " }\n";
617 OS << "}\n";
618 OS << "\n";
619 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000620}
621
Daniel Dunbar1a551802009-07-03 00:10:29 +0000622void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000623 const CodeGenTarget &Target = CGP.getTargetInfo();
624
625 // Determine the target's namespace name.
626 std::string InstNS = Target.getInstNamespace() + "::";
627 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
628
629 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
630 Target.getName() + " target", OS);
631
Dan Gohman72d63af2008-08-26 21:21:20 +0000632 FastISelMap F(InstNS);
633 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000634 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000635}
636
637FastISelEmitter::FastISelEmitter(RecordKeeper &R)
638 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000639 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000640}
Dan Gohman72d63af2008-08-26 21:21:20 +0000641