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