blob: 748c923477838cf2eef0b4c148fe8e483779aecd [file] [log] [blame]
Dan Gohmanb2226e22008-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 Gohmanb4863502008-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 Gohmanb2226e22008-08-13 20:19:35 +000013//
Dan Gohmanb4863502008-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 Gohmanb2226e22008-08-13 20:19:35 +000017//
Dan Gohmanb2226e22008-08-13 20:19:35 +000018//===----------------------------------------------------------------------===//
19
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000020#include "CodeGenDAGPatterns.h"
Jim Grosbache99956e2010-12-07 19:35:36 +000021#include "llvm/ADT/SmallString.h"
Bob Wilson650cd8a2014-10-01 22:44:01 +000022#include "llvm/ADT/StringSwitch.h"
Chad Rosierd0445fd2011-06-07 20:41:31 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000025#include "llvm/TableGen/Error.h"
26#include "llvm/TableGen/Record.h"
27#include "llvm/TableGen/TableGenBackend.h"
Dan Gohmanb2226e22008-08-13 20:19:35 +000028using namespace llvm;
29
Dan Gohmanb2226e22008-08-13 20:19:35 +000030
Owen Anderson0673a8a2008-08-29 17:45:56 +000031/// InstructionMemo - This class holds additional information about an
32/// instruction needed to emit code for it.
33///
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000034namespace {
Owen Anderson0673a8a2008-08-29 17:45:56 +000035struct InstructionMemo {
36 std::string Name;
37 const CodeGenRegisterClass *RC;
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +000038 std::string SubRegNo;
Owen Anderson0673a8a2008-08-29 17:45:56 +000039 std::vector<std::string>* PhysRegs;
Bill Schmidt18767842014-11-14 21:05:45 +000040 std::string PredicateCheck;
Owen Anderson0673a8a2008-08-29 17:45:56 +000041};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000042} // End anonymous namespace
43
Chris Lattner07add492011-04-18 06:22:33 +000044/// ImmPredicateSet - This uniques predicates (represented as a string) and
45/// gives them unique (small) integer ID's that start at 0.
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000046namespace {
Chris Lattner07add492011-04-18 06:22:33 +000047class ImmPredicateSet {
48 DenseMap<TreePattern *, unsigned> ImmIDs;
49 std::vector<TreePredicateFn> PredsByName;
50public:
Jim Grosbachcfb7b912013-08-29 22:41:39 +000051
Chris Lattner07add492011-04-18 06:22:33 +000052 unsigned getIDFor(TreePredicateFn Pred) {
53 unsigned &Entry = ImmIDs[Pred.getOrigPatFragRecord()];
54 if (Entry == 0) {
55 PredsByName.push_back(Pred);
56 Entry = PredsByName.size();
57 }
58 return Entry-1;
59 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +000060
Chris Lattner07add492011-04-18 06:22:33 +000061 const TreePredicateFn &getPredicate(unsigned i) {
62 assert(i < PredsByName.size());
63 return PredsByName[i];
64 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +000065
Chris Lattner07add492011-04-18 06:22:33 +000066 typedef std::vector<TreePredicateFn>::const_iterator iterator;
67 iterator begin() const { return PredsByName.begin(); }
68 iterator end() const { return PredsByName.end(); }
Jim Grosbachcfb7b912013-08-29 22:41:39 +000069
Chris Lattner07add492011-04-18 06:22:33 +000070};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000071} // End anonymous namespace
Owen Anderson0673a8a2008-08-29 17:45:56 +000072
Dan Gohmandbd53282008-08-19 18:06:12 +000073/// OperandsSignature - This class holds a description of a list of operand
74/// types. It has utility methods for emitting text based on the operands.
75///
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000076namespace {
Dan Gohmanb2226e22008-08-13 20:19:35 +000077struct OperandsSignature {
Chris Lattner722f0cc2011-04-17 23:29:05 +000078 class OpKind {
79 enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 };
80 char Repr;
81 public:
Jim Grosbachcfb7b912013-08-29 22:41:39 +000082
Chris Lattner722f0cc2011-04-17 23:29:05 +000083 OpKind() : Repr(OK_Invalid) {}
Jim Grosbachcfb7b912013-08-29 22:41:39 +000084
Chris Lattner722f0cc2011-04-17 23:29:05 +000085 bool operator<(OpKind RHS) const { return Repr < RHS.Repr; }
Chris Lattner07add492011-04-18 06:22:33 +000086 bool operator==(OpKind RHS) const { return Repr == RHS.Repr; }
Chris Lattner722f0cc2011-04-17 23:29:05 +000087
88 static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; }
89 static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; }
Chris Lattner07add492011-04-18 06:22:33 +000090 static OpKind getImm(unsigned V) {
91 assert((unsigned)OK_Imm+V < 128 &&
92 "Too many integer predicates for the 'Repr' char");
93 OpKind K; K.Repr = OK_Imm+V; return K;
94 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +000095
Chris Lattner722f0cc2011-04-17 23:29:05 +000096 bool isReg() const { return Repr == OK_Reg; }
97 bool isFP() const { return Repr == OK_FP; }
Chris Lattner07add492011-04-18 06:22:33 +000098 bool isImm() const { return Repr >= OK_Imm; }
Jim Grosbachcfb7b912013-08-29 22:41:39 +000099
Chris Lattner07add492011-04-18 06:22:33 +0000100 unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000101
Chris Lattner07add492011-04-18 06:22:33 +0000102 void printManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
103 bool StripImmCodes) const {
Chris Lattner722f0cc2011-04-17 23:29:05 +0000104 if (isReg())
105 OS << 'r';
106 else if (isFP())
107 OS << 'f';
Chris Lattner07add492011-04-18 06:22:33 +0000108 else {
Chris Lattner722f0cc2011-04-17 23:29:05 +0000109 OS << 'i';
Chris Lattner07add492011-04-18 06:22:33 +0000110 if (!StripImmCodes)
111 if (unsigned Code = getImmCode())
112 OS << "_" << ImmPredicates.getPredicate(Code-1).getFnName();
113 }
Chris Lattner722f0cc2011-04-17 23:29:05 +0000114 }
115 };
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000116
117
Chris Lattner722f0cc2011-04-17 23:29:05 +0000118 SmallVector<OpKind, 3> Operands;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000119
120 bool operator<(const OperandsSignature &O) const {
121 return Operands < O.Operands;
122 }
Chris Lattner07add492011-04-18 06:22:33 +0000123 bool operator==(const OperandsSignature &O) const {
124 return Operands == O.Operands;
125 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000126
127 bool empty() const { return Operands.empty(); }
128
Chris Lattner07add492011-04-18 06:22:33 +0000129 bool hasAnyImmediateCodes() const {
130 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
131 if (Operands[i].isImm() && Operands[i].getImmCode() != 0)
132 return true;
133 return false;
134 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000135
Chris Lattner07add492011-04-18 06:22:33 +0000136 /// getWithoutImmCodes - Return a copy of this with any immediate codes forced
137 /// to zero.
138 OperandsSignature getWithoutImmCodes() const {
139 OperandsSignature Result;
140 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
141 if (!Operands[i].isImm())
142 Result.Operands.push_back(Operands[i]);
143 else
144 Result.Operands.push_back(OpKind::getImm(0));
145 return Result;
146 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000147
Chris Lattner07add492011-04-18 06:22:33 +0000148 void emitImmediatePredicate(raw_ostream &OS, ImmPredicateSet &ImmPredicates) {
149 bool EmittedAnything = false;
150 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
151 if (!Operands[i].isImm()) continue;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000152
Chris Lattner07add492011-04-18 06:22:33 +0000153 unsigned Code = Operands[i].getImmCode();
154 if (Code == 0) continue;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000155
Chris Lattner07add492011-04-18 06:22:33 +0000156 if (EmittedAnything)
157 OS << " &&\n ";
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000158
Chris Lattner07add492011-04-18 06:22:33 +0000159 TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1);
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000160
Chris Lattner07add492011-04-18 06:22:33 +0000161 // Emit the type check.
162 OS << "VT == "
163 << getEnumName(PredFn.getOrigPatFragRecord()->getTree(0)->getType(0))
164 << " && ";
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000165
166
Chris Lattner07add492011-04-18 06:22:33 +0000167 OS << PredFn.getFnName() << "(imm" << i <<')';
168 EmittedAnything = true;
169 }
170 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000171
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000172 /// initialize - Examine the given pattern and initialize the contents
173 /// of the Operands array accordingly. Return true if all the operands
174 /// are supported, false otherwise.
175 ///
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000176 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
Chris Lattner07add492011-04-18 06:22:33 +0000177 MVT::SimpleValueType VT,
Bill Schmidt9b703f92013-05-22 20:45:11 +0000178 ImmPredicateSet &ImmediatePredicates,
179 const CodeGenRegisterClass *OrigDstRC) {
Chris Lattner07add492011-04-18 06:22:33 +0000180 if (InstPatNode->isLeaf())
181 return false;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000182
Chris Lattner07add492011-04-18 06:22:33 +0000183 if (InstPatNode->getOperator()->getName() == "imm") {
184 Operands.push_back(OpKind::getImm(0));
185 return true;
186 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000187
Chris Lattner07add492011-04-18 06:22:33 +0000188 if (InstPatNode->getOperator()->getName() == "fpimm") {
189 Operands.push_back(OpKind::getFP());
190 return true;
Dan Gohman5ca269e2008-08-27 01:09:54 +0000191 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000192
Craig Topper24064772014-04-15 07:20:03 +0000193 const CodeGenRegisterClass *DstRC = nullptr;
Jim Grosbach8656d822010-12-07 19:36:07 +0000194
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000195 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
196 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach8656d822010-12-07 19:36:07 +0000197
Chris Lattner07add492011-04-18 06:22:33 +0000198 // Handle imm operands specially.
199 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") {
200 unsigned PredNo = 0;
201 if (!Op->getPredicateFns().empty()) {
Chris Lattner80254a52011-04-18 06:36:55 +0000202 TreePredicateFn PredFn = Op->getPredicateFns()[0];
Chris Lattner07add492011-04-18 06:22:33 +0000203 // If there is more than one predicate weighing in on this operand
204 // then we don't handle it. This doesn't typically happen for
205 // immediates anyway.
206 if (Op->getPredicateFns().size() > 1 ||
Chris Lattner80254a52011-04-18 06:36:55 +0000207 !PredFn.isImmediatePattern())
208 return false;
209 // Ignore any instruction with 'FastIselShouldIgnore', these are
210 // not needed and just bloat the fast instruction selector. For
211 // example, X86 doesn't need to generate code to match ADD16ri8 since
212 // ADD16ri will do just fine.
213 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord();
214 if (Rec->getValueAsBit("FastIselShouldIgnore"))
Chris Lattner07add492011-04-18 06:22:33 +0000215 return false;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000216
Chris Lattner80254a52011-04-18 06:36:55 +0000217 PredNo = ImmediatePredicates.getIDFor(PredFn)+1;
Chris Lattner07add492011-04-18 06:22:33 +0000218 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000219
Chris Lattner07add492011-04-18 06:22:33 +0000220 // Handle unmatched immediate sizes here.
221 //if (Op->getType(0) != VT)
222 // return false;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000223
Chris Lattner07add492011-04-18 06:22:33 +0000224 Operands.push_back(OpKind::getImm(PredNo));
225 continue;
226 }
227
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000228
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000229 // For now, filter out any operand with a predicate.
Dan Gohmanfe905652008-08-21 01:41:07 +0000230 // For now, filter out any operand with multiple values.
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000231 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerf1447252010-03-19 21:37:09 +0000232 return false;
Jim Grosbach8656d822010-12-07 19:36:07 +0000233
Dan Gohmanfe905652008-08-21 01:41:07 +0000234 if (!Op->isLeaf()) {
Chris Lattner07add492011-04-18 06:22:33 +0000235 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner722f0cc2011-04-17 23:29:05 +0000236 Operands.push_back(OpKind::getFP());
Dale Johannesen4ff70e382009-05-21 22:25:49 +0000237 continue;
Dan Gohman5ca269e2008-08-27 01:09:54 +0000238 }
Dan Gohman6d153b02008-08-27 16:18:22 +0000239 // For now, ignore other non-leaf nodes.
Dan Gohmanfe905652008-08-21 01:41:07 +0000240 return false;
241 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000242
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000243 assert(Op->hasTypeSet(0) && "Type infererence not done?");
244
245 // For now, all the operands must have the same type (if they aren't
246 // immediates). Note that this causes us to reject variable sized shifts
247 // on X86.
248 if (Op->getType(0) != VT)
249 return false;
250
Sean Silvafb509ed2012-10-10 20:24:43 +0000251 DefInit *OpDI = dyn_cast<DefInit>(Op->getLeafValue());
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000252 if (!OpDI)
253 return false;
254 Record *OpLeafRec = OpDI->getDef();
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000255
Dan Gohmanfe905652008-08-21 01:41:07 +0000256 // For now, the only other thing we accept is register operands.
Craig Topper24064772014-04-15 07:20:03 +0000257 const CodeGenRegisterClass *RC = nullptr;
Owen Andersona84be6c2011-06-27 21:06:21 +0000258 if (OpLeafRec->isSubClassOf("RegisterOperand"))
259 OpLeafRec = OpLeafRec->getValueAsDef("RegClass");
Owen Anderson0673a8a2008-08-29 17:45:56 +0000260 if (OpLeafRec->isSubClassOf("RegisterClass"))
261 RC = &Target.getRegisterClass(OpLeafRec);
262 else if (OpLeafRec->isSubClassOf("Register"))
Jakob Stoklund Olesen22ea4242011-06-15 00:20:40 +0000263 RC = Target.getRegBank().getRegClassForRegister(OpLeafRec);
Bill Schmidt9b703f92013-05-22 20:45:11 +0000264 else if (OpLeafRec->isSubClassOf("ValueType")) {
265 RC = OrigDstRC;
266 } else
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000267 return false;
Jim Grosbach8656d822010-12-07 19:36:07 +0000268
Eric Christopher98f0ea62010-08-24 23:21:59 +0000269 // For now, this needs to be a register class of some sort.
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000270 if (!RC)
271 return false;
Eric Christopher98f0ea62010-08-24 23:21:59 +0000272
Eric Christopher6490bf62010-08-25 04:58:56 +0000273 // For now, all the operands must have the same register class or be
274 // a strict subclass of the destination.
Owen Anderson6f2db722008-08-26 01:22:59 +0000275 if (DstRC) {
Eric Christopher6490bf62010-08-25 04:58:56 +0000276 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Anderson6f2db722008-08-26 01:22:59 +0000277 return false;
278 } else
279 DstRC = RC;
Chris Lattner722f0cc2011-04-17 23:29:05 +0000280 Operands.push_back(OpKind::getReg());
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000281 }
282 return true;
283 }
284
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000285 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000286 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner722f0cc2011-04-17 23:29:05 +0000287 if (Operands[i].isReg()) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000288 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner722f0cc2011-04-17 23:29:05 +0000289 } else if (Operands[i].isImm()) {
Dan Gohmanfe905652008-08-21 01:41:07 +0000290 OS << "uint64_t imm" << i;
Chris Lattner722f0cc2011-04-17 23:29:05 +0000291 } else if (Operands[i].isFP()) {
Cameron Zwariche47e6822012-01-07 08:18:37 +0000292 OS << "const ConstantFP *f" << i;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000293 } else {
Chad Rosierd0445fd2011-06-07 20:41:31 +0000294 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb2226e22008-08-13 20:19:35 +0000295 }
296 if (i + 1 != e)
297 OS << ", ";
298 }
299 }
300
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000301 void PrintArguments(raw_ostream &OS,
Chris Lattner722f0cc2011-04-17 23:29:05 +0000302 const std::vector<std::string> &PR) const {
Owen Anderson0673a8a2008-08-29 17:45:56 +0000303 assert(PR.size() == Operands.size());
Evan Chengca14c072008-09-08 08:39:33 +0000304 bool PrintedArg = false;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000305 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Chengca14c072008-09-08 08:39:33 +0000306 if (PR[i] != "")
307 // Implicit physical register operand.
308 continue;
309
310 if (PrintedArg)
311 OS << ", ";
Chris Lattner722f0cc2011-04-17 23:29:05 +0000312 if (Operands[i].isReg()) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000313 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Chengca14c072008-09-08 08:39:33 +0000314 PrintedArg = true;
Chris Lattner722f0cc2011-04-17 23:29:05 +0000315 } else if (Operands[i].isImm()) {
Owen Anderson0673a8a2008-08-29 17:45:56 +0000316 OS << "imm" << i;
Evan Chengca14c072008-09-08 08:39:33 +0000317 PrintedArg = true;
Chris Lattner722f0cc2011-04-17 23:29:05 +0000318 } else if (Operands[i].isFP()) {
Owen Anderson0673a8a2008-08-29 17:45:56 +0000319 OS << "f" << i;
Evan Chengca14c072008-09-08 08:39:33 +0000320 PrintedArg = true;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000321 } else {
Chad Rosierd0445fd2011-06-07 20:41:31 +0000322 llvm_unreachable("Unknown operand kind!");
Owen Anderson0673a8a2008-08-29 17:45:56 +0000323 }
Owen Anderson0673a8a2008-08-29 17:45:56 +0000324 }
325 }
326
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000327 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000328 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner722f0cc2011-04-17 23:29:05 +0000329 if (Operands[i].isReg()) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000330 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner722f0cc2011-04-17 23:29:05 +0000331 } else if (Operands[i].isImm()) {
Dan Gohmanfe905652008-08-21 01:41:07 +0000332 OS << "imm" << i;
Chris Lattner722f0cc2011-04-17 23:29:05 +0000333 } else if (Operands[i].isFP()) {
Dan Gohman5ca269e2008-08-27 01:09:54 +0000334 OS << "f" << i;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000335 } else {
Chad Rosierd0445fd2011-06-07 20:41:31 +0000336 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb2226e22008-08-13 20:19:35 +0000337 }
338 if (i + 1 != e)
339 OS << ", ";
340 }
341 }
342
Owen Anderson0673a8a2008-08-29 17:45:56 +0000343
Chris Lattner07add492011-04-18 06:22:33 +0000344 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
345 ImmPredicateSet &ImmPredicates,
346 bool StripImmCodes = false) const {
Evan Chengca14c072008-09-08 08:39:33 +0000347 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
348 if (PR[i] != "")
349 // Implicit physical register operand. e.g. Instruction::Mul expect to
350 // select to a binary op. On x86, mul may take a single operand with
351 // the other operand being implicit. We must emit something that looks
Juergen Ributzka88e32512014-09-03 20:56:59 +0000352 // like a binary instruction except for the very inner fastEmitInst_*
Evan Chengca14c072008-09-08 08:39:33 +0000353 // call.
354 continue;
Chris Lattner07add492011-04-18 06:22:33 +0000355 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Evan Chengca14c072008-09-08 08:39:33 +0000356 }
357 }
358
Chris Lattner07add492011-04-18 06:22:33 +0000359 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
360 bool StripImmCodes = false) const {
Chris Lattner722f0cc2011-04-17 23:29:05 +0000361 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
Chris Lattner07add492011-04-18 06:22:33 +0000362 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Dan Gohmanb2226e22008-08-13 20:19:35 +0000363 }
364};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000365} // End anonymous namespace
Dan Gohmanb2226e22008-08-13 20:19:35 +0000366
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000367namespace {
Dan Gohman44003cc2008-08-26 21:21:20 +0000368class FastISelMap {
Bill Schmidt18767842014-11-14 21:05:45 +0000369 // A multimap is needed instead of a "plain" map because the key is
370 // the instruction's complexity (an int) and they are not unique.
371 typedef std::multimap<int, InstructionMemo> PredMap;
Owen Anderson9f944592009-08-11 20:47:22 +0000372 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
373 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman44003cc2008-08-26 21:21:20 +0000374 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach8656d822010-12-07 19:36:07 +0000375 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopher62ac5ca2010-07-26 17:53:07 +0000376 OperandsOpcodeTypeRetPredMap;
Dan Gohman44003cc2008-08-26 21:21:20 +0000377
378 OperandsOpcodeTypeRetPredMap SimplePatterns;
379
Bill Schmidt18767842014-11-14 21:05:45 +0000380 // This is used to check that there are no duplicate predicates
381 typedef std::multimap<std::string, bool> PredCheckMap;
382 typedef std::map<MVT::SimpleValueType, PredCheckMap> RetPredCheckMap;
383 typedef std::map<MVT::SimpleValueType, RetPredCheckMap> TypeRetPredCheckMap;
384 typedef std::map<std::string, TypeRetPredCheckMap> OpcodeTypeRetPredCheckMap;
385 typedef std::map<OperandsSignature, OpcodeTypeRetPredCheckMap>
386 OperandsOpcodeTypeRetPredCheckMap;
387
388 OperandsOpcodeTypeRetPredCheckMap SimplePatternsCheck;
389
Chris Lattner07add492011-04-18 06:22:33 +0000390 std::map<OperandsSignature, std::vector<OperandsSignature> >
391 SignaturesWithConstantForms;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000392
Dan Gohman44003cc2008-08-26 21:21:20 +0000393 std::string InstNS;
Chris Lattner07add492011-04-18 06:22:33 +0000394 ImmPredicateSet ImmediatePredicates;
Dan Gohman44003cc2008-08-26 21:21:20 +0000395public:
396 explicit FastISelMap(std::string InstNS);
397
Chris Lattner07add492011-04-18 06:22:33 +0000398 void collectPatterns(CodeGenDAGPatterns &CGP);
399 void printImmediatePredicates(raw_ostream &OS);
400 void printFunctionDefinitions(raw_ostream &OS);
Bill Schmidt18767842014-11-14 21:05:45 +0000401private:
402 void emitInstructionCode(raw_ostream &OS,
403 const OperandsSignature &Operands,
404 const PredMap &PM,
405 const std::string &RetVTName);
Dan Gohman44003cc2008-08-26 21:21:20 +0000406};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000407} // End anonymous namespace
Dan Gohmanb2226e22008-08-13 20:19:35 +0000408
409static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
410 return CGP.getSDNodeInfo(Op).getEnumName();
411}
412
413static std::string getLegalCName(std::string OpName) {
414 std::string::size_type pos = OpName.find("::");
415 if (pos != std::string::npos)
416 OpName.replace(pos, 2, "_");
417 return OpName;
418}
419
Dan Gohman44003cc2008-08-26 21:21:20 +0000420FastISelMap::FastISelMap(std::string instns)
421 : InstNS(instns) {
422}
Dan Gohmanb2226e22008-08-13 20:19:35 +0000423
Eli Friedman3bc13b42011-04-29 21:58:31 +0000424static std::string PhyRegForNode(TreePatternNode *Op,
425 const CodeGenTarget &Target) {
426 std::string PhysReg;
427
428 if (!Op->isLeaf())
429 return PhysReg;
430
Sean Silva88eb8dd2012-10-10 20:24:47 +0000431 Record *OpLeafRec = cast<DefInit>(Op->getLeafValue())->getDef();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000432 if (!OpLeafRec->isSubClassOf("Register"))
433 return PhysReg;
434
Sean Silva88eb8dd2012-10-10 20:24:47 +0000435 PhysReg += cast<StringInit>(OpLeafRec->getValue("Namespace")->getValue())
436 ->getValue();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000437 PhysReg += "::";
Jakob Stoklund Olesen8e188be2011-06-18 04:26:06 +0000438 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000439 return PhysReg;
440}
441
Chris Lattner07add492011-04-18 06:22:33 +0000442void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman44003cc2008-08-26 21:21:20 +0000443 const CodeGenTarget &Target = CGP.getTargetInfo();
444
445 // Determine the target's namespace name.
446 InstNS = Target.getInstNamespace() + "::";
447 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb2226e22008-08-13 20:19:35 +0000448
Dan Gohman9b29ec72008-08-22 00:28:15 +0000449 // Scan through all the patterns and record the simple ones.
Dan Gohmanb2226e22008-08-13 20:19:35 +0000450 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
451 E = CGP.ptm_end(); I != E; ++I) {
452 const PatternToMatch &Pattern = *I;
453
454 // For now, just look at Instructions, so that we don't have to worry
455 // about emitting multiple instructions for a pattern.
456 TreePatternNode *Dst = Pattern.getDstPattern();
457 if (Dst->isLeaf()) continue;
458 Record *Op = Dst->getOperator();
459 if (!Op->isSubClassOf("Instruction"))
460 continue;
Chris Lattner9aec14b2010-03-19 00:07:20 +0000461 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattner90803912011-04-17 22:24:13 +0000462 if (II.Operands.empty())
Dan Gohmanb2226e22008-08-13 20:19:35 +0000463 continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000464
Evan Cheng7cab17a2008-09-07 08:19:51 +0000465 // For now, ignore multi-instruction patterns.
466 bool MultiInsts = false;
467 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
468 TreePatternNode *ChildOp = Dst->getChild(i);
469 if (ChildOp->isLeaf())
470 continue;
471 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
472 MultiInsts = true;
473 break;
474 }
475 }
476 if (MultiInsts)
477 continue;
478
Dan Gohmanaa13b6f2008-08-19 20:36:33 +0000479 // For now, ignore instructions where the first operand is not an
480 // output register.
Craig Topper24064772014-04-15 07:20:03 +0000481 const CodeGenRegisterClass *DstRC = nullptr;
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000482 std::string SubRegNo;
Owen Anderson787f1002008-08-28 18:06:12 +0000483 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000484 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersona84be6c2011-06-27 21:06:21 +0000485 if (Op0Rec->isSubClassOf("RegisterOperand"))
486 Op0Rec = Op0Rec->getValueAsDef("RegClass");
Owen Anderson787f1002008-08-28 18:06:12 +0000487 if (!Op0Rec->isSubClassOf("RegisterClass"))
488 continue;
489 DstRC = &Target.getRegisterClass(Op0Rec);
490 if (!DstRC)
491 continue;
492 } else {
Eric Christopherbebb8c52010-07-21 22:07:19 +0000493 // If this isn't a leaf, then continue since the register classes are
494 // a bit too complicated for now.
495 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000496
Sean Silvafb509ed2012-10-10 20:24:43 +0000497 DefInit *SR = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue());
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000498 if (SR)
499 SubRegNo = getQualifiedName(SR->getDef());
500 else
501 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Anderson787f1002008-08-28 18:06:12 +0000502 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000503
504 // Inspect the pattern.
505 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
506 if (!InstPatNode) continue;
507 if (InstPatNode->isLeaf()) continue;
508
Chris Lattner6c2d1782010-03-24 00:41:19 +0000509 // Ignore multiple result nodes for now.
510 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000511
Dan Gohmanb2226e22008-08-13 20:19:35 +0000512 Record *InstPatOp = InstPatNode->getOperator();
513 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000514 MVT::SimpleValueType RetVT = MVT::isVoid;
515 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson9f944592009-08-11 20:47:22 +0000516 MVT::SimpleValueType VT = RetVT;
Chris Lattnerf1447252010-03-19 21:37:09 +0000517 if (InstPatNode->getNumChildren()) {
518 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
519 VT = InstPatNode->getChild(0)->getType(0);
520 }
Dan Gohmana6c14d02008-08-19 20:30:54 +0000521
522 // For now, filter out any instructions with predicates.
Dan Gohman6e979022008-10-15 06:17:21 +0000523 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmana6c14d02008-08-19 20:30:54 +0000524 continue;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000525
Dan Gohmanaa13b6f2008-08-19 20:36:33 +0000526 // Check all the operands.
Dan Gohmanb2226e22008-08-13 20:19:35 +0000527 OperandsSignature Operands;
Bill Schmidt9b703f92013-05-22 20:45:11 +0000528 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates,
529 DstRC))
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000530 continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000531
Owen Anderson0673a8a2008-08-29 17:45:56 +0000532 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000533 if (InstPatNode->getOperator()->getName() == "imm" ||
Eric Christopherc50ea3b2011-08-23 15:42:35 +0000534 InstPatNode->getOperator()->getName() == "fpimm")
Owen Anderson0673a8a2008-08-29 17:45:56 +0000535 PhysRegInputs->push_back("");
Eli Friedman3bc13b42011-04-29 21:58:31 +0000536 else {
537 // Compute the PhysRegs used by the given pattern, and check that
538 // the mapping from the src to dst patterns is simple.
539 bool FoundNonSimplePattern = false;
540 unsigned DstIndex = 0;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000541 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
Eli Friedman3bc13b42011-04-29 21:58:31 +0000542 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
543 if (PhysReg.empty()) {
544 if (DstIndex >= Dst->getNumChildren() ||
545 Dst->getChild(DstIndex)->getName() !=
546 InstPatNode->getChild(i)->getName()) {
547 FoundNonSimplePattern = true;
548 break;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000549 }
Eli Friedman3bc13b42011-04-29 21:58:31 +0000550 ++DstIndex;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000551 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000552
Owen Anderson0673a8a2008-08-29 17:45:56 +0000553 PhysRegInputs->push_back(PhysReg);
554 }
Eli Friedman3bc13b42011-04-29 21:58:31 +0000555
556 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
557 FoundNonSimplePattern = true;
558
559 if (FoundNonSimplePattern)
560 continue;
561 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000562
Bob Wilson650cd8a2014-10-01 22:44:01 +0000563 // Check if the operands match one of the patterns handled by FastISel.
564 std::string ManglingSuffix;
565 raw_string_ostream SuffixOS(ManglingSuffix);
566 Operands.PrintManglingSuffix(SuffixOS, ImmediatePredicates, true);
567 SuffixOS.flush();
568 if (!StringSwitch<bool>(ManglingSuffix)
569 .Cases("", "r", "rr", "ri", "rf", true)
570 .Cases("rri", "i", "f", true)
571 .Default(false))
572 continue;
573
Dan Gohman49e19e92008-08-22 00:20:26 +0000574 // Get the predicate that guards this pattern.
575 std::string PredicateCheck = Pattern.getPredicateCheck();
576
Dan Gohmanb2226e22008-08-13 20:19:35 +0000577 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman7b3932e2008-08-21 00:35:26 +0000578 InstructionMemo Memo = {
579 Pattern.getDstPattern()->getOperator()->getName(),
Owen Anderson787f1002008-08-28 18:06:12 +0000580 DstRC,
Owen Anderson0673a8a2008-08-29 17:45:56 +0000581 SubRegNo,
Bill Schmidt18767842014-11-14 21:05:45 +0000582 PhysRegInputs,
583 PredicateCheck
Dan Gohman7b3932e2008-08-21 00:35:26 +0000584 };
Bill Schmidt18767842014-11-14 21:05:45 +0000585
586 int complexity = Pattern.getPatternComplexity(CGP);
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000587
Bill Schmidt18767842014-11-14 21:05:45 +0000588 if (SimplePatternsCheck[Operands][OpcodeName][VT]
589 [RetVT].count(PredicateCheck)) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000590 PrintFatalError(Pattern.getSrcRecord()->getLoc(),
Bill Schmidt18767842014-11-14 21:05:45 +0000591 "Duplicate predicate in FastISel table!");
592 }
593 SimplePatternsCheck[Operands][OpcodeName][VT][RetVT].insert(
594 std::make_pair(PredicateCheck, true));
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000595
Bill Schmidt18767842014-11-14 21:05:45 +0000596 // Note: Instructions with the same complexity will appear in the order
597 // that they are encountered.
598 SimplePatterns[Operands][OpcodeName][VT][RetVT].insert(
599 std::make_pair(complexity, Memo));
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000600
Chris Lattner07add492011-04-18 06:22:33 +0000601 // If any of the operands were immediates with predicates on them, strip
602 // them down to a signature that doesn't have predicates so that we can
603 // associate them with the stripped predicate version.
604 if (Operands.hasAnyImmediateCodes()) {
605 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
606 .push_back(Operands);
607 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000608 }
Dan Gohman44003cc2008-08-26 21:21:20 +0000609}
Dan Gohmanb2226e22008-08-13 20:19:35 +0000610
Chris Lattner07add492011-04-18 06:22:33 +0000611void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
612 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
613 return;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000614
Chris Lattner07add492011-04-18 06:22:33 +0000615 OS << "\n// FastEmit Immediate Predicate functions.\n";
616 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
617 E = ImmediatePredicates.end(); I != E; ++I) {
618 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
619 OS << I->getImmediatePredicateCode() << "\n}\n";
620 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000621
Chris Lattner07add492011-04-18 06:22:33 +0000622 OS << "\n\n";
623}
624
Bill Schmidt18767842014-11-14 21:05:45 +0000625void FastISelMap::emitInstructionCode(raw_ostream &OS,
626 const OperandsSignature &Operands,
627 const PredMap &PM,
628 const std::string &RetVTName) {
629 // Emit code for each possible instruction. There may be
630 // multiple if there are subtarget concerns. A reverse iterator
631 // is used to produce the ones with highest complexity first.
632
633 bool OneHadNoPredicate = false;
634 for (PredMap::const_reverse_iterator PI = PM.rbegin(), PE = PM.rend();
635 PI != PE; ++PI) {
636 const InstructionMemo &Memo = PI->second;
637 std::string PredicateCheck = Memo.PredicateCheck;
638
639 if (PredicateCheck.empty()) {
640 assert(!OneHadNoPredicate &&
641 "Multiple instructions match and more than one had "
642 "no predicate!");
643 OneHadNoPredicate = true;
644 } else {
645 if (OneHadNoPredicate) {
646 // FIXME: This should be a PrintError once the x86 target
647 // fixes PR21575.
648 PrintWarning("Multiple instructions match and one with no "
649 "predicate came before one with a predicate! "
650 "name:" + Memo.Name + " predicate: " +
651 PredicateCheck);
652 }
653 OS << " if (" + PredicateCheck + ") {\n";
654 OS << " ";
655 }
656
657 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
658 if ((*Memo.PhysRegs)[i] != "")
659 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, "
660 << "TII.get(TargetOpcode::COPY), "
661 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
662 }
663
664 OS << " return fastEmitInst_";
665 if (Memo.SubRegNo.empty()) {
666 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
667 ImmediatePredicates, true);
668 OS << "(" << InstNS << Memo.Name << ", ";
669 OS << "&" << InstNS << Memo.RC->getName() << "RegClass";
670 if (!Operands.empty())
671 OS << ", ";
672 Operands.PrintArguments(OS, *Memo.PhysRegs);
673 OS << ");\n";
674 } else {
675 OS << "extractsubreg(" << RetVTName
676 << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
677 }
678
679 if (!PredicateCheck.empty()) {
680 OS << " }\n";
681 }
682 }
683 // Return 0 if all of the possibilities had predicates but none
684 // were satisfied.
685 if (!OneHadNoPredicate)
686 OS << " return 0;\n";
687 OS << "}\n";
688 OS << "\n";
689}
690
Chris Lattner07add492011-04-18 06:22:33 +0000691
692void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000693 // Now emit code for all the patterns that we collected.
Owen Anderson5952cca2008-08-25 23:43:09 +0000694 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb2226e22008-08-13 20:19:35 +0000695 OE = SimplePatterns.end(); OI != OE; ++OI) {
696 const OperandsSignature &Operands = OI->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000697 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000698
Owen Anderson5952cca2008-08-25 23:43:09 +0000699 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000700 I != E; ++I) {
701 const std::string &Opcode = I->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000702 const TypeRetPredMap &TM = I->second;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000703
704 OS << "// FastEmit functions for " << Opcode << ".\n";
705 OS << "\n";
706
707 // Emit one function for each opcode,type pair.
Owen Anderson5952cca2008-08-25 23:43:09 +0000708 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000709 TI != TE; ++TI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000710 MVT::SimpleValueType VT = TI->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000711 const RetPredMap &RM = TI->second;
Owen Anderson5f334d82008-08-26 00:42:26 +0000712 if (RM.size() != 1) {
713 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
714 RI != RE; ++RI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000715 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson5f334d82008-08-26 00:42:26 +0000716 const PredMap &PM = RI->second;
Dan Gohman49e19e92008-08-22 00:20:26 +0000717
Juergen Ributzka88e32512014-09-03 20:56:59 +0000718 OS << "unsigned fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000719 << getLegalCName(Opcode)
720 << "_" << getLegalCName(getName(VT))
721 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000722 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson5f334d82008-08-26 00:42:26 +0000723 OS << "(";
724 Operands.PrintParameters(OS);
725 OS << ") {\n";
Dan Gohman49e19e92008-08-22 00:20:26 +0000726
Bill Schmidt18767842014-11-14 21:05:45 +0000727 emitInstructionCode(OS, Operands, PM, getName(RetVT));
Owen Anderson5f334d82008-08-26 00:42:26 +0000728 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000729
Owen Anderson5f334d82008-08-26 00:42:26 +0000730 // Emit one function for the type that demultiplexes on return type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000731 OS << "unsigned fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000732 << getLegalCName(Opcode) << "_"
Owen Anderson6f2db722008-08-26 01:22:59 +0000733 << getLegalCName(getName(VT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000734 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson9f944592009-08-11 20:47:22 +0000735 OS << "(MVT RetVT";
Owen Anderson5f334d82008-08-26 00:42:26 +0000736 if (!Operands.empty())
737 OS << ", ";
738 Operands.PrintParameters(OS);
Owen Anderson9f944592009-08-11 20:47:22 +0000739 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson5f334d82008-08-26 00:42:26 +0000740 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
741 RI != RE; ++RI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000742 MVT::SimpleValueType RetVT = RI->first;
Juergen Ributzka88e32512014-09-03 20:56:59 +0000743 OS << " case " << getName(RetVT) << ": return fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000744 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
745 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000746 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson5f334d82008-08-26 00:42:26 +0000747 OS << "(";
748 Operands.PrintArguments(OS);
749 OS << ");\n";
750 }
751 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000752
Owen Anderson5f334d82008-08-26 00:42:26 +0000753 } else {
754 // Non-variadic return type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000755 OS << "unsigned fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000756 << getLegalCName(Opcode) << "_"
757 << getLegalCName(getName(VT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000758 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson9f944592009-08-11 20:47:22 +0000759 OS << "(MVT RetVT";
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000760 if (!Operands.empty())
761 OS << ", ";
Owen Anderson5952cca2008-08-25 23:43:09 +0000762 Operands.PrintParameters(OS);
763 OS << ") {\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000764
Owen Anderson9f944592009-08-11 20:47:22 +0000765 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson3ea3efe2008-08-26 18:50:00 +0000766 << ")\n return 0;\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000767
Owen Anderson5f334d82008-08-26 00:42:26 +0000768 const PredMap &PM = RM.begin()->second;
Jim Grosbach8656d822010-12-07 19:36:07 +0000769
Bill Schmidt18767842014-11-14 21:05:45 +0000770 emitInstructionCode(OS, Operands, PM, "RetVT");
Dan Gohman49e19e92008-08-22 00:20:26 +0000771 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000772 }
773
774 // Emit one function for the opcode that demultiplexes based on the type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000775 OS << "unsigned fastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000776 << getLegalCName(Opcode) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000777 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson9f944592009-08-11 20:47:22 +0000778 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000779 if (!Operands.empty())
780 OS << ", ";
781 Operands.PrintParameters(OS);
782 OS << ") {\n";
Owen Anderson9f944592009-08-11 20:47:22 +0000783 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson5952cca2008-08-25 23:43:09 +0000784 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000785 TI != TE; ++TI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000786 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000787 std::string TypeName = getName(VT);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000788 OS << " case " << TypeName << ": return fastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000789 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000790 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000791 OS << "(RetVT";
792 if (!Operands.empty())
793 OS << ", ";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000794 Operands.PrintArguments(OS);
795 OS << ");\n";
796 }
797 OS << " default: return 0;\n";
798 OS << " }\n";
799 OS << "}\n";
800 OS << "\n";
801 }
802
Dan Gohman9b29ec72008-08-22 00:28:15 +0000803 OS << "// Top-level FastEmit function.\n";
804 OS << "\n";
805
Dan Gohmanb2226e22008-08-13 20:19:35 +0000806 // Emit one function for the operand signature that demultiplexes based
807 // on opcode and type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000808 OS << "unsigned fastEmit_";
Chris Lattner07add492011-04-18 06:22:33 +0000809 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman404a9842010-01-05 22:26:32 +0000810 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000811 if (!Operands.empty())
812 OS << ", ";
813 Operands.PrintParameters(OS);
Bob Wilson650cd8a2014-10-01 22:44:01 +0000814 OS << ") ";
815 if (!Operands.hasAnyImmediateCodes())
816 OS << "override ";
817 OS << "{\n";
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000818
Jim Grosbachf29df342013-08-29 22:41:43 +0000819 // If there are any forms of this signature available that operate on
820 // constrained forms of the immediate (e.g., 32-bit sext immediate in a
Chris Lattner07add492011-04-18 06:22:33 +0000821 // 64-bit operand), check them first.
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000822
Chris Lattner07add492011-04-18 06:22:33 +0000823 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
824 = SignaturesWithConstantForms.find(Operands);
825 if (MI != SignaturesWithConstantForms.end()) {
826 // Unique any duplicates out of the list.
827 std::sort(MI->second.begin(), MI->second.end());
828 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
829 MI->second.end());
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000830
Chris Lattner07add492011-04-18 06:22:33 +0000831 // Check each in order it was seen. It would be nice to have a good
832 // relative ordering between them, but we're not going for optimality
833 // here.
834 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
835 OS << " if (";
836 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000837 OS << ")\n if (unsigned Reg = fastEmit_";
Chris Lattner07add492011-04-18 06:22:33 +0000838 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
839 OS << "(VT, RetVT, Opcode";
840 if (!MI->second[i].empty())
841 OS << ", ";
842 MI->second[i].PrintArguments(OS);
843 OS << "))\n return Reg;\n\n";
844 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000845
Chris Lattner07add492011-04-18 06:22:33 +0000846 // Done with this, remove it.
847 SignaturesWithConstantForms.erase(MI);
848 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000849
Dan Gohmanb2226e22008-08-13 20:19:35 +0000850 OS << " switch (Opcode) {\n";
Owen Anderson5952cca2008-08-25 23:43:09 +0000851 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000852 I != E; ++I) {
853 const std::string &Opcode = I->first;
854
Juergen Ributzka88e32512014-09-03 20:56:59 +0000855 OS << " case " << Opcode << ": return fastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000856 << getLegalCName(Opcode) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000857 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000858 OS << "(VT, RetVT";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000859 if (!Operands.empty())
860 OS << ", ";
861 Operands.PrintArguments(OS);
862 OS << ");\n";
863 }
864 OS << " default: return 0;\n";
865 OS << " }\n";
866 OS << "}\n";
867 OS << "\n";
868 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000869
Chris Lattner07add492011-04-18 06:22:33 +0000870 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman44003cc2008-08-26 21:21:20 +0000871}
872
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000873namespace llvm {
874
875void EmitFastISel(RecordKeeper &RK, raw_ostream &OS) {
876 CodeGenDAGPatterns CGP(RK);
Dan Gohman44003cc2008-08-26 21:21:20 +0000877 const CodeGenTarget &Target = CGP.getTargetInfo();
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000878 emitSourceFileHeader("\"Fast\" Instruction Selector for the " +
879 Target.getName() + " target", OS);
Dan Gohman44003cc2008-08-26 21:21:20 +0000880
881 // Determine the target's namespace name.
882 std::string InstNS = Target.getInstNamespace() + "::";
883 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
884
Dan Gohman44003cc2008-08-26 21:21:20 +0000885 FastISelMap F(InstNS);
Chris Lattner07add492011-04-18 06:22:33 +0000886 F.collectPatterns(CGP);
887 F.printImmediatePredicates(OS);
888 F.printFunctionDefinitions(OS);
Dan Gohman71706232008-08-21 00:19:05 +0000889}
890
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000891} // End llvm namespace