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 CodeGenInstruction {
30 Record *TheDef; // The actual record defining this instruction.
31 std::string Namespace; // The namespace the instruction is in.
33 /// AsmString - The format string used to emit a .s file for the
35 std::string AsmString;
37 class ConstraintInfo {
38 enum { None, EarlyClobber, Tied } Kind;
39 unsigned OtherTiedOperand;
41 ConstraintInfo() : Kind(None) {}
43 static ConstraintInfo getEarlyClobber() {
45 I.Kind = EarlyClobber;
46 I.OtherTiedOperand = 0;
50 static ConstraintInfo getTied(unsigned Op) {
53 I.OtherTiedOperand = Op;
57 bool isNone() const { return Kind == None; }
58 bool isEarlyClobber() const { return Kind == EarlyClobber; }
59 bool isTied() const { return Kind == Tied; }
61 unsigned getTiedOperand() const {
63 return OtherTiedOperand;
67 /// OperandInfo - The information we keep track of for each operand in the
68 /// operand list for a tablegen instruction.
70 /// Rec - The definition this operand is declared as.
74 /// Name - If this operand was assigned a symbolic name, this is it,
75 /// otherwise, it's empty.
78 /// PrinterMethodName - The method used to print operands of this type in
80 std::string PrinterMethodName;
82 /// EncoderMethodName - The method used to get the machine operand value
83 /// for binary encoding. "getMachineOpValue" by default.
84 std::string EncoderMethodName;
86 /// MIOperandNo - Currently (this is meant to be phased out), some logical
87 /// operands correspond to multiple MachineInstr operands. In the X86
88 /// target for example, one address operand is represented as 4
89 /// MachineOperands. Because of this, the operand number in the
90 /// OperandList may not match the MachineInstr operand num. Until it
91 /// does, this contains the MI operand index of this operand.
93 unsigned MINumOperands; // The number of operands.
95 /// DoNotEncode - Bools are set to true in this vector for each operand in
96 /// the DisableEncoding list. These should not be emitted by the code
98 std::vector<bool> DoNotEncode;
100 /// MIOperandInfo - Default MI operand type. Note an operand may be made
101 /// up of multiple MI operands.
102 DagInit *MIOperandInfo;
104 /// Constraint info for this operand. This operand can have pieces, so we
105 /// track constraint info for each.
106 std::vector<ConstraintInfo> Constraints;
108 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
109 const std::string &EMN, unsigned MION, unsigned MINO,
111 : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
112 MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {}
115 /// NumDefs - Number of def operands declared, this is the number of
116 /// elements in the instruction's (outs) list.
120 /// OperandList - The list of declared operands, along with their declared
121 /// type (which is a record).
122 std::vector<OperandInfo> OperandList;
124 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
125 /// implicitly defined and used by the instruction.
126 std::vector<Record*> ImplicitDefs, ImplicitUses;
128 // Various boolean values we track for the instruction.
131 bool isIndirectBranch;
136 bool mayLoad, mayStore;
138 bool isConvertibleToThreeAddress;
141 bool isReMaterializable;
143 bool usesCustomInserter;
146 bool isNotDuplicable;
149 bool neverHasSideEffects;
150 bool isAsCheapAsAMove;
151 bool hasExtraSrcRegAllocReq;
152 bool hasExtraDefRegAllocReq;
154 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
155 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
156 /// This throws an exception if the name is invalid. If AllowWholeOp is
157 /// true, references to operands with suboperands are allowed, otherwise
159 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
160 bool AllowWholeOp = true);
162 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
163 /// flat machineinstr operand #.
164 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
165 return OperandList[Op.first].MIOperandNo + Op.second;
168 /// getSubOperandNumber - Unflatten a operand number into an
169 /// operand/suboperand pair.
170 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
171 for (unsigned i = 0; ; ++i) {
172 assert(i < OperandList.size() && "Invalid flat operand #");
173 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
174 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
179 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
180 /// should not be emitted with the code emitter.
181 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
182 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
183 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
184 return OperandList[Op.first].DoNotEncode[Op.second];
188 CodeGenInstruction(Record *R);
190 /// getOperandNamed - Return the index of the operand with the specified
191 /// non-empty name. If the instruction does not have an operand with the
192 /// specified name, throw an exception.
193 unsigned getOperandNamed(const std::string &Name) const;
195 /// hasOperandNamed - Query whether the instruction has an operand of the
196 /// given name. If so, return true and set OpIdx to the index of the
197 /// operand. Otherwise, return false.
198 bool hasOperandNamed(const std::string &Name, unsigned &OpIdx) const;
200 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
201 /// implicit def and it has a known VT, return the VT, otherwise return
204 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
207 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
208 /// include text from the specified variant, returning the new string.
209 static std::string FlattenAsmStringVariants(StringRef AsmString,