Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/CodeGenInstruction.h
35290 views
//===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines a wrapper class for the 'Instruction' TableGen class.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_UTILS_TABLEGEN_CODEGENINSTRUCTION_H13#define LLVM_UTILS_TABLEGEN_CODEGENINSTRUCTION_H1415#include "llvm/ADT/BitVector.h"16#include "llvm/ADT/StringMap.h"17#include "llvm/ADT/StringRef.h"18#include "llvm/CodeGenTypes/MachineValueType.h"19#include "llvm/TableGen/Record.h"20#include <cassert>21#include <string>22#include <utility>23#include <vector>2425namespace llvm {26class CodeGenTarget;2728class CGIOperandList {29public:30class ConstraintInfo {31enum { None, EarlyClobber, Tied } Kind = None;32unsigned OtherTiedOperand = 0;3334public:35ConstraintInfo() = default;3637static ConstraintInfo getEarlyClobber() {38ConstraintInfo I;39I.Kind = EarlyClobber;40I.OtherTiedOperand = 0;41return I;42}4344static ConstraintInfo getTied(unsigned Op) {45ConstraintInfo I;46I.Kind = Tied;47I.OtherTiedOperand = Op;48return I;49}5051bool isNone() const { return Kind == None; }52bool isEarlyClobber() const { return Kind == EarlyClobber; }53bool isTied() const { return Kind == Tied; }5455unsigned getTiedOperand() const {56assert(isTied());57return OtherTiedOperand;58}5960bool operator==(const ConstraintInfo &RHS) const {61if (Kind != RHS.Kind)62return false;63if (Kind == Tied && OtherTiedOperand != RHS.OtherTiedOperand)64return false;65return true;66}67bool operator!=(const ConstraintInfo &RHS) const { return !(*this == RHS); }68};6970/// OperandInfo - The information we keep track of for each operand in the71/// operand list for a tablegen instruction.72struct OperandInfo {73/// Rec - The definition this operand is declared as.74///75Record *Rec;7677/// Name - If this operand was assigned a symbolic name, this is it,78/// otherwise, it's empty.79std::string Name;8081/// The names of sub-operands, if given, otherwise empty.82std::vector<std::string> SubOpNames;8384/// PrinterMethodName - The method used to print operands of this type in85/// the asmprinter.86std::string PrinterMethodName;8788/// The method used to get the machine operand value for binary89/// encoding, per sub-operand. If empty, uses "getMachineOpValue".90std::vector<std::string> EncoderMethodNames;9192/// OperandType - A value from MCOI::OperandType representing the type of93/// the operand.94std::string OperandType;9596/// MIOperandNo - Currently (this is meant to be phased out), some logical97/// operands correspond to multiple MachineInstr operands. In the X8698/// target for example, one address operand is represented as 499/// MachineOperands. Because of this, the operand number in the100/// OperandList may not match the MachineInstr operand num. Until it101/// does, this contains the MI operand index of this operand.102unsigned MIOperandNo;103unsigned MINumOperands; // The number of operands.104105/// DoNotEncode - Bools are set to true in this vector for each operand in106/// the DisableEncoding list. These should not be emitted by the code107/// emitter.108BitVector DoNotEncode;109110/// MIOperandInfo - Default MI operand type. Note an operand may be made111/// up of multiple MI operands.112DagInit *MIOperandInfo;113114/// Constraint info for this operand. This operand can have pieces, so we115/// track constraint info for each.116std::vector<ConstraintInfo> Constraints;117118OperandInfo(Record *R, const std::string &N, const std::string &PMN,119const std::string &OT, unsigned MION, unsigned MINO,120DagInit *MIOI)121: Rec(R), Name(N), SubOpNames(MINO), PrinterMethodName(PMN),122EncoderMethodNames(MINO), OperandType(OT), MIOperandNo(MION),123MINumOperands(MINO), DoNotEncode(MINO), MIOperandInfo(MIOI),124Constraints(MINO) {}125126/// getTiedOperand - If this operand is tied to another one, return the127/// other operand number. Otherwise, return -1.128int getTiedRegister() const {129for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {130const CGIOperandList::ConstraintInfo &CI = Constraints[j];131if (CI.isTied())132return CI.getTiedOperand();133}134return -1;135}136};137138CGIOperandList(Record *D);139140Record *TheDef; // The actual record containing this OperandList.141142/// NumDefs - Number of def operands declared, this is the number of143/// elements in the instruction's (outs) list.144///145unsigned NumDefs;146147/// OperandList - The list of declared operands, along with their declared148/// type (which is a record).149std::vector<OperandInfo> OperandList;150151/// SubOpAliases - List of alias names for suboperands.152StringMap<std::pair<unsigned, unsigned>> SubOpAliases;153154// Information gleaned from the operand list.155bool isPredicable;156bool hasOptionalDef;157bool isVariadic;158159// Provide transparent accessors to the operand list.160bool empty() const { return OperandList.empty(); }161unsigned size() const { return OperandList.size(); }162const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }163OperandInfo &operator[](unsigned i) { return OperandList[i]; }164OperandInfo &back() { return OperandList.back(); }165const OperandInfo &back() const { return OperandList.back(); }166167typedef std::vector<OperandInfo>::iterator iterator;168typedef std::vector<OperandInfo>::const_iterator const_iterator;169iterator begin() { return OperandList.begin(); }170const_iterator begin() const { return OperandList.begin(); }171iterator end() { return OperandList.end(); }172const_iterator end() const { return OperandList.end(); }173174/// getOperandNamed - Return the index of the operand with the specified175/// non-empty name. If the instruction does not have an operand with the176/// specified name, abort.177unsigned getOperandNamed(StringRef Name) const;178179/// hasOperandNamed - Query whether the instruction has an operand of the180/// given name. If so, return true and set OpIdx to the index of the181/// operand. Otherwise, return false.182bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;183184bool hasSubOperandAlias(StringRef Name,185std::pair<unsigned, unsigned> &SubOp) const;186187/// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",188/// where $foo is a whole operand and $foo.bar refers to a suboperand.189/// This aborts if the name is invalid. If AllowWholeOp is true, references190/// to operands with suboperands are allowed, otherwise not.191std::pair<unsigned, unsigned> ParseOperandName(StringRef Op,192bool AllowWholeOp = true);193194/// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a195/// flat machineinstr operand #.196unsigned getFlattenedOperandNumber(std::pair<unsigned, unsigned> Op) const {197return OperandList[Op.first].MIOperandNo + Op.second;198}199200/// getSubOperandNumber - Unflatten a operand number into an201/// operand/suboperand pair.202std::pair<unsigned, unsigned> getSubOperandNumber(unsigned Op) const {203for (unsigned i = 0;; ++i) {204assert(i < OperandList.size() && "Invalid flat operand #");205if (OperandList[i].MIOperandNo + OperandList[i].MINumOperands > Op)206return std::pair(i, Op - OperandList[i].MIOperandNo);207}208}209210/// isFlatOperandNotEmitted - Return true if the specified flat operand #211/// should not be emitted with the code emitter.212bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {213std::pair<unsigned, unsigned> Op = getSubOperandNumber(FlatOpNo);214if (OperandList[Op.first].DoNotEncode.size() > Op.second)215return OperandList[Op.first].DoNotEncode[Op.second];216return false;217}218219void ProcessDisableEncoding(StringRef Value);220};221222class CodeGenInstruction {223public:224Record *TheDef; // The actual record defining this instruction.225StringRef Namespace; // The namespace the instruction is in.226227/// AsmString - The format string used to emit a .s file for the228/// instruction.229std::string AsmString;230231/// Operands - This is information about the (ins) and (outs) list specified232/// to the instruction.233CGIOperandList Operands;234235/// ImplicitDefs/ImplicitUses - These are lists of registers that are236/// implicitly defined and used by the instruction.237std::vector<Record *> ImplicitDefs, ImplicitUses;238239// Various boolean values we track for the instruction.240bool isPreISelOpcode : 1;241bool isReturn : 1;242bool isEHScopeReturn : 1;243bool isBranch : 1;244bool isIndirectBranch : 1;245bool isCompare : 1;246bool isMoveImm : 1;247bool isMoveReg : 1;248bool isBitcast : 1;249bool isSelect : 1;250bool isBarrier : 1;251bool isCall : 1;252bool isAdd : 1;253bool isTrap : 1;254bool canFoldAsLoad : 1;255bool mayLoad : 1;256bool mayLoad_Unset : 1;257bool mayStore : 1;258bool mayStore_Unset : 1;259bool mayRaiseFPException : 1;260bool isPredicable : 1;261bool isConvertibleToThreeAddress : 1;262bool isCommutable : 1;263bool isTerminator : 1;264bool isReMaterializable : 1;265bool hasDelaySlot : 1;266bool usesCustomInserter : 1;267bool hasPostISelHook : 1;268bool hasCtrlDep : 1;269bool isNotDuplicable : 1;270bool hasSideEffects : 1;271bool hasSideEffects_Unset : 1;272bool isAsCheapAsAMove : 1;273bool hasExtraSrcRegAllocReq : 1;274bool hasExtraDefRegAllocReq : 1;275bool isCodeGenOnly : 1;276bool isPseudo : 1;277bool isMeta : 1;278bool isRegSequence : 1;279bool isExtractSubreg : 1;280bool isInsertSubreg : 1;281bool isConvergent : 1;282bool hasNoSchedulingInfo : 1;283bool FastISelShouldIgnore : 1;284bool hasChain : 1;285bool hasChain_Inferred : 1;286bool variadicOpsAreDefs : 1;287bool isAuthenticated : 1;288289std::string DeprecatedReason;290bool HasComplexDeprecationPredicate;291292/// Are there any undefined flags?293bool hasUndefFlags() const {294return mayLoad_Unset || mayStore_Unset || hasSideEffects_Unset;295}296297// The record used to infer instruction flags, or NULL if no flag values298// have been inferred.299Record *InferredFrom;300301// The enum value assigned by CodeGenTarget::computeInstrsByEnum.302mutable unsigned EnumVal = 0;303304CodeGenInstruction(Record *R);305306/// HasOneImplicitDefWithKnownVT - If the instruction has at least one307/// implicit def and it has a known VT, return the VT, otherwise return308/// MVT::Other.309MVT::SimpleValueType310HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;311312/// FlattenAsmStringVariants - Flatten the specified AsmString to only313/// include text from the specified variant, returning the new string.314static std::string FlattenAsmStringVariants(StringRef AsmString,315unsigned Variant);316317// Is the specified operand in a generic instruction implicitly a pointer.318// This can be used on intructions that use typeN or ptypeN to identify319// operands that should be considered as pointers even though SelectionDAG320// didn't make a distinction between integer and pointers.321bool isInOperandAPointer(unsigned i) const {322return isOperandImpl("InOperandList", i, "IsPointer");323}324325bool isOutOperandAPointer(unsigned i) const {326return isOperandImpl("OutOperandList", i, "IsPointer");327}328329/// Check if the operand is required to be an immediate.330bool isInOperandImmArg(unsigned i) const {331return isOperandImpl("InOperandList", i, "IsImmediate");332}333334/// Return true if the instruction uses a variable length encoding.335bool isVariableLengthEncoding() const {336const RecordVal *RV = TheDef->getValue("Inst");337return RV && isa<DagInit>(RV->getValue());338}339340private:341bool isOperandImpl(StringRef OpListName, unsigned i,342StringRef PropertyName) const;343};344} // namespace llvm345346#endif347348349