blob: 0e7b0dc09442d88eb94ead3082f2382a78fa696d [file] [log] [blame]
Michael Kuperstein5f565e02017-01-30 19:03:26 +00001///===- FastISelEmitter.cpp - Generate an instruction selector -------------===//
Dan Gohmanb2226e22008-08-13 20:19:35 +00002//
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"
Bob Wilson650cd8a2014-10-01 22:44:01 +000021#include "llvm/ADT/StringSwitch.h"
Chad Rosierd0445fd2011-06-07 20:41:31 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000024#include "llvm/TableGen/Error.h"
25#include "llvm/TableGen/Record.h"
26#include "llvm/TableGen/TableGenBackend.h"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000027#include <utility>
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
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000420FastISelMap::FastISelMap(std::string instns) : InstNS(std::move(instns)) {}
Dan Gohmanb2226e22008-08-13 20:19:35 +0000421
Eli Friedman3bc13b42011-04-29 21:58:31 +0000422static std::string PhyRegForNode(TreePatternNode *Op,
423 const CodeGenTarget &Target) {
424 std::string PhysReg;
425
426 if (!Op->isLeaf())
427 return PhysReg;
428
Sean Silva88eb8dd2012-10-10 20:24:47 +0000429 Record *OpLeafRec = cast<DefInit>(Op->getLeafValue())->getDef();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000430 if (!OpLeafRec->isSubClassOf("Register"))
431 return PhysReg;
432
Sean Silva88eb8dd2012-10-10 20:24:47 +0000433 PhysReg += cast<StringInit>(OpLeafRec->getValue("Namespace")->getValue())
434 ->getValue();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000435 PhysReg += "::";
Jakob Stoklund Olesen8e188be2011-06-18 04:26:06 +0000436 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000437 return PhysReg;
438}
439
Chris Lattner07add492011-04-18 06:22:33 +0000440void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman44003cc2008-08-26 21:21:20 +0000441 const CodeGenTarget &Target = CGP.getTargetInfo();
442
443 // Determine the target's namespace name.
444 InstNS = Target.getInstNamespace() + "::";
445 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb2226e22008-08-13 20:19:35 +0000446
Dan Gohman9b29ec72008-08-22 00:28:15 +0000447 // Scan through all the patterns and record the simple ones.
Dan Gohmanb2226e22008-08-13 20:19:35 +0000448 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
449 E = CGP.ptm_end(); I != E; ++I) {
450 const PatternToMatch &Pattern = *I;
451
452 // For now, just look at Instructions, so that we don't have to worry
453 // about emitting multiple instructions for a pattern.
454 TreePatternNode *Dst = Pattern.getDstPattern();
455 if (Dst->isLeaf()) continue;
456 Record *Op = Dst->getOperator();
457 if (!Op->isSubClassOf("Instruction"))
458 continue;
Chris Lattner9aec14b2010-03-19 00:07:20 +0000459 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattner90803912011-04-17 22:24:13 +0000460 if (II.Operands.empty())
Dan Gohmanb2226e22008-08-13 20:19:35 +0000461 continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000462
Evan Cheng7cab17a2008-09-07 08:19:51 +0000463 // For now, ignore multi-instruction patterns.
464 bool MultiInsts = false;
465 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
466 TreePatternNode *ChildOp = Dst->getChild(i);
467 if (ChildOp->isLeaf())
468 continue;
469 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
470 MultiInsts = true;
471 break;
472 }
473 }
474 if (MultiInsts)
475 continue;
476
Dan Gohmanaa13b6f2008-08-19 20:36:33 +0000477 // For now, ignore instructions where the first operand is not an
478 // output register.
Craig Topper24064772014-04-15 07:20:03 +0000479 const CodeGenRegisterClass *DstRC = nullptr;
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000480 std::string SubRegNo;
Owen Anderson787f1002008-08-28 18:06:12 +0000481 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000482 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersona84be6c2011-06-27 21:06:21 +0000483 if (Op0Rec->isSubClassOf("RegisterOperand"))
484 Op0Rec = Op0Rec->getValueAsDef("RegClass");
Owen Anderson787f1002008-08-28 18:06:12 +0000485 if (!Op0Rec->isSubClassOf("RegisterClass"))
486 continue;
487 DstRC = &Target.getRegisterClass(Op0Rec);
488 if (!DstRC)
489 continue;
490 } else {
Eric Christopherbebb8c52010-07-21 22:07:19 +0000491 // If this isn't a leaf, then continue since the register classes are
492 // a bit too complicated for now.
493 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000494
Sean Silvafb509ed2012-10-10 20:24:43 +0000495 DefInit *SR = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue());
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000496 if (SR)
497 SubRegNo = getQualifiedName(SR->getDef());
498 else
499 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Anderson787f1002008-08-28 18:06:12 +0000500 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000501
502 // Inspect the pattern.
503 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
504 if (!InstPatNode) continue;
505 if (InstPatNode->isLeaf()) continue;
506
Chris Lattner6c2d1782010-03-24 00:41:19 +0000507 // Ignore multiple result nodes for now.
508 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000509
Dan Gohmanb2226e22008-08-13 20:19:35 +0000510 Record *InstPatOp = InstPatNode->getOperator();
511 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000512 MVT::SimpleValueType RetVT = MVT::isVoid;
513 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
Owen Anderson9f944592009-08-11 20:47:22 +0000514 MVT::SimpleValueType VT = RetVT;
Chris Lattnerf1447252010-03-19 21:37:09 +0000515 if (InstPatNode->getNumChildren()) {
516 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
517 VT = InstPatNode->getChild(0)->getType(0);
518 }
Dan Gohmana6c14d02008-08-19 20:30:54 +0000519
520 // For now, filter out any instructions with predicates.
Dan Gohman6e979022008-10-15 06:17:21 +0000521 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmana6c14d02008-08-19 20:30:54 +0000522 continue;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000523
Dan Gohmanaa13b6f2008-08-19 20:36:33 +0000524 // Check all the operands.
Dan Gohmanb2226e22008-08-13 20:19:35 +0000525 OperandsSignature Operands;
Bill Schmidt9b703f92013-05-22 20:45:11 +0000526 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates,
527 DstRC))
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000528 continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000529
Owen Anderson0673a8a2008-08-29 17:45:56 +0000530 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000531 if (InstPatNode->getOperator()->getName() == "imm" ||
Eric Christopherc50ea3b2011-08-23 15:42:35 +0000532 InstPatNode->getOperator()->getName() == "fpimm")
Owen Anderson0673a8a2008-08-29 17:45:56 +0000533 PhysRegInputs->push_back("");
Eli Friedman3bc13b42011-04-29 21:58:31 +0000534 else {
535 // Compute the PhysRegs used by the given pattern, and check that
536 // the mapping from the src to dst patterns is simple.
537 bool FoundNonSimplePattern = false;
538 unsigned DstIndex = 0;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000539 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
Eli Friedman3bc13b42011-04-29 21:58:31 +0000540 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
541 if (PhysReg.empty()) {
542 if (DstIndex >= Dst->getNumChildren() ||
543 Dst->getChild(DstIndex)->getName() !=
544 InstPatNode->getChild(i)->getName()) {
545 FoundNonSimplePattern = true;
546 break;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000547 }
Eli Friedman3bc13b42011-04-29 21:58:31 +0000548 ++DstIndex;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000549 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000550
Owen Anderson0673a8a2008-08-29 17:45:56 +0000551 PhysRegInputs->push_back(PhysReg);
552 }
Eli Friedman3bc13b42011-04-29 21:58:31 +0000553
554 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
555 FoundNonSimplePattern = true;
556
557 if (FoundNonSimplePattern)
558 continue;
559 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000560
Bob Wilson650cd8a2014-10-01 22:44:01 +0000561 // Check if the operands match one of the patterns handled by FastISel.
562 std::string ManglingSuffix;
563 raw_string_ostream SuffixOS(ManglingSuffix);
564 Operands.PrintManglingSuffix(SuffixOS, ImmediatePredicates, true);
565 SuffixOS.flush();
566 if (!StringSwitch<bool>(ManglingSuffix)
Peter Collingbourned799d282016-10-05 19:25:20 +0000567 .Cases("", "r", "rr", "ri", "i", "f", true)
Bob Wilson650cd8a2014-10-01 22:44:01 +0000568 .Default(false))
569 continue;
570
Dan Gohman49e19e92008-08-22 00:20:26 +0000571 // Get the predicate that guards this pattern.
572 std::string PredicateCheck = Pattern.getPredicateCheck();
573
Dan Gohmanb2226e22008-08-13 20:19:35 +0000574 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman7b3932e2008-08-21 00:35:26 +0000575 InstructionMemo Memo = {
576 Pattern.getDstPattern()->getOperator()->getName(),
Owen Anderson787f1002008-08-28 18:06:12 +0000577 DstRC,
Owen Anderson0673a8a2008-08-29 17:45:56 +0000578 SubRegNo,
Bill Schmidt18767842014-11-14 21:05:45 +0000579 PhysRegInputs,
580 PredicateCheck
Dan Gohman7b3932e2008-08-21 00:35:26 +0000581 };
Bill Schmidt18767842014-11-14 21:05:45 +0000582
583 int complexity = Pattern.getPatternComplexity(CGP);
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000584
Bill Schmidt18767842014-11-14 21:05:45 +0000585 if (SimplePatternsCheck[Operands][OpcodeName][VT]
586 [RetVT].count(PredicateCheck)) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000587 PrintFatalError(Pattern.getSrcRecord()->getLoc(),
Bill Schmidt18767842014-11-14 21:05:45 +0000588 "Duplicate predicate in FastISel table!");
589 }
590 SimplePatternsCheck[Operands][OpcodeName][VT][RetVT].insert(
591 std::make_pair(PredicateCheck, true));
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000592
Bill Schmidt18767842014-11-14 21:05:45 +0000593 // Note: Instructions with the same complexity will appear in the order
594 // that they are encountered.
595 SimplePatterns[Operands][OpcodeName][VT][RetVT].insert(
596 std::make_pair(complexity, Memo));
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000597
Chris Lattner07add492011-04-18 06:22:33 +0000598 // If any of the operands were immediates with predicates on them, strip
599 // them down to a signature that doesn't have predicates so that we can
600 // associate them with the stripped predicate version.
601 if (Operands.hasAnyImmediateCodes()) {
602 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
603 .push_back(Operands);
604 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000605 }
Dan Gohman44003cc2008-08-26 21:21:20 +0000606}
Dan Gohmanb2226e22008-08-13 20:19:35 +0000607
Chris Lattner07add492011-04-18 06:22:33 +0000608void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
609 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
610 return;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000611
Chris Lattner07add492011-04-18 06:22:33 +0000612 OS << "\n// FastEmit Immediate Predicate functions.\n";
613 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
614 E = ImmediatePredicates.end(); I != E; ++I) {
615 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
616 OS << I->getImmediatePredicateCode() << "\n}\n";
617 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000618
Chris Lattner07add492011-04-18 06:22:33 +0000619 OS << "\n\n";
620}
621
Bill Schmidt18767842014-11-14 21:05:45 +0000622void FastISelMap::emitInstructionCode(raw_ostream &OS,
623 const OperandsSignature &Operands,
624 const PredMap &PM,
625 const std::string &RetVTName) {
626 // Emit code for each possible instruction. There may be
627 // multiple if there are subtarget concerns. A reverse iterator
628 // is used to produce the ones with highest complexity first.
629
630 bool OneHadNoPredicate = false;
631 for (PredMap::const_reverse_iterator PI = PM.rbegin(), PE = PM.rend();
632 PI != PE; ++PI) {
633 const InstructionMemo &Memo = PI->second;
634 std::string PredicateCheck = Memo.PredicateCheck;
635
636 if (PredicateCheck.empty()) {
637 assert(!OneHadNoPredicate &&
638 "Multiple instructions match and more than one had "
639 "no predicate!");
640 OneHadNoPredicate = true;
641 } else {
642 if (OneHadNoPredicate) {
Michael Kuperstein5f565e02017-01-30 19:03:26 +0000643 PrintFatalError("Multiple instructions match and one with no "
644 "predicate came before one with a predicate! "
645 "name:" + Memo.Name + " predicate: " + PredicateCheck);
Bill Schmidt18767842014-11-14 21:05:45 +0000646 }
647 OS << " if (" + PredicateCheck + ") {\n";
648 OS << " ";
649 }
650
651 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
652 if ((*Memo.PhysRegs)[i] != "")
653 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, "
654 << "TII.get(TargetOpcode::COPY), "
655 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
656 }
657
658 OS << " return fastEmitInst_";
659 if (Memo.SubRegNo.empty()) {
660 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
661 ImmediatePredicates, true);
662 OS << "(" << InstNS << Memo.Name << ", ";
663 OS << "&" << InstNS << Memo.RC->getName() << "RegClass";
664 if (!Operands.empty())
665 OS << ", ";
666 Operands.PrintArguments(OS, *Memo.PhysRegs);
667 OS << ");\n";
668 } else {
669 OS << "extractsubreg(" << RetVTName
670 << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
671 }
672
673 if (!PredicateCheck.empty()) {
674 OS << " }\n";
675 }
676 }
677 // Return 0 if all of the possibilities had predicates but none
678 // were satisfied.
679 if (!OneHadNoPredicate)
680 OS << " return 0;\n";
681 OS << "}\n";
682 OS << "\n";
683}
684
Chris Lattner07add492011-04-18 06:22:33 +0000685
686void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000687 // Now emit code for all the patterns that we collected.
Owen Anderson5952cca2008-08-25 23:43:09 +0000688 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb2226e22008-08-13 20:19:35 +0000689 OE = SimplePatterns.end(); OI != OE; ++OI) {
690 const OperandsSignature &Operands = OI->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000691 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000692
Owen Anderson5952cca2008-08-25 23:43:09 +0000693 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000694 I != E; ++I) {
695 const std::string &Opcode = I->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000696 const TypeRetPredMap &TM = I->second;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000697
698 OS << "// FastEmit functions for " << Opcode << ".\n";
699 OS << "\n";
700
701 // Emit one function for each opcode,type pair.
Owen Anderson5952cca2008-08-25 23:43:09 +0000702 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000703 TI != TE; ++TI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000704 MVT::SimpleValueType VT = TI->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000705 const RetPredMap &RM = TI->second;
Owen Anderson5f334d82008-08-26 00:42:26 +0000706 if (RM.size() != 1) {
707 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
708 RI != RE; ++RI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000709 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson5f334d82008-08-26 00:42:26 +0000710 const PredMap &PM = RI->second;
Dan Gohman49e19e92008-08-22 00:20:26 +0000711
Juergen Ributzka88e32512014-09-03 20:56:59 +0000712 OS << "unsigned fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000713 << getLegalCName(Opcode)
714 << "_" << getLegalCName(getName(VT))
715 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000716 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson5f334d82008-08-26 00:42:26 +0000717 OS << "(";
718 Operands.PrintParameters(OS);
719 OS << ") {\n";
Dan Gohman49e19e92008-08-22 00:20:26 +0000720
Bill Schmidt18767842014-11-14 21:05:45 +0000721 emitInstructionCode(OS, Operands, PM, getName(RetVT));
Owen Anderson5f334d82008-08-26 00:42:26 +0000722 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000723
Owen Anderson5f334d82008-08-26 00:42:26 +0000724 // Emit one function for the type that demultiplexes on return type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000725 OS << "unsigned fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000726 << getLegalCName(Opcode) << "_"
Owen Anderson6f2db722008-08-26 01:22:59 +0000727 << getLegalCName(getName(VT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000728 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson9f944592009-08-11 20:47:22 +0000729 OS << "(MVT RetVT";
Owen Anderson5f334d82008-08-26 00:42:26 +0000730 if (!Operands.empty())
731 OS << ", ";
732 Operands.PrintParameters(OS);
Owen Anderson9f944592009-08-11 20:47:22 +0000733 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson5f334d82008-08-26 00:42:26 +0000734 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
735 RI != RE; ++RI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000736 MVT::SimpleValueType RetVT = RI->first;
Juergen Ributzka88e32512014-09-03 20:56:59 +0000737 OS << " case " << getName(RetVT) << ": return fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000738 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
739 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000740 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson5f334d82008-08-26 00:42:26 +0000741 OS << "(";
742 Operands.PrintArguments(OS);
743 OS << ");\n";
744 }
745 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000746
Owen Anderson5f334d82008-08-26 00:42:26 +0000747 } else {
748 // Non-variadic return type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000749 OS << "unsigned fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000750 << getLegalCName(Opcode) << "_"
751 << getLegalCName(getName(VT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000752 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson9f944592009-08-11 20:47:22 +0000753 OS << "(MVT RetVT";
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000754 if (!Operands.empty())
755 OS << ", ";
Owen Anderson5952cca2008-08-25 23:43:09 +0000756 Operands.PrintParameters(OS);
757 OS << ") {\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000758
Owen Anderson9f944592009-08-11 20:47:22 +0000759 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson3ea3efe2008-08-26 18:50:00 +0000760 << ")\n return 0;\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000761
Owen Anderson5f334d82008-08-26 00:42:26 +0000762 const PredMap &PM = RM.begin()->second;
Jim Grosbach8656d822010-12-07 19:36:07 +0000763
Bill Schmidt18767842014-11-14 21:05:45 +0000764 emitInstructionCode(OS, Operands, PM, "RetVT");
Dan Gohman49e19e92008-08-22 00:20:26 +0000765 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000766 }
767
768 // Emit one function for the opcode that demultiplexes based on the type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000769 OS << "unsigned fastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000770 << getLegalCName(Opcode) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000771 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson9f944592009-08-11 20:47:22 +0000772 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000773 if (!Operands.empty())
774 OS << ", ";
775 Operands.PrintParameters(OS);
776 OS << ") {\n";
Owen Anderson9f944592009-08-11 20:47:22 +0000777 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson5952cca2008-08-25 23:43:09 +0000778 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000779 TI != TE; ++TI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000780 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000781 std::string TypeName = getName(VT);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000782 OS << " case " << TypeName << ": return fastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000783 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000784 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000785 OS << "(RetVT";
786 if (!Operands.empty())
787 OS << ", ";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000788 Operands.PrintArguments(OS);
789 OS << ");\n";
790 }
791 OS << " default: return 0;\n";
792 OS << " }\n";
793 OS << "}\n";
794 OS << "\n";
795 }
796
Dan Gohman9b29ec72008-08-22 00:28:15 +0000797 OS << "// Top-level FastEmit function.\n";
798 OS << "\n";
799
Dan Gohmanb2226e22008-08-13 20:19:35 +0000800 // Emit one function for the operand signature that demultiplexes based
801 // on opcode and type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000802 OS << "unsigned fastEmit_";
Chris Lattner07add492011-04-18 06:22:33 +0000803 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman404a9842010-01-05 22:26:32 +0000804 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000805 if (!Operands.empty())
806 OS << ", ";
807 Operands.PrintParameters(OS);
Bob Wilson650cd8a2014-10-01 22:44:01 +0000808 OS << ") ";
809 if (!Operands.hasAnyImmediateCodes())
810 OS << "override ";
811 OS << "{\n";
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000812
Jim Grosbachf29df342013-08-29 22:41:43 +0000813 // If there are any forms of this signature available that operate on
814 // constrained forms of the immediate (e.g., 32-bit sext immediate in a
Chris Lattner07add492011-04-18 06:22:33 +0000815 // 64-bit operand), check them first.
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000816
Chris Lattner07add492011-04-18 06:22:33 +0000817 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
818 = SignaturesWithConstantForms.find(Operands);
819 if (MI != SignaturesWithConstantForms.end()) {
820 // Unique any duplicates out of the list.
821 std::sort(MI->second.begin(), MI->second.end());
822 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
823 MI->second.end());
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000824
Chris Lattner07add492011-04-18 06:22:33 +0000825 // Check each in order it was seen. It would be nice to have a good
826 // relative ordering between them, but we're not going for optimality
827 // here.
828 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
829 OS << " if (";
830 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000831 OS << ")\n if (unsigned Reg = fastEmit_";
Chris Lattner07add492011-04-18 06:22:33 +0000832 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
833 OS << "(VT, RetVT, Opcode";
834 if (!MI->second[i].empty())
835 OS << ", ";
836 MI->second[i].PrintArguments(OS);
837 OS << "))\n return Reg;\n\n";
838 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000839
Chris Lattner07add492011-04-18 06:22:33 +0000840 // Done with this, remove it.
841 SignaturesWithConstantForms.erase(MI);
842 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000843
Dan Gohmanb2226e22008-08-13 20:19:35 +0000844 OS << " switch (Opcode) {\n";
Owen Anderson5952cca2008-08-25 23:43:09 +0000845 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000846 I != E; ++I) {
847 const std::string &Opcode = I->first;
848
Juergen Ributzka88e32512014-09-03 20:56:59 +0000849 OS << " case " << Opcode << ": return fastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000850 << getLegalCName(Opcode) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000851 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000852 OS << "(VT, RetVT";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000853 if (!Operands.empty())
854 OS << ", ";
855 Operands.PrintArguments(OS);
856 OS << ");\n";
857 }
858 OS << " default: return 0;\n";
859 OS << " }\n";
860 OS << "}\n";
861 OS << "\n";
862 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000863
Chris Lattner07add492011-04-18 06:22:33 +0000864 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman44003cc2008-08-26 21:21:20 +0000865}
866
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000867namespace llvm {
868
869void EmitFastISel(RecordKeeper &RK, raw_ostream &OS) {
870 CodeGenDAGPatterns CGP(RK);
Dan Gohman44003cc2008-08-26 21:21:20 +0000871 const CodeGenTarget &Target = CGP.getTargetInfo();
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000872 emitSourceFileHeader("\"Fast\" Instruction Selector for the " +
Matthias Braun4a86d452016-12-04 05:48:16 +0000873 Target.getName().str() + " target", OS);
Dan Gohman44003cc2008-08-26 21:21:20 +0000874
875 // Determine the target's namespace name.
876 std::string InstNS = Target.getInstNamespace() + "::";
877 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
878
Dan Gohman44003cc2008-08-26 21:21:20 +0000879 FastISelMap F(InstNS);
Chris Lattner07add492011-04-18 06:22:33 +0000880 F.collectPatterns(CGP);
881 F.printImmediatePredicates(OS);
882 F.printFunctionDefinitions(OS);
Dan Gohman71706232008-08-21 00:19:05 +0000883}
884
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000885} // End llvm namespace