blob: 6201690b10ef75c62daaa5f1fd15d3dcd2aa110e [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"
18#include <algorithm>
Chris Lattner04a3a4c2007-12-30 00:25:23 +000019#include <iostream>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020using namespace llvm;
21
Chris Lattner4533a722008-01-06 01:21:51 +000022static void PrintDefList(const std::vector<Record*> &Uses,
23 unsigned Num, std::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,
31 unsigned Num, std::ostream &OS) {
32 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
42struct RecordNameComparator {
43 bool operator()(const Record *Rec1, const Record *Rec2) const {
44 return Rec1->getName() < Rec2->getName();
45 }
46};
47
48void InstrInfoEmitter::GatherItinClasses() {
49 std::vector<Record*> DefList =
50 Records.getAllDerivedDefinitions("InstrItinClass");
51 std::sort(DefList.begin(), DefList.end(), RecordNameComparator());
52
53 for (unsigned i = 0, N = DefList.size(); i < N; i++)
54 ItinClassMap[DefList[i]->getName()] = i;
55}
56
57unsigned InstrInfoEmitter::getItinClassNumber(const Record *InstRec) {
58 return ItinClassMap[InstRec->getValueAsDef("Itinerary")->getName()];
59}
60
61//===----------------------------------------------------------------------===//
62// Operand Info Emission.
63//===----------------------------------------------------------------------===//
64
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065std::vector<std::string>
66InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) {
67 std::vector<std::string> Result;
68
69 for (unsigned i = 0, e = Inst.OperandList.size(); i != e; ++i) {
70 // Handle aggregate operands and normal operands the same way by expanding
71 // either case into a list of operands for this op.
72 std::vector<CodeGenInstruction::OperandInfo> OperandList;
73
74 // This might be a multiple operand thing. Targets like X86 have
75 // registers in their multi-operand operands. It may also be an anonymous
76 // operand, which has a single operand, but no declared class for the
77 // operand.
78 DagInit *MIOI = Inst.OperandList[i].MIOperandInfo;
79
80 if (!MIOI || MIOI->getNumArgs() == 0) {
81 // Single, anonymous, operand.
82 OperandList.push_back(Inst.OperandList[i]);
83 } else {
84 for (unsigned j = 0, e = Inst.OperandList[i].MINumOperands; j != e; ++j) {
85 OperandList.push_back(Inst.OperandList[i]);
86
87 Record *OpR = dynamic_cast<DefInit*>(MIOI->getArg(j))->getDef();
88 OperandList.back().Rec = OpR;
89 }
90 }
91
92 for (unsigned j = 0, e = OperandList.size(); j != e; ++j) {
93 Record *OpR = OperandList[j].Rec;
94 std::string Res;
95
96 if (OpR->isSubClassOf("RegisterClass"))
97 Res += getQualifiedName(OpR) + "RegClassID, ";
98 else
99 Res += "0, ";
100 // Fill in applicable flags.
101 Res += "0";
102
103 // Ptr value whose register class is resolved via callback.
104 if (OpR->getName() == "ptr_rc")
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000105 Res += "|(1<<TOI::LookupPtrRegClass)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107 // Predicate operands. Check to see if the original unexpanded operand
108 // was of type PredicateOperand.
109 if (Inst.OperandList[i].Rec->isSubClassOf("PredicateOperand"))
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000110 Res += "|(1<<TOI::Predicate)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111
112 // Optional def operands. Check to see if the original unexpanded operand
113 // was of type OptionalDefOperand.
114 if (Inst.OperandList[i].Rec->isSubClassOf("OptionalDefOperand"))
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000115 Res += "|(1<<TOI::OptionalDef)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116
117 // Fill in constraint info.
118 Res += ", " + Inst.OperandList[i].Constraints[j];
119 Result.push_back(Res);
120 }
121 }
122
123 return Result;
124}
125
Chris Lattner0d58d022008-01-06 01:20:13 +0000126void InstrInfoEmitter::EmitOperandInfo(std::ostream &OS,
127 OperandInfoMapTy &OperandInfoIDs) {
128 // ID #0 is for no operand info.
129 unsigned OperandListNum = 0;
130 OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum;
131
132 OS << "\n";
133 const CodeGenTarget &Target = CDP.getTargetInfo();
134 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
135 E = Target.inst_end(); II != E; ++II) {
136 std::vector<std::string> OperandInfo = GetOperandInfo(II->second);
137 unsigned &N = OperandInfoIDs[OperandInfo];
138 if (N != 0) continue;
139
140 N = ++OperandListNum;
141 OS << "static const TargetOperandInfo OperandInfo" << N << "[] = { ";
142 for (unsigned i = 0, e = OperandInfo.size(); i != e; ++i)
143 OS << "{ " << OperandInfo[i] << " }, ";
144 OS << "};\n";
145 }
146}
147
Evan Cheng25144652008-10-17 21:00:09 +0000148void InstrInfoEmitter::DetectRegisterClassBarriers(std::vector<Record*> &Defs,
149 const std::vector<CodeGenRegisterClass> &RCs,
150 std::vector<Record*> &Barriers) {
151 std::set<Record*> DefSet;
152 unsigned NumDefs = Defs.size();
153 for (unsigned i = 0; i < NumDefs; ++i)
154 DefSet.insert(Defs[i]);
155
156 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
157 const CodeGenRegisterClass &RC = RCs[i];
158 unsigned NumRegs = RC.Elements.size();
159 if (NumRegs > NumDefs)
160 continue; // Can't possibly clobber this RC.
161
162 bool Clobber = true;
163 for (unsigned j = 0; j < NumRegs; ++j) {
164 Record *Reg = RC.Elements[j];
165 if (!DefSet.count(Reg)) {
166 Clobber = false;
167 break;
168 }
169 }
170 if (Clobber)
171 Barriers.push_back(RC.TheDef);
172 }
173}
174
Chris Lattner0d58d022008-01-06 01:20:13 +0000175//===----------------------------------------------------------------------===//
176// Main Output.
177//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178
179// run - Emit the main instruction description records for the target...
180void InstrInfoEmitter::run(std::ostream &OS) {
181 GatherItinClasses();
182
183 EmitSourceFileHeader("Target Instruction Descriptors", OS);
184 OS << "namespace llvm {\n\n";
185
Dan Gohman907df572008-04-03 00:02:49 +0000186 CodeGenTarget &Target = CDP.getTargetInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 const std::string &TargetName = Target.getName();
188 Record *InstrInfo = Target.getInstructionSet();
Evan Cheng25144652008-10-17 21:00:09 +0000189 const std::vector<CodeGenRegisterClass> &RCs = Target.getRegisterClasses();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190
191 // Keep track of all of the def lists we have emitted already.
192 std::map<std::vector<Record*>, unsigned> EmittedLists;
193 unsigned ListNumber = 0;
Evan Cheng25144652008-10-17 21:00:09 +0000194 std::map<std::vector<Record*>, unsigned> EmittedBarriers;
195 unsigned BarrierNumber = 0;
196 std::map<Record*, unsigned> BarriersMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
198 // Emit all of the instruction's implicit uses and defs.
199 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
200 E = Target.inst_end(); II != E; ++II) {
201 Record *Inst = II->second.TheDef;
202 std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
203 if (!Uses.empty()) {
204 unsigned &IL = EmittedLists[Uses];
Chris Lattner4533a722008-01-06 01:21:51 +0000205 if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 }
207 std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
208 if (!Defs.empty()) {
Evan Cheng25144652008-10-17 21:00:09 +0000209 std::vector<Record*> RCBarriers;
210 DetectRegisterClassBarriers(Defs, RCs, RCBarriers);
211 if (!RCBarriers.empty()) {
212 unsigned &IB = EmittedBarriers[RCBarriers];
213 if (!IB) PrintBarriers(RCBarriers, IB = ++BarrierNumber, OS);
214 BarriersMap.insert(std::make_pair(Inst, IB));
215 }
216
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 unsigned &IL = EmittedLists[Defs];
Chris Lattner4533a722008-01-06 01:21:51 +0000218 if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 }
220 }
221
Chris Lattner0d58d022008-01-06 01:20:13 +0000222 OperandInfoMapTy OperandInfoIDs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223
224 // Emit all of the operand info records.
Chris Lattner0d58d022008-01-06 01:20:13 +0000225 EmitOperandInfo(OS, OperandInfoIDs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226
Chris Lattner5b930372008-01-07 07:27:27 +0000227 // Emit all of the TargetInstrDesc records in their ENUM ordering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 //
Chris Lattner5b930372008-01-07 07:27:27 +0000229 OS << "\nstatic const TargetInstrDesc " << TargetName
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 << "Insts[] = {\n";
231 std::vector<const CodeGenInstruction*> NumberedInstructions;
232 Target.getInstructionsByEnumValue(NumberedInstructions);
233
234 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i)
235 emitRecord(*NumberedInstructions[i], i, InstrInfo, EmittedLists,
Evan Cheng25144652008-10-17 21:00:09 +0000236 BarriersMap, OperandInfoIDs, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 OS << "};\n";
238 OS << "} // End llvm namespace \n";
239}
240
241void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
242 Record *InstrInfo,
243 std::map<std::vector<Record*>, unsigned> &EmittedLists,
Evan Cheng25144652008-10-17 21:00:09 +0000244 std::map<Record*, unsigned> &BarriersMap,
Chris Lattner0d58d022008-01-06 01:20:13 +0000245 const OperandInfoMapTy &OpInfo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 std::ostream &OS) {
Chris Lattner44435a72008-01-06 01:53:37 +0000247 int MinOperands = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 if (!Inst.OperandList.empty())
249 // Each logical operand can be multiple MI operands.
250 MinOperands = Inst.OperandList.back().MIOperandNo +
251 Inst.OperandList.back().MINumOperands;
Dan Gohman3329ffe2008-05-29 19:57:41 +0000252
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 OS << " { ";
Evan Cheng84c52f62007-08-02 00:20:17 +0000254 OS << Num << ",\t" << MinOperands << ",\t"
Chris Lattnerbf010e32008-01-07 05:06:49 +0000255 << Inst.NumDefs << ",\t" << getItinClassNumber(Inst.TheDef)
256 << ",\t\"" << Inst.TheDef->getName() << "\", 0";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 // Emit all of the target indepedent flags...
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000259 if (Inst.isReturn) OS << "|(1<<TID::Return)";
260 if (Inst.isBranch) OS << "|(1<<TID::Branch)";
261 if (Inst.isIndirectBranch) OS << "|(1<<TID::IndirectBranch)";
262 if (Inst.isBarrier) OS << "|(1<<TID::Barrier)";
263 if (Inst.hasDelaySlot) OS << "|(1<<TID::DelaySlot)";
264 if (Inst.isCall) OS << "|(1<<TID::Call)";
Dan Gohman5574cc72008-12-03 18:15:48 +0000265 if (Inst.canFoldAsLoad) OS << "|(1<<TID::FoldableAsLoad)";
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000266 if (Inst.mayLoad) OS << "|(1<<TID::MayLoad)";
267 if (Inst.mayStore) OS << "|(1<<TID::MayStore)";
268 if (Inst.isPredicable) OS << "|(1<<TID::Predicable)";
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000269 if (Inst.isConvertibleToThreeAddress) OS << "|(1<<TID::ConvertibleTo3Addr)";
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000270 if (Inst.isCommutable) OS << "|(1<<TID::Commutable)";
271 if (Inst.isTerminator) OS << "|(1<<TID::Terminator)";
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000272 if (Inst.isReMaterializable) OS << "|(1<<TID::Rematerializable)";
273 if (Inst.isNotDuplicable) OS << "|(1<<TID::NotDuplicable)";
274 if (Inst.hasOptionalDef) OS << "|(1<<TID::HasOptionalDef)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 if (Inst.usesCustomDAGSchedInserter)
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000276 OS << "|(1<<TID::UsesCustomDAGSchedInserter)";
277 if (Inst.isVariadic) OS << "|(1<<TID::Variadic)";
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000278 if (Inst.hasSideEffects) OS << "|(1<<TID::UnmodeledSideEffects)";
279 if (Inst.isAsCheapAsAMove) OS << "|(1<<TID::CheapAsAMove)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 OS << ", 0";
281
282 // Emit all of the target-specific flags...
283 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
284 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
285 if (LI->getSize() != Shift->getSize())
286 throw "Lengths of " + InstrInfo->getName() +
287 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
288
289 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
290 emitShiftedValue(Inst.TheDef, dynamic_cast<StringInit*>(LI->getElement(i)),
291 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
292
293 OS << ", ";
294
295 // Emit the implicit uses and defs lists...
296 std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
297 if (UseList.empty())
298 OS << "NULL, ";
299 else
300 OS << "ImplicitList" << EmittedLists[UseList] << ", ";
301
302 std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
303 if (DefList.empty())
304 OS << "NULL, ";
305 else
306 OS << "ImplicitList" << EmittedLists[DefList] << ", ";
307
Evan Cheng25144652008-10-17 21:00:09 +0000308 std::map<Record*, unsigned>::iterator BI = BarriersMap.find(Inst.TheDef);
309 if (BI == BarriersMap.end())
310 OS << "NULL, ";
311 else
312 OS << "Barriers" << BI->second << ", ";
313
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 // Emit the operand info.
315 std::vector<std::string> OperandInfo = GetOperandInfo(Inst);
316 if (OperandInfo.empty())
317 OS << "0";
318 else
Chris Lattner0d58d022008-01-06 01:20:13 +0000319 OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320
321 OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
322}
323
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
326 IntInit *ShiftInt, std::ostream &OS) {
327 if (Val == 0 || ShiftInt == 0)
328 throw std::string("Illegal value or shift amount in TargetInfo*!");
329 RecordVal *RV = R->getValue(Val->getValue());
330 int Shift = ShiftInt->getValue();
331
332 if (RV == 0 || RV->getValue() == 0) {
333 // This isn't an error if this is a builtin instruction.
334 if (R->getName() != "PHI" &&
335 R->getName() != "INLINEASM" &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000336 R->getName() != "DBG_LABEL" &&
337 R->getName() != "EH_LABEL" &&
338 R->getName() != "GC_LABEL" &&
Evan Cheng2e28d622008-02-02 04:07:54 +0000339 R->getName() != "DECLARE" &&
Christopher Lamb071a2a72007-07-26 07:48:21 +0000340 R->getName() != "EXTRACT_SUBREG" &&
Evan Cheng3c0eda52008-03-15 00:03:38 +0000341 R->getName() != "INSERT_SUBREG" &&
Christopher Lamb76d72da2008-03-16 03:12:01 +0000342 R->getName() != "IMPLICIT_DEF" &&
343 R->getName() != "SUBREG_TO_REG")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 throw R->getName() + " doesn't have a field named '" +
345 Val->getValue() + "'!";
346 return;
347 }
348
349 Init *Value = RV->getValue();
350 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
351 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
352 return;
353 } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
354 // Convert the Bits to an integer to print...
355 Init *I = BI->convertInitializerTo(new IntRecTy());
356 if (I)
357 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
358 if (II->getValue()) {
359 if (Shift)
360 OS << "|(" << II->getValue() << "<<" << Shift << ")";
361 else
362 OS << "|" << II->getValue();
363 }
364 return;
365 }
366
367 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
368 if (II->getValue()) {
369 if (Shift)
370 OS << "|(" << II->getValue() << "<<" << Shift << ")";
371 else
372 OS << II->getValue();
373 }
374 return;
375 }
376
Chris Lattner04a3a4c2007-12-30 00:25:23 +0000377 std::cerr << "Unhandled initializer: " << *Val << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 throw "In record '" + R->getName() + "' for TSFlag emission.";
379}
380