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. |
| 146 | Res += ", MCOI::"; |
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"; |
| 269 | OS << "int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx) {\n"; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 270 | if (!Operands.empty()) { |
| 271 | OS << " static const int16_t OperandMap [][" << Operands.size() |
| 272 | << "] = {\n"; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 273 | for (const auto &Entry : OperandMap) { |
| 274 | const std::map<unsigned, unsigned> &OpList = Entry.first; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 275 | OS << "{"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 276 | |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 277 | // Emit a row of the OperandMap table |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 278 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 279 | OS << (OpList.count(i) == 0 ? -1 : (int)OpList.find(i)->second) << ", "; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 280 | |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 281 | OS << "},\n"; |
| 282 | } |
| 283 | OS << "};\n"; |
| 284 | |
| 285 | OS << " switch(Opcode) {\n"; |
| 286 | unsigned TableIndex = 0; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 287 | for (const auto &Entry : OperandMap) { |
| 288 | for (const std::string &Name : Entry.second) |
| 289 | OS << " case " << Name << ":\n"; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 290 | |
| 291 | OS << " return OperandMap[" << TableIndex++ << "][NamedIdx];\n"; |
| 292 | } |
| 293 | OS << " default: return -1;\n"; |
| 294 | OS << " }\n"; |
| 295 | } else { |
| 296 | // There are no operands, so no need to emit anything |
| 297 | OS << " return -1;\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 298 | } |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 299 | OS << "}\n"; |
| 300 | OS << "} // End namespace " << Namespace << "\n"; |
| 301 | OS << "} // End namespace llvm\n"; |
| 302 | OS << "#endif //GET_INSTRINFO_NAMED_OPS\n"; |
| 303 | |
| 304 | } |
| 305 | |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 306 | /// Generate an enum for all the operand types for this target, under the |
| 307 | /// llvm::TargetNamespace::OpTypes namespace. |
| 308 | /// Operand types are all definitions derived of the Operand Target.td class. |
| 309 | void InstrInfoEmitter::emitOperandTypesEnum(raw_ostream &OS, |
| 310 | const CodeGenTarget &Target) { |
| 311 | |
| 312 | const std::string &Namespace = Target.getInstNamespace(); |
| 313 | std::vector<Record *> Operands = Records.getAllDerivedDefinitions("Operand"); |
| 314 | |
| 315 | OS << "\n#ifdef GET_INSTRINFO_OPERAND_TYPES_ENUM\n"; |
| 316 | OS << "#undef GET_INSTRINFO_OPERAND_TYPES_ENUM\n"; |
| 317 | OS << "namespace llvm {"; |
| 318 | OS << "namespace " << Namespace << " {\n"; |
| 319 | OS << "namespace OpTypes { \n"; |
| 320 | OS << "enum OperandType {\n"; |
| 321 | |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 322 | unsigned EnumVal = 0; |
| 323 | for (const Record *Op : Operands) { |
| 324 | if (!Op->isAnonymous()) |
| 325 | OS << " " << Op->getName() << " = " << EnumVal << ",\n"; |
| 326 | ++EnumVal; |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | OS << " OPERAND_TYPE_LIST_END" << "\n};\n"; |
| 330 | OS << "} // End namespace OpTypes\n"; |
| 331 | OS << "} // End namespace " << Namespace << "\n"; |
| 332 | OS << "} // End namespace llvm\n"; |
| 333 | OS << "#endif // GET_INSTRINFO_OPERAND_TYPES_ENUM\n"; |
| 334 | } |
| 335 | |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 336 | //===----------------------------------------------------------------------===// |
| 337 | // Main Output. |
| 338 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 339 | |
| 340 | // run - Emit the main instruction description records for the target... |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 341 | void InstrInfoEmitter::run(raw_ostream &OS) { |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 342 | emitSourceFileHeader("Target Instruction Enum Values", OS); |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 343 | emitEnums(OS); |
| 344 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 345 | emitSourceFileHeader("Target Instruction Descriptors", OS); |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 346 | |
| 347 | OS << "\n#ifdef GET_INSTRINFO_MC_DESC\n"; |
| 348 | OS << "#undef GET_INSTRINFO_MC_DESC\n"; |
| 349 | |
Chris Lattner | c9d99ef | 2004-08-17 03:08:28 +0000 | [diff] [blame] | 350 | OS << "namespace llvm {\n\n"; |
| 351 | |
Dan Gohman | fc4ad7de | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 352 | CodeGenTarget &Target = CDP.getTargetInfo(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 353 | const std::string &TargetName = Target.getName(); |
| 354 | Record *InstrInfo = Target.getInstructionSet(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 356 | // Keep track of all of the def lists we have emitted already. |
| 357 | std::map<std::vector<Record*>, unsigned> EmittedLists; |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 358 | unsigned ListNumber = 0; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 360 | // Emit all of the instruction's implicit uses and defs. |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 361 | for (const CodeGenInstruction *II : Target.instructions()) { |
| 362 | Record *Inst = II->TheDef; |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 363 | std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses"); |
| 364 | if (!Uses.empty()) { |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 365 | unsigned &IL = EmittedLists[Uses]; |
Chris Lattner | 85467a1 | 2008-01-06 01:21:51 +0000 | [diff] [blame] | 366 | if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS); |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 368 | std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs"); |
| 369 | if (!Defs.empty()) { |
| 370 | unsigned &IL = EmittedLists[Defs]; |
Chris Lattner | 85467a1 | 2008-01-06 01:21:51 +0000 | [diff] [blame] | 371 | if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS); |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 372 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 375 | OperandInfoMapTy OperandInfoIDs; |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 220d001 | 2005-08-19 16:57:28 +0000 | [diff] [blame] | 377 | // Emit all of the operand info records. |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 378 | EmitOperandInfo(OS, OperandInfoIDs); |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 379 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 380 | // Emit all of the MCInstrDesc records in their ENUM ordering. |
Chris Lattner | 220d001 | 2005-08-19 16:57:28 +0000 | [diff] [blame] | 381 | // |
Benjamin Kramer | 0d6d098 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 382 | OS << "\nextern const MCInstrDesc " << TargetName << "Insts[] = {\n"; |
Chris Lattner | 918be52 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 383 | const std::vector<const CodeGenInstruction*> &NumberedInstructions = |
| 384 | Target.getInstructionsByEnumValue(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 385 | |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 386 | SequenceToOffsetTable<std::string> InstrNames; |
| 387 | unsigned Num = 0; |
| 388 | for (const CodeGenInstruction *Inst : NumberedInstructions) { |
| 389 | // Keep a list of the instruction names. |
| 390 | InstrNames.add(Inst->TheDef->getName()); |
| 391 | // Emit the record into the table. |
| 392 | emitRecord(*Inst, Num++, InstrInfo, EmittedLists, OperandInfoIDs, OS); |
| 393 | } |
Evan Cheng | df8974e | 2011-06-28 20:29:03 +0000 | [diff] [blame] | 394 | OS << "};\n\n"; |
| 395 | |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 396 | // Emit the array of instruction names. |
Craig Topper | 91773ab | 2012-04-01 18:14:14 +0000 | [diff] [blame] | 397 | InstrNames.layout(); |
| 398 | OS << "extern const char " << TargetName << "InstrNameData[] = {\n"; |
| 399 | InstrNames.emit(OS, printChar); |
| 400 | OS << "};\n\n"; |
| 401 | |
| 402 | OS << "extern const unsigned " << TargetName <<"InstrNameIndices[] = {"; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 403 | Num = 0; |
| 404 | for (const CodeGenInstruction *Inst : NumberedInstructions) { |
| 405 | // Newline every eight entries. |
| 406 | if (Num % 8 == 0) |
Benjamin Kramer | bf152d5 | 2012-02-10 13:18:44 +0000 | [diff] [blame] | 407 | OS << "\n "; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 408 | OS << InstrNames.get(Inst->TheDef->getName()) << "U, "; |
| 409 | ++Num; |
Benjamin Kramer | bf152d5 | 2012-02-10 13:18:44 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | OS << "\n};\n\n"; |
| 413 | |
Evan Cheng | df8974e | 2011-06-28 20:29:03 +0000 | [diff] [blame] | 414 | // MCInstrInfo initialization routine. |
| 415 | OS << "static inline void Init" << TargetName |
| 416 | << "MCInstrInfo(MCInstrInfo *II) {\n"; |
| 417 | OS << " II->InitMCInstrInfo(" << TargetName << "Insts, " |
Benjamin Kramer | bf152d5 | 2012-02-10 13:18:44 +0000 | [diff] [blame] | 418 | << TargetName << "InstrNameIndices, " << TargetName << "InstrNameData, " |
Evan Cheng | df8974e | 2011-06-28 20:29:03 +0000 | [diff] [blame] | 419 | << NumberedInstructions.size() << ");\n}\n\n"; |
| 420 | |
Chris Lattner | c9d99ef | 2004-08-17 03:08:28 +0000 | [diff] [blame] | 421 | OS << "} // End llvm namespace \n"; |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 422 | |
| 423 | OS << "#endif // GET_INSTRINFO_MC_DESC\n\n"; |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 424 | |
| 425 | // Create a TargetInstrInfo subclass to hide the MC layer initialization. |
| 426 | OS << "\n#ifdef GET_INSTRINFO_HEADER\n"; |
| 427 | OS << "#undef GET_INSTRINFO_HEADER\n"; |
| 428 | |
| 429 | std::string ClassName = TargetName + "GenInstrInfo"; |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 430 | OS << "namespace llvm {\n"; |
Jakob Stoklund Olesen | 9de596e | 2012-11-28 02:35:17 +0000 | [diff] [blame] | 431 | OS << "struct " << ClassName << " : public TargetInstrInfo {\n" |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 432 | << " explicit " << ClassName << "(int SO = -1, int DO = -1);\n" |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 433 | << " virtual ~" << ClassName << "();\n" |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 434 | << "};\n"; |
| 435 | OS << "} // End llvm namespace \n"; |
| 436 | |
| 437 | OS << "#endif // GET_INSTRINFO_HEADER\n\n"; |
| 438 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 439 | OS << "\n#ifdef GET_INSTRINFO_CTOR_DTOR\n"; |
| 440 | OS << "#undef GET_INSTRINFO_CTOR_DTOR\n"; |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 441 | |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 442 | OS << "namespace llvm {\n"; |
Benjamin Kramer | 0d6d098 | 2011-10-22 16:50:00 +0000 | [diff] [blame] | 443 | OS << "extern const MCInstrDesc " << TargetName << "Insts[];\n"; |
Jakob Stoklund Olesen | e308489 | 2012-03-15 18:05:57 +0000 | [diff] [blame] | 444 | OS << "extern const unsigned " << TargetName << "InstrNameIndices[];\n"; |
Craig Topper | 91773ab | 2012-04-01 18:14:14 +0000 | [diff] [blame] | 445 | OS << "extern const char " << TargetName << "InstrNameData[];\n"; |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 446 | OS << ClassName << "::" << ClassName << "(int SO, int DO)\n" |
Jakob Stoklund Olesen | 9de596e | 2012-11-28 02:35:17 +0000 | [diff] [blame] | 447 | << " : TargetInstrInfo(SO, DO) {\n" |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 448 | << " InitMCInstrInfo(" << TargetName << "Insts, " |
Benjamin Kramer | bf152d5 | 2012-02-10 13:18:44 +0000 | [diff] [blame] | 449 | << TargetName << "InstrNameIndices, " << TargetName << "InstrNameData, " |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 450 | << NumberedInstructions.size() << ");\n}\n" |
| 451 | << ClassName << "::~" << ClassName << "() {}\n"; |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 452 | OS << "} // End llvm namespace \n"; |
| 453 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 454 | OS << "#endif // GET_INSTRINFO_CTOR_DTOR\n\n"; |
Tom Stellard | b162d94 | 2013-06-25 21:22:09 +0000 | [diff] [blame] | 455 | |
| 456 | emitOperandNameMappings(OS, Target, NumberedInstructions); |
Ahmed Bougacha | 3137759 | 2013-11-17 21:24:41 +0000 | [diff] [blame] | 457 | |
| 458 | emitOperandTypesEnum(OS, Target); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Chris Lattner | c860eca | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 461 | void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num, |
Chris Lattner | 99df668 | 2005-08-18 21:36:47 +0000 | [diff] [blame] | 462 | Record *InstrInfo, |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 463 | std::map<std::vector<Record*>, unsigned> &EmittedLists, |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 464 | const OperandInfoMapTy &OpInfo, |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 465 | raw_ostream &OS) { |
Chris Lattner | 27a4c15 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 466 | int MinOperands = 0; |
Richard Trieu | 2448969 | 2012-10-12 17:57:35 +0000 | [diff] [blame] | 467 | if (!Inst.Operands.empty()) |
Chris Lattner | 511ee68 | 2005-08-19 00:59:49 +0000 | [diff] [blame] | 468 | // Each logical operand can be multiple MI operands. |
Chris Lattner | d8adec7 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 469 | MinOperands = Inst.Operands.back().MIOperandNo + |
| 470 | Inst.Operands.back().MINumOperands; |
Dan Gohman | 6e582c4 | 2008-05-29 19:57:41 +0000 | [diff] [blame] | 471 | |
Evan Cheng | 71adba6 | 2006-11-17 01:46:27 +0000 | [diff] [blame] | 472 | OS << " { "; |
Evan Cheng | c9de9ce | 2007-08-02 00:20:17 +0000 | [diff] [blame] | 473 | OS << Num << ",\t" << MinOperands << ",\t" |
Owen Anderson | 651b230 | 2011-07-13 23:22:26 +0000 | [diff] [blame] | 474 | << Inst.Operands.NumDefs << ",\t" |
Andrew Trick | a88f1bd | 2012-09-18 03:55:55 +0000 | [diff] [blame] | 475 | << SchedModels.getSchedClassIdx(Inst) << ",\t" |
Benjamin Kramer | 8e012f5 | 2012-02-09 11:25:09 +0000 | [diff] [blame] | 476 | << Inst.TheDef->getValueAsInt("Size") << ",\t0"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 477 | |
| 478 | // Emit all of the target indepedent flags... |
Jakob Stoklund Olesen | df977fe | 2011-09-25 19:21:35 +0000 | [diff] [blame] | 479 | if (Inst.isPseudo) OS << "|(1<<MCID::Pseudo)"; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 480 | if (Inst.isReturn) OS << "|(1<<MCID::Return)"; |
| 481 | if (Inst.isBranch) OS << "|(1<<MCID::Branch)"; |
| 482 | if (Inst.isIndirectBranch) OS << "|(1<<MCID::IndirectBranch)"; |
| 483 | if (Inst.isCompare) OS << "|(1<<MCID::Compare)"; |
| 484 | if (Inst.isMoveImm) OS << "|(1<<MCID::MoveImm)"; |
| 485 | if (Inst.isBitcast) OS << "|(1<<MCID::Bitcast)"; |
Jakob Stoklund Olesen | 2382d32 | 2012-08-16 23:11:47 +0000 | [diff] [blame] | 486 | if (Inst.isSelect) OS << "|(1<<MCID::Select)"; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 487 | if (Inst.isBarrier) OS << "|(1<<MCID::Barrier)"; |
| 488 | if (Inst.hasDelaySlot) OS << "|(1<<MCID::DelaySlot)"; |
| 489 | if (Inst.isCall) OS << "|(1<<MCID::Call)"; |
| 490 | if (Inst.canFoldAsLoad) OS << "|(1<<MCID::FoldableAsLoad)"; |
| 491 | if (Inst.mayLoad) OS << "|(1<<MCID::MayLoad)"; |
| 492 | if (Inst.mayStore) OS << "|(1<<MCID::MayStore)"; |
| 493 | if (Inst.isPredicable) OS << "|(1<<MCID::Predicable)"; |
| 494 | if (Inst.isConvertibleToThreeAddress) OS << "|(1<<MCID::ConvertibleTo3Addr)"; |
| 495 | if (Inst.isCommutable) OS << "|(1<<MCID::Commutable)"; |
| 496 | if (Inst.isTerminator) OS << "|(1<<MCID::Terminator)"; |
| 497 | if (Inst.isReMaterializable) OS << "|(1<<MCID::Rematerializable)"; |
| 498 | if (Inst.isNotDuplicable) OS << "|(1<<MCID::NotDuplicable)"; |
| 499 | if (Inst.Operands.hasOptionalDef) OS << "|(1<<MCID::HasOptionalDef)"; |
| 500 | if (Inst.usesCustomInserter) OS << "|(1<<MCID::UsesCustomInserter)"; |
Andrew Trick | 52363bd | 2011-09-20 18:22:31 +0000 | [diff] [blame] | 501 | if (Inst.hasPostISelHook) OS << "|(1<<MCID::HasPostISelHook)"; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 502 | if (Inst.Operands.isVariadic)OS << "|(1<<MCID::Variadic)"; |
| 503 | if (Inst.hasSideEffects) OS << "|(1<<MCID::UnmodeledSideEffects)"; |
| 504 | if (Inst.isAsCheapAsAMove) OS << "|(1<<MCID::CheapAsAMove)"; |
| 505 | if (Inst.hasExtraSrcRegAllocReq) OS << "|(1<<MCID::ExtraSrcRegAllocReq)"; |
| 506 | if (Inst.hasExtraDefRegAllocReq) OS << "|(1<<MCID::ExtraDefRegAllocReq)"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 507 | |
| 508 | // Emit all of the target-specific flags... |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 509 | BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags"); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 510 | if (!TSF) |
| 511 | PrintFatalError("no TSFlags?"); |
Jakob Stoklund Olesen | b93331f | 2010-04-05 03:10:20 +0000 | [diff] [blame] | 512 | uint64_t Value = 0; |
| 513 | for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) { |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 514 | if (BitInit *Bit = dyn_cast<BitInit>(TSF->getBit(i))) |
Jakob Stoklund Olesen | b93331f | 2010-04-05 03:10:20 +0000 | [diff] [blame] | 515 | Value |= uint64_t(Bit->getValue()) << i; |
| 516 | else |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 517 | PrintFatalError("Invalid TSFlags bit in " + Inst.TheDef->getName()); |
Jakob Stoklund Olesen | b93331f | 2010-04-05 03:10:20 +0000 | [diff] [blame] | 518 | } |
| 519 | OS << ", 0x"; |
| 520 | OS.write_hex(Value); |
Eric Christopher | 223c481 | 2010-06-09 16:16:48 +0000 | [diff] [blame] | 521 | OS << "ULL, "; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 522 | |
| 523 | // Emit the implicit uses and defs lists... |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 524 | std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses"); |
| 525 | if (UseList.empty()) |
Jim Laskey | 4b49c23 | 2006-07-21 21:15:20 +0000 | [diff] [blame] | 526 | OS << "NULL, "; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 527 | else |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 528 | OS << "ImplicitList" << EmittedLists[UseList] << ", "; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 529 | |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 530 | std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs"); |
| 531 | if (DefList.empty()) |
Jim Laskey | 4b49c23 | 2006-07-21 21:15:20 +0000 | [diff] [blame] | 532 | OS << "NULL, "; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 533 | else |
Chris Lattner | 742606a | 2005-10-28 22:59:53 +0000 | [diff] [blame] | 534 | OS << "ImplicitList" << EmittedLists[DefList] << ", "; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 535 | |
Chris Lattner | 220d001 | 2005-08-19 16:57:28 +0000 | [diff] [blame] | 536 | // Emit the operand info. |
Chris Lattner | 33f5a51 | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 537 | std::vector<std::string> OperandInfo = GetOperandInfo(Inst); |
Chris Lattner | d02bd5b | 2005-08-19 18:46:26 +0000 | [diff] [blame] | 538 | if (OperandInfo.empty()) |
| 539 | OS << "0"; |
Chris Lattner | 220d001 | 2005-08-19 16:57:28 +0000 | [diff] [blame] | 540 | else |
Chris Lattner | 626b89d | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 541 | OS << "OperandInfo" << OpInfo.find(OperandInfo)->second; |
Jakob Stoklund Olesen | b93331f | 2010-04-05 03:10:20 +0000 | [diff] [blame] | 542 | |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 543 | CodeGenTarget &Target = CDP.getTargetInfo(); |
| 544 | if (Inst.HasComplexDeprecationPredicate) |
| 545 | // Emit a function pointer to the complex predicate method. |
| 546 | OS << ",0" |
| 547 | << ",&get" << Inst.DeprecatedReason << "DeprecationInfo"; |
| 548 | else if (!Inst.DeprecatedReason.empty()) |
| 549 | // Emit the Subtarget feature. |
| 550 | OS << "," << Target.getInstNamespace() << "::" << Inst.DeprecatedReason |
| 551 | << ",0"; |
| 552 | else |
| 553 | // Instruction isn't deprecated. |
| 554 | OS << ",0,0"; |
| 555 | |
Chris Lattner | c860eca | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 556 | OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 557 | } |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 558 | |
| 559 | // emitEnums - Print out enum values for all of the instructions. |
| 560 | void InstrInfoEmitter::emitEnums(raw_ostream &OS) { |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 561 | |
| 562 | OS << "\n#ifdef GET_INSTRINFO_ENUM\n"; |
| 563 | OS << "#undef GET_INSTRINFO_ENUM\n"; |
| 564 | |
| 565 | OS << "namespace llvm {\n\n"; |
| 566 | |
| 567 | CodeGenTarget Target(Records); |
| 568 | |
| 569 | // We must emit the PHI opcode first... |
| 570 | std::string Namespace = Target.getInstNamespace(); |
Jim Grosbach | dac4a95 | 2012-04-11 21:02:30 +0000 | [diff] [blame] | 571 | |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 572 | if (Namespace.empty()) { |
| 573 | fprintf(stderr, "No instructions defined!\n"); |
| 574 | exit(1); |
| 575 | } |
| 576 | |
| 577 | const std::vector<const CodeGenInstruction*> &NumberedInstructions = |
| 578 | Target.getInstructionsByEnumValue(); |
| 579 | |
| 580 | OS << "namespace " << Namespace << " {\n"; |
| 581 | OS << " enum {\n"; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 582 | unsigned Num = 0; |
| 583 | for (const CodeGenInstruction *Inst : NumberedInstructions) |
| 584 | OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n"; |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 585 | OS << " INSTRUCTION_LIST_END = " << NumberedInstructions.size() << "\n"; |
Vincent Lejeune | 7deccf0 | 2013-09-03 19:43:28 +0000 | [diff] [blame] | 586 | OS << " };\n"; |
| 587 | OS << "namespace Sched {\n"; |
| 588 | OS << " enum {\n"; |
Jim Grosbach | 5198f3e9 | 2014-04-18 02:09:07 +0000 | [diff] [blame^] | 589 | Num = 0; |
| 590 | for (const auto &Class : SchedModels.explicit_classes()) |
| 591 | OS << " " << Class.Name << "\t= " << Num++ << ",\n"; |
Vincent Lejeune | 7deccf0 | 2013-09-03 19:43:28 +0000 | [diff] [blame] | 592 | OS << " SCHED_LIST_END = " << SchedModels.numInstrSchedClasses() << "\n"; |
| 593 | OS << " };\n}\n}\n"; |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 594 | OS << "} // End llvm namespace \n"; |
| 595 | |
| 596 | OS << "#endif // GET_INSTRINFO_ENUM\n\n"; |
| 597 | } |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 598 | |
| 599 | namespace llvm { |
| 600 | |
| 601 | void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) { |
| 602 | InstrInfoEmitter(RK).run(OS); |
Sebastian Pop | 5c87daf | 2012-10-25 15:54:06 +0000 | [diff] [blame] | 603 | EmitMapTable(RK, OS); |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | } // End llvm namespace |