blob: 0aff1aa6f948db2e4b66313cf0da7c06d4505730 [file] [log] [blame]
Hans Wennborgaa15bff2015-09-10 16:49:58 +00001//===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. --*- C++ -*-===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswelld3032032003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswelld3032032003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
10// This tablegen backend is responsible for emitting a description of the target
11// instruction set for the code generator.
12//
13//===----------------------------------------------------------------------===//
14
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000015#include "CodeGenDAGPatterns.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000016#include "CodeGenInstruction.h"
Andrew Trick87255e32012-07-07 04:00:00 +000017#include "CodeGenSchedule.h"
Chris Lattnerfce96032004-08-01 04:04:35 +000018#include "CodeGenTarget.h"
Andrea Di Biagio95140022018-05-25 15:55:37 +000019#include "PredicateExpander.h"
Craig Topper91773ab2012-04-01 18:14:14 +000020#include "SequenceToOffsetTable.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000021#include "TableGenBackends.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000022#include "llvm/ADT/ArrayRef.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000023#include "llvm/ADT/StringExtras.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000024#include "llvm/Support/Casting.h"
25#include "llvm/Support/raw_ostream.h"
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000026#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000027#include "llvm/TableGen/Record.h"
28#include "llvm/TableGen/TableGenBackend.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000029#include <cassert>
30#include <cstdint>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000031#include <map>
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000032#include <string>
33#include <utility>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000034#include <vector>
Hans Wennborgaa15bff2015-09-10 16:49:58 +000035
Chris Lattner68478662004-08-01 03:55:39 +000036using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000037
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000038namespace {
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000039
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000040class InstrInfoEmitter {
41 RecordKeeper &Records;
42 CodeGenDAGPatterns CDP;
Andrew Trick87255e32012-07-07 04:00:00 +000043 const CodeGenSchedModels &SchedModels;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000044
45public:
Andrew Trick87255e32012-07-07 04:00:00 +000046 InstrInfoEmitter(RecordKeeper &R):
47 Records(R), CDP(R), SchedModels(CDP.getTargetInfo().getSchedModels()) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000048
49 // run - Output the instruction set description.
50 void run(raw_ostream &OS);
51
52private:
53 void emitEnums(raw_ostream &OS);
54
55 typedef std::map<std::vector<std::string>, unsigned> OperandInfoMapTy;
Tom Stellardb162d942013-06-25 21:22:09 +000056
57 /// The keys of this map are maps which have OpName enum values as their keys
58 /// and instruction operand indices as their values. The values of this map
59 /// are lists of instruction names.
60 typedef std::map<std::map<unsigned, unsigned>,
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000061 std::vector<std::string>> OpNameMapTy;
Tom Stellardb162d942013-06-25 21:22:09 +000062 typedef std::map<std::string, unsigned>::iterator StrUintMapIter;
Andrea Di Biagio95140022018-05-25 15:55:37 +000063
64 /// Generate member functions in the target-specific GenInstrInfo class.
65 ///
66 /// This method is used to custom expand TIIPredicate definitions.
67 /// See file llvm/Target/TargetInstPredicates.td for a description of what is
68 /// a TIIPredicate and how to use it.
69 void emitTIIHelperMethods(raw_ostream &OS);
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +000070
71 /// Expand TIIPredicate definitions to functions that accept a const MCInst
72 /// reference.
73 void emitMCIIHelperMethods(raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000074 void emitRecord(const CodeGenInstruction &Inst, unsigned Num,
75 Record *InstrInfo,
76 std::map<std::vector<Record*>, unsigned> &EL,
77 const OperandInfoMapTy &OpInfo,
78 raw_ostream &OS);
Ahmed Bougacha31377592013-11-17 21:24:41 +000079 void emitOperandTypesEnum(raw_ostream &OS, const CodeGenTarget &Target);
Tom Stellardb162d942013-06-25 21:22:09 +000080 void initOperandMapData(
Craig Topper28851b62016-02-01 01:33:42 +000081 ArrayRef<const CodeGenInstruction *> NumberedInstructions,
Craig Topper86a9aee2017-07-07 06:22:35 +000082 StringRef Namespace,
Craig Topper65efcb42014-02-05 07:27:49 +000083 std::map<std::string, unsigned> &Operands,
84 OpNameMapTy &OperandMap);
Tom Stellardb162d942013-06-25 21:22:09 +000085 void emitOperandNameMappings(raw_ostream &OS, const CodeGenTarget &Target,
Craig Topper28851b62016-02-01 01:33:42 +000086 ArrayRef<const CodeGenInstruction*> NumberedInstructions);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000087
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000088 // Operand information.
89 void EmitOperandInfo(raw_ostream &OS, OperandInfoMapTy &OperandInfoIDs);
90 std::vector<std::string> GetOperandInfo(const CodeGenInstruction &Inst);
91};
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000092
Hans Wennborgaa15bff2015-09-10 16:49:58 +000093} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000094
Chris Lattner85467a12008-01-06 01:21:51 +000095static void PrintDefList(const std::vector<Record*> &Uses,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000096 unsigned Num, raw_ostream &OS) {
Craig Toppere5e035a32015-12-05 07:13:35 +000097 OS << "static const MCPhysReg ImplicitList" << Num << "[] = { ";
Craig Topper9cd0ff12016-02-16 02:48:30 +000098 for (Record *U : Uses)
99 OS << getQualifiedName(U) << ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000100 OS << "0 };\n";
101}
102
Chris Lattner626b89d2008-01-06 01:20:13 +0000103//===----------------------------------------------------------------------===//
Chris Lattner626b89d2008-01-06 01:20:13 +0000104// Operand Info Emission.
105//===----------------------------------------------------------------------===//
106
Chris Lattner33f5a512006-11-06 23:49:51 +0000107std::vector<std::string>
108InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) {
109 std::vector<std::string> Result;
Owen Andersona84be6c2011-06-27 21:06:21 +0000110
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000111 for (auto &Op : Inst.Operands) {
Chris Lattnerba7b3672006-11-10 02:01:40 +0000112 // Handle aggregate operands and normal operands the same way by expanding
113 // either case into a list of operands for this op.
Chris Lattnerd8adec72010-11-01 04:03:32 +0000114 std::vector<CGIOperandList::OperandInfo> OperandList;
Chris Lattner33f5a512006-11-06 23:49:51 +0000115
Chris Lattnerba7b3672006-11-10 02:01:40 +0000116 // This might be a multiple operand thing. Targets like X86 have
117 // registers in their multi-operand operands. It may also be an anonymous
118 // operand, which has a single operand, but no declared class for the
119 // operand.
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000120 DagInit *MIOI = Op.MIOperandInfo;
Owen Andersona84be6c2011-06-27 21:06:21 +0000121
Chris Lattnerba7b3672006-11-10 02:01:40 +0000122 if (!MIOI || MIOI->getNumArgs() == 0) {
123 // Single, anonymous, operand.
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000124 OperandList.push_back(Op);
Chris Lattner6bc03042005-11-19 07:05:57 +0000125 } else {
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000126 for (unsigned j = 0, e = Op.MINumOperands; j != e; ++j) {
127 OperandList.push_back(Op);
Chris Lattner33f5a512006-11-06 23:49:51 +0000128
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000129 auto *OpR = cast<DefInit>(MIOI->getArg(j))->getDef();
Chris Lattnerba7b3672006-11-10 02:01:40 +0000130 OperandList.back().Rec = OpR;
Chris Lattner6bc03042005-11-19 07:05:57 +0000131 }
Chris Lattnerd02bd5b2005-08-19 18:46:26 +0000132 }
Chris Lattnerba7b3672006-11-10 02:01:40 +0000133
134 for (unsigned j = 0, e = OperandList.size(); j != e; ++j) {
135 Record *OpR = OperandList[j].Rec;
136 std::string Res;
Owen Andersona84be6c2011-06-27 21:06:21 +0000137
138 if (OpR->isSubClassOf("RegisterOperand"))
139 OpR = OpR->getValueAsDef("RegClass");
Chris Lattnerba7b3672006-11-10 02:01:40 +0000140 if (OpR->isSubClassOf("RegisterClass"))
141 Res += getQualifiedName(OpR) + "RegClassID, ";
Chris Lattnerf3239532009-07-29 21:10:12 +0000142 else if (OpR->isSubClassOf("PointerLikeRegClass"))
143 Res += utostr(OpR->getValueAsInt("RegClassKind")) + ", ";
Chris Lattnerba7b3672006-11-10 02:01:40 +0000144 else
Dan Gohman882bb292010-06-18 18:13:55 +0000145 // -1 means the operand does not have a fixed register class.
146 Res += "-1, ";
Owen Andersona84be6c2011-06-27 21:06:21 +0000147
Chris Lattnerba7b3672006-11-10 02:01:40 +0000148 // Fill in applicable flags.
149 Res += "0";
Owen Andersona84be6c2011-06-27 21:06:21 +0000150
Chris Lattnerba7b3672006-11-10 02:01:40 +0000151 // Ptr value whose register class is resolved via callback.
Chris Lattner426bc7c2009-07-29 20:43:05 +0000152 if (OpR->isSubClassOf("PointerLikeRegClass"))
Evan Cheng6cc775f2011-06-28 19:10:37 +0000153 Res += "|(1<<MCOI::LookupPtrRegClass)";
Chris Lattnerba7b3672006-11-10 02:01:40 +0000154
155 // Predicate operands. Check to see if the original unexpanded operand
Tim Northover42180442013-08-22 09:57:11 +0000156 // was of type PredicateOp.
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000157 if (Op.Rec->isSubClassOf("PredicateOp"))
Evan Cheng6cc775f2011-06-28 19:10:37 +0000158 Res += "|(1<<MCOI::Predicate)";
Owen Andersona84be6c2011-06-27 21:06:21 +0000159
Evan Cheng6e3c7052007-07-10 18:05:01 +0000160 // Optional def operands. Check to see if the original unexpanded operand
161 // was of type OptionalDefOperand.
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000162 if (Op.Rec->isSubClassOf("OptionalDefOperand"))
Evan Cheng6cc775f2011-06-28 19:10:37 +0000163 Res += "|(1<<MCOI::OptionalDef)";
Evan Cheng6e3c7052007-07-10 18:05:01 +0000164
Craig Topperb3584992012-03-11 01:57:56 +0000165 // Fill in operand type.
Tom Stellard89b26102015-01-12 19:33:09 +0000166 Res += ", ";
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000167 assert(!Op.OperandType.empty() && "Invalid operand type.");
168 Res += Op.OperandType;
Craig Topperb3584992012-03-11 01:57:56 +0000169
Chris Lattnerba7b3672006-11-10 02:01:40 +0000170 // Fill in constraint info.
Chris Lattnera9dfb1b2010-02-10 01:45:28 +0000171 Res += ", ";
Owen Andersona84be6c2011-06-27 21:06:21 +0000172
Chris Lattnerd8adec72010-11-01 04:03:32 +0000173 const CGIOperandList::ConstraintInfo &Constraint =
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000174 Op.Constraints[j];
Chris Lattnera9dfb1b2010-02-10 01:45:28 +0000175 if (Constraint.isNone())
176 Res += "0";
177 else if (Constraint.isEarlyClobber())
Evan Cheng6cc775f2011-06-28 19:10:37 +0000178 Res += "(1 << MCOI::EARLY_CLOBBER)";
Chris Lattnera9dfb1b2010-02-10 01:45:28 +0000179 else {
180 assert(Constraint.isTied());
181 Res += "((" + utostr(Constraint.getTiedOperand()) +
Evan Cheng6cc775f2011-06-28 19:10:37 +0000182 " << 16) | (1 << MCOI::TIED_TO))";
Chris Lattnera9dfb1b2010-02-10 01:45:28 +0000183 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000184
Chris Lattnerba7b3672006-11-10 02:01:40 +0000185 Result.push_back(Res);
186 }
Chris Lattnerd02bd5b2005-08-19 18:46:26 +0000187 }
Evan Chengac79c7c2006-11-01 00:27:05 +0000188
Chris Lattnerd02bd5b2005-08-19 18:46:26 +0000189 return Result;
190}
191
Owen Andersona84be6c2011-06-27 21:06:21 +0000192void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS,
Chris Lattner626b89d2008-01-06 01:20:13 +0000193 OperandInfoMapTy &OperandInfoIDs) {
194 // ID #0 is for no operand info.
195 unsigned OperandListNum = 0;
196 OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum;
Owen Andersona84be6c2011-06-27 21:06:21 +0000197
Chris Lattner626b89d2008-01-06 01:20:13 +0000198 OS << "\n";
199 const CodeGenTarget &Target = CDP.getTargetInfo();
Craig Topper8cc904d2016-01-17 20:38:18 +0000200 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000201 std::vector<std::string> OperandInfo = GetOperandInfo(*Inst);
Chris Lattner626b89d2008-01-06 01:20:13 +0000202 unsigned &N = OperandInfoIDs[OperandInfo];
203 if (N != 0) continue;
Owen Andersona84be6c2011-06-27 21:06:21 +0000204
Chris Lattner626b89d2008-01-06 01:20:13 +0000205 N = ++OperandListNum;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000206 OS << "static const MCOperandInfo OperandInfo" << N << "[] = { ";
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000207 for (const std::string &Info : OperandInfo)
208 OS << "{ " << Info << " }, ";
Chris Lattner626b89d2008-01-06 01:20:13 +0000209 OS << "};\n";
210 }
211}
212
Tom Stellardb162d942013-06-25 21:22:09 +0000213/// Initialize data structures for generating operand name mappings.
214///
215/// \param Operands [out] A map used to generate the OpName enum with operand
216/// names as its keys and operand enum values as its values.
217/// \param OperandMap [out] A map for representing the operand name mappings for
218/// each instructions. This is used to generate the OperandMap table as
219/// well as the getNamedOperandIdx() function.
220void InstrInfoEmitter::initOperandMapData(
Craig Topper28851b62016-02-01 01:33:42 +0000221 ArrayRef<const CodeGenInstruction *> NumberedInstructions,
Craig Topper86a9aee2017-07-07 06:22:35 +0000222 StringRef Namespace,
Tom Stellardb162d942013-06-25 21:22:09 +0000223 std::map<std::string, unsigned> &Operands,
224 OpNameMapTy &OperandMap) {
Tom Stellardb162d942013-06-25 21:22:09 +0000225 unsigned NumOperands = 0;
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000226 for (const CodeGenInstruction *Inst : NumberedInstructions) {
227 if (!Inst->TheDef->getValueAsBit("UseNamedOperandTable"))
Tom Stellardb162d942013-06-25 21:22:09 +0000228 continue;
Tom Stellardb162d942013-06-25 21:22:09 +0000229 std::map<unsigned, unsigned> OpList;
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000230 for (const auto &Info : Inst->Operands) {
Tom Stellardb162d942013-06-25 21:22:09 +0000231 StrUintMapIter I = Operands.find(Info.Name);
232
233 if (I == Operands.end()) {
234 I = Operands.insert(Operands.begin(),
235 std::pair<std::string, unsigned>(Info.Name, NumOperands++));
236 }
237 OpList[I->second] = Info.MIOperandNo;
238 }
Craig Topper86a9aee2017-07-07 06:22:35 +0000239 OperandMap[OpList].push_back(Namespace.str() + "::" +
Matthias Braun4a86d452016-12-04 05:48:16 +0000240 Inst->TheDef->getName().str());
Tom Stellardb162d942013-06-25 21:22:09 +0000241 }
242}
243
244/// Generate a table and function for looking up the indices of operands by
245/// name.
246///
247/// This code generates:
248/// - An enum in the llvm::TargetNamespace::OpName namespace, with one entry
249/// for each operand name.
250/// - A 2-dimensional table called OperandMap for mapping OpName enum values to
251/// operand indices.
252/// - A function called getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
253/// for looking up the operand index for an instruction, given a value from
254/// OpName enum
255void InstrInfoEmitter::emitOperandNameMappings(raw_ostream &OS,
256 const CodeGenTarget &Target,
Craig Topper28851b62016-02-01 01:33:42 +0000257 ArrayRef<const CodeGenInstruction*> NumberedInstructions) {
Craig Topper86a9aee2017-07-07 06:22:35 +0000258 StringRef Namespace = Target.getInstNamespace();
Tom Stellardb162d942013-06-25 21:22:09 +0000259 std::string OpNameNS = "OpName";
260 // Map of operand names to their enumeration value. This will be used to
261 // generate the OpName enum.
262 std::map<std::string, unsigned> Operands;
263 OpNameMapTy OperandMap;
264
265 initOperandMapData(NumberedInstructions, Namespace, Operands, OperandMap);
266
267 OS << "#ifdef GET_INSTRINFO_OPERAND_ENUM\n";
268 OS << "#undef GET_INSTRINFO_OPERAND_ENUM\n";
Eric Christopher69c0a7c2015-03-13 01:10:08 +0000269 OS << "namespace llvm {\n";
Tom Stellardb162d942013-06-25 21:22:09 +0000270 OS << "namespace " << Namespace << " {\n";
Craig Topperfc1b4d42016-02-11 07:39:25 +0000271 OS << "namespace " << OpNameNS << " {\n";
Tom Stellardb162d942013-06-25 21:22:09 +0000272 OS << "enum {\n";
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000273 for (const auto &Op : Operands)
274 OS << " " << Op.first << " = " << Op.second << ",\n";
Tom Stellardb162d942013-06-25 21:22:09 +0000275
276 OS << "OPERAND_LAST";
277 OS << "\n};\n";
Hans Wennborgaa15bff2015-09-10 16:49:58 +0000278 OS << "} // end namespace OpName\n";
279 OS << "} // end namespace " << Namespace << "\n";
280 OS << "} // end namespace llvm\n";
Craig Topperfc1b4d42016-02-11 07:39:25 +0000281 OS << "#endif //GET_INSTRINFO_OPERAND_ENUM\n\n";
Tom Stellardb162d942013-06-25 21:22:09 +0000282
283 OS << "#ifdef GET_INSTRINFO_NAMED_OPS\n";
284 OS << "#undef GET_INSTRINFO_NAMED_OPS\n";
Eric Christopher69c0a7c2015-03-13 01:10:08 +0000285 OS << "namespace llvm {\n";
Tom Stellardb162d942013-06-25 21:22:09 +0000286 OS << "namespace " << Namespace << " {\n";
Matt Arsenault41e14812014-08-01 17:00:27 +0000287 OS << "LLVM_READONLY\n";
Tom Stellardb162d942013-06-25 21:22:09 +0000288 OS << "int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx) {\n";
Aaron Ballmane59e3582013-07-15 16:53:32 +0000289 if (!Operands.empty()) {
290 OS << " static const int16_t OperandMap [][" << Operands.size()
291 << "] = {\n";
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000292 for (const auto &Entry : OperandMap) {
293 const std::map<unsigned, unsigned> &OpList = Entry.first;
Aaron Ballmane59e3582013-07-15 16:53:32 +0000294 OS << "{";
Tom Stellardb162d942013-06-25 21:22:09 +0000295
Aaron Ballmane59e3582013-07-15 16:53:32 +0000296 // Emit a row of the OperandMap table
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000297 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
298 OS << (OpList.count(i) == 0 ? -1 : (int)OpList.find(i)->second) << ", ";
Tom Stellardb162d942013-06-25 21:22:09 +0000299
Aaron Ballmane59e3582013-07-15 16:53:32 +0000300 OS << "},\n";
301 }
302 OS << "};\n";
303
304 OS << " switch(Opcode) {\n";
305 unsigned TableIndex = 0;
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000306 for (const auto &Entry : OperandMap) {
307 for (const std::string &Name : Entry.second)
308 OS << " case " << Name << ":\n";
Aaron Ballmane59e3582013-07-15 16:53:32 +0000309
310 OS << " return OperandMap[" << TableIndex++ << "][NamedIdx];\n";
311 }
312 OS << " default: return -1;\n";
313 OS << " }\n";
314 } else {
315 // There are no operands, so no need to emit anything
316 OS << " return -1;\n";
Tom Stellardb162d942013-06-25 21:22:09 +0000317 }
Tom Stellardb162d942013-06-25 21:22:09 +0000318 OS << "}\n";
Hans Wennborgaa15bff2015-09-10 16:49:58 +0000319 OS << "} // end namespace " << Namespace << "\n";
320 OS << "} // end namespace llvm\n";
Craig Topperfc1b4d42016-02-11 07:39:25 +0000321 OS << "#endif //GET_INSTRINFO_NAMED_OPS\n\n";
Tom Stellardb162d942013-06-25 21:22:09 +0000322}
323
Ahmed Bougacha31377592013-11-17 21:24:41 +0000324/// Generate an enum for all the operand types for this target, under the
325/// llvm::TargetNamespace::OpTypes namespace.
326/// Operand types are all definitions derived of the Operand Target.td class.
327void InstrInfoEmitter::emitOperandTypesEnum(raw_ostream &OS,
328 const CodeGenTarget &Target) {
329
Craig Topper86a9aee2017-07-07 06:22:35 +0000330 StringRef Namespace = Target.getInstNamespace();
Ahmed Bougacha31377592013-11-17 21:24:41 +0000331 std::vector<Record *> Operands = Records.getAllDerivedDefinitions("Operand");
332
Craig Topperfc1b4d42016-02-11 07:39:25 +0000333 OS << "#ifdef GET_INSTRINFO_OPERAND_TYPES_ENUM\n";
Ahmed Bougacha31377592013-11-17 21:24:41 +0000334 OS << "#undef GET_INSTRINFO_OPERAND_TYPES_ENUM\n";
Eric Christopher69c0a7c2015-03-13 01:10:08 +0000335 OS << "namespace llvm {\n";
Ahmed Bougacha31377592013-11-17 21:24:41 +0000336 OS << "namespace " << Namespace << " {\n";
Craig Topperfc1b4d42016-02-11 07:39:25 +0000337 OS << "namespace OpTypes {\n";
Ahmed Bougacha31377592013-11-17 21:24:41 +0000338 OS << "enum OperandType {\n";
339
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000340 unsigned EnumVal = 0;
341 for (const Record *Op : Operands) {
342 if (!Op->isAnonymous())
343 OS << " " << Op->getName() << " = " << EnumVal << ",\n";
344 ++EnumVal;
Ahmed Bougacha31377592013-11-17 21:24:41 +0000345 }
346
347 OS << " OPERAND_TYPE_LIST_END" << "\n};\n";
Hans Wennborgaa15bff2015-09-10 16:49:58 +0000348 OS << "} // end namespace OpTypes\n";
349 OS << "} // end namespace " << Namespace << "\n";
350 OS << "} // end namespace llvm\n";
Craig Topperfc1b4d42016-02-11 07:39:25 +0000351 OS << "#endif // GET_INSTRINFO_OPERAND_TYPES_ENUM\n\n";
Ahmed Bougacha31377592013-11-17 21:24:41 +0000352}
353
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +0000354void InstrInfoEmitter::emitMCIIHelperMethods(raw_ostream &OS) {
355 RecVec TIIPredicates = Records.getAllDerivedDefinitions("TIIPredicate");
356 if (TIIPredicates.empty())
357 return;
358
359 CodeGenTarget &Target = CDP.getTargetInfo();
360 const StringRef TargetName = Target.getName();
361 formatted_raw_ostream FOS(OS);
362
363 FOS << "#ifdef GET_GENINSTRINFO_MC_DECL\n";
364 FOS << "#undef GET_GENINSTRINFO_MC_DECL\n\n";
365
366 FOS << "namespace llvm {\n";
367 FOS << "class MCInst;\n\n";
368
369 FOS << "namespace " << TargetName << "_MC {\n\n";
370
371 for (const Record *Rec : TIIPredicates) {
372 FOS << "bool " << Rec->getValueAsString("FunctionName")
373 << "(const MCInst &MI);\n";
374 }
375
376 FOS << "\n} // end " << TargetName << "_MC namespace\n";
377 FOS << "} // end llvm namespace\n\n";
378
379 FOS << "#endif // GET_GENINSTRINFO_MC_DECL\n\n";
380
381 FOS << "#ifdef GET_GENINSTRINFO_MC_HELPERS\n";
382 FOS << "#undef GET_GENINSTRINFO_MC_HELPERS\n\n";
383
384 FOS << "namespace llvm {\n";
385 FOS << "namespace " << TargetName << "_MC {\n\n";
386
387 PredicateExpander PE;
388 PE.setExpandForMC(true);
389 for (const Record *Rec : TIIPredicates) {
390 FOS << "bool " << Rec->getValueAsString("FunctionName");
391 FOS << "(const MCInst &MI) {\n";
392 FOS << " return ";
393 PE.expandPredicate(FOS, Rec->getValueAsDef("Pred"));
394 FOS << ";\n}\n";
395 }
396
397 FOS << "\n} // end " << TargetName << "_MC namespace\n";
398 FOS << "} // end llvm namespace\n\n";
399
400 FOS << "#endif // GET_GENISTRINFO_MC_HELPERS\n";
401}
402
Andrea Di Biagio95140022018-05-25 15:55:37 +0000403void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS) {
404 RecVec TIIPredicates = Records.getAllDerivedDefinitions("TIIPredicate");
405 if (TIIPredicates.empty())
406 return;
407
408 formatted_raw_ostream FOS(OS);
409 PredicateExpander PE;
410 PE.setExpandForMC(false);
411 PE.setIndentLevel(2);
412
413 for (const Record *Rec : TIIPredicates) {
414 FOS << "\n static bool " << Rec->getValueAsString("FunctionName");
415 FOS << "(const MachineInstr &MI) {\n";
416 FOS << " return ";
417 PE.expandPredicate(FOS, Rec->getValueAsDef("Pred"));
418 FOS << ";\n }\n";
419 }
420}
421
Chris Lattner626b89d2008-01-06 01:20:13 +0000422//===----------------------------------------------------------------------===//
423// Main Output.
424//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000425
426// run - Emit the main instruction description records for the target...
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000427void InstrInfoEmitter::run(raw_ostream &OS) {
Craig Toppera3ebc4f2016-02-11 07:39:27 +0000428 emitSourceFileHeader("Target Instruction Enum Values and Descriptors", OS);
Evan Cheng1e210d02011-06-28 20:07:07 +0000429 emitEnums(OS);
430
Craig Topperfc1b4d42016-02-11 07:39:25 +0000431 OS << "#ifdef GET_INSTRINFO_MC_DESC\n";
Evan Cheng1e210d02011-06-28 20:07:07 +0000432 OS << "#undef GET_INSTRINFO_MC_DESC\n";
433
Chris Lattnerc9d99ef2004-08-17 03:08:28 +0000434 OS << "namespace llvm {\n\n";
435
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000436 CodeGenTarget &Target = CDP.getTargetInfo();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000437 const std::string &TargetName = Target.getName();
438 Record *InstrInfo = Target.getInstructionSet();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000439
Chris Lattner99df6682005-08-18 21:36:47 +0000440 // Keep track of all of the def lists we have emitted already.
441 std::map<std::vector<Record*>, unsigned> EmittedLists;
Chris Lattner99df6682005-08-18 21:36:47 +0000442 unsigned ListNumber = 0;
Owen Andersona84be6c2011-06-27 21:06:21 +0000443
Chris Lattner99df6682005-08-18 21:36:47 +0000444 // Emit all of the instruction's implicit uses and defs.
Craig Topper8cc904d2016-01-17 20:38:18 +0000445 for (const CodeGenInstruction *II : Target.getInstructionsByEnumValue()) {
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000446 Record *Inst = II->TheDef;
Chris Lattner742606a2005-10-28 22:59:53 +0000447 std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
448 if (!Uses.empty()) {
Chris Lattner99df6682005-08-18 21:36:47 +0000449 unsigned &IL = EmittedLists[Uses];
Chris Lattner85467a12008-01-06 01:21:51 +0000450 if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS);
Chris Lattner99df6682005-08-18 21:36:47 +0000451 }
Chris Lattner742606a2005-10-28 22:59:53 +0000452 std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
453 if (!Defs.empty()) {
454 unsigned &IL = EmittedLists[Defs];
Chris Lattner85467a12008-01-06 01:21:51 +0000455 if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS);
Chris Lattner99df6682005-08-18 21:36:47 +0000456 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000457 }
458
Chris Lattner626b89d2008-01-06 01:20:13 +0000459 OperandInfoMapTy OperandInfoIDs;
Owen Andersona84be6c2011-06-27 21:06:21 +0000460
Chris Lattner220d0012005-08-19 16:57:28 +0000461 // Emit all of the operand info records.
Chris Lattner626b89d2008-01-06 01:20:13 +0000462 EmitOperandInfo(OS, OperandInfoIDs);
Owen Andersona84be6c2011-06-27 21:06:21 +0000463
Evan Cheng6cc775f2011-06-28 19:10:37 +0000464 // Emit all of the MCInstrDesc records in their ENUM ordering.
Chris Lattner220d0012005-08-19 16:57:28 +0000465 //
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000466 OS << "\nextern const MCInstrDesc " << TargetName << "Insts[] = {\n";
Craig Topper28851b62016-02-01 01:33:42 +0000467 ArrayRef<const CodeGenInstruction*> NumberedInstructions =
Chris Lattner918be522010-03-19 00:34:35 +0000468 Target.getInstructionsByEnumValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000469
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000470 SequenceToOffsetTable<std::string> InstrNames;
471 unsigned Num = 0;
472 for (const CodeGenInstruction *Inst : NumberedInstructions) {
473 // Keep a list of the instruction names.
474 InstrNames.add(Inst->TheDef->getName());
475 // Emit the record into the table.
476 emitRecord(*Inst, Num++, InstrInfo, EmittedLists, OperandInfoIDs, OS);
477 }
Evan Chengdf8974e2011-06-28 20:29:03 +0000478 OS << "};\n\n";
479
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000480 // Emit the array of instruction names.
Craig Topper91773ab2012-04-01 18:14:14 +0000481 InstrNames.layout();
482 OS << "extern const char " << TargetName << "InstrNameData[] = {\n";
483 InstrNames.emit(OS, printChar);
484 OS << "};\n\n";
485
486 OS << "extern const unsigned " << TargetName <<"InstrNameIndices[] = {";
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000487 Num = 0;
488 for (const CodeGenInstruction *Inst : NumberedInstructions) {
489 // Newline every eight entries.
490 if (Num % 8 == 0)
Benjamin Kramerbf152d52012-02-10 13:18:44 +0000491 OS << "\n ";
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000492 OS << InstrNames.get(Inst->TheDef->getName()) << "U, ";
493 ++Num;
Benjamin Kramerbf152d52012-02-10 13:18:44 +0000494 }
495
496 OS << "\n};\n\n";
497
Evan Chengdf8974e2011-06-28 20:29:03 +0000498 // MCInstrInfo initialization routine.
499 OS << "static inline void Init" << TargetName
500 << "MCInstrInfo(MCInstrInfo *II) {\n";
501 OS << " II->InitMCInstrInfo(" << TargetName << "Insts, "
Benjamin Kramerbf152d52012-02-10 13:18:44 +0000502 << TargetName << "InstrNameIndices, " << TargetName << "InstrNameData, "
Evan Chengdf8974e2011-06-28 20:29:03 +0000503 << NumberedInstructions.size() << ");\n}\n\n";
504
Craig Topperfc1b4d42016-02-11 07:39:25 +0000505 OS << "} // end llvm namespace\n";
Evan Cheng1e210d02011-06-28 20:07:07 +0000506
507 OS << "#endif // GET_INSTRINFO_MC_DESC\n\n";
Evan Cheng703a0fb2011-07-01 17:57:27 +0000508
509 // Create a TargetInstrInfo subclass to hide the MC layer initialization.
Craig Topperfc1b4d42016-02-11 07:39:25 +0000510 OS << "#ifdef GET_INSTRINFO_HEADER\n";
Evan Cheng703a0fb2011-07-01 17:57:27 +0000511 OS << "#undef GET_INSTRINFO_HEADER\n";
512
513 std::string ClassName = TargetName + "GenInstrInfo";
Evan Cheng54b68e32011-07-01 20:45:01 +0000514 OS << "namespace llvm {\n";
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000515 OS << "struct " << ClassName << " : public TargetInstrInfo {\n"
Eric Christopher44fd4982015-03-13 01:26:39 +0000516 << " explicit " << ClassName
Dean Michael Berris52735fc2016-07-14 04:06:33 +0000517 << "(int CFSetupOpcode = -1, int CFDestroyOpcode = -1, int CatchRetOpcode = -1, int ReturnOpcode = -1);\n"
Andrea Di Biagio95140022018-05-25 15:55:37 +0000518 << " ~" << ClassName << "() override = default;\n";
519
520 emitTIIHelperMethods(OS);
521
522 OS << "\n};\n} // end llvm namespace\n";
Evan Cheng703a0fb2011-07-01 17:57:27 +0000523
524 OS << "#endif // GET_INSTRINFO_HEADER\n\n";
525
Craig Topperfc1b4d42016-02-11 07:39:25 +0000526 OS << "#ifdef GET_INSTRINFO_CTOR_DTOR\n";
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000527 OS << "#undef GET_INSTRINFO_CTOR_DTOR\n";
Evan Cheng703a0fb2011-07-01 17:57:27 +0000528
Evan Cheng54b68e32011-07-01 20:45:01 +0000529 OS << "namespace llvm {\n";
Benjamin Kramer0d6d0982011-10-22 16:50:00 +0000530 OS << "extern const MCInstrDesc " << TargetName << "Insts[];\n";
Jakob Stoklund Olesene3084892012-03-15 18:05:57 +0000531 OS << "extern const unsigned " << TargetName << "InstrNameIndices[];\n";
Craig Topper91773ab2012-04-01 18:14:14 +0000532 OS << "extern const char " << TargetName << "InstrNameData[];\n";
Eric Christopher44fd4982015-03-13 01:26:39 +0000533 OS << ClassName << "::" << ClassName
Dean Michael Berris52735fc2016-07-14 04:06:33 +0000534 << "(int CFSetupOpcode, int CFDestroyOpcode, int CatchRetOpcode, int ReturnOpcode)\n"
535 << " : TargetInstrInfo(CFSetupOpcode, CFDestroyOpcode, CatchRetOpcode, ReturnOpcode) {\n"
Eric Christopher44fd4982015-03-13 01:26:39 +0000536 << " InitMCInstrInfo(" << TargetName << "Insts, " << TargetName
537 << "InstrNameIndices, " << TargetName << "InstrNameData, "
Hans Wennborgaa15bff2015-09-10 16:49:58 +0000538 << NumberedInstructions.size() << ");\n}\n";
Craig Topperfc1b4d42016-02-11 07:39:25 +0000539 OS << "} // end llvm namespace\n";
Evan Cheng703a0fb2011-07-01 17:57:27 +0000540
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000541 OS << "#endif // GET_INSTRINFO_CTOR_DTOR\n\n";
Tom Stellardb162d942013-06-25 21:22:09 +0000542
543 emitOperandNameMappings(OS, Target, NumberedInstructions);
Ahmed Bougacha31377592013-11-17 21:24:41 +0000544
545 emitOperandTypesEnum(OS, Target);
Andrea Di Biagio8f66adec2018-05-25 16:02:43 +0000546
547 emitMCIIHelperMethods(OS);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000548}
549
Chris Lattnerc860eca2004-08-01 05:04:00 +0000550void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
Chris Lattner99df6682005-08-18 21:36:47 +0000551 Record *InstrInfo,
Chris Lattner742606a2005-10-28 22:59:53 +0000552 std::map<std::vector<Record*>, unsigned> &EmittedLists,
Chris Lattner626b89d2008-01-06 01:20:13 +0000553 const OperandInfoMapTy &OpInfo,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000554 raw_ostream &OS) {
Chris Lattner27a4c152008-01-06 01:53:37 +0000555 int MinOperands = 0;
Richard Trieu24489692012-10-12 17:57:35 +0000556 if (!Inst.Operands.empty())
Chris Lattner511ee682005-08-19 00:59:49 +0000557 // Each logical operand can be multiple MI operands.
Chris Lattnerd8adec72010-11-01 04:03:32 +0000558 MinOperands = Inst.Operands.back().MIOperandNo +
559 Inst.Operands.back().MINumOperands;
Dan Gohman6e582c42008-05-29 19:57:41 +0000560
Evan Cheng71adba62006-11-17 01:46:27 +0000561 OS << " { ";
Evan Chengc9de9ce2007-08-02 00:20:17 +0000562 OS << Num << ",\t" << MinOperands << ",\t"
Owen Anderson651b2302011-07-13 23:22:26 +0000563 << Inst.Operands.NumDefs << ",\t"
Owen Andersonca548002015-05-28 18:03:07 +0000564 << Inst.TheDef->getValueAsInt("Size") << ",\t"
565 << SchedModels.getSchedClassIdx(Inst) << ",\t0";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000566
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000567 CodeGenTarget &Target = CDP.getTargetInfo();
568
Eric Christopher834d6422015-02-26 00:00:33 +0000569 // Emit all of the target independent flags...
Owen Andersonabaa5232015-05-28 18:33:39 +0000570 if (Inst.isPseudo) OS << "|(1ULL<<MCID::Pseudo)";
571 if (Inst.isReturn) OS << "|(1ULL<<MCID::Return)";
572 if (Inst.isBranch) OS << "|(1ULL<<MCID::Branch)";
573 if (Inst.isIndirectBranch) OS << "|(1ULL<<MCID::IndirectBranch)";
574 if (Inst.isCompare) OS << "|(1ULL<<MCID::Compare)";
575 if (Inst.isMoveImm) OS << "|(1ULL<<MCID::MoveImm)";
Petar Jovanovicc0510002018-05-23 15:28:28 +0000576 if (Inst.isMoveReg) OS << "|(1ULL<<MCID::MoveReg)";
Owen Andersonabaa5232015-05-28 18:33:39 +0000577 if (Inst.isBitcast) OS << "|(1ULL<<MCID::Bitcast)";
Sjoerd Meijer724023a2016-09-14 08:20:03 +0000578 if (Inst.isAdd) OS << "|(1ULL<<MCID::Add)";
Owen Andersonabaa5232015-05-28 18:33:39 +0000579 if (Inst.isSelect) OS << "|(1ULL<<MCID::Select)";
580 if (Inst.isBarrier) OS << "|(1ULL<<MCID::Barrier)";
581 if (Inst.hasDelaySlot) OS << "|(1ULL<<MCID::DelaySlot)";
582 if (Inst.isCall) OS << "|(1ULL<<MCID::Call)";
583 if (Inst.canFoldAsLoad) OS << "|(1ULL<<MCID::FoldableAsLoad)";
584 if (Inst.mayLoad) OS << "|(1ULL<<MCID::MayLoad)";
585 if (Inst.mayStore) OS << "|(1ULL<<MCID::MayStore)";
586 if (Inst.isPredicable) OS << "|(1ULL<<MCID::Predicable)";
587 if (Inst.isConvertibleToThreeAddress) OS << "|(1ULL<<MCID::ConvertibleTo3Addr)";
588 if (Inst.isCommutable) OS << "|(1ULL<<MCID::Commutable)";
589 if (Inst.isTerminator) OS << "|(1ULL<<MCID::Terminator)";
590 if (Inst.isReMaterializable) OS << "|(1ULL<<MCID::Rematerializable)";
591 if (Inst.isNotDuplicable) OS << "|(1ULL<<MCID::NotDuplicable)";
592 if (Inst.Operands.hasOptionalDef) OS << "|(1ULL<<MCID::HasOptionalDef)";
593 if (Inst.usesCustomInserter) OS << "|(1ULL<<MCID::UsesCustomInserter)";
594 if (Inst.hasPostISelHook) OS << "|(1ULL<<MCID::HasPostISelHook)";
595 if (Inst.Operands.isVariadic)OS << "|(1ULL<<MCID::Variadic)";
596 if (Inst.hasSideEffects) OS << "|(1ULL<<MCID::UnmodeledSideEffects)";
597 if (Inst.isAsCheapAsAMove) OS << "|(1ULL<<MCID::CheapAsAMove)";
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000598 if (!Target.getAllowRegisterRenaming() || Inst.hasExtraSrcRegAllocReq)
599 OS << "|(1ULL<<MCID::ExtraSrcRegAllocReq)";
600 if (!Target.getAllowRegisterRenaming() || Inst.hasExtraDefRegAllocReq)
601 OS << "|(1ULL<<MCID::ExtraDefRegAllocReq)";
Owen Andersonabaa5232015-05-28 18:33:39 +0000602 if (Inst.isRegSequence) OS << "|(1ULL<<MCID::RegSequence)";
603 if (Inst.isExtractSubreg) OS << "|(1ULL<<MCID::ExtractSubreg)";
604 if (Inst.isInsertSubreg) OS << "|(1ULL<<MCID::InsertSubreg)";
605 if (Inst.isConvergent) OS << "|(1ULL<<MCID::Convergent)";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000606
607 // Emit all of the target-specific flags...
David Greeneaf8ee2c2011-07-29 22:43:06 +0000608 BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags");
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000609 if (!TSF)
610 PrintFatalError("no TSFlags?");
Jakob Stoklund Olesenb93331f2010-04-05 03:10:20 +0000611 uint64_t Value = 0;
612 for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) {
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000613 if (const auto *Bit = dyn_cast<BitInit>(TSF->getBit(i)))
Jakob Stoklund Olesenb93331f2010-04-05 03:10:20 +0000614 Value |= uint64_t(Bit->getValue()) << i;
615 else
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000616 PrintFatalError("Invalid TSFlags bit in " + Inst.TheDef->getName());
Jakob Stoklund Olesenb93331f2010-04-05 03:10:20 +0000617 }
618 OS << ", 0x";
619 OS.write_hex(Value);
Eric Christopher223c4812010-06-09 16:16:48 +0000620 OS << "ULL, ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000621
622 // Emit the implicit uses and defs lists...
Chris Lattner742606a2005-10-28 22:59:53 +0000623 std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
624 if (UseList.empty())
Craig Topper948dfbf2014-04-30 05:53:35 +0000625 OS << "nullptr, ";
Misha Brukman650ba8e2005-04-22 00:00:37 +0000626 else
Chris Lattner742606a2005-10-28 22:59:53 +0000627 OS << "ImplicitList" << EmittedLists[UseList] << ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000628
Chris Lattner742606a2005-10-28 22:59:53 +0000629 std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
630 if (DefList.empty())
Craig Topper948dfbf2014-04-30 05:53:35 +0000631 OS << "nullptr, ";
Misha Brukman650ba8e2005-04-22 00:00:37 +0000632 else
Chris Lattner742606a2005-10-28 22:59:53 +0000633 OS << "ImplicitList" << EmittedLists[DefList] << ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000634
Chris Lattner220d0012005-08-19 16:57:28 +0000635 // Emit the operand info.
Chris Lattner33f5a512006-11-06 23:49:51 +0000636 std::vector<std::string> OperandInfo = GetOperandInfo(Inst);
Chris Lattnerd02bd5b2005-08-19 18:46:26 +0000637 if (OperandInfo.empty())
Craig Topper948dfbf2014-04-30 05:53:35 +0000638 OS << "nullptr";
Chris Lattner220d0012005-08-19 16:57:28 +0000639 else
Chris Lattner626b89d2008-01-06 01:20:13 +0000640 OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
Jakob Stoklund Olesenb93331f2010-04-05 03:10:20 +0000641
Joey Gouly0e76fa72013-09-12 10:28:05 +0000642 if (Inst.HasComplexDeprecationPredicate)
643 // Emit a function pointer to the complex predicate method.
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000644 OS << ", -1 "
Joey Gouly0e76fa72013-09-12 10:28:05 +0000645 << ",&get" << Inst.DeprecatedReason << "DeprecationInfo";
646 else if (!Inst.DeprecatedReason.empty())
647 // Emit the Subtarget feature.
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000648 OS << ", " << Target.getInstNamespace() << "::" << Inst.DeprecatedReason
649 << " ,nullptr";
Joey Gouly0e76fa72013-09-12 10:28:05 +0000650 else
651 // Instruction isn't deprecated.
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000652 OS << ", -1 ,nullptr";
Joey Gouly0e76fa72013-09-12 10:28:05 +0000653
Chris Lattnerc860eca2004-08-01 05:04:00 +0000654 OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000655}
Evan Cheng1e210d02011-06-28 20:07:07 +0000656
657// emitEnums - Print out enum values for all of the instructions.
658void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
Craig Topperfc1b4d42016-02-11 07:39:25 +0000659 OS << "#ifdef GET_INSTRINFO_ENUM\n";
Evan Cheng1e210d02011-06-28 20:07:07 +0000660 OS << "#undef GET_INSTRINFO_ENUM\n";
661
662 OS << "namespace llvm {\n\n";
663
664 CodeGenTarget Target(Records);
665
666 // We must emit the PHI opcode first...
Craig Topper86a9aee2017-07-07 06:22:35 +0000667 StringRef Namespace = Target.getInstNamespace();
Jim Grosbachdac4a952012-04-11 21:02:30 +0000668
James Y Knighte452e272015-05-11 22:17:13 +0000669 if (Namespace.empty())
670 PrintFatalError("No instructions defined!");
Evan Cheng1e210d02011-06-28 20:07:07 +0000671
Evan Cheng1e210d02011-06-28 20:07:07 +0000672 OS << "namespace " << Namespace << " {\n";
673 OS << " enum {\n";
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000674 unsigned Num = 0;
Craig Toppere16efd92016-02-11 07:39:22 +0000675 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue())
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000676 OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
Craig Toppere16efd92016-02-11 07:39:22 +0000677 OS << " INSTRUCTION_LIST_END = " << Num << "\n";
Craig Topperca331be2014-11-23 09:40:10 +0000678 OS << " };\n\n";
Craig Topperac59db22017-12-13 07:26:17 +0000679 OS << "} // end " << Namespace << " namespace\n";
680 OS << "} // end llvm namespace\n";
681 OS << "#endif // GET_INSTRINFO_ENUM\n\n";
682
683 OS << "#ifdef GET_INSTRINFO_SCHED_ENUM\n";
684 OS << "#undef GET_INSTRINFO_SCHED_ENUM\n";
685 OS << "namespace llvm {\n\n";
686 OS << "namespace " << Namespace << " {\n";
Vincent Lejeune7deccf02013-09-03 19:43:28 +0000687 OS << "namespace Sched {\n";
688 OS << " enum {\n";
Jim Grosbach5198f3e92014-04-18 02:09:07 +0000689 Num = 0;
690 for (const auto &Class : SchedModels.explicit_classes())
691 OS << " " << Class.Name << "\t= " << Num++ << ",\n";
Craig Toppere16efd92016-02-11 07:39:22 +0000692 OS << " SCHED_LIST_END = " << Num << "\n";
Craig Topperca331be2014-11-23 09:40:10 +0000693 OS << " };\n";
Hans Wennborgaa15bff2015-09-10 16:49:58 +0000694 OS << "} // end Sched namespace\n";
695 OS << "} // end " << Namespace << " namespace\n";
Craig Topperfc1b4d42016-02-11 07:39:25 +0000696 OS << "} // end llvm namespace\n";
Evan Cheng1e210d02011-06-28 20:07:07 +0000697
Craig Topperac59db22017-12-13 07:26:17 +0000698 OS << "#endif // GET_INSTRINFO_SCHED_ENUM\n\n";
Evan Cheng1e210d02011-06-28 20:07:07 +0000699}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000700
701namespace llvm {
702
703void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
704 InstrInfoEmitter(RK).run(OS);
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000705 EmitMapTable(RK, OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000706}
707
Hans Wennborgaa15bff2015-09-10 16:49:58 +0000708} // end llvm namespace