1 //===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a wrapper class for the 'Instruction' TableGen class.
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_INSTRUCTION_H
15 #define CODEGEN_INSTRUCTION_H
17 #include "llvm/CodeGen/ValueTypes.h"
28 class CGIOperandList {
30 class ConstraintInfo {
31 enum { None, EarlyClobber, Tied } Kind;
32 unsigned OtherTiedOperand;
34 ConstraintInfo() : Kind(None) {}
36 static ConstraintInfo getEarlyClobber() {
38 I.Kind = EarlyClobber;
39 I.OtherTiedOperand = 0;
43 static ConstraintInfo getTied(unsigned Op) {
46 I.OtherTiedOperand = Op;
50 bool isNone() const { return Kind == None; }
51 bool isEarlyClobber() const { return Kind == EarlyClobber; }
52 bool isTied() const { return Kind == Tied; }
54 unsigned getTiedOperand() const {
56 return OtherTiedOperand;
60 /// OperandInfo - The information we keep track of for each operand in the
61 /// operand list for a tablegen instruction.
63 /// Rec - The definition this operand is declared as.
67 /// Name - If this operand was assigned a symbolic name, this is it,
68 /// otherwise, it's empty.
71 /// PrinterMethodName - The method used to print operands of this type in
73 std::string PrinterMethodName;
75 /// EncoderMethodName - The method used to get the machine operand value
76 /// for binary encoding. "getMachineOpValue" by default.
77 std::string EncoderMethodName;
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.
86 unsigned MINumOperands; // The number of operands.
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
91 std::vector<bool> DoNotEncode;
93 /// MIOperandInfo - Default MI operand type. Note an operand may be made
94 /// up of multiple MI operands.
95 DagInit *MIOperandInfo;
97 /// Constraint info for this operand. This operand can have pieces, so we
98 /// track constraint info for each.
99 std::vector<ConstraintInfo> Constraints;
101 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
102 const std::string &EMN, unsigned MION, unsigned MINO,
104 : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
105 MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {}
108 CGIOperandList(Record *D);
110 Record *TheDef; // The actual record containing this OperandList.
112 /// NumDefs - Number of def operands declared, this is the number of
113 /// elements in the instruction's (outs) list.
117 /// OperandList - The list of declared operands, along with their declared
118 /// type (which is a record).
119 std::vector<OperandInfo> OperandList;
121 // Information gleaned from the operand list.
126 // Provide transparent accessors to the operand list.
127 unsigned size() const { return OperandList.size(); }
128 const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
129 OperandInfo &operator[](unsigned i) { return OperandList[i]; }
130 OperandInfo &back() { return OperandList.back(); }
131 const OperandInfo &back() const { return OperandList.back(); }
134 /// getOperandNamed - Return the index of the operand with the specified
135 /// non-empty name. If the instruction does not have an operand with the
136 /// specified name, throw an exception.
137 unsigned getOperandNamed(StringRef Name) const;
139 /// hasOperandNamed - Query whether the instruction has an operand of the
140 /// given name. If so, return true and set OpIdx to the index of the
141 /// operand. Otherwise, return false.
142 bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
144 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
145 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
146 /// This throws an exception if the name is invalid. If AllowWholeOp is
147 /// true, references to operands with suboperands are allowed, otherwise
149 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
150 bool AllowWholeOp = true);
152 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
153 /// flat machineinstr operand #.
154 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
155 return OperandList[Op.first].MIOperandNo + Op.second;
158 /// getSubOperandNumber - Unflatten a operand number into an
159 /// operand/suboperand pair.
160 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
161 for (unsigned i = 0; ; ++i) {
162 assert(i < OperandList.size() && "Invalid flat operand #");
163 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
164 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
169 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
170 /// should not be emitted with the code emitter.
171 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
172 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
173 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
174 return OperandList[Op.first].DoNotEncode[Op.second];
178 void ProcessDisableEncoding(std::string Value);
182 class CodeGenInstruction {
184 Record *TheDef; // The actual record defining this instruction.
185 std::string Namespace; // The namespace the instruction is in.
187 /// AsmString - The format string used to emit a .s file for the
189 std::string AsmString;
191 /// Operands - This is information about the (ins) and (outs) list specified
192 /// to the instruction.
193 CGIOperandList Operands;
195 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
196 /// implicitly defined and used by the instruction.
197 std::vector<Record*> ImplicitDefs, ImplicitUses;
199 // Various boolean values we track for the instruction.
202 bool isIndirectBranch;
207 bool mayLoad, mayStore;
209 bool isConvertibleToThreeAddress;
212 bool isReMaterializable;
214 bool usesCustomInserter;
216 bool isNotDuplicable;
218 bool neverHasSideEffects;
219 bool isAsCheapAsAMove;
220 bool hasExtraSrcRegAllocReq;
221 bool hasExtraDefRegAllocReq;
224 CodeGenInstruction(Record *R);
226 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
227 /// implicit def and it has a known VT, return the VT, otherwise return
230 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
233 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
234 /// include text from the specified variant, returning the new string.
235 static std::string FlattenAsmStringVariants(StringRef AsmString,
240 /// CodeGenInstAlias - This represents an InstAlias definition.
241 class CodeGenInstAlias {
243 Record *TheDef; // The actual record defining this InstAlias.
245 /// AsmString - The format string used to emit a .s file for the
247 std::string AsmString;
249 /// Operands - This is information about the (ins) and (outs) list specified
251 CGIOperandList Operands;
253 CodeGenInstAlias(Record *R);