LLVM  13.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 
22 namespace llvm {
23 
24 class MachineBasicBlock;
25 class MachineFunction;
26 class MDNode;
27 class RegisterBank;
28 struct SlotMapping;
29 class SMDiagnostic;
30 class SourceMgr;
31 class StringRef;
32 class TargetRegisterClass;
33 class TargetSubtargetInfo;
34 
35 struct VRegInfo {
36  enum uint8_t {
38  } Kind = UNKNOWN;
39  bool Explicit = false; ///< VReg was explicitly specified in the .mir file.
40  union {
43  } D;
46 };
47 
50 
52 private:
53  const TargetSubtargetInfo &Subtarget;
54 
55  /// Maps from instruction names to op codes.
56  StringMap<unsigned> Names2InstrOpCodes;
57 
58  /// Maps from register names to registers.
59  StringMap<Register> Names2Regs;
60 
61  /// Maps from register mask names to register masks.
62  StringMap<const uint32_t *> Names2RegMasks;
63 
64  /// Maps from subregister names to subregister indices.
65  StringMap<unsigned> Names2SubRegIndices;
66 
67  /// Maps from target index names to target indices.
68  StringMap<int> Names2TargetIndices;
69 
70  /// Maps from direct target flag names to the direct target flag values.
71  StringMap<unsigned> Names2DirectTargetFlags;
72 
73  /// Maps from direct target flag names to the bitmask target flag values.
74  StringMap<unsigned> Names2BitmaskTargetFlags;
75 
76  /// Maps from MMO target flag names to MMO target flag values.
77  StringMap<MachineMemOperand::Flags> Names2MMOTargetFlags;
78 
79  /// Maps from register class names to register classes.
80  Name2RegClassMap Names2RegClasses;
81 
82  /// Maps from register bank names to register banks.
83  Name2RegBankMap Names2RegBanks;
84 
85  void initNames2InstrOpCodes();
86  void initNames2Regs();
87  void initNames2RegMasks();
88  void initNames2SubRegIndices();
89  void initNames2TargetIndices();
90  void initNames2DirectTargetFlags();
91  void initNames2BitmaskTargetFlags();
92  void initNames2MMOTargetFlags();
93 
94  void initNames2RegClasses();
95  void initNames2RegBanks();
96 
97 public:
98  /// Try to convert an instruction name to an opcode. Return true if the
99  /// instruction name is invalid.
100  bool parseInstrName(StringRef InstrName, unsigned &OpCode);
101 
102  /// Try to convert a register name to a register number. Return true if the
103  /// register name is invalid.
105 
106  /// Check if the given identifier is a name of a register mask.
107  ///
108  /// Return null if the identifier isn't a register mask.
109  const uint32_t *getRegMask(StringRef Identifier);
110 
111  /// Check if the given identifier is a name of a subregister index.
112  ///
113  /// Return 0 if the name isn't a subregister index class.
114  unsigned getSubRegIndex(StringRef Name);
115 
116  /// Try to convert a name of target index to the corresponding target index.
117  ///
118  /// Return true if the name isn't a name of a target index.
119  bool getTargetIndex(StringRef Name, int &Index);
120 
121  /// Try to convert a name of a direct target flag to the corresponding
122  /// target flag.
123  ///
124  /// Return true if the name isn't a name of a direct flag.
125  bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
126 
127  /// Try to convert a name of a bitmask target flag to the corresponding
128  /// target flag.
129  ///
130  /// Return true if the name isn't a name of a bitmask target flag.
131  bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
132 
133  /// Try to convert a name of a MachineMemOperand target flag to the
134  /// corresponding target flag.
135  ///
136  /// Return true if the name isn't a name of a target MMO flag.
138 
139  /// Check if the given identifier is a name of a register class.
140  ///
141  /// Return null if the name isn't a register class.
143 
144  /// Check if the given identifier is a name of a register bank.
145  ///
146  /// Return null if the name isn't a register bank.
148 
150  : Subtarget(STI) {
151  initNames2RegClasses();
152  initNames2RegBanks();
153  }
154 
155  ~PerTargetMIParsingState() = default;
156 
157  void setTarget(const TargetSubtargetInfo &NewSubtarget);
158 };
159 
166 
174 
175  /// Maps from slot numbers to function's unnamed values.
177 
179  const SlotMapping &IRSlots,
181 
184  const Value *getIRValue(unsigned Slot);
185 };
186 
187 /// Parse the machine basic block definitions, and skip the machine
188 /// instructions.
189 ///
190 /// This function runs the first parsing pass on the machine function's body.
191 /// It parses only the machine basic block definitions and creates the machine
192 /// basic blocks in the given machine function.
193 ///
194 /// The machine instructions aren't parsed during the first pass because all
195 /// the machine basic blocks aren't defined yet - this makes it impossible to
196 /// resolve the machine basic block references.
197 ///
198 /// Return true if an error occurred.
201 
202 /// Parse the machine instructions.
203 ///
204 /// This function runs the second parsing pass on the machine function's body.
205 /// It skips the machine basic block definitions and parses only the machine
206 /// instructions and basic block attributes like liveins and successors.
207 ///
208 /// The second parsing pass assumes that the first parsing pass already ran
209 /// on the given source string.
210 ///
211 /// Return true if an error occurred.
214 
218 
220  Register &Reg, StringRef Src,
222 
225 
227  VRegInfo *&Info, StringRef Src,
229 
232 
235 
236 } // end namespace llvm
237 
238 #endif // LLVM_CODEGEN_MIRPARSER_MIPARSER_H
llvm::PerFunctionMIParsingState::SM
SourceMgr * SM
Definition: MIParser.h:163
llvm
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::VRegInfo::RegBank
const RegisterBank * RegBank
Definition: MIParser.h:42
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:124
llvm::PerTargetMIParsingState
Definition: MIParser.h:51
llvm::PerFunctionMIParsingState::StackObjectSlots
DenseMap< unsigned, int > StackObjectSlots
Definition: MIParser.h:171
llvm::VRegInfo::Kind
enum llvm::VRegInfo::uint8_t Kind
llvm::VRegInfo::uint8_t
uint8_t
Definition: MIParser.h:36
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:170
DenseMap.h
llvm::parseMachineInstructions
bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine instructions.
Definition: MIParser.cpp:3221
llvm::VRegInfo::REGBANK
@ REGBANK
Definition: MIParser.h:37
llvm::PerFunctionMIParsingState::VRegInfosNamed
StringMap< VRegInfo * > VRegInfosNamed
Definition: MIParser.h:169
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:41
llvm::parseMachineBasicBlockDefinitions
bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine basic block definitions, and skip the machine instructions.
Definition: MIParser.cpp:3215
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:161
llvm::PerFunctionMIParsingState::JumpTableSlots
DenseMap< unsigned, unsigned > JumpTableSlots
Definition: MIParser.h:173
llvm::parseStackObjectReference
bool parseStackObjectReference(PerFunctionMIParsingState &PFS, int &FI, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3250
llvm::parseMDNode
bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3256
llvm::RegisterBank
This class implements the register bank concept.
Definition: RegisterBank.h:28
llvm::PerFunctionMIParsingState::IRSlots
const SlotMapping & IRSlots
Definition: MIParser.h:164
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:44
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
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:168
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
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:37
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:130
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:57
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
uint32_t
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:160
llvm::parseNamedRegisterReference
bool parseNamedRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3238
llvm::PerFunctionMIParsingState::Slots2Values
DenseMap< unsigned, const Value * > Slots2Values
Maps from slot numbers to function's unnamed values.
Definition: MIParser.h:176
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::VRegInfo::D
union llvm::VRegInfo::@187 D
llvm::PerFunctionMIParsingState::ConstantPoolSlots
DenseMap< unsigned, unsigned > ConstantPoolSlots
Definition: MIParser.h:172
llvm::PerFunctionMIParsingState::MF
MachineFunction & MF
Definition: MIParser.h:162
llvm::parseVirtualRegisterReference
bool parseVirtualRegisterReference(PerFunctionMIParsingState &PFS, VRegInfo *&Info, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3244
llvm::VRegInfo
Definition: MIParser.h:35
llvm::PerTargetMIParsingState::~PerTargetMIParsingState
~PerTargetMIParsingState()=default
llvm::PerTargetMIParsingState::PerTargetMIParsingState
PerTargetMIParsingState(const TargetSubtargetInfo &STI)
Definition: MIParser.h:149
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:37
llvm::PerFunctionMIParsingState::MBBSlots
DenseMap< unsigned, MachineBasicBlock * > MBBSlots
Definition: MIParser.h:167
MachineMemOperand.h
RegName
#define RegName(no)
llvm::parseRegisterReference
bool parseRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3232
Register.h
llvm::VRegInfo::PreferredReg
Register PreferredReg
Definition: MIParser.h:45
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:39
llvm::parseMBBReference
bool parseMBBReference(PerFunctionMIParsingState &PFS, MachineBasicBlock *&MBB, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3226
llvm::VRegInfo::GENERIC
@ GENERIC
Definition: MIParser.h:37
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::PerFunctionMIParsingState::Target
PerTargetMIParsingState & Target
Definition: MIParser.h:165