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