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