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