blob: ce821765a2c262639a7edf3af8fae3719ebbd29d [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()) {
193 // If there is more than one predicate weighing in on this operand
194 // then we don't handle it. This doesn't typically happen for
195 // immediates anyway.
196 if (Op->getPredicateFns().size() > 1 ||
197 !Op->getPredicateFns()[0].isImmediatePattern())
198 return false;
199
200 PredNo = ImmediatePredicates.getIDFor(Op->getPredicateFns()[0])+1;
201 }
202
203 // Handle unmatched immediate sizes here.
204 //if (Op->getType(0) != VT)
205 // return false;
206
207 Operands.push_back(OpKind::getImm(PredNo));
208 continue;
209 }
210
211
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000212 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000213 // For now, filter out any operand with multiple values.
Chris Lattner602fc062011-04-17 20:23:29 +0000214 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerd7349192010-03-19 21:37:09 +0000215 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000216
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000217 if (!Op->isLeaf()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000218 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000219 Operands.push_back(OpKind::getFP());
Dale Johannesenedc87742009-05-21 22:25:49 +0000220 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000221 }
Dan Gohman833ddf82008-08-27 16:18:22 +0000222 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000223 return false;
224 }
Chris Lattner602fc062011-04-17 20:23:29 +0000225
226 assert(Op->hasTypeSet(0) && "Type infererence not done?");
227
228 // For now, all the operands must have the same type (if they aren't
229 // immediates). Note that this causes us to reject variable sized shifts
230 // on X86.
231 if (Op->getType(0) != VT)
232 return false;
233
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000234 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
235 if (!OpDI)
236 return false;
237 Record *OpLeafRec = OpDI->getDef();
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000238
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000239 // For now, the only other thing we accept is register operands.
Owen Anderson667d8f72008-08-29 17:45:56 +0000240 const CodeGenRegisterClass *RC = 0;
241 if (OpLeafRec->isSubClassOf("RegisterClass"))
242 RC = &Target.getRegisterClass(OpLeafRec);
243 else if (OpLeafRec->isSubClassOf("Register"))
244 RC = Target.getRegisterClassForRegister(OpLeafRec);
245 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000246 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000247
Eric Christopher2cfcad92010-08-24 23:21:59 +0000248 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000249 if (!RC)
250 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000251
Eric Christopher53452602010-08-25 04:58:56 +0000252 // For now, all the operands must have the same register class or be
253 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000254 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000255 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000256 return false;
257 } else
258 DstRC = RC;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000259 Operands.push_back(OpKind::getReg());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000260 }
261 return true;
262 }
263
Daniel Dunbar1a551802009-07-03 00:10:29 +0000264 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000265 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000266 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000267 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000268 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000269 OS << "uint64_t imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000270 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000271 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000272 } else {
273 assert("Unknown operand kind!");
274 abort();
275 }
276 if (i + 1 != e)
277 OS << ", ";
278 }
279 }
280
Daniel Dunbar1a551802009-07-03 00:10:29 +0000281 void PrintArguments(raw_ostream &OS,
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000282 const std::vector<std::string> &PR) const {
Owen Anderson667d8f72008-08-29 17:45:56 +0000283 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000284 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000285 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000286 if (PR[i] != "")
287 // Implicit physical register operand.
288 continue;
289
290 if (PrintedArg)
291 OS << ", ";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000292 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000293 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000294 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000295 } else if (Operands[i].isImm()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000296 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000297 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000298 } else if (Operands[i].isFP()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000299 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000300 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000301 } else {
302 assert("Unknown operand kind!");
303 abort();
304 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000305 }
306 }
307
Daniel Dunbar1a551802009-07-03 00:10:29 +0000308 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000309 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000310 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000311 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000312 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000313 OS << "imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000314 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000315 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000316 } else {
317 assert("Unknown operand kind!");
318 abort();
319 }
320 if (i + 1 != e)
321 OS << ", ";
322 }
323 }
324
Owen Anderson667d8f72008-08-29 17:45:56 +0000325
Chris Lattner1518afd2011-04-18 06:22:33 +0000326 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
327 ImmPredicateSet &ImmPredicates,
328 bool StripImmCodes = false) const {
Evan Cheng98d2d072008-09-08 08:39:33 +0000329 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
330 if (PR[i] != "")
331 // Implicit physical register operand. e.g. Instruction::Mul expect to
332 // select to a binary op. On x86, mul may take a single operand with
333 // the other operand being implicit. We must emit something that looks
334 // like a binary instruction except for the very inner FastEmitInst_*
335 // call.
336 continue;
Chris Lattner1518afd2011-04-18 06:22:33 +0000337 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Evan Cheng98d2d072008-09-08 08:39:33 +0000338 }
339 }
340
Chris Lattner1518afd2011-04-18 06:22:33 +0000341 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
342 bool StripImmCodes = false) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000343 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
Chris Lattner1518afd2011-04-18 06:22:33 +0000344 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000345 }
346};
347
Dan Gohman72d63af2008-08-26 21:21:20 +0000348class FastISelMap {
349 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
351 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000352 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach45258f52010-12-07 19:36:07 +0000353 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopherecfa0792010-07-26 17:53:07 +0000354 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000355
356 OperandsOpcodeTypeRetPredMap SimplePatterns;
357
Chris Lattner1518afd2011-04-18 06:22:33 +0000358 std::map<OperandsSignature, std::vector<OperandsSignature> >
359 SignaturesWithConstantForms;
360
Dan Gohman72d63af2008-08-26 21:21:20 +0000361 std::string InstNS;
Chris Lattner1518afd2011-04-18 06:22:33 +0000362 ImmPredicateSet ImmediatePredicates;
Dan Gohman72d63af2008-08-26 21:21:20 +0000363public:
364 explicit FastISelMap(std::string InstNS);
365
Chris Lattner1518afd2011-04-18 06:22:33 +0000366 void collectPatterns(CodeGenDAGPatterns &CGP);
367 void printImmediatePredicates(raw_ostream &OS);
368 void printFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000369};
370
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000371}
372
373static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
374 return CGP.getSDNodeInfo(Op).getEnumName();
375}
376
377static std::string getLegalCName(std::string OpName) {
378 std::string::size_type pos = OpName.find("::");
379 if (pos != std::string::npos)
380 OpName.replace(pos, 2, "_");
381 return OpName;
382}
383
Dan Gohman72d63af2008-08-26 21:21:20 +0000384FastISelMap::FastISelMap(std::string instns)
385 : InstNS(instns) {
386}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000387
Chris Lattner1518afd2011-04-18 06:22:33 +0000388void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000389 const CodeGenTarget &Target = CGP.getTargetInfo();
390
391 // Determine the target's namespace name.
392 InstNS = Target.getInstNamespace() + "::";
393 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000394
Dan Gohman0bfb7522008-08-22 00:28:15 +0000395 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000396 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
397 E = CGP.ptm_end(); I != E; ++I) {
398 const PatternToMatch &Pattern = *I;
399
400 // For now, just look at Instructions, so that we don't have to worry
401 // about emitting multiple instructions for a pattern.
402 TreePatternNode *Dst = Pattern.getDstPattern();
403 if (Dst->isLeaf()) continue;
404 Record *Op = Dst->getOperator();
405 if (!Op->isSubClassOf("Instruction"))
406 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000407 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnera90dbc12011-04-17 22:24:13 +0000408 if (II.Operands.empty())
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000409 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000410
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000411 // For now, ignore multi-instruction patterns.
412 bool MultiInsts = false;
413 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
414 TreePatternNode *ChildOp = Dst->getChild(i);
415 if (ChildOp->isLeaf())
416 continue;
417 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
418 MultiInsts = true;
419 break;
420 }
421 }
422 if (MultiInsts)
423 continue;
424
Dan Gohman379cad42008-08-19 20:36:33 +0000425 // For now, ignore instructions where the first operand is not an
426 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000427 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000428 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000429 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000430 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000431 if (!Op0Rec->isSubClassOf("RegisterClass"))
432 continue;
433 DstRC = &Target.getRegisterClass(Op0Rec);
434 if (!DstRC)
435 continue;
436 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000437 // If this isn't a leaf, then continue since the register classes are
438 // a bit too complicated for now.
439 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000440
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000441 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
442 if (SR)
443 SubRegNo = getQualifiedName(SR->getDef());
444 else
445 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000446 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000447
448 // Inspect the pattern.
449 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
450 if (!InstPatNode) continue;
451 if (InstPatNode->isLeaf()) continue;
452
Chris Lattner084df622010-03-24 00:41:19 +0000453 // Ignore multiple result nodes for now.
454 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000455
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000456 Record *InstPatOp = InstPatNode->getOperator();
457 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000458 MVT::SimpleValueType RetVT = MVT::isVoid;
459 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000461 if (InstPatNode->getNumChildren()) {
462 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
463 VT = InstPatNode->getChild(0)->getType(0);
464 }
Chris Lattner602fc062011-04-17 20:23:29 +0000465
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000466 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000467 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000468 if (InstPatOp->isSubClassOf("Operand"))
469 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000470
471 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000472 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000473 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000474
Dan Gohman379cad42008-08-19 20:36:33 +0000475 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000476 OperandsSignature Operands;
Chris Lattner1518afd2011-04-18 06:22:33 +0000477 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000478 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000479
Owen Anderson667d8f72008-08-29 17:45:56 +0000480 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
481 if (!InstPatNode->isLeaf() &&
482 (InstPatNode->getOperator()->getName() == "imm" ||
483 InstPatNode->getOperator()->getName() == "fpimmm"))
484 PhysRegInputs->push_back("");
485 else if (!InstPatNode->isLeaf()) {
486 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
487 TreePatternNode *Op = InstPatNode->getChild(i);
488 if (!Op->isLeaf()) {
489 PhysRegInputs->push_back("");
490 continue;
491 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000492
Owen Anderson667d8f72008-08-29 17:45:56 +0000493 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
494 Record *OpLeafRec = OpDI->getDef();
495 std::string PhysReg;
496 if (OpLeafRec->isSubClassOf("Register")) {
497 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
498 "Namespace")->getValue())->getValue();
499 PhysReg += "::";
Jim Grosbach45258f52010-12-07 19:36:07 +0000500
Owen Anderson667d8f72008-08-29 17:45:56 +0000501 std::vector<CodeGenRegister> Regs = Target.getRegisters();
502 for (unsigned i = 0; i < Regs.size(); ++i) {
503 if (Regs[i].TheDef == OpLeafRec) {
504 PhysReg += Regs[i].getName();
505 break;
506 }
507 }
508 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000509
Owen Anderson667d8f72008-08-29 17:45:56 +0000510 PhysRegInputs->push_back(PhysReg);
511 }
512 } else
513 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000514
Dan Gohman22bb3112008-08-22 00:20:26 +0000515 // Get the predicate that guards this pattern.
516 std::string PredicateCheck = Pattern.getPredicateCheck();
517
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000518 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000519 InstructionMemo Memo = {
520 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000521 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000522 SubRegNo,
523 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000524 };
Chris Lattner1518afd2011-04-18 06:22:33 +0000525
526 if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck))
527 throw TGError(Pattern.getSrcRecord()->getLoc(),
528 "Duplicate record in FastISel table!");
Jim Grosbach997759a2010-12-07 23:05:49 +0000529
Owen Andersonabb1f162008-08-26 01:22:59 +0000530 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Chris Lattner1518afd2011-04-18 06:22:33 +0000531
532 // If any of the operands were immediates with predicates on them, strip
533 // them down to a signature that doesn't have predicates so that we can
534 // associate them with the stripped predicate version.
535 if (Operands.hasAnyImmediateCodes()) {
536 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
537 .push_back(Operands);
538 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000539 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000540}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000541
Chris Lattner1518afd2011-04-18 06:22:33 +0000542void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
543 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
544 return;
545
546 OS << "\n// FastEmit Immediate Predicate functions.\n";
547 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
548 E = ImmediatePredicates.end(); I != E; ++I) {
549 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
550 OS << I->getImmediatePredicateCode() << "\n}\n";
551 }
552
553 OS << "\n\n";
554}
555
556
557void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000558 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000559 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000560 OE = SimplePatterns.end(); OI != OE; ++OI) {
561 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000562 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000563
Owen Anderson7b2e5792008-08-25 23:43:09 +0000564 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000565 I != E; ++I) {
566 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000567 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000568
569 OS << "// FastEmit functions for " << Opcode << ".\n";
570 OS << "\n";
571
572 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000573 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000574 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000575 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000576 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000577 if (RM.size() != 1) {
578 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
579 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000581 const PredMap &PM = RI->second;
582 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000583
Evan Chengc3f44b02008-09-03 00:03:49 +0000584 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000585 << getLegalCName(Opcode)
586 << "_" << getLegalCName(getName(VT))
587 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000588 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000589 OS << "(";
590 Operands.PrintParameters(OS);
591 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000592
Owen Anderson71669e52008-08-26 00:42:26 +0000593 // Emit code for each possible instruction. There may be
594 // multiple if there are subtarget concerns.
595 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
596 PI != PE; ++PI) {
597 std::string PredicateCheck = PI->first;
598 const InstructionMemo &Memo = PI->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000599
Owen Anderson71669e52008-08-26 00:42:26 +0000600 if (PredicateCheck.empty()) {
601 assert(!HasPred &&
602 "Multiple instructions match, at least one has "
603 "a predicate and at least one doesn't!");
604 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000605 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000606 OS << " ";
607 HasPred = true;
608 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000609
Owen Anderson667d8f72008-08-29 17:45:56 +0000610 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
611 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000612 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
613 << "TII.get(TargetOpcode::COPY), "
614 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000615 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000616
Owen Anderson71669e52008-08-26 00:42:26 +0000617 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000618 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000619 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
620 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000621 OS << "(" << InstNS << Memo.Name << ", ";
622 OS << InstNS << Memo.RC->getName() << "RegisterClass";
623 if (!Operands.empty())
624 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000625 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000626 OS << ");\n";
627 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000628 OS << "extractsubreg(" << getName(RetVT);
Chris Lattner1518afd2011-04-18 06:22:33 +0000629 OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000630 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000631
Owen Anderson667d8f72008-08-29 17:45:56 +0000632 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000633 OS << " }\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000634
Owen Anderson71669e52008-08-26 00:42:26 +0000635 }
636 // Return 0 if none of the predicates were satisfied.
637 if (HasPred)
638 OS << " return 0;\n";
639 OS << "}\n";
640 OS << "\n";
641 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000642
Owen Anderson71669e52008-08-26 00:42:26 +0000643 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000644 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000645 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000646 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000647 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000649 if (!Operands.empty())
650 OS << ", ";
651 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000653 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
654 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000656 OS << " case " << getName(RetVT) << ": return FastEmit_"
657 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
658 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000659 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000660 OS << "(";
661 Operands.PrintArguments(OS);
662 OS << ");\n";
663 }
664 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000665
Owen Anderson71669e52008-08-26 00:42:26 +0000666 } else {
667 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000668 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000669 << getLegalCName(Opcode) << "_"
670 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000671 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000673 if (!Operands.empty())
674 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000675 Operands.PrintParameters(OS);
676 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000677
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000679 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000680
Owen Anderson71669e52008-08-26 00:42:26 +0000681 const PredMap &PM = RM.begin()->second;
682 bool HasPred = false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000683
Owen Anderson7b2e5792008-08-25 23:43:09 +0000684 // Emit code for each possible instruction. There may be
685 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000686 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
687 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000688 std::string PredicateCheck = PI->first;
689 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000690
Owen Anderson7b2e5792008-08-25 23:43:09 +0000691 if (PredicateCheck.empty()) {
692 assert(!HasPred &&
693 "Multiple instructions match, at least one has "
694 "a predicate and at least one doesn't!");
695 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000696 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000697 OS << " ";
698 HasPred = true;
699 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000700
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000701 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
702 if ((*Memo.PhysRegs)[i] != "")
703 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
704 << "TII.get(TargetOpcode::COPY), "
705 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
706 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000707
Owen Anderson7b2e5792008-08-25 23:43:09 +0000708 OS << " return FastEmitInst_";
Jim Grosbach45258f52010-12-07 19:36:07 +0000709
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000710 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000711 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
712 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000713 OS << "(" << InstNS << Memo.Name << ", ";
714 OS << InstNS << Memo.RC->getName() << "RegisterClass";
715 if (!Operands.empty())
716 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000717 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000718 OS << ");\n";
719 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000720 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000721 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000722 OS << ");\n";
723 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000724
Owen Anderson667d8f72008-08-29 17:45:56 +0000725 if (HasPred)
726 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000727 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000728
Owen Anderson7b2e5792008-08-25 23:43:09 +0000729 // Return 0 if none of the predicates were satisfied.
730 if (HasPred)
731 OS << " return 0;\n";
732 OS << "}\n";
733 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000734 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000735 }
736
737 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000738 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000739 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000740 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000742 if (!Operands.empty())
743 OS << ", ";
744 Operands.PrintParameters(OS);
745 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000747 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000748 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000750 std::string TypeName = getName(VT);
751 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000752 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000753 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000754 OS << "(RetVT";
755 if (!Operands.empty())
756 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000757 Operands.PrintArguments(OS);
758 OS << ");\n";
759 }
760 OS << " default: return 0;\n";
761 OS << " }\n";
762 OS << "}\n";
763 OS << "\n";
764 }
765
Dan Gohman0bfb7522008-08-22 00:28:15 +0000766 OS << "// Top-level FastEmit function.\n";
767 OS << "\n";
768
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000769 // Emit one function for the operand signature that demultiplexes based
770 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000771 OS << "unsigned FastEmit_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000772 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000773 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000774 if (!Operands.empty())
775 OS << ", ";
776 Operands.PrintParameters(OS);
777 OS << ") {\n";
Chris Lattner1518afd2011-04-18 06:22:33 +0000778
779 // If there are any forms of this signature available that operand on
780 // constrained forms of the immediate (e.g. 32-bit sext immediate in a
781 // 64-bit operand), check them first.
782
783 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
784 = SignaturesWithConstantForms.find(Operands);
785 if (MI != SignaturesWithConstantForms.end()) {
786 // Unique any duplicates out of the list.
787 std::sort(MI->second.begin(), MI->second.end());
788 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
789 MI->second.end());
790
791 // Check each in order it was seen. It would be nice to have a good
792 // relative ordering between them, but we're not going for optimality
793 // here.
794 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
795 OS << " if (";
796 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
797 OS << ")\n if (unsigned Reg = FastEmit_";
798 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
799 OS << "(VT, RetVT, Opcode";
800 if (!MI->second[i].empty())
801 OS << ", ";
802 MI->second[i].PrintArguments(OS);
803 OS << "))\n return Reg;\n\n";
804 }
805
806 // Done with this, remove it.
807 SignaturesWithConstantForms.erase(MI);
808 }
809
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000810 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000811 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000812 I != E; ++I) {
813 const std::string &Opcode = I->first;
814
815 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000816 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000817 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000818 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000819 if (!Operands.empty())
820 OS << ", ";
821 Operands.PrintArguments(OS);
822 OS << ");\n";
823 }
824 OS << " default: return 0;\n";
825 OS << " }\n";
826 OS << "}\n";
827 OS << "\n";
828 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000829
830 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman72d63af2008-08-26 21:21:20 +0000831}
832
Daniel Dunbar1a551802009-07-03 00:10:29 +0000833void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000834 const CodeGenTarget &Target = CGP.getTargetInfo();
835
836 // Determine the target's namespace name.
837 std::string InstNS = Target.getInstNamespace() + "::";
838 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
839
840 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
841 Target.getName() + " target", OS);
842
Dan Gohman72d63af2008-08-26 21:21:20 +0000843 FastISelMap F(InstNS);
Chris Lattner1518afd2011-04-18 06:22:33 +0000844 F.collectPatterns(CGP);
845 F.printImmediatePredicates(OS);
846 F.printFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000847}
848
849FastISelEmitter::FastISelEmitter(RecordKeeper &R)
Chris Lattner1518afd2011-04-18 06:22:33 +0000850 : Records(R), CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000851}
Dan Gohman72d63af2008-08-26 21:21:20 +0000852