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