blob: cc9f83c260157a1bc763ee4b398230b098f8712c [file] [log] [blame]
Eugene Zelenkofb69e662017-06-06 22:22:41 +00001//===- MIParser.h - Machine Instructions Parser -----------------*- C++ -*-===//
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the function that parses the machine instructions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_CODEGEN_MIRPARSER_MIPARSER_H
14#define LLVM_LIB_CODEGEN_MIRPARSER_MIPARSER_H
15
Alex Lorenz33f0aef2015-06-26 16:46:11 +000016#include "llvm/ADT/DenseMap.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000017#include "llvm/ADT/StringMap.h"
18#include "llvm/Support/Allocator.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000019
20namespace llvm {
21
Alex Lorenz33f0aef2015-06-26 16:46:11 +000022class MachineBasicBlock;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000023class MachineFunction;
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000024class MDNode;
Matthias Braun74ad41c2016-10-11 03:13:01 +000025class RegisterBank;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000026struct SlotMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000027class SMDiagnostic;
28class SourceMgr;
Eugene Zelenkofb69e662017-06-06 22:22:41 +000029class StringRef;
Matthias Braun74ad41c2016-10-11 03:13:01 +000030class TargetRegisterClass;
31
32struct VRegInfo {
33 enum uint8_t {
34 UNKNOWN, NORMAL, GENERIC, REGBANK
35 } Kind = UNKNOWN;
36 bool Explicit = false; ///< VReg was explicitly specified in the .mir file.
37 union {
38 const TargetRegisterClass *RC;
39 const RegisterBank *RegBank;
40 } D;
41 unsigned VReg;
42 unsigned PreferredReg = 0;
43};
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000044
Eugene Zelenkofb69e662017-06-06 22:22:41 +000045using Name2RegClassMap = StringMap<const TargetRegisterClass *>;
46using Name2RegBankMap = StringMap<const RegisterBank *>;
Matthias Braunde5fea22017-01-18 00:59:19 +000047
Alex Lorenz7a503fa2015-07-07 17:46:43 +000048struct PerFunctionMIParsingState {
Matthias Braun74ad41c2016-10-11 03:13:01 +000049 BumpPtrAllocator Allocator;
Matthias Braun83947862016-07-13 22:23:23 +000050 MachineFunction &MF;
Matthias Braune35861d2016-07-13 23:27:50 +000051 SourceMgr *SM;
52 const SlotMapping &IRSlots;
Matthias Braunde5fea22017-01-18 00:59:19 +000053 const Name2RegClassMap &Names2RegClasses;
54 const Name2RegBankMap &Names2RegBanks;
Matthias Braun83947862016-07-13 22:23:23 +000055
Alex Lorenz7a503fa2015-07-07 17:46:43 +000056 DenseMap<unsigned, MachineBasicBlock *> MBBSlots;
Matthias Braun74ad41c2016-10-11 03:13:01 +000057 DenseMap<unsigned, VRegInfo*> VRegInfos;
Puyan Lotfi399b46c2018-03-30 18:15:54 +000058 StringMap<VRegInfo*> VRegInfosNamed;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000059 DenseMap<unsigned, int> FixedStackObjectSlots;
60 DenseMap<unsigned, int> StackObjectSlots;
Alex Lorenzab980492015-07-20 20:51:18 +000061 DenseMap<unsigned, unsigned> ConstantPoolSlots;
Alex Lorenz31d70682015-07-15 23:38:35 +000062 DenseMap<unsigned, unsigned> JumpTableSlots;
Matthias Braun83947862016-07-13 22:23:23 +000063
Matthias Braune35861d2016-07-13 23:27:50 +000064 PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM,
Matthias Braunde5fea22017-01-18 00:59:19 +000065 const SlotMapping &IRSlots,
66 const Name2RegClassMap &Names2RegClasses,
67 const Name2RegBankMap &Names2RegBanks);
Matthias Braun74ad41c2016-10-11 03:13:01 +000068
Fangrui Songcb0bab82018-07-16 18:51:40 +000069 VRegInfo &getVRegInfo(unsigned Num);
Puyan Lotfi399b46c2018-03-30 18:15:54 +000070 VRegInfo &getVRegInfoNamed(StringRef RegName);
Alex Lorenz7a503fa2015-07-07 17:46:43 +000071};
72
Alex Lorenz5022f6b2015-08-13 23:10:16 +000073/// Parse the machine basic block definitions, and skip the machine
74/// instructions.
75///
76/// This function runs the first parsing pass on the machine function's body.
77/// It parses only the machine basic block definitions and creates the machine
78/// basic blocks in the given machine function.
79///
80/// The machine instructions aren't parsed during the first pass because all
81/// the machine basic blocks aren't defined yet - this makes it impossible to
82/// resolve the machine basic block references.
83///
84/// Return true if an error occurred.
Matthias Braun83947862016-07-13 22:23:23 +000085bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +000086 StringRef Src, SMDiagnostic &Error);
Alex Lorenz5022f6b2015-08-13 23:10:16 +000087
88/// Parse the machine instructions.
89///
90/// This function runs the second parsing pass on the machine function's body.
91/// It skips the machine basic block definitions and parses only the machine
92/// instructions and basic block attributes like liveins and successors.
93///
94/// The second parsing pass assumes that the first parsing pass already ran
95/// on the given source string.
96///
97/// Return true if an error occurred.
Matthias Braun74ad41c2016-10-11 03:13:01 +000098bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src,
99 SMDiagnostic &Error);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000100
Matthias Braun74ad41c2016-10-11 03:13:01 +0000101bool parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +0000102 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +0000103 SMDiagnostic &Error);
Alex Lorenzf09df002015-06-30 18:16:42 +0000104
Tom Stellard9c884e42016-11-15 00:03:14 +0000105bool parseRegisterReference(PerFunctionMIParsingState &PFS,
106 unsigned &Reg, StringRef Src,
107 SMDiagnostic &Error);
108
Matthias Braun74ad41c2016-10-11 03:13:01 +0000109bool parseNamedRegisterReference(PerFunctionMIParsingState &PFS, unsigned &Reg,
110 StringRef Src, SMDiagnostic &Error);
Alex Lorenz9fab3702015-07-14 21:24:41 +0000111
Matthias Braun74ad41c2016-10-11 03:13:01 +0000112bool parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
113 VRegInfo *&Info, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +0000114 SMDiagnostic &Error);
115
Matthias Braun74ad41c2016-10-11 03:13:01 +0000116bool parseStackObjectReference(PerFunctionMIParsingState &PFS, int &FI,
117 StringRef Src, SMDiagnostic &Error);
Alex Lorenza314d812015-08-18 22:26:26 +0000118
Matthias Braun74ad41c2016-10-11 03:13:01 +0000119bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, StringRef Src,
120 SMDiagnostic &Error);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000121
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000122} // end namespace llvm
123
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000124#endif // LLVM_LIB_CODEGEN_MIRPARSER_MIPARSER_H