partition operand processing between aliases and instructions.
[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       /// getTiedOperand - If this operand is tied to another one, return the
109       /// other operand number.  Otherwise, return -1.
110       int getTiedRegister() const {
111         for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {
112           const CGIOperandList::ConstraintInfo &CI = Constraints[j];
113           if (CI.isTied()) return CI.getTiedOperand();
114         }
115         return -1;
116       }
117     };
118     
119     CGIOperandList(Record *D);
120     
121     Record *TheDef;            // The actual record containing this OperandList.
122
123     /// NumDefs - Number of def operands declared, this is the number of
124     /// elements in the instruction's (outs) list.
125     ///
126     unsigned NumDefs;
127     
128     /// OperandList - The list of declared operands, along with their declared
129     /// type (which is a record).
130     std::vector<OperandInfo> OperandList;
131     
132     // Information gleaned from the operand list.
133     bool isPredicable;
134     bool hasOptionalDef;
135     bool isVariadic;
136     
137     // Provide transparent accessors to the operand list.
138     unsigned size() const { return OperandList.size(); }
139     const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
140     OperandInfo &operator[](unsigned i) { return OperandList[i]; }
141     OperandInfo &back() { return OperandList.back(); }
142     const OperandInfo &back() const { return OperandList.back(); }
143     
144     
145     /// getOperandNamed - Return the index of the operand with the specified
146     /// non-empty name.  If the instruction does not have an operand with the
147     /// specified name, throw an exception.
148     unsigned getOperandNamed(StringRef Name) const;
149     
150     /// hasOperandNamed - Query whether the instruction has an operand of the
151     /// given name. If so, return true and set OpIdx to the index of the
152     /// operand. Otherwise, return false.
153     bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
154       
155     /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
156     /// where $foo is a whole operand and $foo.bar refers to a suboperand.
157     /// This throws an exception if the name is invalid.  If AllowWholeOp is
158     /// true, references to operands with suboperands are allowed, otherwise
159     /// not.
160     std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
161                                                   bool AllowWholeOp = true);
162     
163     /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
164     /// flat machineinstr operand #.
165     unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
166       return OperandList[Op.first].MIOperandNo + Op.second;
167     }
168     
169     /// getSubOperandNumber - Unflatten a operand number into an
170     /// operand/suboperand pair.
171     std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
172       for (unsigned i = 0; ; ++i) {
173         assert(i < OperandList.size() && "Invalid flat operand #");
174         if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
175           return std::make_pair(i, Op-OperandList[i].MIOperandNo);
176       }
177     }
178     
179     
180     /// isFlatOperandNotEmitted - Return true if the specified flat operand #
181     /// should not be emitted with the code emitter.
182     bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
183       std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
184       if (OperandList[Op.first].DoNotEncode.size() > Op.second)
185         return OperandList[Op.first].DoNotEncode[Op.second];
186       return false;
187     }
188     
189     void ProcessDisableEncoding(std::string Value);
190   };
191   
192
193   class CodeGenInstruction {
194   public:
195     Record *TheDef;            // The actual record defining this instruction.
196     std::string Namespace;     // The namespace the instruction is in.
197
198     /// AsmString - The format string used to emit a .s file for the
199     /// instruction.
200     std::string AsmString;
201
202     /// Operands - This is information about the (ins) and (outs) list specified
203     /// to the instruction.
204     CGIOperandList Operands;
205
206     /// ImplicitDefs/ImplicitUses - These are lists of registers that are
207     /// implicitly defined and used by the instruction.
208     std::vector<Record*> ImplicitDefs, ImplicitUses;
209
210     // Various boolean values we track for the instruction.
211     bool isReturn;
212     bool isBranch;
213     bool isIndirectBranch;
214     bool isCompare;
215     bool isBarrier;
216     bool isCall;
217     bool canFoldAsLoad;
218     bool mayLoad, mayStore;
219     bool isPredicable;
220     bool isConvertibleToThreeAddress;
221     bool isCommutable;
222     bool isTerminator;
223     bool isReMaterializable;
224     bool hasDelaySlot;
225     bool usesCustomInserter;
226     bool hasCtrlDep;
227     bool isNotDuplicable;
228     bool hasSideEffects;
229     bool neverHasSideEffects;
230     bool isAsCheapAsAMove;
231     bool hasExtraSrcRegAllocReq;
232     bool hasExtraDefRegAllocReq;
233
234
235     CodeGenInstruction(Record *R);
236
237     /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
238     /// implicit def and it has a known VT, return the VT, otherwise return
239     /// MVT::Other.
240     MVT::SimpleValueType
241       HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
242     
243     
244     /// FlattenAsmStringVariants - Flatten the specified AsmString to only
245     /// include text from the specified variant, returning the new string.
246     static std::string FlattenAsmStringVariants(StringRef AsmString,
247                                                 unsigned Variant);
248   };
249   
250   
251   /// CodeGenInstAlias - This represents an InstAlias definition.
252   class CodeGenInstAlias {
253   public:
254     Record *TheDef;            // The actual record defining this InstAlias.
255     
256     /// AsmString - The format string used to emit a .s file for the
257     /// instruction.
258     std::string AsmString;
259     
260     /// Operands - This is information about the (ins) and (outs) list specified
261     /// to the alias.
262     CGIOperandList Operands;
263     
264     /// Result - The result instruction.
265     DagInit *Result;
266     
267     CodeGenInstAlias(Record *R);
268   };    
269 }
270
271 #endif