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; |
| 53 | StringRef Filename; |
| 54 | LLVMContext &Context; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 55 | StringMap<std::unique_ptr<yaml::MachineFunction>> Functions; |
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. |
| 58 | StringMap<const TargetRegisterClass *> Names2RegClasses; |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 59 | /// Maps from register bank names to register banks. |
| 60 | StringMap<const RegisterBank *> Names2RegBanks; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 61 | |
| 62 | public: |
| 63 | MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename, |
| 64 | LLVMContext &Context); |
| 65 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 66 | void reportDiagnostic(const SMDiagnostic &Diag); |
| 67 | |
| 68 | /// Report an error with the given message at unknown location. |
| 69 | /// |
| 70 | /// Always returns true. |
| 71 | bool error(const Twine &Message); |
| 72 | |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 73 | /// Report an error with the given message at the given location. |
| 74 | /// |
| 75 | /// Always returns true. |
| 76 | bool error(SMLoc Loc, const Twine &Message); |
| 77 | |
Alex Lorenz | 0fd7c62 | 2015-06-30 17:55:00 +0000 | [diff] [blame] | 78 | /// Report a given error with the location translated from the location in an |
| 79 | /// embedded string literal to a location in the MIR file. |
| 80 | /// |
| 81 | /// Always returns true. |
| 82 | bool error(const SMDiagnostic &Error, SMRange SourceRange); |
| 83 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 84 | /// Try to parse the optional LLVM module and the machine functions in the MIR |
| 85 | /// file. |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 86 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 87 | /// Return null if an error occurred. |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 88 | std::unique_ptr<Module> parse(); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 89 | |
| 90 | /// Parse the machine function in the current YAML document. |
| 91 | /// |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 92 | /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR. |
| 93 | /// A dummy IR function is created and inserted into the given module when |
| 94 | /// this parameter is true. |
| 95 | /// |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 96 | /// Return true if an error occurred. |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 97 | bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 98 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 99 | /// Initialize the machine function to the state that's described in the MIR |
| 100 | /// file. |
| 101 | /// |
| 102 | /// Return true if error occurred. |
| 103 | bool initializeMachineFunction(MachineFunction &MF); |
| 104 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 105 | bool initializeRegisterInfo(PerFunctionMIParsingState &PFS, |
| 106 | const yaml::MachineFunction &YamlMF); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 107 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 108 | void inferRegisterInfo(const PerFunctionMIParsingState &PFS, |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 109 | const yaml::MachineFunction &YamlMF); |
| 110 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 111 | bool initializeFrameInfo(PerFunctionMIParsingState &PFS, |
| 112 | const yaml::MachineFunction &YamlMF); |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 113 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 114 | bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS, |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 115 | std::vector<CalleeSavedInfo> &CSIInfo, |
| 116 | const yaml::StringValue &RegisterSource, |
| 117 | int FrameIdx); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 118 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 119 | bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS, |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 120 | const yaml::MachineStackObject &Object, |
| 121 | int FrameIdx); |
| 122 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 123 | bool initializeConstantPool(PerFunctionMIParsingState &PFS, |
| 124 | MachineConstantPool &ConstantPool, |
| 125 | const yaml::MachineFunction &YamlMF); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 126 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 127 | bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS, |
| 128 | const yaml::MachineJumpTable &YamlJTI); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 129 | |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 130 | private: |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 131 | bool parseMDNode(const PerFunctionMIParsingState &PFS, MDNode *&Node, |
| 132 | const yaml::StringValue &Source); |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 133 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 134 | bool parseMBBReference(const PerFunctionMIParsingState &PFS, |
| 135 | MachineBasicBlock *&MBB, |
| 136 | const yaml::StringValue &Source); |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 137 | |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 138 | /// Return a MIR diagnostic converted from an MI string diagnostic. |
| 139 | SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error, |
| 140 | SMRange SourceRange); |
| 141 | |
Alex Lorenz | 9b62cf6 | 2015-08-13 20:30:11 +0000 | [diff] [blame] | 142 | /// Return a MIR diagnostic converted from a diagnostic located in a YAML |
| 143 | /// block scalar string. |
| 144 | SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error, |
| 145 | SMRange SourceRange); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 146 | |
| 147 | /// Create an empty function with the given name. |
| 148 | void createDummyFunction(StringRef Name, Module &M); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 149 | |
| 150 | void initNames2RegClasses(const MachineFunction &MF); |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 151 | void initNames2RegBanks(const MachineFunction &MF); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 152 | |
| 153 | /// Check if the given identifier is a name of a register class. |
| 154 | /// |
| 155 | /// Return null if the name isn't a register class. |
| 156 | const TargetRegisterClass *getRegClass(const MachineFunction &MF, |
| 157 | StringRef Name); |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 158 | |
| 159 | /// Check if the given identifier is a name of a register bank. |
| 160 | /// |
| 161 | /// Return null if the name isn't a register bank. |
| 162 | const RegisterBank *getRegBank(const MachineFunction &MF, StringRef Name); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 165 | } // end namespace llvm |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 166 | |
| 167 | MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, |
| 168 | StringRef Filename, LLVMContext &Context) |
| 169 | : SM(), Filename(Filename), Context(Context) { |
| 170 | SM.AddNewSourceBuffer(std::move(Contents), SMLoc()); |
| 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; |
| 203 | } |
| 204 | Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag)); |
| 205 | } |
| 206 | |
| 207 | static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { |
| 208 | reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag); |
| 209 | } |
| 210 | |
| 211 | std::unique_ptr<Module> MIRParserImpl::parse() { |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 212 | yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(), |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 213 | /*Ctxt=*/nullptr, handleYAMLDiag, this); |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 214 | In.setContext(&In); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 215 | |
| 216 | if (!In.setCurrentDocument()) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 217 | if (In.error()) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 218 | return nullptr; |
| 219 | // Create an empty module when the MIR file is empty. |
| 220 | return llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 223 | std::unique_ptr<Module> M; |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 224 | bool NoLLVMIR = false; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 225 | // Parse the block scalar manually so that we can return unique pointer |
| 226 | // without having to go trough YAML traits. |
| 227 | if (const auto *BSN = |
| 228 | dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 229 | SMDiagnostic Error; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 230 | M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error, |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 231 | Context, &IRSlots); |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 232 | if (!M) { |
Alex Lorenz | 9b62cf6 | 2015-08-13 20:30:11 +0000 | [diff] [blame] | 233 | reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange())); |
Matthias Braun | d6f9562 | 2016-07-14 00:42:37 +0000 | [diff] [blame^] | 234 | return nullptr; |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 235 | } |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 236 | In.nextDocument(); |
| 237 | if (!In.setCurrentDocument()) |
| 238 | return M; |
| 239 | } else { |
| 240 | // Create an new, empty module. |
| 241 | M = llvm::make_unique<Module>(Filename, Context); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 242 | NoLLVMIR = true; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Parse the machine functions. |
| 246 | do { |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 247 | if (parseMachineFunction(In, *M, NoLLVMIR)) |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 248 | return nullptr; |
| 249 | In.nextDocument(); |
| 250 | } while (In.setCurrentDocument()); |
| 251 | |
| 252 | return M; |
| 253 | } |
| 254 | |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 255 | bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M, |
| 256 | bool NoLLVMIR) { |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 257 | auto MF = llvm::make_unique<yaml::MachineFunction>(); |
| 258 | yaml::yamlize(In, *MF, false); |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 259 | if (In.error()) |
| 260 | return true; |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 261 | auto FunctionName = MF->Name; |
Alex Lorenz | fe2aa97 | 2015-06-15 22:23:23 +0000 | [diff] [blame] | 262 | if (Functions.find(FunctionName) != Functions.end()) |
| 263 | return error(Twine("redefinition of machine function '") + FunctionName + |
| 264 | "'"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 265 | Functions.insert(std::make_pair(FunctionName, std::move(MF))); |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 266 | if (NoLLVMIR) |
| 267 | createDummyFunction(FunctionName, M); |
Alex Lorenz | 5ef16b8 | 2015-06-16 17:06:29 +0000 | [diff] [blame] | 268 | else if (!M.getFunction(FunctionName)) |
| 269 | return error(Twine("function '") + FunctionName + |
| 270 | "' isn't defined in the provided LLVM IR"); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 271 | return false; |
| 272 | } |
| 273 | |
Alex Lorenz | 8e7a58d7 | 2015-06-15 23:07:38 +0000 | [diff] [blame] | 274 | void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) { |
| 275 | auto &Context = M.getContext(); |
| 276 | Function *F = cast<Function>(M.getOrInsertFunction( |
| 277 | Name, FunctionType::get(Type::getVoidTy(Context), false))); |
| 278 | BasicBlock *BB = BasicBlock::Create(Context, "entry", F); |
| 279 | new UnreachableInst(Context, BB); |
| 280 | } |
| 281 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 282 | bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) { |
| 283 | auto It = Functions.find(MF.getName()); |
| 284 | if (It == Functions.end()) |
| 285 | return error(Twine("no machine function information for function '") + |
| 286 | MF.getName() + "' in the MIR file"); |
| 287 | // TODO: Recreate the machine function. |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 288 | const yaml::MachineFunction &YamlMF = *It->getValue(); |
| 289 | if (YamlMF.Alignment) |
| 290 | MF.setAlignment(YamlMF.Alignment); |
| 291 | MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice); |
| 292 | MF.setHasInlineAsm(YamlMF.HasInlineAsm); |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 293 | if (YamlMF.AllVRegsAllocated) |
| 294 | MF.getProperties().set(MachineFunctionProperties::Property::AllVRegsAllocated); |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 295 | PerFunctionMIParsingState PFS(MF, SM, IRSlots); |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 296 | if (initializeRegisterInfo(PFS, YamlMF)) |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 297 | return true; |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 298 | if (!YamlMF.Constants.empty()) { |
| 299 | auto *ConstantPool = MF.getConstantPool(); |
| 300 | assert(ConstantPool && "Constant pool must be created"); |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 301 | if (initializeConstantPool(PFS, *ConstantPool, YamlMF)) |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 302 | return true; |
| 303 | } |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 304 | |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 305 | StringRef BlockStr = YamlMF.Body.Value.Value; |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 306 | SMDiagnostic Error; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 307 | SourceMgr BlockSM; |
| 308 | BlockSM.AddNewSourceBuffer( |
| 309 | MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false), |
| 310 | SMLoc()); |
| 311 | PFS.SM = &BlockSM; |
| 312 | if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) { |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 313 | reportDiagnostic( |
| 314 | diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange)); |
| 315 | return true; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 316 | } |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 317 | PFS.SM = &SM; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 318 | |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 319 | if (MF.empty()) |
Alex Lorenz | c8704b0 | 2015-07-09 21:21:33 +0000 | [diff] [blame] | 320 | return error(Twine("machine function '") + Twine(MF.getName()) + |
| 321 | "' requires at least one machine basic block in its body"); |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 322 | // Initialize the frame information after creating all the MBBs so that the |
| 323 | // MBB references in the frame information can be resolved. |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 324 | if (initializeFrameInfo(PFS, YamlMF)) |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 325 | return true; |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 326 | // Initialize the jump table after creating all the MBBs so that the MBB |
| 327 | // references can be resolved. |
| 328 | if (!YamlMF.JumpTableInfo.Entries.empty() && |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 329 | initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo)) |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 330 | return true; |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 331 | // Parse the machine instructions after creating all of the MBBs so that the |
| 332 | // parser can resolve the MBB references. |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 333 | StringRef InsnStr = YamlMF.Body.Value.Value; |
| 334 | SourceMgr InsnSM; |
| 335 | InsnSM.AddNewSourceBuffer( |
| 336 | MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false), |
| 337 | SMLoc()); |
| 338 | PFS.SM = &InsnSM; |
| 339 | if (parseMachineInstructions(PFS, InsnStr, Error)) { |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame] | 340 | reportDiagnostic( |
| 341 | diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange)); |
| 342 | return true; |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 343 | } |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 344 | PFS.SM = &SM; |
| 345 | |
| 346 | inferRegisterInfo(PFS, YamlMF); |
Alex Lorenz | c7bf204 | 2015-07-24 17:44:49 +0000 | [diff] [blame] | 347 | // FIXME: This is a temporary workaround until the reserved registers can be |
| 348 | // serialized. |
| 349 | MF.getRegInfo().freezeReservedRegs(MF); |
| 350 | MF.verify(); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 354 | bool MIRParserImpl::initializeRegisterInfo(PerFunctionMIParsingState &PFS, |
| 355 | const yaml::MachineFunction &YamlMF) { |
| 356 | MachineFunction &MF = PFS.MF; |
Alex Lorenz | db07c40 | 2015-07-28 16:48:37 +0000 | [diff] [blame] | 357 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 358 | assert(RegInfo.isSSA()); |
| 359 | if (!YamlMF.IsSSA) |
| 360 | RegInfo.leaveSSA(); |
| 361 | assert(RegInfo.tracksLiveness()); |
| 362 | if (!YamlMF.TracksRegLiveness) |
| 363 | RegInfo.invalidateLiveness(); |
| 364 | RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 365 | |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 366 | SMDiagnostic Error; |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 367 | // Parse the virtual register information. |
| 368 | for (const auto &VReg : YamlMF.VirtualRegisters) { |
Quentin Colombet | 050b211 | 2016-03-08 01:17:03 +0000 | [diff] [blame] | 369 | unsigned Reg; |
| 370 | if (StringRef(VReg.Class.Value).equals("_")) { |
| 371 | // This is a generic virtual register. |
| 372 | // The size will be set appropriately when we reach the definition. |
| 373 | Reg = RegInfo.createGenericVirtualRegister(/*Size*/ 1); |
Quentin Colombet | 2c64696 | 2016-06-08 23:27:46 +0000 | [diff] [blame] | 374 | PFS.GenericVRegs.insert(Reg); |
Quentin Colombet | 050b211 | 2016-03-08 01:17:03 +0000 | [diff] [blame] | 375 | } else { |
| 376 | const auto *RC = getRegClass(MF, VReg.Class.Value); |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 377 | if (RC) { |
| 378 | Reg = RegInfo.createVirtualRegister(RC); |
| 379 | } else { |
| 380 | const auto *RegBank = getRegBank(MF, VReg.Class.Value); |
| 381 | if (!RegBank) |
| 382 | return error( |
| 383 | VReg.Class.SourceRange.Start, |
| 384 | Twine("use of undefined register class or register bank '") + |
| 385 | VReg.Class.Value + "'"); |
| 386 | Reg = RegInfo.createGenericVirtualRegister(/*Size*/ 1); |
| 387 | RegInfo.setRegBank(Reg, *RegBank); |
Quentin Colombet | 2c64696 | 2016-06-08 23:27:46 +0000 | [diff] [blame] | 388 | PFS.GenericVRegs.insert(Reg); |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 389 | } |
Quentin Colombet | 050b211 | 2016-03-08 01:17:03 +0000 | [diff] [blame] | 390 | } |
Alex Lorenz | a06c0c6 | 2015-07-30 21:54:10 +0000 | [diff] [blame] | 391 | if (!PFS.VirtualRegisterSlots.insert(std::make_pair(VReg.ID.Value, Reg)) |
| 392 | .second) |
| 393 | return error(VReg.ID.SourceRange.Start, |
| 394 | Twine("redefinition of virtual register '%") + |
| 395 | Twine(VReg.ID.Value) + "'"); |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 396 | if (!VReg.PreferredRegister.Value.empty()) { |
| 397 | unsigned PreferredReg = 0; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 398 | if (parseNamedRegisterReference(PFS, PreferredReg, |
| 399 | VReg.PreferredRegister.Value, Error)) |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 400 | return error(Error, VReg.PreferredRegister.SourceRange); |
| 401 | RegInfo.setSimpleHint(Reg, PreferredReg); |
| 402 | } |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 403 | } |
Alex Lorenz | 12045a4 | 2015-07-27 17:42:45 +0000 | [diff] [blame] | 404 | |
| 405 | // Parse the liveins. |
| 406 | for (const auto &LiveIn : YamlMF.LiveIns) { |
| 407 | unsigned Reg = 0; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 408 | if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error)) |
Alex Lorenz | 12045a4 | 2015-07-27 17:42:45 +0000 | [diff] [blame] | 409 | return error(Error, LiveIn.Register.SourceRange); |
| 410 | unsigned VReg = 0; |
| 411 | if (!LiveIn.VirtualRegister.Value.empty()) { |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 412 | if (parseVirtualRegisterReference(PFS, VReg, LiveIn.VirtualRegister.Value, |
| 413 | Error)) |
Alex Lorenz | 12045a4 | 2015-07-27 17:42:45 +0000 | [diff] [blame] | 414 | return error(Error, LiveIn.VirtualRegister.SourceRange); |
| 415 | } |
| 416 | RegInfo.addLiveIn(Reg, VReg); |
| 417 | } |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 418 | |
| 419 | // Parse the callee saved register mask. |
| 420 | BitVector CalleeSavedRegisterMask(RegInfo.getUsedPhysRegsMask().size()); |
| 421 | if (!YamlMF.CalleeSavedRegisters) |
| 422 | return false; |
| 423 | for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) { |
| 424 | unsigned Reg = 0; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 425 | if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error)) |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 426 | return error(Error, RegSource.SourceRange); |
| 427 | CalleeSavedRegisterMask[Reg] = true; |
| 428 | } |
| 429 | RegInfo.setUsedPhysRegMask(CalleeSavedRegisterMask.flip()); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 430 | return false; |
| 431 | } |
| 432 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 433 | void MIRParserImpl::inferRegisterInfo(const PerFunctionMIParsingState &PFS, |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 434 | const yaml::MachineFunction &YamlMF) { |
| 435 | if (YamlMF.CalleeSavedRegisters) |
| 436 | return; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 437 | MachineRegisterInfo &MRI = PFS.MF.getRegInfo(); |
| 438 | for (const MachineBasicBlock &MBB : PFS.MF) { |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 439 | for (const MachineInstr &MI : MBB) { |
| 440 | for (const MachineOperand &MO : MI.operands()) { |
| 441 | if (!MO.isRegMask()) |
| 442 | continue; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 443 | MRI.addPhysRegsUsedFromRegMask(MO.getRegMask()); |
Alex Lorenz | c483808 | 2015-08-11 00:32:49 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 449 | bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS, |
| 450 | const yaml::MachineFunction &YamlMF) { |
| 451 | MachineFunction &MF = PFS.MF; |
Alex Lorenz | db07c40 | 2015-07-28 16:48:37 +0000 | [diff] [blame] | 452 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 453 | const Function &F = *MF.getFunction(); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 454 | const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo; |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 455 | MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken); |
| 456 | MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken); |
| 457 | MFI.setHasStackMap(YamlMFI.HasStackMap); |
| 458 | MFI.setHasPatchPoint(YamlMFI.HasPatchPoint); |
| 459 | MFI.setStackSize(YamlMFI.StackSize); |
| 460 | MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment); |
| 461 | if (YamlMFI.MaxAlignment) |
| 462 | MFI.ensureMaxAlignment(YamlMFI.MaxAlignment); |
| 463 | MFI.setAdjustsStack(YamlMFI.AdjustsStack); |
| 464 | MFI.setHasCalls(YamlMFI.HasCalls); |
| 465 | MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize); |
| 466 | MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment); |
| 467 | MFI.setHasVAStart(YamlMFI.HasVAStart); |
| 468 | MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc); |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 469 | if (!YamlMFI.SavePoint.Value.empty()) { |
| 470 | MachineBasicBlock *MBB = nullptr; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 471 | if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint)) |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 472 | return true; |
| 473 | MFI.setSavePoint(MBB); |
| 474 | } |
| 475 | if (!YamlMFI.RestorePoint.Value.empty()) { |
| 476 | MachineBasicBlock *MBB = nullptr; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 477 | if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint)) |
Alex Lorenz | a6f9a37 | 2015-07-29 21:09:09 +0000 | [diff] [blame] | 478 | return true; |
| 479 | MFI.setRestorePoint(MBB); |
| 480 | } |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 481 | |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 482 | std::vector<CalleeSavedInfo> CSIInfo; |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 483 | // Initialize the fixed frame objects. |
| 484 | for (const auto &Object : YamlMF.FixedStackObjects) { |
| 485 | int ObjectIdx; |
| 486 | if (Object.Type != yaml::FixedMachineStackObject::SpillSlot) |
| 487 | ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset, |
| 488 | Object.IsImmutable, Object.IsAliased); |
| 489 | else |
| 490 | ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset); |
| 491 | MFI.setObjectAlignment(ObjectIdx, Object.Alignment); |
Alex Lorenz | 1d9a303 | 2015-08-10 23:45:02 +0000 | [diff] [blame] | 492 | if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value, |
| 493 | ObjectIdx)) |
| 494 | .second) |
| 495 | return error(Object.ID.SourceRange.Start, |
| 496 | Twine("redefinition of fixed stack object '%fixed-stack.") + |
| 497 | Twine(Object.ID.Value) + "'"); |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 498 | if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister, |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 499 | ObjectIdx)) |
| 500 | return true; |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | // Initialize the ordinary frame objects. |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 504 | for (const auto &Object : YamlMF.StackObjects) { |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 505 | int ObjectIdx; |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 506 | const AllocaInst *Alloca = nullptr; |
| 507 | const yaml::StringValue &Name = Object.Name; |
| 508 | if (!Name.Value.empty()) { |
| 509 | Alloca = dyn_cast_or_null<AllocaInst>( |
| 510 | F.getValueSymbolTable().lookup(Name.Value)); |
| 511 | if (!Alloca) |
| 512 | return error(Name.SourceRange.Start, |
| 513 | "alloca instruction named '" + Name.Value + |
| 514 | "' isn't defined in the function '" + F.getName() + |
| 515 | "'"); |
| 516 | } |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 517 | if (Object.Type == yaml::MachineStackObject::VariableSized) |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 518 | ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca); |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 519 | else |
| 520 | ObjectIdx = MFI.CreateStackObject( |
| 521 | Object.Size, Object.Alignment, |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 522 | Object.Type == yaml::MachineStackObject::SpillSlot, Alloca); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 523 | MFI.setObjectOffset(ObjectIdx, Object.Offset); |
Alex Lorenz | c5d35ba | 2015-08-10 23:50:41 +0000 | [diff] [blame] | 524 | if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx)) |
| 525 | .second) |
| 526 | return error(Object.ID.SourceRange.Start, |
| 527 | Twine("redefinition of stack object '%stack.") + |
| 528 | Twine(Object.ID.Value) + "'"); |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 529 | if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister, |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 530 | ObjectIdx)) |
| 531 | return true; |
Alex Lorenz | a56ba6a | 2015-08-17 22:17:42 +0000 | [diff] [blame] | 532 | if (Object.LocalOffset) |
| 533 | MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue()); |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 534 | if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx)) |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 535 | return true; |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 536 | } |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 537 | MFI.setCalleeSavedInfo(CSIInfo); |
| 538 | if (!CSIInfo.empty()) |
| 539 | MFI.setCalleeSavedInfoValid(true); |
Alex Lorenz | a314d81 | 2015-08-18 22:26:26 +0000 | [diff] [blame] | 540 | |
| 541 | // Initialize the various stack object references after initializing the |
| 542 | // stack objects. |
| 543 | if (!YamlMFI.StackProtector.Value.empty()) { |
| 544 | SMDiagnostic Error; |
| 545 | int FI; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 546 | if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error)) |
Alex Lorenz | a314d81 | 2015-08-18 22:26:26 +0000 | [diff] [blame] | 547 | return error(Error, YamlMFI.StackProtector.SourceRange); |
| 548 | MFI.setStackProtectorIndex(FI); |
| 549 | } |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 550 | return false; |
| 551 | } |
| 552 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 553 | bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS, |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 554 | std::vector<CalleeSavedInfo> &CSIInfo, |
| 555 | const yaml::StringValue &RegisterSource, int FrameIdx) { |
| 556 | if (RegisterSource.Value.empty()) |
| 557 | return false; |
| 558 | unsigned Reg = 0; |
| 559 | SMDiagnostic Error; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 560 | if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error)) |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 561 | return error(Error, RegisterSource.SourceRange); |
| 562 | CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx)); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 563 | return false; |
| 564 | } |
| 565 | |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 566 | /// Verify that given node is of a certain type. Return true on error. |
| 567 | template <typename T> |
| 568 | static bool typecheckMDNode(T *&Result, MDNode *Node, |
| 569 | const yaml::StringValue &Source, |
| 570 | StringRef TypeString, MIRParserImpl &Parser) { |
| 571 | if (!Node) |
| 572 | return false; |
| 573 | Result = dyn_cast<T>(Node); |
| 574 | if (!Result) |
| 575 | return Parser.error(Source.SourceRange.Start, |
| 576 | "expected a reference to a '" + TypeString + |
| 577 | "' metadata node"); |
| 578 | return false; |
| 579 | } |
| 580 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 581 | bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS, |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 582 | const yaml::MachineStackObject &Object, int FrameIdx) { |
| 583 | // Debug information can only be attached to stack objects; Fixed stack |
| 584 | // objects aren't supported. |
| 585 | assert(FrameIdx >= 0 && "Expected a stack object frame index"); |
| 586 | MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 587 | if (parseMDNode(PFS, Var, Object.DebugVar) || |
| 588 | parseMDNode(PFS, Expr, Object.DebugExpr) || |
| 589 | parseMDNode(PFS, Loc, Object.DebugLoc)) |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 590 | return true; |
| 591 | if (!Var && !Expr && !Loc) |
| 592 | return false; |
| 593 | DILocalVariable *DIVar = nullptr; |
| 594 | DIExpression *DIExpr = nullptr; |
| 595 | DILocation *DILoc = nullptr; |
| 596 | if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) || |
| 597 | typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) || |
| 598 | typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this)) |
| 599 | return true; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 600 | PFS.MF.getMMI().setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc); |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 601 | return false; |
| 602 | } |
| 603 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 604 | bool MIRParserImpl::parseMDNode(const PerFunctionMIParsingState &PFS, |
| 605 | MDNode *&Node, const yaml::StringValue &Source) { |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 606 | if (Source.Value.empty()) |
| 607 | return false; |
| 608 | SMDiagnostic Error; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 609 | if (llvm::parseMDNode(PFS, Node, Source.Value, Error)) |
Alex Lorenz | df9e3c6 | 2015-08-19 00:13:25 +0000 | [diff] [blame] | 610 | return error(Error, Source.SourceRange); |
| 611 | return false; |
| 612 | } |
| 613 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 614 | bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS, |
| 615 | MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) { |
| 616 | DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots; |
| 617 | const MachineFunction &MF = PFS.MF; |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 618 | const auto &M = *MF.getFunction()->getParent(); |
| 619 | SMDiagnostic Error; |
| 620 | for (const auto &YamlConstant : YamlMF.Constants) { |
| 621 | const Constant *Value = dyn_cast_or_null<Constant>( |
| 622 | parseConstantValue(YamlConstant.Value.Value, Error, M)); |
| 623 | if (!Value) |
| 624 | return error(Error, YamlConstant.Value.SourceRange); |
| 625 | unsigned Alignment = |
| 626 | YamlConstant.Alignment |
| 627 | ? YamlConstant.Alignment |
| 628 | : M.getDataLayout().getPrefTypeAlignment(Value->getType()); |
Alex Lorenz | 60bf599 | 2015-07-30 22:00:17 +0000 | [diff] [blame] | 629 | unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment); |
| 630 | if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index)) |
| 631 | .second) |
| 632 | return error(YamlConstant.ID.SourceRange.Start, |
| 633 | Twine("redefinition of constant pool item '%const.") + |
| 634 | Twine(YamlConstant.ID.Value) + "'"); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 635 | } |
| 636 | return false; |
| 637 | } |
| 638 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 639 | bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS, |
| 640 | const yaml::MachineJumpTable &YamlJTI) { |
| 641 | MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 642 | for (const auto &Entry : YamlJTI.Entries) { |
| 643 | std::vector<MachineBasicBlock *> Blocks; |
| 644 | for (const auto &MBBSource : Entry.Blocks) { |
| 645 | MachineBasicBlock *MBB = nullptr; |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 646 | if (parseMBBReference(PFS, MBB, MBBSource.Value)) |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 647 | return true; |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 648 | Blocks.push_back(MBB); |
| 649 | } |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 650 | unsigned Index = JTI->createJumpTableIndex(Blocks); |
Alex Lorenz | 59ed591 | 2015-07-31 23:13:23 +0000 | [diff] [blame] | 651 | if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index)) |
| 652 | .second) |
| 653 | return error(Entry.ID.SourceRange.Start, |
| 654 | Twine("redefinition of jump table entry '%jump-table.") + |
| 655 | Twine(Entry.ID.Value) + "'"); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 656 | } |
| 657 | return false; |
| 658 | } |
| 659 | |
Matthias Braun | 8394786 | 2016-07-13 22:23:23 +0000 | [diff] [blame] | 660 | bool MIRParserImpl::parseMBBReference(const PerFunctionMIParsingState &PFS, |
| 661 | MachineBasicBlock *&MBB, |
| 662 | const yaml::StringValue &Source) { |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 663 | SMDiagnostic Error; |
Matthias Braun | e35861d | 2016-07-13 23:27:50 +0000 | [diff] [blame] | 664 | if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error)) |
Alex Lorenz | 05fa73b | 2015-07-29 20:57:11 +0000 | [diff] [blame] | 665 | return error(Error, Source.SourceRange); |
| 666 | return false; |
| 667 | } |
| 668 | |
Alex Lorenz | 51af160 | 2015-06-23 22:39:23 +0000 | [diff] [blame] | 669 | SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error, |
| 670 | SMRange SourceRange) { |
| 671 | assert(SourceRange.isValid() && "Invalid source range"); |
| 672 | SMLoc Loc = SourceRange.Start; |
| 673 | bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() && |
| 674 | *Loc.getPointer() == '\''; |
| 675 | // Translate the location of the error from the location in the MI string to |
| 676 | // the corresponding location in the MIR file. |
| 677 | Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() + |
| 678 | (HasQuote ? 1 : 0)); |
| 679 | |
| 680 | // TODO: Translate any source ranges as well. |
| 681 | return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None, |
| 682 | Error.getFixIts()); |
| 683 | } |
| 684 | |
Alex Lorenz | 9b62cf6 | 2015-08-13 20:30:11 +0000 | [diff] [blame] | 685 | SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error, |
| 686 | SMRange SourceRange) { |
Alex Lorenz | 09b832c | 2015-05-29 17:05:41 +0000 | [diff] [blame] | 687 | assert(SourceRange.isValid()); |
| 688 | |
| 689 | // Translate the location of the error from the location in the llvm IR string |
| 690 | // to the corresponding location in the MIR file. |
| 691 | auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start); |
| 692 | unsigned Line = LineAndColumn.first + Error.getLineNo() - 1; |
| 693 | unsigned Column = Error.getColumnNo(); |
| 694 | StringRef LineStr = Error.getLineContents(); |
| 695 | SMLoc Loc = Error.getLoc(); |
| 696 | |
| 697 | // Get the full line and adjust the column number by taking the indentation of |
| 698 | // LLVM IR into account. |
| 699 | for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E; |
| 700 | L != E; ++L) { |
| 701 | if (L.line_number() == Line) { |
| 702 | LineStr = *L; |
| 703 | Loc = SMLoc::getFromPointer(LineStr.data()); |
| 704 | auto Indent = LineStr.find(Error.getLineContents()); |
| 705 | if (Indent != StringRef::npos) |
| 706 | Column += Indent; |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(), |
| 712 | Error.getMessage(), LineStr, Error.getRanges(), |
| 713 | Error.getFixIts()); |
| 714 | } |
| 715 | |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 716 | void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) { |
| 717 | if (!Names2RegClasses.empty()) |
| 718 | return; |
| 719 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 720 | for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) { |
| 721 | const auto *RC = TRI->getRegClass(I); |
| 722 | Names2RegClasses.insert( |
| 723 | std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC)); |
| 724 | } |
| 725 | } |
| 726 | |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 727 | void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) { |
| 728 | if (!Names2RegBanks.empty()) |
| 729 | return; |
| 730 | const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo(); |
| 731 | // If the target does not support GlobalISel, we may not have a |
| 732 | // register bank info. |
| 733 | if (!RBI) |
| 734 | return; |
| 735 | for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) { |
| 736 | const auto &RegBank = RBI->getRegBank(I); |
| 737 | Names2RegBanks.insert( |
| 738 | std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank)); |
| 739 | } |
| 740 | } |
| 741 | |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 742 | const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF, |
| 743 | StringRef Name) { |
| 744 | initNames2RegClasses(MF); |
| 745 | auto RegClassInfo = Names2RegClasses.find(Name); |
| 746 | if (RegClassInfo == Names2RegClasses.end()) |
| 747 | return nullptr; |
| 748 | return RegClassInfo->getValue(); |
| 749 | } |
| 750 | |
Quentin Colombet | 876ddf8 | 2016-04-08 16:40:43 +0000 | [diff] [blame] | 751 | const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF, |
| 752 | StringRef Name) { |
| 753 | initNames2RegBanks(MF); |
| 754 | auto RegBankInfo = Names2RegBanks.find(Name); |
| 755 | if (RegBankInfo == Names2RegBanks.end()) |
| 756 | return nullptr; |
| 757 | return RegBankInfo->getValue(); |
| 758 | } |
| 759 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 760 | MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl) |
| 761 | : Impl(std::move(Impl)) {} |
| 762 | |
| 763 | MIRParser::~MIRParser() {} |
| 764 | |
| 765 | std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); } |
| 766 | |
| 767 | bool MIRParser::initializeMachineFunction(MachineFunction &MF) { |
| 768 | return Impl->initializeMachineFunction(MF); |
| 769 | } |
| 770 | |
| 771 | std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename, |
| 772 | SMDiagnostic &Error, |
| 773 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 774 | auto FileOrErr = MemoryBuffer::getFile(Filename); |
| 775 | if (std::error_code EC = FileOrErr.getError()) { |
| 776 | Error = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 777 | "Could not open input file: " + EC.message()); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 778 | return nullptr; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 779 | } |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 780 | return createMIRParser(std::move(FileOrErr.get()), Context); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 783 | std::unique_ptr<MIRParser> |
| 784 | llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents, |
| 785 | LLVMContext &Context) { |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 786 | auto Filename = Contents->getBufferIdentifier(); |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 787 | return llvm::make_unique<MIRParser>( |
| 788 | llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context)); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 789 | } |