Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 1 | //===- MIRParser.cpp - MIR serialization format parser implementation -----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the class that parses the optional LLVM IR and machine |
| 10 | // functions that are stored in MIR files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/MIRParser/MIRParser.h" |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 15 | #include "MIParser.h" |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringMap.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 20 | #include "llvm/AsmParser/Parser.h" |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 21 | #include "llvm/AsmParser/SlotMapping.h" |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/GlobalISel/RegisterBank.h" |
| 23 | #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h" |
| 24 | #include "llvm/CodeGen/MIRYamlMapping.h" |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineConstantPool.h" |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunction.h" |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 30 | #include "llvm/IR/BasicBlock.h" |
Reid Kleckner | 2886580 | 2016-04-14 18:29:59 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DebugInfo.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 32 | #include "llvm/IR/DiagnosticInfo.h" |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Instructions.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 34 | #include "llvm/IR/LLVMContext.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Module.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 36 | #include "llvm/IR/ValueSymbolTable.h" |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 37 | #include "llvm/Support/LineIterator.h" |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MemoryBuffer.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 39 | #include "llvm/Support/SMLoc.h" |
| 40 | #include "llvm/Support/SourceMgr.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 41 | #include "llvm/Support/YAMLTraits.h" |
| 42 | #include <memory> |
| 43 | |
| 44 | using namespace llvm; |
| 45 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 46 | namespace llvm { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 47 | |
| 48 | /// This class implements the parsing of LLVM IR that's embedded inside a MIR |
| 49 | /// file. |
| 50 | class MIRParserImpl { |
| 51 | SourceMgr SM; |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 52 | yaml::Input In; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 53 | StringRef Filename; |
| 54 | LLVMContext &Context; |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 55 | SlotMapping IRSlots; |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 56 | /// Maps from register class names to register classes. |
Matthias Braun | de5fea2 | 2017-01-18 00:59:19 +0000 | [diff] [blame] | 57 | Name2RegClassMap Names2RegClasses; |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 58 | /// Maps from register bank names to register banks. |
Matthias Braun | de5fea2 | 2017-01-18 00:59:19 +0000 | [diff] [blame] | 59 | Name2RegBankMap Names2RegBanks; |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 60 | /// True when the MIR file doesn't have LLVM IR. Dummy IR functions are |
| 61 | /// created and inserted into the given module when this is true. |
| 62 | bool NoLLVMIR = false; |
| 63 | /// True when a well formed MIR file does not contain any MIR/machine function |
| 64 | /// parts. |
| 65 | bool NoMIRDocuments = false; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 66 | |
| 67 | public: |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 68 | MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, |
| 69 | StringRef Filename, LLVMContext &Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 70 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 71 | void reportDiagnostic(const SMDiagnostic &Diag); |
| 72 | |
| 73 | /// Report an error with the given message at unknown location. |
| 74 | /// |
| 75 | /// Always returns true. |
| 76 | bool error(const Twine &Message); |
| 77 | |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 78 | /// Report an error with the given message at the given location. |
| 79 | /// |
| 80 | /// Always returns true. |
| 81 | bool error(SMLoc Loc, const Twine &Message); |
| 82 | |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 83 | /// Report a given error with the location translated from the location in an |
| 84 | /// embedded string literal to a location in the MIR file. |
| 85 | /// |
| 86 | /// Always returns true. |
| 87 | bool error(const SMDiagnostic &Error, SMRange SourceRange); |
| 88 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 89 | /// Try to parse the optional LLVM module and the machine functions in the MIR |
| 90 | /// file. |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 91 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 92 | /// Return null if an error occurred. |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 93 | std::unique_ptr<Module> parseIRModule(); |
| 94 | |
| 95 | bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 96 | |
| 97 | /// Parse the machine function in the current YAML document. |
| 98 | /// |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 99 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 100 | /// Return true if an error occurred. |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 101 | bool parseMachineFunction(Module &M, MachineModuleInfo &MMI); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 102 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 103 | /// Initialize the machine function to the state that's described in the MIR |
| 104 | /// file. |
| 105 | /// |
| 106 | /// Return true if error occurred. |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 107 | bool initializeMachineFunction(const yaml::MachineFunction &YamlMF, |
| 108 | MachineFunction &MF); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 109 | |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 110 | bool parseRegisterInfo(PerFunctionMIParsingState &PFS, |
| 111 | const yaml::MachineFunction &YamlMF); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 112 | |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 113 | bool setupRegisterInfo(const PerFunctionMIParsingState &PFS, |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 114 | const yaml::MachineFunction &YamlMF); |
| 115 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 116 | bool initializeFrameInfo(PerFunctionMIParsingState &PFS, |
| 117 | const yaml::MachineFunction &YamlMF); |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 118 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 119 | bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS, |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 120 | std::vector<CalleeSavedInfo> &CSIInfo, |
| 121 | const yaml::StringValue &RegisterSource, |
Matthias Braun | 5c3e8a4 | 2017-09-28 18:52:14 +0000 | [diff] [blame] | 122 | bool IsRestored, int FrameIdx); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 123 | |
Francis Visoiu Mistrih | 57fcd34 | 2018-04-25 18:58:06 +0000 | [diff] [blame] | 124 | template <typename T> |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 125 | bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS, |
Francis Visoiu Mistrih | 57fcd34 | 2018-04-25 18:58:06 +0000 | [diff] [blame] | 126 | const T &Object, |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 127 | int FrameIdx); |
| 128 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 129 | bool initializeConstantPool(PerFunctionMIParsingState &PFS, |
| 130 | MachineConstantPool &ConstantPool, |
| 131 | const yaml::MachineFunction &YamlMF); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 132 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 133 | bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS, |
| 134 | const yaml::MachineJumpTable &YamlJTI); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 135 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 136 | private: |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 137 | bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 138 | const yaml::StringValue &Source); |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 139 | |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 140 | bool parseMBBReference(PerFunctionMIParsingState &PFS, |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 141 | MachineBasicBlock *&MBB, |
| 142 | const yaml::StringValue &Source); |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 143 | |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 144 | /// Return a MIR diagnostic converted from an MI string diagnostic. |
| 145 | SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error, |
| 146 | SMRange SourceRange); |
| 147 | |
Alex Lorenz | 9b62cf6 | 2015-08-13 20:30:11 +0000 | [diff] [blame] | 148 | /// Return a MIR diagnostic converted from a diagnostic located in a YAML |
| 149 | /// block scalar string. |
| 150 | SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error, |
| 151 | SMRange SourceRange); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 152 | |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 153 | void initNames2RegClasses(const MachineFunction &MF); |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 154 | void initNames2RegBanks(const MachineFunction &MF); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 155 | |
| 156 | /// Check if the given identifier is a name of a register class. |
| 157 | /// |
| 158 | /// Return null if the name isn't a register class. |
| 159 | const TargetRegisterClass *getRegClass(const MachineFunction &MF, |
| 160 | StringRef Name); |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 161 | |
| 162 | /// Check if the given identifier is a name of a register bank. |
| 163 | /// |
| 164 | /// Return null if the name isn't a register bank. |
| 165 | const RegisterBank *getRegBank(const MachineFunction &MF, StringRef Name); |
Matthias Braun | 90799ce | 2016-08-23 21:19:49 +0000 | [diff] [blame] | 166 | |
| 167 | void computeFunctionProperties(MachineFunction &MF); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 170 | } // end namespace llvm |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 171 | |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 172 | static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { |
| 173 | reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag); |
| 174 | } |
| 175 | |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 176 | MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, |
| 177 | StringRef Filename, LLVMContext &Context) |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 178 | : SM(), |
| 179 | In(SM.getMemoryBuffer( |
| 180 | SM.AddNewSourceBuffer(std::move(Contents), SMLoc()))->getBuffer(), |
| 181 | nullptr, handleYAMLDiag, this), |
| 182 | Filename(Filename), |
| 183 | Context(Context) { |
| 184 | In.setContext(&In); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 187 | bool MIRParserImpl::error(const Twine &Message) { |
| 188 | Context.diagnose(DiagnosticInfoMIRParser( |
| 189 | DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str()))); |
| 190 | return true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 191 | } |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 192 | |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 193 | bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) { |
| 194 | Context.diagnose(DiagnosticInfoMIRParser( |
| 195 | DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message))); |
| 196 | return true; |
| 197 | } |
| 198 | |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 199 | bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) { |
| 200 | assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error"); |
| 201 | reportDiagnostic(diagFromMIStringDiag(Error, SourceRange)); |
| 202 | return true; |
| 203 | } |
| 204 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 205 | void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) { |
| 206 | DiagnosticSeverity Kind; |
| 207 | switch (Diag.getKind()) { |
| 208 | case SourceMgr::DK_Error: |
| 209 | Kind = DS_Error; |
| 210 | break; |
| 211 | case SourceMgr::DK_Warning: |
| 212 | Kind = DS_Warning; |
| 213 | break; |
| 214 | case SourceMgr::DK_Note: |
| 215 | Kind = DS_Note; |
| 216 | break; |
Adam Nemet | 01104ae | 2017-10-12 23:56:02 +0000 | [diff] [blame] | 217 | case SourceMgr::DK_Remark: |
| 218 | llvm_unreachable("remark unexpected"); |
| 219 | break; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 220 | } |
| 221 | Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag)); |
| 222 | } |
| 223 | |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 224 | std::unique_ptr<Module> MIRParserImpl::parseIRModule() { |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 225 | if (!In.setCurrentDocument()) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 226 | if (In.error()) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 227 | return nullptr; |
| 228 | // Create an empty module when the MIR file is empty. |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 229 | NoMIRDocuments = true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 230 | return llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 233 | std::unique_ptr<Module> M; |
| 234 | // Parse the block scalar manually so that we can return unique pointer |
| 235 | // without having to go trough YAML traits. |
| 236 | if (const auto *BSN = |
| 237 | dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 238 | SMDiagnostic Error; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 239 | M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error, |
Yaxun Liu | c00d81e | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 240 | Context, &IRSlots, /*UpgradeDebugInfo=*/false); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 241 | if (!M) { |
Alex Lorenz | 9b62cf6 | 2015-08-13 20:30:11 +0000 | [diff] [blame] | 242 | reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange())); |
Matthias Braun | d6f9562 | 2016-07-14 00:42:37 +0000 | [diff] [blame] | 243 | return nullptr; |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 244 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 245 | In.nextDocument(); |
| 246 | if (!In.setCurrentDocument()) |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 247 | NoMIRDocuments = true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 248 | } else { |
| 249 | // Create an new, empty module. |
| 250 | M = llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 251 | NoLLVMIR = true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 252 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 253 | return M; |
| 254 | } |
| 255 | |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 256 | bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) { |
| 257 | if (NoMIRDocuments) |
| 258 | return false; |
| 259 | |
| 260 | // Parse the machine functions. |
| 261 | do { |
| 262 | if (parseMachineFunction(M, MMI)) |
| 263 | return true; |
| 264 | In.nextDocument(); |
| 265 | } while (In.setCurrentDocument()); |
| 266 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 267 | return false; |
| 268 | } |
| 269 | |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 270 | /// Create an empty function with the given name. |
| 271 | static Function *createDummyFunction(StringRef Name, Module &M) { |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 272 | auto &Context = M.getContext(); |
James Y Knight | f47d6b3 | 2019-01-31 20:35:56 +0000 | [diff] [blame] | 273 | Function *F = |
| 274 | Function::Create(FunctionType::get(Type::getVoidTy(Context), false), |
| 275 | Function::ExternalLinkage, Name, M); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 276 | BasicBlock *BB = BasicBlock::Create(Context, "entry", F); |
| 277 | new UnreachableInst(Context, BB); |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 278 | return F; |
| 279 | } |
| 280 | |
| 281 | bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) { |
| 282 | // Parse the yaml. |
| 283 | yaml::MachineFunction YamlMF; |
| 284 | yaml::EmptyContext Ctx; |
| 285 | yaml::yamlize(In, YamlMF, false, Ctx); |
| 286 | if (In.error()) |
| 287 | return true; |
| 288 | |
| 289 | // Search for the corresponding IR function. |
| 290 | StringRef FunctionName = YamlMF.Name; |
| 291 | Function *F = M.getFunction(FunctionName); |
| 292 | if (!F) { |
| 293 | if (NoLLVMIR) { |
| 294 | F = createDummyFunction(FunctionName, M); |
| 295 | } else { |
| 296 | return error(Twine("function '") + FunctionName + |
| 297 | "' isn't defined in the provided LLVM IR"); |
| 298 | } |
| 299 | } |
| 300 | if (MMI.getMachineFunction(*F) != nullptr) |
| 301 | return error(Twine("redefinition of machine function '") + FunctionName + |
| 302 | "'"); |
| 303 | |
| 304 | // Create the MachineFunction. |
| 305 | MachineFunction &MF = MMI.getOrCreateMachineFunction(*F); |
| 306 | if (initializeMachineFunction(YamlMF, MF)) |
| 307 | return true; |
| 308 | |
| 309 | return false; |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Matthias Braun | 79f85b3 | 2016-08-24 01:32:41 +0000 | [diff] [blame] | 312 | static bool isSSA(const MachineFunction &MF) { |
| 313 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 314 | for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) { |
| 315 | unsigned Reg = TargetRegisterInfo::index2VirtReg(I); |
| 316 | if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg)) |
| 317 | return false; |
| 318 | } |
| 319 | return true; |
| 320 | } |
| 321 | |
Matthias Braun | 90799ce | 2016-08-23 21:19:49 +0000 | [diff] [blame] | 322 | void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) { |
Matthias Braun | 79f85b3 | 2016-08-24 01:32:41 +0000 | [diff] [blame] | 323 | MachineFunctionProperties &Properties = MF.getProperties(); |
Matthias Braun | a319e2c | 2016-08-24 22:34:06 +0000 | [diff] [blame] | 324 | |
| 325 | bool HasPHI = false; |
| 326 | bool HasInlineAsm = false; |
| 327 | for (const MachineBasicBlock &MBB : MF) { |
| 328 | for (const MachineInstr &MI : MBB) { |
| 329 | if (MI.isPHI()) |
| 330 | HasPHI = true; |
| 331 | if (MI.isInlineAsm()) |
| 332 | HasInlineAsm = true; |
| 333 | } |
| 334 | } |
| 335 | if (!HasPHI) |
Matthias Braun | 79f85b3 | 2016-08-24 01:32:41 +0000 | [diff] [blame] | 336 | Properties.set(MachineFunctionProperties::Property::NoPHIs); |
Matthias Braun | a319e2c | 2016-08-24 22:34:06 +0000 | [diff] [blame] | 337 | MF.setHasInlineAsm(HasInlineAsm); |
Matthias Braun | 79f85b3 | 2016-08-24 01:32:41 +0000 | [diff] [blame] | 338 | |
| 339 | if (isSSA(MF)) |
| 340 | Properties.set(MachineFunctionProperties::Property::IsSSA); |
| 341 | else |
Quentin Colombet | e609a9a | 2016-08-26 22:09:11 +0000 | [diff] [blame] | 342 | Properties.reset(MachineFunctionProperties::Property::IsSSA); |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 343 | |
| 344 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 345 | if (MRI.getNumVirtRegs() == 0) |
| 346 | Properties.set(MachineFunctionProperties::Property::NoVRegs); |
Matthias Braun | 90799ce | 2016-08-23 21:19:49 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 349 | bool |
| 350 | MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF, |
| 351 | MachineFunction &MF) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 352 | // TODO: Recreate the machine function. |
Matthias Braun | de5fea2 | 2017-01-18 00:59:19 +0000 | [diff] [blame] | 353 | initNames2RegClasses(MF); |
| 354 | initNames2RegBanks(MF); |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 355 | if (YamlMF.Alignment) |
| 356 | MF.setAlignment(YamlMF.Alignment); |
| 357 | MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice); |
Sanjin Sijaric | 625d08e | 2018-10-24 21:07:38 +0000 | [diff] [blame] | 358 | MF.setHasWinCFI(YamlMF.HasWinCFI); |
Ahmed Bougacha | 0d7b0cb | 2016-08-02 15:10:25 +0000 | [diff] [blame] | 359 | |
| 360 | if (YamlMF.Legalized) |
| 361 | MF.getProperties().set(MachineFunctionProperties::Property::Legalized); |
Ahmed Bougacha | 2471265 | 2016-08-02 16:17:10 +0000 | [diff] [blame] | 362 | if (YamlMF.RegBankSelected) |
| 363 | MF.getProperties().set( |
| 364 | MachineFunctionProperties::Property::RegBankSelected); |
Ahmed Bougacha | b109d51 | 2016-08-02 16:49:19 +0000 | [diff] [blame] | 365 | if (YamlMF.Selected) |
| 366 | MF.getProperties().set(MachineFunctionProperties::Property::Selected); |
Roman Tereshin | 3054ece | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 367 | if (YamlMF.FailedISel) |
| 368 | MF.getProperties().set(MachineFunctionProperties::Property::FailedISel); |
Ahmed Bougacha | 0d7b0cb | 2016-08-02 15:10:25 +0000 | [diff] [blame] | 369 | |
Matthias Braun | de5fea2 | 2017-01-18 00:59:19 +0000 | [diff] [blame] | 370 | PerFunctionMIParsingState PFS(MF, SM, IRSlots, Names2RegClasses, |
| 371 | Names2RegBanks); |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 372 | if (parseRegisterInfo(PFS, YamlMF)) |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 373 | return true; |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 374 | if (!YamlMF.Constants.empty()) { |
| 375 | auto *ConstantPool = MF.getConstantPool(); |
| 376 | assert(ConstantPool && "Constant pool must be created"); |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 377 | if (initializeConstantPool(PFS, *ConstantPool, YamlMF)) |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 378 | return true; |
| 379 | } |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 380 | |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 381 | StringRef BlockStr = YamlMF.Body.Value.Value; |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 382 | SMDiagnostic Error; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 383 | SourceMgr BlockSM; |
| 384 | BlockSM.AddNewSourceBuffer( |
| 385 | MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false), |
| 386 | SMLoc()); |
| 387 | PFS.SM = &BlockSM; |
| 388 | if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) { |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 389 | reportDiagnostic( |
| 390 | diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange)); |
| 391 | return true; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 392 | } |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 393 | PFS.SM = &SM; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 394 | |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 395 | // Initialize the frame information after creating all the MBBs so that the |
| 396 | // MBB references in the frame information can be resolved. |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 397 | if (initializeFrameInfo(PFS, YamlMF)) |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 398 | return true; |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 399 | // Initialize the jump table after creating all the MBBs so that the MBB |
| 400 | // references can be resolved. |
| 401 | if (!YamlMF.JumpTableInfo.Entries.empty() && |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 402 | initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo)) |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 403 | return true; |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 404 | // Parse the machine instructions after creating all of the MBBs so that the |
| 405 | // parser can resolve the MBB references. |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 406 | StringRef InsnStr = YamlMF.Body.Value.Value; |
| 407 | SourceMgr InsnSM; |
| 408 | InsnSM.AddNewSourceBuffer( |
| 409 | MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false), |
| 410 | SMLoc()); |
| 411 | PFS.SM = &InsnSM; |
| 412 | if (parseMachineInstructions(PFS, InsnStr, Error)) { |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 413 | reportDiagnostic( |
| 414 | diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange)); |
| 415 | return true; |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 416 | } |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 417 | PFS.SM = &SM; |
| 418 | |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 419 | if (setupRegisterInfo(PFS, YamlMF)) |
| 420 | return true; |
Matthias Braun | 90799ce | 2016-08-23 21:19:49 +0000 | [diff] [blame] | 421 | |
| 422 | computeFunctionProperties(MF); |
| 423 | |
Matthias Braun | 5c290dc | 2018-01-19 03:16:36 +0000 | [diff] [blame] | 424 | MF.getSubtarget().mirFileLoaded(MF); |
| 425 | |
Alex Lorenz | c7bf204 | 2015-07-24 17:44:49 +0000 | [diff] [blame] | 426 | MF.verify(); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 427 | return false; |
| 428 | } |
| 429 | |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 430 | bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS, |
| 431 | const yaml::MachineFunction &YamlMF) { |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 432 | MachineFunction &MF = PFS.MF; |
Alex Lorenz | db07c40 | 2015-07-28 16:48:37 +0000 | [diff] [blame] | 433 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 434 | assert(RegInfo.tracksLiveness()); |
| 435 | if (!YamlMF.TracksRegLiveness) |
| 436 | RegInfo.invalidateLiveness(); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 437 | |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 438 | SMDiagnostic Error; |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 439 | // Parse the virtual register information. |
| 440 | for (const auto &VReg : YamlMF.VirtualRegisters) { |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 441 | VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value); |
| 442 | if (Info.Explicit) |
| 443 | return error(VReg.ID.SourceRange.Start, |
| 444 | Twine("redefinition of virtual register '%") + |
| 445 | Twine(VReg.ID.Value) + "'"); |
| 446 | Info.Explicit = true; |
| 447 | |
Quentin Colombet | 050b211 | 2016-03-08 01:17:03 +0000 | [diff] [blame] | 448 | if (StringRef(VReg.Class.Value).equals("_")) { |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 449 | Info.Kind = VRegInfo::GENERIC; |
Justin Bogner | 6c7663f | 2017-11-17 18:51:20 +0000 | [diff] [blame] | 450 | Info.D.RegBank = nullptr; |
Quentin Colombet | 050b211 | 2016-03-08 01:17:03 +0000 | [diff] [blame] | 451 | } else { |
| 452 | const auto *RC = getRegClass(MF, VReg.Class.Value); |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 453 | if (RC) { |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 454 | Info.Kind = VRegInfo::NORMAL; |
| 455 | Info.D.RC = RC; |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 456 | } else { |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 457 | const RegisterBank *RegBank = getRegBank(MF, VReg.Class.Value); |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 458 | if (!RegBank) |
| 459 | return error( |
| 460 | VReg.Class.SourceRange.Start, |
| 461 | Twine("use of undefined register class or register bank '") + |
| 462 | VReg.Class.Value + "'"); |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 463 | Info.Kind = VRegInfo::REGBANK; |
| 464 | Info.D.RegBank = RegBank; |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 465 | } |
Quentin Colombet | 050b211 | 2016-03-08 01:17:03 +0000 | [diff] [blame] | 466 | } |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 467 | |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 468 | if (!VReg.PreferredRegister.Value.empty()) { |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 469 | if (Info.Kind != VRegInfo::NORMAL) |
| 470 | return error(VReg.Class.SourceRange.Start, |
| 471 | Twine("preferred register can only be set for normal vregs")); |
Tom Stellard | 9c884e4 | 2016-11-15 00:03:14 +0000 | [diff] [blame] | 472 | |
| 473 | if (parseRegisterReference(PFS, Info.PreferredReg, |
| 474 | VReg.PreferredRegister.Value, Error)) |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 475 | return error(Error, VReg.PreferredRegister.SourceRange); |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 476 | } |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 477 | } |
Alex Lorenz | 12045a4 | 2015-07-27 17:42:45 +0000 | [diff] [blame] | 478 | |
| 479 | // Parse the liveins. |
| 480 | for (const auto &LiveIn : YamlMF.LiveIns) { |
| 481 | unsigned Reg = 0; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 482 | if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error)) |
Alex Lorenz | 12045a4 | 2015-07-27 17:42:45 +0000 | [diff] [blame] | 483 | return error(Error, LiveIn.Register.SourceRange); |
| 484 | unsigned VReg = 0; |
| 485 | if (!LiveIn.VirtualRegister.Value.empty()) { |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 486 | VRegInfo *Info; |
| 487 | if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value, |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 488 | Error)) |
Alex Lorenz | 12045a4 | 2015-07-27 17:42:45 +0000 | [diff] [blame] | 489 | return error(Error, LiveIn.VirtualRegister.SourceRange); |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 490 | VReg = Info->VReg; |
Alex Lorenz | 12045a4 | 2015-07-27 17:42:45 +0000 | [diff] [blame] | 491 | } |
| 492 | RegInfo.addLiveIn(Reg, VReg); |
| 493 | } |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 494 | |
Oren Ben Simhon | 0ef61ec | 2017-03-19 08:14:18 +0000 | [diff] [blame] | 495 | // Parse the callee saved registers (Registers that will |
| 496 | // be saved for the caller). |
| 497 | if (YamlMF.CalleeSavedRegisters) { |
| 498 | SmallVector<MCPhysReg, 16> CalleeSavedRegisters; |
| 499 | for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) { |
| 500 | unsigned Reg = 0; |
| 501 | if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error)) |
| 502 | return error(Error, RegSource.SourceRange); |
| 503 | CalleeSavedRegisters.push_back(Reg); |
| 504 | } |
| 505 | RegInfo.setCalleeSavedRegs(CalleeSavedRegisters); |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 506 | } |
Oren Ben Simhon | 0ef61ec | 2017-03-19 08:14:18 +0000 | [diff] [blame] | 507 | |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 508 | return false; |
| 509 | } |
| 510 | |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 511 | bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS, |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 512 | const yaml::MachineFunction &YamlMF) { |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 513 | MachineFunction &MF = PFS.MF; |
| 514 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 515 | bool Error = false; |
| 516 | // Create VRegs |
Puyan Lotfi | 399b46c | 2018-03-30 18:15:54 +0000 | [diff] [blame] | 517 | auto populateVRegInfo = [&] (const VRegInfo &Info, Twine Name) { |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 518 | unsigned Reg = Info.VReg; |
| 519 | switch (Info.Kind) { |
| 520 | case VRegInfo::UNKNOWN: |
| 521 | error(Twine("Cannot determine class/bank of virtual register ") + |
Puyan Lotfi | 399b46c | 2018-03-30 18:15:54 +0000 | [diff] [blame] | 522 | Name + " in function '" + MF.getName() + "'"); |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 523 | Error = true; |
| 524 | break; |
| 525 | case VRegInfo::NORMAL: |
| 526 | MRI.setRegClass(Reg, Info.D.RC); |
| 527 | if (Info.PreferredReg != 0) |
| 528 | MRI.setSimpleHint(Reg, Info.PreferredReg); |
| 529 | break; |
| 530 | case VRegInfo::GENERIC: |
| 531 | break; |
| 532 | case VRegInfo::REGBANK: |
| 533 | MRI.setRegBank(Reg, *Info.D.RegBank); |
| 534 | break; |
| 535 | } |
Puyan Lotfi | 399b46c | 2018-03-30 18:15:54 +0000 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | for (auto I = PFS.VRegInfosNamed.begin(), E = PFS.VRegInfosNamed.end(); |
| 539 | I != E; I++) { |
| 540 | const VRegInfo &Info = *I->second; |
| 541 | populateVRegInfo(Info, Twine(I->first())); |
| 542 | } |
| 543 | |
| 544 | for (auto P : PFS.VRegInfos) { |
| 545 | const VRegInfo &Info = *P.second; |
| 546 | populateVRegInfo(Info, Twine(P.first)); |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | // Compute MachineRegisterInfo::UsedPhysRegMask |
Oren Ben Simhon | 0ef61ec | 2017-03-19 08:14:18 +0000 | [diff] [blame] | 550 | for (const MachineBasicBlock &MBB : MF) { |
| 551 | for (const MachineInstr &MI : MBB) { |
| 552 | for (const MachineOperand &MO : MI.operands()) { |
| 553 | if (!MO.isRegMask()) |
| 554 | continue; |
| 555 | MRI.addPhysRegsUsedFromRegMask(MO.getRegMask()); |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | } |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 559 | |
| 560 | // FIXME: This is a temporary workaround until the reserved registers can be |
| 561 | // serialized. |
| 562 | MRI.freezeReservedRegs(MF); |
| 563 | return Error; |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 566 | bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS, |
| 567 | const yaml::MachineFunction &YamlMF) { |
| 568 | MachineFunction &MF = PFS.MF; |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 569 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 570 | const Function &F = MF.getFunction(); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 571 | const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo; |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 572 | MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken); |
| 573 | MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken); |
| 574 | MFI.setHasStackMap(YamlMFI.HasStackMap); |
| 575 | MFI.setHasPatchPoint(YamlMFI.HasPatchPoint); |
| 576 | MFI.setStackSize(YamlMFI.StackSize); |
| 577 | MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment); |
| 578 | if (YamlMFI.MaxAlignment) |
| 579 | MFI.ensureMaxAlignment(YamlMFI.MaxAlignment); |
| 580 | MFI.setAdjustsStack(YamlMFI.AdjustsStack); |
| 581 | MFI.setHasCalls(YamlMFI.HasCalls); |
Matthias Braun | ab9438c | 2017-05-01 22:32:25 +0000 | [diff] [blame] | 582 | if (YamlMFI.MaxCallFrameSize != ~0u) |
| 583 | MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize); |
Reid Kleckner | 9ea2c01 | 2018-10-01 21:59:45 +0000 | [diff] [blame] | 584 | MFI.setCVBytesOfCalleeSavedRegisters(YamlMFI.CVBytesOfCalleeSavedRegisters); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 585 | MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment); |
| 586 | MFI.setHasVAStart(YamlMFI.HasVAStart); |
| 587 | MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc); |
Francis Visoiu Mistrih | 537d7ee | 2018-04-06 08:56:25 +0000 | [diff] [blame] | 588 | MFI.setLocalFrameSize(YamlMFI.LocalFrameSize); |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 589 | if (!YamlMFI.SavePoint.Value.empty()) { |
| 590 | MachineBasicBlock *MBB = nullptr; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 591 | if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint)) |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 592 | return true; |
| 593 | MFI.setSavePoint(MBB); |
| 594 | } |
| 595 | if (!YamlMFI.RestorePoint.Value.empty()) { |
| 596 | MachineBasicBlock *MBB = nullptr; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 597 | if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint)) |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 598 | return true; |
| 599 | MFI.setRestorePoint(MBB); |
| 600 | } |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 601 | |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 602 | std::vector<CalleeSavedInfo> CSIInfo; |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 603 | // Initialize the fixed frame objects. |
| 604 | for (const auto &Object : YamlMF.FixedStackObjects) { |
| 605 | int ObjectIdx; |
| 606 | if (Object.Type != yaml::FixedMachineStackObject::SpillSlot) |
| 607 | ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset, |
| 608 | Object.IsImmutable, Object.IsAliased); |
| 609 | else |
| 610 | ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset); |
| 611 | MFI.setObjectAlignment(ObjectIdx, Object.Alignment); |
Matt Arsenault | db78273 | 2017-07-20 21:03:45 +0000 | [diff] [blame] | 612 | MFI.setStackID(ObjectIdx, Object.StackID); |
Alex Lorenz | 1d9a303 | 2015-08-10 23:45:02 +0000 | [diff] [blame] | 613 | if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value, |
| 614 | ObjectIdx)) |
| 615 | .second) |
| 616 | return error(Object.ID.SourceRange.Start, |
| 617 | Twine("redefinition of fixed stack object '%fixed-stack.") + |
| 618 | Twine(Object.ID.Value) + "'"); |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 619 | if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister, |
Matthias Braun | 5c3e8a4 | 2017-09-28 18:52:14 +0000 | [diff] [blame] | 620 | Object.CalleeSavedRestored, ObjectIdx)) |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 621 | return true; |
Francis Visoiu Mistrih | 57fcd34 | 2018-04-25 18:58:06 +0000 | [diff] [blame] | 622 | if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx)) |
| 623 | return true; |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | // Initialize the ordinary frame objects. |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 627 | for (const auto &Object : YamlMF.StackObjects) { |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 628 | int ObjectIdx; |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 629 | const AllocaInst *Alloca = nullptr; |
| 630 | const yaml::StringValue &Name = Object.Name; |
| 631 | if (!Name.Value.empty()) { |
| 632 | Alloca = dyn_cast_or_null<AllocaInst>( |
Mehdi Amini | a53d49e | 2016-09-17 06:00:02 +0000 | [diff] [blame] | 633 | F.getValueSymbolTable()->lookup(Name.Value)); |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 634 | if (!Alloca) |
| 635 | return error(Name.SourceRange.Start, |
| 636 | "alloca instruction named '" + Name.Value + |
| 637 | "' isn't defined in the function '" + F.getName() + |
| 638 | "'"); |
| 639 | } |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 640 | if (Object.Type == yaml::MachineStackObject::VariableSized) |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 641 | ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca); |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 642 | else |
| 643 | ObjectIdx = MFI.CreateStackObject( |
| 644 | Object.Size, Object.Alignment, |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 645 | Object.Type == yaml::MachineStackObject::SpillSlot, Alloca); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 646 | MFI.setObjectOffset(ObjectIdx, Object.Offset); |
Matt Arsenault | db78273 | 2017-07-20 21:03:45 +0000 | [diff] [blame] | 647 | MFI.setStackID(ObjectIdx, Object.StackID); |
| 648 | |
Alex Lorenz | c5d35ba | 2015-08-10 23:50:41 +0000 | [diff] [blame] | 649 | if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx)) |
| 650 | .second) |
| 651 | return error(Object.ID.SourceRange.Start, |
| 652 | Twine("redefinition of stack object '%stack.") + |
| 653 | Twine(Object.ID.Value) + "'"); |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 654 | if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister, |
Matthias Braun | 5c3e8a4 | 2017-09-28 18:52:14 +0000 | [diff] [blame] | 655 | Object.CalleeSavedRestored, ObjectIdx)) |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 656 | return true; |
Alex Lorenz | a56ba6a | 2015-08-17 22:17:42 +0000 | [diff] [blame] | 657 | if (Object.LocalOffset) |
| 658 | MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue()); |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 659 | if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx)) |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 660 | return true; |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 661 | } |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 662 | MFI.setCalleeSavedInfo(CSIInfo); |
| 663 | if (!CSIInfo.empty()) |
| 664 | MFI.setCalleeSavedInfoValid(true); |
Alex Lorenz | a314d81 | 2015-08-18 22:26:26 +0000 | [diff] [blame] | 665 | |
| 666 | // Initialize the various stack object references after initializing the |
| 667 | // stack objects. |
| 668 | if (!YamlMFI.StackProtector.Value.empty()) { |
| 669 | SMDiagnostic Error; |
| 670 | int FI; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 671 | if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error)) |
Alex Lorenz | a314d81 | 2015-08-18 22:26:26 +0000 | [diff] [blame] | 672 | return error(Error, YamlMFI.StackProtector.SourceRange); |
| 673 | MFI.setStackProtectorIndex(FI); |
| 674 | } |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 675 | return false; |
| 676 | } |
| 677 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 678 | bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS, |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 679 | std::vector<CalleeSavedInfo> &CSIInfo, |
Matthias Braun | 5c3e8a4 | 2017-09-28 18:52:14 +0000 | [diff] [blame] | 680 | const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) { |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 681 | if (RegisterSource.Value.empty()) |
| 682 | return false; |
| 683 | unsigned Reg = 0; |
| 684 | SMDiagnostic Error; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 685 | if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error)) |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 686 | return error(Error, RegisterSource.SourceRange); |
Matthias Braun | 5c3e8a4 | 2017-09-28 18:52:14 +0000 | [diff] [blame] | 687 | CalleeSavedInfo CSI(Reg, FrameIdx); |
| 688 | CSI.setRestored(IsRestored); |
| 689 | CSIInfo.push_back(CSI); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 690 | return false; |
| 691 | } |
| 692 | |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 693 | /// Verify that given node is of a certain type. Return true on error. |
| 694 | template <typename T> |
| 695 | static bool typecheckMDNode(T *&Result, MDNode *Node, |
| 696 | const yaml::StringValue &Source, |
| 697 | StringRef TypeString, MIRParserImpl &Parser) { |
| 698 | if (!Node) |
| 699 | return false; |
| 700 | Result = dyn_cast<T>(Node); |
| 701 | if (!Result) |
| 702 | return Parser.error(Source.SourceRange.Start, |
| 703 | "expected a reference to a '" + TypeString + |
| 704 | "' metadata node"); |
| 705 | return false; |
| 706 | } |
| 707 | |
Francis Visoiu Mistrih | 57fcd34 | 2018-04-25 18:58:06 +0000 | [diff] [blame] | 708 | template <typename T> |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 709 | bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS, |
Francis Visoiu Mistrih | 57fcd34 | 2018-04-25 18:58:06 +0000 | [diff] [blame] | 710 | const T &Object, int FrameIdx) { |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 711 | // Debug information can only be attached to stack objects; Fixed stack |
| 712 | // objects aren't supported. |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 713 | MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 714 | if (parseMDNode(PFS, Var, Object.DebugVar) || |
| 715 | parseMDNode(PFS, Expr, Object.DebugExpr) || |
| 716 | parseMDNode(PFS, Loc, Object.DebugLoc)) |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 717 | return true; |
| 718 | if (!Var && !Expr && !Loc) |
| 719 | return false; |
| 720 | DILocalVariable *DIVar = nullptr; |
| 721 | DIExpression *DIExpr = nullptr; |
| 722 | DILocation *DILoc = nullptr; |
| 723 | if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) || |
| 724 | typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) || |
| 725 | typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this)) |
| 726 | return true; |
Francis Visoiu Mistrih | 57fcd34 | 2018-04-25 18:58:06 +0000 | [diff] [blame] | 727 | PFS.MF.setVariableDbgInfo(DIVar, DIExpr, FrameIdx, DILoc); |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 728 | return false; |
| 729 | } |
| 730 | |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 731 | bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS, |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 732 | MDNode *&Node, const yaml::StringValue &Source) { |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 733 | if (Source.Value.empty()) |
| 734 | return false; |
| 735 | SMDiagnostic Error; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 736 | if (llvm::parseMDNode(PFS, Node, Source.Value, Error)) |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 737 | return error(Error, Source.SourceRange); |
| 738 | return false; |
| 739 | } |
| 740 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 741 | bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS, |
| 742 | MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) { |
| 743 | DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots; |
| 744 | const MachineFunction &MF = PFS.MF; |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 745 | const auto &M = *MF.getFunction().getParent(); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 746 | SMDiagnostic Error; |
| 747 | for (const auto &YamlConstant : YamlMF.Constants) { |
Diana Picus | d5a00b0 | 2017-08-02 11:09:30 +0000 | [diff] [blame] | 748 | if (YamlConstant.IsTargetSpecific) |
| 749 | // FIXME: Support target-specific constant pools |
| 750 | return error(YamlConstant.Value.SourceRange.Start, |
| 751 | "Can't parse target-specific constant pool entries yet"); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 752 | const Constant *Value = dyn_cast_or_null<Constant>( |
| 753 | parseConstantValue(YamlConstant.Value.Value, Error, M)); |
| 754 | if (!Value) |
| 755 | return error(Error, YamlConstant.Value.SourceRange); |
| 756 | unsigned Alignment = |
| 757 | YamlConstant.Alignment |
| 758 | ? YamlConstant.Alignment |
| 759 | : M.getDataLayout().getPrefTypeAlignment(Value->getType()); |
Alex Lorenz | 60bf599 | 2015-07-30 22:00:17 +0000 | [diff] [blame] | 760 | unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment); |
| 761 | if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index)) |
| 762 | .second) |
| 763 | return error(YamlConstant.ID.SourceRange.Start, |
| 764 | Twine("redefinition of constant pool item '%const.") + |
| 765 | Twine(YamlConstant.ID.Value) + "'"); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 766 | } |
| 767 | return false; |
| 768 | } |
| 769 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 770 | bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS, |
| 771 | const yaml::MachineJumpTable &YamlJTI) { |
| 772 | MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 773 | for (const auto &Entry : YamlJTI.Entries) { |
| 774 | std::vector<MachineBasicBlock *> Blocks; |
| 775 | for (const auto &MBBSource : Entry.Blocks) { |
| 776 | MachineBasicBlock *MBB = nullptr; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 777 | if (parseMBBReference(PFS, MBB, MBBSource.Value)) |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 778 | return true; |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 779 | Blocks.push_back(MBB); |
| 780 | } |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 781 | unsigned Index = JTI->createJumpTableIndex(Blocks); |
Alex Lorenz | 59ed591 | 2015-07-31 23:13:23 +0000 | [diff] [blame] | 782 | if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index)) |
| 783 | .second) |
| 784 | return error(Entry.ID.SourceRange.Start, |
| 785 | Twine("redefinition of jump table entry '%jump-table.") + |
| 786 | Twine(Entry.ID.Value) + "'"); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 787 | } |
| 788 | return false; |
| 789 | } |
| 790 | |
Matthias Braun | 74ad41c | 2016-10-11 03:13:01 +0000 | [diff] [blame] | 791 | bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS, |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 792 | MachineBasicBlock *&MBB, |
| 793 | const yaml::StringValue &Source) { |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 794 | SMDiagnostic Error; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 795 | if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error)) |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 796 | return error(Error, Source.SourceRange); |
| 797 | return false; |
| 798 | } |
| 799 | |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 800 | SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error, |
| 801 | SMRange SourceRange) { |
| 802 | assert(SourceRange.isValid() && "Invalid source range"); |
| 803 | SMLoc Loc = SourceRange.Start; |
| 804 | bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() && |
| 805 | *Loc.getPointer() == '\''; |
| 806 | // Translate the location of the error from the location in the MI string to |
| 807 | // the corresponding location in the MIR file. |
| 808 | Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() + |
| 809 | (HasQuote ? 1 : 0)); |
| 810 | |
| 811 | // TODO: Translate any source ranges as well. |
| 812 | return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None, |
| 813 | Error.getFixIts()); |
| 814 | } |
| 815 | |
Alex Lorenz | 9b62cf6 | 2015-08-13 20:30:11 +0000 | [diff] [blame] | 816 | SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error, |
| 817 | SMRange SourceRange) { |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 818 | assert(SourceRange.isValid()); |
| 819 | |
| 820 | // Translate the location of the error from the location in the llvm IR string |
| 821 | // to the corresponding location in the MIR file. |
| 822 | auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start); |
| 823 | unsigned Line = LineAndColumn.first + Error.getLineNo() - 1; |
| 824 | unsigned Column = Error.getColumnNo(); |
| 825 | StringRef LineStr = Error.getLineContents(); |
| 826 | SMLoc Loc = Error.getLoc(); |
| 827 | |
| 828 | // Get the full line and adjust the column number by taking the indentation of |
| 829 | // LLVM IR into account. |
| 830 | for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E; |
| 831 | L != E; ++L) { |
| 832 | if (L.line_number() == Line) { |
| 833 | LineStr = *L; |
| 834 | Loc = SMLoc::getFromPointer(LineStr.data()); |
| 835 | auto Indent = LineStr.find(Error.getLineContents()); |
| 836 | if (Indent != StringRef::npos) |
| 837 | Column += Indent; |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(), |
| 843 | Error.getMessage(), LineStr, Error.getRanges(), |
| 844 | Error.getFixIts()); |
| 845 | } |
| 846 | |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 847 | void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) { |
| 848 | if (!Names2RegClasses.empty()) |
| 849 | return; |
| 850 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 851 | for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) { |
| 852 | const auto *RC = TRI->getRegClass(I); |
| 853 | Names2RegClasses.insert( |
| 854 | std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC)); |
| 855 | } |
| 856 | } |
| 857 | |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 858 | void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) { |
| 859 | if (!Names2RegBanks.empty()) |
| 860 | return; |
| 861 | const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo(); |
| 862 | // If the target does not support GlobalISel, we may not have a |
| 863 | // register bank info. |
| 864 | if (!RBI) |
| 865 | return; |
| 866 | for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) { |
| 867 | const auto &RegBank = RBI->getRegBank(I); |
| 868 | Names2RegBanks.insert( |
| 869 | std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank)); |
| 870 | } |
| 871 | } |
| 872 | |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 873 | const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF, |
| 874 | StringRef Name) { |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 875 | auto RegClassInfo = Names2RegClasses.find(Name); |
| 876 | if (RegClassInfo == Names2RegClasses.end()) |
| 877 | return nullptr; |
| 878 | return RegClassInfo->getValue(); |
| 879 | } |
| 880 | |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 881 | const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF, |
| 882 | StringRef Name) { |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 883 | auto RegBankInfo = Names2RegBanks.find(Name); |
| 884 | if (RegBankInfo == Names2RegBanks.end()) |
| 885 | return nullptr; |
| 886 | return RegBankInfo->getValue(); |
| 887 | } |
| 888 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 889 | MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl) |
| 890 | : Impl(std::move(Impl)) {} |
| 891 | |
| 892 | MIRParser::~MIRParser() {} |
| 893 | |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 894 | std::unique_ptr<Module> MIRParser::parseIRModule() { |
| 895 | return Impl->parseIRModule(); |
| 896 | } |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 897 | |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 898 | bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) { |
| 899 | return Impl->parseMachineFunctions(M, MMI); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename, |
| 903 | SMDiagnostic &Error, |
| 904 | LLVMContext &Context) { |
Matthias Braun | 7e23fc0 | 2017-06-06 20:06:57 +0000 | [diff] [blame] | 905 | auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 906 | if (std::error_code EC = FileOrErr.getError()) { |
| 907 | Error = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 908 | "Could not open input file: " + EC.message()); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 909 | return nullptr; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 910 | } |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 911 | return createMIRParser(std::move(FileOrErr.get()), Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 914 | std::unique_ptr<MIRParser> |
| 915 | llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents, |
| 916 | LLVMContext &Context) { |
Mehdi Amini | 05188a6 | 2016-09-17 05:41:02 +0000 | [diff] [blame] | 917 | auto Filename = Contents->getBufferIdentifier(); |
Mehdi Amini | 8d904e9 | 2016-09-17 05:33:58 +0000 | [diff] [blame] | 918 | if (Context.shouldDiscardValueNames()) { |
| 919 | Context.diagnose(DiagnosticInfoMIRParser( |
| 920 | DS_Error, |
| 921 | SMDiagnostic( |
| 922 | Filename, SourceMgr::DK_Error, |
| 923 | "Can't read MIR with a Context that discards named Values"))); |
| 924 | return nullptr; |
| 925 | } |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 926 | return llvm::make_unique<MIRParser>( |
| 927 | llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context)); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 928 | } |