blob: e3d47aa49a0e6e1cd6dab2930f969556ade3a715 [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};
Chris Lattner1518afd2011-04-18 06:22:33 +000038
39/// ImmPredicateSet - This uniques predicates (represented as a string) and
40/// gives them unique (small) integer ID's that start at 0.
41class ImmPredicateSet {
42 DenseMap<TreePattern *, unsigned> ImmIDs;
43 std::vector<TreePredicateFn> PredsByName;
44public:
45
46 unsigned getIDFor(TreePredicateFn Pred) {
47 unsigned &Entry = ImmIDs[Pred.getOrigPatFragRecord()];
48 if (Entry == 0) {
49 PredsByName.push_back(Pred);
50 Entry = PredsByName.size();
51 }
52 return Entry-1;
53 }
54
55 const TreePredicateFn &getPredicate(unsigned i) {
56 assert(i < PredsByName.size());
57 return PredsByName[i];
58 }
59
60 typedef std::vector<TreePredicateFn>::const_iterator iterator;
61 iterator begin() const { return PredsByName.begin(); }
62 iterator end() const { return PredsByName.end(); }
63
64};
Owen Anderson667d8f72008-08-29 17:45:56 +000065
Dan Gohman04b7dfb2008-08-19 18:06:12 +000066/// OperandsSignature - This class holds a description of a list of operand
67/// types. It has utility methods for emitting text based on the operands.
68///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000069struct OperandsSignature {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000070 class OpKind {
71 enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 };
72 char Repr;
73 public:
74
75 OpKind() : Repr(OK_Invalid) {}
76
77 bool operator<(OpKind RHS) const { return Repr < RHS.Repr; }
Chris Lattner1518afd2011-04-18 06:22:33 +000078 bool operator==(OpKind RHS) const { return Repr == RHS.Repr; }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000079
80 static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; }
81 static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; }
Chris Lattner1518afd2011-04-18 06:22:33 +000082 static OpKind getImm(unsigned V) {
83 assert((unsigned)OK_Imm+V < 128 &&
84 "Too many integer predicates for the 'Repr' char");
85 OpKind K; K.Repr = OK_Imm+V; return K;
86 }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000087
88 bool isReg() const { return Repr == OK_Reg; }
89 bool isFP() const { return Repr == OK_FP; }
Chris Lattner1518afd2011-04-18 06:22:33 +000090 bool isImm() const { return Repr >= OK_Imm; }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000091
Chris Lattner1518afd2011-04-18 06:22:33 +000092 unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; }
93
94 void printManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
95 bool StripImmCodes) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000096 if (isReg())
97 OS << 'r';
98 else if (isFP())
99 OS << 'f';
Chris Lattner1518afd2011-04-18 06:22:33 +0000100 else {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000101 OS << 'i';
Chris Lattner1518afd2011-04-18 06:22:33 +0000102 if (!StripImmCodes)
103 if (unsigned Code = getImmCode())
104 OS << "_" << ImmPredicates.getPredicate(Code-1).getFnName();
105 }
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000106 }
107 };
108
Chris Lattner1518afd2011-04-18 06:22:33 +0000109
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000110 SmallVector<OpKind, 3> Operands;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000111
112 bool operator<(const OperandsSignature &O) const {
113 return Operands < O.Operands;
114 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000115 bool operator==(const OperandsSignature &O) const {
116 return Operands == O.Operands;
117 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000118
119 bool empty() const { return Operands.empty(); }
120
Chris Lattner1518afd2011-04-18 06:22:33 +0000121 bool hasAnyImmediateCodes() const {
122 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
123 if (Operands[i].isImm() && Operands[i].getImmCode() != 0)
124 return true;
125 return false;
126 }
127
128 /// getWithoutImmCodes - Return a copy of this with any immediate codes forced
129 /// to zero.
130 OperandsSignature getWithoutImmCodes() const {
131 OperandsSignature Result;
132 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
133 if (!Operands[i].isImm())
134 Result.Operands.push_back(Operands[i]);
135 else
136 Result.Operands.push_back(OpKind::getImm(0));
137 return Result;
138 }
139
140 void emitImmediatePredicate(raw_ostream &OS, ImmPredicateSet &ImmPredicates) {
141 bool EmittedAnything = false;
142 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
143 if (!Operands[i].isImm()) continue;
144
145 unsigned Code = Operands[i].getImmCode();
146 if (Code == 0) continue;
147
148 if (EmittedAnything)
149 OS << " &&\n ";
150
151 TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1);
152
153 // Emit the type check.
154 OS << "VT == "
155 << getEnumName(PredFn.getOrigPatFragRecord()->getTree(0)->getType(0))
156 << " && ";
157
158
159 OS << PredFn.getFnName() << "(imm" << i <<')';
160 EmittedAnything = true;
161 }
162 }
163
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000164 /// initialize - Examine the given pattern and initialize the contents
165 /// of the Operands array accordingly. Return true if all the operands
166 /// are supported, false otherwise.
167 ///
Chris Lattner602fc062011-04-17 20:23:29 +0000168 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
Chris Lattner1518afd2011-04-18 06:22:33 +0000169 MVT::SimpleValueType VT,
170 ImmPredicateSet &ImmediatePredicates) {
171 if (InstPatNode->isLeaf())
172 return false;
173
174 if (InstPatNode->getOperator()->getName() == "imm") {
175 Operands.push_back(OpKind::getImm(0));
176 return true;
177 }
178
179 if (InstPatNode->getOperator()->getName() == "fpimm") {
180 Operands.push_back(OpKind::getFP());
181 return true;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000182 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000183
Owen Andersonabb1f162008-08-26 01:22:59 +0000184 const CodeGenRegisterClass *DstRC = 0;
Jim Grosbach45258f52010-12-07 19:36:07 +0000185
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000186 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
187 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach45258f52010-12-07 19:36:07 +0000188
Chris Lattner1518afd2011-04-18 06:22:33 +0000189 // Handle imm operands specially.
190 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") {
191 unsigned PredNo = 0;
192 if (!Op->getPredicateFns().empty()) {
Chris Lattner202a7a12011-04-18 06:36:55 +0000193 TreePredicateFn PredFn = Op->getPredicateFns()[0];
Chris Lattner1518afd2011-04-18 06:22:33 +0000194 // If there is more than one predicate weighing in on this operand
195 // then we don't handle it. This doesn't typically happen for
196 // immediates anyway.
197 if (Op->getPredicateFns().size() > 1 ||
Chris Lattner202a7a12011-04-18 06:36:55 +0000198 !PredFn.isImmediatePattern())
199 return false;
200 // Ignore any instruction with 'FastIselShouldIgnore', these are
201 // not needed and just bloat the fast instruction selector. For
202 // example, X86 doesn't need to generate code to match ADD16ri8 since
203 // ADD16ri will do just fine.
204 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord();
205 if (Rec->getValueAsBit("FastIselShouldIgnore"))
Chris Lattner1518afd2011-04-18 06:22:33 +0000206 return false;
207
Chris Lattner202a7a12011-04-18 06:36:55 +0000208 PredNo = ImmediatePredicates.getIDFor(PredFn)+1;
Chris Lattner1518afd2011-04-18 06:22:33 +0000209 }
210
211 // Handle unmatched immediate sizes here.
212 //if (Op->getType(0) != VT)
213 // return false;
214
215 Operands.push_back(OpKind::getImm(PredNo));
216 continue;
217 }
218
219
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000220 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000221 // For now, filter out any operand with multiple values.
Chris Lattner602fc062011-04-17 20:23:29 +0000222 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerd7349192010-03-19 21:37:09 +0000223 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000224
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000225 if (!Op->isLeaf()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000226 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000227 Operands.push_back(OpKind::getFP());
Dale Johannesenedc87742009-05-21 22:25:49 +0000228 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000229 }
Dan Gohman833ddf82008-08-27 16:18:22 +0000230 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000231 return false;
232 }
Chris Lattner602fc062011-04-17 20:23:29 +0000233
234 assert(Op->hasTypeSet(0) && "Type infererence not done?");
235
236 // For now, all the operands must have the same type (if they aren't
237 // immediates). Note that this causes us to reject variable sized shifts
238 // on X86.
239 if (Op->getType(0) != VT)
240 return false;
241
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000242 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
243 if (!OpDI)
244 return false;
245 Record *OpLeafRec = OpDI->getDef();
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000246
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000247 // For now, the only other thing we accept is register operands.
Owen Anderson667d8f72008-08-29 17:45:56 +0000248 const CodeGenRegisterClass *RC = 0;
249 if (OpLeafRec->isSubClassOf("RegisterClass"))
250 RC = &Target.getRegisterClass(OpLeafRec);
251 else if (OpLeafRec->isSubClassOf("Register"))
252 RC = Target.getRegisterClassForRegister(OpLeafRec);
253 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000254 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000255
Eric Christopher2cfcad92010-08-24 23:21:59 +0000256 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000257 if (!RC)
258 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000259
Eric Christopher53452602010-08-25 04:58:56 +0000260 // For now, all the operands must have the same register class or be
261 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000262 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000263 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000264 return false;
265 } else
266 DstRC = RC;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000267 Operands.push_back(OpKind::getReg());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000268 }
269 return true;
270 }
271
Daniel Dunbar1a551802009-07-03 00:10:29 +0000272 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000273 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000274 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000275 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000276 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000277 OS << "uint64_t imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000278 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000279 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000280 } else {
281 assert("Unknown operand kind!");
282 abort();
283 }
284 if (i + 1 != e)
285 OS << ", ";
286 }
287 }
288
Daniel Dunbar1a551802009-07-03 00:10:29 +0000289 void PrintArguments(raw_ostream &OS,
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000290 const std::vector<std::string> &PR) const {
Owen Anderson667d8f72008-08-29 17:45:56 +0000291 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000292 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000293 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000294 if (PR[i] != "")
295 // Implicit physical register operand.
296 continue;
297
298 if (PrintedArg)
299 OS << ", ";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000300 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000301 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000302 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000303 } else if (Operands[i].isImm()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000304 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000305 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000306 } else if (Operands[i].isFP()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000307 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000308 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000309 } else {
310 assert("Unknown operand kind!");
311 abort();
312 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000313 }
314 }
315
Daniel Dunbar1a551802009-07-03 00:10:29 +0000316 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000317 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000318 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000319 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000320 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000321 OS << "imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000322 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000323 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000324 } else {
325 assert("Unknown operand kind!");
326 abort();
327 }
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
Chris Lattner1518afd2011-04-18 06:22:33 +0000396void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000397 const CodeGenTarget &Target = CGP.getTargetInfo();
398
399 // Determine the target's namespace name.
400 InstNS = Target.getInstNamespace() + "::";
401 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000402
Dan Gohman0bfb7522008-08-22 00:28:15 +0000403 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000404 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
405 E = CGP.ptm_end(); I != E; ++I) {
406 const PatternToMatch &Pattern = *I;
407
408 // For now, just look at Instructions, so that we don't have to worry
409 // about emitting multiple instructions for a pattern.
410 TreePatternNode *Dst = Pattern.getDstPattern();
411 if (Dst->isLeaf()) continue;
412 Record *Op = Dst->getOperator();
413 if (!Op->isSubClassOf("Instruction"))
414 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000415 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnera90dbc12011-04-17 22:24:13 +0000416 if (II.Operands.empty())
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000417 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000418
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000419 // For now, ignore multi-instruction patterns.
420 bool MultiInsts = false;
421 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
422 TreePatternNode *ChildOp = Dst->getChild(i);
423 if (ChildOp->isLeaf())
424 continue;
425 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
426 MultiInsts = true;
427 break;
428 }
429 }
430 if (MultiInsts)
431 continue;
432
Dan Gohman379cad42008-08-19 20:36:33 +0000433 // For now, ignore instructions where the first operand is not an
434 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000435 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000436 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000437 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000438 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000439 if (!Op0Rec->isSubClassOf("RegisterClass"))
440 continue;
441 DstRC = &Target.getRegisterClass(Op0Rec);
442 if (!DstRC)
443 continue;
444 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000445 // If this isn't a leaf, then continue since the register classes are
446 // a bit too complicated for now.
447 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000448
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000449 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
450 if (SR)
451 SubRegNo = getQualifiedName(SR->getDef());
452 else
453 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000454 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000455
456 // Inspect the pattern.
457 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
458 if (!InstPatNode) continue;
459 if (InstPatNode->isLeaf()) continue;
460
Chris Lattner084df622010-03-24 00:41:19 +0000461 // Ignore multiple result nodes for now.
462 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000463
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000464 Record *InstPatOp = InstPatNode->getOperator();
465 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000466 MVT::SimpleValueType RetVT = MVT::isVoid;
467 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000469 if (InstPatNode->getNumChildren()) {
470 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
471 VT = InstPatNode->getChild(0)->getType(0);
472 }
Chris Lattner602fc062011-04-17 20:23:29 +0000473
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000474 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000475 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000476 if (InstPatOp->isSubClassOf("Operand"))
477 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000478
479 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000480 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000481 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000482
Dan Gohman379cad42008-08-19 20:36:33 +0000483 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000484 OperandsSignature Operands;
Chris Lattner1518afd2011-04-18 06:22:33 +0000485 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000486 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000487
Owen Anderson667d8f72008-08-29 17:45:56 +0000488 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
489 if (!InstPatNode->isLeaf() &&
490 (InstPatNode->getOperator()->getName() == "imm" ||
491 InstPatNode->getOperator()->getName() == "fpimmm"))
492 PhysRegInputs->push_back("");
493 else if (!InstPatNode->isLeaf()) {
494 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
495 TreePatternNode *Op = InstPatNode->getChild(i);
496 if (!Op->isLeaf()) {
497 PhysRegInputs->push_back("");
498 continue;
499 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000500
Owen Anderson667d8f72008-08-29 17:45:56 +0000501 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
502 Record *OpLeafRec = OpDI->getDef();
503 std::string PhysReg;
504 if (OpLeafRec->isSubClassOf("Register")) {
505 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
506 "Namespace")->getValue())->getValue();
507 PhysReg += "::";
Jim Grosbach45258f52010-12-07 19:36:07 +0000508
Owen Anderson667d8f72008-08-29 17:45:56 +0000509 std::vector<CodeGenRegister> Regs = Target.getRegisters();
510 for (unsigned i = 0; i < Regs.size(); ++i) {
511 if (Regs[i].TheDef == OpLeafRec) {
512 PhysReg += Regs[i].getName();
513 break;
514 }
515 }
516 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000517
Owen Anderson667d8f72008-08-29 17:45:56 +0000518 PhysRegInputs->push_back(PhysReg);
519 }
520 } else
521 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000522
Dan Gohman22bb3112008-08-22 00:20:26 +0000523 // Get the predicate that guards this pattern.
524 std::string PredicateCheck = Pattern.getPredicateCheck();
525
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000526 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000527 InstructionMemo Memo = {
528 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000529 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000530 SubRegNo,
531 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000532 };
Chris Lattner1518afd2011-04-18 06:22:33 +0000533
534 if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck))
535 throw TGError(Pattern.getSrcRecord()->getLoc(),
536 "Duplicate record in FastISel table!");
Jim Grosbach997759a2010-12-07 23:05:49 +0000537
Owen Andersonabb1f162008-08-26 01:22:59 +0000538 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Chris Lattner1518afd2011-04-18 06:22:33 +0000539
540 // If any of the operands were immediates with predicates on them, strip
541 // them down to a signature that doesn't have predicates so that we can
542 // associate them with the stripped predicate version.
543 if (Operands.hasAnyImmediateCodes()) {
544 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
545 .push_back(Operands);
546 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000547 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000548}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000549
Chris Lattner1518afd2011-04-18 06:22:33 +0000550void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
551 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
552 return;
553
554 OS << "\n// FastEmit Immediate Predicate functions.\n";
555 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
556 E = ImmediatePredicates.end(); I != E; ++I) {
557 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
558 OS << I->getImmediatePredicateCode() << "\n}\n";
559 }
560
561 OS << "\n\n";
562}
563
564
565void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000566 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000567 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000568 OE = SimplePatterns.end(); OI != OE; ++OI) {
569 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000570 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000571
Owen Anderson7b2e5792008-08-25 23:43:09 +0000572 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000573 I != E; ++I) {
574 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000575 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000576
577 OS << "// FastEmit functions for " << Opcode << ".\n";
578 OS << "\n";
579
580 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000581 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000582 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000584 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000585 if (RM.size() != 1) {
586 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
587 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000589 const PredMap &PM = RI->second;
590 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000591
Evan Chengc3f44b02008-09-03 00:03:49 +0000592 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000593 << getLegalCName(Opcode)
594 << "_" << getLegalCName(getName(VT))
595 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000596 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000597 OS << "(";
598 Operands.PrintParameters(OS);
599 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000600
Owen Anderson71669e52008-08-26 00:42:26 +0000601 // Emit code for each possible instruction. There may be
602 // multiple if there are subtarget concerns.
603 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
604 PI != PE; ++PI) {
605 std::string PredicateCheck = PI->first;
606 const InstructionMemo &Memo = PI->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000607
Owen Anderson71669e52008-08-26 00:42:26 +0000608 if (PredicateCheck.empty()) {
609 assert(!HasPred &&
610 "Multiple instructions match, at least one has "
611 "a predicate and at least one doesn't!");
612 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000613 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000614 OS << " ";
615 HasPred = true;
616 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000617
Owen Anderson667d8f72008-08-29 17:45:56 +0000618 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
619 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000620 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
621 << "TII.get(TargetOpcode::COPY), "
622 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000623 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000624
Owen Anderson71669e52008-08-26 00:42:26 +0000625 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000626 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000627 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
628 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000629 OS << "(" << InstNS << Memo.Name << ", ";
630 OS << InstNS << Memo.RC->getName() << "RegisterClass";
631 if (!Operands.empty())
632 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000633 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000634 OS << ");\n";
635 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000636 OS << "extractsubreg(" << getName(RetVT);
Chris Lattner1518afd2011-04-18 06:22:33 +0000637 OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000638 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000639
Owen Anderson667d8f72008-08-29 17:45:56 +0000640 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000641 OS << " }\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000642
Owen Anderson71669e52008-08-26 00:42:26 +0000643 }
644 // Return 0 if none of the predicates were satisfied.
645 if (HasPred)
646 OS << " return 0;\n";
647 OS << "}\n";
648 OS << "\n";
649 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000650
Owen Anderson71669e52008-08-26 00:42:26 +0000651 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000652 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000653 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000654 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000655 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000657 if (!Operands.empty())
658 OS << ", ";
659 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000661 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
662 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000664 OS << " case " << getName(RetVT) << ": return FastEmit_"
665 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
666 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000667 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000668 OS << "(";
669 Operands.PrintArguments(OS);
670 OS << ");\n";
671 }
672 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000673
Owen Anderson71669e52008-08-26 00:42:26 +0000674 } else {
675 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000676 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000677 << getLegalCName(Opcode) << "_"
678 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000679 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000681 if (!Operands.empty())
682 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000683 Operands.PrintParameters(OS);
684 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000685
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000687 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000688
Owen Anderson71669e52008-08-26 00:42:26 +0000689 const PredMap &PM = RM.begin()->second;
690 bool HasPred = false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000691
Owen Anderson7b2e5792008-08-25 23:43:09 +0000692 // Emit code for each possible instruction. There may be
693 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000694 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
695 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000696 std::string PredicateCheck = PI->first;
697 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000698
Owen Anderson7b2e5792008-08-25 23:43:09 +0000699 if (PredicateCheck.empty()) {
700 assert(!HasPred &&
701 "Multiple instructions match, at least one has "
702 "a predicate and at least one doesn't!");
703 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000704 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000705 OS << " ";
706 HasPred = true;
707 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000708
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000709 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
710 if ((*Memo.PhysRegs)[i] != "")
711 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
712 << "TII.get(TargetOpcode::COPY), "
713 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
714 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000715
Owen Anderson7b2e5792008-08-25 23:43:09 +0000716 OS << " return FastEmitInst_";
Jim Grosbach45258f52010-12-07 19:36:07 +0000717
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000718 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000719 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
720 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000721 OS << "(" << InstNS << Memo.Name << ", ";
722 OS << InstNS << Memo.RC->getName() << "RegisterClass";
723 if (!Operands.empty())
724 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000725 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000726 OS << ");\n";
727 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000728 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000729 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000730 OS << ");\n";
731 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000732
Owen Anderson667d8f72008-08-29 17:45:56 +0000733 if (HasPred)
734 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000735 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000736
Owen Anderson7b2e5792008-08-25 23:43:09 +0000737 // Return 0 if none of the predicates were satisfied.
738 if (HasPred)
739 OS << " return 0;\n";
740 OS << "}\n";
741 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000742 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000743 }
744
745 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000746 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000747 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000748 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000750 if (!Operands.empty())
751 OS << ", ";
752 Operands.PrintParameters(OS);
753 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000755 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000756 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000758 std::string TypeName = getName(VT);
759 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000760 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000761 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000762 OS << "(RetVT";
763 if (!Operands.empty())
764 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000765 Operands.PrintArguments(OS);
766 OS << ");\n";
767 }
768 OS << " default: return 0;\n";
769 OS << " }\n";
770 OS << "}\n";
771 OS << "\n";
772 }
773
Dan Gohman0bfb7522008-08-22 00:28:15 +0000774 OS << "// Top-level FastEmit function.\n";
775 OS << "\n";
776
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000777 // Emit one function for the operand signature that demultiplexes based
778 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000779 OS << "unsigned FastEmit_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000780 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000781 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000782 if (!Operands.empty())
783 OS << ", ";
784 Operands.PrintParameters(OS);
785 OS << ") {\n";
Chris Lattner1518afd2011-04-18 06:22:33 +0000786
787 // If there are any forms of this signature available that operand on
788 // constrained forms of the immediate (e.g. 32-bit sext immediate in a
789 // 64-bit operand), check them first.
790
791 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
792 = SignaturesWithConstantForms.find(Operands);
793 if (MI != SignaturesWithConstantForms.end()) {
794 // Unique any duplicates out of the list.
795 std::sort(MI->second.begin(), MI->second.end());
796 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
797 MI->second.end());
798
799 // Check each in order it was seen. It would be nice to have a good
800 // relative ordering between them, but we're not going for optimality
801 // here.
802 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
803 OS << " if (";
804 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
805 OS << ")\n if (unsigned Reg = FastEmit_";
806 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
807 OS << "(VT, RetVT, Opcode";
808 if (!MI->second[i].empty())
809 OS << ", ";
810 MI->second[i].PrintArguments(OS);
811 OS << "))\n return Reg;\n\n";
812 }
813
814 // Done with this, remove it.
815 SignaturesWithConstantForms.erase(MI);
816 }
817
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000818 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000819 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000820 I != E; ++I) {
821 const std::string &Opcode = I->first;
822
823 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000824 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000825 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000826 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000827 if (!Operands.empty())
828 OS << ", ";
829 Operands.PrintArguments(OS);
830 OS << ");\n";
831 }
832 OS << " default: return 0;\n";
833 OS << " }\n";
834 OS << "}\n";
835 OS << "\n";
836 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000837
838 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman72d63af2008-08-26 21:21:20 +0000839}
840
Daniel Dunbar1a551802009-07-03 00:10:29 +0000841void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000842 const CodeGenTarget &Target = CGP.getTargetInfo();
843
844 // Determine the target's namespace name.
845 std::string InstNS = Target.getInstNamespace() + "::";
846 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
847
848 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
849 Target.getName() + " target", OS);
850
Dan Gohman72d63af2008-08-26 21:21:20 +0000851 FastISelMap F(InstNS);
Chris Lattner1518afd2011-04-18 06:22:33 +0000852 F.collectPatterns(CGP);
853 F.printImmediatePredicates(OS);
854 F.printFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000855}
856
857FastISelEmitter::FastISelEmitter(RecordKeeper &R)
Chris Lattner1518afd2011-04-18 06:22:33 +0000858 : Records(R), CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000859}
Dan Gohman72d63af2008-08-26 21:21:20 +0000860