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 /// getTiedOperand - If this operand is tied to another one, return the
109 /// other operand number. Otherwise, return -1.
110 int getTiedRegister() const {
111 for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {
112 const CGIOperandList::ConstraintInfo &CI = Constraints[j];
113 if (CI.isTied()) return CI.getTiedOperand();
119 CGIOperandList(Record *D);
121 Record *TheDef; // The actual record containing this OperandList.
123 /// NumDefs - Number of def operands declared, this is the number of
124 /// elements in the instruction's (outs) list.
128 /// OperandList - The list of declared operands, along with their declared
129 /// type (which is a record).
130 std::vector<OperandInfo> OperandList;
132 // Information gleaned from the operand list.
137 // Provide transparent accessors to the operand list.
138 unsigned size() const { return OperandList.size(); }
139 const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
140 OperandInfo &operator[](unsigned i) { return OperandList[i]; }
141 OperandInfo &back() { return OperandList.back(); }
142 const OperandInfo &back() const { return OperandList.back(); }
145 /// getOperandNamed - Return the index of the operand with the specified
146 /// non-empty name. If the instruction does not have an operand with the
147 /// specified name, throw an exception.
148 unsigned getOperandNamed(StringRef Name) const;
150 /// hasOperandNamed - Query whether the instruction has an operand of the
151 /// given name. If so, return true and set OpIdx to the index of the
152 /// operand. Otherwise, return false.
153 bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
155 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
156 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
157 /// This throws an exception if the name is invalid. If AllowWholeOp is
158 /// true, references to operands with suboperands are allowed, otherwise
160 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
161 bool AllowWholeOp = true);
163 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
164 /// flat machineinstr operand #.
165 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
166 return OperandList[Op.first].MIOperandNo + Op.second;
169 /// getSubOperandNumber - Unflatten a operand number into an
170 /// operand/suboperand pair.
171 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
172 for (unsigned i = 0; ; ++i) {
173 assert(i < OperandList.size() && "Invalid flat operand #");
174 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
175 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
180 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
181 /// should not be emitted with the code emitter.
182 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
183 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
184 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
185 return OperandList[Op.first].DoNotEncode[Op.second];
189 void ProcessDisableEncoding(std::string Value);
193 class CodeGenInstruction {
195 Record *TheDef; // The actual record defining this instruction.
196 std::string Namespace; // The namespace the instruction is in.
198 /// AsmString - The format string used to emit a .s file for the
200 std::string AsmString;
202 /// Operands - This is information about the (ins) and (outs) list specified
203 /// to the instruction.
204 CGIOperandList Operands;
206 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
207 /// implicitly defined and used by the instruction.
208 std::vector<Record*> ImplicitDefs, ImplicitUses;
210 // Various boolean values we track for the instruction.
213 bool isIndirectBranch;
218 bool mayLoad, mayStore;
220 bool isConvertibleToThreeAddress;
223 bool isReMaterializable;
225 bool usesCustomInserter;
227 bool isNotDuplicable;
229 bool neverHasSideEffects;
230 bool isAsCheapAsAMove;
231 bool hasExtraSrcRegAllocReq;
232 bool hasExtraDefRegAllocReq;
235 CodeGenInstruction(Record *R);
237 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
238 /// implicit def and it has a known VT, return the VT, otherwise return
241 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
244 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
245 /// include text from the specified variant, returning the new string.
246 static std::string FlattenAsmStringVariants(StringRef AsmString,
251 /// CodeGenInstAlias - This represents an InstAlias definition.
252 class CodeGenInstAlias {
254 Record *TheDef; // The actual record defining this InstAlias.
256 /// AsmString - The format string used to emit a .s file for the
258 std::string AsmString;
260 /// Operands - This is information about the (ins) and (outs) list specified
262 CGIOperandList Operands;
264 /// Result - The result instruction.
267 CodeGenInstAlias(Record *R);