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