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