[llvm-exegesis] [NFC] Fixing typo.
[llvm-complete.git] / utils / TableGen / CodeGenInstruction.h
blob28056a98056e3c2af0a82da2d6f4fc1b6e925513
1 //===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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"
19 #include <string>
20 #include <utility>
21 #include <vector>
23 namespace llvm {
24 template <typename T> class ArrayRef;
25 class Record;
26 class DagInit;
27 class CodeGenTarget;
29 class CGIOperandList {
30 public:
31 class ConstraintInfo {
32 enum { None, EarlyClobber, Tied } Kind;
33 unsigned OtherTiedOperand;
34 public:
35 ConstraintInfo() : Kind(None) {}
37 static ConstraintInfo getEarlyClobber() {
38 ConstraintInfo I;
39 I.Kind = EarlyClobber;
40 I.OtherTiedOperand = 0;
41 return I;
44 static ConstraintInfo getTied(unsigned Op) {
45 ConstraintInfo I;
46 I.Kind = Tied;
47 I.OtherTiedOperand = Op;
48 return I;
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 {
56 assert(isTied());
57 return OtherTiedOperand;
60 bool operator==(const ConstraintInfo &RHS) const {
61 if (Kind != RHS.Kind)
62 return false;
63 if (Kind == Tied && OtherTiedOperand != RHS.OtherTiedOperand)
64 return false;
65 return true;
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.
74 struct OperandInfo {
75 /// Rec - The definition this operand is declared as.
76 ///
77 Record *Rec;
79 /// Name - If this operand was assigned a symbolic name, this is it,
80 /// otherwise, it's empty.
81 std::string Name;
83 /// PrinterMethodName - The method used to print operands of this type in
84 /// the asmprinter.
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
92 /// the operand.
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
106 /// emitter.
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();
132 return -1;
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.
143 unsigned NumDefs;
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.
150 bool isPredicable;
151 bool hasOptionalDef;
152 bool isVariadic;
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];
209 return false;
212 void ProcessDisableEncoding(std::string Value);
216 class CodeGenInstruction {
217 public:
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
222 /// instruction.
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.
234 bool isReturn : 1;
235 bool isEHScopeReturn : 1;
236 bool isBranch : 1;
237 bool isIndirectBranch : 1;
238 bool isCompare : 1;
239 bool isMoveImm : 1;
240 bool isMoveReg : 1;
241 bool isBitcast : 1;
242 bool isSelect : 1;
243 bool isBarrier : 1;
244 bool isCall : 1;
245 bool isAdd : 1;
246 bool isTrap : 1;
247 bool canFoldAsLoad : 1;
248 bool mayLoad : 1;
249 bool mayLoad_Unset : 1;
250 bool mayStore : 1;
251 bool mayStore_Unset : 1;
252 bool isPredicable : 1;
253 bool isConvertibleToThreeAddress : 1;
254 bool isCommutable : 1;
255 bool isTerminator : 1;
256 bool isReMaterializable : 1;
257 bool hasDelaySlot : 1;
258 bool usesCustomInserter : 1;
259 bool hasPostISelHook : 1;
260 bool hasCtrlDep : 1;
261 bool isNotDuplicable : 1;
262 bool hasSideEffects : 1;
263 bool hasSideEffects_Unset : 1;
264 bool isAsCheapAsAMove : 1;
265 bool hasExtraSrcRegAllocReq : 1;
266 bool hasExtraDefRegAllocReq : 1;
267 bool isCodeGenOnly : 1;
268 bool isPseudo : 1;
269 bool isRegSequence : 1;
270 bool isExtractSubreg : 1;
271 bool isInsertSubreg : 1;
272 bool isConvergent : 1;
273 bool hasNoSchedulingInfo : 1;
274 bool FastISelShouldIgnore : 1;
275 bool hasChain : 1;
276 bool hasChain_Inferred : 1;
277 bool variadicOpsAreDefs : 1;
279 std::string DeprecatedReason;
280 bool HasComplexDeprecationPredicate;
282 /// Are there any undefined flags?
283 bool hasUndefFlags() const {
284 return mayLoad_Unset || mayStore_Unset || hasSideEffects_Unset;
287 // The record used to infer instruction flags, or NULL if no flag values
288 // have been inferred.
289 Record *InferredFrom;
291 CodeGenInstruction(Record *R);
293 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
294 /// implicit def and it has a known VT, return the VT, otherwise return
295 /// MVT::Other.
296 MVT::SimpleValueType
297 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
300 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
301 /// include text from the specified variant, returning the new string.
302 static std::string FlattenAsmStringVariants(StringRef AsmString,
303 unsigned Variant);
305 // Is the specified operand in a generic instruction implicitly a pointer.
306 // This can be used on intructions that use typeN or ptypeN to identify
307 // operands that should be considered as pointers even though SelectionDAG
308 // didn't make a distinction between integer and pointers.
309 bool isOperandAPointer(unsigned i) const;
313 /// CodeGenInstAlias - This represents an InstAlias definition.
314 class CodeGenInstAlias {
315 public:
316 Record *TheDef; // The actual record defining this InstAlias.
318 /// AsmString - The format string used to emit a .s file for the
319 /// instruction.
320 std::string AsmString;
322 /// Result - The result instruction.
323 DagInit *Result;
325 /// ResultInst - The instruction generated by the alias (decoded from
326 /// Result).
327 CodeGenInstruction *ResultInst;
330 struct ResultOperand {
331 private:
332 std::string Name;
333 Record *R;
335 int64_t Imm;
336 public:
337 enum {
338 K_Record,
339 K_Imm,
340 K_Reg
341 } Kind;
343 ResultOperand(std::string N, Record *r)
344 : Name(std::move(N)), R(r), Kind(K_Record) {}
345 ResultOperand(int64_t I) : Imm(I), Kind(K_Imm) {}
346 ResultOperand(Record *r) : R(r), Kind(K_Reg) {}
348 bool isRecord() const { return Kind == K_Record; }
349 bool isImm() const { return Kind == K_Imm; }
350 bool isReg() const { return Kind == K_Reg; }
352 StringRef getName() const { assert(isRecord()); return Name; }
353 Record *getRecord() const { assert(isRecord()); return R; }
354 int64_t getImm() const { assert(isImm()); return Imm; }
355 Record *getRegister() const { assert(isReg()); return R; }
357 unsigned getMINumOperands() const;
360 /// ResultOperands - The decoded operands for the result instruction.
361 std::vector<ResultOperand> ResultOperands;
363 /// ResultInstOperandIndex - For each operand, this vector holds a pair of
364 /// indices to identify the corresponding operand in the result
365 /// instruction. The first index specifies the operand and the second
366 /// index specifies the suboperand. If there are no suboperands or if all
367 /// of them are matched by the operand, the second value should be -1.
368 std::vector<std::pair<unsigned, int> > ResultInstOperandIndex;
370 CodeGenInstAlias(Record *R, CodeGenTarget &T);
372 bool tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
373 Record *InstOpRec, bool hasSubOps, ArrayRef<SMLoc> Loc,
374 CodeGenTarget &T, ResultOperand &ResOp);
378 #endif