blob: ca784d0dda92e3b5e52e76e30d811061648c4afd [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
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000020#include "CodeGenDAGPatterns.h"
Jim Grosbach76612b52010-12-07 19:35:36 +000021#include "llvm/ADT/SmallString.h"
Chad Rosier36a300a2011-06-07 20:41:31 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000024#include "llvm/TableGen/Error.h"
25#include "llvm/TableGen/Record.h"
26#include "llvm/TableGen/TableGenBackend.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000027using namespace llvm;
28
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000029
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///
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000033namespace {
Owen Anderson667d8f72008-08-29 17:45:56 +000034struct 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};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000040} // End anonymous namespace
41
Chris Lattner1518afd2011-04-18 06:22:33 +000042/// ImmPredicateSet - This uniques predicates (represented as a string) and
43/// gives them unique (small) integer ID's that start at 0.
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000044namespace {
Chris Lattner1518afd2011-04-18 06:22:33 +000045class ImmPredicateSet {
46 DenseMap<TreePattern *, unsigned> ImmIDs;
47 std::vector<TreePredicateFn> PredsByName;
48public:
49
50 unsigned getIDFor(TreePredicateFn Pred) {
51 unsigned &Entry = ImmIDs[Pred.getOrigPatFragRecord()];
52 if (Entry == 0) {
53 PredsByName.push_back(Pred);
54 Entry = PredsByName.size();
55 }
56 return Entry-1;
57 }
58
59 const TreePredicateFn &getPredicate(unsigned i) {
60 assert(i < PredsByName.size());
61 return PredsByName[i];
62 }
63
64 typedef std::vector<TreePredicateFn>::const_iterator iterator;
65 iterator begin() const { return PredsByName.begin(); }
66 iterator end() const { return PredsByName.end(); }
67
68};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000069} // End anonymous namespace
Owen Anderson667d8f72008-08-29 17:45:56 +000070
Dan Gohman04b7dfb2008-08-19 18:06:12 +000071/// OperandsSignature - This class holds a description of a list of operand
72/// types. It has utility methods for emitting text based on the operands.
73///
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000074namespace {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000075struct OperandsSignature {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000076 class OpKind {
77 enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 };
78 char Repr;
79 public:
80
81 OpKind() : Repr(OK_Invalid) {}
82
83 bool operator<(OpKind RHS) const { return Repr < RHS.Repr; }
Chris Lattner1518afd2011-04-18 06:22:33 +000084 bool operator==(OpKind RHS) const { return Repr == RHS.Repr; }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000085
86 static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; }
87 static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; }
Chris Lattner1518afd2011-04-18 06:22:33 +000088 static OpKind getImm(unsigned V) {
89 assert((unsigned)OK_Imm+V < 128 &&
90 "Too many integer predicates for the 'Repr' char");
91 OpKind K; K.Repr = OK_Imm+V; return K;
92 }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000093
94 bool isReg() const { return Repr == OK_Reg; }
95 bool isFP() const { return Repr == OK_FP; }
Chris Lattner1518afd2011-04-18 06:22:33 +000096 bool isImm() const { return Repr >= OK_Imm; }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000097
Chris Lattner1518afd2011-04-18 06:22:33 +000098 unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; }
99
100 void printManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
101 bool StripImmCodes) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000102 if (isReg())
103 OS << 'r';
104 else if (isFP())
105 OS << 'f';
Chris Lattner1518afd2011-04-18 06:22:33 +0000106 else {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000107 OS << 'i';
Chris Lattner1518afd2011-04-18 06:22:33 +0000108 if (!StripImmCodes)
109 if (unsigned Code = getImmCode())
110 OS << "_" << ImmPredicates.getPredicate(Code-1).getFnName();
111 }
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000112 }
113 };
114
Chris Lattner1518afd2011-04-18 06:22:33 +0000115
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000116 SmallVector<OpKind, 3> Operands;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000117
118 bool operator<(const OperandsSignature &O) const {
119 return Operands < O.Operands;
120 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000121 bool operator==(const OperandsSignature &O) const {
122 return Operands == O.Operands;
123 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000124
125 bool empty() const { return Operands.empty(); }
126
Chris Lattner1518afd2011-04-18 06:22:33 +0000127 bool hasAnyImmediateCodes() const {
128 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
129 if (Operands[i].isImm() && Operands[i].getImmCode() != 0)
130 return true;
131 return false;
132 }
133
134 /// getWithoutImmCodes - Return a copy of this with any immediate codes forced
135 /// to zero.
136 OperandsSignature getWithoutImmCodes() const {
137 OperandsSignature Result;
138 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
139 if (!Operands[i].isImm())
140 Result.Operands.push_back(Operands[i]);
141 else
142 Result.Operands.push_back(OpKind::getImm(0));
143 return Result;
144 }
145
146 void emitImmediatePredicate(raw_ostream &OS, ImmPredicateSet &ImmPredicates) {
147 bool EmittedAnything = false;
148 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
149 if (!Operands[i].isImm()) continue;
150
151 unsigned Code = Operands[i].getImmCode();
152 if (Code == 0) continue;
153
154 if (EmittedAnything)
155 OS << " &&\n ";
156
157 TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1);
158
159 // Emit the type check.
160 OS << "VT == "
161 << getEnumName(PredFn.getOrigPatFragRecord()->getTree(0)->getType(0))
162 << " && ";
163
164
165 OS << PredFn.getFnName() << "(imm" << i <<')';
166 EmittedAnything = true;
167 }
168 }
169
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000170 /// initialize - Examine the given pattern and initialize the contents
171 /// of the Operands array accordingly. Return true if all the operands
172 /// are supported, false otherwise.
173 ///
Chris Lattner602fc062011-04-17 20:23:29 +0000174 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
Chris Lattner1518afd2011-04-18 06:22:33 +0000175 MVT::SimpleValueType VT,
176 ImmPredicateSet &ImmediatePredicates) {
177 if (InstPatNode->isLeaf())
178 return false;
179
180 if (InstPatNode->getOperator()->getName() == "imm") {
181 Operands.push_back(OpKind::getImm(0));
182 return true;
183 }
184
185 if (InstPatNode->getOperator()->getName() == "fpimm") {
186 Operands.push_back(OpKind::getFP());
187 return true;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000188 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000189
Owen Andersonabb1f162008-08-26 01:22:59 +0000190 const CodeGenRegisterClass *DstRC = 0;
Jim Grosbach45258f52010-12-07 19:36:07 +0000191
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000192 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
193 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach45258f52010-12-07 19:36:07 +0000194
Chris Lattner1518afd2011-04-18 06:22:33 +0000195 // Handle imm operands specially.
196 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") {
197 unsigned PredNo = 0;
198 if (!Op->getPredicateFns().empty()) {
Chris Lattner202a7a12011-04-18 06:36:55 +0000199 TreePredicateFn PredFn = Op->getPredicateFns()[0];
Chris Lattner1518afd2011-04-18 06:22:33 +0000200 // If there is more than one predicate weighing in on this operand
201 // then we don't handle it. This doesn't typically happen for
202 // immediates anyway.
203 if (Op->getPredicateFns().size() > 1 ||
Chris Lattner202a7a12011-04-18 06:36:55 +0000204 !PredFn.isImmediatePattern())
205 return false;
206 // Ignore any instruction with 'FastIselShouldIgnore', these are
207 // not needed and just bloat the fast instruction selector. For
208 // example, X86 doesn't need to generate code to match ADD16ri8 since
209 // ADD16ri will do just fine.
210 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord();
211 if (Rec->getValueAsBit("FastIselShouldIgnore"))
Chris Lattner1518afd2011-04-18 06:22:33 +0000212 return false;
213
Chris Lattner202a7a12011-04-18 06:36:55 +0000214 PredNo = ImmediatePredicates.getIDFor(PredFn)+1;
Chris Lattner1518afd2011-04-18 06:22:33 +0000215 }
216
217 // Handle unmatched immediate sizes here.
218 //if (Op->getType(0) != VT)
219 // return false;
220
221 Operands.push_back(OpKind::getImm(PredNo));
222 continue;
223 }
224
225
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000226 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000227 // For now, filter out any operand with multiple values.
Chris Lattner602fc062011-04-17 20:23:29 +0000228 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerd7349192010-03-19 21:37:09 +0000229 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000230
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000231 if (!Op->isLeaf()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000232 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000233 Operands.push_back(OpKind::getFP());
Dale Johannesenedc87742009-05-21 22:25:49 +0000234 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000235 }
Dan Gohman833ddf82008-08-27 16:18:22 +0000236 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000237 return false;
238 }
Chris Lattner602fc062011-04-17 20:23:29 +0000239
240 assert(Op->hasTypeSet(0) && "Type infererence not done?");
241
242 // For now, all the operands must have the same type (if they aren't
243 // immediates). Note that this causes us to reject variable sized shifts
244 // on X86.
245 if (Op->getType(0) != VT)
246 return false;
247
David Greene05bce0b2011-07-29 22:43:06 +0000248 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000249 if (!OpDI)
250 return false;
251 Record *OpLeafRec = OpDI->getDef();
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000252
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000253 // For now, the only other thing we accept is register operands.
Owen Anderson667d8f72008-08-29 17:45:56 +0000254 const CodeGenRegisterClass *RC = 0;
Owen Andersonbea6f612011-06-27 21:06:21 +0000255 if (OpLeafRec->isSubClassOf("RegisterOperand"))
256 OpLeafRec = OpLeafRec->getValueAsDef("RegClass");
Owen Anderson667d8f72008-08-29 17:45:56 +0000257 if (OpLeafRec->isSubClassOf("RegisterClass"))
258 RC = &Target.getRegisterClass(OpLeafRec);
259 else if (OpLeafRec->isSubClassOf("Register"))
Jakob Stoklund Olesen7b9cafd2011-06-15 00:20:40 +0000260 RC = Target.getRegBank().getRegClassForRegister(OpLeafRec);
Owen Anderson667d8f72008-08-29 17:45:56 +0000261 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000262 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000263
Eric Christopher2cfcad92010-08-24 23:21:59 +0000264 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000265 if (!RC)
266 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000267
Eric Christopher53452602010-08-25 04:58:56 +0000268 // For now, all the operands must have the same register class or be
269 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000270 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000271 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000272 return false;
273 } else
274 DstRC = RC;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000275 Operands.push_back(OpKind::getReg());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000276 }
277 return true;
278 }
279
Daniel Dunbar1a551802009-07-03 00:10:29 +0000280 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000281 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000282 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000283 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000284 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000285 OS << "uint64_t imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000286 } else if (Operands[i].isFP()) {
Cameron Zwarich82f00022012-01-07 08:18:37 +0000287 OS << "const ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000288 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000289 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000290 }
291 if (i + 1 != e)
292 OS << ", ";
293 }
294 }
295
Daniel Dunbar1a551802009-07-03 00:10:29 +0000296 void PrintArguments(raw_ostream &OS,
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000297 const std::vector<std::string> &PR) const {
Owen Anderson667d8f72008-08-29 17:45:56 +0000298 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000299 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000300 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000301 if (PR[i] != "")
302 // Implicit physical register operand.
303 continue;
304
305 if (PrintedArg)
306 OS << ", ";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000307 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000308 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000309 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000310 } else if (Operands[i].isImm()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000311 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000312 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000313 } else if (Operands[i].isFP()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000314 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000315 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000316 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000317 llvm_unreachable("Unknown operand kind!");
Owen Anderson667d8f72008-08-29 17:45:56 +0000318 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000319 }
320 }
321
Daniel Dunbar1a551802009-07-03 00:10:29 +0000322 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000323 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000324 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000325 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000326 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000327 OS << "imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000328 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000329 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000330 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000331 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000332 }
333 if (i + 1 != e)
334 OS << ", ";
335 }
336 }
337
Owen Anderson667d8f72008-08-29 17:45:56 +0000338
Chris Lattner1518afd2011-04-18 06:22:33 +0000339 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
340 ImmPredicateSet &ImmPredicates,
341 bool StripImmCodes = false) const {
Evan Cheng98d2d072008-09-08 08:39:33 +0000342 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
343 if (PR[i] != "")
344 // Implicit physical register operand. e.g. Instruction::Mul expect to
345 // select to a binary op. On x86, mul may take a single operand with
346 // the other operand being implicit. We must emit something that looks
347 // like a binary instruction except for the very inner FastEmitInst_*
348 // call.
349 continue;
Chris Lattner1518afd2011-04-18 06:22:33 +0000350 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Evan Cheng98d2d072008-09-08 08:39:33 +0000351 }
352 }
353
Chris Lattner1518afd2011-04-18 06:22:33 +0000354 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
355 bool StripImmCodes = false) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000356 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
Chris Lattner1518afd2011-04-18 06:22:33 +0000357 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000358 }
359};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000360} // End anonymous namespace
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000361
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000362namespace {
Dan Gohman72d63af2008-08-26 21:21:20 +0000363class FastISelMap {
364 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000365 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
366 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000367 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach45258f52010-12-07 19:36:07 +0000368 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopherecfa0792010-07-26 17:53:07 +0000369 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000370
371 OperandsOpcodeTypeRetPredMap SimplePatterns;
372
Chris Lattner1518afd2011-04-18 06:22:33 +0000373 std::map<OperandsSignature, std::vector<OperandsSignature> >
374 SignaturesWithConstantForms;
375
Dan Gohman72d63af2008-08-26 21:21:20 +0000376 std::string InstNS;
Chris Lattner1518afd2011-04-18 06:22:33 +0000377 ImmPredicateSet ImmediatePredicates;
Dan Gohman72d63af2008-08-26 21:21:20 +0000378public:
379 explicit FastISelMap(std::string InstNS);
380
Chris Lattner1518afd2011-04-18 06:22:33 +0000381 void collectPatterns(CodeGenDAGPatterns &CGP);
382 void printImmediatePredicates(raw_ostream &OS);
383 void printFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000384};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000385} // End anonymous namespace
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000386
387static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
388 return CGP.getSDNodeInfo(Op).getEnumName();
389}
390
391static std::string getLegalCName(std::string OpName) {
392 std::string::size_type pos = OpName.find("::");
393 if (pos != std::string::npos)
394 OpName.replace(pos, 2, "_");
395 return OpName;
396}
397
Dan Gohman72d63af2008-08-26 21:21:20 +0000398FastISelMap::FastISelMap(std::string instns)
399 : InstNS(instns) {
400}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000401
Eli Friedman206a10c2011-04-29 21:58:31 +0000402static std::string PhyRegForNode(TreePatternNode *Op,
403 const CodeGenTarget &Target) {
404 std::string PhysReg;
405
406 if (!Op->isLeaf())
407 return PhysReg;
408
David Greene05bce0b2011-07-29 22:43:06 +0000409 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
Eli Friedman206a10c2011-04-29 21:58:31 +0000410 Record *OpLeafRec = OpDI->getDef();
411 if (!OpLeafRec->isSubClassOf("Register"))
412 return PhysReg;
413
David Greene05bce0b2011-07-29 22:43:06 +0000414 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
Eli Friedman206a10c2011-04-29 21:58:31 +0000415 "Namespace")->getValue())->getValue();
416 PhysReg += "::";
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000417 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName();
Eli Friedman206a10c2011-04-29 21:58:31 +0000418 return PhysReg;
419}
420
Chris Lattner1518afd2011-04-18 06:22:33 +0000421void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000422 const CodeGenTarget &Target = CGP.getTargetInfo();
423
424 // Determine the target's namespace name.
425 InstNS = Target.getInstNamespace() + "::";
426 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000427
Dan Gohman0bfb7522008-08-22 00:28:15 +0000428 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000429 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
430 E = CGP.ptm_end(); I != E; ++I) {
431 const PatternToMatch &Pattern = *I;
432
433 // For now, just look at Instructions, so that we don't have to worry
434 // about emitting multiple instructions for a pattern.
435 TreePatternNode *Dst = Pattern.getDstPattern();
436 if (Dst->isLeaf()) continue;
437 Record *Op = Dst->getOperator();
438 if (!Op->isSubClassOf("Instruction"))
439 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000440 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnera90dbc12011-04-17 22:24:13 +0000441 if (II.Operands.empty())
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000442 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000443
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000444 // For now, ignore multi-instruction patterns.
445 bool MultiInsts = false;
446 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
447 TreePatternNode *ChildOp = Dst->getChild(i);
448 if (ChildOp->isLeaf())
449 continue;
450 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
451 MultiInsts = true;
452 break;
453 }
454 }
455 if (MultiInsts)
456 continue;
457
Dan Gohman379cad42008-08-19 20:36:33 +0000458 // For now, ignore instructions where the first operand is not an
459 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000460 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000461 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000462 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000463 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonbea6f612011-06-27 21:06:21 +0000464 if (Op0Rec->isSubClassOf("RegisterOperand"))
465 Op0Rec = Op0Rec->getValueAsDef("RegClass");
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000466 if (!Op0Rec->isSubClassOf("RegisterClass"))
467 continue;
468 DstRC = &Target.getRegisterClass(Op0Rec);
469 if (!DstRC)
470 continue;
471 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000472 // If this isn't a leaf, then continue since the register classes are
473 // a bit too complicated for now.
474 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000475
David Greene05bce0b2011-07-29 22:43:06 +0000476 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000477 if (SR)
478 SubRegNo = getQualifiedName(SR->getDef());
479 else
480 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000481 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000482
483 // Inspect the pattern.
484 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
485 if (!InstPatNode) continue;
486 if (InstPatNode->isLeaf()) continue;
487
Chris Lattner084df622010-03-24 00:41:19 +0000488 // Ignore multiple result nodes for now.
489 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000490
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000491 Record *InstPatOp = InstPatNode->getOperator();
492 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000493 MVT::SimpleValueType RetVT = MVT::isVoid;
494 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000496 if (InstPatNode->getNumChildren()) {
497 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
498 VT = InstPatNode->getChild(0)->getType(0);
499 }
Dan Gohmanf4137b52008-08-19 20:30:54 +0000500
501 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000502 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000503 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000504
Dan Gohman379cad42008-08-19 20:36:33 +0000505 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000506 OperandsSignature Operands;
Chris Lattner1518afd2011-04-18 06:22:33 +0000507 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000508 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000509
Owen Anderson667d8f72008-08-29 17:45:56 +0000510 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
Eli Friedman206a10c2011-04-29 21:58:31 +0000511 if (InstPatNode->getOperator()->getName() == "imm" ||
Eric Christopher691a4882011-08-23 15:42:35 +0000512 InstPatNode->getOperator()->getName() == "fpimm")
Owen Anderson667d8f72008-08-29 17:45:56 +0000513 PhysRegInputs->push_back("");
Eli Friedman206a10c2011-04-29 21:58:31 +0000514 else {
515 // Compute the PhysRegs used by the given pattern, and check that
516 // the mapping from the src to dst patterns is simple.
517 bool FoundNonSimplePattern = false;
518 unsigned DstIndex = 0;
Owen Anderson667d8f72008-08-29 17:45:56 +0000519 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
Eli Friedman206a10c2011-04-29 21:58:31 +0000520 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
521 if (PhysReg.empty()) {
522 if (DstIndex >= Dst->getNumChildren() ||
523 Dst->getChild(DstIndex)->getName() !=
524 InstPatNode->getChild(i)->getName()) {
525 FoundNonSimplePattern = true;
526 break;
Owen Anderson667d8f72008-08-29 17:45:56 +0000527 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000528 ++DstIndex;
Owen Anderson667d8f72008-08-29 17:45:56 +0000529 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000530
Owen Anderson667d8f72008-08-29 17:45:56 +0000531 PhysRegInputs->push_back(PhysReg);
532 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000533
534 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
535 FoundNonSimplePattern = true;
536
537 if (FoundNonSimplePattern)
538 continue;
539 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000540
Dan Gohman22bb3112008-08-22 00:20:26 +0000541 // Get the predicate that guards this pattern.
542 std::string PredicateCheck = Pattern.getPredicateCheck();
543
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000544 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000545 InstructionMemo Memo = {
546 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000547 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000548 SubRegNo,
549 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000550 };
Chris Lattner1518afd2011-04-18 06:22:33 +0000551
552 if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck))
553 throw TGError(Pattern.getSrcRecord()->getLoc(),
554 "Duplicate record in FastISel table!");
Jim Grosbach997759a2010-12-07 23:05:49 +0000555
Owen Andersonabb1f162008-08-26 01:22:59 +0000556 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Chris Lattner1518afd2011-04-18 06:22:33 +0000557
558 // If any of the operands were immediates with predicates on them, strip
559 // them down to a signature that doesn't have predicates so that we can
560 // associate them with the stripped predicate version.
561 if (Operands.hasAnyImmediateCodes()) {
562 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
563 .push_back(Operands);
564 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000565 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000566}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000567
Chris Lattner1518afd2011-04-18 06:22:33 +0000568void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
569 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
570 return;
571
572 OS << "\n// FastEmit Immediate Predicate functions.\n";
573 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
574 E = ImmediatePredicates.end(); I != E; ++I) {
575 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
576 OS << I->getImmediatePredicateCode() << "\n}\n";
577 }
578
579 OS << "\n\n";
580}
581
582
583void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000584 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000585 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000586 OE = SimplePatterns.end(); OI != OE; ++OI) {
587 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000588 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000589
Owen Anderson7b2e5792008-08-25 23:43:09 +0000590 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000591 I != E; ++I) {
592 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000593 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000594
595 OS << "// FastEmit functions for " << Opcode << ".\n";
596 OS << "\n";
597
598 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000599 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000600 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000602 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000603 if (RM.size() != 1) {
604 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
605 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000607 const PredMap &PM = RI->second;
608 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000609
Evan Chengc3f44b02008-09-03 00:03:49 +0000610 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000611 << getLegalCName(Opcode)
612 << "_" << getLegalCName(getName(VT))
613 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000614 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000615 OS << "(";
616 Operands.PrintParameters(OS);
617 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000618
Owen Anderson71669e52008-08-26 00:42:26 +0000619 // Emit code for each possible instruction. There may be
620 // multiple if there are subtarget concerns.
621 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
622 PI != PE; ++PI) {
623 std::string PredicateCheck = PI->first;
624 const InstructionMemo &Memo = PI->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000625
Owen Anderson71669e52008-08-26 00:42:26 +0000626 if (PredicateCheck.empty()) {
627 assert(!HasPred &&
628 "Multiple instructions match, at least one has "
629 "a predicate and at least one doesn't!");
630 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000631 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000632 OS << " ";
633 HasPred = true;
634 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000635
Owen Anderson667d8f72008-08-29 17:45:56 +0000636 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
637 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000638 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
639 << "TII.get(TargetOpcode::COPY), "
640 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000641 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000642
Owen Anderson71669e52008-08-26 00:42:26 +0000643 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000644 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000645 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
646 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000647 OS << "(" << InstNS << Memo.Name << ", ";
Craig Topper9b58f292012-04-19 06:52:06 +0000648 OS << "&" << InstNS << Memo.RC->getName() << "RegClass";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000649 if (!Operands.empty())
650 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000651 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000652 OS << ");\n";
653 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000654 OS << "extractsubreg(" << getName(RetVT);
Chris Lattner1518afd2011-04-18 06:22:33 +0000655 OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000656 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000657
Owen Anderson667d8f72008-08-29 17:45:56 +0000658 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000659 OS << " }\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000660
Owen Anderson71669e52008-08-26 00:42:26 +0000661 }
662 // Return 0 if none of the predicates were satisfied.
663 if (HasPred)
664 OS << " return 0;\n";
665 OS << "}\n";
666 OS << "\n";
667 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000668
Owen Anderson71669e52008-08-26 00:42:26 +0000669 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000670 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000671 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000672 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000673 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000675 if (!Operands.empty())
676 OS << ", ";
677 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000679 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
680 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000682 OS << " case " << getName(RetVT) << ": return FastEmit_"
683 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
684 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000685 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000686 OS << "(";
687 Operands.PrintArguments(OS);
688 OS << ");\n";
689 }
690 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000691
Owen Anderson71669e52008-08-26 00:42:26 +0000692 } else {
693 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000694 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000695 << getLegalCName(Opcode) << "_"
696 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000697 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000699 if (!Operands.empty())
700 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000701 Operands.PrintParameters(OS);
702 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000703
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000705 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000706
Owen Anderson71669e52008-08-26 00:42:26 +0000707 const PredMap &PM = RM.begin()->second;
708 bool HasPred = false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000709
Owen Anderson7b2e5792008-08-25 23:43:09 +0000710 // Emit code for each possible instruction. There may be
711 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000712 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
713 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000714 std::string PredicateCheck = PI->first;
715 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000716
Owen Anderson7b2e5792008-08-25 23:43:09 +0000717 if (PredicateCheck.empty()) {
718 assert(!HasPred &&
719 "Multiple instructions match, at least one has "
720 "a predicate and at least one doesn't!");
721 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000722 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000723 OS << " ";
724 HasPred = true;
725 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000726
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000727 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
728 if ((*Memo.PhysRegs)[i] != "")
729 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
730 << "TII.get(TargetOpcode::COPY), "
731 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
732 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000733
Owen Anderson7b2e5792008-08-25 23:43:09 +0000734 OS << " return FastEmitInst_";
Jim Grosbach45258f52010-12-07 19:36:07 +0000735
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000736 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000737 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
738 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000739 OS << "(" << InstNS << Memo.Name << ", ";
Craig Topper9b58f292012-04-19 06:52:06 +0000740 OS << "&" << InstNS << Memo.RC->getName() << "RegClass";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000741 if (!Operands.empty())
742 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000743 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000744 OS << ");\n";
745 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000746 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000747 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000748 OS << ");\n";
749 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000750
Owen Anderson667d8f72008-08-29 17:45:56 +0000751 if (HasPred)
752 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000753 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000754
Owen Anderson7b2e5792008-08-25 23:43:09 +0000755 // Return 0 if none of the predicates were satisfied.
756 if (HasPred)
757 OS << " return 0;\n";
758 OS << "}\n";
759 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000760 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000761 }
762
763 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000764 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000765 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000766 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000768 if (!Operands.empty())
769 OS << ", ";
770 Operands.PrintParameters(OS);
771 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000773 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000774 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000776 std::string TypeName = getName(VT);
777 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000778 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000779 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000780 OS << "(RetVT";
781 if (!Operands.empty())
782 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000783 Operands.PrintArguments(OS);
784 OS << ");\n";
785 }
786 OS << " default: return 0;\n";
787 OS << " }\n";
788 OS << "}\n";
789 OS << "\n";
790 }
791
Dan Gohman0bfb7522008-08-22 00:28:15 +0000792 OS << "// Top-level FastEmit function.\n";
793 OS << "\n";
794
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000795 // Emit one function for the operand signature that demultiplexes based
796 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000797 OS << "unsigned FastEmit_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000798 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000799 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000800 if (!Operands.empty())
801 OS << ", ";
802 Operands.PrintParameters(OS);
803 OS << ") {\n";
Chris Lattner1518afd2011-04-18 06:22:33 +0000804
805 // If there are any forms of this signature available that operand on
806 // constrained forms of the immediate (e.g. 32-bit sext immediate in a
807 // 64-bit operand), check them first.
808
809 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
810 = SignaturesWithConstantForms.find(Operands);
811 if (MI != SignaturesWithConstantForms.end()) {
812 // Unique any duplicates out of the list.
813 std::sort(MI->second.begin(), MI->second.end());
814 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
815 MI->second.end());
816
817 // Check each in order it was seen. It would be nice to have a good
818 // relative ordering between them, but we're not going for optimality
819 // here.
820 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
821 OS << " if (";
822 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
823 OS << ")\n if (unsigned Reg = FastEmit_";
824 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
825 OS << "(VT, RetVT, Opcode";
826 if (!MI->second[i].empty())
827 OS << ", ";
828 MI->second[i].PrintArguments(OS);
829 OS << "))\n return Reg;\n\n";
830 }
831
832 // Done with this, remove it.
833 SignaturesWithConstantForms.erase(MI);
834 }
835
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000836 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000837 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000838 I != E; ++I) {
839 const std::string &Opcode = I->first;
840
841 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000842 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000843 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000844 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000845 if (!Operands.empty())
846 OS << ", ";
847 Operands.PrintArguments(OS);
848 OS << ");\n";
849 }
850 OS << " default: return 0;\n";
851 OS << " }\n";
852 OS << "}\n";
853 OS << "\n";
854 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000855
856 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman72d63af2008-08-26 21:21:20 +0000857}
858
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000859namespace llvm {
860
861void EmitFastISel(RecordKeeper &RK, raw_ostream &OS) {
862 CodeGenDAGPatterns CGP(RK);
Dan Gohman72d63af2008-08-26 21:21:20 +0000863 const CodeGenTarget &Target = CGP.getTargetInfo();
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000864 emitSourceFileHeader("\"Fast\" Instruction Selector for the " +
865 Target.getName() + " target", OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000866
867 // Determine the target's namespace name.
868 std::string InstNS = Target.getInstNamespace() + "::";
869 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
870
Dan Gohman72d63af2008-08-26 21:21:20 +0000871 FastISelMap F(InstNS);
Chris Lattner1518afd2011-04-18 06:22:33 +0000872 F.collectPatterns(CGP);
873 F.printImmediatePredicates(OS);
874 F.printFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000875}
876
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000877} // End llvm namespace