blob: 2bb351b7085d7f24ba1e636eadb92b0aef9198f0 [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"
Peter Collingbourne7c788882011-10-01 16:41:13 +000021#include "llvm/TableGen/Error.h"
22#include "llvm/TableGen/Record.h"
Jim Grosbach76612b52010-12-07 19:35:36 +000023#include "llvm/ADT/SmallString.h"
Chad Rosier36a300a2011-06-07 20:41:31 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000026using namespace llvm;
27
28namespace {
29
Owen Anderson667d8f72008-08-29 17:45:56 +000030/// InstructionMemo - This class holds additional information about an
31/// instruction needed to emit code for it.
32///
33struct InstructionMemo {
34 std::string Name;
35 const CodeGenRegisterClass *RC;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +000036 std::string SubRegNo;
Owen Anderson667d8f72008-08-29 17:45:56 +000037 std::vector<std::string>* PhysRegs;
38};
Chris Lattner1518afd2011-04-18 06:22:33 +000039
40/// ImmPredicateSet - This uniques predicates (represented as a string) and
41/// gives them unique (small) integer ID's that start at 0.
42class ImmPredicateSet {
43 DenseMap<TreePattern *, unsigned> ImmIDs;
44 std::vector<TreePredicateFn> PredsByName;
45public:
46
47 unsigned getIDFor(TreePredicateFn Pred) {
48 unsigned &Entry = ImmIDs[Pred.getOrigPatFragRecord()];
49 if (Entry == 0) {
50 PredsByName.push_back(Pred);
51 Entry = PredsByName.size();
52 }
53 return Entry-1;
54 }
55
56 const TreePredicateFn &getPredicate(unsigned i) {
57 assert(i < PredsByName.size());
58 return PredsByName[i];
59 }
60
61 typedef std::vector<TreePredicateFn>::const_iterator iterator;
62 iterator begin() const { return PredsByName.begin(); }
63 iterator end() const { return PredsByName.end(); }
64
65};
Owen Anderson667d8f72008-08-29 17:45:56 +000066
Dan Gohman04b7dfb2008-08-19 18:06:12 +000067/// OperandsSignature - This class holds a description of a list of operand
68/// types. It has utility methods for emitting text based on the operands.
69///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000070struct OperandsSignature {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000071 class OpKind {
72 enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 };
73 char Repr;
74 public:
75
76 OpKind() : Repr(OK_Invalid) {}
77
78 bool operator<(OpKind RHS) const { return Repr < RHS.Repr; }
Chris Lattner1518afd2011-04-18 06:22:33 +000079 bool operator==(OpKind RHS) const { return Repr == RHS.Repr; }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000080
81 static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; }
82 static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; }
Chris Lattner1518afd2011-04-18 06:22:33 +000083 static OpKind getImm(unsigned V) {
84 assert((unsigned)OK_Imm+V < 128 &&
85 "Too many integer predicates for the 'Repr' char");
86 OpKind K; K.Repr = OK_Imm+V; return K;
87 }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000088
89 bool isReg() const { return Repr == OK_Reg; }
90 bool isFP() const { return Repr == OK_FP; }
Chris Lattner1518afd2011-04-18 06:22:33 +000091 bool isImm() const { return Repr >= OK_Imm; }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000092
Chris Lattner1518afd2011-04-18 06:22:33 +000093 unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; }
94
95 void printManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
96 bool StripImmCodes) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000097 if (isReg())
98 OS << 'r';
99 else if (isFP())
100 OS << 'f';
Chris Lattner1518afd2011-04-18 06:22:33 +0000101 else {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000102 OS << 'i';
Chris Lattner1518afd2011-04-18 06:22:33 +0000103 if (!StripImmCodes)
104 if (unsigned Code = getImmCode())
105 OS << "_" << ImmPredicates.getPredicate(Code-1).getFnName();
106 }
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000107 }
108 };
109
Chris Lattner1518afd2011-04-18 06:22:33 +0000110
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000111 SmallVector<OpKind, 3> Operands;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000112
113 bool operator<(const OperandsSignature &O) const {
114 return Operands < O.Operands;
115 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000116 bool operator==(const OperandsSignature &O) const {
117 return Operands == O.Operands;
118 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000119
120 bool empty() const { return Operands.empty(); }
121
Chris Lattner1518afd2011-04-18 06:22:33 +0000122 bool hasAnyImmediateCodes() const {
123 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
124 if (Operands[i].isImm() && Operands[i].getImmCode() != 0)
125 return true;
126 return false;
127 }
128
129 /// getWithoutImmCodes - Return a copy of this with any immediate codes forced
130 /// to zero.
131 OperandsSignature getWithoutImmCodes() const {
132 OperandsSignature Result;
133 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
134 if (!Operands[i].isImm())
135 Result.Operands.push_back(Operands[i]);
136 else
137 Result.Operands.push_back(OpKind::getImm(0));
138 return Result;
139 }
140
141 void emitImmediatePredicate(raw_ostream &OS, ImmPredicateSet &ImmPredicates) {
142 bool EmittedAnything = false;
143 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
144 if (!Operands[i].isImm()) continue;
145
146 unsigned Code = Operands[i].getImmCode();
147 if (Code == 0) continue;
148
149 if (EmittedAnything)
150 OS << " &&\n ";
151
152 TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1);
153
154 // Emit the type check.
155 OS << "VT == "
156 << getEnumName(PredFn.getOrigPatFragRecord()->getTree(0)->getType(0))
157 << " && ";
158
159
160 OS << PredFn.getFnName() << "(imm" << i <<')';
161 EmittedAnything = true;
162 }
163 }
164
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000165 /// initialize - Examine the given pattern and initialize the contents
166 /// of the Operands array accordingly. Return true if all the operands
167 /// are supported, false otherwise.
168 ///
Chris Lattner602fc062011-04-17 20:23:29 +0000169 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
Chris Lattner1518afd2011-04-18 06:22:33 +0000170 MVT::SimpleValueType VT,
171 ImmPredicateSet &ImmediatePredicates) {
172 if (InstPatNode->isLeaf())
173 return false;
174
175 if (InstPatNode->getOperator()->getName() == "imm") {
176 Operands.push_back(OpKind::getImm(0));
177 return true;
178 }
179
180 if (InstPatNode->getOperator()->getName() == "fpimm") {
181 Operands.push_back(OpKind::getFP());
182 return true;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000183 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000184
Owen Andersonabb1f162008-08-26 01:22:59 +0000185 const CodeGenRegisterClass *DstRC = 0;
Jim Grosbach45258f52010-12-07 19:36:07 +0000186
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000187 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
188 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach45258f52010-12-07 19:36:07 +0000189
Chris Lattner1518afd2011-04-18 06:22:33 +0000190 // Handle imm operands specially.
191 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") {
192 unsigned PredNo = 0;
193 if (!Op->getPredicateFns().empty()) {
Chris Lattner202a7a12011-04-18 06:36:55 +0000194 TreePredicateFn PredFn = Op->getPredicateFns()[0];
Chris Lattner1518afd2011-04-18 06:22:33 +0000195 // If there is more than one predicate weighing in on this operand
196 // then we don't handle it. This doesn't typically happen for
197 // immediates anyway.
198 if (Op->getPredicateFns().size() > 1 ||
Chris Lattner202a7a12011-04-18 06:36:55 +0000199 !PredFn.isImmediatePattern())
200 return false;
201 // Ignore any instruction with 'FastIselShouldIgnore', these are
202 // not needed and just bloat the fast instruction selector. For
203 // example, X86 doesn't need to generate code to match ADD16ri8 since
204 // ADD16ri will do just fine.
205 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord();
206 if (Rec->getValueAsBit("FastIselShouldIgnore"))
Chris Lattner1518afd2011-04-18 06:22:33 +0000207 return false;
208
Chris Lattner202a7a12011-04-18 06:36:55 +0000209 PredNo = ImmediatePredicates.getIDFor(PredFn)+1;
Chris Lattner1518afd2011-04-18 06:22:33 +0000210 }
211
212 // Handle unmatched immediate sizes here.
213 //if (Op->getType(0) != VT)
214 // return false;
215
216 Operands.push_back(OpKind::getImm(PredNo));
217 continue;
218 }
219
220
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000221 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000222 // For now, filter out any operand with multiple values.
Chris Lattner602fc062011-04-17 20:23:29 +0000223 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerd7349192010-03-19 21:37:09 +0000224 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000225
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000226 if (!Op->isLeaf()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000227 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000228 Operands.push_back(OpKind::getFP());
Dale Johannesenedc87742009-05-21 22:25:49 +0000229 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000230 }
Dan Gohman833ddf82008-08-27 16:18:22 +0000231 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000232 return false;
233 }
Chris Lattner602fc062011-04-17 20:23:29 +0000234
235 assert(Op->hasTypeSet(0) && "Type infererence not done?");
236
237 // For now, all the operands must have the same type (if they aren't
238 // immediates). Note that this causes us to reject variable sized shifts
239 // on X86.
240 if (Op->getType(0) != VT)
241 return false;
242
David Greene05bce0b2011-07-29 22:43:06 +0000243 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000244 if (!OpDI)
245 return false;
246 Record *OpLeafRec = OpDI->getDef();
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000247
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000248 // For now, the only other thing we accept is register operands.
Owen Anderson667d8f72008-08-29 17:45:56 +0000249 const CodeGenRegisterClass *RC = 0;
Owen Andersonbea6f612011-06-27 21:06:21 +0000250 if (OpLeafRec->isSubClassOf("RegisterOperand"))
251 OpLeafRec = OpLeafRec->getValueAsDef("RegClass");
Owen Anderson667d8f72008-08-29 17:45:56 +0000252 if (OpLeafRec->isSubClassOf("RegisterClass"))
253 RC = &Target.getRegisterClass(OpLeafRec);
254 else if (OpLeafRec->isSubClassOf("Register"))
Jakob Stoklund Olesen7b9cafd2011-06-15 00:20:40 +0000255 RC = Target.getRegBank().getRegClassForRegister(OpLeafRec);
Owen Anderson667d8f72008-08-29 17:45:56 +0000256 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000257 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000258
Eric Christopher2cfcad92010-08-24 23:21:59 +0000259 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000260 if (!RC)
261 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000262
Eric Christopher53452602010-08-25 04:58:56 +0000263 // For now, all the operands must have the same register class or be
264 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000265 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000266 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000267 return false;
268 } else
269 DstRC = RC;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000270 Operands.push_back(OpKind::getReg());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000271 }
272 return true;
273 }
274
Daniel Dunbar1a551802009-07-03 00:10:29 +0000275 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000276 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000277 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000278 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000279 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000280 OS << "uint64_t imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000281 } else if (Operands[i].isFP()) {
Cameron Zwarich82f00022012-01-07 08:18:37 +0000282 OS << "const ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000283 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000284 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000285 }
286 if (i + 1 != e)
287 OS << ", ";
288 }
289 }
290
Daniel Dunbar1a551802009-07-03 00:10:29 +0000291 void PrintArguments(raw_ostream &OS,
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000292 const std::vector<std::string> &PR) const {
Owen Anderson667d8f72008-08-29 17:45:56 +0000293 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000294 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000295 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000296 if (PR[i] != "")
297 // Implicit physical register operand.
298 continue;
299
300 if (PrintedArg)
301 OS << ", ";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000302 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000303 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000304 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000305 } else if (Operands[i].isImm()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000306 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000307 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000308 } else if (Operands[i].isFP()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000309 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000310 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000311 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000312 llvm_unreachable("Unknown operand kind!");
Owen Anderson667d8f72008-08-29 17:45:56 +0000313 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000314 }
315 }
316
Daniel Dunbar1a551802009-07-03 00:10:29 +0000317 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000318 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000319 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000320 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000321 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000322 OS << "imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000323 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000324 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000325 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000326 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000327 }
328 if (i + 1 != e)
329 OS << ", ";
330 }
331 }
332
Owen Anderson667d8f72008-08-29 17:45:56 +0000333
Chris Lattner1518afd2011-04-18 06:22:33 +0000334 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
335 ImmPredicateSet &ImmPredicates,
336 bool StripImmCodes = false) const {
Evan Cheng98d2d072008-09-08 08:39:33 +0000337 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
338 if (PR[i] != "")
339 // Implicit physical register operand. e.g. Instruction::Mul expect to
340 // select to a binary op. On x86, mul may take a single operand with
341 // the other operand being implicit. We must emit something that looks
342 // like a binary instruction except for the very inner FastEmitInst_*
343 // call.
344 continue;
Chris Lattner1518afd2011-04-18 06:22:33 +0000345 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Evan Cheng98d2d072008-09-08 08:39:33 +0000346 }
347 }
348
Chris Lattner1518afd2011-04-18 06:22:33 +0000349 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
350 bool StripImmCodes = false) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000351 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
Chris Lattner1518afd2011-04-18 06:22:33 +0000352 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000353 }
354};
355
Dan Gohman72d63af2008-08-26 21:21:20 +0000356class FastISelMap {
357 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
359 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000360 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach45258f52010-12-07 19:36:07 +0000361 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopherecfa0792010-07-26 17:53:07 +0000362 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000363
364 OperandsOpcodeTypeRetPredMap SimplePatterns;
365
Chris Lattner1518afd2011-04-18 06:22:33 +0000366 std::map<OperandsSignature, std::vector<OperandsSignature> >
367 SignaturesWithConstantForms;
368
Dan Gohman72d63af2008-08-26 21:21:20 +0000369 std::string InstNS;
Chris Lattner1518afd2011-04-18 06:22:33 +0000370 ImmPredicateSet ImmediatePredicates;
Dan Gohman72d63af2008-08-26 21:21:20 +0000371public:
372 explicit FastISelMap(std::string InstNS);
373
Chris Lattner1518afd2011-04-18 06:22:33 +0000374 void collectPatterns(CodeGenDAGPatterns &CGP);
375 void printImmediatePredicates(raw_ostream &OS);
376 void printFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000377};
378
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000379}
380
381static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
382 return CGP.getSDNodeInfo(Op).getEnumName();
383}
384
385static std::string getLegalCName(std::string OpName) {
386 std::string::size_type pos = OpName.find("::");
387 if (pos != std::string::npos)
388 OpName.replace(pos, 2, "_");
389 return OpName;
390}
391
Dan Gohman72d63af2008-08-26 21:21:20 +0000392FastISelMap::FastISelMap(std::string instns)
393 : InstNS(instns) {
394}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000395
Eli Friedman206a10c2011-04-29 21:58:31 +0000396static std::string PhyRegForNode(TreePatternNode *Op,
397 const CodeGenTarget &Target) {
398 std::string PhysReg;
399
400 if (!Op->isLeaf())
401 return PhysReg;
402
David Greene05bce0b2011-07-29 22:43:06 +0000403 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
Eli Friedman206a10c2011-04-29 21:58:31 +0000404 Record *OpLeafRec = OpDI->getDef();
405 if (!OpLeafRec->isSubClassOf("Register"))
406 return PhysReg;
407
David Greene05bce0b2011-07-29 22:43:06 +0000408 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
Eli Friedman206a10c2011-04-29 21:58:31 +0000409 "Namespace")->getValue())->getValue();
410 PhysReg += "::";
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000411 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName();
Eli Friedman206a10c2011-04-29 21:58:31 +0000412 return PhysReg;
413}
414
Chris Lattner1518afd2011-04-18 06:22:33 +0000415void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000416 const CodeGenTarget &Target = CGP.getTargetInfo();
417
418 // Determine the target's namespace name.
419 InstNS = Target.getInstNamespace() + "::";
420 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000421
Dan Gohman0bfb7522008-08-22 00:28:15 +0000422 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000423 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
424 E = CGP.ptm_end(); I != E; ++I) {
425 const PatternToMatch &Pattern = *I;
426
427 // For now, just look at Instructions, so that we don't have to worry
428 // about emitting multiple instructions for a pattern.
429 TreePatternNode *Dst = Pattern.getDstPattern();
430 if (Dst->isLeaf()) continue;
431 Record *Op = Dst->getOperator();
432 if (!Op->isSubClassOf("Instruction"))
433 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000434 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnera90dbc12011-04-17 22:24:13 +0000435 if (II.Operands.empty())
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000436 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000437
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000438 // For now, ignore multi-instruction patterns.
439 bool MultiInsts = false;
440 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
441 TreePatternNode *ChildOp = Dst->getChild(i);
442 if (ChildOp->isLeaf())
443 continue;
444 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
445 MultiInsts = true;
446 break;
447 }
448 }
449 if (MultiInsts)
450 continue;
451
Dan Gohman379cad42008-08-19 20:36:33 +0000452 // For now, ignore instructions where the first operand is not an
453 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000454 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000455 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000456 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000457 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonbea6f612011-06-27 21:06:21 +0000458 if (Op0Rec->isSubClassOf("RegisterOperand"))
459 Op0Rec = Op0Rec->getValueAsDef("RegClass");
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000460 if (!Op0Rec->isSubClassOf("RegisterClass"))
461 continue;
462 DstRC = &Target.getRegisterClass(Op0Rec);
463 if (!DstRC)
464 continue;
465 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000466 // If this isn't a leaf, then continue since the register classes are
467 // a bit too complicated for now.
468 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000469
David Greene05bce0b2011-07-29 22:43:06 +0000470 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000471 if (SR)
472 SubRegNo = getQualifiedName(SR->getDef());
473 else
474 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000475 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000476
477 // Inspect the pattern.
478 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
479 if (!InstPatNode) continue;
480 if (InstPatNode->isLeaf()) continue;
481
Chris Lattner084df622010-03-24 00:41:19 +0000482 // Ignore multiple result nodes for now.
483 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000484
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000485 Record *InstPatOp = InstPatNode->getOperator();
486 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000487 MVT::SimpleValueType RetVT = MVT::isVoid;
488 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000490 if (InstPatNode->getNumChildren()) {
491 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
492 VT = InstPatNode->getChild(0)->getType(0);
493 }
Dan Gohmanf4137b52008-08-19 20:30:54 +0000494
495 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000496 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000497 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000498
Dan Gohman379cad42008-08-19 20:36:33 +0000499 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000500 OperandsSignature Operands;
Chris Lattner1518afd2011-04-18 06:22:33 +0000501 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000502 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000503
Owen Anderson667d8f72008-08-29 17:45:56 +0000504 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
Eli Friedman206a10c2011-04-29 21:58:31 +0000505 if (InstPatNode->getOperator()->getName() == "imm" ||
Eric Christopher691a4882011-08-23 15:42:35 +0000506 InstPatNode->getOperator()->getName() == "fpimm")
Owen Anderson667d8f72008-08-29 17:45:56 +0000507 PhysRegInputs->push_back("");
Eli Friedman206a10c2011-04-29 21:58:31 +0000508 else {
509 // Compute the PhysRegs used by the given pattern, and check that
510 // the mapping from the src to dst patterns is simple.
511 bool FoundNonSimplePattern = false;
512 unsigned DstIndex = 0;
Owen Anderson667d8f72008-08-29 17:45:56 +0000513 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
Eli Friedman206a10c2011-04-29 21:58:31 +0000514 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
515 if (PhysReg.empty()) {
516 if (DstIndex >= Dst->getNumChildren() ||
517 Dst->getChild(DstIndex)->getName() !=
518 InstPatNode->getChild(i)->getName()) {
519 FoundNonSimplePattern = true;
520 break;
Owen Anderson667d8f72008-08-29 17:45:56 +0000521 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000522 ++DstIndex;
Owen Anderson667d8f72008-08-29 17:45:56 +0000523 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000524
Owen Anderson667d8f72008-08-29 17:45:56 +0000525 PhysRegInputs->push_back(PhysReg);
526 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000527
528 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
529 FoundNonSimplePattern = true;
530
531 if (FoundNonSimplePattern)
532 continue;
533 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000534
Dan Gohman22bb3112008-08-22 00:20:26 +0000535 // Get the predicate that guards this pattern.
536 std::string PredicateCheck = Pattern.getPredicateCheck();
537
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000538 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000539 InstructionMemo Memo = {
540 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000541 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000542 SubRegNo,
543 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000544 };
Chris Lattner1518afd2011-04-18 06:22:33 +0000545
546 if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck))
547 throw TGError(Pattern.getSrcRecord()->getLoc(),
548 "Duplicate record in FastISel table!");
Jim Grosbach997759a2010-12-07 23:05:49 +0000549
Owen Andersonabb1f162008-08-26 01:22:59 +0000550 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Chris Lattner1518afd2011-04-18 06:22:33 +0000551
552 // If any of the operands were immediates with predicates on them, strip
553 // them down to a signature that doesn't have predicates so that we can
554 // associate them with the stripped predicate version.
555 if (Operands.hasAnyImmediateCodes()) {
556 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
557 .push_back(Operands);
558 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000559 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000560}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000561
Chris Lattner1518afd2011-04-18 06:22:33 +0000562void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
563 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
564 return;
565
566 OS << "\n// FastEmit Immediate Predicate functions.\n";
567 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
568 E = ImmediatePredicates.end(); I != E; ++I) {
569 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
570 OS << I->getImmediatePredicateCode() << "\n}\n";
571 }
572
573 OS << "\n\n";
574}
575
576
577void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000578 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000579 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000580 OE = SimplePatterns.end(); OI != OE; ++OI) {
581 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000582 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000583
Owen Anderson7b2e5792008-08-25 23:43:09 +0000584 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000585 I != E; ++I) {
586 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000587 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000588
589 OS << "// FastEmit functions for " << Opcode << ".\n";
590 OS << "\n";
591
592 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000593 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000594 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000596 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000597 if (RM.size() != 1) {
598 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
599 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000601 const PredMap &PM = RI->second;
602 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000603
Evan Chengc3f44b02008-09-03 00:03:49 +0000604 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000605 << getLegalCName(Opcode)
606 << "_" << getLegalCName(getName(VT))
607 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000608 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000609 OS << "(";
610 Operands.PrintParameters(OS);
611 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000612
Owen Anderson71669e52008-08-26 00:42:26 +0000613 // Emit code for each possible instruction. There may be
614 // multiple if there are subtarget concerns.
615 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
616 PI != PE; ++PI) {
617 std::string PredicateCheck = PI->first;
618 const InstructionMemo &Memo = PI->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000619
Owen Anderson71669e52008-08-26 00:42:26 +0000620 if (PredicateCheck.empty()) {
621 assert(!HasPred &&
622 "Multiple instructions match, at least one has "
623 "a predicate and at least one doesn't!");
624 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000625 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000626 OS << " ";
627 HasPred = true;
628 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000629
Owen Anderson667d8f72008-08-29 17:45:56 +0000630 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
631 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000632 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
633 << "TII.get(TargetOpcode::COPY), "
634 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000635 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000636
Owen Anderson71669e52008-08-26 00:42:26 +0000637 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000638 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000639 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
640 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000641 OS << "(" << InstNS << Memo.Name << ", ";
Craig Topper9b58f292012-04-19 06:52:06 +0000642 OS << "&" << InstNS << Memo.RC->getName() << "RegClass";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000643 if (!Operands.empty())
644 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000645 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000646 OS << ");\n";
647 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000648 OS << "extractsubreg(" << getName(RetVT);
Chris Lattner1518afd2011-04-18 06:22:33 +0000649 OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000650 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000651
Owen Anderson667d8f72008-08-29 17:45:56 +0000652 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000653 OS << " }\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000654
Owen Anderson71669e52008-08-26 00:42:26 +0000655 }
656 // Return 0 if none of the predicates were satisfied.
657 if (HasPred)
658 OS << " return 0;\n";
659 OS << "}\n";
660 OS << "\n";
661 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000662
Owen Anderson71669e52008-08-26 00:42:26 +0000663 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000664 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000665 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000666 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000667 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000669 if (!Operands.empty())
670 OS << ", ";
671 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000673 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
674 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000676 OS << " case " << getName(RetVT) << ": return FastEmit_"
677 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
678 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000679 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000680 OS << "(";
681 Operands.PrintArguments(OS);
682 OS << ");\n";
683 }
684 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000685
Owen Anderson71669e52008-08-26 00:42:26 +0000686 } else {
687 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000688 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000689 << getLegalCName(Opcode) << "_"
690 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000691 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000693 if (!Operands.empty())
694 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000695 Operands.PrintParameters(OS);
696 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000697
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000699 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000700
Owen Anderson71669e52008-08-26 00:42:26 +0000701 const PredMap &PM = RM.begin()->second;
702 bool HasPred = false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000703
Owen Anderson7b2e5792008-08-25 23:43:09 +0000704 // Emit code for each possible instruction. There may be
705 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000706 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
707 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000708 std::string PredicateCheck = PI->first;
709 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000710
Owen Anderson7b2e5792008-08-25 23:43:09 +0000711 if (PredicateCheck.empty()) {
712 assert(!HasPred &&
713 "Multiple instructions match, at least one has "
714 "a predicate and at least one doesn't!");
715 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000716 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000717 OS << " ";
718 HasPred = true;
719 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000720
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000721 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
722 if ((*Memo.PhysRegs)[i] != "")
723 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
724 << "TII.get(TargetOpcode::COPY), "
725 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
726 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000727
Owen Anderson7b2e5792008-08-25 23:43:09 +0000728 OS << " return FastEmitInst_";
Jim Grosbach45258f52010-12-07 19:36:07 +0000729
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000730 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000731 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
732 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000733 OS << "(" << InstNS << Memo.Name << ", ";
Craig Topper9b58f292012-04-19 06:52:06 +0000734 OS << "&" << InstNS << Memo.RC->getName() << "RegClass";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000735 if (!Operands.empty())
736 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000737 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000738 OS << ");\n";
739 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000740 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000741 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000742 OS << ");\n";
743 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000744
Owen Anderson667d8f72008-08-29 17:45:56 +0000745 if (HasPred)
746 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000747 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000748
Owen Anderson7b2e5792008-08-25 23:43:09 +0000749 // Return 0 if none of the predicates were satisfied.
750 if (HasPred)
751 OS << " return 0;\n";
752 OS << "}\n";
753 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000754 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000755 }
756
757 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000758 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000759 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000760 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000762 if (!Operands.empty())
763 OS << ", ";
764 Operands.PrintParameters(OS);
765 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000767 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000768 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000770 std::string TypeName = getName(VT);
771 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000772 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000773 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000774 OS << "(RetVT";
775 if (!Operands.empty())
776 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000777 Operands.PrintArguments(OS);
778 OS << ");\n";
779 }
780 OS << " default: return 0;\n";
781 OS << " }\n";
782 OS << "}\n";
783 OS << "\n";
784 }
785
Dan Gohman0bfb7522008-08-22 00:28:15 +0000786 OS << "// Top-level FastEmit function.\n";
787 OS << "\n";
788
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000789 // Emit one function for the operand signature that demultiplexes based
790 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000791 OS << "unsigned FastEmit_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000792 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000793 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000794 if (!Operands.empty())
795 OS << ", ";
796 Operands.PrintParameters(OS);
797 OS << ") {\n";
Chris Lattner1518afd2011-04-18 06:22:33 +0000798
799 // If there are any forms of this signature available that operand on
800 // constrained forms of the immediate (e.g. 32-bit sext immediate in a
801 // 64-bit operand), check them first.
802
803 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
804 = SignaturesWithConstantForms.find(Operands);
805 if (MI != SignaturesWithConstantForms.end()) {
806 // Unique any duplicates out of the list.
807 std::sort(MI->second.begin(), MI->second.end());
808 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
809 MI->second.end());
810
811 // Check each in order it was seen. It would be nice to have a good
812 // relative ordering between them, but we're not going for optimality
813 // here.
814 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
815 OS << " if (";
816 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
817 OS << ")\n if (unsigned Reg = FastEmit_";
818 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
819 OS << "(VT, RetVT, Opcode";
820 if (!MI->second[i].empty())
821 OS << ", ";
822 MI->second[i].PrintArguments(OS);
823 OS << "))\n return Reg;\n\n";
824 }
825
826 // Done with this, remove it.
827 SignaturesWithConstantForms.erase(MI);
828 }
829
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000830 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000831 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000832 I != E; ++I) {
833 const std::string &Opcode = I->first;
834
835 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000836 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000837 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000838 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000839 if (!Operands.empty())
840 OS << ", ";
841 Operands.PrintArguments(OS);
842 OS << ");\n";
843 }
844 OS << " default: return 0;\n";
845 OS << " }\n";
846 OS << "}\n";
847 OS << "\n";
848 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000849
850 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman72d63af2008-08-26 21:21:20 +0000851}
852
Daniel Dunbar1a551802009-07-03 00:10:29 +0000853void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000854 const CodeGenTarget &Target = CGP.getTargetInfo();
855
856 // Determine the target's namespace name.
857 std::string InstNS = Target.getInstNamespace() + "::";
858 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
859
860 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
861 Target.getName() + " target", OS);
862
Dan Gohman72d63af2008-08-26 21:21:20 +0000863 FastISelMap F(InstNS);
Chris Lattner1518afd2011-04-18 06:22:33 +0000864 F.collectPatterns(CGP);
865 F.printImmediatePredicates(OS);
866 F.printFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000867}
868
869FastISelEmitter::FastISelEmitter(RecordKeeper &R)
Chris Lattner1518afd2011-04-18 06:22:33 +0000870 : Records(R), CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000871}
Dan Gohman72d63af2008-08-26 21:21:20 +0000872