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