Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/CodeGenTarget.h
35290 views
//===- CodeGenTarget.h - Target 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 wrappers for the Target class and related global9// functionality. This makes it easier to access the data and provides a single10// place that needs to check it for validity. All of these classes abort11// on error conditions.12//13//===----------------------------------------------------------------------===//1415#ifndef LLVM_UTILS_TABLEGEN_CODEGENTARGET_H16#define LLVM_UTILS_TABLEGEN_CODEGENTARGET_H1718#include "Basic/SDNodeProperties.h"19#include "CodeGenHwModes.h"20#include "CodeGenInstruction.h"21#include "InfoByHwMode.h"22#include "llvm/ADT/ArrayRef.h"23#include "llvm/ADT/DenseMap.h"24#include "llvm/ADT/SmallVector.h"25#include "llvm/ADT/StringRef.h"26#include "llvm/CodeGenTypes/MachineValueType.h"27#include <cassert>28#include <memory>29#include <optional>30#include <string>31#include <vector>3233namespace llvm {3435class RecordKeeper;36class Record;37class CodeGenRegBank;38class CodeGenRegister;39class CodeGenRegisterClass;40class CodeGenSchedModels;41class CodeGenSubRegIndex;4243/// getValueType - Return the MVT::SimpleValueType that the specified TableGen44/// record corresponds to.45MVT::SimpleValueType getValueType(const Record *Rec);4647StringRef getName(MVT::SimpleValueType T);48StringRef getEnumName(MVT::SimpleValueType T);4950/// getQualifiedName - Return the name of the specified record, with a51/// namespace qualifier if the record contains one.52std::string getQualifiedName(const Record *R);5354/// CodeGenTarget - This class corresponds to the Target class in the .td files.55///56class CodeGenTarget {57RecordKeeper &Records;58Record *TargetRec;5960mutable DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>>61Instructions;62mutable std::unique_ptr<CodeGenRegBank> RegBank;63mutable std::vector<Record *> RegAltNameIndices;64mutable SmallVector<ValueTypeByHwMode, 8> LegalValueTypes;65CodeGenHwModes CGH;66std::vector<Record *> MacroFusions;67mutable bool HasVariableLengthEncodings = false;6869void ReadRegAltNameIndices() const;70void ReadInstructions() const;71void ReadLegalValueTypes() const;7273mutable std::unique_ptr<CodeGenSchedModels> SchedModels;7475mutable StringRef InstNamespace;76mutable std::vector<const CodeGenInstruction *> InstrsByEnum;77mutable unsigned NumPseudoInstructions = 0;7879public:80CodeGenTarget(RecordKeeper &Records);81~CodeGenTarget();8283Record *getTargetRecord() const { return TargetRec; }84StringRef getName() const;8586/// getInstNamespace - Return the target-specific instruction namespace.87///88StringRef getInstNamespace() const;8990/// getRegNamespace - Return the target-specific register namespace.91StringRef getRegNamespace() const;9293/// getInstructionSet - Return the InstructionSet object.94///95Record *getInstructionSet() const;9697/// getAllowRegisterRenaming - Return the AllowRegisterRenaming flag value for98/// this target.99///100bool getAllowRegisterRenaming() const;101102/// getAsmParser - Return the AssemblyParser definition for this target.103///104Record *getAsmParser() const;105106/// getAsmParserVariant - Return the AssemblyParserVariant definition for107/// this target.108///109Record *getAsmParserVariant(unsigned i) const;110111/// getAsmParserVariantCount - Return the AssemblyParserVariant definition112/// available for this target.113///114unsigned getAsmParserVariantCount() const;115116/// getAsmWriter - Return the AssemblyWriter definition for this target.117///118Record *getAsmWriter() const;119120/// getRegBank - Return the register bank description.121CodeGenRegBank &getRegBank() const;122123/// Return the largest register class on \p RegBank which supports \p Ty and124/// covers \p SubIdx if it exists.125std::optional<CodeGenRegisterClass *>126getSuperRegForSubReg(const ValueTypeByHwMode &Ty, CodeGenRegBank &RegBank,127const CodeGenSubRegIndex *SubIdx,128bool MustBeAllocatable = false) const;129130/// getRegisterByName - If there is a register with the specific AsmName,131/// return it.132const CodeGenRegister *getRegisterByName(StringRef Name) const;133134const std::vector<Record *> &getRegAltNameIndices() const {135if (RegAltNameIndices.empty())136ReadRegAltNameIndices();137return RegAltNameIndices;138}139140const CodeGenRegisterClass &getRegisterClass(Record *R) const;141142/// getRegisterVTs - Find the union of all possible SimpleValueTypes for the143/// specified physical register.144std::vector<ValueTypeByHwMode> getRegisterVTs(Record *R) const;145146ArrayRef<ValueTypeByHwMode> getLegalValueTypes() const {147if (LegalValueTypes.empty())148ReadLegalValueTypes();149return LegalValueTypes;150}151152CodeGenSchedModels &getSchedModels() const;153154const CodeGenHwModes &getHwModes() const { return CGH; }155156bool hasMacroFusion() const { return !MacroFusions.empty(); }157158const std::vector<Record *> getMacroFusions() const { return MacroFusions; }159160private:161DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>> &162getInstructions() const {163if (Instructions.empty())164ReadInstructions();165return Instructions;166}167168public:169CodeGenInstruction &getInstruction(const Record *InstRec) const {170if (Instructions.empty())171ReadInstructions();172auto I = Instructions.find(InstRec);173assert(I != Instructions.end() && "Not an instruction");174return *I->second;175}176177/// Returns the number of predefined instructions.178static unsigned getNumFixedInstructions();179180/// Returns the number of pseudo instructions.181unsigned getNumPseudoInstructions() const {182if (InstrsByEnum.empty())183ComputeInstrsByEnum();184return NumPseudoInstructions;185}186187/// Return all of the instructions defined by the target, ordered by their188/// enum value.189/// The following order of instructions is also guaranteed:190/// - fixed / generic instructions as declared in TargetOpcodes.def, in order;191/// - pseudo instructions in lexicographical order sorted by name;192/// - other instructions in lexicographical order sorted by name.193ArrayRef<const CodeGenInstruction *> getInstructionsByEnumValue() const {194if (InstrsByEnum.empty())195ComputeInstrsByEnum();196return InstrsByEnum;197}198199/// Return the integer enum value corresponding to this instruction record.200unsigned getInstrIntValue(const Record *R) const {201if (InstrsByEnum.empty())202ComputeInstrsByEnum();203return getInstruction(R).EnumVal;204}205206typedef ArrayRef<const CodeGenInstruction *>::const_iterator inst_iterator;207inst_iterator inst_begin() const {208return getInstructionsByEnumValue().begin();209}210inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }211212/// Return whether instructions have variable length encodings on this target.213bool hasVariableLengthEncodings() const { return HasVariableLengthEncodings; }214215/// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?216///217bool isLittleEndianEncoding() const;218219/// reverseBitsForLittleEndianEncoding - For little-endian instruction bit220/// encodings, reverse the bit order of all instructions.221void reverseBitsForLittleEndianEncoding();222223/// guessInstructionProperties - should we just guess unset instruction224/// properties?225bool guessInstructionProperties() const;226227private:228void ComputeInstrsByEnum() const;229};230231/// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern232/// tablegen class in TargetSelectionDAG.td233class ComplexPattern {234Record *Ty;235unsigned NumOperands;236std::string SelectFunc;237std::vector<Record *> RootNodes;238unsigned Properties; // Node properties239unsigned Complexity;240241public:242ComplexPattern(Record *R);243244Record *getValueType() const { return Ty; }245unsigned getNumOperands() const { return NumOperands; }246const std::string &getSelectFunc() const { return SelectFunc; }247const std::vector<Record *> &getRootNodes() const { return RootNodes; }248bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }249unsigned getComplexity() const { return Complexity; }250};251252} // namespace llvm253254#endif255256257