1 //===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines a wrapper class for the 'Instruction' TableGen class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_UTILS_TABLEGEN_CODEGENINSTRUCTION_H
14 #define LLVM_UTILS_TABLEGEN_CODEGENINSTRUCTION_H
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/MachineValueType.h"
18 #include "llvm/Support/SMLoc.h"
24 template <typename T
> class ArrayRef
;
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
;
60 bool operator==(const ConstraintInfo
&RHS
) const {
63 if (Kind
== Tied
&& OtherTiedOperand
!= RHS
.OtherTiedOperand
)
67 bool operator!=(const ConstraintInfo
&RHS
) const {
68 return !(*this == RHS
);
72 /// OperandInfo - The information we keep track of for each operand in the
73 /// operand list for a tablegen instruction.
75 /// Rec - The definition this operand is declared as.
79 /// Name - If this operand was assigned a symbolic name, this is it,
80 /// otherwise, it's empty.
83 /// PrinterMethodName - The method used to print operands of this type in
85 std::string PrinterMethodName
;
87 /// EncoderMethodName - The method used to get the machine operand value
88 /// for binary encoding. "getMachineOpValue" by default.
89 std::string EncoderMethodName
;
91 /// OperandType - A value from MCOI::OperandType representing the type of
93 std::string OperandType
;
95 /// MIOperandNo - Currently (this is meant to be phased out), some logical
96 /// operands correspond to multiple MachineInstr operands. In the X86
97 /// target for example, one address operand is represented as 4
98 /// MachineOperands. Because of this, the operand number in the
99 /// OperandList may not match the MachineInstr operand num. Until it
100 /// does, this contains the MI operand index of this operand.
101 unsigned MIOperandNo
;
102 unsigned MINumOperands
; // The number of operands.
104 /// DoNotEncode - Bools are set to true in this vector for each operand in
105 /// the DisableEncoding list. These should not be emitted by the code
107 std::vector
<bool> DoNotEncode
;
109 /// MIOperandInfo - Default MI operand type. Note an operand may be made
110 /// up of multiple MI operands.
111 DagInit
*MIOperandInfo
;
113 /// Constraint info for this operand. This operand can have pieces, so we
114 /// track constraint info for each.
115 std::vector
<ConstraintInfo
> Constraints
;
117 OperandInfo(Record
*R
, const std::string
&N
, const std::string
&PMN
,
118 const std::string
&EMN
, const std::string
&OT
, unsigned MION
,
119 unsigned MINO
, DagInit
*MIOI
)
120 : Rec(R
), Name(N
), PrinterMethodName(PMN
), EncoderMethodName(EMN
),
121 OperandType(OT
), MIOperandNo(MION
), MINumOperands(MINO
),
122 MIOperandInfo(MIOI
) {}
125 /// getTiedOperand - If this operand is tied to another one, return the
126 /// other operand number. Otherwise, return -1.
127 int getTiedRegister() const {
128 for (unsigned j
= 0, e
= Constraints
.size(); j
!= e
; ++j
) {
129 const CGIOperandList::ConstraintInfo
&CI
= Constraints
[j
];
130 if (CI
.isTied()) return CI
.getTiedOperand();
136 CGIOperandList(Record
*D
);
138 Record
*TheDef
; // The actual record containing this OperandList.
140 /// NumDefs - Number of def operands declared, this is the number of
141 /// elements in the instruction's (outs) list.
145 /// OperandList - The list of declared operands, along with their declared
146 /// type (which is a record).
147 std::vector
<OperandInfo
> OperandList
;
149 // Information gleaned from the operand list.
154 // Provide transparent accessors to the operand list.
155 bool empty() const { return OperandList
.empty(); }
156 unsigned size() const { return OperandList
.size(); }
157 const OperandInfo
&operator[](unsigned i
) const { return OperandList
[i
]; }
158 OperandInfo
&operator[](unsigned i
) { return OperandList
[i
]; }
159 OperandInfo
&back() { return OperandList
.back(); }
160 const OperandInfo
&back() const { return OperandList
.back(); }
162 typedef std::vector
<OperandInfo
>::iterator iterator
;
163 typedef std::vector
<OperandInfo
>::const_iterator const_iterator
;
164 iterator
begin() { return OperandList
.begin(); }
165 const_iterator
begin() const { return OperandList
.begin(); }
166 iterator
end() { return OperandList
.end(); }
167 const_iterator
end() const { return OperandList
.end(); }
169 /// getOperandNamed - Return the index of the operand with the specified
170 /// non-empty name. If the instruction does not have an operand with the
171 /// specified name, abort.
172 unsigned getOperandNamed(StringRef Name
) const;
174 /// hasOperandNamed - Query whether the instruction has an operand of the
175 /// given name. If so, return true and set OpIdx to the index of the
176 /// operand. Otherwise, return false.
177 bool hasOperandNamed(StringRef Name
, unsigned &OpIdx
) const;
179 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
180 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
181 /// This aborts if the name is invalid. If AllowWholeOp is true, references
182 /// to operands with suboperands are allowed, otherwise not.
183 std::pair
<unsigned,unsigned> ParseOperandName(const std::string
&Op
,
184 bool AllowWholeOp
= true);
186 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
187 /// flat machineinstr operand #.
188 unsigned getFlattenedOperandNumber(std::pair
<unsigned,unsigned> Op
) const {
189 return OperandList
[Op
.first
].MIOperandNo
+ Op
.second
;
192 /// getSubOperandNumber - Unflatten a operand number into an
193 /// operand/suboperand pair.
194 std::pair
<unsigned,unsigned> getSubOperandNumber(unsigned Op
) const {
195 for (unsigned i
= 0; ; ++i
) {
196 assert(i
< OperandList
.size() && "Invalid flat operand #");
197 if (OperandList
[i
].MIOperandNo
+OperandList
[i
].MINumOperands
> Op
)
198 return std::make_pair(i
, Op
-OperandList
[i
].MIOperandNo
);
203 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
204 /// should not be emitted with the code emitter.
205 bool isFlatOperandNotEmitted(unsigned FlatOpNo
) const {
206 std::pair
<unsigned,unsigned> Op
= getSubOperandNumber(FlatOpNo
);
207 if (OperandList
[Op
.first
].DoNotEncode
.size() > Op
.second
)
208 return OperandList
[Op
.first
].DoNotEncode
[Op
.second
];
212 void ProcessDisableEncoding(std::string Value
);
216 class CodeGenInstruction
{
218 Record
*TheDef
; // The actual record defining this instruction.
219 StringRef Namespace
; // The namespace the instruction is in.
221 /// AsmString - The format string used to emit a .s file for the
223 std::string AsmString
;
225 /// Operands - This is information about the (ins) and (outs) list specified
226 /// to the instruction.
227 CGIOperandList Operands
;
229 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
230 /// implicitly defined and used by the instruction.
231 std::vector
<Record
*> ImplicitDefs
, ImplicitUses
;
233 // Various boolean values we track for the instruction.
235 bool isEHScopeReturn
: 1;
237 bool isIndirectBranch
: 1;
247 bool canFoldAsLoad
: 1;
249 bool mayLoad_Unset
: 1;
251 bool mayStore_Unset
: 1;
252 bool mayRaiseFPException
: 1;
253 bool isPredicable
: 1;
254 bool isConvertibleToThreeAddress
: 1;
255 bool isCommutable
: 1;
256 bool isTerminator
: 1;
257 bool isReMaterializable
: 1;
258 bool hasDelaySlot
: 1;
259 bool usesCustomInserter
: 1;
260 bool hasPostISelHook
: 1;
262 bool isNotDuplicable
: 1;
263 bool hasSideEffects
: 1;
264 bool hasSideEffects_Unset
: 1;
265 bool isAsCheapAsAMove
: 1;
266 bool hasExtraSrcRegAllocReq
: 1;
267 bool hasExtraDefRegAllocReq
: 1;
268 bool isCodeGenOnly
: 1;
270 bool isRegSequence
: 1;
271 bool isExtractSubreg
: 1;
272 bool isInsertSubreg
: 1;
273 bool isConvergent
: 1;
274 bool hasNoSchedulingInfo
: 1;
275 bool FastISelShouldIgnore
: 1;
277 bool hasChain_Inferred
: 1;
278 bool variadicOpsAreDefs
: 1;
280 std::string DeprecatedReason
;
281 bool HasComplexDeprecationPredicate
;
283 /// Are there any undefined flags?
284 bool hasUndefFlags() const {
285 return mayLoad_Unset
|| mayStore_Unset
|| hasSideEffects_Unset
;
288 // The record used to infer instruction flags, or NULL if no flag values
289 // have been inferred.
290 Record
*InferredFrom
;
292 CodeGenInstruction(Record
*R
);
294 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
295 /// implicit def and it has a known VT, return the VT, otherwise return
298 HasOneImplicitDefWithKnownVT(const CodeGenTarget
&TargetInfo
) const;
301 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
302 /// include text from the specified variant, returning the new string.
303 static std::string
FlattenAsmStringVariants(StringRef AsmString
,
306 // Is the specified operand in a generic instruction implicitly a pointer.
307 // This can be used on intructions that use typeN or ptypeN to identify
308 // operands that should be considered as pointers even though SelectionDAG
309 // didn't make a distinction between integer and pointers.
310 bool isOperandAPointer(unsigned i
) const;
314 /// CodeGenInstAlias - This represents an InstAlias definition.
315 class CodeGenInstAlias
{
317 Record
*TheDef
; // The actual record defining this InstAlias.
319 /// AsmString - The format string used to emit a .s file for the
321 std::string AsmString
;
323 /// Result - The result instruction.
326 /// ResultInst - The instruction generated by the alias (decoded from
328 CodeGenInstruction
*ResultInst
;
331 struct ResultOperand
{
344 ResultOperand(std::string N
, Record
*r
)
345 : Name(std::move(N
)), R(r
), Kind(K_Record
) {}
346 ResultOperand(int64_t I
) : Imm(I
), Kind(K_Imm
) {}
347 ResultOperand(Record
*r
) : R(r
), Kind(K_Reg
) {}
349 bool isRecord() const { return Kind
== K_Record
; }
350 bool isImm() const { return Kind
== K_Imm
; }
351 bool isReg() const { return Kind
== K_Reg
; }
353 StringRef
getName() const { assert(isRecord()); return Name
; }
354 Record
*getRecord() const { assert(isRecord()); return R
; }
355 int64_t getImm() const { assert(isImm()); return Imm
; }
356 Record
*getRegister() const { assert(isReg()); return R
; }
358 unsigned getMINumOperands() const;
361 /// ResultOperands - The decoded operands for the result instruction.
362 std::vector
<ResultOperand
> ResultOperands
;
364 /// ResultInstOperandIndex - For each operand, this vector holds a pair of
365 /// indices to identify the corresponding operand in the result
366 /// instruction. The first index specifies the operand and the second
367 /// index specifies the suboperand. If there are no suboperands or if all
368 /// of them are matched by the operand, the second value should be -1.
369 std::vector
<std::pair
<unsigned, int> > ResultInstOperandIndex
;
371 CodeGenInstAlias(Record
*R
, CodeGenTarget
&T
);
373 bool tryAliasOpMatch(DagInit
*Result
, unsigned AliasOpNo
,
374 Record
*InstOpRec
, bool hasSubOps
, ArrayRef
<SMLoc
> Loc
,
375 CodeGenTarget
&T
, ResultOperand
&ResOp
);