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"
19 #include "llvm/Support/SourceMgr.h"
30 class CGIOperandList
{
32 class ConstraintInfo
{
33 enum { None
, EarlyClobber
, Tied
} Kind
;
34 unsigned OtherTiedOperand
;
36 ConstraintInfo() : Kind(None
) {}
38 static ConstraintInfo
getEarlyClobber() {
40 I
.Kind
= EarlyClobber
;
41 I
.OtherTiedOperand
= 0;
45 static ConstraintInfo
getTied(unsigned Op
) {
48 I
.OtherTiedOperand
= Op
;
52 bool isNone() const { return Kind
== None
; }
53 bool isEarlyClobber() const { return Kind
== EarlyClobber
; }
54 bool isTied() const { return Kind
== Tied
; }
56 unsigned getTiedOperand() const {
58 return OtherTiedOperand
;
62 /// OperandInfo - The information we keep track of for each operand in the
63 /// operand list for a tablegen instruction.
65 /// Rec - The definition this operand is declared as.
69 /// Name - If this operand was assigned a symbolic name, this is it,
70 /// otherwise, it's empty.
73 /// PrinterMethodName - The method used to print operands of this type in
75 std::string PrinterMethodName
;
77 /// EncoderMethodName - The method used to get the machine operand value
78 /// for binary encoding. "getMachineOpValue" by default.
79 std::string EncoderMethodName
;
81 /// MIOperandNo - Currently (this is meant to be phased out), some logical
82 /// operands correspond to multiple MachineInstr operands. In the X86
83 /// target for example, one address operand is represented as 4
84 /// MachineOperands. Because of this, the operand number in the
85 /// OperandList may not match the MachineInstr operand num. Until it
86 /// does, this contains the MI operand index of this operand.
88 unsigned MINumOperands
; // The number of operands.
90 /// DoNotEncode - Bools are set to true in this vector for each operand in
91 /// the DisableEncoding list. These should not be emitted by the code
93 std::vector
<bool> DoNotEncode
;
95 /// MIOperandInfo - Default MI operand type. Note an operand may be made
96 /// up of multiple MI operands.
97 DagInit
*MIOperandInfo
;
99 /// Constraint info for this operand. This operand can have pieces, so we
100 /// track constraint info for each.
101 std::vector
<ConstraintInfo
> Constraints
;
103 OperandInfo(Record
*R
, const std::string
&N
, const std::string
&PMN
,
104 const std::string
&EMN
, unsigned MION
, unsigned MINO
,
106 : Rec(R
), Name(N
), PrinterMethodName(PMN
), EncoderMethodName(EMN
),
107 MIOperandNo(MION
), MINumOperands(MINO
), MIOperandInfo(MIOI
) {}
110 /// getTiedOperand - If this operand is tied to another one, return the
111 /// other operand number. Otherwise, return -1.
112 int getTiedRegister() const {
113 for (unsigned j
= 0, e
= Constraints
.size(); j
!= e
; ++j
) {
114 const CGIOperandList::ConstraintInfo
&CI
= Constraints
[j
];
115 if (CI
.isTied()) return CI
.getTiedOperand();
121 CGIOperandList(Record
*D
);
123 Record
*TheDef
; // The actual record containing this OperandList.
125 /// NumDefs - Number of def operands declared, this is the number of
126 /// elements in the instruction's (outs) list.
130 /// OperandList - The list of declared operands, along with their declared
131 /// type (which is a record).
132 std::vector
<OperandInfo
> OperandList
;
134 // Information gleaned from the operand list.
139 // Provide transparent accessors to the operand list.
140 bool empty() const { return OperandList
.empty(); }
141 unsigned size() const { return OperandList
.size(); }
142 const OperandInfo
&operator[](unsigned i
) const { return OperandList
[i
]; }
143 OperandInfo
&operator[](unsigned i
) { return OperandList
[i
]; }
144 OperandInfo
&back() { return OperandList
.back(); }
145 const OperandInfo
&back() const { return OperandList
.back(); }
148 /// getOperandNamed - Return the index of the operand with the specified
149 /// non-empty name. If the instruction does not have an operand with the
150 /// specified name, throw an exception.
151 unsigned getOperandNamed(StringRef Name
) const;
153 /// hasOperandNamed - Query whether the instruction has an operand of the
154 /// given name. If so, return true and set OpIdx to the index of the
155 /// operand. Otherwise, return false.
156 bool hasOperandNamed(StringRef Name
, unsigned &OpIdx
) const;
158 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
159 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
160 /// This throws an exception if the name is invalid. If AllowWholeOp is
161 /// true, references to operands with suboperands are allowed, otherwise
163 std::pair
<unsigned,unsigned> ParseOperandName(const std::string
&Op
,
164 bool AllowWholeOp
= true);
166 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
167 /// flat machineinstr operand #.
168 unsigned getFlattenedOperandNumber(std::pair
<unsigned,unsigned> Op
) const {
169 return OperandList
[Op
.first
].MIOperandNo
+ Op
.second
;
172 /// getSubOperandNumber - Unflatten a operand number into an
173 /// operand/suboperand pair.
174 std::pair
<unsigned,unsigned> getSubOperandNumber(unsigned Op
) const {
175 for (unsigned i
= 0; ; ++i
) {
176 assert(i
< OperandList
.size() && "Invalid flat operand #");
177 if (OperandList
[i
].MIOperandNo
+OperandList
[i
].MINumOperands
> Op
)
178 return std::make_pair(i
, Op
-OperandList
[i
].MIOperandNo
);
183 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
184 /// should not be emitted with the code emitter.
185 bool isFlatOperandNotEmitted(unsigned FlatOpNo
) const {
186 std::pair
<unsigned,unsigned> Op
= getSubOperandNumber(FlatOpNo
);
187 if (OperandList
[Op
.first
].DoNotEncode
.size() > Op
.second
)
188 return OperandList
[Op
.first
].DoNotEncode
[Op
.second
];
192 void ProcessDisableEncoding(std::string Value
);
196 class CodeGenInstruction
{
198 Record
*TheDef
; // The actual record defining this instruction.
199 std::string Namespace
; // The namespace the instruction is in.
201 /// AsmString - The format string used to emit a .s file for the
203 std::string AsmString
;
205 /// Operands - This is information about the (ins) and (outs) list specified
206 /// to the instruction.
207 CGIOperandList Operands
;
209 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
210 /// implicitly defined and used by the instruction.
211 std::vector
<Record
*> ImplicitDefs
, ImplicitUses
;
213 // Various boolean values we track for the instruction.
216 bool isIndirectBranch
;
223 bool mayLoad
, mayStore
;
225 bool isConvertibleToThreeAddress
;
228 bool isReMaterializable
;
230 bool usesCustomInserter
;
232 bool isNotDuplicable
;
234 bool neverHasSideEffects
;
235 bool isAsCheapAsAMove
;
236 bool hasExtraSrcRegAllocReq
;
237 bool hasExtraDefRegAllocReq
;
242 CodeGenInstruction(Record
*R
);
244 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
245 /// implicit def and it has a known VT, return the VT, otherwise return
248 HasOneImplicitDefWithKnownVT(const CodeGenTarget
&TargetInfo
) const;
251 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
252 /// include text from the specified variant, returning the new string.
253 static std::string
FlattenAsmStringVariants(StringRef AsmString
,
258 /// CodeGenInstAlias - This represents an InstAlias definition.
259 class CodeGenInstAlias
{
261 Record
*TheDef
; // The actual record defining this InstAlias.
263 /// AsmString - The format string used to emit a .s file for the
265 std::string AsmString
;
267 /// Result - The result instruction.
270 /// ResultInst - The instruction generated by the alias (decoded from
272 CodeGenInstruction
*ResultInst
;
275 struct ResultOperand
{
288 ResultOperand(StringRef N
, Record
*r
) : Name(N
), R(r
), Kind(K_Record
) {}
289 ResultOperand(int64_t I
) : Imm(I
), Kind(K_Imm
) {}
290 ResultOperand(Record
*r
) : R(r
), Kind(K_Reg
) {}
292 bool isRecord() const { return Kind
== K_Record
; }
293 bool isImm() const { return Kind
== K_Imm
; }
294 bool isReg() const { return Kind
== K_Reg
; }
296 StringRef
getName() const { assert(isRecord()); return Name
; }
297 Record
*getRecord() const { assert(isRecord()); return R
; }
298 int64_t getImm() const { assert(isImm()); return Imm
; }
299 Record
*getRegister() const { assert(isReg()); return R
; }
302 /// ResultOperands - The decoded operands for the result instruction.
303 std::vector
<ResultOperand
> ResultOperands
;
305 /// ResultInstOperandIndex - For each operand, this vector holds a pair of
306 /// indices to identify the corresponding operand in the result
307 /// instruction. The first index specifies the operand and the second
308 /// index specifies the suboperand. If there are no suboperands or if all
309 /// of them are matched by the operand, the second value should be -1.
310 std::vector
<std::pair
<unsigned, int> > ResultInstOperandIndex
;
312 CodeGenInstAlias(Record
*R
, CodeGenTarget
&T
);
314 bool tryAliasOpMatch(DagInit
*Result
, unsigned AliasOpNo
,
315 Record
*InstOpRec
, bool hasSubOps
, SMLoc Loc
,
316 CodeGenTarget
&T
, ResultOperand
&ResOp
);