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