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"
27 class CodeGenInstruction {
29 Record *TheDef; // The actual record defining this instruction.
30 std::string Namespace; // The namespace the instruction is in.
32 /// AsmString - The format string used to emit a .s file for the
34 std::string AsmString;
36 class ConstraintInfo {
37 enum { None, EarlyClobber, Tied } Kind;
38 unsigned OtherTiedOperand;
40 ConstraintInfo() : Kind(None) {}
42 static ConstraintInfo getEarlyClobber() {
44 I.Kind = EarlyClobber;
45 I.OtherTiedOperand = 0;
49 static ConstraintInfo getTied(unsigned Op) {
52 I.OtherTiedOperand = Op;
56 bool isNone() const { return Kind == None; }
57 bool isEarlyClobber() const { return Kind == EarlyClobber; }
58 bool isTied() const { return Kind == Tied; }
60 unsigned getTiedOperand() const {
62 return OtherTiedOperand;
66 /// OperandInfo - The information we keep track of for each operand in the
67 /// operand list for a tablegen instruction.
69 /// Rec - The definition this operand is declared as.
73 /// Name - If this operand was assigned a symbolic name, this is it,
74 /// otherwise, it's empty.
77 /// PrinterMethodName - The method used to print operands of this type in
79 std::string PrinterMethodName;
81 /// EncoderMethodName - The method used to get the machine operand value
82 /// for binary encoding. "getMachineOpValue" by default.
83 std::string EncoderMethodName;
85 /// MIOperandNo - Currently (this is meant to be phased out), some logical
86 /// operands correspond to multiple MachineInstr operands. In the X86
87 /// target for example, one address operand is represented as 4
88 /// MachineOperands. Because of this, the operand number in the
89 /// OperandList may not match the MachineInstr operand num. Until it
90 /// does, this contains the MI operand index of this operand.
92 unsigned MINumOperands; // The number of operands.
94 /// DoNotEncode - Bools are set to true in this vector for each operand in
95 /// the DisableEncoding list. These should not be emitted by the code
97 std::vector<bool> DoNotEncode;
99 /// MIOperandInfo - Default MI operand type. Note an operand may be made
100 /// up of multiple MI operands.
101 DagInit *MIOperandInfo;
103 /// Constraint info for this operand. This operand can have pieces, so we
104 /// track constraint info for each.
105 std::vector<ConstraintInfo> Constraints;
107 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
108 const std::string &EMN, unsigned MION, unsigned MINO,
110 : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
111 MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {}
114 /// NumDefs - Number of def operands declared, this is the number of
115 /// elements in the instruction's (outs) list.
119 /// OperandList - The list of declared operands, along with their declared
120 /// type (which is a record).
121 std::vector<OperandInfo> OperandList;
123 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
124 /// implicitly defined and used by the instruction.
125 std::vector<Record*> ImplicitDefs, ImplicitUses;
127 // Various boolean values we track for the instruction.
130 bool isIndirectBranch;
135 bool mayLoad, mayStore;
137 bool isConvertibleToThreeAddress;
140 bool isReMaterializable;
142 bool usesCustomInserter;
145 bool isNotDuplicable;
148 bool neverHasSideEffects;
149 bool isAsCheapAsAMove;
150 bool hasExtraSrcRegAllocReq;
151 bool hasExtraDefRegAllocReq;
153 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
154 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
155 /// This throws an exception if the name is invalid. If AllowWholeOp is
156 /// true, references to operands with suboperands are allowed, otherwise
158 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
159 bool AllowWholeOp = true);
161 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
162 /// flat machineinstr operand #.
163 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
164 return OperandList[Op.first].MIOperandNo + Op.second;
167 /// getSubOperandNumber - Unflatten a operand number into an
168 /// operand/suboperand pair.
169 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
170 for (unsigned i = 0; ; ++i) {
171 assert(i < OperandList.size() && "Invalid flat operand #");
172 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
173 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
178 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
179 /// should not be emitted with the code emitter.
180 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
181 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
182 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
183 return OperandList[Op.first].DoNotEncode[Op.second];
187 CodeGenInstruction(Record *R, const std::string &AsmStr);
189 /// getOperandNamed - Return the index of the operand with the specified
190 /// non-empty name. If the instruction does not have an operand with the
191 /// specified name, throw an exception.
192 unsigned getOperandNamed(const std::string &Name) const;
194 /// hasOperandNamed - Query whether the instruction has an operand of the
195 /// given name. If so, return true and set OpIdx to the index of the
196 /// operand. Otherwise, return false.
197 bool hasOperandNamed(const std::string &Name, unsigned &OpIdx) const;
199 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
200 /// implicit def and it has a known VT, return the VT, otherwise return
203 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;