blob: b6631c848e0d9a9085da1baced8153df8ebc6571 [file] [log] [blame]
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001//===- FastISelEmitter.cpp - Generate an instruction selector -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000010// This tablegen backend emits code for use by the "fast" instruction
11// selection algorithm. See the comments at the top of
12// lib/CodeGen/SelectionDAG/FastISel.cpp for background.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000013//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000014// This file scans through the target's tablegen instruction-info files
15// and extracts instructions with obvious-looking patterns, and it emits
16// code to look up these instructions by type and operator.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000017//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000018//===----------------------------------------------------------------------===//
19
20#include "FastISelEmitter.h"
21#include "Record.h"
22#include "llvm/Support/Debug.h"
Jim Grosbach76612b52010-12-07 19:35:36 +000023#include "llvm/ADT/SmallString.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000024#include "llvm/ADT/VectorExtras.h"
25using namespace llvm;
26
27namespace {
28
Owen Anderson667d8f72008-08-29 17:45:56 +000029/// InstructionMemo - This class holds additional information about an
30/// instruction needed to emit code for it.
31///
32struct InstructionMemo {
33 std::string Name;
34 const CodeGenRegisterClass *RC;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +000035 std::string SubRegNo;
Owen Anderson667d8f72008-08-29 17:45:56 +000036 std::vector<std::string>* PhysRegs;
37};
38
Dan Gohman04b7dfb2008-08-19 18:06:12 +000039/// OperandsSignature - This class holds a description of a list of operand
40/// types. It has utility methods for emitting text based on the operands.
41///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000042struct OperandsSignature {
43 std::vector<std::string> Operands;
44
45 bool operator<(const OperandsSignature &O) const {
46 return Operands < O.Operands;
47 }
48
49 bool empty() const { return Operands.empty(); }
50
Dan Gohmand1d2ee82008-08-19 20:56:30 +000051 /// initialize - Examine the given pattern and initialize the contents
52 /// of the Operands array accordingly. Return true if all the operands
53 /// are supported, false otherwise.
54 ///
Chris Lattner602fc062011-04-17 20:23:29 +000055 bool initialize(TreePatternNode *InstPatNode, 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 }
Jim Grosbach45258f52010-12-07 19:36:07 +000068
Owen Andersonabb1f162008-08-26 01:22:59 +000069 const CodeGenRegisterClass *DstRC = 0;
Jim Grosbach45258f52010-12-07 19:36:07 +000070
Dan Gohmand1d2ee82008-08-19 20:56:30 +000071 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
72 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach45258f52010-12-07 19:36:07 +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 Lattner602fc062011-04-17 20:23:29 +000076 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerd7349192010-03-19 21:37:09 +000077 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +000078
Dan Gohmand5fe57d2008-08-21 01:41:07 +000079 if (!Op->isLeaf()) {
80 if (Op->getOperator()->getName() == "imm") {
81 Operands.push_back("i");
Dale Johannesenedc87742009-05-21 22:25:49 +000082 continue;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000083 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000084 if (Op->getOperator()->getName() == "fpimm") {
85 Operands.push_back("f");
Dale Johannesenedc87742009-05-21 22:25:49 +000086 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +000087 }
Dan Gohman833ddf82008-08-27 16:18:22 +000088 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +000089 return false;
90 }
Chris Lattner602fc062011-04-17 20:23:29 +000091
92 assert(Op->hasTypeSet(0) && "Type infererence not done?");
93
94 // For now, all the operands must have the same type (if they aren't
95 // immediates). Note that this causes us to reject variable sized shifts
96 // on X86.
97 if (Op->getType(0) != VT)
98 return false;
99
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000100 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
101 if (!OpDI)
102 return false;
103 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000104 // For now, the only other thing we accept is register operands.
Evan Cheng98d2d072008-09-08 08:39:33 +0000105
Owen Anderson667d8f72008-08-29 17:45:56 +0000106 const CodeGenRegisterClass *RC = 0;
107 if (OpLeafRec->isSubClassOf("RegisterClass"))
108 RC = &Target.getRegisterClass(OpLeafRec);
109 else if (OpLeafRec->isSubClassOf("Register"))
110 RC = Target.getRegisterClassForRegister(OpLeafRec);
111 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000112 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000113
Eric Christopher2cfcad92010-08-24 23:21:59 +0000114 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000115 if (!RC)
116 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000117
Eric Christopher53452602010-08-25 04:58:56 +0000118 // For now, all the operands must have the same register class or be
119 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000120 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000121 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000122 return false;
123 } else
124 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000125 Operands.push_back("r");
126 }
127 return true;
128 }
129
Daniel Dunbar1a551802009-07-03 00:10:29 +0000130 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000131 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
132 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000133 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000134 } else if (Operands[i] == "i") {
135 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000136 } else if (Operands[i] == "f") {
137 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000138 } else {
139 assert("Unknown operand kind!");
140 abort();
141 }
142 if (i + 1 != e)
143 OS << ", ";
144 }
145 }
146
Daniel Dunbar1a551802009-07-03 00:10:29 +0000147 void PrintArguments(raw_ostream &OS,
Owen Anderson667d8f72008-08-29 17:45:56 +0000148 const std::vector<std::string>& PR) const {
149 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000150 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000151 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000152 if (PR[i] != "")
153 // Implicit physical register operand.
154 continue;
155
156 if (PrintedArg)
157 OS << ", ";
158 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000159 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000160 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000161 } else if (Operands[i] == "i") {
162 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000163 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000164 } else if (Operands[i] == "f") {
165 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000166 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000167 } else {
168 assert("Unknown operand kind!");
169 abort();
170 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000171 }
172 }
173
Daniel Dunbar1a551802009-07-03 00:10:29 +0000174 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000175 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
176 if (Operands[i] == "r") {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000177 OS << "Op" << i << ", Op" << i << "IsKill";
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000178 } else if (Operands[i] == "i") {
179 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000180 } else if (Operands[i] == "f") {
181 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000182 } else {
183 assert("Unknown operand kind!");
184 abort();
185 }
186 if (i + 1 != e)
187 OS << ", ";
188 }
189 }
190
Owen Anderson667d8f72008-08-29 17:45:56 +0000191
Daniel Dunbar1a551802009-07-03 00:10:29 +0000192 void PrintManglingSuffix(raw_ostream &OS,
Evan Cheng98d2d072008-09-08 08:39:33 +0000193 const std::vector<std::string>& PR) const {
194 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
195 if (PR[i] != "")
196 // Implicit physical register operand. e.g. Instruction::Mul expect to
197 // select to a binary op. On x86, mul may take a single operand with
198 // the other operand being implicit. We must emit something that looks
199 // like a binary instruction except for the very inner FastEmitInst_*
200 // call.
201 continue;
202 OS << Operands[i];
203 }
204 }
205
Daniel Dunbar1a551802009-07-03 00:10:29 +0000206 void PrintManglingSuffix(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000207 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
208 OS << Operands[i];
209 }
210 }
211};
212
Dan Gohman72d63af2008-08-26 21:21:20 +0000213class FastISelMap {
214 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
216 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000217 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach45258f52010-12-07 19:36:07 +0000218 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopherecfa0792010-07-26 17:53:07 +0000219 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000220
221 OperandsOpcodeTypeRetPredMap SimplePatterns;
222
223 std::string InstNS;
224
225public:
226 explicit FastISelMap(std::string InstNS);
227
228 void CollectPatterns(CodeGenDAGPatterns &CGP);
Daniel Dunbar1a551802009-07-03 00:10:29 +0000229 void PrintFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000230};
231
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000232}
233
234static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
235 return CGP.getSDNodeInfo(Op).getEnumName();
236}
237
238static std::string getLegalCName(std::string OpName) {
239 std::string::size_type pos = OpName.find("::");
240 if (pos != std::string::npos)
241 OpName.replace(pos, 2, "_");
242 return OpName;
243}
244
Dan Gohman72d63af2008-08-26 21:21:20 +0000245FastISelMap::FastISelMap(std::string instns)
246 : InstNS(instns) {
247}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000248
Dan Gohman72d63af2008-08-26 21:21:20 +0000249void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
250 const CodeGenTarget &Target = CGP.getTargetInfo();
251
252 // Determine the target's namespace name.
253 InstNS = Target.getInstNamespace() + "::";
254 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000255
Dan Gohman0bfb7522008-08-22 00:28:15 +0000256 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000257 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
258 E = CGP.ptm_end(); I != E; ++I) {
259 const PatternToMatch &Pattern = *I;
260
261 // For now, just look at Instructions, so that we don't have to worry
262 // about emitting multiple instructions for a pattern.
263 TreePatternNode *Dst = Pattern.getDstPattern();
264 if (Dst->isLeaf()) continue;
265 Record *Op = Dst->getOperator();
266 if (!Op->isSubClassOf("Instruction"))
267 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000268 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000269 if (II.Operands.size() == 0)
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000270 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000271
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000272 // For now, ignore multi-instruction patterns.
273 bool MultiInsts = false;
274 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
275 TreePatternNode *ChildOp = Dst->getChild(i);
276 if (ChildOp->isLeaf())
277 continue;
278 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
279 MultiInsts = true;
280 break;
281 }
282 }
283 if (MultiInsts)
284 continue;
285
Dan Gohman379cad42008-08-19 20:36:33 +0000286 // For now, ignore instructions where the first operand is not an
287 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000288 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000289 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000290 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000291 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000292 if (!Op0Rec->isSubClassOf("RegisterClass"))
293 continue;
294 DstRC = &Target.getRegisterClass(Op0Rec);
295 if (!DstRC)
296 continue;
297 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000298 // If this isn't a leaf, then continue since the register classes are
299 // a bit too complicated for now.
300 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000301
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000302 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
303 if (SR)
304 SubRegNo = getQualifiedName(SR->getDef());
305 else
306 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000307 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000308
309 // Inspect the pattern.
310 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
311 if (!InstPatNode) continue;
312 if (InstPatNode->isLeaf()) continue;
313
Chris Lattner084df622010-03-24 00:41:19 +0000314 // Ignore multiple result nodes for now.
315 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000316
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000317 Record *InstPatOp = InstPatNode->getOperator();
318 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000319 MVT::SimpleValueType RetVT = MVT::isVoid;
320 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000322 if (InstPatNode->getNumChildren()) {
323 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
324 VT = InstPatNode->getChild(0)->getType(0);
325 }
Chris Lattner602fc062011-04-17 20:23:29 +0000326
327 if (InstPatOp->getName() =="shl") {
328 InstPatNode->dump();
329 }
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;
Jim Grosbach45258f52010-12-07 19:36:07 +0000345
Owen Anderson667d8f72008-08-29 17:45:56 +0000346 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 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000358
Owen Anderson667d8f72008-08-29 17:45:56 +0000359 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 += "::";
Jim Grosbach45258f52010-12-07 19:36:07 +0000366
Owen Anderson667d8f72008-08-29 17:45:56 +0000367 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 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000375
Owen Anderson667d8f72008-08-29 17:45:56 +0000376 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 };
Jim Grosbach76612b52010-12-07 19:35:36 +0000391 if (SimplePatterns[Operands][OpcodeName][VT][RetVT]
Jim Grosbach997759a2010-12-07 23:05:49 +0000392 .count(PredicateCheck))
393 throw TGError(Pattern.getSrcRecord()->getLoc(), "Duplicate record!");
394
Owen Andersonabb1f162008-08-26 01:22:59 +0000395 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000396 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000397}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000398
Daniel Dunbar1a551802009-07-03 00:10:29 +0000399void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000400 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000401 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000402 OE = SimplePatterns.end(); OI != OE; ++OI) {
403 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000404 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000405
Owen Anderson7b2e5792008-08-25 23:43:09 +0000406 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000407 I != E; ++I) {
408 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000409 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000410
411 OS << "// FastEmit functions for " << Opcode << ".\n";
412 OS << "\n";
413
414 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000415 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000416 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000418 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000419 if (RM.size() != 1) {
420 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
421 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000423 const PredMap &PM = RI->second;
424 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000425
Evan Chengc3f44b02008-09-03 00:03:49 +0000426 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000427 << getLegalCName(Opcode)
428 << "_" << getLegalCName(getName(VT))
429 << "_" << getLegalCName(getName(RetVT)) << "_";
430 Operands.PrintManglingSuffix(OS);
431 OS << "(";
432 Operands.PrintParameters(OS);
433 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000434
Owen Anderson71669e52008-08-26 00:42:26 +0000435 // Emit code for each possible instruction. There may be
436 // multiple if there are subtarget concerns.
437 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
438 PI != PE; ++PI) {
439 std::string PredicateCheck = PI->first;
440 const InstructionMemo &Memo = PI->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000441
Owen Anderson71669e52008-08-26 00:42:26 +0000442 if (PredicateCheck.empty()) {
443 assert(!HasPred &&
444 "Multiple instructions match, at least one has "
445 "a predicate and at least one doesn't!");
446 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000447 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000448 OS << " ";
449 HasPred = true;
450 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000451
Owen Anderson667d8f72008-08-29 17:45:56 +0000452 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
453 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000454 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
455 << "TII.get(TargetOpcode::COPY), "
456 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000457 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000458
Owen Anderson71669e52008-08-26 00:42:26 +0000459 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000460 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000461 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000462 OS << "(" << InstNS << Memo.Name << ", ";
463 OS << InstNS << Memo.RC->getName() << "RegisterClass";
464 if (!Operands.empty())
465 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000466 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000467 OS << ");\n";
468 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000469 OS << "extractsubreg(" << getName(RetVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000470 OS << ", Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000471 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000472 OS << ");\n";
473 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000474
Owen Anderson667d8f72008-08-29 17:45:56 +0000475 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000476 OS << " }\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000477
Owen Anderson71669e52008-08-26 00:42:26 +0000478 }
479 // Return 0 if none of the predicates were satisfied.
480 if (HasPred)
481 OS << " return 0;\n";
482 OS << "}\n";
483 OS << "\n";
484 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000485
Owen Anderson71669e52008-08-26 00:42:26 +0000486 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000487 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000488 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000489 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000490 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000492 if (!Operands.empty())
493 OS << ", ";
494 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000496 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
497 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000498 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000499 OS << " case " << getName(RetVT) << ": return FastEmit_"
500 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
501 << "_" << getLegalCName(getName(RetVT)) << "_";
502 Operands.PrintManglingSuffix(OS);
503 OS << "(";
504 Operands.PrintArguments(OS);
505 OS << ");\n";
506 }
507 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000508
Owen Anderson71669e52008-08-26 00:42:26 +0000509 } else {
510 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000511 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000512 << getLegalCName(Opcode) << "_"
513 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000514 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000516 if (!Operands.empty())
517 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000518 Operands.PrintParameters(OS);
519 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000522 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000523
Owen Anderson71669e52008-08-26 00:42:26 +0000524 const PredMap &PM = RM.begin()->second;
525 bool HasPred = false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000526
Owen Anderson7b2e5792008-08-25 23:43:09 +0000527 // Emit code for each possible instruction. There may be
528 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000529 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
530 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000531 std::string PredicateCheck = PI->first;
532 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000533
Owen Anderson7b2e5792008-08-25 23:43:09 +0000534 if (PredicateCheck.empty()) {
535 assert(!HasPred &&
536 "Multiple instructions match, at least one has "
537 "a predicate and at least one doesn't!");
538 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000539 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000540 OS << " ";
541 HasPred = true;
542 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000543
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000544 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
545 if ((*Memo.PhysRegs)[i] != "")
546 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
547 << "TII.get(TargetOpcode::COPY), "
548 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
549 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000550
Owen Anderson7b2e5792008-08-25 23:43:09 +0000551 OS << " return FastEmitInst_";
Jim Grosbach45258f52010-12-07 19:36:07 +0000552
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000553 if (Memo.SubRegNo.empty()) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000554 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000555 OS << "(" << InstNS << Memo.Name << ", ";
556 OS << InstNS << Memo.RC->getName() << "RegisterClass";
557 if (!Operands.empty())
558 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000559 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000560 OS << ");\n";
561 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000562 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000563 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000564 OS << ");\n";
565 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000566
Owen Anderson667d8f72008-08-29 17:45:56 +0000567 if (HasPred)
568 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000569 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000570
Owen Anderson7b2e5792008-08-25 23:43:09 +0000571 // Return 0 if none of the predicates were satisfied.
572 if (HasPred)
573 OS << " return 0;\n";
574 OS << "}\n";
575 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000576 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000577 }
578
579 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000580 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000581 << getLegalCName(Opcode) << "_";
582 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000584 if (!Operands.empty())
585 OS << ", ";
586 Operands.PrintParameters(OS);
587 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000589 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000590 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000592 std::string TypeName = getName(VT);
593 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000594 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
595 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000596 OS << "(RetVT";
597 if (!Operands.empty())
598 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000599 Operands.PrintArguments(OS);
600 OS << ");\n";
601 }
602 OS << " default: return 0;\n";
603 OS << " }\n";
604 OS << "}\n";
605 OS << "\n";
606 }
607
Dan Gohman0bfb7522008-08-22 00:28:15 +0000608 OS << "// Top-level FastEmit function.\n";
609 OS << "\n";
610
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000611 // Emit one function for the operand signature that demultiplexes based
612 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000613 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000614 Operands.PrintManglingSuffix(OS);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000615 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000616 if (!Operands.empty())
617 OS << ", ";
618 Operands.PrintParameters(OS);
619 OS << ") {\n";
620 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000621 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000622 I != E; ++I) {
623 const std::string &Opcode = I->first;
624
625 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000626 << getLegalCName(Opcode) << "_";
627 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000628 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000629 if (!Operands.empty())
630 OS << ", ";
631 Operands.PrintArguments(OS);
632 OS << ");\n";
633 }
634 OS << " default: return 0;\n";
635 OS << " }\n";
636 OS << "}\n";
637 OS << "\n";
638 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000639}
640
Daniel Dunbar1a551802009-07-03 00:10:29 +0000641void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000642 const CodeGenTarget &Target = CGP.getTargetInfo();
643
644 // Determine the target's namespace name.
645 std::string InstNS = Target.getInstNamespace() + "::";
646 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
647
648 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
649 Target.getName() + " target", OS);
650
Dan Gohman72d63af2008-08-26 21:21:20 +0000651 FastISelMap F(InstNS);
652 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000653 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000654}
655
656FastISelEmitter::FastISelEmitter(RecordKeeper &R)
657 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000658 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000659}
Dan Gohman72d63af2008-08-26 21:21:20 +0000660