blob: 610f4d21bf2d49a24fdd8741cbf51aa681cd526c [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.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000162 TreePattern *TP = PredFn.getOrigPatFragRecord();
163 ValueTypeByHwMode VVT = TP->getTree(0)->getType(0);
164 assert(VVT.isSimple() &&
165 "Cannot use variable value types with fast isel");
166 OS << "VT == " << getEnumName(VVT.getSimple().SimpleTy) << " && ";
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000167
Chris Lattner07add492011-04-18 06:22:33 +0000168 OS << PredFn.getFnName() << "(imm" << i <<')';
169 EmittedAnything = true;
170 }
171 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000172
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000173 /// initialize - Examine the given pattern and initialize the contents
174 /// of the Operands array accordingly. Return true if all the operands
175 /// are supported, false otherwise.
176 ///
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000177 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
Chris Lattner07add492011-04-18 06:22:33 +0000178 MVT::SimpleValueType VT,
Bill Schmidt9b703f92013-05-22 20:45:11 +0000179 ImmPredicateSet &ImmediatePredicates,
180 const CodeGenRegisterClass *OrigDstRC) {
Chris Lattner07add492011-04-18 06:22:33 +0000181 if (InstPatNode->isLeaf())
182 return false;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000183
Chris Lattner07add492011-04-18 06:22:33 +0000184 if (InstPatNode->getOperator()->getName() == "imm") {
185 Operands.push_back(OpKind::getImm(0));
186 return true;
187 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000188
Chris Lattner07add492011-04-18 06:22:33 +0000189 if (InstPatNode->getOperator()->getName() == "fpimm") {
190 Operands.push_back(OpKind::getFP());
191 return true;
Dan Gohman5ca269e2008-08-27 01:09:54 +0000192 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000193
Craig Topper24064772014-04-15 07:20:03 +0000194 const CodeGenRegisterClass *DstRC = nullptr;
Jim Grosbach8656d822010-12-07 19:36:07 +0000195
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000196 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
197 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach8656d822010-12-07 19:36:07 +0000198
Chris Lattner07add492011-04-18 06:22:33 +0000199 // Handle imm operands specially.
200 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") {
201 unsigned PredNo = 0;
202 if (!Op->getPredicateFns().empty()) {
Chris Lattner80254a52011-04-18 06:36:55 +0000203 TreePredicateFn PredFn = Op->getPredicateFns()[0];
Chris Lattner07add492011-04-18 06:22:33 +0000204 // If there is more than one predicate weighing in on this operand
205 // then we don't handle it. This doesn't typically happen for
206 // immediates anyway.
207 if (Op->getPredicateFns().size() > 1 ||
Chris Lattner80254a52011-04-18 06:36:55 +0000208 !PredFn.isImmediatePattern())
209 return false;
210 // Ignore any instruction with 'FastIselShouldIgnore', these are
211 // not needed and just bloat the fast instruction selector. For
212 // example, X86 doesn't need to generate code to match ADD16ri8 since
213 // ADD16ri will do just fine.
214 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord();
215 if (Rec->getValueAsBit("FastIselShouldIgnore"))
Chris Lattner07add492011-04-18 06:22:33 +0000216 return false;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000217
Chris Lattner80254a52011-04-18 06:36:55 +0000218 PredNo = ImmediatePredicates.getIDFor(PredFn)+1;
Chris Lattner07add492011-04-18 06:22:33 +0000219 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000220
Chris Lattner07add492011-04-18 06:22:33 +0000221 Operands.push_back(OpKind::getImm(PredNo));
222 continue;
223 }
224
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000225
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000226 // For now, filter out any operand with a predicate.
Dan Gohmanfe905652008-08-21 01:41:07 +0000227 // For now, filter out any operand with multiple values.
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000228 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
Chris Lattnerf1447252010-03-19 21:37:09 +0000229 return false;
Jim Grosbach8656d822010-12-07 19:36:07 +0000230
Dan Gohmanfe905652008-08-21 01:41:07 +0000231 if (!Op->isLeaf()) {
Chris Lattner07add492011-04-18 06:22:33 +0000232 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner722f0cc2011-04-17 23:29:05 +0000233 Operands.push_back(OpKind::getFP());
Dale Johannesen4ff70e382009-05-21 22:25:49 +0000234 continue;
Dan Gohman5ca269e2008-08-27 01:09:54 +0000235 }
Dan Gohman6d153b02008-08-27 16:18:22 +0000236 // For now, ignore other non-leaf nodes.
Dan Gohmanfe905652008-08-21 01:41:07 +0000237 return false;
238 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000239
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000240 assert(Op->hasConcreteType(0) && "Type infererence not done?");
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000241
242 // For now, all the operands must have the same type (if they aren't
243 // immediates). Note that this causes us to reject variable sized shifts
244 // on X86.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000245 if (Op->getSimpleType(0) != VT)
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000246 return false;
247
Sean Silvafb509ed2012-10-10 20:24:43 +0000248 DefInit *OpDI = dyn_cast<DefInit>(Op->getLeafValue());
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000249 if (!OpDI)
250 return false;
251 Record *OpLeafRec = OpDI->getDef();
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000252
Dan Gohmanfe905652008-08-21 01:41:07 +0000253 // For now, the only other thing we accept is register operands.
Craig Topper24064772014-04-15 07:20:03 +0000254 const CodeGenRegisterClass *RC = nullptr;
Owen Andersona84be6c2011-06-27 21:06:21 +0000255 if (OpLeafRec->isSubClassOf("RegisterOperand"))
256 OpLeafRec = OpLeafRec->getValueAsDef("RegClass");
Owen Anderson0673a8a2008-08-29 17:45:56 +0000257 if (OpLeafRec->isSubClassOf("RegisterClass"))
258 RC = &Target.getRegisterClass(OpLeafRec);
259 else if (OpLeafRec->isSubClassOf("Register"))
Jakob Stoklund Olesen22ea4242011-06-15 00:20:40 +0000260 RC = Target.getRegBank().getRegClassForRegister(OpLeafRec);
Bill Schmidt9b703f92013-05-22 20:45:11 +0000261 else if (OpLeafRec->isSubClassOf("ValueType")) {
262 RC = OrigDstRC;
263 } else
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000264 return false;
Jim Grosbach8656d822010-12-07 19:36:07 +0000265
Eric Christopher98f0ea62010-08-24 23:21:59 +0000266 // For now, this needs to be a register class of some sort.
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000267 if (!RC)
268 return false;
Eric Christopher98f0ea62010-08-24 23:21:59 +0000269
Eric Christopher6490bf62010-08-25 04:58:56 +0000270 // For now, all the operands must have the same register class or be
271 // a strict subclass of the destination.
Owen Anderson6f2db722008-08-26 01:22:59 +0000272 if (DstRC) {
Eric Christopher6490bf62010-08-25 04:58:56 +0000273 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Anderson6f2db722008-08-26 01:22:59 +0000274 return false;
275 } else
276 DstRC = RC;
Chris Lattner722f0cc2011-04-17 23:29:05 +0000277 Operands.push_back(OpKind::getReg());
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000278 }
279 return true;
280 }
281
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000282 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000283 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner722f0cc2011-04-17 23:29:05 +0000284 if (Operands[i].isReg()) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000285 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner722f0cc2011-04-17 23:29:05 +0000286 } else if (Operands[i].isImm()) {
Dan Gohmanfe905652008-08-21 01:41:07 +0000287 OS << "uint64_t imm" << i;
Chris Lattner722f0cc2011-04-17 23:29:05 +0000288 } else if (Operands[i].isFP()) {
Cameron Zwariche47e6822012-01-07 08:18:37 +0000289 OS << "const ConstantFP *f" << i;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000290 } else {
Chad Rosierd0445fd2011-06-07 20:41:31 +0000291 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb2226e22008-08-13 20:19:35 +0000292 }
293 if (i + 1 != e)
294 OS << ", ";
295 }
296 }
297
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000298 void PrintArguments(raw_ostream &OS,
Chris Lattner722f0cc2011-04-17 23:29:05 +0000299 const std::vector<std::string> &PR) const {
Owen Anderson0673a8a2008-08-29 17:45:56 +0000300 assert(PR.size() == Operands.size());
Evan Chengca14c072008-09-08 08:39:33 +0000301 bool PrintedArg = false;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000302 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Chengca14c072008-09-08 08:39:33 +0000303 if (PR[i] != "")
304 // Implicit physical register operand.
305 continue;
306
307 if (PrintedArg)
308 OS << ", ";
Chris Lattner722f0cc2011-04-17 23:29:05 +0000309 if (Operands[i].isReg()) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000310 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Chengca14c072008-09-08 08:39:33 +0000311 PrintedArg = true;
Chris Lattner722f0cc2011-04-17 23:29:05 +0000312 } else if (Operands[i].isImm()) {
Owen Anderson0673a8a2008-08-29 17:45:56 +0000313 OS << "imm" << i;
Evan Chengca14c072008-09-08 08:39:33 +0000314 PrintedArg = true;
Chris Lattner722f0cc2011-04-17 23:29:05 +0000315 } else if (Operands[i].isFP()) {
Owen Anderson0673a8a2008-08-29 17:45:56 +0000316 OS << "f" << i;
Evan Chengca14c072008-09-08 08:39:33 +0000317 PrintedArg = true;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000318 } else {
Chad Rosierd0445fd2011-06-07 20:41:31 +0000319 llvm_unreachable("Unknown operand kind!");
Owen Anderson0673a8a2008-08-29 17:45:56 +0000320 }
Owen Anderson0673a8a2008-08-29 17:45:56 +0000321 }
322 }
323
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000324 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000325 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner722f0cc2011-04-17 23:29:05 +0000326 if (Operands[i].isReg()) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000327 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner722f0cc2011-04-17 23:29:05 +0000328 } else if (Operands[i].isImm()) {
Dan Gohmanfe905652008-08-21 01:41:07 +0000329 OS << "imm" << i;
Chris Lattner722f0cc2011-04-17 23:29:05 +0000330 } else if (Operands[i].isFP()) {
Dan Gohman5ca269e2008-08-27 01:09:54 +0000331 OS << "f" << i;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000332 } else {
Chad Rosierd0445fd2011-06-07 20:41:31 +0000333 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb2226e22008-08-13 20:19:35 +0000334 }
335 if (i + 1 != e)
336 OS << ", ";
337 }
338 }
339
Owen Anderson0673a8a2008-08-29 17:45:56 +0000340
Chris Lattner07add492011-04-18 06:22:33 +0000341 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
342 ImmPredicateSet &ImmPredicates,
343 bool StripImmCodes = false) const {
Evan Chengca14c072008-09-08 08:39:33 +0000344 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
345 if (PR[i] != "")
346 // Implicit physical register operand. e.g. Instruction::Mul expect to
347 // select to a binary op. On x86, mul may take a single operand with
348 // the other operand being implicit. We must emit something that looks
Juergen Ributzka88e32512014-09-03 20:56:59 +0000349 // like a binary instruction except for the very inner fastEmitInst_*
Evan Chengca14c072008-09-08 08:39:33 +0000350 // call.
351 continue;
Chris Lattner07add492011-04-18 06:22:33 +0000352 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Evan Chengca14c072008-09-08 08:39:33 +0000353 }
354 }
355
Chris Lattner07add492011-04-18 06:22:33 +0000356 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
357 bool StripImmCodes = false) const {
Chris Lattner722f0cc2011-04-17 23:29:05 +0000358 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
Chris Lattner07add492011-04-18 06:22:33 +0000359 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Dan Gohmanb2226e22008-08-13 20:19:35 +0000360 }
361};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000362} // End anonymous namespace
Dan Gohmanb2226e22008-08-13 20:19:35 +0000363
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000364namespace {
Dan Gohman44003cc2008-08-26 21:21:20 +0000365class FastISelMap {
Simon Pilgrimd0faf162017-10-06 15:33:55 +0000366 // A multimap is needed instead of a "plain" map because the key is
Bill Schmidt18767842014-11-14 21:05:45 +0000367 // the instruction's complexity (an int) and they are not unique.
368 typedef std::multimap<int, InstructionMemo> PredMap;
Owen Anderson9f944592009-08-11 20:47:22 +0000369 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
370 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman44003cc2008-08-26 21:21:20 +0000371 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach8656d822010-12-07 19:36:07 +0000372 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopher62ac5ca2010-07-26 17:53:07 +0000373 OperandsOpcodeTypeRetPredMap;
Dan Gohman44003cc2008-08-26 21:21:20 +0000374
375 OperandsOpcodeTypeRetPredMap SimplePatterns;
376
Simon Pilgrimd0faf162017-10-06 15:33:55 +0000377 // This is used to check that there are no duplicate predicates
Bill Schmidt18767842014-11-14 21:05:45 +0000378 typedef std::multimap<std::string, bool> PredCheckMap;
379 typedef std::map<MVT::SimpleValueType, PredCheckMap> RetPredCheckMap;
380 typedef std::map<MVT::SimpleValueType, RetPredCheckMap> TypeRetPredCheckMap;
381 typedef std::map<std::string, TypeRetPredCheckMap> OpcodeTypeRetPredCheckMap;
382 typedef std::map<OperandsSignature, OpcodeTypeRetPredCheckMap>
383 OperandsOpcodeTypeRetPredCheckMap;
384
385 OperandsOpcodeTypeRetPredCheckMap SimplePatternsCheck;
386
Chris Lattner07add492011-04-18 06:22:33 +0000387 std::map<OperandsSignature, std::vector<OperandsSignature> >
388 SignaturesWithConstantForms;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000389
Craig Topper15e3de12017-07-07 06:22:36 +0000390 StringRef InstNS;
Chris Lattner07add492011-04-18 06:22:33 +0000391 ImmPredicateSet ImmediatePredicates;
Dan Gohman44003cc2008-08-26 21:21:20 +0000392public:
Craig Topper15e3de12017-07-07 06:22:36 +0000393 explicit FastISelMap(StringRef InstNS);
Dan Gohman44003cc2008-08-26 21:21:20 +0000394
Chris Lattner07add492011-04-18 06:22:33 +0000395 void collectPatterns(CodeGenDAGPatterns &CGP);
396 void printImmediatePredicates(raw_ostream &OS);
397 void printFunctionDefinitions(raw_ostream &OS);
Simon Pilgrimd0faf162017-10-06 15:33:55 +0000398private:
399 void emitInstructionCode(raw_ostream &OS,
Bill Schmidt18767842014-11-14 21:05:45 +0000400 const OperandsSignature &Operands,
Simon Pilgrimd0faf162017-10-06 15:33:55 +0000401 const PredMap &PM,
Bill Schmidt18767842014-11-14 21:05:45 +0000402 const std::string &RetVTName);
Dan Gohman44003cc2008-08-26 21:21:20 +0000403};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000404} // End anonymous namespace
Dan Gohmanb2226e22008-08-13 20:19:35 +0000405
406static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
407 return CGP.getSDNodeInfo(Op).getEnumName();
408}
409
410static std::string getLegalCName(std::string OpName) {
411 std::string::size_type pos = OpName.find("::");
412 if (pos != std::string::npos)
413 OpName.replace(pos, 2, "_");
414 return OpName;
415}
416
Craig Topper15e3de12017-07-07 06:22:36 +0000417FastISelMap::FastISelMap(StringRef instns) : InstNS(instns) {}
Dan Gohmanb2226e22008-08-13 20:19:35 +0000418
Eli Friedman3bc13b42011-04-29 21:58:31 +0000419static std::string PhyRegForNode(TreePatternNode *Op,
420 const CodeGenTarget &Target) {
421 std::string PhysReg;
422
423 if (!Op->isLeaf())
424 return PhysReg;
425
Sean Silva88eb8dd2012-10-10 20:24:47 +0000426 Record *OpLeafRec = cast<DefInit>(Op->getLeafValue())->getDef();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000427 if (!OpLeafRec->isSubClassOf("Register"))
428 return PhysReg;
429
Sean Silva88eb8dd2012-10-10 20:24:47 +0000430 PhysReg += cast<StringInit>(OpLeafRec->getValue("Namespace")->getValue())
431 ->getValue();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000432 PhysReg += "::";
Jakob Stoklund Olesen8e188be2011-06-18 04:26:06 +0000433 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000434 return PhysReg;
435}
436
Chris Lattner07add492011-04-18 06:22:33 +0000437void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman44003cc2008-08-26 21:21:20 +0000438 const CodeGenTarget &Target = CGP.getTargetInfo();
439
Dan Gohman9b29ec72008-08-22 00:28:15 +0000440 // Scan through all the patterns and record the simple ones.
Dan Gohmanb2226e22008-08-13 20:19:35 +0000441 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
442 E = CGP.ptm_end(); I != E; ++I) {
443 const PatternToMatch &Pattern = *I;
444
445 // For now, just look at Instructions, so that we don't have to worry
446 // about emitting multiple instructions for a pattern.
447 TreePatternNode *Dst = Pattern.getDstPattern();
448 if (Dst->isLeaf()) continue;
449 Record *Op = Dst->getOperator();
450 if (!Op->isSubClassOf("Instruction"))
451 continue;
Chris Lattner9aec14b2010-03-19 00:07:20 +0000452 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattner90803912011-04-17 22:24:13 +0000453 if (II.Operands.empty())
Dan Gohmanb2226e22008-08-13 20:19:35 +0000454 continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000455
Evan Cheng7cab17a2008-09-07 08:19:51 +0000456 // For now, ignore multi-instruction patterns.
457 bool MultiInsts = false;
458 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
459 TreePatternNode *ChildOp = Dst->getChild(i);
460 if (ChildOp->isLeaf())
461 continue;
462 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
463 MultiInsts = true;
464 break;
465 }
466 }
467 if (MultiInsts)
468 continue;
469
Dan Gohmanaa13b6f2008-08-19 20:36:33 +0000470 // For now, ignore instructions where the first operand is not an
471 // output register.
Craig Topper24064772014-04-15 07:20:03 +0000472 const CodeGenRegisterClass *DstRC = nullptr;
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000473 std::string SubRegNo;
Owen Anderson787f1002008-08-28 18:06:12 +0000474 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000475 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersona84be6c2011-06-27 21:06:21 +0000476 if (Op0Rec->isSubClassOf("RegisterOperand"))
477 Op0Rec = Op0Rec->getValueAsDef("RegClass");
Owen Anderson787f1002008-08-28 18:06:12 +0000478 if (!Op0Rec->isSubClassOf("RegisterClass"))
479 continue;
480 DstRC = &Target.getRegisterClass(Op0Rec);
481 if (!DstRC)
482 continue;
483 } else {
Eric Christopherbebb8c52010-07-21 22:07:19 +0000484 // If this isn't a leaf, then continue since the register classes are
485 // a bit too complicated for now.
486 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000487
Sean Silvafb509ed2012-10-10 20:24:43 +0000488 DefInit *SR = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue());
Jakob Stoklund Olesen1c696462010-05-24 14:48:12 +0000489 if (SR)
490 SubRegNo = getQualifiedName(SR->getDef());
491 else
492 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Anderson787f1002008-08-28 18:06:12 +0000493 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000494
495 // Inspect the pattern.
496 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
497 if (!InstPatNode) continue;
498 if (InstPatNode->isLeaf()) continue;
499
Chris Lattner6c2d1782010-03-24 00:41:19 +0000500 // Ignore multiple result nodes for now.
501 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000502
Dan Gohmanb2226e22008-08-13 20:19:35 +0000503 Record *InstPatOp = InstPatNode->getOperator();
504 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000505 MVT::SimpleValueType RetVT = MVT::isVoid;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000506 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getSimpleType(0);
Owen Anderson9f944592009-08-11 20:47:22 +0000507 MVT::SimpleValueType VT = RetVT;
Chris Lattnerf1447252010-03-19 21:37:09 +0000508 if (InstPatNode->getNumChildren()) {
509 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000510 VT = InstPatNode->getChild(0)->getSimpleType(0);
Chris Lattnerf1447252010-03-19 21:37:09 +0000511 }
Dan Gohmana6c14d02008-08-19 20:30:54 +0000512
513 // For now, filter out any instructions with predicates.
Dan Gohman6e979022008-10-15 06:17:21 +0000514 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmana6c14d02008-08-19 20:30:54 +0000515 continue;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000516
Dan Gohmanaa13b6f2008-08-19 20:36:33 +0000517 // Check all the operands.
Dan Gohmanb2226e22008-08-13 20:19:35 +0000518 OperandsSignature Operands;
Bill Schmidt9b703f92013-05-22 20:45:11 +0000519 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates,
520 DstRC))
Dan Gohman98e6f1c2008-08-19 20:56:30 +0000521 continue;
Jim Grosbach8656d822010-12-07 19:36:07 +0000522
Owen Anderson0673a8a2008-08-29 17:45:56 +0000523 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
Eli Friedman3bc13b42011-04-29 21:58:31 +0000524 if (InstPatNode->getOperator()->getName() == "imm" ||
Eric Christopherc50ea3b2011-08-23 15:42:35 +0000525 InstPatNode->getOperator()->getName() == "fpimm")
Owen Anderson0673a8a2008-08-29 17:45:56 +0000526 PhysRegInputs->push_back("");
Eli Friedman3bc13b42011-04-29 21:58:31 +0000527 else {
528 // Compute the PhysRegs used by the given pattern, and check that
529 // the mapping from the src to dst patterns is simple.
530 bool FoundNonSimplePattern = false;
531 unsigned DstIndex = 0;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000532 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
Eli Friedman3bc13b42011-04-29 21:58:31 +0000533 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
534 if (PhysReg.empty()) {
535 if (DstIndex >= Dst->getNumChildren() ||
536 Dst->getChild(DstIndex)->getName() !=
537 InstPatNode->getChild(i)->getName()) {
538 FoundNonSimplePattern = true;
539 break;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000540 }
Eli Friedman3bc13b42011-04-29 21:58:31 +0000541 ++DstIndex;
Owen Anderson0673a8a2008-08-29 17:45:56 +0000542 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000543
Owen Anderson0673a8a2008-08-29 17:45:56 +0000544 PhysRegInputs->push_back(PhysReg);
545 }
Eli Friedman3bc13b42011-04-29 21:58:31 +0000546
547 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
548 FoundNonSimplePattern = true;
549
550 if (FoundNonSimplePattern)
551 continue;
552 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000553
Bob Wilson650cd8a2014-10-01 22:44:01 +0000554 // Check if the operands match one of the patterns handled by FastISel.
555 std::string ManglingSuffix;
556 raw_string_ostream SuffixOS(ManglingSuffix);
557 Operands.PrintManglingSuffix(SuffixOS, ImmediatePredicates, true);
558 SuffixOS.flush();
559 if (!StringSwitch<bool>(ManglingSuffix)
Peter Collingbourned799d282016-10-05 19:25:20 +0000560 .Cases("", "r", "rr", "ri", "i", "f", true)
Bob Wilson650cd8a2014-10-01 22:44:01 +0000561 .Default(false))
562 continue;
563
Dan Gohman49e19e92008-08-22 00:20:26 +0000564 // Get the predicate that guards this pattern.
565 std::string PredicateCheck = Pattern.getPredicateCheck();
566
Dan Gohmanb2226e22008-08-13 20:19:35 +0000567 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman7b3932e2008-08-21 00:35:26 +0000568 InstructionMemo Memo = {
569 Pattern.getDstPattern()->getOperator()->getName(),
Owen Anderson787f1002008-08-28 18:06:12 +0000570 DstRC,
Owen Anderson0673a8a2008-08-29 17:45:56 +0000571 SubRegNo,
Bill Schmidt18767842014-11-14 21:05:45 +0000572 PhysRegInputs,
573 PredicateCheck
Dan Gohman7b3932e2008-08-21 00:35:26 +0000574 };
Simon Pilgrimd0faf162017-10-06 15:33:55 +0000575
Bill Schmidt18767842014-11-14 21:05:45 +0000576 int complexity = Pattern.getPatternComplexity(CGP);
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000577
Bill Schmidt18767842014-11-14 21:05:45 +0000578 if (SimplePatternsCheck[Operands][OpcodeName][VT]
579 [RetVT].count(PredicateCheck)) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000580 PrintFatalError(Pattern.getSrcRecord()->getLoc(),
Bill Schmidt18767842014-11-14 21:05:45 +0000581 "Duplicate predicate in FastISel table!");
582 }
583 SimplePatternsCheck[Operands][OpcodeName][VT][RetVT].insert(
584 std::make_pair(PredicateCheck, true));
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000585
Bill Schmidt18767842014-11-14 21:05:45 +0000586 // Note: Instructions with the same complexity will appear in the order
587 // that they are encountered.
588 SimplePatterns[Operands][OpcodeName][VT][RetVT].insert(
589 std::make_pair(complexity, Memo));
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000590
Chris Lattner07add492011-04-18 06:22:33 +0000591 // If any of the operands were immediates with predicates on them, strip
592 // them down to a signature that doesn't have predicates so that we can
593 // associate them with the stripped predicate version.
594 if (Operands.hasAnyImmediateCodes()) {
595 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
596 .push_back(Operands);
597 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000598 }
Dan Gohman44003cc2008-08-26 21:21:20 +0000599}
Dan Gohmanb2226e22008-08-13 20:19:35 +0000600
Chris Lattner07add492011-04-18 06:22:33 +0000601void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
602 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
603 return;
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000604
Chris Lattner07add492011-04-18 06:22:33 +0000605 OS << "\n// FastEmit Immediate Predicate functions.\n";
606 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
607 E = ImmediatePredicates.end(); I != E; ++I) {
608 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
609 OS << I->getImmediatePredicateCode() << "\n}\n";
610 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000611
Chris Lattner07add492011-04-18 06:22:33 +0000612 OS << "\n\n";
613}
614
Simon Pilgrimd0faf162017-10-06 15:33:55 +0000615void FastISelMap::emitInstructionCode(raw_ostream &OS,
Bill Schmidt18767842014-11-14 21:05:45 +0000616 const OperandsSignature &Operands,
Simon Pilgrimd0faf162017-10-06 15:33:55 +0000617 const PredMap &PM,
Bill Schmidt18767842014-11-14 21:05:45 +0000618 const std::string &RetVTName) {
619 // Emit code for each possible instruction. There may be
620 // multiple if there are subtarget concerns. A reverse iterator
621 // is used to produce the ones with highest complexity first.
622
623 bool OneHadNoPredicate = false;
624 for (PredMap::const_reverse_iterator PI = PM.rbegin(), PE = PM.rend();
625 PI != PE; ++PI) {
626 const InstructionMemo &Memo = PI->second;
627 std::string PredicateCheck = Memo.PredicateCheck;
628
629 if (PredicateCheck.empty()) {
630 assert(!OneHadNoPredicate &&
631 "Multiple instructions match and more than one had "
632 "no predicate!");
633 OneHadNoPredicate = true;
634 } else {
635 if (OneHadNoPredicate) {
Michael Kuperstein5f565e02017-01-30 19:03:26 +0000636 PrintFatalError("Multiple instructions match and one with no "
637 "predicate came before one with a predicate! "
638 "name:" + Memo.Name + " predicate: " + PredicateCheck);
Bill Schmidt18767842014-11-14 21:05:45 +0000639 }
640 OS << " if (" + PredicateCheck + ") {\n";
641 OS << " ";
642 }
643
644 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
645 if ((*Memo.PhysRegs)[i] != "")
646 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, "
647 << "TII.get(TargetOpcode::COPY), "
648 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
649 }
650
651 OS << " return fastEmitInst_";
652 if (Memo.SubRegNo.empty()) {
653 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
654 ImmediatePredicates, true);
Craig Topper15e3de12017-07-07 06:22:36 +0000655 OS << "(" << InstNS << "::" << Memo.Name << ", ";
656 OS << "&" << InstNS << "::" << Memo.RC->getName() << "RegClass";
Bill Schmidt18767842014-11-14 21:05:45 +0000657 if (!Operands.empty())
658 OS << ", ";
659 Operands.PrintArguments(OS, *Memo.PhysRegs);
660 OS << ");\n";
661 } else {
662 OS << "extractsubreg(" << RetVTName
663 << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
664 }
665
666 if (!PredicateCheck.empty()) {
667 OS << " }\n";
668 }
669 }
670 // Return 0 if all of the possibilities had predicates but none
671 // were satisfied.
672 if (!OneHadNoPredicate)
673 OS << " return 0;\n";
674 OS << "}\n";
675 OS << "\n";
676}
677
Chris Lattner07add492011-04-18 06:22:33 +0000678
679void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb2226e22008-08-13 20:19:35 +0000680 // Now emit code for all the patterns that we collected.
Owen Anderson5952cca2008-08-25 23:43:09 +0000681 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb2226e22008-08-13 20:19:35 +0000682 OE = SimplePatterns.end(); OI != OE; ++OI) {
683 const OperandsSignature &Operands = OI->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000684 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000685
Owen Anderson5952cca2008-08-25 23:43:09 +0000686 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000687 I != E; ++I) {
688 const std::string &Opcode = I->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000689 const TypeRetPredMap &TM = I->second;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000690
691 OS << "// FastEmit functions for " << Opcode << ".\n";
692 OS << "\n";
693
694 // Emit one function for each opcode,type pair.
Owen Anderson5952cca2008-08-25 23:43:09 +0000695 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000696 TI != TE; ++TI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000697 MVT::SimpleValueType VT = TI->first;
Owen Anderson5952cca2008-08-25 23:43:09 +0000698 const RetPredMap &RM = TI->second;
Owen Anderson5f334d82008-08-26 00:42:26 +0000699 if (RM.size() != 1) {
700 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
701 RI != RE; ++RI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000702 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson5f334d82008-08-26 00:42:26 +0000703 const PredMap &PM = RI->second;
Dan Gohman49e19e92008-08-22 00:20:26 +0000704
Juergen Ributzka88e32512014-09-03 20:56:59 +0000705 OS << "unsigned fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000706 << getLegalCName(Opcode)
707 << "_" << getLegalCName(getName(VT))
708 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000709 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson5f334d82008-08-26 00:42:26 +0000710 OS << "(";
711 Operands.PrintParameters(OS);
712 OS << ") {\n";
Dan Gohman49e19e92008-08-22 00:20:26 +0000713
Bill Schmidt18767842014-11-14 21:05:45 +0000714 emitInstructionCode(OS, Operands, PM, getName(RetVT));
Owen Anderson5f334d82008-08-26 00:42:26 +0000715 }
Jim Grosbach8656d822010-12-07 19:36:07 +0000716
Owen Anderson5f334d82008-08-26 00:42:26 +0000717 // Emit one function for the type that demultiplexes on return type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000718 OS << "unsigned fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000719 << getLegalCName(Opcode) << "_"
Owen Anderson6f2db722008-08-26 01:22:59 +0000720 << getLegalCName(getName(VT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000721 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson9f944592009-08-11 20:47:22 +0000722 OS << "(MVT RetVT";
Owen Anderson5f334d82008-08-26 00:42:26 +0000723 if (!Operands.empty())
724 OS << ", ";
725 Operands.PrintParameters(OS);
Owen Anderson9f944592009-08-11 20:47:22 +0000726 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson5f334d82008-08-26 00:42:26 +0000727 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
728 RI != RE; ++RI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000729 MVT::SimpleValueType RetVT = RI->first;
Juergen Ributzka88e32512014-09-03 20:56:59 +0000730 OS << " case " << getName(RetVT) << ": return fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000731 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
732 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000733 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson5f334d82008-08-26 00:42:26 +0000734 OS << "(";
735 Operands.PrintArguments(OS);
736 OS << ");\n";
737 }
738 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000739
Owen Anderson5f334d82008-08-26 00:42:26 +0000740 } else {
741 // Non-variadic return type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000742 OS << "unsigned fastEmit_"
Owen Anderson5f334d82008-08-26 00:42:26 +0000743 << getLegalCName(Opcode) << "_"
744 << getLegalCName(getName(VT)) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000745 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson9f944592009-08-11 20:47:22 +0000746 OS << "(MVT RetVT";
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000747 if (!Operands.empty())
748 OS << ", ";
Owen Anderson5952cca2008-08-25 23:43:09 +0000749 Operands.PrintParameters(OS);
750 OS << ") {\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000751
Owen Anderson9f944592009-08-11 20:47:22 +0000752 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson3ea3efe2008-08-26 18:50:00 +0000753 << ")\n return 0;\n";
Jim Grosbach8656d822010-12-07 19:36:07 +0000754
Owen Anderson5f334d82008-08-26 00:42:26 +0000755 const PredMap &PM = RM.begin()->second;
Jim Grosbach8656d822010-12-07 19:36:07 +0000756
Bill Schmidt18767842014-11-14 21:05:45 +0000757 emitInstructionCode(OS, Operands, PM, "RetVT");
Dan Gohman49e19e92008-08-22 00:20:26 +0000758 }
Dan Gohmanb2226e22008-08-13 20:19:35 +0000759 }
760
761 // Emit one function for the opcode that demultiplexes based on the type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000762 OS << "unsigned fastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000763 << getLegalCName(Opcode) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000764 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson9f944592009-08-11 20:47:22 +0000765 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000766 if (!Operands.empty())
767 OS << ", ";
768 Operands.PrintParameters(OS);
769 OS << ") {\n";
Owen Anderson9f944592009-08-11 20:47:22 +0000770 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson5952cca2008-08-25 23:43:09 +0000771 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000772 TI != TE; ++TI) {
Owen Anderson9f944592009-08-11 20:47:22 +0000773 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb2226e22008-08-13 20:19:35 +0000774 std::string TypeName = getName(VT);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000775 OS << " case " << TypeName << ": return fastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000776 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000777 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000778 OS << "(RetVT";
779 if (!Operands.empty())
780 OS << ", ";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000781 Operands.PrintArguments(OS);
782 OS << ");\n";
783 }
784 OS << " default: return 0;\n";
785 OS << " }\n";
786 OS << "}\n";
787 OS << "\n";
788 }
789
Dan Gohman9b29ec72008-08-22 00:28:15 +0000790 OS << "// Top-level FastEmit function.\n";
791 OS << "\n";
792
Dan Gohmanb2226e22008-08-13 20:19:35 +0000793 // Emit one function for the operand signature that demultiplexes based
794 // on opcode and type.
Juergen Ributzka88e32512014-09-03 20:56:59 +0000795 OS << "unsigned fastEmit_";
Chris Lattner07add492011-04-18 06:22:33 +0000796 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman404a9842010-01-05 22:26:32 +0000797 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000798 if (!Operands.empty())
799 OS << ", ";
800 Operands.PrintParameters(OS);
Bob Wilson650cd8a2014-10-01 22:44:01 +0000801 OS << ") ";
802 if (!Operands.hasAnyImmediateCodes())
803 OS << "override ";
804 OS << "{\n";
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000805
Jim Grosbachf29df342013-08-29 22:41:43 +0000806 // If there are any forms of this signature available that operate on
807 // constrained forms of the immediate (e.g., 32-bit sext immediate in a
Chris Lattner07add492011-04-18 06:22:33 +0000808 // 64-bit operand), check them first.
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000809
Chris Lattner07add492011-04-18 06:22:33 +0000810 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
811 = SignaturesWithConstantForms.find(Operands);
812 if (MI != SignaturesWithConstantForms.end()) {
813 // Unique any duplicates out of the list.
814 std::sort(MI->second.begin(), MI->second.end());
815 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
816 MI->second.end());
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000817
Chris Lattner07add492011-04-18 06:22:33 +0000818 // Check each in order it was seen. It would be nice to have a good
819 // relative ordering between them, but we're not going for optimality
820 // here.
821 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
822 OS << " if (";
823 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000824 OS << ")\n if (unsigned Reg = fastEmit_";
Chris Lattner07add492011-04-18 06:22:33 +0000825 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
826 OS << "(VT, RetVT, Opcode";
827 if (!MI->second[i].empty())
828 OS << ", ";
829 MI->second[i].PrintArguments(OS);
830 OS << "))\n return Reg;\n\n";
831 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000832
Chris Lattner07add492011-04-18 06:22:33 +0000833 // Done with this, remove it.
834 SignaturesWithConstantForms.erase(MI);
835 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000836
Dan Gohmanb2226e22008-08-13 20:19:35 +0000837 OS << " switch (Opcode) {\n";
Owen Anderson5952cca2008-08-25 23:43:09 +0000838 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb2226e22008-08-13 20:19:35 +0000839 I != E; ++I) {
840 const std::string &Opcode = I->first;
841
Juergen Ributzka88e32512014-09-03 20:56:59 +0000842 OS << " case " << Opcode << ": return fastEmit_"
Dan Gohmanfe905652008-08-21 01:41:07 +0000843 << getLegalCName(Opcode) << "_";
Chris Lattner07add492011-04-18 06:22:33 +0000844 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000845 OS << "(VT, RetVT";
Dan Gohmanb2226e22008-08-13 20:19:35 +0000846 if (!Operands.empty())
847 OS << ", ";
848 Operands.PrintArguments(OS);
849 OS << ");\n";
850 }
851 OS << " default: return 0;\n";
852 OS << " }\n";
853 OS << "}\n";
854 OS << "\n";
855 }
Jim Grosbachcfb7b912013-08-29 22:41:39 +0000856
Chris Lattner07add492011-04-18 06:22:33 +0000857 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman44003cc2008-08-26 21:21:20 +0000858}
859
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000860namespace llvm {
861
862void EmitFastISel(RecordKeeper &RK, raw_ostream &OS) {
863 CodeGenDAGPatterns CGP(RK);
Dan Gohman44003cc2008-08-26 21:21:20 +0000864 const CodeGenTarget &Target = CGP.getTargetInfo();
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000865 emitSourceFileHeader("\"Fast\" Instruction Selector for the " +
Matthias Braun4a86d452016-12-04 05:48:16 +0000866 Target.getName().str() + " target", OS);
Dan Gohman44003cc2008-08-26 21:21:20 +0000867
868 // Determine the target's namespace name.
Craig Topper15e3de12017-07-07 06:22:36 +0000869 StringRef InstNS = Target.getInstNamespace();
870 assert(!InstNS.empty() && "Can't determine target-specific namespace!");
Dan Gohman44003cc2008-08-26 21:21:20 +0000871
Dan Gohman44003cc2008-08-26 21:21:20 +0000872 FastISelMap F(InstNS);
Chris Lattner07add492011-04-18 06:22:33 +0000873 F.collectPatterns(CGP);
874 F.printImmediatePredicates(OS);
875 F.printFunctionDefinitions(OS);
Dan Gohman71706232008-08-21 00:19:05 +0000876}
877
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000878} // End llvm namespace