blob: 62ac64d8c12e8c701ec9af35bf0e3180c11cbb17 [file] [log] [blame]
Dan Gohmanb2226e22008-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 Gohmanb4863502008-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 Gohmanb2226e22008-08-13 20:19:35 +000013//
Dan Gohmanb4863502008-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 Gohmanb2226e22008-08-13 20:19:35 +000017//
Dan Gohmanb2226e22008-08-13 20:19:35 +000018//===----------------------------------------------------------------------===//
19
20#include "FastISelEmitter.h"
21#include "Record.h"
22#include "llvm/Support/Debug.h"
Jim Grosbache99956e2010-12-07 19:35:36 +000023#include "llvm/ADT/SmallString.h"
Dan Gohmanb2226e22008-08-13 20:19:35 +000024#include "llvm/ADT/VectorExtras.h"
25using namespace llvm;
26
27namespace {
28
Owen Anderson0673a8a2008-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 Olesen1c696462010-05-24 14:48:12 +000035 std::string SubRegNo;
Owen Anderson0673a8a2008-08-29 17:45:56 +000036 std::vector<std::string>* PhysRegs;
37};
38
Dan Gohmandbd53282008-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 Gohmanb2226e22008-08-13 20:19:35 +000042struct OperandsSignature {
Chris Lattner90803912011-04-17 22:24:13 +000043 SmallVector<char, 3> Operands;
Dan Gohmanb2226e22008-08-13 20:19:35 +000044
45 bool operator<(const OperandsSignature &O) const {
46 return Operands < O.Operands;
47 }
48
49 bool empty() const { return Operands.empty(); }
50
Dan Gohman98e6f1c2008-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 Lattnerb53ccb82011-04-17 20:23:29 +000055 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
Owen Anderson9f944592009-08-11 20:47:22 +000056 MVT::SimpleValueType VT) {
Eric Christophere1270c62010-07-28 01:52:23 +000057
Dan Gohman388fa732010-05-27 16:25:05 +000058 if (!InstPatNode->isLeaf()) {
59 if (InstPatNode->getOperator()->getName() == "imm") {
Chris Lattner90803912011-04-17 22:24:13 +000060 Operands.push_back('i');
Dan Gohman388fa732010-05-27 16:25:05 +000061 return true;
62 }
63 if (InstPatNode->getOperator()->getName() == "fpimm") {
Chris Lattner90803912011-04-17 22:24:13 +000064 Operands.push_back('f');
Dan Gohman388fa732010-05-27 16:25:05 +000065 return true;
66 }
Dan Gohman5ca269e2008-08-27 01:09:54 +000067 }
Jim Grosbach8656d822010-12-07 19:36:07 +000068
Owen Anderson6f2db722008-08-26 01:22:59 +000069 const CodeGenRegisterClass *DstRC = 0;
Jim Grosbach8656d822010-12-07 19:36:07 +000070
Dan Gohman98e6f1c2008-08-19 20:56:30 +000071 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
72 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach8656d822010-12-07 19:36:07 +000073
Dan Gohman98e6f1c2008-08-19 20:56:30 +000074 // For now, filter out any operand with a predicate.
Dan Gohmanfe905652008-08-21 01:41:07 +000075 // For now, filter out any operand with multiple values.
Chris Lattnerb53ccb82011-04-17 20:23:29 +000076 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerf1447252010-03-19 21:37:09 +000077 return false;
Jim Grosbach8656d822010-12-07 19:36:07 +000078
Dan Gohmanfe905652008-08-21 01:41:07 +000079 if (!Op->isLeaf()) {
80 if (Op->getOperator()->getName() == "imm") {
Chris Lattner90803912011-04-17 22:24:13 +000081 Operands.push_back('i');
Dale Johannesen4ff70e382009-05-21 22:25:49 +000082 continue;
Dan Gohmanfe905652008-08-21 01:41:07 +000083 }
Dan Gohman5ca269e2008-08-27 01:09:54 +000084 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner90803912011-04-17 22:24:13 +000085 Operands.push_back('f');
Dale Johannesen4ff70e382009-05-21 22:25:49 +000086 continue;
Dan Gohman5ca269e2008-08-27 01:09:54 +000087 }
Dan Gohman6d153b02008-08-27 16:18:22 +000088 // For now, ignore other non-leaf nodes.
Dan Gohmanfe905652008-08-21 01:41:07 +000089 return false;
90 }
Chris Lattnerb53ccb82011-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 Gohman98e6f1c2008-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 Gohmanfe905652008-08-21 01:41:07 +0000104 // For now, the only other thing we accept is register operands.
Evan Chengca14c072008-09-08 08:39:33 +0000105
Owen Anderson0673a8a2008-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 Gohman98e6f1c2008-08-19 20:56:30 +0000112 return false;
Jim Grosbach8656d822010-12-07 19:36:07 +0000113
Eric Christopher98f0ea62010-08-24 23:21:59 +0000114 // For now, this needs to be a register class of some sort.
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000115 if (!RC)
116 return false;
Eric Christopher98f0ea62010-08-24 23:21:59 +0000117
Eric Christopher6490bf62010-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 Anderson6f2db722008-08-26 01:22:59 +0000120 if (DstRC) {
Eric Christopher6490bf62010-08-25 04:58:56 +0000121 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Anderson6f2db722008-08-26 01:22:59 +0000122 return false;
123 } else
124 DstRC = RC;
Chris Lattner90803912011-04-17 22:24:13 +0000125 Operands.push_back('r');
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000126 }
127 return true;
128 }
129
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000130 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000131 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner90803912011-04-17 22:24:13 +0000132 if (Operands[i] == 'r') {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000133 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner90803912011-04-17 22:24:13 +0000134 } else if (Operands[i] == 'i') {
Dan Gohmanfe905652008-08-21 01:41:07 +0000135 OS << "uint64_t imm" << i;
Chris Lattner90803912011-04-17 22:24:13 +0000136 } else if (Operands[i] == 'f') {
Dan Gohman5ca269e2008-08-27 01:09:54 +0000137 OS << "ConstantFP *f" << i;
Dan Gohmanb2226e22008-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 Dunbar38a22bf2009-07-03 00:10:29 +0000147 void PrintArguments(raw_ostream &OS,
Owen Anderson0673a8a2008-08-29 17:45:56 +0000148 const std::vector<std::string>& PR) const {
149 assert(PR.size() == Operands.size());
Evan Chengca14c072008-09-08 08:39:33 +0000150 bool PrintedArg = false;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000151 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Chengca14c072008-09-08 08:39:33 +0000152 if (PR[i] != "")
153 // Implicit physical register operand.
154 continue;
155
156 if (PrintedArg)
157 OS << ", ";
Chris Lattner90803912011-04-17 22:24:13 +0000158 if (Operands[i] == 'r') {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000159 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Chengca14c072008-09-08 08:39:33 +0000160 PrintedArg = true;
Chris Lattner90803912011-04-17 22:24:13 +0000161 } else if (Operands[i] == 'i') {
Owen Anderson0673a8a2008-08-29 17:45:56 +0000162 OS << "imm" << i;
Evan Chengca14c072008-09-08 08:39:33 +0000163 PrintedArg = true;
Chris Lattner90803912011-04-17 22:24:13 +0000164 } else if (Operands[i] == 'f') {
Owen Anderson0673a8a2008-08-29 17:45:56 +0000165 OS << "f" << i;
Evan Chengca14c072008-09-08 08:39:33 +0000166 PrintedArg = true;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000167 } else {
168 assert("Unknown operand kind!");
169 abort();
170 }
Owen Anderson0673a8a2008-08-29 17:45:56 +0000171 }
172 }
173
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000174 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000175 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner90803912011-04-17 22:24:13 +0000176 if (Operands[i] == 'r') {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000177 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner90803912011-04-17 22:24:13 +0000178 } else if (Operands[i] == 'i') {
Dan Gohmanfe905652008-08-21 01:41:07 +0000179 OS << "imm" << i;
Chris Lattner90803912011-04-17 22:24:13 +0000180 } else if (Operands[i] == 'f') {
Dan Gohman5ca269e2008-08-27 01:09:54 +0000181 OS << "f" << i;
Dan Gohmanb2226e22008-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 Anderson0673a8a2008-08-29 17:45:56 +0000191
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000192 void PrintManglingSuffix(raw_ostream &OS,
Evan Chengca14c072008-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 Dunbar38a22bf2009-07-03 00:10:29 +0000206 void PrintManglingSuffix(raw_ostream &OS) const {
Dan Gohmanb2226e22008-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 Gohman44003cc2008-08-26 21:21:20 +0000213class FastISelMap {
214 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson9f944592009-08-11 20:47:22 +0000215 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
216 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman44003cc2008-08-26 21:21:20 +0000217 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach8656d822010-12-07 19:36:07 +0000218 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopher62ac5ca2010-07-26 17:53:07 +0000219 OperandsOpcodeTypeRetPredMap;
Dan Gohman44003cc2008-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 Dunbar38a22bf2009-07-03 00:10:29 +0000229 void PrintFunctionDefinitions(raw_ostream &OS);
Dan Gohman44003cc2008-08-26 21:21:20 +0000230};
231
Dan Gohmanb2226e22008-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 Gohman44003cc2008-08-26 21:21:20 +0000245FastISelMap::FastISelMap(std::string instns)
246 : InstNS(instns) {
247}
Dan Gohmanb2226e22008-08-13 20:19:35 +0000248
Dan Gohman44003cc2008-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 Gohmanb2226e22008-08-13 20:19:35 +0000255
Dan Gohman9b29ec72008-08-22 00:28:15 +0000256 // Scan through all the patterns and record the simple ones.
Dan Gohmanb2226e22008-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 Lattner9aec14b2010-03-19 00:07:20 +0000268 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattner90803912011-04-17 22:24:13 +0000269 if (II.Operands.empty())
Dan Gohmanb2226e22008-08-13 20:19:35 +0000270 continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000271
Evan Cheng7cab17a2008-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 Gohmanaa13b6f2008-08-19 20:36:33 +0000286 // For now, ignore instructions where the first operand is not an
287 // output register.
Owen Anderson787f1002008-08-28 18:06:12 +0000288 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000289 std::string SubRegNo;
Owen Anderson787f1002008-08-28 18:06:12 +0000290 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000291 Record *Op0Rec = II.Operands[0].Rec;
Owen Anderson787f1002008-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 Christopherbebb8c52010-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 Grosbach8656d822010-12-07 19:36:07 +0000301
Jakob Stoklund Olesen1c696462010-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 Anderson787f1002008-08-28 18:06:12 +0000307 }
Dan Gohmanb2226e22008-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 Lattner6c2d1782010-03-24 00:41:19 +0000314 // Ignore multiple result nodes for now.
315 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000316
Dan Gohmanb2226e22008-08-13 20:19:35 +0000317 Record *InstPatOp = InstPatNode->getOperator();
318 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000319 MVT::SimpleValueType RetVT = MVT::isVoid;
320 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson9f944592009-08-11 20:47:22 +0000321 MVT::SimpleValueType VT = RetVT;
Chris Lattnerf1447252010-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 Lattnerb53ccb82011-04-17 20:23:29 +0000326
Dan Gohmanb2226e22008-08-13 20:19:35 +0000327 // For now, filter out instructions which just set a register to
Dan Gohmana6c14d02008-08-19 20:30:54 +0000328 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb2226e22008-08-13 20:19:35 +0000329 if (InstPatOp->isSubClassOf("Operand"))
330 continue;
Dan Gohmana6c14d02008-08-19 20:30:54 +0000331
332 // For now, filter out any instructions with predicates.
Dan Gohman6e979022008-10-15 06:17:21 +0000333 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmana6c14d02008-08-19 20:30:54 +0000334 continue;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000335
Dan Gohmanaa13b6f2008-08-19 20:36:33 +0000336 // Check all the operands.
Dan Gohmanb2226e22008-08-13 20:19:35 +0000337 OperandsSignature Operands;
Owen Anderson6f2db722008-08-26 01:22:59 +0000338 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000339 continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000340
Owen Anderson0673a8a2008-08-29 17:45:56 +0000341 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
342 if (!InstPatNode->isLeaf() &&
343 (InstPatNode->getOperator()->getName() == "imm" ||
344 InstPatNode->getOperator()->getName() == "fpimmm"))
345 PhysRegInputs->push_back("");
346 else if (!InstPatNode->isLeaf()) {
347 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
348 TreePatternNode *Op = InstPatNode->getChild(i);
349 if (!Op->isLeaf()) {
350 PhysRegInputs->push_back("");
351 continue;
352 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000353
Owen Anderson0673a8a2008-08-29 17:45:56 +0000354 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
355 Record *OpLeafRec = OpDI->getDef();
356 std::string PhysReg;
357 if (OpLeafRec->isSubClassOf("Register")) {
358 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
359 "Namespace")->getValue())->getValue();
360 PhysReg += "::";
Jim Grosbach8656d822010-12-07 19:36:07 +0000361
Owen Anderson0673a8a2008-08-29 17:45:56 +0000362 std::vector<CodeGenRegister> Regs = Target.getRegisters();
363 for (unsigned i = 0; i < Regs.size(); ++i) {
364 if (Regs[i].TheDef == OpLeafRec) {
365 PhysReg += Regs[i].getName();
366 break;
367 }
368 }
369 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000370
Owen Anderson0673a8a2008-08-29 17:45:56 +0000371 PhysRegInputs->push_back(PhysReg);
372 }
373 } else
374 PhysRegInputs->push_back("");
Dan Gohmanb2226e22008-08-13 20:19:35 +0000375
Dan Gohman49e19e92008-08-22 00:20:26 +0000376 // Get the predicate that guards this pattern.
377 std::string PredicateCheck = Pattern.getPredicateCheck();
378
Dan Gohmanb2226e22008-08-13 20:19:35 +0000379 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman7b3932e2008-08-21 00:35:26 +0000380 InstructionMemo Memo = {
381 Pattern.getDstPattern()->getOperator()->getName(),
Owen Anderson787f1002008-08-28 18:06:12 +0000382 DstRC,
Owen Anderson0673a8a2008-08-29 17:45:56 +0000383 SubRegNo,
384 PhysRegInputs
Dan Gohman7b3932e2008-08-21 00:35:26 +0000385 };
Jim Grosbache99956e2010-12-07 19:35:36 +0000386 if (SimplePatterns[Operands][OpcodeName][VT][RetVT]
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000387 .count(PredicateCheck))
388 throw TGError(Pattern.getSrcRecord()->getLoc(), "Duplicate record!");
389
Owen Anderson6f2db722008-08-26 01:22:59 +0000390 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000391 }
Dan Gohman44003cc2008-08-26 21:21:20 +0000392}
Dan Gohmanb2226e22008-08-13 20:19:35 +0000393
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000394void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000395 // Now emit code for all the patterns that we collected.
Owen Anderson5952cca2008-08-25 23:43:09 +0000396 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb2226e22008-08-13 20:19:35 +0000397 OE = SimplePatterns.end(); OI != OE; ++OI) {
398 const OperandsSignature &Operands = OI->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000399 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000400
Owen Anderson5952cca2008-08-25 23:43:09 +0000401 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000402 I != E; ++I) {
403 const std::string &Opcode = I->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000404 const TypeRetPredMap &TM = I->second;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000405
406 OS << "// FastEmit functions for " << Opcode << ".\n";
407 OS << "\n";
408
409 // Emit one function for each opcode,type pair.
Owen Anderson5952cca2008-08-25 23:43:09 +0000410 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000411 TI != TE; ++TI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000412 MVT::SimpleValueType VT = TI->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000413 const RetPredMap &RM = TI->second;
Owen Anderson5f334d82008-08-26 00:42:26 +0000414 if (RM.size() != 1) {
415 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
416 RI != RE; ++RI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000417 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson5f334d82008-08-26 00:42:26 +0000418 const PredMap &PM = RI->second;
419 bool HasPred = false;
Dan Gohman49e19e92008-08-22 00:20:26 +0000420
Evan Cheng24422d42008-09-03 00:03:49 +0000421 OS << "unsigned FastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000422 << getLegalCName(Opcode)
423 << "_" << getLegalCName(getName(VT))
424 << "_" << getLegalCName(getName(RetVT)) << "_";
425 Operands.PrintManglingSuffix(OS);
426 OS << "(";
427 Operands.PrintParameters(OS);
428 OS << ") {\n";
Dan Gohman49e19e92008-08-22 00:20:26 +0000429
Owen Anderson5f334d82008-08-26 00:42:26 +0000430 // Emit code for each possible instruction. There may be
431 // multiple if there are subtarget concerns.
432 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
433 PI != PE; ++PI) {
434 std::string PredicateCheck = PI->first;
435 const InstructionMemo &Memo = PI->second;
Jim Grosbach8656d822010-12-07 19:36:07 +0000436
Owen Anderson5f334d82008-08-26 00:42:26 +0000437 if (PredicateCheck.empty()) {
438 assert(!HasPred &&
439 "Multiple instructions match, at least one has "
440 "a predicate and at least one doesn't!");
441 } else {
Owen Anderson0673a8a2008-08-29 17:45:56 +0000442 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson5f334d82008-08-26 00:42:26 +0000443 OS << " ";
444 HasPred = true;
445 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000446
Owen Anderson0673a8a2008-08-29 17:45:56 +0000447 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
448 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4a52e2f2010-07-11 03:53:50 +0000449 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
450 << "TII.get(TargetOpcode::COPY), "
451 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson0673a8a2008-08-29 17:45:56 +0000452 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000453
Owen Anderson5f334d82008-08-26 00:42:26 +0000454 OS << " return FastEmitInst_";
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000455 if (Memo.SubRegNo.empty()) {
Evan Chengca14c072008-09-08 08:39:33 +0000456 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Anderson787f1002008-08-28 18:06:12 +0000457 OS << "(" << InstNS << Memo.Name << ", ";
458 OS << InstNS << Memo.RC->getName() << "RegisterClass";
459 if (!Operands.empty())
460 OS << ", ";
Owen Anderson0673a8a2008-08-29 17:45:56 +0000461 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Anderson787f1002008-08-28 18:06:12 +0000462 OS << ");\n";
463 } else {
Evan Cheng4a0bf662009-01-22 09:10:11 +0000464 OS << "extractsubreg(" << getName(RetVT);
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000465 OS << ", Op0, Op0IsKill, ";
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000466 OS << Memo.SubRegNo;
Owen Anderson787f1002008-08-28 18:06:12 +0000467 OS << ");\n";
468 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000469
Owen Anderson0673a8a2008-08-29 17:45:56 +0000470 if (HasPred)
Evan Cheng09638d32008-09-07 08:23:06 +0000471 OS << " }\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000472
Owen Anderson5f334d82008-08-26 00:42:26 +0000473 }
474 // Return 0 if none of the predicates were satisfied.
475 if (HasPred)
476 OS << " return 0;\n";
477 OS << "}\n";
478 OS << "\n";
479 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000480
Owen Anderson5f334d82008-08-26 00:42:26 +0000481 // Emit one function for the type that demultiplexes on return type.
Evan Cheng24422d42008-09-03 00:03:49 +0000482 OS << "unsigned FastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000483 << getLegalCName(Opcode) << "_"
Owen Anderson6f2db722008-08-26 01:22:59 +0000484 << getLegalCName(getName(VT)) << "_";
Owen Anderson5f334d82008-08-26 00:42:26 +0000485 Operands.PrintManglingSuffix(OS);
Owen Anderson9f944592009-08-11 20:47:22 +0000486 OS << "(MVT RetVT";
Owen Anderson5f334d82008-08-26 00:42:26 +0000487 if (!Operands.empty())
488 OS << ", ";
489 Operands.PrintParameters(OS);
Owen Anderson9f944592009-08-11 20:47:22 +0000490 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson5f334d82008-08-26 00:42:26 +0000491 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
492 RI != RE; ++RI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000493 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson5f334d82008-08-26 00:42:26 +0000494 OS << " case " << getName(RetVT) << ": return FastEmit_"
495 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
496 << "_" << getLegalCName(getName(RetVT)) << "_";
497 Operands.PrintManglingSuffix(OS);
498 OS << "(";
499 Operands.PrintArguments(OS);
500 OS << ");\n";
501 }
502 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000503
Owen Anderson5f334d82008-08-26 00:42:26 +0000504 } else {
505 // Non-variadic return type.
Evan Cheng24422d42008-09-03 00:03:49 +0000506 OS << "unsigned FastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000507 << getLegalCName(Opcode) << "_"
508 << getLegalCName(getName(VT)) << "_";
Dan Gohman49e19e92008-08-22 00:20:26 +0000509 Operands.PrintManglingSuffix(OS);
Owen Anderson9f944592009-08-11 20:47:22 +0000510 OS << "(MVT RetVT";
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000511 if (!Operands.empty())
512 OS << ", ";
Owen Anderson5952cca2008-08-25 23:43:09 +0000513 Operands.PrintParameters(OS);
514 OS << ") {\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000515
Owen Anderson9f944592009-08-11 20:47:22 +0000516 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson3ea3efe2008-08-26 18:50:00 +0000517 << ")\n return 0;\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000518
Owen Anderson5f334d82008-08-26 00:42:26 +0000519 const PredMap &PM = RM.begin()->second;
520 bool HasPred = false;
Jim Grosbach8656d822010-12-07 19:36:07 +0000521
Owen Anderson5952cca2008-08-25 23:43:09 +0000522 // Emit code for each possible instruction. There may be
523 // multiple if there are subtarget concerns.
Evan Chengca14c072008-09-08 08:39:33 +0000524 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
525 ++PI) {
Owen Anderson5952cca2008-08-25 23:43:09 +0000526 std::string PredicateCheck = PI->first;
527 const InstructionMemo &Memo = PI->second;
Owen Anderson5f334d82008-08-26 00:42:26 +0000528
Owen Anderson5952cca2008-08-25 23:43:09 +0000529 if (PredicateCheck.empty()) {
530 assert(!HasPred &&
531 "Multiple instructions match, at least one has "
532 "a predicate and at least one doesn't!");
533 } else {
Owen Anderson0673a8a2008-08-29 17:45:56 +0000534 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson5952cca2008-08-25 23:43:09 +0000535 OS << " ";
536 HasPred = true;
537 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000538
Jakob Stoklund Olesen4a52e2f2010-07-11 03:53:50 +0000539 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
540 if ((*Memo.PhysRegs)[i] != "")
541 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
542 << "TII.get(TargetOpcode::COPY), "
543 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
544 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000545
Owen Anderson5952cca2008-08-25 23:43:09 +0000546 OS << " return FastEmitInst_";
Jim Grosbach8656d822010-12-07 19:36:07 +0000547
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000548 if (Memo.SubRegNo.empty()) {
Evan Chengca14c072008-09-08 08:39:33 +0000549 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Anderson787f1002008-08-28 18:06:12 +0000550 OS << "(" << InstNS << Memo.Name << ", ";
551 OS << InstNS << Memo.RC->getName() << "RegisterClass";
552 if (!Operands.empty())
553 OS << ", ";
Owen Anderson0673a8a2008-08-29 17:45:56 +0000554 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Anderson787f1002008-08-28 18:06:12 +0000555 OS << ");\n";
556 } else {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000557 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000558 OS << Memo.SubRegNo;
Owen Anderson787f1002008-08-28 18:06:12 +0000559 OS << ");\n";
560 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000561
Owen Anderson0673a8a2008-08-29 17:45:56 +0000562 if (HasPred)
563 OS << " }\n";
Owen Anderson5952cca2008-08-25 23:43:09 +0000564 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000565
Owen Anderson5952cca2008-08-25 23:43:09 +0000566 // Return 0 if none of the predicates were satisfied.
567 if (HasPred)
568 OS << " return 0;\n";
569 OS << "}\n";
570 OS << "\n";
Dan Gohman49e19e92008-08-22 00:20:26 +0000571 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000572 }
573
574 // Emit one function for the opcode that demultiplexes based on the type.
Evan Cheng24422d42008-09-03 00:03:49 +0000575 OS << "unsigned FastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000576 << getLegalCName(Opcode) << "_";
577 Operands.PrintManglingSuffix(OS);
Owen Anderson9f944592009-08-11 20:47:22 +0000578 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000579 if (!Operands.empty())
580 OS << ", ";
581 Operands.PrintParameters(OS);
582 OS << ") {\n";
Owen Anderson9f944592009-08-11 20:47:22 +0000583 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson5952cca2008-08-25 23:43:09 +0000584 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000585 TI != TE; ++TI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000586 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000587 std::string TypeName = getName(VT);
588 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000589 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
590 Operands.PrintManglingSuffix(OS);
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000591 OS << "(RetVT";
592 if (!Operands.empty())
593 OS << ", ";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000594 Operands.PrintArguments(OS);
595 OS << ");\n";
596 }
597 OS << " default: return 0;\n";
598 OS << " }\n";
599 OS << "}\n";
600 OS << "\n";
601 }
602
Dan Gohman9b29ec72008-08-22 00:28:15 +0000603 OS << "// Top-level FastEmit function.\n";
604 OS << "\n";
605
Dan Gohmanb2226e22008-08-13 20:19:35 +0000606 // Emit one function for the operand signature that demultiplexes based
607 // on opcode and type.
Evan Cheng24422d42008-09-03 00:03:49 +0000608 OS << "unsigned FastEmit_";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000609 Operands.PrintManglingSuffix(OS);
Dan Gohman404a9842010-01-05 22:26:32 +0000610 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000611 if (!Operands.empty())
612 OS << ", ";
613 Operands.PrintParameters(OS);
614 OS << ") {\n";
615 OS << " switch (Opcode) {\n";
Owen Anderson5952cca2008-08-25 23:43:09 +0000616 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000617 I != E; ++I) {
618 const std::string &Opcode = I->first;
619
620 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000621 << getLegalCName(Opcode) << "_";
622 Operands.PrintManglingSuffix(OS);
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000623 OS << "(VT, RetVT";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000624 if (!Operands.empty())
625 OS << ", ";
626 Operands.PrintArguments(OS);
627 OS << ");\n";
628 }
629 OS << " default: return 0;\n";
630 OS << " }\n";
631 OS << "}\n";
632 OS << "\n";
633 }
Dan Gohman44003cc2008-08-26 21:21:20 +0000634}
635
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000636void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman44003cc2008-08-26 21:21:20 +0000637 const CodeGenTarget &Target = CGP.getTargetInfo();
638
639 // Determine the target's namespace name.
640 std::string InstNS = Target.getInstNamespace() + "::";
641 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
642
643 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
644 Target.getName() + " target", OS);
645
Dan Gohman44003cc2008-08-26 21:21:20 +0000646 FastISelMap F(InstNS);
647 F.CollectPatterns(CGP);
Dan Gohman44003cc2008-08-26 21:21:20 +0000648 F.PrintFunctionDefinitions(OS);
Dan Gohman71706232008-08-21 00:19:05 +0000649}
650
651FastISelEmitter::FastISelEmitter(RecordKeeper &R)
652 : Records(R),
Dan Gohman44003cc2008-08-26 21:21:20 +0000653 CGP(R) {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000654}
Dan Gohman44003cc2008-08-26 21:21:20 +0000655