blob: 4d3aa5e621c9c749347ed86d1d66fd6ce756019f [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfd6c2f02007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend is responsible for emitting a description of the target
11// instruction set for the code generator.
12//
13//===----------------------------------------------------------------------===//
14
15#include "InstrInfoEmitter.h"
16#include "CodeGenTarget.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "Record.h"
Chris Lattnerf5377682009-08-24 03:52:50 +000018#include "llvm/ADT/StringExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include <algorithm>
20using namespace llvm;
21
Chris Lattner4533a722008-01-06 01:21:51 +000022static void PrintDefList(const std::vector<Record*> &Uses,
Daniel Dunbard4287062009-07-03 00:10:29 +000023 unsigned Num, raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024 OS << "static const unsigned ImplicitList" << Num << "[] = { ";
25 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
26 OS << getQualifiedName(Uses[i]) << ", ";
27 OS << "0 };\n";
28}
29
Evan Cheng25144652008-10-17 21:00:09 +000030static void PrintBarriers(std::vector<Record*> &Barriers,
Daniel Dunbard4287062009-07-03 00:10:29 +000031 unsigned Num, raw_ostream &OS) {
Evan Cheng25144652008-10-17 21:00:09 +000032 OS << "static const TargetRegisterClass* Barriers" << Num << "[] = { ";
33 for (unsigned i = 0, e = Barriers.size(); i != e; ++i)
34 OS << "&" << getQualifiedName(Barriers[i]) << "RegClass, ";
35 OS << "NULL };\n";
36}
37
Chris Lattner0d58d022008-01-06 01:20:13 +000038//===----------------------------------------------------------------------===//
39// Instruction Itinerary Information.
40//===----------------------------------------------------------------------===//
41
Chris Lattner0d58d022008-01-06 01:20:13 +000042void InstrInfoEmitter::GatherItinClasses() {
43 std::vector<Record*> DefList =
44 Records.getAllDerivedDefinitions("InstrItinClass");
Chris Lattner95127fb2010-03-19 01:07:44 +000045 std::sort(DefList.begin(), DefList.end(), LessRecord());
Chris Lattner0d58d022008-01-06 01:20:13 +000046
47 for (unsigned i = 0, N = DefList.size(); i < N; i++)
48 ItinClassMap[DefList[i]->getName()] = i;
49}
50
51unsigned InstrInfoEmitter::getItinClassNumber(const Record *InstRec) {
52 return ItinClassMap[InstRec->getValueAsDef("Itinerary")->getName()];
53}
54
55//===----------------------------------------------------------------------===//
56// Operand Info Emission.
57//===----------------------------------------------------------------------===//
58
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059std::vector<std::string>
60InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) {
61 std::vector<std::string> Result;
62
63 for (unsigned i = 0, e = Inst.OperandList.size(); i != e; ++i) {
64 // Handle aggregate operands and normal operands the same way by expanding
65 // either case into a list of operands for this op.
66 std::vector<CodeGenInstruction::OperandInfo> OperandList;
67
68 // This might be a multiple operand thing. Targets like X86 have
69 // registers in their multi-operand operands. It may also be an anonymous
70 // operand, which has a single operand, but no declared class for the
71 // operand.
72 DagInit *MIOI = Inst.OperandList[i].MIOperandInfo;
73
74 if (!MIOI || MIOI->getNumArgs() == 0) {
75 // Single, anonymous, operand.
76 OperandList.push_back(Inst.OperandList[i]);
77 } else {
78 for (unsigned j = 0, e = Inst.OperandList[i].MINumOperands; j != e; ++j) {
79 OperandList.push_back(Inst.OperandList[i]);
80
81 Record *OpR = dynamic_cast<DefInit*>(MIOI->getArg(j))->getDef();
82 OperandList.back().Rec = OpR;
83 }
84 }
85
86 for (unsigned j = 0, e = OperandList.size(); j != e; ++j) {
87 Record *OpR = OperandList[j].Rec;
88 std::string Res;
89
90 if (OpR->isSubClassOf("RegisterClass"))
91 Res += getQualifiedName(OpR) + "RegClassID, ";
Chris Lattner6a66b292009-07-29 21:10:12 +000092 else if (OpR->isSubClassOf("PointerLikeRegClass"))
93 Res += utostr(OpR->getValueAsInt("RegClassKind")) + ", ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 else
Dan Gohman05bf7d12010-06-18 18:13:55 +000095 // -1 means the operand does not have a fixed register class.
96 Res += "-1, ";
Chris Lattner6a66b292009-07-29 21:10:12 +000097
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 // Fill in applicable flags.
99 Res += "0";
100
101 // Ptr value whose register class is resolved via callback.
Chris Lattner3064b402009-07-29 20:43:05 +0000102 if (OpR->isSubClassOf("PointerLikeRegClass"))
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000103 Res += "|(1<<TOI::LookupPtrRegClass)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
105 // Predicate operands. Check to see if the original unexpanded operand
106 // was of type PredicateOperand.
107 if (Inst.OperandList[i].Rec->isSubClassOf("PredicateOperand"))
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000108 Res += "|(1<<TOI::Predicate)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110 // Optional def operands. Check to see if the original unexpanded operand
111 // was of type OptionalDefOperand.
112 if (Inst.OperandList[i].Rec->isSubClassOf("OptionalDefOperand"))
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000113 Res += "|(1<<TOI::OptionalDef)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
115 // Fill in constraint info.
Chris Lattnerfd38cb12010-02-10 01:45:28 +0000116 Res += ", ";
117
118 const CodeGenInstruction::ConstraintInfo &Constraint =
119 Inst.OperandList[i].Constraints[j];
120 if (Constraint.isNone())
121 Res += "0";
122 else if (Constraint.isEarlyClobber())
123 Res += "(1 << TOI::EARLY_CLOBBER)";
124 else {
125 assert(Constraint.isTied());
126 Res += "((" + utostr(Constraint.getTiedOperand()) +
127 " << 16) | (1 << TOI::TIED_TO))";
128 }
129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 Result.push_back(Res);
131 }
132 }
133
134 return Result;
135}
136
Daniel Dunbard4287062009-07-03 00:10:29 +0000137void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS,
Chris Lattner0d58d022008-01-06 01:20:13 +0000138 OperandInfoMapTy &OperandInfoIDs) {
139 // ID #0 is for no operand info.
140 unsigned OperandListNum = 0;
141 OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum;
142
143 OS << "\n";
144 const CodeGenTarget &Target = CDP.getTargetInfo();
145 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
146 E = Target.inst_end(); II != E; ++II) {
Chris Lattnerd268a352010-03-19 01:00:55 +0000147 std::vector<std::string> OperandInfo = GetOperandInfo(**II);
Chris Lattner0d58d022008-01-06 01:20:13 +0000148 unsigned &N = OperandInfoIDs[OperandInfo];
149 if (N != 0) continue;
150
151 N = ++OperandListNum;
152 OS << "static const TargetOperandInfo OperandInfo" << N << "[] = { ";
153 for (unsigned i = 0, e = OperandInfo.size(); i != e; ++i)
154 OS << "{ " << OperandInfo[i] << " }, ";
155 OS << "};\n";
156 }
157}
158
Evan Cheng25144652008-10-17 21:00:09 +0000159void InstrInfoEmitter::DetectRegisterClassBarriers(std::vector<Record*> &Defs,
160 const std::vector<CodeGenRegisterClass> &RCs,
161 std::vector<Record*> &Barriers) {
162 std::set<Record*> DefSet;
163 unsigned NumDefs = Defs.size();
164 for (unsigned i = 0; i < NumDefs; ++i)
165 DefSet.insert(Defs[i]);
166
167 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
168 const CodeGenRegisterClass &RC = RCs[i];
169 unsigned NumRegs = RC.Elements.size();
170 if (NumRegs > NumDefs)
171 continue; // Can't possibly clobber this RC.
172
173 bool Clobber = true;
174 for (unsigned j = 0; j < NumRegs; ++j) {
175 Record *Reg = RC.Elements[j];
176 if (!DefSet.count(Reg)) {
177 Clobber = false;
178 break;
179 }
180 }
181 if (Clobber)
182 Barriers.push_back(RC.TheDef);
183 }
184}
185
Chris Lattner0d58d022008-01-06 01:20:13 +0000186//===----------------------------------------------------------------------===//
187// Main Output.
188//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189
190// run - Emit the main instruction description records for the target...
Daniel Dunbard4287062009-07-03 00:10:29 +0000191void InstrInfoEmitter::run(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 GatherItinClasses();
193
194 EmitSourceFileHeader("Target Instruction Descriptors", OS);
195 OS << "namespace llvm {\n\n";
196
Dan Gohman907df572008-04-03 00:02:49 +0000197 CodeGenTarget &Target = CDP.getTargetInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 const std::string &TargetName = Target.getName();
199 Record *InstrInfo = Target.getInstructionSet();
Evan Cheng25144652008-10-17 21:00:09 +0000200 const std::vector<CodeGenRegisterClass> &RCs = Target.getRegisterClasses();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201
202 // Keep track of all of the def lists we have emitted already.
203 std::map<std::vector<Record*>, unsigned> EmittedLists;
204 unsigned ListNumber = 0;
Evan Cheng25144652008-10-17 21:00:09 +0000205 std::map<std::vector<Record*>, unsigned> EmittedBarriers;
206 unsigned BarrierNumber = 0;
207 std::map<Record*, unsigned> BarriersMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208
209 // Emit all of the instruction's implicit uses and defs.
210 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
211 E = Target.inst_end(); II != E; ++II) {
Chris Lattnerd268a352010-03-19 01:00:55 +0000212 Record *Inst = (*II)->TheDef;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
214 if (!Uses.empty()) {
215 unsigned &IL = EmittedLists[Uses];
Chris Lattner4533a722008-01-06 01:21:51 +0000216 if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 }
218 std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
219 if (!Defs.empty()) {
Evan Cheng25144652008-10-17 21:00:09 +0000220 std::vector<Record*> RCBarriers;
221 DetectRegisterClassBarriers(Defs, RCs, RCBarriers);
222 if (!RCBarriers.empty()) {
223 unsigned &IB = EmittedBarriers[RCBarriers];
224 if (!IB) PrintBarriers(RCBarriers, IB = ++BarrierNumber, OS);
225 BarriersMap.insert(std::make_pair(Inst, IB));
226 }
227
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 unsigned &IL = EmittedLists[Defs];
Chris Lattner4533a722008-01-06 01:21:51 +0000229 if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 }
231 }
232
Chris Lattner0d58d022008-01-06 01:20:13 +0000233 OperandInfoMapTy OperandInfoIDs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234
235 // Emit all of the operand info records.
Chris Lattner0d58d022008-01-06 01:20:13 +0000236 EmitOperandInfo(OS, OperandInfoIDs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237
Chris Lattner5b930372008-01-07 07:27:27 +0000238 // Emit all of the TargetInstrDesc records in their ENUM ordering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 //
Chris Lattner5b930372008-01-07 07:27:27 +0000240 OS << "\nstatic const TargetInstrDesc " << TargetName
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 << "Insts[] = {\n";
Chris Lattnera2e1d002010-03-19 00:34:35 +0000242 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
243 Target.getInstructionsByEnumValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244
245 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i)
246 emitRecord(*NumberedInstructions[i], i, InstrInfo, EmittedLists,
Evan Cheng25144652008-10-17 21:00:09 +0000247 BarriersMap, OperandInfoIDs, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 OS << "};\n";
249 OS << "} // End llvm namespace \n";
250}
251
252void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
253 Record *InstrInfo,
254 std::map<std::vector<Record*>, unsigned> &EmittedLists,
Evan Cheng25144652008-10-17 21:00:09 +0000255 std::map<Record*, unsigned> &BarriersMap,
Chris Lattner0d58d022008-01-06 01:20:13 +0000256 const OperandInfoMapTy &OpInfo,
Daniel Dunbard4287062009-07-03 00:10:29 +0000257 raw_ostream &OS) {
Chris Lattner44435a72008-01-06 01:53:37 +0000258 int MinOperands = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 if (!Inst.OperandList.empty())
260 // Each logical operand can be multiple MI operands.
261 MinOperands = Inst.OperandList.back().MIOperandNo +
262 Inst.OperandList.back().MINumOperands;
Dan Gohman3329ffe2008-05-29 19:57:41 +0000263
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 OS << " { ";
Evan Cheng84c52f62007-08-02 00:20:17 +0000265 OS << Num << ",\t" << MinOperands << ",\t"
Chris Lattnerbf010e32008-01-07 05:06:49 +0000266 << Inst.NumDefs << ",\t" << getItinClassNumber(Inst.TheDef)
267 << ",\t\"" << Inst.TheDef->getName() << "\", 0";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 // Emit all of the target indepedent flags...
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000270 if (Inst.isReturn) OS << "|(1<<TID::Return)";
271 if (Inst.isBranch) OS << "|(1<<TID::Branch)";
272 if (Inst.isIndirectBranch) OS << "|(1<<TID::IndirectBranch)";
Bill Wendling8f636172010-08-08 01:49:35 +0000273 if (Inst.isCompare) OS << "|(1<<TID::Compare)";
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000274 if (Inst.isBarrier) OS << "|(1<<TID::Barrier)";
275 if (Inst.hasDelaySlot) OS << "|(1<<TID::DelaySlot)";
276 if (Inst.isCall) OS << "|(1<<TID::Call)";
Dan Gohman5574cc72008-12-03 18:15:48 +0000277 if (Inst.canFoldAsLoad) OS << "|(1<<TID::FoldableAsLoad)";
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000278 if (Inst.mayLoad) OS << "|(1<<TID::MayLoad)";
279 if (Inst.mayStore) OS << "|(1<<TID::MayStore)";
280 if (Inst.isPredicable) OS << "|(1<<TID::Predicable)";
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000281 if (Inst.isConvertibleToThreeAddress) OS << "|(1<<TID::ConvertibleTo3Addr)";
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000282 if (Inst.isCommutable) OS << "|(1<<TID::Commutable)";
283 if (Inst.isTerminator) OS << "|(1<<TID::Terminator)";
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000284 if (Inst.isReMaterializable) OS << "|(1<<TID::Rematerializable)";
285 if (Inst.isNotDuplicable) OS << "|(1<<TID::NotDuplicable)";
286 if (Inst.hasOptionalDef) OS << "|(1<<TID::HasOptionalDef)";
Dan Gohman30afe012009-10-29 18:10:34 +0000287 if (Inst.usesCustomInserter) OS << "|(1<<TID::UsesCustomInserter)";
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000288 if (Inst.isVariadic) OS << "|(1<<TID::Variadic)";
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000289 if (Inst.hasSideEffects) OS << "|(1<<TID::UnmodeledSideEffects)";
290 if (Inst.isAsCheapAsAMove) OS << "|(1<<TID::CheapAsAMove)";
Evan Chengf6ea3032009-10-01 08:21:18 +0000291 if (Inst.hasExtraSrcRegAllocReq) OS << "|(1<<TID::ExtraSrcRegAllocReq)";
292 if (Inst.hasExtraDefRegAllocReq) OS << "|(1<<TID::ExtraDefRegAllocReq)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293
294 // Emit all of the target-specific flags...
Jakob Stoklund Olesen4f8ea292010-04-05 03:10:20 +0000295 BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags");
296 if (!TSF) throw "no TSFlags?";
297 uint64_t Value = 0;
298 for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) {
299 if (BitInit *Bit = dynamic_cast<BitInit*>(TSF->getBit(i)))
300 Value |= uint64_t(Bit->getValue()) << i;
301 else
302 throw "Invalid TSFlags bit in " + Inst.TheDef->getName();
303 }
304 OS << ", 0x";
305 OS.write_hex(Value);
Eric Christopher6a354322010-06-09 16:16:48 +0000306 OS << "ULL, ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307
308 // Emit the implicit uses and defs lists...
309 std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
310 if (UseList.empty())
311 OS << "NULL, ";
312 else
313 OS << "ImplicitList" << EmittedLists[UseList] << ", ";
314
315 std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
316 if (DefList.empty())
317 OS << "NULL, ";
318 else
319 OS << "ImplicitList" << EmittedLists[DefList] << ", ";
320
Evan Cheng25144652008-10-17 21:00:09 +0000321 std::map<Record*, unsigned>::iterator BI = BarriersMap.find(Inst.TheDef);
322 if (BI == BarriersMap.end())
323 OS << "NULL, ";
324 else
325 OS << "Barriers" << BI->second << ", ";
326
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 // Emit the operand info.
328 std::vector<std::string> OperandInfo = GetOperandInfo(Inst);
329 if (OperandInfo.empty())
330 OS << "0";
331 else
Chris Lattner0d58d022008-01-06 01:20:13 +0000332 OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
Jakob Stoklund Olesen4f8ea292010-04-05 03:10:20 +0000333
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
335}