78c88d797d36f0d471030865f6ecb47d25412ee0
[oota-llvm.git] / utils / TableGen / CodeGenInstruction.h
1 //===- CodeGenInstruction.h - Instruction 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 a wrapper class for the 'Instruction' TableGen class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef CODEGEN_INSTRUCTION_H
15 #define CODEGEN_INSTRUCTION_H
16
17 #include "llvm/CodeGen/ValueTypes.h"
18 #include <string>
19 #include <vector>
20 #include <utility>
21
22 namespace llvm {
23   class Record;
24   class DagInit;
25   class CodeGenTarget;
26   class StringRef;
27   
28   class CGIOperandList {
29   public:
30     class ConstraintInfo {
31       enum { None, EarlyClobber, Tied } Kind;
32       unsigned OtherTiedOperand;
33     public:
34       ConstraintInfo() : Kind(None) {}
35       
36       static ConstraintInfo getEarlyClobber() {
37         ConstraintInfo I;
38         I.Kind = EarlyClobber;
39         I.OtherTiedOperand = 0;
40         return I;
41       }
42       
43       static ConstraintInfo getTied(unsigned Op) {
44         ConstraintInfo I;
45         I.Kind = Tied;
46         I.OtherTiedOperand = Op;
47         return I;
48       }
49       
50       bool isNone() const { return Kind == None; }
51       bool isEarlyClobber() const { return Kind == EarlyClobber; }
52       bool isTied() const { return Kind == Tied; }
53       
54       unsigned getTiedOperand() const {
55         assert(isTied());
56         return OtherTiedOperand;
57       }
58     };
59
60     /// OperandInfo - The information we keep track of for each operand in the
61     /// operand list for a tablegen instruction.
62     struct OperandInfo {
63       /// Rec - The definition this operand is declared as.
64       ///
65       Record *Rec;
66       
67       /// Name - If this operand was assigned a symbolic name, this is it,
68       /// otherwise, it's empty.
69       std::string Name;
70       
71       /// PrinterMethodName - The method used to print operands of this type in
72       /// the asmprinter.
73       std::string PrinterMethodName;
74       
75       /// EncoderMethodName - The method used to get the machine operand value
76       /// for binary encoding. "getMachineOpValue" by default.
77       std::string EncoderMethodName;
78       
79       /// MIOperandNo - Currently (this is meant to be phased out), some logical
80       /// operands correspond to multiple MachineInstr operands.  In the X86
81       /// target for example, one address operand is represented as 4
82       /// MachineOperands.  Because of this, the operand number in the
83       /// OperandList may not match the MachineInstr operand num.  Until it
84       /// does, this contains the MI operand index of this operand.
85       unsigned MIOperandNo;
86       unsigned MINumOperands;   // The number of operands.
87       
88       /// DoNotEncode - Bools are set to true in this vector for each operand in
89       /// the DisableEncoding list.  These should not be emitted by the code
90       /// emitter.
91       std::vector<bool> DoNotEncode;
92       
93       /// MIOperandInfo - Default MI operand type. Note an operand may be made
94       /// up of multiple MI operands.
95       DagInit *MIOperandInfo;
96       
97       /// Constraint info for this operand.  This operand can have pieces, so we
98       /// track constraint info for each.
99       std::vector<ConstraintInfo> Constraints;
100       
101       OperandInfo(Record *R, const std::string &N, const std::string &PMN,
102                   const std::string &EMN, unsigned MION, unsigned MINO,
103                   DagInit *MIOI)
104       : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
105       MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {}
106     };
107     
108     CGIOperandList(Record *D);
109     
110     Record *TheDef;            // The actual record containing this OperandList.
111
112     /// NumDefs - Number of def operands declared, this is the number of
113     /// elements in the instruction's (outs) list.
114     ///
115     unsigned NumDefs;
116     
117     /// OperandList - The list of declared operands, along with their declared
118     /// type (which is a record).
119     std::vector<OperandInfo> OperandList;
120     
121     // Information gleaned from the operand list.
122     bool isPredicable;
123     bool hasOptionalDef;
124     bool isVariadic;
125     
126     // Provide transparent accessors to the operand list.
127     unsigned size() const { return OperandList.size(); }
128     const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
129     OperandInfo &operator[](unsigned i) { return OperandList[i]; }
130     OperandInfo &back() { return OperandList.back(); }
131     const OperandInfo &back() const { return OperandList.back(); }
132     
133     
134     /// getOperandNamed - Return the index of the operand with the specified
135     /// non-empty name.  If the instruction does not have an operand with the
136     /// specified name, throw an exception.
137     unsigned getOperandNamed(StringRef Name) const;
138     
139     /// hasOperandNamed - Query whether the instruction has an operand of the
140     /// given name. If so, return true and set OpIdx to the index of the
141     /// operand. Otherwise, return false.
142     bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
143       
144     /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
145     /// where $foo is a whole operand and $foo.bar refers to a suboperand.
146     /// This throws an exception if the name is invalid.  If AllowWholeOp is
147     /// true, references to operands with suboperands are allowed, otherwise
148     /// not.
149     std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
150                                                   bool AllowWholeOp = true);
151     
152     /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
153     /// flat machineinstr operand #.
154     unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
155       return OperandList[Op.first].MIOperandNo + Op.second;
156     }
157     
158     /// getSubOperandNumber - Unflatten a operand number into an
159     /// operand/suboperand pair.
160     std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
161       for (unsigned i = 0; ; ++i) {
162         assert(i < OperandList.size() && "Invalid flat operand #");
163         if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
164           return std::make_pair(i, Op-OperandList[i].MIOperandNo);
165       }
166     }
167     
168     
169     /// isFlatOperandNotEmitted - Return true if the specified flat operand #
170     /// should not be emitted with the code emitter.
171     bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
172       std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
173       if (OperandList[Op.first].DoNotEncode.size() > Op.second)
174         return OperandList[Op.first].DoNotEncode[Op.second];
175       return false;
176     }
177     
178     void ProcessDisableEncoding(std::string Value);
179   };
180   
181
182   class CodeGenInstruction {
183   public:
184     Record *TheDef;            // The actual record defining this instruction.
185     std::string Namespace;     // The namespace the instruction is in.
186
187     /// AsmString - The format string used to emit a .s file for the
188     /// instruction.
189     std::string AsmString;
190
191     /// Operands - This is information about the (ins) and (outs) list specified
192     /// to the instruction.
193     CGIOperandList Operands;
194
195     /// ImplicitDefs/ImplicitUses - These are lists of registers that are
196     /// implicitly defined and used by the instruction.
197     std::vector<Record*> ImplicitDefs, ImplicitUses;
198
199     // Various boolean values we track for the instruction.
200     bool isReturn;
201     bool isBranch;
202     bool isIndirectBranch;
203     bool isCompare;
204     bool isBarrier;
205     bool isCall;
206     bool canFoldAsLoad;
207     bool mayLoad, mayStore;
208     bool isPredicable;
209     bool isConvertibleToThreeAddress;
210     bool isCommutable;
211     bool isTerminator;
212     bool isReMaterializable;
213     bool hasDelaySlot;
214     bool usesCustomInserter;
215     bool hasCtrlDep;
216     bool isNotDuplicable;
217     bool hasSideEffects;
218     bool neverHasSideEffects;
219     bool isAsCheapAsAMove;
220     bool hasExtraSrcRegAllocReq;
221     bool hasExtraDefRegAllocReq;
222
223
224     CodeGenInstruction(Record *R);
225
226     /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
227     /// implicit def and it has a known VT, return the VT, otherwise return
228     /// MVT::Other.
229     MVT::SimpleValueType
230       HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
231     
232     
233     /// FlattenAsmStringVariants - Flatten the specified AsmString to only
234     /// include text from the specified variant, returning the new string.
235     static std::string FlattenAsmStringVariants(StringRef AsmString,
236                                                 unsigned Variant);
237   };
238   
239   
240   /// CodeGenInstAlias - This represents an InstAlias definition.
241   class CodeGenInstAlias {
242   public:
243     Record *TheDef;            // The actual record defining this InstAlias.
244     
245     /// AsmString - The format string used to emit a .s file for the
246     /// instruction.
247     std::string AsmString;
248     
249     /// Operands - This is information about the (ins) and (outs) list specified
250     /// to the alias.
251     CGIOperandList Operands;
252     
253     /// Result - The result instruction.
254     DagInit *Result;
255     
256     CodeGenInstAlias(Record *R);
257   };    
258 }
259
260 #endif