realize that instructions who match intrinsics that read memory read memory.
[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 enum SDNP {
34   SDNPCommutative, 
35   SDNPAssociative, 
36   SDNPHasChain,
37   SDNPOutFlag,
38   SDNPInFlag,
39   SDNPOptInFlag,
40   SDNPMayLoad,
41   SDNPMayStore
42 };
43
44 /// getValueType - Return the MVT::ValueType that the specified TableGen record
45 /// corresponds to.
46 MVT::ValueType getValueType(Record *Rec);
47
48 std::string getName(MVT::ValueType T);
49 std::string getEnumName(MVT::ValueType T);
50
51 /// getQualifiedName - Return the name of the specified record, with a
52 /// namespace qualifier if the record contains one.
53 std::string getQualifiedName(const Record *R);
54   
55 /// CodeGenTarget - This class corresponds to the Target class in the .td files.
56 ///
57 class CodeGenTarget {
58   Record *TargetRec;
59
60   mutable std::map<std::string, CodeGenInstruction> Instructions;
61   mutable std::vector<CodeGenRegister> Registers;
62   mutable std::vector<CodeGenRegisterClass> RegisterClasses;
63   mutable std::vector<MVT::ValueType> LegalValueTypes;
64   void ReadRegisters() const;
65   void ReadRegisterClasses() const;
66   void ReadInstructions() const;
67   void ReadLegalValueTypes() const;
68 public:
69   CodeGenTarget();
70
71   Record *getTargetRecord() const { return TargetRec; }
72   const std::string &getName() const;
73
74   /// getInstructionSet - Return the InstructionSet object.
75   ///
76   Record *getInstructionSet() const;
77
78   /// getAsmWriter - Return the AssemblyWriter definition for this target.
79   ///
80   Record *getAsmWriter() const;
81
82   const std::vector<CodeGenRegister> &getRegisters() const {
83     if (Registers.empty()) ReadRegisters();
84     return Registers;
85   }
86
87   const std::vector<CodeGenRegisterClass> &getRegisterClasses() const {
88     if (RegisterClasses.empty()) ReadRegisterClasses();
89     return RegisterClasses;
90   }
91   
92   const CodeGenRegisterClass &getRegisterClass(Record *R) const {
93     const std::vector<CodeGenRegisterClass> &RC = getRegisterClasses();
94     for (unsigned i = 0, e = RC.size(); i != e; ++i)
95       if (RC[i].TheDef == R)
96         return RC[i];
97     assert(0 && "Didn't find the register class");
98     abort();
99   }
100   
101   /// getRegisterClassForRegister - Find the register class that contains the
102   /// specified physical register.  If there register exists in multiple
103   /// register classes or is not in a register class, return null.
104   const CodeGenRegisterClass *getRegisterClassForRegister(Record *R) const {
105     const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
106     const CodeGenRegisterClass *FoundRC = 0;
107     for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
108       const CodeGenRegisterClass &RC = RegisterClasses[i];
109       for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
110         if (R == RC.Elements[ei]) {
111           if (FoundRC) return 0;  // In multiple RC's
112           FoundRC = &RC;
113           break;
114         }
115       }
116     }
117     return FoundRC;
118   }
119
120   /// getRegisterVTs - Find the union of all possible ValueTypes for the
121   /// specified physical register.
122   std::vector<unsigned char> getRegisterVTs(Record *R) const;
123   
124   const std::vector<MVT::ValueType> &getLegalValueTypes() const {
125     if (LegalValueTypes.empty()) ReadLegalValueTypes();
126     return LegalValueTypes;
127   }
128   
129   /// isLegalValueType - Return true if the specified value type is natively
130   /// supported by the target (i.e. there are registers that directly hold it).
131   bool isLegalValueType(MVT::ValueType VT) const {
132     const std::vector<MVT::ValueType> &LegalVTs = getLegalValueTypes();
133     for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i)
134       if (LegalVTs[i] == VT) return true;
135     return false;    
136   }
137
138   /// getInstructions - Return all of the instructions defined for this target.
139   ///
140   const std::map<std::string, CodeGenInstruction> &getInstructions() const {
141     if (Instructions.empty()) ReadInstructions();
142     return Instructions;
143   }
144
145   CodeGenInstruction &getInstruction(const std::string &Name) const {
146     const std::map<std::string, CodeGenInstruction> &Insts = getInstructions();
147     assert(Insts.count(Name) && "Not an instruction!");
148     return const_cast<CodeGenInstruction&>(Insts.find(Name)->second);
149   }
150
151   typedef std::map<std::string,
152                    CodeGenInstruction>::const_iterator inst_iterator;
153   inst_iterator inst_begin() const { return getInstructions().begin(); }
154   inst_iterator inst_end() const { return Instructions.end(); }
155
156   /// getInstructionsByEnumValue - Return all of the instructions defined by the
157   /// target, ordered by their enum value.
158   void getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
159                                                 &NumberedInstructions);
160
161
162   /// isLittleEndianEncoding - are instruction bit patterns defined as  [0..n]?
163   ///
164   bool isLittleEndianEncoding() const;
165 };
166
167 /// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
168 /// tablegen class in TargetSelectionDAG.td
169 class ComplexPattern {
170   MVT::ValueType Ty;
171   unsigned NumOperands;
172   std::string SelectFunc;
173   std::vector<Record*> RootNodes;
174   unsigned Properties;
175 public:
176   ComplexPattern() : NumOperands(0) {};
177   ComplexPattern(Record *R);
178
179   MVT::ValueType getValueType() const { return Ty; }
180   unsigned getNumOperands() const { return NumOperands; }
181   const std::string &getSelectFunc() const { return SelectFunc; }
182   const std::vector<Record*> &getRootNodes() const {
183     return RootNodes;
184   }
185   bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
186
187 };
188
189 } // End llvm namespace
190
191 #endif