Factor the code for determining the target-specific instruction
[oota-llvm.git] / utils / TableGen / CodeGenTarget.h
1 //===- CodeGenTarget.h - Target Class Wrapper -------------------*- C++ -*-===//
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 file defines wrappers for the Target class and related global
11 // functionality.  This makes it easier to access the data and provides a single
12 // place that needs to check it for validity.  All of these classes throw
13 // exceptions on error conditions.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef CODEGEN_TARGET_H
18 #define CODEGEN_TARGET_H
19
20 #include "CodeGenRegisters.h"
21 #include "CodeGenInstruction.h"
22 #include <iosfwd>
23 #include <map>
24
25 namespace llvm {
26
27 class Record;
28 class RecordKeeper;
29 struct CodeGenRegister;
30 class CodeGenTarget;
31
32 // SelectionDAG node properties.
33 //  SDNPMemOperand: indicates that a node touches memory and therefore must
34 //                  have an associated memory operand that describes the access.
35 enum SDNP {
36   SDNPCommutative, 
37   SDNPAssociative, 
38   SDNPHasChain,
39   SDNPOutFlag,
40   SDNPInFlag,
41   SDNPOptInFlag,
42   SDNPMayLoad,
43   SDNPMayStore,
44   SDNPSideEffect,
45   SDNPMemOperand
46 };
47
48 // ComplexPattern attributes.
49 enum CPAttr { CPAttrParentAsRoot };
50
51 /// getValueType - Return the MVT::SimpleValueType that the specified TableGen
52 /// record corresponds to.
53 MVT::SimpleValueType getValueType(Record *Rec);
54
55 std::string getName(MVT::SimpleValueType T);
56 std::string getEnumName(MVT::SimpleValueType T);
57
58 /// getQualifiedName - Return the name of the specified record, with a
59 /// namespace qualifier if the record contains one.
60 std::string getQualifiedName(const Record *R);
61   
62 /// CodeGenTarget - This class corresponds to the Target class in the .td files.
63 ///
64 class CodeGenTarget {
65   Record *TargetRec;
66
67   mutable std::map<std::string, CodeGenInstruction> Instructions;
68   mutable std::vector<CodeGenRegister> Registers;
69   mutable std::vector<CodeGenRegisterClass> RegisterClasses;
70   mutable std::vector<MVT::SimpleValueType> LegalValueTypes;
71   void ReadRegisters() const;
72   void ReadRegisterClasses() const;
73   void ReadInstructions() const;
74   void ReadLegalValueTypes() const;
75 public:
76   CodeGenTarget();
77
78   Record *getTargetRecord() const { return TargetRec; }
79   const std::string &getName() const;
80
81   /// getInstNamespace - Return the target-specific instruction namespace.
82   ///
83   std::string getInstNamespace() const;
84
85   /// getInstructionSet - Return the InstructionSet object.
86   ///
87   Record *getInstructionSet() const;
88
89   /// getAsmWriter - Return the AssemblyWriter definition for this target.
90   ///
91   Record *getAsmWriter() const;
92
93   const std::vector<CodeGenRegister> &getRegisters() const {
94     if (Registers.empty()) ReadRegisters();
95     return Registers;
96   }
97
98   const std::vector<CodeGenRegisterClass> &getRegisterClasses() const {
99     if (RegisterClasses.empty()) ReadRegisterClasses();
100     return RegisterClasses;
101   }
102   
103   const CodeGenRegisterClass &getRegisterClass(Record *R) const {
104     const std::vector<CodeGenRegisterClass> &RC = getRegisterClasses();
105     for (unsigned i = 0, e = RC.size(); i != e; ++i)
106       if (RC[i].TheDef == R)
107         return RC[i];
108     assert(0 && "Didn't find the register class");
109     abort();
110   }
111   
112   /// getRegisterClassForRegister - Find the register class that contains the
113   /// specified physical register.  If there register exists in multiple
114   /// register classes or is not in a register class, return null.
115   const CodeGenRegisterClass *getRegisterClassForRegister(Record *R) const {
116     const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
117     const CodeGenRegisterClass *FoundRC = 0;
118     for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
119       const CodeGenRegisterClass &RC = RegisterClasses[i];
120       for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
121         if (R == RC.Elements[ei]) {
122           if (FoundRC) return 0;  // In multiple RC's
123           FoundRC = &RC;
124           break;
125         }
126       }
127     }
128     return FoundRC;
129   }
130
131   /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the
132   /// specified physical register.
133   std::vector<unsigned char> getRegisterVTs(Record *R) const;
134   
135   const std::vector<MVT::SimpleValueType> &getLegalValueTypes() const {
136     if (LegalValueTypes.empty()) ReadLegalValueTypes();
137     return LegalValueTypes;
138   }
139   
140   /// isLegalValueType - Return true if the specified value type is natively
141   /// supported by the target (i.e. there are registers that directly hold it).
142   bool isLegalValueType(MVT::SimpleValueType VT) const {
143     const std::vector<MVT::SimpleValueType> &LegalVTs = getLegalValueTypes();
144     for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i)
145       if (LegalVTs[i] == VT) return true;
146     return false;    
147   }
148
149   /// getInstructions - Return all of the instructions defined for this target.
150   ///
151   const std::map<std::string, CodeGenInstruction> &getInstructions() const {
152     if (Instructions.empty()) ReadInstructions();
153     return Instructions;
154   }
155   std::map<std::string, CodeGenInstruction> &getInstructions() {
156     if (Instructions.empty()) ReadInstructions();
157     return Instructions;
158   }
159
160   CodeGenInstruction &getInstruction(const std::string &Name) const {
161     const std::map<std::string, CodeGenInstruction> &Insts = getInstructions();
162     assert(Insts.count(Name) && "Not an instruction!");
163     return const_cast<CodeGenInstruction&>(Insts.find(Name)->second);
164   }
165
166   typedef std::map<std::string,
167                    CodeGenInstruction>::const_iterator inst_iterator;
168   inst_iterator inst_begin() const { return getInstructions().begin(); }
169   inst_iterator inst_end() const { return Instructions.end(); }
170
171   /// getInstructionsByEnumValue - Return all of the instructions defined by the
172   /// target, ordered by their enum value.
173   void getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
174                                                 &NumberedInstructions);
175
176
177   /// isLittleEndianEncoding - are instruction bit patterns defined as  [0..n]?
178   ///
179   bool isLittleEndianEncoding() const;
180 };
181
182 /// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
183 /// tablegen class in TargetSelectionDAG.td
184 class ComplexPattern {
185   MVT::SimpleValueType Ty;
186   unsigned NumOperands;
187   std::string SelectFunc;
188   std::vector<Record*> RootNodes;
189   unsigned Properties; // Node properties
190   unsigned Attributes; // Pattern attributes
191 public:
192   ComplexPattern() : NumOperands(0) {};
193   ComplexPattern(Record *R);
194
195   MVT::SimpleValueType getValueType() const { return Ty; }
196   unsigned getNumOperands() const { return NumOperands; }
197   const std::string &getSelectFunc() const { return SelectFunc; }
198   const std::vector<Record*> &getRootNodes() const {
199     return RootNodes;
200   }
201   bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
202   bool hasAttribute(enum CPAttr Attr) const { return Attributes & (1 << Attr); }
203 };
204
205 } // End llvm namespace
206
207 #endif