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"
18 #include "llvm/ADT/StringRef.h"
29 class CGIOperandList {
31 class ConstraintInfo {
32 enum { None, EarlyClobber, Tied } Kind;
33 unsigned OtherTiedOperand;
35 ConstraintInfo() : Kind(None) {}
37 static ConstraintInfo getEarlyClobber() {
39 I.Kind = EarlyClobber;
40 I.OtherTiedOperand = 0;
44 static ConstraintInfo getTied(unsigned Op) {
47 I.OtherTiedOperand = Op;
51 bool isNone() const { return Kind == None; }
52 bool isEarlyClobber() const { return Kind == EarlyClobber; }
53 bool isTied() const { return Kind == Tied; }
55 unsigned getTiedOperand() const {
57 return OtherTiedOperand;
61 /// OperandInfo - The information we keep track of for each operand in the
62 /// operand list for a tablegen instruction.
64 /// Rec - The definition this operand is declared as.
68 /// Name - If this operand was assigned a symbolic name, this is it,
69 /// otherwise, it's empty.
72 /// PrinterMethodName - The method used to print operands of this type in
74 std::string PrinterMethodName;
76 /// EncoderMethodName - The method used to get the machine operand value
77 /// for binary encoding. "getMachineOpValue" by default.
78 std::string EncoderMethodName;
80 /// MIOperandNo - Currently (this is meant to be phased out), some logical
81 /// operands correspond to multiple MachineInstr operands. In the X86
82 /// target for example, one address operand is represented as 4
83 /// MachineOperands. Because of this, the operand number in the
84 /// OperandList may not match the MachineInstr operand num. Until it
85 /// does, this contains the MI operand index of this operand.
87 unsigned MINumOperands; // The number of operands.
89 /// DoNotEncode - Bools are set to true in this vector for each operand in
90 /// the DisableEncoding list. These should not be emitted by the code
92 std::vector<bool> DoNotEncode;
94 /// MIOperandInfo - Default MI operand type. Note an operand may be made
95 /// up of multiple MI operands.
96 DagInit *MIOperandInfo;
98 /// Constraint info for this operand. This operand can have pieces, so we
99 /// track constraint info for each.
100 std::vector<ConstraintInfo> Constraints;
102 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
103 const std::string &EMN, unsigned MION, unsigned MINO,
105 : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
106 MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {}
109 /// getTiedOperand - If this operand is tied to another one, return the
110 /// other operand number. Otherwise, return -1.
111 int getTiedRegister() const {
112 for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {
113 const CGIOperandList::ConstraintInfo &CI = Constraints[j];
114 if (CI.isTied()) return CI.getTiedOperand();
120 CGIOperandList(Record *D);
122 Record *TheDef; // The actual record containing this OperandList.
124 /// NumDefs - Number of def operands declared, this is the number of
125 /// elements in the instruction's (outs) list.
129 /// OperandList - The list of declared operands, along with their declared
130 /// type (which is a record).
131 std::vector<OperandInfo> OperandList;
133 // Information gleaned from the operand list.
138 // Provide transparent accessors to the operand list.
139 unsigned size() const { return OperandList.size(); }
140 const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
141 OperandInfo &operator[](unsigned i) { return OperandList[i]; }
142 OperandInfo &back() { return OperandList.back(); }
143 const OperandInfo &back() const { return OperandList.back(); }
146 /// getOperandNamed - Return the index of the operand with the specified
147 /// non-empty name. If the instruction does not have an operand with the
148 /// specified name, throw an exception.
149 unsigned getOperandNamed(StringRef Name) const;
151 /// hasOperandNamed - Query whether the instruction has an operand of the
152 /// given name. If so, return true and set OpIdx to the index of the
153 /// operand. Otherwise, return false.
154 bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
156 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
157 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
158 /// This throws an exception if the name is invalid. If AllowWholeOp is
159 /// true, references to operands with suboperands are allowed, otherwise
161 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
162 bool AllowWholeOp = true);
164 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
165 /// flat machineinstr operand #.
166 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
167 return OperandList[Op.first].MIOperandNo + Op.second;
170 /// getSubOperandNumber - Unflatten a operand number into an
171 /// operand/suboperand pair.
172 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
173 for (unsigned i = 0; ; ++i) {
174 assert(i < OperandList.size() && "Invalid flat operand #");
175 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
176 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
181 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
182 /// should not be emitted with the code emitter.
183 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
184 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
185 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
186 return OperandList[Op.first].DoNotEncode[Op.second];
190 void ProcessDisableEncoding(std::string Value);
194 class CodeGenInstruction {
196 Record *TheDef; // The actual record defining this instruction.
197 std::string Namespace; // The namespace the instruction is in.
199 /// AsmString - The format string used to emit a .s file for the
201 std::string AsmString;
203 /// Operands - This is information about the (ins) and (outs) list specified
204 /// to the instruction.
205 CGIOperandList Operands;
207 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
208 /// implicitly defined and used by the instruction.
209 std::vector<Record*> ImplicitDefs, ImplicitUses;
211 // Various boolean values we track for the instruction.
214 bool isIndirectBranch;
219 bool mayLoad, mayStore;
221 bool isConvertibleToThreeAddress;
224 bool isReMaterializable;
226 bool usesCustomInserter;
228 bool isNotDuplicable;
230 bool neverHasSideEffects;
231 bool isAsCheapAsAMove;
232 bool hasExtraSrcRegAllocReq;
233 bool hasExtraDefRegAllocReq;
236 CodeGenInstruction(Record *R);
238 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
239 /// implicit def and it has a known VT, return the VT, otherwise return
242 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
245 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
246 /// include text from the specified variant, returning the new string.
247 static std::string FlattenAsmStringVariants(StringRef AsmString,
252 /// CodeGenInstAlias - This represents an InstAlias definition.
253 class CodeGenInstAlias {
255 Record *TheDef; // The actual record defining this InstAlias.
257 /// AsmString - The format string used to emit a .s file for the
259 std::string AsmString;
261 /// Result - The result instruction.
264 /// ResultInst - The instruction generated by the alias (decoded from
266 CodeGenInstruction *ResultInst;
269 struct ResultOperand {
273 ResultOperand(StringRef N, Record *r) : Name(N), R(r) {}
276 /// ResultOperands - The decoded operands for the result instruction.
277 std::vector<ResultOperand> ResultOperands;
279 CodeGenInstAlias(Record *R, CodeGenTarget &T);
281 /// getResultInstOperandIndexForResultOperandIndex - Given an index into the
282 /// ResultOperands array, translate it to a valid index in ResultInst's
284 unsigned getResultInstOperandIndexForResultOperandIndex(unsigned i) const;