LLVM  14.0.0git
MIParser.h
Go to the documentation of this file.
1 //===- MIParser.h - Machine Instructions Parser -----------------*- 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 declares the function that parses the machine instructions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CODEGEN_MIRPARSER_MIPARSER_H
14 #define LLVM_CODEGEN_MIRPARSER_MIPARSER_H
15 
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/StringMap.h"
19 #include "llvm/CodeGen/Register.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Support/SMLoc.h"
22 #include <utility>
23 
24 namespace llvm {
25 
26 class MachineBasicBlock;
27 class MachineFunction;
28 class MDNode;
29 class RegisterBank;
30 struct SlotMapping;
31 class SMDiagnostic;
32 class SourceMgr;
33 class StringRef;
34 class TargetRegisterClass;
35 class TargetSubtargetInfo;
36 
37 struct VRegInfo {
38  enum uint8_t {
40  } Kind = UNKNOWN;
41  bool Explicit = false; ///< VReg was explicitly specified in the .mir file.
42  union {
45  } D;
48 };
49 
52 
54 private:
55  const TargetSubtargetInfo &Subtarget;
56 
57  /// Maps from instruction names to op codes.
58  StringMap<unsigned> Names2InstrOpCodes;
59 
60  /// Maps from register names to registers.
61  StringMap<Register> Names2Regs;
62 
63  /// Maps from register mask names to register masks.
64  StringMap<const uint32_t *> Names2RegMasks;
65 
66  /// Maps from subregister names to subregister indices.
67  StringMap<unsigned> Names2SubRegIndices;
68 
69  /// Maps from target index names to target indices.
70  StringMap<int> Names2TargetIndices;
71 
72  /// Maps from direct target flag names to the direct target flag values.
73  StringMap<unsigned> Names2DirectTargetFlags;
74 
75  /// Maps from direct target flag names to the bitmask target flag values.
76  StringMap<unsigned> Names2BitmaskTargetFlags;
77 
78  /// Maps from MMO target flag names to MMO target flag values.
79  StringMap<MachineMemOperand::Flags> Names2MMOTargetFlags;
80 
81  /// Maps from register class names to register classes.
82  Name2RegClassMap Names2RegClasses;
83 
84  /// Maps from register bank names to register banks.
85  Name2RegBankMap Names2RegBanks;
86 
87  void initNames2InstrOpCodes();
88  void initNames2Regs();
89  void initNames2RegMasks();
90  void initNames2SubRegIndices();
91  void initNames2TargetIndices();
92  void initNames2DirectTargetFlags();
93  void initNames2BitmaskTargetFlags();
94  void initNames2MMOTargetFlags();
95 
96  void initNames2RegClasses();
97  void initNames2RegBanks();
98 
99 public:
100  /// Try to convert an instruction name to an opcode. Return true if the
101  /// instruction name is invalid.
102  bool parseInstrName(StringRef InstrName, unsigned &OpCode);
103 
104  /// Try to convert a register name to a register number. Return true if the
105  /// register name is invalid.
107 
108  /// Check if the given identifier is a name of a register mask.
109  ///
110  /// Return null if the identifier isn't a register mask.
111  const uint32_t *getRegMask(StringRef Identifier);
112 
113  /// Check if the given identifier is a name of a subregister index.
114  ///
115  /// Return 0 if the name isn't a subregister index class.
116  unsigned getSubRegIndex(StringRef Name);
117 
118  /// Try to convert a name of target index to the corresponding target index.
119  ///
120  /// Return true if the name isn't a name of a target index.
121  bool getTargetIndex(StringRef Name, int &Index);
122 
123  /// Try to convert a name of a direct target flag to the corresponding
124  /// target flag.
125  ///
126  /// Return true if the name isn't a name of a direct flag.
127  bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
128 
129  /// Try to convert a name of a bitmask target flag to the corresponding
130  /// target flag.
131  ///
132  /// Return true if the name isn't a name of a bitmask target flag.
133  bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
134 
135  /// Try to convert a name of a MachineMemOperand target flag to the
136  /// corresponding target flag.
137  ///
138  /// Return true if the name isn't a name of a target MMO flag.
140 
141  /// Check if the given identifier is a name of a register class.
142  ///
143  /// Return null if the name isn't a register class.
145 
146  /// Check if the given identifier is a name of a register bank.
147  ///
148  /// Return null if the name isn't a register bank.
150 
152  : Subtarget(STI) {
153  initNames2RegClasses();
154  initNames2RegBanks();
155  }
156 
157  ~PerTargetMIParsingState() = default;
158 
159  void setTarget(const TargetSubtargetInfo &NewSubtarget);
160 };
161 
168 
169  std::map<unsigned, TrackingMDNodeRef> MachineMetadataNodes;
170  std::map<unsigned, std::pair<TempMDTuple, SMLoc>> MachineForwardRefMDNodes;
171 
179 
180  /// Maps from slot numbers to function's unnamed values.
182 
184  const SlotMapping &IRSlots,
186 
189  const Value *getIRValue(unsigned Slot);
190 };
191 
192 /// Parse the machine basic block definitions, and skip the machine
193 /// instructions.
194 ///
195 /// This function runs the first parsing pass on the machine function's body.
196 /// It parses only the machine basic block definitions and creates the machine
197 /// basic blocks in the given machine function.
198 ///
199 /// The machine instructions aren't parsed during the first pass because all
200 /// the machine basic blocks aren't defined yet - this makes it impossible to
201 /// resolve the machine basic block references.
202 ///
203 /// Return true if an error occurred.
206 
207 /// Parse the machine instructions.
208 ///
209 /// This function runs the second parsing pass on the machine function's body.
210 /// It skips the machine basic block definitions and parses only the machine
211 /// instructions and basic block attributes like liveins and successors.
212 ///
213 /// The second parsing pass assumes that the first parsing pass already ran
214 /// on the given source string.
215 ///
216 /// Return true if an error occurred.
219 
223 
225  Register &Reg, StringRef Src,
227 
230 
232  VRegInfo *&Info, StringRef Src,
234 
237 
240 
242  SMRange SourceRange, SMDiagnostic &Error);
243 
244 } // end namespace llvm
245 
246 #endif // LLVM_CODEGEN_MIRPARSER_MIPARSER_H
llvm::PerFunctionMIParsingState::SM
SourceMgr * SM
Definition: MIParser.h:165
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::VRegInfo::RegBank
const RegisterBank * RegBank
Definition: MIParser.h:44
llvm::PerTargetMIParsingState::getRegBank
const RegisterBank * getRegBank(StringRef Name)
Check if the given identifier is a name of a register bank.
Definition: MIParser.cpp:312
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::PerTargetMIParsingState
Definition: MIParser.h:53
llvm::PerFunctionMIParsingState::StackObjectSlots
DenseMap< unsigned, int > StackObjectSlots
Definition: MIParser.h:176
llvm::VRegInfo::Kind
enum llvm::VRegInfo::uint8_t Kind
llvm::VRegInfo::uint8_t
uint8_t
Definition: MIParser.h:38
llvm::PerFunctionMIParsingState::PerFunctionMIParsingState
PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &Target)
Definition: MIParser.cpp:319
Allocator.h
llvm::PerFunctionMIParsingState::FixedStackObjectSlots
DenseMap< unsigned, int > FixedStackObjectSlots
Definition: MIParser.h:175
DenseMap.h
llvm::parseMachineInstructions
bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine instructions.
Definition: MIParser.cpp:3409
llvm::VRegInfo::REGBANK
@ REGBANK
Definition: MIParser.h:39
llvm::PerFunctionMIParsingState::VRegInfosNamed
StringMap< VRegInfo * > VRegInfosNamed
Definition: MIParser.h:174
llvm::PerFunctionMIParsingState::getVRegInfoNamed
VRegInfo & getVRegInfoNamed(StringRef RegName)
Definition: MIParser.cpp:335
llvm::PerTargetMIParsingState::getMMOTargetFlag
bool getMMOTargetFlag(StringRef Name, MachineMemOperand::Flags &Flag)
Try to convert a name of a MachineMemOperand target flag to the corresponding target flag.
Definition: MIParser.cpp:265
llvm::VRegInfo::RC
const TargetRegisterClass * RC
Definition: MIParser.h:43
llvm::parseMachineBasicBlockDefinitions
bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine basic block definitions, and skip the machine instructions.
Definition: MIParser.cpp:3403
llvm::PerTargetMIParsingState::parseInstrName
bool parseInstrName(StringRef InstrName, unsigned &OpCode)
Try to convert an instruction name to an opcode.
Definition: MIParser.cpp:143
llvm::PerFunctionMIParsingState::Allocator
BumpPtrAllocator Allocator
Definition: MIParser.h:163
llvm::PerFunctionMIParsingState::JumpTableSlots
DenseMap< unsigned, unsigned > JumpTableSlots
Definition: MIParser.h:178
llvm::parseStackObjectReference
bool parseStackObjectReference(PerFunctionMIParsingState &PFS, int &FI, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3438
llvm::parseMDNode
bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3444
llvm::RegisterBank
This class implements the register bank concept.
Definition: RegisterBank.h:28
llvm::PerFunctionMIParsingState::IRSlots
const SlotMapping & IRSlots
Definition: MIParser.h:166
llvm::PerTargetMIParsingState::getTargetIndex
bool getTargetIndex(StringRef Name, int &Index)
Try to convert a name of target index to the corresponding target index.
Definition: MIParser.cpp:201
llvm::TargetRegisterClass
Definition: TargetRegisterInfo.h:46
llvm::VRegInfo::VReg
Register VReg
Definition: MIParser.h:46
llvm::SMDiagnostic
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:251
llvm::MCID::Flag
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:146
SMLoc.h
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
StringMap.h
llvm::PerTargetMIParsingState::getRegClass
const TargetRegisterClass * getRegClass(StringRef Name)
Check if the given identifier is a name of a register class.
Definition: MIParser.cpp:305
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::StringMap< const TargetRegisterClass * >
llvm::PerFunctionMIParsingState::VRegInfos
DenseMap< Register, VRegInfo * > VRegInfos
Definition: MIParser.h:173
llvm::PerFunctionMIParsingState::getIRValue
const Value * getIRValue(unsigned Slot)
Definition: MIParser.cpp:369
llvm::SlotMapping
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition: SlotMapping.h:32
llvm::parseMachineMetadata
bool parseMachineMetadata(PerFunctionMIParsingState &PFS, StringRef Src, SMRange SourceRange, SMDiagnostic &Error)
Definition: MIParser.cpp:3449
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::PerFunctionMIParsingState::getVRegInfo
VRegInfo & getVRegInfo(Register Num)
Definition: MIParser.cpp:324
llvm::VRegInfo::NORMAL
@ NORMAL
Definition: MIParser.h:39
llvm::PerFunctionMIParsingState::MachineForwardRefMDNodes
std::map< unsigned, std::pair< TempMDTuple, SMLoc > > MachineForwardRefMDNodes
Definition: MIParser.h:170
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
llvm::DenseMap
Definition: DenseMap.h:714
llvm::MachineMemOperand::Flags
Flags
Flags values. These may be or'd together.
Definition: MachineMemOperand.h:131
llvm::MDNode
Metadata node.
Definition: Metadata.h:897
llvm::PerTargetMIParsingState::getRegisterByName
bool getRegisterByName(StringRef RegName, Register &Reg)
Try to convert a register name to a register number.
Definition: MIParser.cpp:124
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
uint32_t
llvm::PerFunctionMIParsingState::MachineMetadataNodes
std::map< unsigned, TrackingMDNodeRef > MachineMetadataNodes
Definition: MIParser.h:169
llvm::PerTargetMIParsingState::setTarget
void setTarget(const TargetSubtargetInfo &NewSubtarget)
Definition: MIParser.cpp:86
Node
Definition: ItaniumDemangle.h:114
llvm::TargetSubtargetInfo
TargetSubtargetInfo - Generic base class for all target subtargets.
Definition: TargetSubtargetInfo.h:59
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::PerFunctionMIParsingState
Definition: MIParser.h:162
llvm::parseNamedRegisterReference
bool parseNamedRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3426
llvm::PerFunctionMIParsingState::Slots2Values
DenseMap< unsigned, const Value * > Slots2Values
Maps from slot numbers to function's unnamed values.
Definition: MIParser.h:181
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::PerTargetMIParsingState::getSubRegIndex
unsigned getSubRegIndex(StringRef Name)
Check if the given identifier is a name of a subregister index.
Definition: MIParser.cpp:183
llvm::PerFunctionMIParsingState::ConstantPoolSlots
DenseMap< unsigned, unsigned > ConstantPoolSlots
Definition: MIParser.h:177
llvm::PerFunctionMIParsingState::MF
MachineFunction & MF
Definition: MIParser.h:164
llvm::parseVirtualRegisterReference
bool parseVirtualRegisterReference(PerFunctionMIParsingState &PFS, VRegInfo *&Info, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3432
llvm::VRegInfo
Definition: MIParser.h:37
llvm::PerTargetMIParsingState::~PerTargetMIParsingState
~PerTargetMIParsingState()=default
llvm::PerTargetMIParsingState::PerTargetMIParsingState
PerTargetMIParsingState(const TargetSubtargetInfo &STI)
Definition: MIParser.h:151
llvm::PerTargetMIParsingState::getRegMask
const uint32_t * getRegMask(StringRef Identifier)
Check if the given identifier is a name of a register mask.
Definition: MIParser.cpp:166
llvm::VRegInfo::UNKNOWN
@ UNKNOWN
Definition: MIParser.h:39
llvm::SMRange
Represents a range in source code.
Definition: SMLoc.h:48
llvm::PerFunctionMIParsingState::MBBSlots
DenseMap< unsigned, MachineBasicBlock * > MBBSlots
Definition: MIParser.h:172
llvm::VRegInfo::D
union llvm::VRegInfo::@188 D
MachineMemOperand.h
RegName
#define RegName(no)
llvm::parseRegisterReference
bool parseRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3420
Register.h
llvm::VRegInfo::PreferredReg
Register PreferredReg
Definition: MIParser.h:47
llvm::PerTargetMIParsingState::getBitmaskTargetFlag
bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag)
Try to convert a name of a bitmask target flag to the corresponding target flag.
Definition: MIParser.cpp:244
llvm::PerTargetMIParsingState::getDirectTargetFlag
bool getDirectTargetFlag(StringRef Name, unsigned &Flag)
Try to convert a name of a direct target flag to the corresponding target flag.
Definition: MIParser.cpp:222
llvm::VRegInfo::Explicit
bool Explicit
VReg was explicitly specified in the .mir file.
Definition: MIParser.h:41
llvm::parseMBBReference
bool parseMBBReference(PerFunctionMIParsingState &PFS, MachineBasicBlock *&MBB, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3414
llvm::VRegInfo::GENERIC
@ GENERIC
Definition: MIParser.h:39
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::PerFunctionMIParsingState::Target
PerTargetMIParsingState & Target
Definition: MIParser.h:167