1 //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is responsible for emitting a description of the target
11 // instruction set for the code generator.
13 //===----------------------------------------------------------------------===//
15 #include "InstrInfoEmitter.h"
16 #include "CodeGenTarget.h"
18 #include "llvm/ADT/StringExtras.h"
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
]) << ", ";
30 static void PrintBarriers(std::vector
<Record
*> &Barriers
,
31 unsigned Num
, raw_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, ";
38 //===----------------------------------------------------------------------===//
39 // Instruction Itinerary Information.
40 //===----------------------------------------------------------------------===//
42 void InstrInfoEmitter::GatherItinClasses() {
43 std::vector
<Record
*> DefList
=
44 Records
.getAllDerivedDefinitions("InstrItinClass");
45 std::sort(DefList
.begin(), DefList
.end(), LessRecord());
47 for (unsigned i
= 0, N
= DefList
.size(); i
< N
; i
++)
48 ItinClassMap
[DefList
[i
]->getName()] = i
;
51 unsigned InstrInfoEmitter::getItinClassNumber(const Record
*InstRec
) {
52 return ItinClassMap
[InstRec
->getValueAsDef("Itinerary")->getName()];
55 //===----------------------------------------------------------------------===//
56 // Operand Info Emission.
57 //===----------------------------------------------------------------------===//
59 std::vector
<std::string
>
60 InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction
&Inst
) {
61 std::vector
<std::string
> Result
;
63 for (unsigned i
= 0, e
= Inst
.Operands
.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
<CGIOperandList::OperandInfo
> OperandList
;
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
72 DagInit
*MIOI
= Inst
.Operands
[i
].MIOperandInfo
;
74 if (!MIOI
|| MIOI
->getNumArgs() == 0) {
75 // Single, anonymous, operand.
76 OperandList
.push_back(Inst
.Operands
[i
]);
78 for (unsigned j
= 0, e
= Inst
.Operands
[i
].MINumOperands
; j
!= e
; ++j
) {
79 OperandList
.push_back(Inst
.Operands
[i
]);
81 Record
*OpR
= dynamic_cast<DefInit
*>(MIOI
->getArg(j
))->getDef();
82 OperandList
.back().Rec
= OpR
;
86 for (unsigned j
= 0, e
= OperandList
.size(); j
!= e
; ++j
) {
87 Record
*OpR
= OperandList
[j
].Rec
;
90 if (OpR
->isSubClassOf("RegisterClass"))
91 Res
+= getQualifiedName(OpR
) + "RegClassID, ";
92 else if (OpR
->isSubClassOf("PointerLikeRegClass"))
93 Res
+= utostr(OpR
->getValueAsInt("RegClassKind")) + ", ";
95 // -1 means the operand does not have a fixed register class.
98 // Fill in applicable flags.
101 // Ptr value whose register class is resolved via callback.
102 if (OpR
->isSubClassOf("PointerLikeRegClass"))
103 Res
+= "|(1<<TOI::LookupPtrRegClass)";
105 // Predicate operands. Check to see if the original unexpanded operand
106 // was of type PredicateOperand.
107 if (Inst
.Operands
[i
].Rec
->isSubClassOf("PredicateOperand"))
108 Res
+= "|(1<<TOI::Predicate)";
110 // Optional def operands. Check to see if the original unexpanded operand
111 // was of type OptionalDefOperand.
112 if (Inst
.Operands
[i
].Rec
->isSubClassOf("OptionalDefOperand"))
113 Res
+= "|(1<<TOI::OptionalDef)";
115 // Fill in constraint info.
118 const CGIOperandList::ConstraintInfo
&Constraint
=
119 Inst
.Operands
[i
].Constraints
[j
];
120 if (Constraint
.isNone())
122 else if (Constraint
.isEarlyClobber())
123 Res
+= "(1 << TOI::EARLY_CLOBBER)";
125 assert(Constraint
.isTied());
126 Res
+= "((" + utostr(Constraint
.getTiedOperand()) +
127 " << 16) | (1 << TOI::TIED_TO))";
130 Result
.push_back(Res
);
137 void InstrInfoEmitter::EmitOperandInfo(raw_ostream
&OS
,
138 OperandInfoMapTy
&OperandInfoIDs
) {
139 // ID #0 is for no operand info.
140 unsigned OperandListNum
= 0;
141 OperandInfoIDs
[std::vector
<std::string
>()] = ++OperandListNum
;
144 const CodeGenTarget
&Target
= CDP
.getTargetInfo();
145 for (CodeGenTarget::inst_iterator II
= Target
.inst_begin(),
146 E
= Target
.inst_end(); II
!= E
; ++II
) {
147 std::vector
<std::string
> OperandInfo
= GetOperandInfo(**II
);
148 unsigned &N
= OperandInfoIDs
[OperandInfo
];
149 if (N
!= 0) continue;
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
] << " }, ";
159 void 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
]);
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.
174 for (unsigned j
= 0; j
< NumRegs
; ++j
) {
175 Record
*Reg
= RC
.Elements
[j
];
176 if (!DefSet
.count(Reg
)) {
182 Barriers
.push_back(RC
.TheDef
);
186 //===----------------------------------------------------------------------===//
188 //===----------------------------------------------------------------------===//
190 // run - Emit the main instruction description records for the target...
191 void InstrInfoEmitter::run(raw_ostream
&OS
) {
194 EmitSourceFileHeader("Target Instruction Descriptors", OS
);
195 OS
<< "namespace llvm {\n\n";
197 CodeGenTarget
&Target
= CDP
.getTargetInfo();
198 const std::string
&TargetName
= Target
.getName();
199 Record
*InstrInfo
= Target
.getInstructionSet();
200 const std::vector
<CodeGenRegisterClass
> &RCs
= Target
.getRegisterClasses();
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;
205 std::map
<std::vector
<Record
*>, unsigned> EmittedBarriers
;
206 unsigned BarrierNumber
= 0;
207 std::map
<Record
*, unsigned> BarriersMap
;
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
) {
212 Record
*Inst
= (*II
)->TheDef
;
213 std::vector
<Record
*> Uses
= Inst
->getValueAsListOfDefs("Uses");
215 unsigned &IL
= EmittedLists
[Uses
];
216 if (!IL
) PrintDefList(Uses
, IL
= ++ListNumber
, OS
);
218 std::vector
<Record
*> Defs
= Inst
->getValueAsListOfDefs("Defs");
220 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
));
228 unsigned &IL
= EmittedLists
[Defs
];
229 if (!IL
) PrintDefList(Defs
, IL
= ++ListNumber
, OS
);
233 OperandInfoMapTy OperandInfoIDs
;
235 // Emit all of the operand info records.
236 EmitOperandInfo(OS
, OperandInfoIDs
);
238 // Emit all of the TargetInstrDesc records in their ENUM ordering.
240 OS
<< "\nstatic const TargetInstrDesc " << TargetName
242 const std::vector
<const CodeGenInstruction
*> &NumberedInstructions
=
243 Target
.getInstructionsByEnumValue();
245 for (unsigned i
= 0, e
= NumberedInstructions
.size(); i
!= e
; ++i
)
246 emitRecord(*NumberedInstructions
[i
], i
, InstrInfo
, EmittedLists
,
247 BarriersMap
, OperandInfoIDs
, OS
);
249 OS
<< "} // End llvm namespace \n";
252 void InstrInfoEmitter::emitRecord(const CodeGenInstruction
&Inst
, unsigned Num
,
254 std::map
<std::vector
<Record
*>, unsigned> &EmittedLists
,
255 std::map
<Record
*, unsigned> &BarriersMap
,
256 const OperandInfoMapTy
&OpInfo
,
259 if (!Inst
.Operands
.size() == 0)
260 // Each logical operand can be multiple MI operands.
261 MinOperands
= Inst
.Operands
.back().MIOperandNo
+
262 Inst
.Operands
.back().MINumOperands
;
265 OS
<< Num
<< ",\t" << MinOperands
<< ",\t"
266 << Inst
.Operands
.NumDefs
<< ",\t" << getItinClassNumber(Inst
.TheDef
)
267 << ",\t\"" << Inst
.TheDef
->getName() << "\", 0";
269 // Emit all of the target indepedent flags...
270 if (Inst
.isReturn
) OS
<< "|(1<<TID::Return)";
271 if (Inst
.isBranch
) OS
<< "|(1<<TID::Branch)";
272 if (Inst
.isIndirectBranch
) OS
<< "|(1<<TID::IndirectBranch)";
273 if (Inst
.isCompare
) OS
<< "|(1<<TID::Compare)";
274 if (Inst
.isMoveImm
) OS
<< "|(1<<TID::MoveImm)";
275 if (Inst
.isBitcast
) OS
<< "|(1<<TID::Bitcast)";
276 if (Inst
.isBarrier
) OS
<< "|(1<<TID::Barrier)";
277 if (Inst
.hasDelaySlot
) OS
<< "|(1<<TID::DelaySlot)";
278 if (Inst
.isCall
) OS
<< "|(1<<TID::Call)";
279 if (Inst
.canFoldAsLoad
) OS
<< "|(1<<TID::FoldableAsLoad)";
280 if (Inst
.mayLoad
) OS
<< "|(1<<TID::MayLoad)";
281 if (Inst
.mayStore
) OS
<< "|(1<<TID::MayStore)";
282 if (Inst
.isPredicable
) OS
<< "|(1<<TID::Predicable)";
283 if (Inst
.isConvertibleToThreeAddress
) OS
<< "|(1<<TID::ConvertibleTo3Addr)";
284 if (Inst
.isCommutable
) OS
<< "|(1<<TID::Commutable)";
285 if (Inst
.isTerminator
) OS
<< "|(1<<TID::Terminator)";
286 if (Inst
.isReMaterializable
) OS
<< "|(1<<TID::Rematerializable)";
287 if (Inst
.isNotDuplicable
) OS
<< "|(1<<TID::NotDuplicable)";
288 if (Inst
.Operands
.hasOptionalDef
) OS
<< "|(1<<TID::HasOptionalDef)";
289 if (Inst
.usesCustomInserter
) OS
<< "|(1<<TID::UsesCustomInserter)";
290 if (Inst
.Operands
.isVariadic
)OS
<< "|(1<<TID::Variadic)";
291 if (Inst
.hasSideEffects
) OS
<< "|(1<<TID::UnmodeledSideEffects)";
292 if (Inst
.isAsCheapAsAMove
) OS
<< "|(1<<TID::CheapAsAMove)";
293 if (Inst
.hasExtraSrcRegAllocReq
) OS
<< "|(1<<TID::ExtraSrcRegAllocReq)";
294 if (Inst
.hasExtraDefRegAllocReq
) OS
<< "|(1<<TID::ExtraDefRegAllocReq)";
296 // Emit all of the target-specific flags...
297 BitsInit
*TSF
= Inst
.TheDef
->getValueAsBitsInit("TSFlags");
298 if (!TSF
) throw "no TSFlags?";
300 for (unsigned i
= 0, e
= TSF
->getNumBits(); i
!= e
; ++i
) {
301 if (BitInit
*Bit
= dynamic_cast<BitInit
*>(TSF
->getBit(i
)))
302 Value
|= uint64_t(Bit
->getValue()) << i
;
304 throw "Invalid TSFlags bit in " + Inst
.TheDef
->getName();
310 // Emit the implicit uses and defs lists...
311 std::vector
<Record
*> UseList
= Inst
.TheDef
->getValueAsListOfDefs("Uses");
315 OS
<< "ImplicitList" << EmittedLists
[UseList
] << ", ";
317 std::vector
<Record
*> DefList
= Inst
.TheDef
->getValueAsListOfDefs("Defs");
321 OS
<< "ImplicitList" << EmittedLists
[DefList
] << ", ";
323 std::map
<Record
*, unsigned>::iterator BI
= BarriersMap
.find(Inst
.TheDef
);
324 if (BI
== BarriersMap
.end())
327 OS
<< "Barriers" << BI
->second
<< ", ";
329 // Emit the operand info.
330 std::vector
<std::string
> OperandInfo
= GetOperandInfo(Inst
);
331 if (OperandInfo
.empty())
334 OS
<< "OperandInfo" << OpInfo
.find(OperandInfo
)->second
;
336 OS
<< " }, // Inst #" << Num
<< " = " << Inst
.TheDef
->getName() << "\n";