fa2b19264d9f98b7ff4ab45301d19b3f1c539315
[oota-llvm.git] / utils / TableGen / InstrInfoEmitter.cpp
1 //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
17 #include "Record.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include <algorithm>
20 using namespace llvm;
21
22 static void PrintDefList(const std::vector<Record*> &Uses,
23                          unsigned Num, raw_ostream &OS) {
24   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
30 //===----------------------------------------------------------------------===//
31 // Instruction Itinerary Information.
32 //===----------------------------------------------------------------------===//
33
34 void InstrInfoEmitter::GatherItinClasses() {
35   std::vector<Record*> DefList =
36   Records.getAllDerivedDefinitions("InstrItinClass");
37   std::sort(DefList.begin(), DefList.end(), LessRecord());
38
39   for (unsigned i = 0, N = DefList.size(); i < N; i++)
40     ItinClassMap[DefList[i]->getName()] = i;
41 }
42
43 unsigned InstrInfoEmitter::getItinClassNumber(const Record *InstRec) {
44   return ItinClassMap[InstRec->getValueAsDef("Itinerary")->getName()];
45 }
46
47 //===----------------------------------------------------------------------===//
48 // Operand Info Emission.
49 //===----------------------------------------------------------------------===//
50
51 std::vector<std::string>
52 InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) {
53   std::vector<std::string> Result;
54
55   for (unsigned i = 0, e = Inst.Operands.size(); i != e; ++i) {
56     // Handle aggregate operands and normal operands the same way by expanding
57     // either case into a list of operands for this op.
58     std::vector<CGIOperandList::OperandInfo> OperandList;
59
60     // This might be a multiple operand thing.  Targets like X86 have
61     // registers in their multi-operand operands.  It may also be an anonymous
62     // operand, which has a single operand, but no declared class for the
63     // operand.
64     DagInit *MIOI = Inst.Operands[i].MIOperandInfo;
65
66     if (!MIOI || MIOI->getNumArgs() == 0) {
67       // Single, anonymous, operand.
68       OperandList.push_back(Inst.Operands[i]);
69     } else {
70       for (unsigned j = 0, e = Inst.Operands[i].MINumOperands; j != e; ++j) {
71         OperandList.push_back(Inst.Operands[i]);
72
73         Record *OpR = dynamic_cast<DefInit*>(MIOI->getArg(j))->getDef();
74         OperandList.back().Rec = OpR;
75       }
76     }
77
78     for (unsigned j = 0, e = OperandList.size(); j != e; ++j) {
79       Record *OpR = OperandList[j].Rec;
80       std::string Res;
81
82       if (OpR->isSubClassOf("RegisterOperand"))
83         OpR = OpR->getValueAsDef("RegClass");
84       if (OpR->isSubClassOf("RegisterClass"))
85         Res += getQualifiedName(OpR) + "RegClassID, ";
86       else if (OpR->isSubClassOf("PointerLikeRegClass"))
87         Res += utostr(OpR->getValueAsInt("RegClassKind")) + ", ";
88       else
89         // -1 means the operand does not have a fixed register class.
90         Res += "-1, ";
91
92       // Fill in applicable flags.
93       Res += "0";
94
95       // Ptr value whose register class is resolved via callback.
96       if (OpR->isSubClassOf("PointerLikeRegClass"))
97         Res += "|(1<<MCOI::LookupPtrRegClass)";
98
99       // Predicate operands.  Check to see if the original unexpanded operand
100       // was of type PredicateOperand.
101       if (Inst.Operands[i].Rec->isSubClassOf("PredicateOperand"))
102         Res += "|(1<<MCOI::Predicate)";
103
104       // Optional def operands.  Check to see if the original unexpanded operand
105       // was of type OptionalDefOperand.
106       if (Inst.Operands[i].Rec->isSubClassOf("OptionalDefOperand"))
107         Res += "|(1<<MCOI::OptionalDef)";
108
109       // Fill in constraint info.
110       Res += ", ";
111
112       const CGIOperandList::ConstraintInfo &Constraint =
113         Inst.Operands[i].Constraints[j];
114       if (Constraint.isNone())
115         Res += "0";
116       else if (Constraint.isEarlyClobber())
117         Res += "(1 << MCOI::EARLY_CLOBBER)";
118       else {
119         assert(Constraint.isTied());
120         Res += "((" + utostr(Constraint.getTiedOperand()) +
121                     " << 16) | (1 << MCOI::TIED_TO))";
122       }
123
124       Result.push_back(Res);
125     }
126   }
127
128   return Result;
129 }
130
131 void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS,
132                                        OperandInfoMapTy &OperandInfoIDs) {
133   // ID #0 is for no operand info.
134   unsigned OperandListNum = 0;
135   OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum;
136
137   OS << "\n";
138   const CodeGenTarget &Target = CDP.getTargetInfo();
139   for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
140        E = Target.inst_end(); II != E; ++II) {
141     std::vector<std::string> OperandInfo = GetOperandInfo(**II);
142     unsigned &N = OperandInfoIDs[OperandInfo];
143     if (N != 0) continue;
144
145     N = ++OperandListNum;
146     OS << "static const MCOperandInfo OperandInfo" << N << "[] = { ";
147     for (unsigned i = 0, e = OperandInfo.size(); i != e; ++i)
148       OS << "{ " << OperandInfo[i] << " }, ";
149     OS << "};\n";
150   }
151 }
152
153 //===----------------------------------------------------------------------===//
154 // Main Output.
155 //===----------------------------------------------------------------------===//
156
157 // run - Emit the main instruction description records for the target...
158 void InstrInfoEmitter::run(raw_ostream &OS) {
159   GatherItinClasses();
160
161   EmitSourceFileHeader("Target Instruction Descriptors", OS);
162   OS << "namespace llvm {\n\n";
163
164   CodeGenTarget &Target = CDP.getTargetInfo();
165   const std::string &TargetName = Target.getName();
166   Record *InstrInfo = Target.getInstructionSet();
167
168   // Keep track of all of the def lists we have emitted already.
169   std::map<std::vector<Record*>, unsigned> EmittedLists;
170   unsigned ListNumber = 0;
171
172   // Emit all of the instruction's implicit uses and defs.
173   for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
174          E = Target.inst_end(); II != E; ++II) {
175     Record *Inst = (*II)->TheDef;
176     std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
177     if (!Uses.empty()) {
178       unsigned &IL = EmittedLists[Uses];
179       if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS);
180     }
181     std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
182     if (!Defs.empty()) {
183       unsigned &IL = EmittedLists[Defs];
184       if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS);
185     }
186   }
187
188   OperandInfoMapTy OperandInfoIDs;
189
190   // Emit all of the operand info records.
191   EmitOperandInfo(OS, OperandInfoIDs);
192
193   // Emit all of the MCInstrDesc records in their ENUM ordering.
194   //
195   OS << "\nstatic const MCInstrDesc " << TargetName
196      << "Insts[] = {\n";
197   const std::vector<const CodeGenInstruction*> &NumberedInstructions =
198     Target.getInstructionsByEnumValue();
199
200   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i)
201     emitRecord(*NumberedInstructions[i], i, InstrInfo, EmittedLists,
202                OperandInfoIDs, OS);
203   OS << "};\n";
204   OS << "} // End llvm namespace \n";
205 }
206
207 void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
208                                   Record *InstrInfo,
209                          std::map<std::vector<Record*>, unsigned> &EmittedLists,
210                                   const OperandInfoMapTy &OpInfo,
211                                   raw_ostream &OS) {
212   int MinOperands = 0;
213   if (!Inst.Operands.size() == 0)
214     // Each logical operand can be multiple MI operands.
215     MinOperands = Inst.Operands.back().MIOperandNo +
216                   Inst.Operands.back().MINumOperands;
217
218   OS << "  { ";
219   OS << Num << ",\t" << MinOperands << ",\t"
220      << Inst.Operands.NumDefs << ",\t" << getItinClassNumber(Inst.TheDef)
221      << ",\t\"" << Inst.TheDef->getName() << "\", 0";
222
223   // Emit all of the target indepedent flags...
224   if (Inst.isReturn)           OS << "|(1<<MCID::Return)";
225   if (Inst.isBranch)           OS << "|(1<<MCID::Branch)";
226   if (Inst.isIndirectBranch)   OS << "|(1<<MCID::IndirectBranch)";
227   if (Inst.isCompare)          OS << "|(1<<MCID::Compare)";
228   if (Inst.isMoveImm)          OS << "|(1<<MCID::MoveImm)";
229   if (Inst.isBitcast)          OS << "|(1<<MCID::Bitcast)";
230   if (Inst.isBarrier)          OS << "|(1<<MCID::Barrier)";
231   if (Inst.hasDelaySlot)       OS << "|(1<<MCID::DelaySlot)";
232   if (Inst.isCall)             OS << "|(1<<MCID::Call)";
233   if (Inst.canFoldAsLoad)      OS << "|(1<<MCID::FoldableAsLoad)";
234   if (Inst.mayLoad)            OS << "|(1<<MCID::MayLoad)";
235   if (Inst.mayStore)           OS << "|(1<<MCID::MayStore)";
236   if (Inst.isPredicable)       OS << "|(1<<MCID::Predicable)";
237   if (Inst.isConvertibleToThreeAddress) OS << "|(1<<MCID::ConvertibleTo3Addr)";
238   if (Inst.isCommutable)       OS << "|(1<<MCID::Commutable)";
239   if (Inst.isTerminator)       OS << "|(1<<MCID::Terminator)";
240   if (Inst.isReMaterializable) OS << "|(1<<MCID::Rematerializable)";
241   if (Inst.isNotDuplicable)    OS << "|(1<<MCID::NotDuplicable)";
242   if (Inst.Operands.hasOptionalDef) OS << "|(1<<MCID::HasOptionalDef)";
243   if (Inst.usesCustomInserter) OS << "|(1<<MCID::UsesCustomInserter)";
244   if (Inst.Operands.isVariadic)OS << "|(1<<MCID::Variadic)";
245   if (Inst.hasSideEffects)     OS << "|(1<<MCID::UnmodeledSideEffects)";
246   if (Inst.isAsCheapAsAMove)   OS << "|(1<<MCID::CheapAsAMove)";
247   if (Inst.hasExtraSrcRegAllocReq) OS << "|(1<<MCID::ExtraSrcRegAllocReq)";
248   if (Inst.hasExtraDefRegAllocReq) OS << "|(1<<MCID::ExtraDefRegAllocReq)";
249
250   // Emit all of the target-specific flags...
251   BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags");
252   if (!TSF) throw "no TSFlags?";
253   uint64_t Value = 0;
254   for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) {
255     if (BitInit *Bit = dynamic_cast<BitInit*>(TSF->getBit(i)))
256       Value |= uint64_t(Bit->getValue()) << i;
257     else
258       throw "Invalid TSFlags bit in " + Inst.TheDef->getName();
259   }
260   OS << ", 0x";
261   OS.write_hex(Value);
262   OS << "ULL, ";
263
264   // Emit the implicit uses and defs lists...
265   std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
266   if (UseList.empty())
267     OS << "NULL, ";
268   else
269     OS << "ImplicitList" << EmittedLists[UseList] << ", ";
270
271   std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
272   if (DefList.empty())
273     OS << "NULL, ";
274   else
275     OS << "ImplicitList" << EmittedLists[DefList] << ", ";
276
277   // Emit the operand info.
278   std::vector<std::string> OperandInfo = GetOperandInfo(Inst);
279   if (OperandInfo.empty())
280     OS << "0";
281   else
282     OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
283
284   OS << " },  // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
285 }