Do not pass a copy of the value map, pass a reference to it.
[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 isBarrier;
127     bool isCall;
128     bool canFoldAsLoad;
129     bool mayLoad, mayStore;
130     bool isPredicable;
131     bool isConvertibleToThreeAddress;
132     bool isCommutable;
133     bool isTerminator;
134     bool isReMaterializable;
135     bool hasDelaySlot;
136     bool usesCustomInserter;
137     bool isVariadic;
138     bool hasCtrlDep;
139     bool isNotDuplicable;
140     bool hasOptionalDef;
141     bool hasSideEffects;
142     bool neverHasSideEffects;
143     bool isAsCheapAsAMove;
144     bool hasExtraSrcRegAllocReq;
145     bool hasExtraDefRegAllocReq;
146     
147     /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
148     /// where $foo is a whole operand and $foo.bar refers to a suboperand.
149     /// This throws an exception if the name is invalid.  If AllowWholeOp is
150     /// true, references to operands with suboperands are allowed, otherwise
151     /// not.
152     std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
153                                                   bool AllowWholeOp = true);
154     
155     /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
156     /// flat machineinstr operand #.
157     unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
158       return OperandList[Op.first].MIOperandNo + Op.second;
159     }
160     
161     /// getSubOperandNumber - Unflatten a operand number into an
162     /// operand/suboperand pair.
163     std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
164       for (unsigned i = 0; ; ++i) {
165         assert(i < OperandList.size() && "Invalid flat operand #");
166         if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
167           return std::make_pair(i, Op-OperandList[i].MIOperandNo);
168       }
169     }
170     
171     
172     /// isFlatOperandNotEmitted - Return true if the specified flat operand #
173     /// should not be emitted with the code emitter.
174     bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
175       std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
176       if (OperandList[Op.first].DoNotEncode.size() > Op.second)
177         return OperandList[Op.first].DoNotEncode[Op.second];
178       return false;
179     }
180
181     CodeGenInstruction(Record *R, const std::string &AsmStr);
182
183     /// getOperandNamed - Return the index of the operand with the specified
184     /// non-empty name.  If the instruction does not have an operand with the
185     /// specified name, throw an exception.
186     unsigned getOperandNamed(const std::string &Name) const;
187     
188     /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
189     /// implicit def and it has a known VT, return the VT, otherwise return
190     /// MVT::Other.
191     MVT::SimpleValueType 
192       HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
193   };
194 }
195
196 #endif