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