Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 1 | //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. --*- C++ -*-===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | d303203 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 8adcd9f | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | d303203 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 9 | // |
| 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 Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 15 | #include "CodeGenDAGPatterns.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 16 | #include "CodeGenInstruction.h" |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 17 | #include "CodeGenSchedule.h" |
Chris Lattner | fce9603 | 2004-08-01 04:04:35 +0000 | [diff] [blame] | 18 | #include "CodeGenTarget.h" |
Craig Topper | 91773ab | 2012-04-01 18:14:14 +0000 | [diff] [blame] | 19 | #include "SequenceToOffsetTable.h" |
Chandler Carruth | 91d19d8 | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 20 | #include "TableGenBackends.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/ArrayRef.h" |
Chris Lattner | 06fa176 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Casting.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 25 | #include "llvm/TableGen/Error.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 26 | #include "llvm/TableGen/Record.h" |
| 27 | #include "llvm/TableGen/TableGenBackend.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 28 | #include <cassert> |
| 29 | #include <cstdint> |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 30 | #include <map> |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 31 | #include <string> |
| 32 | #include <utility> |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 33 | #include <vector> |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 6847866 | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 37 | namespace { |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 38 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 39 | class InstrInfoEmitter { |
| 40 | RecordKeeper &Records; |
| 41 | CodeGenDAGPatterns CDP; |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 42 | const CodeGenSchedModels &SchedModels; |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 43 | |
| 44 | public: |
Andrew Trick | 87255e3 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 45 | InstrInfoEmitter(RecordKeeper &R): |
| 46 | Records(R), CDP(R), SchedModels(CDP.getTargetInfo().getSchedModels()) {} |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 47 | |
| 48 | // run - Output the instruction set description. |
| 49 | void run(raw_ostream &OS); |
| 50 | |
| 51 | private: |
| 52 | void emitEnums(raw_ostream &OS); |
| 53 | |
| 54 | typedef std::map<std::vector<std::string>, unsigned> OperandInfoMapTy; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 55 | |
| 56 | /// The keys of this map are maps which have OpName enum values as their keys |
| 57 | /// and instruction operand indices as their values. The values of this map |
| 58 | /// are lists of instruction names. |
| 59 | typedef std::map<std::map<unsigned, unsigned>, |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 60 | std::vector<std::string>> OpNameMapTy; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 61 | typedef std::map<std::string, unsigned>::iterator StrUintMapIter; |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 62 | void emitRecord(const CodeGenInstruction &Inst, unsigned Num, |
| 63 | Record *InstrInfo, |
| 64 | std::map<std::vector<Record*>, unsigned> &EL, |
| 65 | const OperandInfoMapTy &OpInfo, |
| 66 | raw_ostream &OS); |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 67 | void emitOperandTypesEnum(raw_ostream &OS, const CodeGenTarget &Target); |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 68 | void initOperandMapData( |
Craig Topper | 28851b6 | 2016-02-01 01:33:42 +0000 | [diff] [blame] | 69 | ArrayRef<const CodeGenInstruction *> NumberedInstructions, |
Craig Topper | 86a9aee | 2017-07-07 06:22:35 +0000 | [diff] [blame] | 70 | StringRef Namespace, |
Craig Topper | 65efcb4 | 2014-02-05 07:27:49 +0000 | [diff] [blame] | 71 | std::map<std::string, unsigned> &Operands, |
| 72 | OpNameMapTy &OperandMap); |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 73 | void emitOperandNameMappings(raw_ostream &OS, const CodeGenTarget &Target, |
Craig Topper | 28851b6 | 2016-02-01 01:33:42 +0000 | [diff] [blame] | 74 | ArrayRef<const CodeGenInstruction*> NumberedInstructions); |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 75 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 76 | // Operand information. |
| 77 | void EmitOperandInfo(raw_ostream &OS, OperandInfoMapTy &OperandInfoIDs); |
| 78 | std::vector<std::string> GetOperandInfo(const CodeGenInstruction &Inst); |
| 79 | }; |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 80 | |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 81 | } // end anonymous namespace |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 85467a1 | 2008-01-06 01:21:51 +0000 | [diff] [blame] | 83 | static void PrintDefList(const std::vector<Record*> &Uses, |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 84 | unsigned Num, raw_ostream &OS) { |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 85 | OS << "static const MCPhysReg ImplicitList" << Num << "[] = { "; |
Craig Topper | 9cd0ff1 | 2016-02-16 02:48:30 +0000 | [diff] [blame] | 86 | for (Record *U : Uses) |
| 87 | OS << getQualifiedName(U) << ", "; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 88 | OS << "0 };\n"; |
| 89 | } |
| 90 | |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 91 | //===----------------------------------------------------------------------===// |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 92 | // Operand Info Emission. |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | |
Chris Lattner | 33f5a51 | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 95 | std::vector<std::string> |
| 96 | InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) { |
| 97 | std::vector<std::string> Result; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 98 | |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 99 | for (auto &Op : Inst.Operands) { |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 100 | // Handle aggregate operands and normal operands the same way by expanding |
| 101 | // either case into a list of operands for this op. |
Chris Lattner | d8adec7 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 102 | std::vector<CGIOperandList::OperandInfo> OperandList; |
Chris Lattner | 33f5a51 | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 103 | |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 104 | // This might be a multiple operand thing. Targets like X86 have |
| 105 | // registers in their multi-operand operands. It may also be an anonymous |
| 106 | // operand, which has a single operand, but no declared class for the |
| 107 | // operand. |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 108 | DagInit *MIOI = Op.MIOperandInfo; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 109 | |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 110 | if (!MIOI || MIOI->getNumArgs() == 0) { |
| 111 | // Single, anonymous, operand. |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 112 | OperandList.push_back(Op); |
Chris Lattner | 6bc0304 | 2005-11-19 07:05:57 +0000 | [diff] [blame] | 113 | } else { |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 114 | for (unsigned j = 0, e = Op.MINumOperands; j != e; ++j) { |
| 115 | OperandList.push_back(Op); |
Chris Lattner | 33f5a51 | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 116 | |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 117 | auto *OpR = cast<DefInit>(MIOI->getArg(j))->getDef(); |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 118 | OperandList.back().Rec = OpR; |
Chris Lattner | 6bc0304 | 2005-11-19 07:05:57 +0000 | [diff] [blame] | 119 | } |
Chris Lattner | d02bd5b | 2005-08-19 18:46:26 +0000 | [diff] [blame] | 120 | } |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 121 | |
| 122 | for (unsigned j = 0, e = OperandList.size(); j != e; ++j) { |
| 123 | Record *OpR = OperandList[j].Rec; |
| 124 | std::string Res; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 125 | |
| 126 | if (OpR->isSubClassOf("RegisterOperand")) |
| 127 | OpR = OpR->getValueAsDef("RegClass"); |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 128 | if (OpR->isSubClassOf("RegisterClass")) |
| 129 | Res += getQualifiedName(OpR) + "RegClassID, "; |
Chris Lattner | f323953 | 2009-07-29 21:10:12 +0000 | [diff] [blame] | 130 | else if (OpR->isSubClassOf("PointerLikeRegClass")) |
| 131 | Res += utostr(OpR->getValueAsInt("RegClassKind")) + ", "; |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 132 | else |
Dan Gohman | 882bb29 | 2010-06-18 18:13:55 +0000 | [diff] [blame] | 133 | // -1 means the operand does not have a fixed register class. |
| 134 | Res += "-1, "; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 135 | |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 136 | // Fill in applicable flags. |
| 137 | Res += "0"; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 138 | |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 139 | // Ptr value whose register class is resolved via callback. |
Chris Lattner | 426bc7c | 2009-07-29 20:43:05 +0000 | [diff] [blame] | 140 | if (OpR->isSubClassOf("PointerLikeRegClass")) |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 141 | Res += "|(1<<MCOI::LookupPtrRegClass)"; |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 142 | |
| 143 | // Predicate operands. Check to see if the original unexpanded operand |
Tim Northover | 4218044 | 2013-08-22 09:57:11 +0000 | [diff] [blame] | 144 | // was of type PredicateOp. |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 145 | if (Op.Rec->isSubClassOf("PredicateOp")) |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 146 | Res += "|(1<<MCOI::Predicate)"; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 147 | |
Evan Cheng | 6e3c705 | 2007-07-10 18:05:01 +0000 | [diff] [blame] | 148 | // Optional def operands. Check to see if the original unexpanded operand |
| 149 | // was of type OptionalDefOperand. |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 150 | if (Op.Rec->isSubClassOf("OptionalDefOperand")) |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 151 | Res += "|(1<<MCOI::OptionalDef)"; |
Evan Cheng | 6e3c705 | 2007-07-10 18:05:01 +0000 | [diff] [blame] | 152 | |
Craig Topper | b358499 | 2012-03-11 01:57:56 +0000 | [diff] [blame] | 153 | // Fill in operand type. |
Tom Stellard | 89b2610 | 2015-01-12 19:33:09 +0000 | [diff] [blame] | 154 | Res += ", "; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 155 | assert(!Op.OperandType.empty() && "Invalid operand type."); |
| 156 | Res += Op.OperandType; |
Craig Topper | b358499 | 2012-03-11 01:57:56 +0000 | [diff] [blame] | 157 | |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 158 | // Fill in constraint info. |
Chris Lattner | a9dfb1b | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 159 | Res += ", "; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 160 | |
Chris Lattner | d8adec7 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 161 | const CGIOperandList::ConstraintInfo &Constraint = |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 162 | Op.Constraints[j]; |
Chris Lattner | a9dfb1b | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 163 | if (Constraint.isNone()) |
| 164 | Res += "0"; |
| 165 | else if (Constraint.isEarlyClobber()) |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 166 | Res += "(1 << MCOI::EARLY_CLOBBER)"; |
Chris Lattner | a9dfb1b | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 167 | else { |
| 168 | assert(Constraint.isTied()); |
| 169 | Res += "((" + utostr(Constraint.getTiedOperand()) + |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 170 | " << 16) | (1 << MCOI::TIED_TO))"; |
Chris Lattner | a9dfb1b | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 171 | } |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 172 | |
Chris Lattner | ba7b367 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 173 | Result.push_back(Res); |
| 174 | } |
Chris Lattner | d02bd5b | 2005-08-19 18:46:26 +0000 | [diff] [blame] | 175 | } |
Evan Cheng | ac79c7c | 2006-11-01 00:27:05 +0000 | [diff] [blame] | 176 | |
Chris Lattner | d02bd5b | 2005-08-19 18:46:26 +0000 | [diff] [blame] | 177 | return Result; |
| 178 | } |
| 179 | |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 180 | void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS, |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 181 | OperandInfoMapTy &OperandInfoIDs) { |
| 182 | // ID #0 is for no operand info. |
| 183 | unsigned OperandListNum = 0; |
| 184 | OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 186 | OS << "\n"; |
| 187 | const CodeGenTarget &Target = CDP.getTargetInfo(); |
Craig Topper | 8cc904d | 2016-01-17 20:38:18 +0000 | [diff] [blame] | 188 | for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) { |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 189 | std::vector<std::string> OperandInfo = GetOperandInfo(*Inst); |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 190 | unsigned &N = OperandInfoIDs[OperandInfo]; |
| 191 | if (N != 0) continue; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 193 | N = ++OperandListNum; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 194 | OS << "static const MCOperandInfo OperandInfo" << N << "[] = { "; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 195 | for (const std::string &Info : OperandInfo) |
| 196 | OS << "{ " << Info << " }, "; |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 197 | OS << "};\n"; |
| 198 | } |
| 199 | } |
| 200 | |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 201 | /// Initialize data structures for generating operand name mappings. |
| 202 | /// |
| 203 | /// \param Operands [out] A map used to generate the OpName enum with operand |
| 204 | /// names as its keys and operand enum values as its values. |
| 205 | /// \param OperandMap [out] A map for representing the operand name mappings for |
| 206 | /// each instructions. This is used to generate the OperandMap table as |
| 207 | /// well as the getNamedOperandIdx() function. |
| 208 | void InstrInfoEmitter::initOperandMapData( |
Craig Topper | 28851b6 | 2016-02-01 01:33:42 +0000 | [diff] [blame] | 209 | ArrayRef<const CodeGenInstruction *> NumberedInstructions, |
Craig Topper | 86a9aee | 2017-07-07 06:22:35 +0000 | [diff] [blame] | 210 | StringRef Namespace, |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 211 | std::map<std::string, unsigned> &Operands, |
| 212 | OpNameMapTy &OperandMap) { |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 213 | unsigned NumOperands = 0; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 214 | for (const CodeGenInstruction *Inst : NumberedInstructions) { |
| 215 | if (!Inst->TheDef->getValueAsBit("UseNamedOperandTable")) |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 216 | continue; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 217 | std::map<unsigned, unsigned> OpList; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 218 | for (const auto &Info : Inst->Operands) { |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 219 | StrUintMapIter I = Operands.find(Info.Name); |
| 220 | |
| 221 | if (I == Operands.end()) { |
| 222 | I = Operands.insert(Operands.begin(), |
| 223 | std::pair<std::string, unsigned>(Info.Name, NumOperands++)); |
| 224 | } |
| 225 | OpList[I->second] = Info.MIOperandNo; |
| 226 | } |
Craig Topper | 86a9aee | 2017-07-07 06:22:35 +0000 | [diff] [blame] | 227 | OperandMap[OpList].push_back(Namespace.str() + "::" + |
Matthias Braun | 4a86d45 | 2016-12-04 05:48:16 +0000 | [diff] [blame] | 228 | Inst->TheDef->getName().str()); |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | /// Generate a table and function for looking up the indices of operands by |
| 233 | /// name. |
| 234 | /// |
| 235 | /// This code generates: |
| 236 | /// - An enum in the llvm::TargetNamespace::OpName namespace, with one entry |
| 237 | /// for each operand name. |
| 238 | /// - A 2-dimensional table called OperandMap for mapping OpName enum values to |
| 239 | /// operand indices. |
| 240 | /// - A function called getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx) |
| 241 | /// for looking up the operand index for an instruction, given a value from |
| 242 | /// OpName enum |
| 243 | void InstrInfoEmitter::emitOperandNameMappings(raw_ostream &OS, |
| 244 | const CodeGenTarget &Target, |
Craig Topper | 28851b6 | 2016-02-01 01:33:42 +0000 | [diff] [blame] | 245 | ArrayRef<const CodeGenInstruction*> NumberedInstructions) { |
Craig Topper | 86a9aee | 2017-07-07 06:22:35 +0000 | [diff] [blame] | 246 | StringRef Namespace = Target.getInstNamespace(); |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 247 | std::string OpNameNS = "OpName"; |
| 248 | // Map of operand names to their enumeration value. This will be used to |
| 249 | // generate the OpName enum. |
| 250 | std::map<std::string, unsigned> Operands; |
| 251 | OpNameMapTy OperandMap; |
| 252 | |
| 253 | initOperandMapData(NumberedInstructions, Namespace, Operands, OperandMap); |
| 254 | |
| 255 | OS << "#ifdef GET_INSTRINFO_OPERAND_ENUM\n"; |
| 256 | OS << "#undef GET_INSTRINFO_OPERAND_ENUM\n"; |
Eric Christopher | 69c0a7c | 2015-03-13 01:10:08 +0000 | [diff] [blame] | 257 | OS << "namespace llvm {\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 258 | OS << "namespace " << Namespace << " {\n"; |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 259 | OS << "namespace " << OpNameNS << " {\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 260 | OS << "enum {\n"; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 261 | for (const auto &Op : Operands) |
| 262 | OS << " " << Op.first << " = " << Op.second << ",\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 263 | |
| 264 | OS << "OPERAND_LAST"; |
| 265 | OS << "\n};\n"; |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 266 | OS << "} // end namespace OpName\n"; |
| 267 | OS << "} // end namespace " << Namespace << "\n"; |
| 268 | OS << "} // end namespace llvm\n"; |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 269 | OS << "#endif //GET_INSTRINFO_OPERAND_ENUM\n\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 270 | |
| 271 | OS << "#ifdef GET_INSTRINFO_NAMED_OPS\n"; |
| 272 | OS << "#undef GET_INSTRINFO_NAMED_OPS\n"; |
Eric Christopher | 69c0a7c | 2015-03-13 01:10:08 +0000 | [diff] [blame] | 273 | OS << "namespace llvm {\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 274 | OS << "namespace " << Namespace << " {\n"; |
Matt Arsenault | 41e1481 | 2014-08-01 17:00:27 +0000 | [diff] [blame] | 275 | OS << "LLVM_READONLY\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 276 | OS << "int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx) {\n"; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 277 | if (!Operands.empty()) { |
| 278 | OS << " static const int16_t OperandMap [][" << Operands.size() |
| 279 | << "] = {\n"; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 280 | for (const auto &Entry : OperandMap) { |
| 281 | const std::map<unsigned, unsigned> &OpList = Entry.first; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 282 | OS << "{"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 283 | |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 284 | // Emit a row of the OperandMap table |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 285 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 286 | OS << (OpList.count(i) == 0 ? -1 : (int)OpList.find(i)->second) << ", "; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 287 | |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 288 | OS << "},\n"; |
| 289 | } |
| 290 | OS << "};\n"; |
| 291 | |
| 292 | OS << " switch(Opcode) {\n"; |
| 293 | unsigned TableIndex = 0; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 294 | for (const auto &Entry : OperandMap) { |
| 295 | for (const std::string &Name : Entry.second) |
| 296 | OS << " case " << Name << ":\n"; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 297 | |
| 298 | OS << " return OperandMap[" << TableIndex++ << "][NamedIdx];\n"; |
| 299 | } |
| 300 | OS << " default: return -1;\n"; |
| 301 | OS << " }\n"; |
| 302 | } else { |
| 303 | // There are no operands, so no need to emit anything |
| 304 | OS << " return -1;\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 305 | } |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 306 | OS << "}\n"; |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 307 | OS << "} // end namespace " << Namespace << "\n"; |
| 308 | OS << "} // end namespace llvm\n"; |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 309 | OS << "#endif //GET_INSTRINFO_NAMED_OPS\n\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 312 | /// Generate an enum for all the operand types for this target, under the |
| 313 | /// llvm::TargetNamespace::OpTypes namespace. |
| 314 | /// Operand types are all definitions derived of the Operand Target.td class. |
| 315 | void InstrInfoEmitter::emitOperandTypesEnum(raw_ostream &OS, |
| 316 | const CodeGenTarget &Target) { |
| 317 | |
Craig Topper | 86a9aee | 2017-07-07 06:22:35 +0000 | [diff] [blame] | 318 | StringRef Namespace = Target.getInstNamespace(); |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 319 | std::vector<Record *> Operands = Records.getAllDerivedDefinitions("Operand"); |
| 320 | |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 321 | OS << "#ifdef GET_INSTRINFO_OPERAND_TYPES_ENUM\n"; |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 322 | OS << "#undef GET_INSTRINFO_OPERAND_TYPES_ENUM\n"; |
Eric Christopher | 69c0a7c | 2015-03-13 01:10:08 +0000 | [diff] [blame] | 323 | OS << "namespace llvm {\n"; |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 324 | OS << "namespace " << Namespace << " {\n"; |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 325 | OS << "namespace OpTypes {\n"; |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 326 | OS << "enum OperandType {\n"; |
| 327 | |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 328 | unsigned EnumVal = 0; |
| 329 | for (const Record *Op : Operands) { |
| 330 | if (!Op->isAnonymous()) |
| 331 | OS << " " << Op->getName() << " = " << EnumVal << ",\n"; |
| 332 | ++EnumVal; |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | OS << " OPERAND_TYPE_LIST_END" << "\n};\n"; |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 336 | OS << "} // end namespace OpTypes\n"; |
| 337 | OS << "} // end namespace " << Namespace << "\n"; |
| 338 | OS << "} // end namespace llvm\n"; |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 339 | OS << "#endif // GET_INSTRINFO_OPERAND_TYPES_ENUM\n\n"; |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 342 | //===----------------------------------------------------------------------===// |
| 343 | // Main Output. |
| 344 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 345 | |
| 346 | // run - Emit the main instruction description records for the target... |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 347 | void InstrInfoEmitter::run(raw_ostream &OS) { |
Craig Topper | a3ebc4f | 2016-02-11 07:39:27 +0000 | [diff] [blame] | 348 | emitSourceFileHeader("Target Instruction Enum Values and Descriptors", OS); |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 349 | emitEnums(OS); |
| 350 | |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 351 | OS << "#ifdef GET_INSTRINFO_MC_DESC\n"; |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 352 | OS << "#undef GET_INSTRINFO_MC_DESC\n"; |
| 353 | |
Chris Lattner | c9d99ef | 2004-08-17 03:08:28 +0000 | [diff] [blame] | 354 | OS << "namespace llvm {\n\n"; |
| 355 | |
Dan Gohman | fc4ad7de | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 356 | CodeGenTarget &Target = CDP.getTargetInfo(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 357 | const std::string &TargetName = Target.getName(); |
| 358 | Record *InstrInfo = Target.getInstructionSet(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 360 | // Keep track of all of the def lists we have emitted already. |
| 361 | std::map<std::vector<Record*>, unsigned> EmittedLists; |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 362 | unsigned ListNumber = 0; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 364 | // Emit all of the instruction's implicit uses and defs. |
Craig Topper | 8cc904d | 2016-01-17 20:38:18 +0000 | [diff] [blame] | 365 | for (const CodeGenInstruction *II : Target.getInstructionsByEnumValue()) { |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 366 | Record *Inst = II->TheDef; |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 367 | std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses"); |
| 368 | if (!Uses.empty()) { |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 369 | unsigned &IL = EmittedLists[Uses]; |
Chris Lattner | 85467a1 | 2008-01-06 01:21:51 +0000 | [diff] [blame] | 370 | if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS); |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 371 | } |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 372 | std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs"); |
| 373 | if (!Defs.empty()) { |
| 374 | unsigned &IL = EmittedLists[Defs]; |
Chris Lattner | 85467a1 | 2008-01-06 01:21:51 +0000 | [diff] [blame] | 375 | if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS); |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 376 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 379 | OperandInfoMapTy OperandInfoIDs; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 220d001 | 2005-08-19 16:57:28 +0000 | [diff] [blame] | 381 | // Emit all of the operand info records. |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 382 | EmitOperandInfo(OS, OperandInfoIDs); |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 383 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 384 | // Emit all of the MCInstrDesc records in their ENUM ordering. |
Chris Lattner | 220d001 | 2005-08-19 16:57:28 +0000 | [diff] [blame] | 385 | // |
Benjamin Kramer | 0d6d098 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 386 | OS << "\nextern const MCInstrDesc " << TargetName << "Insts[] = {\n"; |
Craig Topper | 28851b6 | 2016-02-01 01:33:42 +0000 | [diff] [blame] | 387 | ArrayRef<const CodeGenInstruction*> NumberedInstructions = |
Chris Lattner | 918be52 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 388 | Target.getInstructionsByEnumValue(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 389 | |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 390 | SequenceToOffsetTable<std::string> InstrNames; |
| 391 | unsigned Num = 0; |
| 392 | for (const CodeGenInstruction *Inst : NumberedInstructions) { |
| 393 | // Keep a list of the instruction names. |
| 394 | InstrNames.add(Inst->TheDef->getName()); |
| 395 | // Emit the record into the table. |
| 396 | emitRecord(*Inst, Num++, InstrInfo, EmittedLists, OperandInfoIDs, OS); |
| 397 | } |
Evan Cheng | df8974e | 2011-06-28 20:29:03 +0000 | [diff] [blame] | 398 | OS << "};\n\n"; |
| 399 | |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 400 | // Emit the array of instruction names. |
Craig Topper | 91773ab | 2012-04-01 18:14:14 +0000 | [diff] [blame] | 401 | InstrNames.layout(); |
| 402 | OS << "extern const char " << TargetName << "InstrNameData[] = {\n"; |
| 403 | InstrNames.emit(OS, printChar); |
| 404 | OS << "};\n\n"; |
| 405 | |
| 406 | OS << "extern const unsigned " << TargetName <<"InstrNameIndices[] = {"; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 407 | Num = 0; |
| 408 | for (const CodeGenInstruction *Inst : NumberedInstructions) { |
| 409 | // Newline every eight entries. |
| 410 | if (Num % 8 == 0) |
Benjamin Kramer | bf152d5 | 2012-02-10 13:18:44 +0000 | [diff] [blame] | 411 | OS << "\n "; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 412 | OS << InstrNames.get(Inst->TheDef->getName()) << "U, "; |
| 413 | ++Num; |
Benjamin Kramer | bf152d5 | 2012-02-10 13:18:44 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | OS << "\n};\n\n"; |
| 417 | |
Evan Cheng | df8974e | 2011-06-28 20:29:03 +0000 | [diff] [blame] | 418 | // MCInstrInfo initialization routine. |
| 419 | OS << "static inline void Init" << TargetName |
| 420 | << "MCInstrInfo(MCInstrInfo *II) {\n"; |
| 421 | OS << " II->InitMCInstrInfo(" << TargetName << "Insts, " |
Benjamin Kramer | bf152d5 | 2012-02-10 13:18:44 +0000 | [diff] [blame] | 422 | << TargetName << "InstrNameIndices, " << TargetName << "InstrNameData, " |
Evan Cheng | df8974e | 2011-06-28 20:29:03 +0000 | [diff] [blame] | 423 | << NumberedInstructions.size() << ");\n}\n\n"; |
| 424 | |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 425 | OS << "} // end llvm namespace\n"; |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 426 | |
| 427 | OS << "#endif // GET_INSTRINFO_MC_DESC\n\n"; |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 428 | |
| 429 | // Create a TargetInstrInfo subclass to hide the MC layer initialization. |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 430 | OS << "#ifdef GET_INSTRINFO_HEADER\n"; |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 431 | OS << "#undef GET_INSTRINFO_HEADER\n"; |
| 432 | |
| 433 | std::string ClassName = TargetName + "GenInstrInfo"; |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 434 | OS << "namespace llvm {\n"; |
Jakob Stoklund Olesen | 9de596e | 2012-11-28 02:35:17 +0000 | [diff] [blame] | 435 | OS << "struct " << ClassName << " : public TargetInstrInfo {\n" |
Eric Christopher | 44fd498 | 2015-03-13 01:26:39 +0000 | [diff] [blame] | 436 | << " explicit " << ClassName |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 437 | << "(int CFSetupOpcode = -1, int CFDestroyOpcode = -1, int CatchRetOpcode = -1, int ReturnOpcode = -1);\n" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 438 | << " ~" << ClassName << "() override = default;\n" |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 439 | << "};\n"; |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 440 | OS << "} // end llvm namespace\n"; |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 441 | |
| 442 | OS << "#endif // GET_INSTRINFO_HEADER\n\n"; |
| 443 | |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 444 | OS << "#ifdef GET_INSTRINFO_CTOR_DTOR\n"; |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 445 | OS << "#undef GET_INSTRINFO_CTOR_DTOR\n"; |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 446 | |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 447 | OS << "namespace llvm {\n"; |
Benjamin Kramer | 0d6d098 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 448 | OS << "extern const MCInstrDesc " << TargetName << "Insts[];\n"; |
Jakob Stoklund Olesen | e308489 | 2012-03-15 18:05:57 +0000 | [diff] [blame] | 449 | OS << "extern const unsigned " << TargetName << "InstrNameIndices[];\n"; |
Craig Topper | 91773ab | 2012-04-01 18:14:14 +0000 | [diff] [blame] | 450 | OS << "extern const char " << TargetName << "InstrNameData[];\n"; |
Eric Christopher | 44fd498 | 2015-03-13 01:26:39 +0000 | [diff] [blame] | 451 | OS << ClassName << "::" << ClassName |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 452 | << "(int CFSetupOpcode, int CFDestroyOpcode, int CatchRetOpcode, int ReturnOpcode)\n" |
| 453 | << " : TargetInstrInfo(CFSetupOpcode, CFDestroyOpcode, CatchRetOpcode, ReturnOpcode) {\n" |
Eric Christopher | 44fd498 | 2015-03-13 01:26:39 +0000 | [diff] [blame] | 454 | << " InitMCInstrInfo(" << TargetName << "Insts, " << TargetName |
| 455 | << "InstrNameIndices, " << TargetName << "InstrNameData, " |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 456 | << NumberedInstructions.size() << ");\n}\n"; |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 457 | OS << "} // end llvm namespace\n"; |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 458 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 459 | OS << "#endif // GET_INSTRINFO_CTOR_DTOR\n\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 460 | |
| 461 | emitOperandNameMappings(OS, Target, NumberedInstructions); |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 462 | |
| 463 | emitOperandTypesEnum(OS, Target); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Chris Lattner | c860eca | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 466 | void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num, |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 467 | Record *InstrInfo, |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 468 | std::map<std::vector<Record*>, unsigned> &EmittedLists, |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 469 | const OperandInfoMapTy &OpInfo, |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 470 | raw_ostream &OS) { |
Chris Lattner | 27a4c15 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 471 | int MinOperands = 0; |
Richard Trieu | 2448969 | 2012-10-12 17:57:35 +0000 | [diff] [blame] | 472 | if (!Inst.Operands.empty()) |
Chris Lattner | 511ee68 | 2005-08-19 00:59:49 +0000 | [diff] [blame] | 473 | // Each logical operand can be multiple MI operands. |
Chris Lattner | d8adec7 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 474 | MinOperands = Inst.Operands.back().MIOperandNo + |
| 475 | Inst.Operands.back().MINumOperands; |
Dan Gohman | 6e582c4 | 2008-05-29 19:57:41 +0000 | [diff] [blame] | 476 | |
Evan Cheng | 71adba6 | 2006-11-17 01:46:27 +0000 | [diff] [blame] | 477 | OS << " { "; |
Evan Cheng | c9de9ce | 2007-08-02 00:20:17 +0000 | [diff] [blame] | 478 | OS << Num << ",\t" << MinOperands << ",\t" |
Owen Anderson | 651b230 | 2011-07-13 23:22:26 +0000 | [diff] [blame] | 479 | << Inst.Operands.NumDefs << ",\t" |
Owen Anderson | ca54800 | 2015-05-28 18:03:07 +0000 | [diff] [blame] | 480 | << Inst.TheDef->getValueAsInt("Size") << ",\t" |
| 481 | << SchedModels.getSchedClassIdx(Inst) << ",\t0"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 482 | |
Eric Christopher | 834d642 | 2015-02-26 00:00:33 +0000 | [diff] [blame] | 483 | // Emit all of the target independent flags... |
Owen Anderson | abaa523 | 2015-05-28 18:33:39 +0000 | [diff] [blame] | 484 | if (Inst.isPseudo) OS << "|(1ULL<<MCID::Pseudo)"; |
| 485 | if (Inst.isReturn) OS << "|(1ULL<<MCID::Return)"; |
| 486 | if (Inst.isBranch) OS << "|(1ULL<<MCID::Branch)"; |
| 487 | if (Inst.isIndirectBranch) OS << "|(1ULL<<MCID::IndirectBranch)"; |
| 488 | if (Inst.isCompare) OS << "|(1ULL<<MCID::Compare)"; |
| 489 | if (Inst.isMoveImm) OS << "|(1ULL<<MCID::MoveImm)"; |
| 490 | if (Inst.isBitcast) OS << "|(1ULL<<MCID::Bitcast)"; |
Sjoerd Meijer | 724023a | 2016-09-14 08:20:03 +0000 | [diff] [blame] | 491 | if (Inst.isAdd) OS << "|(1ULL<<MCID::Add)"; |
Owen Anderson | abaa523 | 2015-05-28 18:33:39 +0000 | [diff] [blame] | 492 | if (Inst.isSelect) OS << "|(1ULL<<MCID::Select)"; |
| 493 | if (Inst.isBarrier) OS << "|(1ULL<<MCID::Barrier)"; |
| 494 | if (Inst.hasDelaySlot) OS << "|(1ULL<<MCID::DelaySlot)"; |
| 495 | if (Inst.isCall) OS << "|(1ULL<<MCID::Call)"; |
| 496 | if (Inst.canFoldAsLoad) OS << "|(1ULL<<MCID::FoldableAsLoad)"; |
| 497 | if (Inst.mayLoad) OS << "|(1ULL<<MCID::MayLoad)"; |
| 498 | if (Inst.mayStore) OS << "|(1ULL<<MCID::MayStore)"; |
| 499 | if (Inst.isPredicable) OS << "|(1ULL<<MCID::Predicable)"; |
| 500 | if (Inst.isConvertibleToThreeAddress) OS << "|(1ULL<<MCID::ConvertibleTo3Addr)"; |
| 501 | if (Inst.isCommutable) OS << "|(1ULL<<MCID::Commutable)"; |
| 502 | if (Inst.isTerminator) OS << "|(1ULL<<MCID::Terminator)"; |
| 503 | if (Inst.isReMaterializable) OS << "|(1ULL<<MCID::Rematerializable)"; |
| 504 | if (Inst.isNotDuplicable) OS << "|(1ULL<<MCID::NotDuplicable)"; |
| 505 | if (Inst.Operands.hasOptionalDef) OS << "|(1ULL<<MCID::HasOptionalDef)"; |
| 506 | if (Inst.usesCustomInserter) OS << "|(1ULL<<MCID::UsesCustomInserter)"; |
| 507 | if (Inst.hasPostISelHook) OS << "|(1ULL<<MCID::HasPostISelHook)"; |
| 508 | if (Inst.Operands.isVariadic)OS << "|(1ULL<<MCID::Variadic)"; |
| 509 | if (Inst.hasSideEffects) OS << "|(1ULL<<MCID::UnmodeledSideEffects)"; |
| 510 | if (Inst.isAsCheapAsAMove) OS << "|(1ULL<<MCID::CheapAsAMove)"; |
| 511 | if (Inst.hasExtraSrcRegAllocReq) OS << "|(1ULL<<MCID::ExtraSrcRegAllocReq)"; |
| 512 | if (Inst.hasExtraDefRegAllocReq) OS << "|(1ULL<<MCID::ExtraDefRegAllocReq)"; |
| 513 | if (Inst.isRegSequence) OS << "|(1ULL<<MCID::RegSequence)"; |
| 514 | if (Inst.isExtractSubreg) OS << "|(1ULL<<MCID::ExtractSubreg)"; |
| 515 | if (Inst.isInsertSubreg) OS << "|(1ULL<<MCID::InsertSubreg)"; |
| 516 | if (Inst.isConvergent) OS << "|(1ULL<<MCID::Convergent)"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 517 | |
| 518 | // Emit all of the target-specific flags... |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 519 | BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags"); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 520 | if (!TSF) |
| 521 | PrintFatalError("no TSFlags?"); |
Jakob Stoklund Olesen | b93331f | 2010-04-05 03:10:20 +0000 | [diff] [blame] | 522 | uint64_t Value = 0; |
| 523 | for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) { |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 524 | if (const auto *Bit = dyn_cast<BitInit>(TSF->getBit(i))) |
Jakob Stoklund Olesen | b93331f | 2010-04-05 03:10:20 +0000 | [diff] [blame] | 525 | Value |= uint64_t(Bit->getValue()) << i; |
| 526 | else |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 527 | PrintFatalError("Invalid TSFlags bit in " + Inst.TheDef->getName()); |
Jakob Stoklund Olesen | b93331f | 2010-04-05 03:10:20 +0000 | [diff] [blame] | 528 | } |
| 529 | OS << ", 0x"; |
| 530 | OS.write_hex(Value); |
Eric Christopher | 223c481 | 2010-06-09 16:16:48 +0000 | [diff] [blame] | 531 | OS << "ULL, "; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 532 | |
| 533 | // Emit the implicit uses and defs lists... |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 534 | std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses"); |
| 535 | if (UseList.empty()) |
Craig Topper | 948dfbf | 2014-04-30 05:53:35 +0000 | [diff] [blame] | 536 | OS << "nullptr, "; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 537 | else |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 538 | OS << "ImplicitList" << EmittedLists[UseList] << ", "; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 539 | |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 540 | std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs"); |
| 541 | if (DefList.empty()) |
Craig Topper | 948dfbf | 2014-04-30 05:53:35 +0000 | [diff] [blame] | 542 | OS << "nullptr, "; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 543 | else |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 544 | OS << "ImplicitList" << EmittedLists[DefList] << ", "; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 220d001 | 2005-08-19 16:57:28 +0000 | [diff] [blame] | 546 | // Emit the operand info. |
Chris Lattner | 33f5a51 | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 547 | std::vector<std::string> OperandInfo = GetOperandInfo(Inst); |
Chris Lattner | d02bd5b | 2005-08-19 18:46:26 +0000 | [diff] [blame] | 548 | if (OperandInfo.empty()) |
Craig Topper | 948dfbf | 2014-04-30 05:53:35 +0000 | [diff] [blame] | 549 | OS << "nullptr"; |
Chris Lattner | 220d001 | 2005-08-19 16:57:28 +0000 | [diff] [blame] | 550 | else |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 551 | OS << "OperandInfo" << OpInfo.find(OperandInfo)->second; |
Jakob Stoklund Olesen | b93331f | 2010-04-05 03:10:20 +0000 | [diff] [blame] | 552 | |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 553 | CodeGenTarget &Target = CDP.getTargetInfo(); |
| 554 | if (Inst.HasComplexDeprecationPredicate) |
| 555 | // Emit a function pointer to the complex predicate method. |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 556 | OS << ", -1 " |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 557 | << ",&get" << Inst.DeprecatedReason << "DeprecationInfo"; |
| 558 | else if (!Inst.DeprecatedReason.empty()) |
| 559 | // Emit the Subtarget feature. |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 560 | OS << ", " << Target.getInstNamespace() << "::" << Inst.DeprecatedReason |
| 561 | << " ,nullptr"; |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 562 | else |
| 563 | // Instruction isn't deprecated. |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 564 | OS << ", -1 ,nullptr"; |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 565 | |
Chris Lattner | c860eca | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 566 | OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 567 | } |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 568 | |
| 569 | // emitEnums - Print out enum values for all of the instructions. |
| 570 | void InstrInfoEmitter::emitEnums(raw_ostream &OS) { |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 571 | OS << "#ifdef GET_INSTRINFO_ENUM\n"; |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 572 | OS << "#undef GET_INSTRINFO_ENUM\n"; |
| 573 | |
| 574 | OS << "namespace llvm {\n\n"; |
| 575 | |
| 576 | CodeGenTarget Target(Records); |
| 577 | |
| 578 | // We must emit the PHI opcode first... |
Craig Topper | 86a9aee | 2017-07-07 06:22:35 +0000 | [diff] [blame] | 579 | StringRef Namespace = Target.getInstNamespace(); |
Jim Grosbach | dac4a95 | 2012-04-11 21:02:30 +0000 | [diff] [blame] | 580 | |
James Y Knight | e452e27 | 2015-05-11 22:17:13 +0000 | [diff] [blame] | 581 | if (Namespace.empty()) |
| 582 | PrintFatalError("No instructions defined!"); |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 583 | |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 584 | OS << "namespace " << Namespace << " {\n"; |
| 585 | OS << " enum {\n"; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 586 | unsigned Num = 0; |
Craig Topper | e16efd9 | 2016-02-11 07:39:22 +0000 | [diff] [blame] | 587 | for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 588 | OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n"; |
Craig Topper | e16efd9 | 2016-02-11 07:39:22 +0000 | [diff] [blame] | 589 | OS << " INSTRUCTION_LIST_END = " << Num << "\n"; |
Craig Topper | ca331be | 2014-11-23 09:40:10 +0000 | [diff] [blame] | 590 | OS << " };\n\n"; |
Vincent Lejeune | 7deccf0 | 2013-09-03 19:43:28 +0000 | [diff] [blame] | 591 | OS << "namespace Sched {\n"; |
| 592 | OS << " enum {\n"; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame] | 593 | Num = 0; |
| 594 | for (const auto &Class : SchedModels.explicit_classes()) |
| 595 | OS << " " << Class.Name << "\t= " << Num++ << ",\n"; |
Craig Topper | e16efd9 | 2016-02-11 07:39:22 +0000 | [diff] [blame] | 596 | OS << " SCHED_LIST_END = " << Num << "\n"; |
Craig Topper | ca331be | 2014-11-23 09:40:10 +0000 | [diff] [blame] | 597 | OS << " };\n"; |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 598 | OS << "} // end Sched namespace\n"; |
| 599 | OS << "} // end " << Namespace << " namespace\n"; |
Craig Topper | fc1b4d4 | 2016-02-11 07:39:25 +0000 | [diff] [blame] | 600 | OS << "} // end llvm namespace\n"; |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 601 | |
| 602 | OS << "#endif // GET_INSTRINFO_ENUM\n\n"; |
| 603 | } |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 604 | |
| 605 | namespace llvm { |
| 606 | |
| 607 | void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) { |
| 608 | InstrInfoEmitter(RK).run(OS); |
Sebastian Pop | 5c87daf | 2012-10-25 15:54:06 +0000 | [diff] [blame] | 609 | EmitMapTable(RK, OS); |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 612 | } // end llvm namespace |