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