blob: e35cf0f157496c080a44aa57457abddd2b6ef4de [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:
Jim Grosbachdd462302013-08-29 22:41:39 +000049
Chris Lattner1518afd2011-04-18 06:22:33 +000050 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 }
Jim Grosbachdd462302013-08-29 22:41:39 +000058
Chris Lattner1518afd2011-04-18 06:22:33 +000059 const TreePredicateFn &getPredicate(unsigned i) {
60 assert(i < PredsByName.size());
61 return PredsByName[i];
62 }
Jim Grosbachdd462302013-08-29 22:41:39 +000063
Chris Lattner1518afd2011-04-18 06:22:33 +000064 typedef std::vector<TreePredicateFn>::const_iterator iterator;
65 iterator begin() const { return PredsByName.begin(); }
66 iterator end() const { return PredsByName.end(); }
Jim Grosbachdd462302013-08-29 22:41:39 +000067
Chris Lattner1518afd2011-04-18 06:22:33 +000068};
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:
Jim Grosbachdd462302013-08-29 22:41:39 +000080
Chris Lattner9bfd5f32011-04-17 23:29:05 +000081 OpKind() : Repr(OK_Invalid) {}
Jim Grosbachdd462302013-08-29 22:41:39 +000082
Chris Lattner9bfd5f32011-04-17 23:29:05 +000083 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 }
Jim Grosbachdd462302013-08-29 22:41:39 +000093
Chris Lattner9bfd5f32011-04-17 23:29:05 +000094 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; }
Jim Grosbachdd462302013-08-29 22:41:39 +000097
Chris Lattner1518afd2011-04-18 06:22:33 +000098 unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; }
Jim Grosbachdd462302013-08-29 22:41:39 +000099
Chris Lattner1518afd2011-04-18 06:22:33 +0000100 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 };
Jim Grosbachdd462302013-08-29 22:41:39 +0000114
115
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 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000133
Chris Lattner1518afd2011-04-18 06:22:33 +0000134 /// 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 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000145
Chris Lattner1518afd2011-04-18 06:22:33 +0000146 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;
Jim Grosbachdd462302013-08-29 22:41:39 +0000150
Chris Lattner1518afd2011-04-18 06:22:33 +0000151 unsigned Code = Operands[i].getImmCode();
152 if (Code == 0) continue;
Jim Grosbachdd462302013-08-29 22:41:39 +0000153
Chris Lattner1518afd2011-04-18 06:22:33 +0000154 if (EmittedAnything)
155 OS << " &&\n ";
Jim Grosbachdd462302013-08-29 22:41:39 +0000156
Chris Lattner1518afd2011-04-18 06:22:33 +0000157 TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1);
Jim Grosbachdd462302013-08-29 22:41:39 +0000158
Chris Lattner1518afd2011-04-18 06:22:33 +0000159 // Emit the type check.
160 OS << "VT == "
161 << getEnumName(PredFn.getOrigPatFragRecord()->getTree(0)->getType(0))
162 << " && ";
Jim Grosbachdd462302013-08-29 22:41:39 +0000163
164
Chris Lattner1518afd2011-04-18 06:22:33 +0000165 OS << PredFn.getFnName() << "(imm" << i <<')';
166 EmittedAnything = true;
167 }
168 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000169
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,
Bill Schmidtd35da502013-05-22 20:45:11 +0000176 ImmPredicateSet &ImmediatePredicates,
177 const CodeGenRegisterClass *OrigDstRC) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000178 if (InstPatNode->isLeaf())
179 return false;
Jim Grosbachdd462302013-08-29 22:41:39 +0000180
Chris Lattner1518afd2011-04-18 06:22:33 +0000181 if (InstPatNode->getOperator()->getName() == "imm") {
182 Operands.push_back(OpKind::getImm(0));
183 return true;
184 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000185
Chris Lattner1518afd2011-04-18 06:22:33 +0000186 if (InstPatNode->getOperator()->getName() == "fpimm") {
187 Operands.push_back(OpKind::getFP());
188 return true;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000189 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000190
Owen Andersonabb1f162008-08-26 01:22:59 +0000191 const CodeGenRegisterClass *DstRC = 0;
Jim Grosbach45258f52010-12-07 19:36:07 +0000192
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000193 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
194 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach45258f52010-12-07 19:36:07 +0000195
Chris Lattner1518afd2011-04-18 06:22:33 +0000196 // Handle imm operands specially.
197 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") {
198 unsigned PredNo = 0;
199 if (!Op->getPredicateFns().empty()) {
Chris Lattner202a7a12011-04-18 06:36:55 +0000200 TreePredicateFn PredFn = Op->getPredicateFns()[0];
Chris Lattner1518afd2011-04-18 06:22:33 +0000201 // If there is more than one predicate weighing in on this operand
202 // then we don't handle it. This doesn't typically happen for
203 // immediates anyway.
204 if (Op->getPredicateFns().size() > 1 ||
Chris Lattner202a7a12011-04-18 06:36:55 +0000205 !PredFn.isImmediatePattern())
206 return false;
207 // Ignore any instruction with 'FastIselShouldIgnore', these are
208 // not needed and just bloat the fast instruction selector. For
209 // example, X86 doesn't need to generate code to match ADD16ri8 since
210 // ADD16ri will do just fine.
211 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord();
212 if (Rec->getValueAsBit("FastIselShouldIgnore"))
Chris Lattner1518afd2011-04-18 06:22:33 +0000213 return false;
Jim Grosbachdd462302013-08-29 22:41:39 +0000214
Chris Lattner202a7a12011-04-18 06:36:55 +0000215 PredNo = ImmediatePredicates.getIDFor(PredFn)+1;
Chris Lattner1518afd2011-04-18 06:22:33 +0000216 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000217
Chris Lattner1518afd2011-04-18 06:22:33 +0000218 // Handle unmatched immediate sizes here.
219 //if (Op->getType(0) != VT)
220 // return false;
Jim Grosbachdd462302013-08-29 22:41:39 +0000221
Chris Lattner1518afd2011-04-18 06:22:33 +0000222 Operands.push_back(OpKind::getImm(PredNo));
223 continue;
224 }
225
Jim Grosbachdd462302013-08-29 22:41:39 +0000226
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000227 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000228 // For now, filter out any operand with multiple values.
Chris Lattner602fc062011-04-17 20:23:29 +0000229 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerd7349192010-03-19 21:37:09 +0000230 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000231
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000232 if (!Op->isLeaf()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000233 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000234 Operands.push_back(OpKind::getFP());
Dale Johannesenedc87742009-05-21 22:25:49 +0000235 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000236 }
Dan Gohman833ddf82008-08-27 16:18:22 +0000237 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000238 return false;
239 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000240
Chris Lattner602fc062011-04-17 20:23:29 +0000241 assert(Op->hasTypeSet(0) && "Type infererence not done?");
242
243 // For now, all the operands must have the same type (if they aren't
244 // immediates). Note that this causes us to reject variable sized shifts
245 // on X86.
246 if (Op->getType(0) != VT)
247 return false;
248
Sean Silva6cfc8062012-10-10 20:24:43 +0000249 DefInit *OpDI = dyn_cast<DefInit>(Op->getLeafValue());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000250 if (!OpDI)
251 return false;
252 Record *OpLeafRec = OpDI->getDef();
Jim Grosbachdd462302013-08-29 22:41:39 +0000253
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000254 // For now, the only other thing we accept is register operands.
Owen Anderson667d8f72008-08-29 17:45:56 +0000255 const CodeGenRegisterClass *RC = 0;
Owen Andersonbea6f612011-06-27 21:06:21 +0000256 if (OpLeafRec->isSubClassOf("RegisterOperand"))
257 OpLeafRec = OpLeafRec->getValueAsDef("RegClass");
Owen Anderson667d8f72008-08-29 17:45:56 +0000258 if (OpLeafRec->isSubClassOf("RegisterClass"))
259 RC = &Target.getRegisterClass(OpLeafRec);
260 else if (OpLeafRec->isSubClassOf("Register"))
Jakob Stoklund Olesen7b9cafd2011-06-15 00:20:40 +0000261 RC = Target.getRegBank().getRegClassForRegister(OpLeafRec);
Bill Schmidtd35da502013-05-22 20:45:11 +0000262 else if (OpLeafRec->isSubClassOf("ValueType")) {
263 RC = OrigDstRC;
264 } else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000265 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000266
Eric Christopher2cfcad92010-08-24 23:21:59 +0000267 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000268 if (!RC)
269 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000270
Eric Christopher53452602010-08-25 04:58:56 +0000271 // For now, all the operands must have the same register class or be
272 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000273 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000274 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000275 return false;
276 } else
277 DstRC = RC;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000278 Operands.push_back(OpKind::getReg());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000279 }
280 return true;
281 }
282
Daniel Dunbar1a551802009-07-03 00:10:29 +0000283 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000284 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000285 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000286 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000287 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000288 OS << "uint64_t imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000289 } else if (Operands[i].isFP()) {
Cameron Zwarich82f00022012-01-07 08:18:37 +0000290 OS << "const ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000291 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000292 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000293 }
294 if (i + 1 != e)
295 OS << ", ";
296 }
297 }
298
Daniel Dunbar1a551802009-07-03 00:10:29 +0000299 void PrintArguments(raw_ostream &OS,
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000300 const std::vector<std::string> &PR) const {
Owen Anderson667d8f72008-08-29 17:45:56 +0000301 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000302 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000303 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000304 if (PR[i] != "")
305 // Implicit physical register operand.
306 continue;
307
308 if (PrintedArg)
309 OS << ", ";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000310 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000311 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000312 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000313 } else if (Operands[i].isImm()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000314 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000315 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000316 } else if (Operands[i].isFP()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000317 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000318 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000319 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000320 llvm_unreachable("Unknown operand kind!");
Owen Anderson667d8f72008-08-29 17:45:56 +0000321 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000322 }
323 }
324
Daniel Dunbar1a551802009-07-03 00:10:29 +0000325 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000326 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000327 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000328 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000329 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000330 OS << "imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000331 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000332 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000333 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000334 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000335 }
336 if (i + 1 != e)
337 OS << ", ";
338 }
339 }
340
Owen Anderson667d8f72008-08-29 17:45:56 +0000341
Chris Lattner1518afd2011-04-18 06:22:33 +0000342 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
343 ImmPredicateSet &ImmPredicates,
344 bool StripImmCodes = false) const {
Evan Cheng98d2d072008-09-08 08:39:33 +0000345 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
346 if (PR[i] != "")
347 // Implicit physical register operand. e.g. Instruction::Mul expect to
348 // select to a binary op. On x86, mul may take a single operand with
349 // the other operand being implicit. We must emit something that looks
350 // like a binary instruction except for the very inner FastEmitInst_*
351 // call.
352 continue;
Chris Lattner1518afd2011-04-18 06:22:33 +0000353 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Evan Cheng98d2d072008-09-08 08:39:33 +0000354 }
355 }
356
Chris Lattner1518afd2011-04-18 06:22:33 +0000357 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
358 bool StripImmCodes = false) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000359 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
Chris Lattner1518afd2011-04-18 06:22:33 +0000360 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000361 }
362};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000363} // End anonymous namespace
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000364
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000365namespace {
Dan Gohman72d63af2008-08-26 21:21:20 +0000366class FastISelMap {
367 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
369 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000370 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach45258f52010-12-07 19:36:07 +0000371 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopherecfa0792010-07-26 17:53:07 +0000372 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000373
374 OperandsOpcodeTypeRetPredMap SimplePatterns;
375
Chris Lattner1518afd2011-04-18 06:22:33 +0000376 std::map<OperandsSignature, std::vector<OperandsSignature> >
377 SignaturesWithConstantForms;
Jim Grosbachdd462302013-08-29 22:41:39 +0000378
Dan Gohman72d63af2008-08-26 21:21:20 +0000379 std::string InstNS;
Chris Lattner1518afd2011-04-18 06:22:33 +0000380 ImmPredicateSet ImmediatePredicates;
Dan Gohman72d63af2008-08-26 21:21:20 +0000381public:
382 explicit FastISelMap(std::string InstNS);
383
Chris Lattner1518afd2011-04-18 06:22:33 +0000384 void collectPatterns(CodeGenDAGPatterns &CGP);
385 void printImmediatePredicates(raw_ostream &OS);
386 void printFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000387};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000388} // End anonymous namespace
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000389
390static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
391 return CGP.getSDNodeInfo(Op).getEnumName();
392}
393
394static std::string getLegalCName(std::string OpName) {
395 std::string::size_type pos = OpName.find("::");
396 if (pos != std::string::npos)
397 OpName.replace(pos, 2, "_");
398 return OpName;
399}
400
Dan Gohman72d63af2008-08-26 21:21:20 +0000401FastISelMap::FastISelMap(std::string instns)
402 : InstNS(instns) {
403}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000404
Eli Friedman206a10c2011-04-29 21:58:31 +0000405static std::string PhyRegForNode(TreePatternNode *Op,
406 const CodeGenTarget &Target) {
407 std::string PhysReg;
408
409 if (!Op->isLeaf())
410 return PhysReg;
411
Sean Silva3f7b7f82012-10-10 20:24:47 +0000412 Record *OpLeafRec = cast<DefInit>(Op->getLeafValue())->getDef();
Eli Friedman206a10c2011-04-29 21:58:31 +0000413 if (!OpLeafRec->isSubClassOf("Register"))
414 return PhysReg;
415
Sean Silva3f7b7f82012-10-10 20:24:47 +0000416 PhysReg += cast<StringInit>(OpLeafRec->getValue("Namespace")->getValue())
417 ->getValue();
Eli Friedman206a10c2011-04-29 21:58:31 +0000418 PhysReg += "::";
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000419 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName();
Eli Friedman206a10c2011-04-29 21:58:31 +0000420 return PhysReg;
421}
422
Chris Lattner1518afd2011-04-18 06:22:33 +0000423void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000424 const CodeGenTarget &Target = CGP.getTargetInfo();
425
426 // Determine the target's namespace name.
427 InstNS = Target.getInstNamespace() + "::";
428 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000429
Dan Gohman0bfb7522008-08-22 00:28:15 +0000430 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000431 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
432 E = CGP.ptm_end(); I != E; ++I) {
433 const PatternToMatch &Pattern = *I;
434
435 // For now, just look at Instructions, so that we don't have to worry
436 // about emitting multiple instructions for a pattern.
437 TreePatternNode *Dst = Pattern.getDstPattern();
438 if (Dst->isLeaf()) continue;
439 Record *Op = Dst->getOperator();
440 if (!Op->isSubClassOf("Instruction"))
441 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000442 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnera90dbc12011-04-17 22:24:13 +0000443 if (II.Operands.empty())
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000444 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000445
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000446 // For now, ignore multi-instruction patterns.
447 bool MultiInsts = false;
448 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
449 TreePatternNode *ChildOp = Dst->getChild(i);
450 if (ChildOp->isLeaf())
451 continue;
452 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
453 MultiInsts = true;
454 break;
455 }
456 }
457 if (MultiInsts)
458 continue;
459
Dan Gohman379cad42008-08-19 20:36:33 +0000460 // For now, ignore instructions where the first operand is not an
461 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000462 const CodeGenRegisterClass *DstRC = 0;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000463 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000464 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000465 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonbea6f612011-06-27 21:06:21 +0000466 if (Op0Rec->isSubClassOf("RegisterOperand"))
467 Op0Rec = Op0Rec->getValueAsDef("RegClass");
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000468 if (!Op0Rec->isSubClassOf("RegisterClass"))
469 continue;
470 DstRC = &Target.getRegisterClass(Op0Rec);
471 if (!DstRC)
472 continue;
473 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000474 // If this isn't a leaf, then continue since the register classes are
475 // a bit too complicated for now.
476 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000477
Sean Silva6cfc8062012-10-10 20:24:43 +0000478 DefInit *SR = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue());
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000479 if (SR)
480 SubRegNo = getQualifiedName(SR->getDef());
481 else
482 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000483 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000484
485 // Inspect the pattern.
486 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
487 if (!InstPatNode) continue;
488 if (InstPatNode->isLeaf()) continue;
489
Chris Lattner084df622010-03-24 00:41:19 +0000490 // Ignore multiple result nodes for now.
491 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000492
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000493 Record *InstPatOp = InstPatNode->getOperator();
494 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000495 MVT::SimpleValueType RetVT = MVT::isVoid;
496 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000498 if (InstPatNode->getNumChildren()) {
499 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
500 VT = InstPatNode->getChild(0)->getType(0);
501 }
Dan Gohmanf4137b52008-08-19 20:30:54 +0000502
503 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000504 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000505 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000506
Dan Gohman379cad42008-08-19 20:36:33 +0000507 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000508 OperandsSignature Operands;
Bill Schmidtd35da502013-05-22 20:45:11 +0000509 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates,
510 DstRC))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000511 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000512
Owen Anderson667d8f72008-08-29 17:45:56 +0000513 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
Eli Friedman206a10c2011-04-29 21:58:31 +0000514 if (InstPatNode->getOperator()->getName() == "imm" ||
Eric Christopher691a4882011-08-23 15:42:35 +0000515 InstPatNode->getOperator()->getName() == "fpimm")
Owen Anderson667d8f72008-08-29 17:45:56 +0000516 PhysRegInputs->push_back("");
Eli Friedman206a10c2011-04-29 21:58:31 +0000517 else {
518 // Compute the PhysRegs used by the given pattern, and check that
519 // the mapping from the src to dst patterns is simple.
520 bool FoundNonSimplePattern = false;
521 unsigned DstIndex = 0;
Owen Anderson667d8f72008-08-29 17:45:56 +0000522 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
Eli Friedman206a10c2011-04-29 21:58:31 +0000523 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
524 if (PhysReg.empty()) {
525 if (DstIndex >= Dst->getNumChildren() ||
526 Dst->getChild(DstIndex)->getName() !=
527 InstPatNode->getChild(i)->getName()) {
528 FoundNonSimplePattern = true;
529 break;
Owen Anderson667d8f72008-08-29 17:45:56 +0000530 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000531 ++DstIndex;
Owen Anderson667d8f72008-08-29 17:45:56 +0000532 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000533
Owen Anderson667d8f72008-08-29 17:45:56 +0000534 PhysRegInputs->push_back(PhysReg);
535 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000536
537 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
538 FoundNonSimplePattern = true;
539
540 if (FoundNonSimplePattern)
541 continue;
542 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000543
Dan Gohman22bb3112008-08-22 00:20:26 +0000544 // Get the predicate that guards this pattern.
545 std::string PredicateCheck = Pattern.getPredicateCheck();
546
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000547 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000548 InstructionMemo Memo = {
549 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000550 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000551 SubRegNo,
552 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000553 };
Jim Grosbachdd462302013-08-29 22:41:39 +0000554
Chris Lattner1518afd2011-04-18 06:22:33 +0000555 if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000556 PrintFatalError(Pattern.getSrcRecord()->getLoc(),
Chris Lattner1518afd2011-04-18 06:22:33 +0000557 "Duplicate record in FastISel table!");
Jim Grosbach997759a2010-12-07 23:05:49 +0000558
Owen Andersonabb1f162008-08-26 01:22:59 +0000559 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Jim Grosbachdd462302013-08-29 22:41:39 +0000560
Chris Lattner1518afd2011-04-18 06:22:33 +0000561 // If any of the operands were immediates with predicates on them, strip
562 // them down to a signature that doesn't have predicates so that we can
563 // associate them with the stripped predicate version.
564 if (Operands.hasAnyImmediateCodes()) {
565 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
566 .push_back(Operands);
567 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000568 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000569}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000570
Chris Lattner1518afd2011-04-18 06:22:33 +0000571void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
572 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
573 return;
Jim Grosbachdd462302013-08-29 22:41:39 +0000574
Chris Lattner1518afd2011-04-18 06:22:33 +0000575 OS << "\n// FastEmit Immediate Predicate functions.\n";
576 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
577 E = ImmediatePredicates.end(); I != E; ++I) {
578 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
579 OS << I->getImmediatePredicateCode() << "\n}\n";
580 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000581
Chris Lattner1518afd2011-04-18 06:22:33 +0000582 OS << "\n\n";
583}
584
585
586void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000587 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000588 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000589 OE = SimplePatterns.end(); OI != OE; ++OI) {
590 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000591 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000592
Owen Anderson7b2e5792008-08-25 23:43:09 +0000593 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000594 I != E; ++I) {
595 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000596 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000597
598 OS << "// FastEmit functions for " << Opcode << ".\n";
599 OS << "\n";
600
601 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000602 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000603 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000605 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000606 if (RM.size() != 1) {
607 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
608 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000610 const PredMap &PM = RI->second;
611 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000612
Evan Chengc3f44b02008-09-03 00:03:49 +0000613 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000614 << getLegalCName(Opcode)
615 << "_" << getLegalCName(getName(VT))
616 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000617 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000618 OS << "(";
619 Operands.PrintParameters(OS);
620 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000621
Owen Anderson71669e52008-08-26 00:42:26 +0000622 // Emit code for each possible instruction. There may be
623 // multiple if there are subtarget concerns.
624 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
625 PI != PE; ++PI) {
626 std::string PredicateCheck = PI->first;
627 const InstructionMemo &Memo = PI->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000628
Owen Anderson71669e52008-08-26 00:42:26 +0000629 if (PredicateCheck.empty()) {
630 assert(!HasPred &&
631 "Multiple instructions match, at least one has "
632 "a predicate and at least one doesn't!");
633 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000634 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000635 OS << " ";
636 HasPred = true;
637 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000638
Owen Anderson667d8f72008-08-29 17:45:56 +0000639 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
640 if ((*Memo.PhysRegs)[i] != "")
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000641 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
642 << "TII.get(TargetOpcode::COPY), "
643 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000644 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000645
Owen Anderson71669e52008-08-26 00:42:26 +0000646 OS << " return FastEmitInst_";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000647 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000648 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
649 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000650 OS << "(" << InstNS << Memo.Name << ", ";
Craig Topper9b58f292012-04-19 06:52:06 +0000651 OS << "&" << InstNS << Memo.RC->getName() << "RegClass";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000652 if (!Operands.empty())
653 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000654 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000655 OS << ");\n";
656 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000657 OS << "extractsubreg(" << getName(RetVT);
Chris Lattner1518afd2011-04-18 06:22:33 +0000658 OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000659 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000660
Owen Anderson667d8f72008-08-29 17:45:56 +0000661 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000662 OS << " }\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000663
Owen Anderson71669e52008-08-26 00:42:26 +0000664 }
665 // Return 0 if none of the predicates were satisfied.
666 if (HasPred)
667 OS << " return 0;\n";
668 OS << "}\n";
669 OS << "\n";
670 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000671
Owen Anderson71669e52008-08-26 00:42:26 +0000672 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000673 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000674 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000675 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000676 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000678 if (!Operands.empty())
679 OS << ", ";
680 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000682 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
683 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000685 OS << " case " << getName(RetVT) << ": return FastEmit_"
686 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
687 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000688 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000689 OS << "(";
690 Operands.PrintArguments(OS);
691 OS << ");\n";
692 }
693 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000694
Owen Anderson71669e52008-08-26 00:42:26 +0000695 } else {
696 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000697 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000698 << getLegalCName(Opcode) << "_"
699 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000700 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000702 if (!Operands.empty())
703 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000704 Operands.PrintParameters(OS);
705 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000706
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000708 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000709
Owen Anderson71669e52008-08-26 00:42:26 +0000710 const PredMap &PM = RM.begin()->second;
711 bool HasPred = false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000712
Owen Anderson7b2e5792008-08-25 23:43:09 +0000713 // Emit code for each possible instruction. There may be
714 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000715 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
716 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000717 std::string PredicateCheck = PI->first;
718 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000719
Owen Anderson7b2e5792008-08-25 23:43:09 +0000720 if (PredicateCheck.empty()) {
721 assert(!HasPred &&
722 "Multiple instructions match, at least one has "
723 "a predicate and at least one doesn't!");
724 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000725 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000726 OS << " ";
727 HasPred = true;
728 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000729
Jakob Stoklund Olesen4f8e7712010-07-11 03:53:50 +0000730 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
731 if ((*Memo.PhysRegs)[i] != "")
732 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
733 << "TII.get(TargetOpcode::COPY), "
734 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
735 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000736
Owen Anderson7b2e5792008-08-25 23:43:09 +0000737 OS << " return FastEmitInst_";
Jim Grosbach45258f52010-12-07 19:36:07 +0000738
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000739 if (Memo.SubRegNo.empty()) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000740 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
741 ImmediatePredicates, true);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000742 OS << "(" << InstNS << Memo.Name << ", ";
Craig Topper9b58f292012-04-19 06:52:06 +0000743 OS << "&" << InstNS << Memo.RC->getName() << "RegClass";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000744 if (!Operands.empty())
745 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000746 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000747 OS << ");\n";
748 } else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000749 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000750 OS << Memo.SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000751 OS << ");\n";
752 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000753
Owen Anderson667d8f72008-08-29 17:45:56 +0000754 if (HasPred)
755 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000756 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000757
Owen Anderson7b2e5792008-08-25 23:43:09 +0000758 // Return 0 if none of the predicates were satisfied.
759 if (HasPred)
760 OS << " return 0;\n";
761 OS << "}\n";
762 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000763 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000764 }
765
766 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000767 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000768 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000769 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000771 if (!Operands.empty())
772 OS << ", ";
773 Operands.PrintParameters(OS);
774 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000776 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000777 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000779 std::string TypeName = getName(VT);
780 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000781 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000782 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000783 OS << "(RetVT";
784 if (!Operands.empty())
785 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000786 Operands.PrintArguments(OS);
787 OS << ");\n";
788 }
789 OS << " default: return 0;\n";
790 OS << " }\n";
791 OS << "}\n";
792 OS << "\n";
793 }
794
Dan Gohman0bfb7522008-08-22 00:28:15 +0000795 OS << "// Top-level FastEmit function.\n";
796 OS << "\n";
797
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000798 // Emit one function for the operand signature that demultiplexes based
799 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000800 OS << "unsigned FastEmit_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000801 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000802 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000803 if (!Operands.empty())
804 OS << ", ";
805 Operands.PrintParameters(OS);
806 OS << ") {\n";
Jim Grosbachdd462302013-08-29 22:41:39 +0000807
Jim Grosbachff372dc2013-08-29 22:41:43 +0000808 // If there are any forms of this signature available that operate on
809 // constrained forms of the immediate (e.g., 32-bit sext immediate in a
Chris Lattner1518afd2011-04-18 06:22:33 +0000810 // 64-bit operand), check them first.
Jim Grosbachdd462302013-08-29 22:41:39 +0000811
Chris Lattner1518afd2011-04-18 06:22:33 +0000812 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
813 = SignaturesWithConstantForms.find(Operands);
814 if (MI != SignaturesWithConstantForms.end()) {
815 // Unique any duplicates out of the list.
816 std::sort(MI->second.begin(), MI->second.end());
817 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
818 MI->second.end());
Jim Grosbachdd462302013-08-29 22:41:39 +0000819
Chris Lattner1518afd2011-04-18 06:22:33 +0000820 // Check each in order it was seen. It would be nice to have a good
821 // relative ordering between them, but we're not going for optimality
822 // here.
823 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
824 OS << " if (";
825 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
826 OS << ")\n if (unsigned Reg = FastEmit_";
827 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
828 OS << "(VT, RetVT, Opcode";
829 if (!MI->second[i].empty())
830 OS << ", ";
831 MI->second[i].PrintArguments(OS);
832 OS << "))\n return Reg;\n\n";
833 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000834
Chris Lattner1518afd2011-04-18 06:22:33 +0000835 // Done with this, remove it.
836 SignaturesWithConstantForms.erase(MI);
837 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000838
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000839 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000840 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000841 I != E; ++I) {
842 const std::string &Opcode = I->first;
843
844 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000845 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000846 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000847 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000848 if (!Operands.empty())
849 OS << ", ";
850 Operands.PrintArguments(OS);
851 OS << ");\n";
852 }
853 OS << " default: return 0;\n";
854 OS << " }\n";
855 OS << "}\n";
856 OS << "\n";
857 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000858
Chris Lattner1518afd2011-04-18 06:22:33 +0000859 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman72d63af2008-08-26 21:21:20 +0000860}
861
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000862namespace llvm {
863
864void EmitFastISel(RecordKeeper &RK, raw_ostream &OS) {
865 CodeGenDAGPatterns CGP(RK);
Dan Gohman72d63af2008-08-26 21:21:20 +0000866 const CodeGenTarget &Target = CGP.getTargetInfo();
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000867 emitSourceFileHeader("\"Fast\" Instruction Selector for the " +
868 Target.getName() + " target", OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000869
870 // Determine the target's namespace name.
871 std::string InstNS = Target.getInstNamespace() + "::";
872 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
873
Dan Gohman72d63af2008-08-26 21:21:20 +0000874 FastISelMap F(InstNS);
Chris Lattner1518afd2011-04-18 06:22:33 +0000875 F.collectPatterns(CGP);
876 F.printImmediatePredicates(OS);
877 F.printFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000878}
879
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000880} // End llvm namespace