blob: f54e8df40f2d623c8dc0997af7dee7b694d1ffbd [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"
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000021#include "Error.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000022#include "Record.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"
Chad Rosier36a300a2011-06-07 20:41:31 +000025#include "llvm/Support/Debug.h"
26#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000027using namespace llvm;
28
29namespace {
30
Owen Anderson667d8f72008-08-29 17:45:56 +000031/// InstructionMemo - This class holds additional information about an
32/// instruction needed to emit code for it.
33///
34struct InstructionMemo {
35 std::string Name;
36 const CodeGenRegisterClass *RC;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +000037 std::string SubRegNo;
Owen Anderson667d8f72008-08-29 17:45:56 +000038 std::vector<std::string>* PhysRegs;
39};
Chris Lattner1518afd2011-04-18 06:22:33 +000040
41/// ImmPredicateSet - This uniques predicates (represented as a string) and
42/// gives them unique (small) integer ID's that start at 0.
43class ImmPredicateSet {
44 DenseMap<TreePattern *, unsigned> ImmIDs;
45 std::vector<TreePredicateFn> PredsByName;
46public:
47
48 unsigned getIDFor(TreePredicateFn Pred) {
49 unsigned &Entry = ImmIDs[Pred.getOrigPatFragRecord()];
50 if (Entry == 0) {
51 PredsByName.push_back(Pred);
52 Entry = PredsByName.size();
53 }
54 return Entry-1;
55 }
56
57 const TreePredicateFn &getPredicate(unsigned i) {
58 assert(i < PredsByName.size());
59 return PredsByName[i];
60 }
61
62 typedef std::vector<TreePredicateFn>::const_iterator iterator;
63 iterator begin() const { return PredsByName.begin(); }
64 iterator end() const { return PredsByName.end(); }
65
66};
Owen Anderson667d8f72008-08-29 17:45:56 +000067
Dan Gohman04b7dfb2008-08-19 18:06:12 +000068/// OperandsSignature - This class holds a description of a list of operand
69/// types. It has utility methods for emitting text based on the operands.
70///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000071struct OperandsSignature {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000072 class OpKind {
73 enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 };
74 char Repr;
75 public:
76
77 OpKind() : Repr(OK_Invalid) {}
78
79 bool operator<(OpKind RHS) const { return Repr < RHS.Repr; }
Chris Lattner1518afd2011-04-18 06:22:33 +000080 bool operator==(OpKind RHS) const { return Repr == RHS.Repr; }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000081
82 static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; }
83 static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; }
Chris Lattner1518afd2011-04-18 06:22:33 +000084 static OpKind getImm(unsigned V) {
85 assert((unsigned)OK_Imm+V < 128 &&
86 "Too many integer predicates for the 'Repr' char");
87 OpKind K; K.Repr = OK_Imm+V; return K;
88 }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000089
90 bool isReg() const { return Repr == OK_Reg; }
91 bool isFP() const { return Repr == OK_FP; }
Chris Lattner1518afd2011-04-18 06:22:33 +000092 bool isImm() const { return Repr >= OK_Imm; }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000093
Chris Lattner1518afd2011-04-18 06:22:33 +000094 unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; }
95
96 void printManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
97 bool StripImmCodes) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000098 if (isReg())
99 OS << 'r';
100 else if (isFP())
101 OS << 'f';
Chris Lattner1518afd2011-04-18 06:22:33 +0000102 else {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000103 OS << 'i';
Chris Lattner1518afd2011-04-18 06:22:33 +0000104 if (!StripImmCodes)
105 if (unsigned Code = getImmCode())
106 OS << "_" << ImmPredicates.getPredicate(Code-1).getFnName();
107 }
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000108 }
109 };
110
Chris Lattner1518afd2011-04-18 06:22:33 +0000111
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000112 SmallVector<OpKind, 3> Operands;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000113
114 bool operator<(const OperandsSignature &O) const {
115 return Operands < O.Operands;
116 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000117 bool operator==(const OperandsSignature &O) const {
118 return Operands == O.Operands;
119 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000120
121 bool empty() const { return Operands.empty(); }
122
Chris Lattner1518afd2011-04-18 06:22:33 +0000123 bool hasAnyImmediateCodes() const {
124 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
125 if (Operands[i].isImm() && Operands[i].getImmCode() != 0)
126 return true;
127 return false;
128 }
129
130 /// getWithoutImmCodes - Return a copy of this with any immediate codes forced
131 /// to zero.
132 OperandsSignature getWithoutImmCodes() const {
133 OperandsSignature Result;
134 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
135 if (!Operands[i].isImm())
136 Result.Operands.push_back(Operands[i]);
137 else
138 Result.Operands.push_back(OpKind::getImm(0));
139 return Result;
140 }
141
142 void emitImmediatePredicate(raw_ostream &OS, ImmPredicateSet &ImmPredicates) {
143 bool EmittedAnything = false;
144 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
145 if (!Operands[i].isImm()) continue;
146
147 unsigned Code = Operands[i].getImmCode();
148 if (Code == 0) continue;
149
150 if (EmittedAnything)
151 OS << " &&\n ";
152
153 TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1);
154
155 // Emit the type check.
156 OS << "VT == "
157 << getEnumName(PredFn.getOrigPatFragRecord()->getTree(0)->getType(0))
158 << " && ";
159
160
161 OS << PredFn.getFnName() << "(imm" << i <<')';
162 EmittedAnything = true;
163 }
164 }
165
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000166 /// initialize - Examine the given pattern and initialize the contents
167 /// of the Operands array accordingly. Return true if all the operands
168 /// are supported, false otherwise.
169 ///
Chris Lattner602fc062011-04-17 20:23:29 +0000170 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
Chris Lattner1518afd2011-04-18 06:22:33 +0000171 MVT::SimpleValueType VT,
172 ImmPredicateSet &ImmediatePredicates) {
173 if (InstPatNode->isLeaf())
174 return false;
175
176 if (InstPatNode->getOperator()->getName() == "imm") {
177 Operands.push_back(OpKind::getImm(0));
178 return true;
179 }
180
181 if (InstPatNode->getOperator()->getName() == "fpimm") {
182 Operands.push_back(OpKind::getFP());
183 return true;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000184 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000185
Owen Andersonabb1f162008-08-26 01:22:59 +0000186 const CodeGenRegisterClass *DstRC = 0;
Jim Grosbach45258f52010-12-07 19:36:07 +0000187
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000188 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
189 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach45258f52010-12-07 19:36:07 +0000190
Chris Lattner1518afd2011-04-18 06:22:33 +0000191 // Handle imm operands specially.
192 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") {
193 unsigned PredNo = 0;
194 if (!Op->getPredicateFns().empty()) {
Chris Lattner202a7a12011-04-18 06:36:55 +0000195 TreePredicateFn PredFn = Op->getPredicateFns()[0];
Chris Lattner1518afd2011-04-18 06:22:33 +0000196 // If there is more than one predicate weighing in on this operand
197 // then we don't handle it. This doesn't typically happen for
198 // immediates anyway.
199 if (Op->getPredicateFns().size() > 1 ||
Chris Lattner202a7a12011-04-18 06:36:55 +0000200 !PredFn.isImmediatePattern())
201 return false;
202 // Ignore any instruction with 'FastIselShouldIgnore', these are
203 // not needed and just bloat the fast instruction selector. For
204 // example, X86 doesn't need to generate code to match ADD16ri8 since
205 // ADD16ri will do just fine.
206 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord();
207 if (Rec->getValueAsBit("FastIselShouldIgnore"))
Chris Lattner1518afd2011-04-18 06:22:33 +0000208 return false;
209
Chris Lattner202a7a12011-04-18 06:36:55 +0000210 PredNo = ImmediatePredicates.getIDFor(PredFn)+1;
Chris Lattner1518afd2011-04-18 06:22:33 +0000211 }
212
213 // Handle unmatched immediate sizes here.
214 //if (Op->getType(0) != VT)
215 // return false;
216
217 Operands.push_back(OpKind::getImm(PredNo));
218 continue;
219 }
220
221
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000222 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000223 // For now, filter out any operand with multiple values.
Chris Lattner602fc062011-04-17 20:23:29 +0000224 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerd7349192010-03-19 21:37:09 +0000225 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000226
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000227 if (!Op->isLeaf()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000228 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000229 Operands.push_back(OpKind::getFP());
Dale Johannesenedc87742009-05-21 22:25:49 +0000230 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000231 }
Dan Gohman833ddf82008-08-27 16:18:22 +0000232 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000233 return false;
234 }
Chris Lattner602fc062011-04-17 20:23:29 +0000235
236 assert(Op->hasTypeSet(0) && "Type infererence not done?");
237
238 // For now, all the operands must have the same type (if they aren't
239 // immediates). Note that this causes us to reject variable sized shifts
240 // on X86.
241 if (Op->getType(0) != VT)
242 return false;
243
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000244 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
245 if (!OpDI)
246 return false;
247 Record *OpLeafRec = OpDI->getDef();
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000248
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000249 // For now, the only other thing we accept is register operands.
Owen Anderson667d8f72008-08-29 17:45:56 +0000250 const CodeGenRegisterClass *RC = 0;
Owen Andersonbea6f612011-06-27 21:06:21 +0000251 if (OpLeafRec->isSubClassOf("RegisterOperand"))
252 OpLeafRec = OpLeafRec->getValueAsDef("RegClass");
Owen Anderson667d8f72008-08-29 17:45:56 +0000253 if (OpLeafRec->isSubClassOf("RegisterClass"))
254 RC = &Target.getRegisterClass(OpLeafRec);
255 else if (OpLeafRec->isSubClassOf("Register"))
Jakob Stoklund Olesen7b9cafd2011-06-15 00:20:40 +0000256 RC = Target.getRegBank().getRegClassForRegister(OpLeafRec);
Owen Anderson667d8f72008-08-29 17:45:56 +0000257 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000258 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000259
Eric Christopher2cfcad92010-08-24 23:21:59 +0000260 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000261 if (!RC)
262 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000263
Eric Christopher53452602010-08-25 04:58:56 +0000264 // For now, all the operands must have the same register class or be
265 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000266 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000267 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000268 return false;
269 } else
270 DstRC = RC;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000271 Operands.push_back(OpKind::getReg());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000272 }
273 return true;
274 }
275
Daniel Dunbar1a551802009-07-03 00:10:29 +0000276 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000277 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000278 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000279 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000280 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000281 OS << "uint64_t imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000282 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000283 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000284 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000285 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000286 }
287 if (i + 1 != e)
288 OS << ", ";
289 }
290 }
291
Daniel Dunbar1a551802009-07-03 00:10:29 +0000292 void PrintArguments(raw_ostream &OS,
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000293 const std::vector<std::string> &PR) const {
Owen Anderson667d8f72008-08-29 17:45:56 +0000294 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000295 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000296 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000297 if (PR[i] != "")
298 // Implicit physical register operand.
299 continue;
300
301 if (PrintedArg)
302 OS << ", ";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000303 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000304 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000305 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000306 } else if (Operands[i].isImm()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000307 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000308 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000309 } else if (Operands[i].isFP()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000310 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000311 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000312 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000313 llvm_unreachable("Unknown operand kind!");
Owen Anderson667d8f72008-08-29 17:45:56 +0000314 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000315 }
316 }
317
Daniel Dunbar1a551802009-07-03 00:10:29 +0000318 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000319 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000320 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000321 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000322 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000323 OS << "imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000324 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000325 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000326 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000327 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000328 }
329 if (i + 1 != e)
330 OS << ", ";
331 }
332 }
333
Owen Anderson667d8f72008-08-29 17:45:56 +0000334
Chris Lattner1518afd2011-04-18 06:22:33 +0000335 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
336 ImmPredicateSet &ImmPredicates,
337 bool StripImmCodes = false) const {
Evan Cheng98d2d072008-09-08 08:39:33 +0000338 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
339 if (PR[i] != "")
340 // Implicit physical register operand. e.g. Instruction::Mul expect to
341 // select to a binary op. On x86, mul may take a single operand with
342 // the other operand being implicit. We must emit something that looks
343 // like a binary instruction except for the very inner FastEmitInst_*
344 // call.
345 continue;
Chris Lattner1518afd2011-04-18 06:22:33 +0000346 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Evan Cheng98d2d072008-09-08 08:39:33 +0000347 }
348 }
349
Chris Lattner1518afd2011-04-18 06:22:33 +0000350 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
351 bool StripImmCodes = false) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000352 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
Chris Lattner1518afd2011-04-18 06:22:33 +0000353 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000354 }
355};
356
Dan Gohman72d63af2008-08-26 21:21:20 +0000357class FastISelMap {
358 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
360 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000361 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach45258f52010-12-07 19:36:07 +0000362 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopherecfa0792010-07-26 17:53:07 +0000363 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000364
365 OperandsOpcodeTypeRetPredMap SimplePatterns;
366
Chris Lattner1518afd2011-04-18 06:22:33 +0000367 std::map<OperandsSignature, std::vector<OperandsSignature> >
368 SignaturesWithConstantForms;
369
Dan Gohman72d63af2008-08-26 21:21:20 +0000370 std::string InstNS;
Chris Lattner1518afd2011-04-18 06:22:33 +0000371 ImmPredicateSet ImmediatePredicates;
Dan Gohman72d63af2008-08-26 21:21:20 +0000372public:
373 explicit FastISelMap(std::string InstNS);
374
Chris Lattner1518afd2011-04-18 06:22:33 +0000375 void collectPatterns(CodeGenDAGPatterns &CGP);
376 void printImmediatePredicates(raw_ostream &OS);
377 void printFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000378};
379
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000380}
381
382static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
383 return CGP.getSDNodeInfo(Op).getEnumName();
384}
385
386static std::string getLegalCName(std::string OpName) {
387 std::string::size_type pos = OpName.find("::");
388 if (pos != std::string::npos)
389 OpName.replace(pos, 2, "_");
390 return OpName;
391}
392
Dan Gohman72d63af2008-08-26 21:21:20 +0000393FastISelMap::FastISelMap(std::string instns)
394 : InstNS(instns) {
395}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000396
Eli Friedman206a10c2011-04-29 21:58:31 +0000397static std::string PhyRegForNode(TreePatternNode *Op,
398 const CodeGenTarget &Target) {
399 std::string PhysReg;
400
401 if (!Op->isLeaf())
402 return PhysReg;
403
404 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
405 Record *OpLeafRec = OpDI->getDef();
406 if (!OpLeafRec->isSubClassOf("Register"))
407 return PhysReg;
408
409 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
410 "Namespace")->getValue())->getValue();
411 PhysReg += "::";
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000412 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName();
Eli Friedman206a10c2011-04-29 21:58:31 +0000413 return PhysReg;
414}
415
Chris Lattner1518afd2011-04-18 06:22:33 +0000416void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000417 const CodeGenTarget &Target = CGP.getTargetInfo();
418
419 // Determine the target's namespace name.
420 InstNS = Target.getInstNamespace() + "::";
421 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000422
Dan Gohman0bfb7522008-08-22 00:28:15 +0000423 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000424 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
425 E = CGP.ptm_end(); I != E; ++I) {
426 const PatternToMatch &Pattern = *I;
427
428 // For now, just look at Instructions, so that we don't have to worry
429 // about emitting multiple instructions for a pattern.
430 TreePatternNode *Dst = Pattern.getDstPattern();
431 if (Dst->isLeaf()) continue;
432 Record *Op = Dst->getOperator();
433 if (!Op->isSubClassOf("Instruction"))
434 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000435 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnera90dbc12011-04-17 22:24:13 +0000436 if (II.Operands.empty())
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000437 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000438
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000439 // For now, ignore multi-instruction patterns.
440 bool MultiInsts = false;
441 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
442 TreePatternNode *ChildOp = Dst->getChild(i);
443 if (ChildOp->isLeaf())
444 continue;
445 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
446 MultiInsts = true;
447 break;
448 }
449 }
450 if (MultiInsts)
451 continue;
452
Dan Gohman379cad42008-08-19 20:36:33 +0000453 // For now, ignore instructions where the first operand is not an
454 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000455 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000456 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000457 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000458 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonbea6f612011-06-27 21:06:21 +0000459 if (Op0Rec->isSubClassOf("RegisterOperand"))
460 Op0Rec = Op0Rec->getValueAsDef("RegClass");
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000461 if (!Op0Rec->isSubClassOf("RegisterClass"))
462 continue;
463 DstRC = &Target.getRegisterClass(Op0Rec);
464 if (!DstRC)
465 continue;
466 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000467 // If this isn't a leaf, then continue since the register classes are
468 // a bit too complicated for now.
469 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000470
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000471 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
472 if (SR)
473 SubRegNo = getQualifiedName(SR->getDef());
474 else
475 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000476 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000477
478 // Inspect the pattern.
479 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
480 if (!InstPatNode) continue;
481 if (InstPatNode->isLeaf()) continue;
482
Chris Lattner084df622010-03-24 00:41:19 +0000483 // Ignore multiple result nodes for now.
484 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000485
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000486 Record *InstPatOp = InstPatNode->getOperator();
487 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000488 MVT::SimpleValueType RetVT = MVT::isVoid;
489 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000491 if (InstPatNode->getNumChildren()) {
492 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
493 VT = InstPatNode->getChild(0)->getType(0);
494 }
Dan Gohmanf4137b52008-08-19 20:30:54 +0000495
496 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000497 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000498 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000499
Dan Gohman379cad42008-08-19 20:36:33 +0000500 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000501 OperandsSignature Operands;
Chris Lattner1518afd2011-04-18 06:22:33 +0000502 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000503 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000504
Owen Anderson667d8f72008-08-29 17:45:56 +0000505 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
Eli Friedman206a10c2011-04-29 21:58:31 +0000506 if (InstPatNode->getOperator()->getName() == "imm" ||
507 InstPatNode->getOperator()->getName() == "fpimmm")
Owen Anderson667d8f72008-08-29 17:45:56 +0000508 PhysRegInputs->push_back("");
Eli Friedman206a10c2011-04-29 21:58:31 +0000509 else {
510 // Compute the PhysRegs used by the given pattern, and check that
511 // the mapping from the src to dst patterns is simple.
512 bool FoundNonSimplePattern = false;
513 unsigned DstIndex = 0;
Owen Anderson667d8f72008-08-29 17:45:56 +0000514 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
Eli Friedman206a10c2011-04-29 21:58:31 +0000515 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
516 if (PhysReg.empty()) {
517 if (DstIndex >= Dst->getNumChildren() ||
518 Dst->getChild(DstIndex)->getName() !=
519 InstPatNode->getChild(i)->getName()) {
520 FoundNonSimplePattern = true;
521 break;
Owen Anderson667d8f72008-08-29 17:45:56 +0000522 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000523 ++DstIndex;
Owen Anderson667d8f72008-08-29 17:45:56 +0000524 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000525
Owen Anderson667d8f72008-08-29 17:45:56 +0000526 PhysRegInputs->push_back(PhysReg);
527 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000528
529 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
530 FoundNonSimplePattern = true;
531
532 if (FoundNonSimplePattern)
533 continue;
534 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000535
Dan Gohman22bb3112008-08-22 00:20:26 +0000536 // Get the predicate that guards this pattern.
537 std::string PredicateCheck = Pattern.getPredicateCheck();
538
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000539 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000540 InstructionMemo Memo = {
541 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000542 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000543 SubRegNo,
544 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000545 };
Chris Lattner1518afd2011-04-18 06:22:33 +0000546
547 if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck))
548 throw TGError(Pattern.getSrcRecord()->getLoc(),
549 "Duplicate record in FastISel table!");
Jim Grosbach997759a2010-12-07 23:05:49 +0000550
Owen Andersonabb1f162008-08-26 01:22:59 +0000551 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Chris Lattner1518afd2011-04-18 06:22:33 +0000552
553 // If any of the operands were immediates with predicates on them, strip
554 // them down to a signature that doesn't have predicates so that we can
555 // associate them with the stripped predicate version.
556 if (Operands.hasAnyImmediateCodes()) {
557 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
558 .push_back(Operands);
559 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000560 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000561}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000562
Chris Lattner1518afd2011-04-18 06:22:33 +0000563void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
564 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
565 return;
566
567 OS << "\n// FastEmit Immediate Predicate functions.\n";
568 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
569 E = ImmediatePredicates.end(); I != E; ++I) {
570 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
571 OS << I->getImmediatePredicateCode() << "\n}\n";
572 }
573
574 OS << "\n\n";
575}
576
577
578void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000579 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000580 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000581 OE = SimplePatterns.end(); OI != OE; ++OI) {
582 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000583 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000584
Owen Anderson7b2e5792008-08-25 23:43:09 +0000585 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000586 I != E; ++I) {
587 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000588 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000589
590 OS << "// FastEmit functions for " << Opcode << ".\n";
591 OS << "\n";
592
593 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000594 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000595 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000597 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000598 if (RM.size() != 1) {
599 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
600 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000602 const PredMap &PM = RI->second;
603 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000604
Evan Chengc3f44b02008-09-03 00:03:49 +0000605 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000606 << getLegalCName(Opcode)
607 << "_" << getLegalCName(getName(VT))
608 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000609 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000610 OS << "(";
611 Operands.PrintParameters(OS);
612 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000613
Owen Anderson71669e52008-08-26 00:42:26 +0000614 // Emit code for each possible instruction. There may be
615 // multiple if there are subtarget concerns.
616 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
617 PI != PE; ++PI) {
618 std::string PredicateCheck = PI->first;
619 const InstructionMemo &Memo = PI->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000620
Owen Anderson71669e52008-08-26 00:42:26 +0000621 if (PredicateCheck.empty()) {
622 assert(!HasPred &&
623 "Multiple instructions match, at least one has "
624 "a predicate and at least one doesn't!");
625 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000626 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000627 OS << " ";
628 HasPred = true;
629 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000630
Owen Anderson667d8f72008-08-29 17:45:56 +0000631 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
632 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000633 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
634 << "TII.get(TargetOpcode::COPY), "
635 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000636 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000637
Owen Anderson71669e52008-08-26 00:42:26 +0000638 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000639 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000640 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
641 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000642 OS << "(" << InstNS << Memo.Name << ", ";
643 OS << InstNS << Memo.RC->getName() << "RegisterClass";
644 if (!Operands.empty())
645 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000646 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000647 OS << ");\n";
648 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000649 OS << "extractsubreg(" << getName(RetVT);
Chris Lattner1518afd2011-04-18 06:22:33 +0000650 OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000651 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000652
Owen Anderson667d8f72008-08-29 17:45:56 +0000653 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000654 OS << " }\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000655
Owen Anderson71669e52008-08-26 00:42:26 +0000656 }
657 // Return 0 if none of the predicates were satisfied.
658 if (HasPred)
659 OS << " return 0;\n";
660 OS << "}\n";
661 OS << "\n";
662 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000663
Owen Anderson71669e52008-08-26 00:42:26 +0000664 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000665 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000666 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000667 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000668 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000670 if (!Operands.empty())
671 OS << ", ";
672 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000674 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
675 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000677 OS << " case " << getName(RetVT) << ": return FastEmit_"
678 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
679 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000680 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000681 OS << "(";
682 Operands.PrintArguments(OS);
683 OS << ");\n";
684 }
685 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000686
Owen Anderson71669e52008-08-26 00:42:26 +0000687 } else {
688 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000689 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000690 << getLegalCName(Opcode) << "_"
691 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000692 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000693 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000694 if (!Operands.empty())
695 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000696 Operands.PrintParameters(OS);
697 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000698
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000700 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000701
Owen Anderson71669e52008-08-26 00:42:26 +0000702 const PredMap &PM = RM.begin()->second;
703 bool HasPred = false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000704
Owen Anderson7b2e5792008-08-25 23:43:09 +0000705 // Emit code for each possible instruction. There may be
706 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000707 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
708 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000709 std::string PredicateCheck = PI->first;
710 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000711
Owen Anderson7b2e5792008-08-25 23:43:09 +0000712 if (PredicateCheck.empty()) {
713 assert(!HasPred &&
714 "Multiple instructions match, at least one has "
715 "a predicate and at least one doesn't!");
716 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000717 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000718 OS << " ";
719 HasPred = true;
720 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000721
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000722 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
723 if ((*Memo.PhysRegs)[i] != "")
724 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
725 << "TII.get(TargetOpcode::COPY), "
726 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
727 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000728
Owen Anderson7b2e5792008-08-25 23:43:09 +0000729 OS << " return FastEmitInst_";
Jim Grosbach45258f52010-12-07 19:36:07 +0000730
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000731 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000732 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
733 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000734 OS << "(" << InstNS << Memo.Name << ", ";
735 OS << InstNS << Memo.RC->getName() << "RegisterClass";
736 if (!Operands.empty())
737 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000738 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000739 OS << ");\n";
740 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000741 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000742 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000743 OS << ");\n";
744 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000745
Owen Anderson667d8f72008-08-29 17:45:56 +0000746 if (HasPred)
747 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000748 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000749
Owen Anderson7b2e5792008-08-25 23:43:09 +0000750 // Return 0 if none of the predicates were satisfied.
751 if (HasPred)
752 OS << " return 0;\n";
753 OS << "}\n";
754 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000755 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000756 }
757
758 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000759 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000760 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000761 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000763 if (!Operands.empty())
764 OS << ", ";
765 Operands.PrintParameters(OS);
766 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000768 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000769 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000771 std::string TypeName = getName(VT);
772 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000773 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000774 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000775 OS << "(RetVT";
776 if (!Operands.empty())
777 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000778 Operands.PrintArguments(OS);
779 OS << ");\n";
780 }
781 OS << " default: return 0;\n";
782 OS << " }\n";
783 OS << "}\n";
784 OS << "\n";
785 }
786
Dan Gohman0bfb7522008-08-22 00:28:15 +0000787 OS << "// Top-level FastEmit function.\n";
788 OS << "\n";
789
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000790 // Emit one function for the operand signature that demultiplexes based
791 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000792 OS << "unsigned FastEmit_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000793 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000794 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000795 if (!Operands.empty())
796 OS << ", ";
797 Operands.PrintParameters(OS);
798 OS << ") {\n";
Chris Lattner1518afd2011-04-18 06:22:33 +0000799
800 // If there are any forms of this signature available that operand on
801 // constrained forms of the immediate (e.g. 32-bit sext immediate in a
802 // 64-bit operand), check them first.
803
804 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
805 = SignaturesWithConstantForms.find(Operands);
806 if (MI != SignaturesWithConstantForms.end()) {
807 // Unique any duplicates out of the list.
808 std::sort(MI->second.begin(), MI->second.end());
809 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
810 MI->second.end());
811
812 // Check each in order it was seen. It would be nice to have a good
813 // relative ordering between them, but we're not going for optimality
814 // here.
815 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
816 OS << " if (";
817 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
818 OS << ")\n if (unsigned Reg = FastEmit_";
819 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
820 OS << "(VT, RetVT, Opcode";
821 if (!MI->second[i].empty())
822 OS << ", ";
823 MI->second[i].PrintArguments(OS);
824 OS << "))\n return Reg;\n\n";
825 }
826
827 // Done with this, remove it.
828 SignaturesWithConstantForms.erase(MI);
829 }
830
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000831 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000832 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000833 I != E; ++I) {
834 const std::string &Opcode = I->first;
835
836 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000837 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000838 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000839 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000840 if (!Operands.empty())
841 OS << ", ";
842 Operands.PrintArguments(OS);
843 OS << ");\n";
844 }
845 OS << " default: return 0;\n";
846 OS << " }\n";
847 OS << "}\n";
848 OS << "\n";
849 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000850
851 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman72d63af2008-08-26 21:21:20 +0000852}
853
Daniel Dunbar1a551802009-07-03 00:10:29 +0000854void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000855 const CodeGenTarget &Target = CGP.getTargetInfo();
856
857 // Determine the target's namespace name.
858 std::string InstNS = Target.getInstNamespace() + "::";
859 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
860
861 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
862 Target.getName() + " target", OS);
863
Dan Gohman72d63af2008-08-26 21:21:20 +0000864 FastISelMap F(InstNS);
Chris Lattner1518afd2011-04-18 06:22:33 +0000865 F.collectPatterns(CGP);
866 F.printImmediatePredicates(OS);
867 F.printFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000868}
869
870FastISelEmitter::FastISelEmitter(RecordKeeper &R)
Chris Lattner1518afd2011-04-18 06:22:33 +0000871 : Records(R), CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000872}
Dan Gohman72d63af2008-08-26 21:21:20 +0000873