blob: 5134b684c6f9cdb5bfa5e458fa30b28d7bf3d685 [file] [log] [blame]
Michael Kuperstein6d17dbe2017-01-30 19:03:26 +00001///===- FastISelEmitter.cpp - Generate an instruction selector -------------===//
Dan Gohmanb0cf29c2008-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 Gohman5ec9efd2008-09-30 20:48:29 +000010// This tablegen backend emits code for use by the "fast" instruction
11// selection algorithm. See the comments at the top of
12// lib/CodeGen/SelectionDAG/FastISel.cpp for background.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000013//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000014// This file scans through the target's tablegen instruction-info files
15// and extracts instructions with obvious-looking patterns, and it emits
16// code to look up these instructions by type and operator.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000017//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000018//===----------------------------------------------------------------------===//
19
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000020#include "CodeGenDAGPatterns.h"
Bob Wilson52c08fd2014-10-01 22:44:01 +000021#include "llvm/ADT/StringSwitch.h"
Chad Rosier36a300a2011-06-07 20:41:31 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000024#include "llvm/TableGen/Error.h"
25#include "llvm/TableGen/Record.h"
26#include "llvm/TableGen/TableGenBackend.h"
Benjamin Kramer14aae012016-05-27 14:27:24 +000027#include <utility>
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000028using namespace llvm;
29
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000030
Owen Anderson667d8f72008-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 Olesen6f36fa92012-06-11 15:37:55 +000034namespace {
Owen Anderson667d8f72008-08-29 17:45:56 +000035struct InstructionMemo {
36 std::string Name;
37 const CodeGenRegisterClass *RC;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +000038 std::string SubRegNo;
Florian Hahne4c36d22018-05-29 17:40:03 +000039 std::vector<std::string> PhysRegs;
Bill Schmidt6536b832014-11-14 21:05:45 +000040 std::string PredicateCheck;
Florian Hahne4c36d22018-05-29 17:40:03 +000041
Simon Pilgrim24696e22018-08-28 11:10:27 +000042 InstructionMemo(StringRef Name, const CodeGenRegisterClass *RC,
43 std::string SubRegNo, std::vector<std::string> PhysRegs,
44 std::string PredicateCheck)
45 : Name(Name), RC(RC), SubRegNo(std::move(SubRegNo)),
46 PhysRegs(std::move(PhysRegs)),
47 PredicateCheck(std::move(PredicateCheck)) {}
Florian Hahn98de50d2018-05-29 18:34:42 +000048
Florian Hahne4c36d22018-05-29 17:40:03 +000049 // Make sure we do not copy InstructionMemo.
50 InstructionMemo(const InstructionMemo &Other) = delete;
51 InstructionMemo(InstructionMemo &&Other) = default;
Owen Anderson667d8f72008-08-29 17:45:56 +000052};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000053} // End anonymous namespace
54
Chris Lattner1518afd2011-04-18 06:22:33 +000055/// ImmPredicateSet - This uniques predicates (represented as a string) and
56/// gives them unique (small) integer ID's that start at 0.
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000057namespace {
Chris Lattner1518afd2011-04-18 06:22:33 +000058class ImmPredicateSet {
59 DenseMap<TreePattern *, unsigned> ImmIDs;
60 std::vector<TreePredicateFn> PredsByName;
61public:
Jim Grosbachdd462302013-08-29 22:41:39 +000062
Chris Lattner1518afd2011-04-18 06:22:33 +000063 unsigned getIDFor(TreePredicateFn Pred) {
64 unsigned &Entry = ImmIDs[Pred.getOrigPatFragRecord()];
65 if (Entry == 0) {
66 PredsByName.push_back(Pred);
67 Entry = PredsByName.size();
68 }
69 return Entry-1;
70 }
Jim Grosbachdd462302013-08-29 22:41:39 +000071
Chris Lattner1518afd2011-04-18 06:22:33 +000072 const TreePredicateFn &getPredicate(unsigned i) {
73 assert(i < PredsByName.size());
74 return PredsByName[i];
75 }
Jim Grosbachdd462302013-08-29 22:41:39 +000076
Chris Lattner1518afd2011-04-18 06:22:33 +000077 typedef std::vector<TreePredicateFn>::const_iterator iterator;
78 iterator begin() const { return PredsByName.begin(); }
79 iterator end() const { return PredsByName.end(); }
Jim Grosbachdd462302013-08-29 22:41:39 +000080
Chris Lattner1518afd2011-04-18 06:22:33 +000081};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000082} // End anonymous namespace
Owen Anderson667d8f72008-08-29 17:45:56 +000083
Dan Gohman04b7dfb2008-08-19 18:06:12 +000084/// OperandsSignature - This class holds a description of a list of operand
85/// types. It has utility methods for emitting text based on the operands.
86///
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000087namespace {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000088struct OperandsSignature {
Chris Lattner9bfd5f32011-04-17 23:29:05 +000089 class OpKind {
90 enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 };
91 char Repr;
92 public:
Jim Grosbachdd462302013-08-29 22:41:39 +000093
Chris Lattner9bfd5f32011-04-17 23:29:05 +000094 OpKind() : Repr(OK_Invalid) {}
Jim Grosbachdd462302013-08-29 22:41:39 +000095
Chris Lattner9bfd5f32011-04-17 23:29:05 +000096 bool operator<(OpKind RHS) const { return Repr < RHS.Repr; }
Chris Lattner1518afd2011-04-18 06:22:33 +000097 bool operator==(OpKind RHS) const { return Repr == RHS.Repr; }
Chris Lattner9bfd5f32011-04-17 23:29:05 +000098
99 static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; }
100 static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; }
Chris Lattner1518afd2011-04-18 06:22:33 +0000101 static OpKind getImm(unsigned V) {
102 assert((unsigned)OK_Imm+V < 128 &&
103 "Too many integer predicates for the 'Repr' char");
104 OpKind K; K.Repr = OK_Imm+V; return K;
105 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000106
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000107 bool isReg() const { return Repr == OK_Reg; }
108 bool isFP() const { return Repr == OK_FP; }
Chris Lattner1518afd2011-04-18 06:22:33 +0000109 bool isImm() const { return Repr >= OK_Imm; }
Jim Grosbachdd462302013-08-29 22:41:39 +0000110
Chris Lattner1518afd2011-04-18 06:22:33 +0000111 unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; }
Jim Grosbachdd462302013-08-29 22:41:39 +0000112
Chris Lattner1518afd2011-04-18 06:22:33 +0000113 void printManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
114 bool StripImmCodes) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000115 if (isReg())
116 OS << 'r';
117 else if (isFP())
118 OS << 'f';
Chris Lattner1518afd2011-04-18 06:22:33 +0000119 else {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000120 OS << 'i';
Chris Lattner1518afd2011-04-18 06:22:33 +0000121 if (!StripImmCodes)
122 if (unsigned Code = getImmCode())
123 OS << "_" << ImmPredicates.getPredicate(Code-1).getFnName();
124 }
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000125 }
126 };
Jim Grosbachdd462302013-08-29 22:41:39 +0000127
128
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000129 SmallVector<OpKind, 3> Operands;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000130
131 bool operator<(const OperandsSignature &O) const {
132 return Operands < O.Operands;
133 }
Chris Lattner1518afd2011-04-18 06:22:33 +0000134 bool operator==(const OperandsSignature &O) const {
135 return Operands == O.Operands;
136 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000137
138 bool empty() const { return Operands.empty(); }
139
Chris Lattner1518afd2011-04-18 06:22:33 +0000140 bool hasAnyImmediateCodes() const {
141 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
142 if (Operands[i].isImm() && Operands[i].getImmCode() != 0)
143 return true;
144 return false;
145 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000146
Chris Lattner1518afd2011-04-18 06:22:33 +0000147 /// getWithoutImmCodes - Return a copy of this with any immediate codes forced
148 /// to zero.
149 OperandsSignature getWithoutImmCodes() const {
150 OperandsSignature Result;
151 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
152 if (!Operands[i].isImm())
153 Result.Operands.push_back(Operands[i]);
154 else
155 Result.Operands.push_back(OpKind::getImm(0));
156 return Result;
157 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000158
Chris Lattner1518afd2011-04-18 06:22:33 +0000159 void emitImmediatePredicate(raw_ostream &OS, ImmPredicateSet &ImmPredicates) {
160 bool EmittedAnything = false;
161 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
162 if (!Operands[i].isImm()) continue;
Jim Grosbachdd462302013-08-29 22:41:39 +0000163
Chris Lattner1518afd2011-04-18 06:22:33 +0000164 unsigned Code = Operands[i].getImmCode();
165 if (Code == 0) continue;
Jim Grosbachdd462302013-08-29 22:41:39 +0000166
Chris Lattner1518afd2011-04-18 06:22:33 +0000167 if (EmittedAnything)
168 OS << " &&\n ";
Jim Grosbachdd462302013-08-29 22:41:39 +0000169
Chris Lattner1518afd2011-04-18 06:22:33 +0000170 TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1);
Jim Grosbachdd462302013-08-29 22:41:39 +0000171
Chris Lattner1518afd2011-04-18 06:22:33 +0000172 // Emit the type check.
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +0000173 TreePattern *TP = PredFn.getOrigPatFragRecord();
174 ValueTypeByHwMode VVT = TP->getTree(0)->getType(0);
175 assert(VVT.isSimple() &&
176 "Cannot use variable value types with fast isel");
177 OS << "VT == " << getEnumName(VVT.getSimple().SimpleTy) << " && ";
Jim Grosbachdd462302013-08-29 22:41:39 +0000178
Chris Lattner1518afd2011-04-18 06:22:33 +0000179 OS << PredFn.getFnName() << "(imm" << i <<')';
180 EmittedAnything = true;
181 }
182 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000183
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000184 /// initialize - Examine the given pattern and initialize the contents
185 /// of the Operands array accordingly. Return true if all the operands
186 /// are supported, false otherwise.
187 ///
Chris Lattner602fc062011-04-17 20:23:29 +0000188 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
Chris Lattner1518afd2011-04-18 06:22:33 +0000189 MVT::SimpleValueType VT,
Bill Schmidtd35da502013-05-22 20:45:11 +0000190 ImmPredicateSet &ImmediatePredicates,
191 const CodeGenRegisterClass *OrigDstRC) {
Chris Lattner1518afd2011-04-18 06:22:33 +0000192 if (InstPatNode->isLeaf())
193 return false;
Jim Grosbachdd462302013-08-29 22:41:39 +0000194
Chris Lattner1518afd2011-04-18 06:22:33 +0000195 if (InstPatNode->getOperator()->getName() == "imm") {
196 Operands.push_back(OpKind::getImm(0));
197 return true;
198 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000199
Chris Lattner1518afd2011-04-18 06:22:33 +0000200 if (InstPatNode->getOperator()->getName() == "fpimm") {
201 Operands.push_back(OpKind::getFP());
202 return true;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000203 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000204
Craig Topper095734c2014-04-15 07:20:03 +0000205 const CodeGenRegisterClass *DstRC = nullptr;
Jim Grosbach45258f52010-12-07 19:36:07 +0000206
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000207 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
Florian Hahn74dff3b2018-06-14 20:32:58 +0000208 TreePatternNode *Op = InstPatNode->getChild(i);
Jim Grosbach45258f52010-12-07 19:36:07 +0000209
Chris Lattner1518afd2011-04-18 06:22:33 +0000210 // Handle imm operands specially.
Florian Hahn74dff3b2018-06-14 20:32:58 +0000211 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") {
Chris Lattner1518afd2011-04-18 06:22:33 +0000212 unsigned PredNo = 0;
Nicolai Haehnle98272e42018-11-30 14:15:13 +0000213 if (!Op->getPredicateCalls().empty()) {
214 TreePredicateFn PredFn = Op->getPredicateCalls()[0].Fn;
Chris Lattner1518afd2011-04-18 06:22:33 +0000215 // If there is more than one predicate weighing in on this operand
216 // then we don't handle it. This doesn't typically happen for
217 // immediates anyway.
Nicolai Haehnle98272e42018-11-30 14:15:13 +0000218 if (Op->getPredicateCalls().size() > 1 ||
219 !PredFn.isImmediatePattern() || PredFn.usesOperands())
Chris Lattner202a7a12011-04-18 06:36:55 +0000220 return false;
221 // Ignore any instruction with 'FastIselShouldIgnore', these are
222 // not needed and just bloat the fast instruction selector. For
223 // example, X86 doesn't need to generate code to match ADD16ri8 since
224 // ADD16ri will do just fine.
225 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord();
226 if (Rec->getValueAsBit("FastIselShouldIgnore"))
Chris Lattner1518afd2011-04-18 06:22:33 +0000227 return false;
Jim Grosbachdd462302013-08-29 22:41:39 +0000228
Chris Lattner202a7a12011-04-18 06:36:55 +0000229 PredNo = ImmediatePredicates.getIDFor(PredFn)+1;
Chris Lattner1518afd2011-04-18 06:22:33 +0000230 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000231
Chris Lattner1518afd2011-04-18 06:22:33 +0000232 Operands.push_back(OpKind::getImm(PredNo));
233 continue;
234 }
235
Jim Grosbachdd462302013-08-29 22:41:39 +0000236
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000237 // For now, filter out any operand with a predicate.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000238 // For now, filter out any operand with multiple values.
Nicolai Haehnle98272e42018-11-30 14:15:13 +0000239 if (!Op->getPredicateCalls().empty() || Op->getNumTypes() != 1)
Chris Lattnerd7349192010-03-19 21:37:09 +0000240 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000241
Florian Hahn74dff3b2018-06-14 20:32:58 +0000242 if (!Op->isLeaf()) {
243 if (Op->getOperator()->getName() == "fpimm") {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000244 Operands.push_back(OpKind::getFP());
Dale Johannesenedc87742009-05-21 22:25:49 +0000245 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000246 }
Dan Gohman833ddf82008-08-27 16:18:22 +0000247 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000248 return false;
249 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000250
Florian Hahn74dff3b2018-06-14 20:32:58 +0000251 assert(Op->hasConcreteType(0) && "Type infererence not done?");
Chris Lattner602fc062011-04-17 20:23:29 +0000252
253 // For now, all the operands must have the same type (if they aren't
254 // immediates). Note that this causes us to reject variable sized shifts
255 // on X86.
Florian Hahn74dff3b2018-06-14 20:32:58 +0000256 if (Op->getSimpleType(0) != VT)
Chris Lattner602fc062011-04-17 20:23:29 +0000257 return false;
258
Florian Hahn74dff3b2018-06-14 20:32:58 +0000259 DefInit *OpDI = dyn_cast<DefInit>(Op->getLeafValue());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000260 if (!OpDI)
261 return false;
262 Record *OpLeafRec = OpDI->getDef();
Jim Grosbachdd462302013-08-29 22:41:39 +0000263
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000264 // For now, the only other thing we accept is register operands.
Craig Topper095734c2014-04-15 07:20:03 +0000265 const CodeGenRegisterClass *RC = nullptr;
Owen Andersonbea6f612011-06-27 21:06:21 +0000266 if (OpLeafRec->isSubClassOf("RegisterOperand"))
267 OpLeafRec = OpLeafRec->getValueAsDef("RegClass");
Owen Anderson667d8f72008-08-29 17:45:56 +0000268 if (OpLeafRec->isSubClassOf("RegisterClass"))
269 RC = &Target.getRegisterClass(OpLeafRec);
270 else if (OpLeafRec->isSubClassOf("Register"))
Jakob Stoklund Olesen7b9cafd2011-06-15 00:20:40 +0000271 RC = Target.getRegBank().getRegClassForRegister(OpLeafRec);
Bill Schmidtd35da502013-05-22 20:45:11 +0000272 else if (OpLeafRec->isSubClassOf("ValueType")) {
273 RC = OrigDstRC;
274 } else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000275 return false;
Jim Grosbach45258f52010-12-07 19:36:07 +0000276
Eric Christopher2cfcad92010-08-24 23:21:59 +0000277 // For now, this needs to be a register class of some sort.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000278 if (!RC)
279 return false;
Eric Christopher2cfcad92010-08-24 23:21:59 +0000280
Eric Christopher53452602010-08-25 04:58:56 +0000281 // For now, all the operands must have the same register class or be
282 // a strict subclass of the destination.
Owen Andersonabb1f162008-08-26 01:22:59 +0000283 if (DstRC) {
Eric Christopher53452602010-08-25 04:58:56 +0000284 if (DstRC != RC && !DstRC->hasSubClass(RC))
Owen Andersonabb1f162008-08-26 01:22:59 +0000285 return false;
286 } else
287 DstRC = RC;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000288 Operands.push_back(OpKind::getReg());
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000289 }
290 return true;
291 }
292
Daniel Dunbar1a551802009-07-03 00:10:29 +0000293 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000294 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000295 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000296 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000297 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000298 OS << "uint64_t imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000299 } else if (Operands[i].isFP()) {
Cameron Zwarich82f00022012-01-07 08:18:37 +0000300 OS << "const ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000301 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000302 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000303 }
304 if (i + 1 != e)
305 OS << ", ";
306 }
307 }
308
Daniel Dunbar1a551802009-07-03 00:10:29 +0000309 void PrintArguments(raw_ostream &OS,
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000310 const std::vector<std::string> &PR) const {
Owen Anderson667d8f72008-08-29 17:45:56 +0000311 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000312 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000313 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000314 if (PR[i] != "")
315 // Implicit physical register operand.
316 continue;
317
318 if (PrintedArg)
319 OS << ", ";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000320 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000321 OS << "Op" << i << ", Op" << i << "IsKill";
Evan Cheng98d2d072008-09-08 08:39:33 +0000322 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000323 } else if (Operands[i].isImm()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000324 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000325 PrintedArg = true;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000326 } else if (Operands[i].isFP()) {
Owen Anderson667d8f72008-08-29 17:45:56 +0000327 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000328 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000329 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000330 llvm_unreachable("Unknown operand kind!");
Owen Anderson667d8f72008-08-29 17:45:56 +0000331 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000332 }
333 }
334
Daniel Dunbar1a551802009-07-03 00:10:29 +0000335 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000336 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000337 if (Operands[i].isReg()) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000338 OS << "Op" << i << ", Op" << i << "IsKill";
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000339 } else if (Operands[i].isImm()) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000340 OS << "imm" << i;
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000341 } else if (Operands[i].isFP()) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000342 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000343 } else {
Chad Rosier36a300a2011-06-07 20:41:31 +0000344 llvm_unreachable("Unknown operand kind!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000345 }
346 if (i + 1 != e)
347 OS << ", ";
348 }
349 }
350
Owen Anderson667d8f72008-08-29 17:45:56 +0000351
Chris Lattner1518afd2011-04-18 06:22:33 +0000352 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
353 ImmPredicateSet &ImmPredicates,
354 bool StripImmCodes = false) const {
Evan Cheng98d2d072008-09-08 08:39:33 +0000355 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
356 if (PR[i] != "")
357 // Implicit physical register operand. e.g. Instruction::Mul expect to
358 // select to a binary op. On x86, mul may take a single operand with
359 // the other operand being implicit. We must emit something that looks
Juergen Ributzkaecadea92014-09-03 20:56:59 +0000360 // like a binary instruction except for the very inner fastEmitInst_*
Evan Cheng98d2d072008-09-08 08:39:33 +0000361 // call.
362 continue;
Chris Lattner1518afd2011-04-18 06:22:33 +0000363 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Evan Cheng98d2d072008-09-08 08:39:33 +0000364 }
365 }
366
Chris Lattner1518afd2011-04-18 06:22:33 +0000367 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
368 bool StripImmCodes = false) const {
Chris Lattner9bfd5f32011-04-17 23:29:05 +0000369 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
Chris Lattner1518afd2011-04-18 06:22:33 +0000370 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000371 }
372};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000373} // End anonymous namespace
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000374
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000375namespace {
Dan Gohman72d63af2008-08-26 21:21:20 +0000376class FastISelMap {
Simon Pilgrim89f71162017-10-06 15:33:55 +0000377 // A multimap is needed instead of a "plain" map because the key is
Bill Schmidt6536b832014-11-14 21:05:45 +0000378 // the instruction's complexity (an int) and they are not unique.
379 typedef std::multimap<int, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
381 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000382 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
Jim Grosbach45258f52010-12-07 19:36:07 +0000383 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
Eric Christopherecfa0792010-07-26 17:53:07 +0000384 OperandsOpcodeTypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000385
386 OperandsOpcodeTypeRetPredMap SimplePatterns;
387
Simon Pilgrim89f71162017-10-06 15:33:55 +0000388 // This is used to check that there are no duplicate predicates
Bill Schmidt6536b832014-11-14 21:05:45 +0000389 typedef std::multimap<std::string, bool> PredCheckMap;
390 typedef std::map<MVT::SimpleValueType, PredCheckMap> RetPredCheckMap;
391 typedef std::map<MVT::SimpleValueType, RetPredCheckMap> TypeRetPredCheckMap;
392 typedef std::map<std::string, TypeRetPredCheckMap> OpcodeTypeRetPredCheckMap;
393 typedef std::map<OperandsSignature, OpcodeTypeRetPredCheckMap>
394 OperandsOpcodeTypeRetPredCheckMap;
395
396 OperandsOpcodeTypeRetPredCheckMap SimplePatternsCheck;
397
Chris Lattner1518afd2011-04-18 06:22:33 +0000398 std::map<OperandsSignature, std::vector<OperandsSignature> >
399 SignaturesWithConstantForms;
Jim Grosbachdd462302013-08-29 22:41:39 +0000400
Craig Topperdc02fd02017-07-07 06:22:36 +0000401 StringRef InstNS;
Chris Lattner1518afd2011-04-18 06:22:33 +0000402 ImmPredicateSet ImmediatePredicates;
Dan Gohman72d63af2008-08-26 21:21:20 +0000403public:
Craig Topperdc02fd02017-07-07 06:22:36 +0000404 explicit FastISelMap(StringRef InstNS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000405
Chris Lattner1518afd2011-04-18 06:22:33 +0000406 void collectPatterns(CodeGenDAGPatterns &CGP);
407 void printImmediatePredicates(raw_ostream &OS);
408 void printFunctionDefinitions(raw_ostream &OS);
Simon Pilgrim89f71162017-10-06 15:33:55 +0000409private:
410 void emitInstructionCode(raw_ostream &OS,
Bill Schmidt6536b832014-11-14 21:05:45 +0000411 const OperandsSignature &Operands,
Simon Pilgrim89f71162017-10-06 15:33:55 +0000412 const PredMap &PM,
Bill Schmidt6536b832014-11-14 21:05:45 +0000413 const std::string &RetVTName);
Dan Gohman72d63af2008-08-26 21:21:20 +0000414};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000415} // End anonymous namespace
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000416
417static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
418 return CGP.getSDNodeInfo(Op).getEnumName();
419}
420
421static std::string getLegalCName(std::string OpName) {
422 std::string::size_type pos = OpName.find("::");
423 if (pos != std::string::npos)
424 OpName.replace(pos, 2, "_");
425 return OpName;
426}
427
Craig Topperdc02fd02017-07-07 06:22:36 +0000428FastISelMap::FastISelMap(StringRef instns) : InstNS(instns) {}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000429
Florian Hahn74dff3b2018-06-14 20:32:58 +0000430static std::string PhyRegForNode(TreePatternNode *Op,
Eli Friedman206a10c2011-04-29 21:58:31 +0000431 const CodeGenTarget &Target) {
432 std::string PhysReg;
433
Florian Hahn74dff3b2018-06-14 20:32:58 +0000434 if (!Op->isLeaf())
Eli Friedman206a10c2011-04-29 21:58:31 +0000435 return PhysReg;
436
Florian Hahn74dff3b2018-06-14 20:32:58 +0000437 Record *OpLeafRec = cast<DefInit>(Op->getLeafValue())->getDef();
Eli Friedman206a10c2011-04-29 21:58:31 +0000438 if (!OpLeafRec->isSubClassOf("Register"))
439 return PhysReg;
440
Sean Silva3f7b7f82012-10-10 20:24:47 +0000441 PhysReg += cast<StringInit>(OpLeafRec->getValue("Namespace")->getValue())
442 ->getValue();
Eli Friedman206a10c2011-04-29 21:58:31 +0000443 PhysReg += "::";
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000444 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName();
Eli Friedman206a10c2011-04-29 21:58:31 +0000445 return PhysReg;
446}
447
Chris Lattner1518afd2011-04-18 06:22:33 +0000448void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000449 const CodeGenTarget &Target = CGP.getTargetInfo();
450
Dan Gohman0bfb7522008-08-22 00:28:15 +0000451 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000452 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
453 E = CGP.ptm_end(); I != E; ++I) {
454 const PatternToMatch &Pattern = *I;
455
456 // For now, just look at Instructions, so that we don't have to worry
457 // about emitting multiple instructions for a pattern.
458 TreePatternNode *Dst = Pattern.getDstPattern();
459 if (Dst->isLeaf()) continue;
460 Record *Op = Dst->getOperator();
461 if (!Op->isSubClassOf("Instruction"))
462 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000463 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Chris Lattnera90dbc12011-04-17 22:24:13 +0000464 if (II.Operands.empty())
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000465 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000466
Simon Dardisf690e9b2018-05-22 14:36:58 +0000467 // Allow instructions to be marked as unavailable for FastISel for
468 // certain cases, i.e. an ISA has two 'and' instruction which differ
469 // by what registers they can use but are otherwise identical for
470 // codegen purposes.
471 if (II.FastISelShouldIgnore)
472 continue;
473
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000474 // For now, ignore multi-instruction patterns.
475 bool MultiInsts = false;
476 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
Florian Hahn74dff3b2018-06-14 20:32:58 +0000477 TreePatternNode *ChildOp = Dst->getChild(i);
478 if (ChildOp->isLeaf())
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000479 continue;
Florian Hahn74dff3b2018-06-14 20:32:58 +0000480 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000481 MultiInsts = true;
482 break;
483 }
484 }
485 if (MultiInsts)
486 continue;
487
Dan Gohman379cad42008-08-19 20:36:33 +0000488 // For now, ignore instructions where the first operand is not an
489 // output register.
Craig Topper095734c2014-04-15 07:20:03 +0000490 const CodeGenRegisterClass *DstRC = nullptr;
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000491 std::string SubRegNo;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000492 if (Op->getName() != "EXTRACT_SUBREG") {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000493 Record *Op0Rec = II.Operands[0].Rec;
Owen Andersonbea6f612011-06-27 21:06:21 +0000494 if (Op0Rec->isSubClassOf("RegisterOperand"))
495 Op0Rec = Op0Rec->getValueAsDef("RegClass");
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000496 if (!Op0Rec->isSubClassOf("RegisterClass"))
497 continue;
498 DstRC = &Target.getRegisterClass(Op0Rec);
499 if (!DstRC)
500 continue;
501 } else {
Eric Christopher07fdd892010-07-21 22:07:19 +0000502 // If this isn't a leaf, then continue since the register classes are
503 // a bit too complicated for now.
Florian Hahn74dff3b2018-06-14 20:32:58 +0000504 if (!Dst->getChild(1)->isLeaf()) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000505
Florian Hahn74dff3b2018-06-14 20:32:58 +0000506 DefInit *SR = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue());
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +0000507 if (SR)
508 SubRegNo = getQualifiedName(SR->getDef());
509 else
Florian Hahn74dff3b2018-06-14 20:32:58 +0000510 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000511 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000512
513 // Inspect the pattern.
514 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
515 if (!InstPatNode) continue;
516 if (InstPatNode->isLeaf()) continue;
517
Chris Lattner084df622010-03-24 00:41:19 +0000518 // Ignore multiple result nodes for now.
519 if (InstPatNode->getNumTypes() > 1) continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000520
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000521 Record *InstPatOp = InstPatNode->getOperator();
522 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattnerd7349192010-03-19 21:37:09 +0000523 MVT::SimpleValueType RetVT = MVT::isVoid;
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +0000524 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getSimpleType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 MVT::SimpleValueType VT = RetVT;
Chris Lattnerd7349192010-03-19 21:37:09 +0000526 if (InstPatNode->getNumChildren()) {
Florian Hahn74dff3b2018-06-14 20:32:58 +0000527 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
528 VT = InstPatNode->getChild(0)->getSimpleType(0);
Chris Lattnerd7349192010-03-19 21:37:09 +0000529 }
Dan Gohmanf4137b52008-08-19 20:30:54 +0000530
531 // For now, filter out any instructions with predicates.
Nicolai Haehnle98272e42018-11-30 14:15:13 +0000532 if (!InstPatNode->getPredicateCalls().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000533 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000534
Dan Gohman379cad42008-08-19 20:36:33 +0000535 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000536 OperandsSignature Operands;
Bill Schmidtd35da502013-05-22 20:45:11 +0000537 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates,
538 DstRC))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000539 continue;
Jim Grosbach45258f52010-12-07 19:36:07 +0000540
Florian Hahne4c36d22018-05-29 17:40:03 +0000541 std::vector<std::string> PhysRegInputs;
Eli Friedman206a10c2011-04-29 21:58:31 +0000542 if (InstPatNode->getOperator()->getName() == "imm" ||
Eric Christopher691a4882011-08-23 15:42:35 +0000543 InstPatNode->getOperator()->getName() == "fpimm")
Florian Hahne4c36d22018-05-29 17:40:03 +0000544 PhysRegInputs.push_back("");
Eli Friedman206a10c2011-04-29 21:58:31 +0000545 else {
546 // Compute the PhysRegs used by the given pattern, and check that
547 // the mapping from the src to dst patterns is simple.
548 bool FoundNonSimplePattern = false;
549 unsigned DstIndex = 0;
Owen Anderson667d8f72008-08-29 17:45:56 +0000550 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
Eli Friedman206a10c2011-04-29 21:58:31 +0000551 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
552 if (PhysReg.empty()) {
553 if (DstIndex >= Dst->getNumChildren() ||
Florian Hahn74dff3b2018-06-14 20:32:58 +0000554 Dst->getChild(DstIndex)->getName() !=
555 InstPatNode->getChild(i)->getName()) {
Eli Friedman206a10c2011-04-29 21:58:31 +0000556 FoundNonSimplePattern = true;
557 break;
Owen Anderson667d8f72008-08-29 17:45:56 +0000558 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000559 ++DstIndex;
Owen Anderson667d8f72008-08-29 17:45:56 +0000560 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000561
Florian Hahne4c36d22018-05-29 17:40:03 +0000562 PhysRegInputs.push_back(PhysReg);
Owen Anderson667d8f72008-08-29 17:45:56 +0000563 }
Eli Friedman206a10c2011-04-29 21:58:31 +0000564
565 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
566 FoundNonSimplePattern = true;
567
568 if (FoundNonSimplePattern)
569 continue;
570 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000571
Bob Wilson52c08fd2014-10-01 22:44:01 +0000572 // Check if the operands match one of the patterns handled by FastISel.
573 std::string ManglingSuffix;
574 raw_string_ostream SuffixOS(ManglingSuffix);
575 Operands.PrintManglingSuffix(SuffixOS, ImmediatePredicates, true);
576 SuffixOS.flush();
577 if (!StringSwitch<bool>(ManglingSuffix)
Peter Collingbourne81c297e2016-10-05 19:25:20 +0000578 .Cases("", "r", "rr", "ri", "i", "f", true)
Bob Wilson52c08fd2014-10-01 22:44:01 +0000579 .Default(false))
580 continue;
581
Dan Gohman22bb3112008-08-22 00:20:26 +0000582 // Get the predicate that guards this pattern.
583 std::string PredicateCheck = Pattern.getPredicateCheck();
584
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000585 // Ok, we found a pattern that we can handle. Remember it.
Florian Hahn98de50d2018-05-29 18:34:42 +0000586 InstructionMemo Memo(
Dan Gohman520b50c2008-08-21 00:35:26 +0000587 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000588 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000589 SubRegNo,
Bill Schmidt6536b832014-11-14 21:05:45 +0000590 PhysRegInputs,
591 PredicateCheck
Florian Hahn98de50d2018-05-29 18:34:42 +0000592 );
Simon Pilgrim89f71162017-10-06 15:33:55 +0000593
Bill Schmidt6536b832014-11-14 21:05:45 +0000594 int complexity = Pattern.getPatternComplexity(CGP);
Jim Grosbachdd462302013-08-29 22:41:39 +0000595
Bill Schmidt6536b832014-11-14 21:05:45 +0000596 if (SimplePatternsCheck[Operands][OpcodeName][VT]
597 [RetVT].count(PredicateCheck)) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000598 PrintFatalError(Pattern.getSrcRecord()->getLoc(),
Bill Schmidt6536b832014-11-14 21:05:45 +0000599 "Duplicate predicate in FastISel table!");
600 }
601 SimplePatternsCheck[Operands][OpcodeName][VT][RetVT].insert(
602 std::make_pair(PredicateCheck, true));
Jim Grosbach997759a2010-12-07 23:05:49 +0000603
Bill Schmidt6536b832014-11-14 21:05:45 +0000604 // Note: Instructions with the same complexity will appear in the order
605 // that they are encountered.
Florian Hahne4c36d22018-05-29 17:40:03 +0000606 SimplePatterns[Operands][OpcodeName][VT][RetVT].emplace(complexity,
607 std::move(Memo));
Jim Grosbachdd462302013-08-29 22:41:39 +0000608
Chris Lattner1518afd2011-04-18 06:22:33 +0000609 // If any of the operands were immediates with predicates on them, strip
610 // them down to a signature that doesn't have predicates so that we can
611 // associate them with the stripped predicate version.
612 if (Operands.hasAnyImmediateCodes()) {
613 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
614 .push_back(Operands);
615 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000616 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000617}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000618
Chris Lattner1518afd2011-04-18 06:22:33 +0000619void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
620 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
621 return;
Jim Grosbachdd462302013-08-29 22:41:39 +0000622
Chris Lattner1518afd2011-04-18 06:22:33 +0000623 OS << "\n// FastEmit Immediate Predicate functions.\n";
624 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
625 E = ImmediatePredicates.end(); I != E; ++I) {
626 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
627 OS << I->getImmediatePredicateCode() << "\n}\n";
628 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000629
Chris Lattner1518afd2011-04-18 06:22:33 +0000630 OS << "\n\n";
631}
632
Simon Pilgrim89f71162017-10-06 15:33:55 +0000633void FastISelMap::emitInstructionCode(raw_ostream &OS,
Bill Schmidt6536b832014-11-14 21:05:45 +0000634 const OperandsSignature &Operands,
Simon Pilgrim89f71162017-10-06 15:33:55 +0000635 const PredMap &PM,
Bill Schmidt6536b832014-11-14 21:05:45 +0000636 const std::string &RetVTName) {
637 // Emit code for each possible instruction. There may be
638 // multiple if there are subtarget concerns. A reverse iterator
639 // is used to produce the ones with highest complexity first.
640
641 bool OneHadNoPredicate = false;
642 for (PredMap::const_reverse_iterator PI = PM.rbegin(), PE = PM.rend();
643 PI != PE; ++PI) {
644 const InstructionMemo &Memo = PI->second;
645 std::string PredicateCheck = Memo.PredicateCheck;
646
647 if (PredicateCheck.empty()) {
648 assert(!OneHadNoPredicate &&
649 "Multiple instructions match and more than one had "
650 "no predicate!");
651 OneHadNoPredicate = true;
652 } else {
653 if (OneHadNoPredicate) {
Michael Kuperstein6d17dbe2017-01-30 19:03:26 +0000654 PrintFatalError("Multiple instructions match and one with no "
655 "predicate came before one with a predicate! "
656 "name:" + Memo.Name + " predicate: " + PredicateCheck);
Bill Schmidt6536b832014-11-14 21:05:45 +0000657 }
658 OS << " if (" + PredicateCheck + ") {\n";
659 OS << " ";
660 }
661
Florian Hahne4c36d22018-05-29 17:40:03 +0000662 for (unsigned i = 0; i < Memo.PhysRegs.size(); ++i) {
663 if (Memo.PhysRegs[i] != "")
Bill Schmidt6536b832014-11-14 21:05:45 +0000664 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, "
Florian Hahne4c36d22018-05-29 17:40:03 +0000665 << "TII.get(TargetOpcode::COPY), " << Memo.PhysRegs[i]
666 << ").addReg(Op" << i << ");\n";
Bill Schmidt6536b832014-11-14 21:05:45 +0000667 }
668
669 OS << " return fastEmitInst_";
670 if (Memo.SubRegNo.empty()) {
Florian Hahne4c36d22018-05-29 17:40:03 +0000671 Operands.PrintManglingSuffix(OS, Memo.PhysRegs, ImmediatePredicates,
672 true);
Craig Topperdc02fd02017-07-07 06:22:36 +0000673 OS << "(" << InstNS << "::" << Memo.Name << ", ";
674 OS << "&" << InstNS << "::" << Memo.RC->getName() << "RegClass";
Bill Schmidt6536b832014-11-14 21:05:45 +0000675 if (!Operands.empty())
676 OS << ", ";
Florian Hahne4c36d22018-05-29 17:40:03 +0000677 Operands.PrintArguments(OS, Memo.PhysRegs);
Bill Schmidt6536b832014-11-14 21:05:45 +0000678 OS << ");\n";
679 } else {
680 OS << "extractsubreg(" << RetVTName
681 << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
682 }
683
684 if (!PredicateCheck.empty()) {
685 OS << " }\n";
686 }
687 }
688 // Return 0 if all of the possibilities had predicates but none
689 // were satisfied.
690 if (!OneHadNoPredicate)
691 OS << " return 0;\n";
692 OS << "}\n";
693 OS << "\n";
694}
695
Chris Lattner1518afd2011-04-18 06:22:33 +0000696
697void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000698 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000699 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000700 OE = SimplePatterns.end(); OI != OE; ++OI) {
701 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000702 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000703
Owen Anderson7b2e5792008-08-25 23:43:09 +0000704 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000705 I != E; ++I) {
706 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000707 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000708
709 OS << "// FastEmit functions for " << Opcode << ".\n";
710 OS << "\n";
711
712 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000713 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000714 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000716 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000717 if (RM.size() != 1) {
718 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
719 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000721 const PredMap &PM = RI->second;
Dan Gohman22bb3112008-08-22 00:20:26 +0000722
Juergen Ributzkaecadea92014-09-03 20:56:59 +0000723 OS << "unsigned fastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000724 << getLegalCName(Opcode)
725 << "_" << getLegalCName(getName(VT))
726 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000727 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000728 OS << "(";
729 Operands.PrintParameters(OS);
730 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000731
Bill Schmidt6536b832014-11-14 21:05:45 +0000732 emitInstructionCode(OS, Operands, PM, getName(RetVT));
Owen Anderson71669e52008-08-26 00:42:26 +0000733 }
Jim Grosbach45258f52010-12-07 19:36:07 +0000734
Owen Anderson71669e52008-08-26 00:42:26 +0000735 // Emit one function for the type that demultiplexes on return type.
Juergen Ributzkaecadea92014-09-03 20:56:59 +0000736 OS << "unsigned fastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000737 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000738 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000739 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000741 if (!Operands.empty())
742 OS << ", ";
743 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000745 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
746 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 MVT::SimpleValueType RetVT = RI->first;
Juergen Ributzkaecadea92014-09-03 20:56:59 +0000748 OS << " case " << getName(RetVT) << ": return fastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000749 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
750 << "_" << getLegalCName(getName(RetVT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000751 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson71669e52008-08-26 00:42:26 +0000752 OS << "(";
753 Operands.PrintArguments(OS);
754 OS << ");\n";
755 }
756 OS << " default: return 0;\n}\n}\n\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000757
Owen Anderson71669e52008-08-26 00:42:26 +0000758 } else {
759 // Non-variadic return type.
Juergen Ributzkaecadea92014-09-03 20:56:59 +0000760 OS << "unsigned fastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000761 << getLegalCName(Opcode) << "_"
762 << getLegalCName(getName(VT)) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000763 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000765 if (!Operands.empty())
766 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000767 Operands.PrintParameters(OS);
768 OS << ") {\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000769
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000771 << ")\n return 0;\n";
Jim Grosbach45258f52010-12-07 19:36:07 +0000772
Owen Anderson71669e52008-08-26 00:42:26 +0000773 const PredMap &PM = RM.begin()->second;
Jim Grosbach45258f52010-12-07 19:36:07 +0000774
Bill Schmidt6536b832014-11-14 21:05:45 +0000775 emitInstructionCode(OS, Operands, PM, "RetVT");
Dan Gohman22bb3112008-08-22 00:20:26 +0000776 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000777 }
778
779 // Emit one function for the opcode that demultiplexes based on the type.
Juergen Ributzkaecadea92014-09-03 20:56:59 +0000780 OS << "unsigned fastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000781 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000782 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000784 if (!Operands.empty())
785 OS << ", ";
786 Operands.PrintParameters(OS);
787 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000789 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000790 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000792 std::string TypeName = getName(VT);
Juergen Ributzkaecadea92014-09-03 20:56:59 +0000793 OS << " case " << TypeName << ": return fastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000794 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000795 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000796 OS << "(RetVT";
797 if (!Operands.empty())
798 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000799 Operands.PrintArguments(OS);
800 OS << ");\n";
801 }
802 OS << " default: return 0;\n";
803 OS << " }\n";
804 OS << "}\n";
805 OS << "\n";
806 }
807
Dan Gohman0bfb7522008-08-22 00:28:15 +0000808 OS << "// Top-level FastEmit function.\n";
809 OS << "\n";
810
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000811 // Emit one function for the operand signature that demultiplexes based
812 // on opcode and type.
Juergen Ributzkaecadea92014-09-03 20:56:59 +0000813 OS << "unsigned fastEmit_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000814 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000815 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000816 if (!Operands.empty())
817 OS << ", ";
818 Operands.PrintParameters(OS);
Bob Wilson52c08fd2014-10-01 22:44:01 +0000819 OS << ") ";
820 if (!Operands.hasAnyImmediateCodes())
821 OS << "override ";
822 OS << "{\n";
Jim Grosbachdd462302013-08-29 22:41:39 +0000823
Jim Grosbachff372dc2013-08-29 22:41:43 +0000824 // If there are any forms of this signature available that operate on
825 // constrained forms of the immediate (e.g., 32-bit sext immediate in a
Chris Lattner1518afd2011-04-18 06:22:33 +0000826 // 64-bit operand), check them first.
Jim Grosbachdd462302013-08-29 22:41:39 +0000827
Chris Lattner1518afd2011-04-18 06:22:33 +0000828 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
829 = SignaturesWithConstantForms.find(Operands);
830 if (MI != SignaturesWithConstantForms.end()) {
831 // Unique any duplicates out of the list.
Fangrui Songc4662642018-09-30 22:31:29 +0000832 llvm::sort(MI->second);
Chris Lattner1518afd2011-04-18 06:22:33 +0000833 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
834 MI->second.end());
Jim Grosbachdd462302013-08-29 22:41:39 +0000835
Chris Lattner1518afd2011-04-18 06:22:33 +0000836 // Check each in order it was seen. It would be nice to have a good
837 // relative ordering between them, but we're not going for optimality
838 // here.
839 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
840 OS << " if (";
841 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
Juergen Ributzkaecadea92014-09-03 20:56:59 +0000842 OS << ")\n if (unsigned Reg = fastEmit_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000843 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
844 OS << "(VT, RetVT, Opcode";
845 if (!MI->second[i].empty())
846 OS << ", ";
847 MI->second[i].PrintArguments(OS);
848 OS << "))\n return Reg;\n\n";
849 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000850
Chris Lattner1518afd2011-04-18 06:22:33 +0000851 // Done with this, remove it.
852 SignaturesWithConstantForms.erase(MI);
853 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000854
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000855 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000856 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000857 I != E; ++I) {
858 const std::string &Opcode = I->first;
859
Juergen Ributzkaecadea92014-09-03 20:56:59 +0000860 OS << " case " << Opcode << ": return fastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000861 << getLegalCName(Opcode) << "_";
Chris Lattner1518afd2011-04-18 06:22:33 +0000862 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000863 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000864 if (!Operands.empty())
865 OS << ", ";
866 Operands.PrintArguments(OS);
867 OS << ");\n";
868 }
869 OS << " default: return 0;\n";
870 OS << " }\n";
871 OS << "}\n";
872 OS << "\n";
873 }
Jim Grosbachdd462302013-08-29 22:41:39 +0000874
Chris Lattner1518afd2011-04-18 06:22:33 +0000875 // TODO: SignaturesWithConstantForms should be empty here.
Dan Gohman72d63af2008-08-26 21:21:20 +0000876}
877
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000878namespace llvm {
879
880void EmitFastISel(RecordKeeper &RK, raw_ostream &OS) {
881 CodeGenDAGPatterns CGP(RK);
Dan Gohman72d63af2008-08-26 21:21:20 +0000882 const CodeGenTarget &Target = CGP.getTargetInfo();
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000883 emitSourceFileHeader("\"Fast\" Instruction Selector for the " +
Matthias Braun0c517c82016-12-04 05:48:16 +0000884 Target.getName().str() + " target", OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000885
886 // Determine the target's namespace name.
Craig Topperdc02fd02017-07-07 06:22:36 +0000887 StringRef InstNS = Target.getInstNamespace();
888 assert(!InstNS.empty() && "Can't determine target-specific namespace!");
Dan Gohman72d63af2008-08-26 21:21:20 +0000889
Dan Gohman72d63af2008-08-26 21:21:20 +0000890 FastISelMap F(InstNS);
Chris Lattner1518afd2011-04-18 06:22:33 +0000891 F.collectPatterns(CGP);
892 F.printImmediatePredicates(OS);
893 F.printFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000894}
895
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000896} // End llvm namespace