blob: ef912df3e88e67c0bdcad716dc7e03ff22d7b0d8 [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"
Jim Grosbach76612b52010-12-07 19:35:36 +000022#include "llvm/ADT/SmallString.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000023#include "llvm/ADT/VectorExtras.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
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000243 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
244 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;
250 if (OpLeafRec->isSubClassOf("RegisterClass"))
251 RC = &Target.getRegisterClass(OpLeafRec);
252 else if (OpLeafRec->isSubClassOf("Register"))
Jakob Stoklund Olesen7b9cafd2011-06-15 00:20:40 +0000253 RC = Target.getRegBank().getRegClassForRegister(OpLeafRec);
Owen Anderson667d8f72008-08-29 17:45:56 +0000254 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000255 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000256
Eric Christopher2cfcad92010-08-24 23:21:59 +0000257 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000258 if (!RC)
259 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000260
Eric Christopher53452602010-08-25 04:58:56 +0000261 // For now, all the operands must have the same register class or be
262 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000263 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000264 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000265 return false;
266 } else
267 DstRC = RC;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000268 Operands.push_back(OpKind::getReg());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000269 }
270 return true;
271 }
272
Daniel Dunbar1a551802009-07-03 00:10:29 +0000273 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000274 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000275 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000276 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000277 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000278 OS << "uint64_t imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000279 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000280 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000281 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000282 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000283 }
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 {
Chad Rosier36a300a2011-06-07 20:41:31 +0000310 llvm_unreachable("Unknown operand kind!");
Owen Anderson667d8f72008-08-29 17:45:56 +0000311 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000312 }
313 }
314
Daniel Dunbar1a551802009-07-03 00:10:29 +0000315 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000316 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000317 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000318 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000319 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000320 OS << "imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000321 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000322 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000323 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000324 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000325 }
326 if (i + 1 != e)
327 OS << ", ";
328 }
329 }
330
Owen Anderson667d8f72008-08-29 17:45:56 +0000331
Chris Lattner1518afd2011-04-18 06:22:33 +0000332 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
333 ImmPredicateSet &ImmPredicates,
334 bool StripImmCodes = false) const {
Evan Cheng98d2d072008-09-08 08:39:33 +0000335 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
336 if (PR[i] != "")
337 // Implicit physical register operand. e.g. Instruction::Mul expect to
338 // select to a binary op. On x86, mul may take a single operand with
339 // the other operand being implicit. We must emit something that looks
340 // like a binary instruction except for the very inner FastEmitInst_*
341 // call.
342 continue;
Chris Lattner1518afd2011-04-18 06:22:33 +0000343 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Evan Cheng98d2d072008-09-08 08:39:33 +0000344 }
345 }
346
Chris Lattner1518afd2011-04-18 06:22:33 +0000347 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
348 bool StripImmCodes = false) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000349 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
Chris Lattner1518afd2011-04-18 06:22:33 +0000350 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000351 }
352};
353
Dan Gohman72d63af2008-08-26 21:21:20 +0000354class FastISelMap {
355 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
357 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000358 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach45258f52010-12-07 19:36:07 +0000359 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopherecfa0792010-07-26 17:53:07 +0000360 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000361
362 OperandsOpcodeTypeRetPredMap SimplePatterns;
363
Chris Lattner1518afd2011-04-18 06:22:33 +0000364 std::map<OperandsSignature, std::vector<OperandsSignature> >
365 SignaturesWithConstantForms;
366
Dan Gohman72d63af2008-08-26 21:21:20 +0000367 std::string InstNS;
Chris Lattner1518afd2011-04-18 06:22:33 +0000368 ImmPredicateSet ImmediatePredicates;
Dan Gohman72d63af2008-08-26 21:21:20 +0000369public:
370 explicit FastISelMap(std::string InstNS);
371
Chris Lattner1518afd2011-04-18 06:22:33 +0000372 void collectPatterns(CodeGenDAGPatterns &CGP);
373 void printImmediatePredicates(raw_ostream &OS);
374 void printFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000375};
376
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000377}
378
379static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
380 return CGP.getSDNodeInfo(Op).getEnumName();
381}
382
383static std::string getLegalCName(std::string OpName) {
384 std::string::size_type pos = OpName.find("::");
385 if (pos != std::string::npos)
386 OpName.replace(pos, 2, "_");
387 return OpName;
388}
389
Dan Gohman72d63af2008-08-26 21:21:20 +0000390FastISelMap::FastISelMap(std::string instns)
391 : InstNS(instns) {
392}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000393
Eli Friedman206a10c2011-04-29 21:58:31 +0000394static std::string PhyRegForNode(TreePatternNode *Op,
395 const CodeGenTarget &Target) {
396 std::string PhysReg;
397
398 if (!Op->isLeaf())
399 return PhysReg;
400
401 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
402 Record *OpLeafRec = OpDI->getDef();
403 if (!OpLeafRec->isSubClassOf("Register"))
404 return PhysReg;
405
406 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
407 "Namespace")->getValue())->getValue();
408 PhysReg += "::";
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000409 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName();
Eli Friedman206a10c2011-04-29 21:58:31 +0000410 return PhysReg;
411}
412
Chris Lattner1518afd2011-04-18 06:22:33 +0000413void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000414 const CodeGenTarget &Target = CGP.getTargetInfo();
415
416 // Determine the target's namespace name.
417 InstNS = Target.getInstNamespace() + "::";
418 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000419
Dan Gohman0bfb7522008-08-22 00:28:15 +0000420 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000421 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
422 E = CGP.ptm_end(); I != E; ++I) {
423 const PatternToMatch &Pattern = *I;
424
425 // For now, just look at Instructions, so that we don't have to worry
426 // about emitting multiple instructions for a pattern.
427 TreePatternNode *Dst = Pattern.getDstPattern();
428 if (Dst->isLeaf()) continue;
429 Record *Op = Dst->getOperator();
430 if (!Op->isSubClassOf("Instruction"))
431 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000432 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnera90dbc12011-04-17 22:24:13 +0000433 if (II.Operands.empty())
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000434 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000435
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000436 // For now, ignore multi-instruction patterns.
437 bool MultiInsts = false;
438 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
439 TreePatternNode *ChildOp = Dst->getChild(i);
440 if (ChildOp->isLeaf())
441 continue;
442 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
443 MultiInsts = true;
444 break;
445 }
446 }
447 if (MultiInsts)
448 continue;
449
Dan Gohman379cad42008-08-19 20:36:33 +0000450 // For now, ignore instructions where the first operand is not an
451 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000452 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000453 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000454 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000455 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000456 if (!Op0Rec->isSubClassOf("RegisterClass"))
457 continue;
458 DstRC = &Target.getRegisterClass(Op0Rec);
459 if (!DstRC)
460 continue;
461 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000462 // If this isn't a leaf, then continue since the register classes are
463 // a bit too complicated for now.
464 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000465
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000466 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
467 if (SR)
468 SubRegNo = getQualifiedName(SR->getDef());
469 else
470 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000471 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000472
473 // Inspect the pattern.
474 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
475 if (!InstPatNode) continue;
476 if (InstPatNode->isLeaf()) continue;
477
Chris Lattner084df622010-03-24 00:41:19 +0000478 // Ignore multiple result nodes for now.
479 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000480
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000481 Record *InstPatOp = InstPatNode->getOperator();
482 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000483 MVT::SimpleValueType RetVT = MVT::isVoid;
484 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000486 if (InstPatNode->getNumChildren()) {
487 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
488 VT = InstPatNode->getChild(0)->getType(0);
489 }
Dan Gohmanf4137b52008-08-19 20:30:54 +0000490
491 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000492 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000493 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000494
Dan Gohman379cad42008-08-19 20:36:33 +0000495 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000496 OperandsSignature Operands;
Chris Lattner1518afd2011-04-18 06:22:33 +0000497 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000498 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000499
Owen Anderson667d8f72008-08-29 17:45:56 +0000500 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
Eli Friedman206a10c2011-04-29 21:58:31 +0000501 if (InstPatNode->getOperator()->getName() == "imm" ||
502 InstPatNode->getOperator()->getName() == "fpimmm")
Owen Anderson667d8f72008-08-29 17:45:56 +0000503 PhysRegInputs->push_back("");
Eli Friedman206a10c2011-04-29 21:58:31 +0000504 else {
505 // Compute the PhysRegs used by the given pattern, and check that
506 // the mapping from the src to dst patterns is simple.
507 bool FoundNonSimplePattern = false;
508 unsigned DstIndex = 0;
Owen Anderson667d8f72008-08-29 17:45:56 +0000509 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
Eli Friedman206a10c2011-04-29 21:58:31 +0000510 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
511 if (PhysReg.empty()) {
512 if (DstIndex >= Dst->getNumChildren() ||
513 Dst->getChild(DstIndex)->getName() !=
514 InstPatNode->getChild(i)->getName()) {
515 FoundNonSimplePattern = true;
516 break;
Owen Anderson667d8f72008-08-29 17:45:56 +0000517 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000518 ++DstIndex;
Owen Anderson667d8f72008-08-29 17:45:56 +0000519 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000520
Owen Anderson667d8f72008-08-29 17:45:56 +0000521 PhysRegInputs->push_back(PhysReg);
522 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000523
524 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
525 FoundNonSimplePattern = true;
526
527 if (FoundNonSimplePattern)
528 continue;
529 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000530
Dan Gohman22bb3112008-08-22 00:20:26 +0000531 // Get the predicate that guards this pattern.
532 std::string PredicateCheck = Pattern.getPredicateCheck();
533
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000534 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000535 InstructionMemo Memo = {
536 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000537 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000538 SubRegNo,
539 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000540 };
Chris Lattner1518afd2011-04-18 06:22:33 +0000541
542 if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck))
543 throw TGError(Pattern.getSrcRecord()->getLoc(),
544 "Duplicate record in FastISel table!");
Jim Grosbach997759a2010-12-07 23:05:49 +0000545
Owen Andersonabb1f162008-08-26 01:22:59 +0000546 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Chris Lattner1518afd2011-04-18 06:22:33 +0000547
548 // If any of the operands were immediates with predicates on them, strip
549 // them down to a signature that doesn't have predicates so that we can
550 // associate them with the stripped predicate version.
551 if (Operands.hasAnyImmediateCodes()) {
552 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
553 .push_back(Operands);
554 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000555 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000556}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000557
Chris Lattner1518afd2011-04-18 06:22:33 +0000558void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
559 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
560 return;
561
562 OS << "\n// FastEmit Immediate Predicate functions.\n";
563 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
564 E = ImmediatePredicates.end(); I != E; ++I) {
565 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
566 OS << I->getImmediatePredicateCode() << "\n}\n";
567 }
568
569 OS << "\n\n";
570}
571
572
573void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000574 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000575 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000576 OE = SimplePatterns.end(); OI != OE; ++OI) {
577 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000578 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000579
Owen Anderson7b2e5792008-08-25 23:43:09 +0000580 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000581 I != E; ++I) {
582 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000583 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000584
585 OS << "// FastEmit functions for " << Opcode << ".\n";
586 OS << "\n";
587
588 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000589 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000590 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000592 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000593 if (RM.size() != 1) {
594 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
595 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000597 const PredMap &PM = RI->second;
598 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000599
Evan Chengc3f44b02008-09-03 00:03:49 +0000600 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000601 << getLegalCName(Opcode)
602 << "_" << getLegalCName(getName(VT))
603 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000604 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000605 OS << "(";
606 Operands.PrintParameters(OS);
607 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000608
Owen Anderson71669e52008-08-26 00:42:26 +0000609 // Emit code for each possible instruction. There may be
610 // multiple if there are subtarget concerns.
611 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
612 PI != PE; ++PI) {
613 std::string PredicateCheck = PI->first;
614 const InstructionMemo &Memo = PI->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000615
Owen Anderson71669e52008-08-26 00:42:26 +0000616 if (PredicateCheck.empty()) {
617 assert(!HasPred &&
618 "Multiple instructions match, at least one has "
619 "a predicate and at least one doesn't!");
620 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000621 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000622 OS << " ";
623 HasPred = true;
624 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000625
Owen Anderson667d8f72008-08-29 17:45:56 +0000626 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
627 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000628 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
629 << "TII.get(TargetOpcode::COPY), "
630 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000631 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000632
Owen Anderson71669e52008-08-26 00:42:26 +0000633 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000634 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000635 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
636 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000637 OS << "(" << InstNS << Memo.Name << ", ";
638 OS << InstNS << Memo.RC->getName() << "RegisterClass";
639 if (!Operands.empty())
640 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000641 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000642 OS << ");\n";
643 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000644 OS << "extractsubreg(" << getName(RetVT);
Chris Lattner1518afd2011-04-18 06:22:33 +0000645 OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000646 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000647
Owen Anderson667d8f72008-08-29 17:45:56 +0000648 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000649 OS << " }\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000650
Owen Anderson71669e52008-08-26 00:42:26 +0000651 }
652 // Return 0 if none of the predicates were satisfied.
653 if (HasPred)
654 OS << " return 0;\n";
655 OS << "}\n";
656 OS << "\n";
657 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000658
Owen Anderson71669e52008-08-26 00:42:26 +0000659 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000660 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000661 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000662 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000663 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000665 if (!Operands.empty())
666 OS << ", ";
667 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000669 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
670 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000672 OS << " case " << getName(RetVT) << ": return FastEmit_"
673 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
674 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000675 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000676 OS << "(";
677 Operands.PrintArguments(OS);
678 OS << ");\n";
679 }
680 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000681
Owen Anderson71669e52008-08-26 00:42:26 +0000682 } else {
683 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000684 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000685 << getLegalCName(Opcode) << "_"
686 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000687 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000689 if (!Operands.empty())
690 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000691 Operands.PrintParameters(OS);
692 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000695 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000696
Owen Anderson71669e52008-08-26 00:42:26 +0000697 const PredMap &PM = RM.begin()->second;
698 bool HasPred = false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000699
Owen Anderson7b2e5792008-08-25 23:43:09 +0000700 // Emit code for each possible instruction. There may be
701 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000702 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
703 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000704 std::string PredicateCheck = PI->first;
705 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000706
Owen Anderson7b2e5792008-08-25 23:43:09 +0000707 if (PredicateCheck.empty()) {
708 assert(!HasPred &&
709 "Multiple instructions match, at least one has "
710 "a predicate and at least one doesn't!");
711 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000712 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000713 OS << " ";
714 HasPred = true;
715 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000716
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000717 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
718 if ((*Memo.PhysRegs)[i] != "")
719 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
720 << "TII.get(TargetOpcode::COPY), "
721 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
722 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000723
Owen Anderson7b2e5792008-08-25 23:43:09 +0000724 OS << " return FastEmitInst_";
Jim Grosbach45258f52010-12-07 19:36:07 +0000725
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000726 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000727 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
728 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000729 OS << "(" << InstNS << Memo.Name << ", ";
730 OS << InstNS << Memo.RC->getName() << "RegisterClass";
731 if (!Operands.empty())
732 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000733 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000734 OS << ");\n";
735 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000736 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000737 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000738 OS << ");\n";
739 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000740
Owen Anderson667d8f72008-08-29 17:45:56 +0000741 if (HasPred)
742 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000743 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000744
Owen Anderson7b2e5792008-08-25 23:43:09 +0000745 // Return 0 if none of the predicates were satisfied.
746 if (HasPred)
747 OS << " return 0;\n";
748 OS << "}\n";
749 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000750 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000751 }
752
753 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000754 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000755 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000756 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000758 if (!Operands.empty())
759 OS << ", ";
760 Operands.PrintParameters(OS);
761 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000763 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000764 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000766 std::string TypeName = getName(VT);
767 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000768 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000769 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000770 OS << "(RetVT";
771 if (!Operands.empty())
772 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000773 Operands.PrintArguments(OS);
774 OS << ");\n";
775 }
776 OS << " default: return 0;\n";
777 OS << " }\n";
778 OS << "}\n";
779 OS << "\n";
780 }
781
Dan Gohman0bfb7522008-08-22 00:28:15 +0000782 OS << "// Top-level FastEmit function.\n";
783 OS << "\n";
784
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000785 // Emit one function for the operand signature that demultiplexes based
786 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000787 OS << "unsigned FastEmit_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000788 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000789 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000790 if (!Operands.empty())
791 OS << ", ";
792 Operands.PrintParameters(OS);
793 OS << ") {\n";
Chris Lattner1518afd2011-04-18 06:22:33 +0000794
795 // If there are any forms of this signature available that operand on
796 // constrained forms of the immediate (e.g. 32-bit sext immediate in a
797 // 64-bit operand), check them first.
798
799 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
800 = SignaturesWithConstantForms.find(Operands);
801 if (MI != SignaturesWithConstantForms.end()) {
802 // Unique any duplicates out of the list.
803 std::sort(MI->second.begin(), MI->second.end());
804 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
805 MI->second.end());
806
807 // Check each in order it was seen. It would be nice to have a good
808 // relative ordering between them, but we're not going for optimality
809 // here.
810 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
811 OS << " if (";
812 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
813 OS << ")\n if (unsigned Reg = FastEmit_";
814 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
815 OS << "(VT, RetVT, Opcode";
816 if (!MI->second[i].empty())
817 OS << ", ";
818 MI->second[i].PrintArguments(OS);
819 OS << "))\n return Reg;\n\n";
820 }
821
822 // Done with this, remove it.
823 SignaturesWithConstantForms.erase(MI);
824 }
825
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000826 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000827 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000828 I != E; ++I) {
829 const std::string &Opcode = I->first;
830
831 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000832 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000833 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000834 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000835 if (!Operands.empty())
836 OS << ", ";
837 Operands.PrintArguments(OS);
838 OS << ");\n";
839 }
840 OS << " default: return 0;\n";
841 OS << " }\n";
842 OS << "}\n";
843 OS << "\n";
844 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000845
846 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman72d63af2008-08-26 21:21:20 +0000847}
848
Daniel Dunbar1a551802009-07-03 00:10:29 +0000849void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000850 const CodeGenTarget &Target = CGP.getTargetInfo();
851
852 // Determine the target's namespace name.
853 std::string InstNS = Target.getInstNamespace() + "::";
854 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
855
856 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
857 Target.getName() + " target", OS);
858
Dan Gohman72d63af2008-08-26 21:21:20 +0000859 FastISelMap F(InstNS);
Chris Lattner1518afd2011-04-18 06:22:33 +0000860 F.collectPatterns(CGP);
861 F.printImmediatePredicates(OS);
862 F.printFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000863}
864
865FastISelEmitter::FastISelEmitter(RecordKeeper &R)
Chris Lattner1518afd2011-04-18 06:22:33 +0000866 : Records(R), CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000867}
Dan Gohman72d63af2008-08-26 21:21:20 +0000868