blob: 4502da176f4fca7f9eafc316bcba334e32a19d79 [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>
19using namespace llvm;
20
Chris Lattner4533a722008-01-06 01:21:51 +000021static void PrintDefList(const std::vector<Record*> &Uses,
Daniel Dunbard4287062009-07-03 00:10:29 +000022 unsigned Num, raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023 OS << "static const unsigned ImplicitList" << Num << "[] = { ";
24 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
25 OS << getQualifiedName(Uses[i]) << ", ";
26 OS << "0 };\n";
27}
28
Evan Cheng25144652008-10-17 21:00:09 +000029static void PrintBarriers(std::vector<Record*> &Barriers,
Daniel Dunbard4287062009-07-03 00:10:29 +000030 unsigned Num, raw_ostream &OS) {
Evan Cheng25144652008-10-17 21:00:09 +000031 OS << "static const TargetRegisterClass* Barriers" << Num << "[] = { ";
32 for (unsigned i = 0, e = Barriers.size(); i != e; ++i)
33 OS << "&" << getQualifiedName(Barriers[i]) << "RegClass, ";
34 OS << "NULL };\n";
35}
36
Chris Lattner0d58d022008-01-06 01:20:13 +000037//===----------------------------------------------------------------------===//
38// Instruction Itinerary Information.
39//===----------------------------------------------------------------------===//
40
41struct RecordNameComparator {
42 bool operator()(const Record *Rec1, const Record *Rec2) const {
43 return Rec1->getName() < Rec2->getName();
44 }
45};
46
47void InstrInfoEmitter::GatherItinClasses() {
48 std::vector<Record*> DefList =
49 Records.getAllDerivedDefinitions("InstrItinClass");
50 std::sort(DefList.begin(), DefList.end(), RecordNameComparator());
51
52 for (unsigned i = 0, N = DefList.size(); i < N; i++)
53 ItinClassMap[DefList[i]->getName()] = i;
54}
55
56unsigned InstrInfoEmitter::getItinClassNumber(const Record *InstRec) {
57 return ItinClassMap[InstRec->getValueAsDef("Itinerary")->getName()];
58}
59
60//===----------------------------------------------------------------------===//
61// Operand Info Emission.
62//===----------------------------------------------------------------------===//
63
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064std::vector<std::string>
65InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) {
66 std::vector<std::string> Result;
67
68 for (unsigned i = 0, e = Inst.OperandList.size(); i != e; ++i) {
69 // Handle aggregate operands and normal operands the same way by expanding
70 // either case into a list of operands for this op.
71 std::vector<CodeGenInstruction::OperandInfo> OperandList;
72
73 // This might be a multiple operand thing. Targets like X86 have
74 // registers in their multi-operand operands. It may also be an anonymous
75 // operand, which has a single operand, but no declared class for the
76 // operand.
77 DagInit *MIOI = Inst.OperandList[i].MIOperandInfo;
78
79 if (!MIOI || MIOI->getNumArgs() == 0) {
80 // Single, anonymous, operand.
81 OperandList.push_back(Inst.OperandList[i]);
82 } else {
83 for (unsigned j = 0, e = Inst.OperandList[i].MINumOperands; j != e; ++j) {
84 OperandList.push_back(Inst.OperandList[i]);
85
86 Record *OpR = dynamic_cast<DefInit*>(MIOI->getArg(j))->getDef();
87 OperandList.back().Rec = OpR;
88 }
89 }
90
91 for (unsigned j = 0, e = OperandList.size(); j != e; ++j) {
92 Record *OpR = OperandList[j].Rec;
93 std::string Res;
94
95 if (OpR->isSubClassOf("RegisterClass"))
96 Res += getQualifiedName(OpR) + "RegClassID, ";
97 else
98 Res += "0, ";
99 // Fill in applicable flags.
100 Res += "0";
101
102 // Ptr value whose register class is resolved via callback.
103 if (OpR->getName() == "ptr_rc")
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000104 Res += "|(1<<TOI::LookupPtrRegClass)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
106 // Predicate operands. Check to see if the original unexpanded operand
107 // was of type PredicateOperand.
108 if (Inst.OperandList[i].Rec->isSubClassOf("PredicateOperand"))
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000109 Res += "|(1<<TOI::Predicate)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110
111 // Optional def operands. Check to see if the original unexpanded operand
112 // was of type OptionalDefOperand.
113 if (Inst.OperandList[i].Rec->isSubClassOf("OptionalDefOperand"))
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000114 Res += "|(1<<TOI::OptionalDef)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115
116 // Fill in constraint info.
117 Res += ", " + Inst.OperandList[i].Constraints[j];
118 Result.push_back(Res);
119 }
120 }
121
122 return Result;
123}
124
Daniel Dunbard4287062009-07-03 00:10:29 +0000125void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS,
Chris Lattner0d58d022008-01-06 01:20:13 +0000126 OperandInfoMapTy &OperandInfoIDs) {
127 // ID #0 is for no operand info.
128 unsigned OperandListNum = 0;
129 OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum;
130
131 OS << "\n";
132 const CodeGenTarget &Target = CDP.getTargetInfo();
133 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
134 E = Target.inst_end(); II != E; ++II) {
135 std::vector<std::string> OperandInfo = GetOperandInfo(II->second);
136 unsigned &N = OperandInfoIDs[OperandInfo];
137 if (N != 0) continue;
138
139 N = ++OperandListNum;
140 OS << "static const TargetOperandInfo OperandInfo" << N << "[] = { ";
141 for (unsigned i = 0, e = OperandInfo.size(); i != e; ++i)
142 OS << "{ " << OperandInfo[i] << " }, ";
143 OS << "};\n";
144 }
145}
146
Evan Cheng25144652008-10-17 21:00:09 +0000147void InstrInfoEmitter::DetectRegisterClassBarriers(std::vector<Record*> &Defs,
148 const std::vector<CodeGenRegisterClass> &RCs,
149 std::vector<Record*> &Barriers) {
150 std::set<Record*> DefSet;
151 unsigned NumDefs = Defs.size();
152 for (unsigned i = 0; i < NumDefs; ++i)
153 DefSet.insert(Defs[i]);
154
155 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
156 const CodeGenRegisterClass &RC = RCs[i];
157 unsigned NumRegs = RC.Elements.size();
158 if (NumRegs > NumDefs)
159 continue; // Can't possibly clobber this RC.
160
161 bool Clobber = true;
162 for (unsigned j = 0; j < NumRegs; ++j) {
163 Record *Reg = RC.Elements[j];
164 if (!DefSet.count(Reg)) {
165 Clobber = false;
166 break;
167 }
168 }
169 if (Clobber)
170 Barriers.push_back(RC.TheDef);
171 }
172}
173
Chris Lattner0d58d022008-01-06 01:20:13 +0000174//===----------------------------------------------------------------------===//
175// Main Output.
176//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
178// run - Emit the main instruction description records for the target...
Daniel Dunbard4287062009-07-03 00:10:29 +0000179void InstrInfoEmitter::run(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 GatherItinClasses();
181
182 EmitSourceFileHeader("Target Instruction Descriptors", OS);
183 OS << "namespace llvm {\n\n";
184
Dan Gohman907df572008-04-03 00:02:49 +0000185 CodeGenTarget &Target = CDP.getTargetInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 const std::string &TargetName = Target.getName();
187 Record *InstrInfo = Target.getInstructionSet();
Evan Cheng25144652008-10-17 21:00:09 +0000188 const std::vector<CodeGenRegisterClass> &RCs = Target.getRegisterClasses();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189
190 // Keep track of all of the def lists we have emitted already.
191 std::map<std::vector<Record*>, unsigned> EmittedLists;
192 unsigned ListNumber = 0;
Evan Cheng25144652008-10-17 21:00:09 +0000193 std::map<std::vector<Record*>, unsigned> EmittedBarriers;
194 unsigned BarrierNumber = 0;
195 std::map<Record*, unsigned> BarriersMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196
197 // Emit all of the instruction's implicit uses and defs.
198 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
199 E = Target.inst_end(); II != E; ++II) {
200 Record *Inst = II->second.TheDef;
201 std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
202 if (!Uses.empty()) {
203 unsigned &IL = EmittedLists[Uses];
Chris Lattner4533a722008-01-06 01:21:51 +0000204 if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 }
206 std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
207 if (!Defs.empty()) {
Evan Cheng25144652008-10-17 21:00:09 +0000208 std::vector<Record*> RCBarriers;
209 DetectRegisterClassBarriers(Defs, RCs, RCBarriers);
210 if (!RCBarriers.empty()) {
211 unsigned &IB = EmittedBarriers[RCBarriers];
212 if (!IB) PrintBarriers(RCBarriers, IB = ++BarrierNumber, OS);
213 BarriersMap.insert(std::make_pair(Inst, IB));
214 }
215
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 unsigned &IL = EmittedLists[Defs];
Chris Lattner4533a722008-01-06 01:21:51 +0000217 if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 }
219 }
220
Chris Lattner0d58d022008-01-06 01:20:13 +0000221 OperandInfoMapTy OperandInfoIDs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222
223 // Emit all of the operand info records.
Chris Lattner0d58d022008-01-06 01:20:13 +0000224 EmitOperandInfo(OS, OperandInfoIDs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225
Chris Lattner5b930372008-01-07 07:27:27 +0000226 // Emit all of the TargetInstrDesc records in their ENUM ordering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 //
Chris Lattner5b930372008-01-07 07:27:27 +0000228 OS << "\nstatic const TargetInstrDesc " << TargetName
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 << "Insts[] = {\n";
230 std::vector<const CodeGenInstruction*> NumberedInstructions;
231 Target.getInstructionsByEnumValue(NumberedInstructions);
232
233 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i)
234 emitRecord(*NumberedInstructions[i], i, InstrInfo, EmittedLists,
Evan Cheng25144652008-10-17 21:00:09 +0000235 BarriersMap, OperandInfoIDs, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 OS << "};\n";
237 OS << "} // End llvm namespace \n";
238}
239
240void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
241 Record *InstrInfo,
242 std::map<std::vector<Record*>, unsigned> &EmittedLists,
Evan Cheng25144652008-10-17 21:00:09 +0000243 std::map<Record*, unsigned> &BarriersMap,
Chris Lattner0d58d022008-01-06 01:20:13 +0000244 const OperandInfoMapTy &OpInfo,
Daniel Dunbard4287062009-07-03 00:10:29 +0000245 raw_ostream &OS) {
Chris Lattner44435a72008-01-06 01:53:37 +0000246 int MinOperands = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 if (!Inst.OperandList.empty())
248 // Each logical operand can be multiple MI operands.
249 MinOperands = Inst.OperandList.back().MIOperandNo +
250 Inst.OperandList.back().MINumOperands;
Dan Gohman3329ffe2008-05-29 19:57:41 +0000251
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 OS << " { ";
Evan Cheng84c52f62007-08-02 00:20:17 +0000253 OS << Num << ",\t" << MinOperands << ",\t"
Chris Lattnerbf010e32008-01-07 05:06:49 +0000254 << Inst.NumDefs << ",\t" << getItinClassNumber(Inst.TheDef)
255 << ",\t\"" << Inst.TheDef->getName() << "\", 0";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 // Emit all of the target indepedent flags...
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000258 if (Inst.isReturn) OS << "|(1<<TID::Return)";
259 if (Inst.isBranch) OS << "|(1<<TID::Branch)";
260 if (Inst.isIndirectBranch) OS << "|(1<<TID::IndirectBranch)";
261 if (Inst.isBarrier) OS << "|(1<<TID::Barrier)";
262 if (Inst.hasDelaySlot) OS << "|(1<<TID::DelaySlot)";
263 if (Inst.isCall) OS << "|(1<<TID::Call)";
Dan Gohman5574cc72008-12-03 18:15:48 +0000264 if (Inst.canFoldAsLoad) OS << "|(1<<TID::FoldableAsLoad)";
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000265 if (Inst.mayLoad) OS << "|(1<<TID::MayLoad)";
266 if (Inst.mayStore) OS << "|(1<<TID::MayStore)";
267 if (Inst.isPredicable) OS << "|(1<<TID::Predicable)";
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000268 if (Inst.isConvertibleToThreeAddress) OS << "|(1<<TID::ConvertibleTo3Addr)";
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000269 if (Inst.isCommutable) OS << "|(1<<TID::Commutable)";
270 if (Inst.isTerminator) OS << "|(1<<TID::Terminator)";
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000271 if (Inst.isReMaterializable) OS << "|(1<<TID::Rematerializable)";
272 if (Inst.isNotDuplicable) OS << "|(1<<TID::NotDuplicable)";
273 if (Inst.hasOptionalDef) OS << "|(1<<TID::HasOptionalDef)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 if (Inst.usesCustomDAGSchedInserter)
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000275 OS << "|(1<<TID::UsesCustomDAGSchedInserter)";
276 if (Inst.isVariadic) OS << "|(1<<TID::Variadic)";
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000277 if (Inst.hasSideEffects) OS << "|(1<<TID::UnmodeledSideEffects)";
278 if (Inst.isAsCheapAsAMove) OS << "|(1<<TID::CheapAsAMove)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 OS << ", 0";
280
281 // Emit all of the target-specific flags...
282 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
283 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
284 if (LI->getSize() != Shift->getSize())
285 throw "Lengths of " + InstrInfo->getName() +
286 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
287
288 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
289 emitShiftedValue(Inst.TheDef, dynamic_cast<StringInit*>(LI->getElement(i)),
290 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
291
292 OS << ", ";
293
294 // Emit the implicit uses and defs lists...
295 std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
296 if (UseList.empty())
297 OS << "NULL, ";
298 else
299 OS << "ImplicitList" << EmittedLists[UseList] << ", ";
300
301 std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
302 if (DefList.empty())
303 OS << "NULL, ";
304 else
305 OS << "ImplicitList" << EmittedLists[DefList] << ", ";
306
Evan Cheng25144652008-10-17 21:00:09 +0000307 std::map<Record*, unsigned>::iterator BI = BarriersMap.find(Inst.TheDef);
308 if (BI == BarriersMap.end())
309 OS << "NULL, ";
310 else
311 OS << "Barriers" << BI->second << ", ";
312
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 // Emit the operand info.
314 std::vector<std::string> OperandInfo = GetOperandInfo(Inst);
315 if (OperandInfo.empty())
316 OS << "0";
317 else
Chris Lattner0d58d022008-01-06 01:20:13 +0000318 OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319
320 OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
321}
322
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323
324void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
Daniel Dunbard4287062009-07-03 00:10:29 +0000325 IntInit *ShiftInt, raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 if (Val == 0 || ShiftInt == 0)
327 throw std::string("Illegal value or shift amount in TargetInfo*!");
328 RecordVal *RV = R->getValue(Val->getValue());
329 int Shift = ShiftInt->getValue();
330
331 if (RV == 0 || RV->getValue() == 0) {
332 // This isn't an error if this is a builtin instruction.
333 if (R->getName() != "PHI" &&
334 R->getName() != "INLINEASM" &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000335 R->getName() != "DBG_LABEL" &&
336 R->getName() != "EH_LABEL" &&
337 R->getName() != "GC_LABEL" &&
Evan Cheng2e28d622008-02-02 04:07:54 +0000338 R->getName() != "DECLARE" &&
Christopher Lamb071a2a72007-07-26 07:48:21 +0000339 R->getName() != "EXTRACT_SUBREG" &&
Evan Cheng3c0eda52008-03-15 00:03:38 +0000340 R->getName() != "INSERT_SUBREG" &&
Christopher Lamb76d72da2008-03-16 03:12:01 +0000341 R->getName() != "IMPLICIT_DEF" &&
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000342 R->getName() != "SUBREG_TO_REG" &&
Dan Gohman4c10fc72009-04-13 21:06:25 +0000343 R->getName() != "COPY_TO_REGCLASS")
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
Daniel Dunbard4287062009-07-03 00:10:29 +0000377 errs() << "Unhandled initializer: " << *Val << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 throw "In record '" + R->getName() + "' for TSFlag emission.";
379}
380