From e35861d67b8b5629bbef8db3367a0a9d6f38ff91 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Wed, 13 Jul 2016 23:27:50 +0000 Subject: [PATCH] MIRParser: Move SlotMapping and SourceMgr refs to PFS; NFC Code cleanup: Move references to SlotMapping and SourceMgr into the PerFunctionMIParsingState to avoid unnecessary passing around in parameters. llvm-svn: 275342 --- llvm/lib/CodeGen/MIRParser/MIParser.cpp | 81 ++++++++++++-------------------- llvm/lib/CodeGen/MIRParser/MIParser.h | 27 +++++------ llvm/lib/CodeGen/MIRParser/MIRParser.cpp | 49 +++++++++++-------- 3 files changed, 69 insertions(+), 88 deletions(-) diff --git a/llvm/lib/CodeGen/MIRParser/MIParser.cpp b/llvm/lib/CodeGen/MIRParser/MIParser.cpp index 7448952..73f4eea 100644 --- a/llvm/lib/CodeGen/MIRParser/MIParser.cpp +++ b/llvm/lib/CodeGen/MIRParser/MIParser.cpp @@ -36,8 +36,9 @@ using namespace llvm; -PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF) - : MF(MF) { +PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF, + SourceMgr &SM, const SlotMapping &IRSlots) + : MF(MF), SM(&SM), IRSlots(IRSlots) { } namespace { @@ -60,14 +61,11 @@ struct ParsedMachineOperand { }; class MIParser { - SourceMgr &SM; MachineFunction &MF; SMDiagnostic &Error; StringRef Source, CurrentSource; MIToken Token; const PerFunctionMIParsingState &PFS; - /// Maps from indices to unnamed global values and metadata nodes. - const SlotMapping &IRSlots; /// Maps from instruction names to op codes. StringMap Names2InstrOpCodes; /// Maps from register names to registers. @@ -88,8 +86,8 @@ class MIParser { StringMap Names2BitmaskTargetFlags; public: - MIParser(const PerFunctionMIParsingState &PFS, SourceMgr &SM, - SMDiagnostic &Error, StringRef Source, const SlotMapping &IRSlots); + MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error, + StringRef Source); /// \p SkipChar gives the number of characters to skip before looking /// for the next token. @@ -256,11 +254,10 @@ private: } // end anonymous namespace -MIParser::MIParser(const PerFunctionMIParsingState &PFS, SourceMgr &SM, - SMDiagnostic &Error, StringRef Source, - const SlotMapping &IRSlots) - : SM(SM), MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), - PFS(PFS), IRSlots(IRSlots) {} +MIParser::MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error, + StringRef Source) + : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS) +{} void MIParser::lex(unsigned SkipChar) { CurrentSource = lexMIToken( @@ -271,6 +268,7 @@ void MIParser::lex(unsigned SkipChar) { bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); } bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) { + const SourceMgr &SM = *PFS.SM; assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size())); const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID()); if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) { @@ -1011,7 +1009,7 @@ bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue, auto Source = StringValue.str(); // The source has to be null terminated. SMDiagnostic Err; C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(), - &IRSlots); + &PFS.IRSlots); if (!C) return error(Loc + Err.getColumnNo(), Err.getMessage()); return false; @@ -1029,7 +1027,7 @@ bool MIParser::parseIRType(StringRef::iterator Loc, StringRef StringValue, auto Source = StringValue.str(); // The source has to be null terminated. SMDiagnostic Err; Ty = parseTypeAtBeginning(Source.c_str(), Read, Err, - *MF.getFunction()->getParent(), &IRSlots); + *MF.getFunction()->getParent(), &PFS.IRSlots); if (!Ty) return error(Loc + Err.getColumnNo(), Err.getMessage()); return false; @@ -1182,10 +1180,10 @@ bool MIParser::parseGlobalValue(GlobalValue *&GV) { unsigned GVIdx; if (getUnsigned(GVIdx)) return true; - if (GVIdx >= IRSlots.GlobalValues.size()) + if (GVIdx >= PFS.IRSlots.GlobalValues.size()) return error(Twine("use of undefined global value '@") + Twine(GVIdx) + "'"); - GV = IRSlots.GlobalValues[GVIdx]; + GV = PFS.IRSlots.GlobalValues[GVIdx]; break; } default: @@ -1263,8 +1261,8 @@ bool MIParser::parseMDNode(MDNode *&Node) { unsigned ID; if (getUnsigned(ID)) return true; - auto NodeInfo = IRSlots.MetadataNodes.find(ID); - if (NodeInfo == IRSlots.MetadataNodes.end()) + auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID); + if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'"); lex(); Node = NodeInfo->second.get(); @@ -2055,61 +2053,40 @@ bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) { bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, StringRef Src, - const SlotMapping &IRSlots, SMDiagnostic &Error) { - SourceMgr SM; - SM.AddNewSourceBuffer( - MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false), - SMLoc()); - return MIParser(PFS, SM, Error, Src, IRSlots) - .parseBasicBlockDefinitions(PFS.MBBSlots); + return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots); } bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS, - StringRef Src, const SlotMapping &IRSlots, - SMDiagnostic &Error) { - SourceMgr SM; - SM.AddNewSourceBuffer( - MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false), - SMLoc()); - return MIParser(PFS, SM, Error, Src, IRSlots).parseBasicBlocks(); + StringRef Src, SMDiagnostic &Error) { + return MIParser(PFS, Error, Src).parseBasicBlocks(); } bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS, - MachineBasicBlock *&MBB, SourceMgr &SM, - StringRef Src, const SlotMapping &IRSlots, + MachineBasicBlock *&MBB, StringRef Src, SMDiagnostic &Error) { - return MIParser(PFS, SM, Error, Src, IRSlots).parseStandaloneMBB(MBB); + return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB); } bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS, - unsigned &Reg, SourceMgr &SM, - StringRef Src, - const SlotMapping &IRSlots, + unsigned &Reg, StringRef Src, SMDiagnostic &Error) { - return MIParser(PFS, SM, Error, Src, IRSlots) - .parseStandaloneNamedRegister(Reg); + return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg); } bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS, - unsigned &Reg, SourceMgr &SM, - StringRef Src, - const SlotMapping &IRSlots, + unsigned &Reg, StringRef Src, SMDiagnostic &Error) { - return MIParser(PFS, SM, Error, Src, IRSlots) - .parseStandaloneVirtualRegister(Reg); + return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg); } bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS, - int &FI, SourceMgr &SM, StringRef Src, - const SlotMapping &IRSlots, + int &FI, StringRef Src, SMDiagnostic &Error) { - return MIParser(PFS, SM, Error, Src, IRSlots) - .parseStandaloneStackObject(FI); + return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI); } bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS, - MDNode *&Node, SourceMgr &SM, StringRef Src, - const SlotMapping &IRSlots, SMDiagnostic &Error) { - return MIParser(PFS, SM, Error, Src, IRSlots).parseStandaloneMDNode(Node); + MDNode *&Node, StringRef Src, SMDiagnostic &Error) { + return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node); } diff --git a/llvm/lib/CodeGen/MIRParser/MIParser.h b/llvm/lib/CodeGen/MIRParser/MIParser.h index ab160dc..18895b9 100644 --- a/llvm/lib/CodeGen/MIRParser/MIParser.h +++ b/llvm/lib/CodeGen/MIRParser/MIParser.h @@ -32,6 +32,8 @@ class SourceMgr; struct PerFunctionMIParsingState { MachineFunction &MF; + SourceMgr *SM; + const SlotMapping &IRSlots; DenseMap MBBSlots; DenseMap VirtualRegisterSlots; @@ -42,7 +44,8 @@ struct PerFunctionMIParsingState { /// Hold the generic virtual registers. SmallSet GenericVRegs; - PerFunctionMIParsingState(MachineFunction &MF); + PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM, + const SlotMapping &IRSlots); }; /// Parse the machine basic block definitions, and skip the machine @@ -58,9 +61,7 @@ struct PerFunctionMIParsingState { /// /// Return true if an error occurred. bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, - StringRef Src, - const SlotMapping &IRSlots, - SMDiagnostic &Error); + StringRef Src, SMDiagnostic &Error); /// Parse the machine instructions. /// @@ -73,31 +74,25 @@ bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, /// /// Return true if an error occurred. bool parseMachineInstructions(const PerFunctionMIParsingState &PFS, - StringRef Src, const SlotMapping &IRSlots, - SMDiagnostic &Error); + StringRef Src, SMDiagnostic &Error); bool parseMBBReference(const PerFunctionMIParsingState &PFS, - MachineBasicBlock *&MBB, SourceMgr &SM, - StringRef Src, const SlotMapping &IRSlots, + MachineBasicBlock *&MBB, StringRef Src, SMDiagnostic &Error); bool parseNamedRegisterReference(const PerFunctionMIParsingState &PFS, - unsigned &Reg, SourceMgr &SM, - StringRef Src, const SlotMapping &IRSlots, + unsigned &Reg, StringRef Src, SMDiagnostic &Error); bool parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS, - unsigned &Reg, SourceMgr &SM, - StringRef Src, const SlotMapping &IRSlots, + unsigned &Reg, StringRef Src, SMDiagnostic &Error); bool parseStackObjectReference(const PerFunctionMIParsingState &PFS, - int &FI, SourceMgr &SM, StringRef Src, - const SlotMapping &IRSlots, SMDiagnostic &Error); + int &FI, StringRef Src, SMDiagnostic &Error); bool parseMDNode(const PerFunctionMIParsingState &PFS, MDNode *&Node, - SourceMgr &SM, StringRef Src, const SlotMapping &IRSlots, - SMDiagnostic &Error); + StringRef Src, SMDiagnostic &Error); } // end namespace llvm diff --git a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp index 4b0f9fe..2d97960 100644 --- a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp +++ b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp @@ -292,7 +292,7 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) { MF.setHasInlineAsm(YamlMF.HasInlineAsm); if (YamlMF.AllVRegsAllocated) MF.getProperties().set(MachineFunctionProperties::Property::AllVRegsAllocated); - PerFunctionMIParsingState PFS(MF); + PerFunctionMIParsingState PFS(MF, SM, IRSlots); if (initializeRegisterInfo(PFS, YamlMF)) return true; if (!YamlMF.Constants.empty()) { @@ -302,13 +302,19 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) { return true; } + StringRef BlockStr = YamlMF.Body.Value.Value; SMDiagnostic Error; - if (parseMachineBasicBlockDefinitions(PFS, YamlMF.Body.Value.Value, IRSlots, - Error)) { + SourceMgr BlockSM; + BlockSM.AddNewSourceBuffer( + MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false), + SMLoc()); + PFS.SM = &BlockSM; + if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) { reportDiagnostic( diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange)); return true; } + PFS.SM = &SM; if (MF.empty()) return error(Twine("machine function '") + Twine(MF.getName()) + @@ -324,12 +330,20 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) { return true; // Parse the machine instructions after creating all of the MBBs so that the // parser can resolve the MBB references. - if (parseMachineInstructions(PFS, YamlMF.Body.Value.Value, IRSlots, Error)) { + StringRef InsnStr = YamlMF.Body.Value.Value; + SourceMgr InsnSM; + InsnSM.AddNewSourceBuffer( + MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false), + SMLoc()); + PFS.SM = &InsnSM; + if (parseMachineInstructions(PFS, InsnStr, Error)) { reportDiagnostic( diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange)); return true; } - inferRegisterInfo(MF, YamlMF); + PFS.SM = &SM; + + inferRegisterInfo(PFS, YamlMF); // FIXME: This is a temporary workaround until the reserved registers can be // serialized. MF.getRegInfo().freezeReservedRegs(MF); @@ -381,9 +395,8 @@ bool MIRParserImpl::initializeRegisterInfo(PerFunctionMIParsingState &PFS, Twine(VReg.ID.Value) + "'"); if (!VReg.PreferredRegister.Value.empty()) { unsigned PreferredReg = 0; - if (parseNamedRegisterReference(PFS, PreferredReg, SM, - VReg.PreferredRegister.Value, IRSlots, - Error)) + if (parseNamedRegisterReference(PFS, PreferredReg, + VReg.PreferredRegister.Value, Error)) return error(Error, VReg.PreferredRegister.SourceRange); RegInfo.setSimpleHint(Reg, PreferredReg); } @@ -392,13 +405,12 @@ bool MIRParserImpl::initializeRegisterInfo(PerFunctionMIParsingState &PFS, // Parse the liveins. for (const auto &LiveIn : YamlMF.LiveIns) { unsigned Reg = 0; - if (parseNamedRegisterReference(PFS, Reg, SM, LiveIn.Register.Value, - IRSlots, Error)) + if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error)) return error(Error, LiveIn.Register.SourceRange); unsigned VReg = 0; if (!LiveIn.VirtualRegister.Value.empty()) { - if (parseVirtualRegisterReference( - PFS, VReg, SM, LiveIn.VirtualRegister.Value, IRSlots, Error)) + if (parseVirtualRegisterReference(PFS, VReg, LiveIn.VirtualRegister.Value, + Error)) return error(Error, LiveIn.VirtualRegister.SourceRange); } RegInfo.addLiveIn(Reg, VReg); @@ -410,8 +422,7 @@ bool MIRParserImpl::initializeRegisterInfo(PerFunctionMIParsingState &PFS, return false; for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) { unsigned Reg = 0; - if (parseNamedRegisterReference(PFS, Reg, SM, RegSource.Value, IRSlots, - Error)) + if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error)) return error(Error, RegSource.SourceRange); CalleeSavedRegisterMask[Reg] = true; } @@ -532,8 +543,7 @@ bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS, if (!YamlMFI.StackProtector.Value.empty()) { SMDiagnostic Error; int FI; - if (parseStackObjectReference(PFS, FI, SM, YamlMFI.StackProtector.Value, - IRSlots, Error)) + if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error)) return error(Error, YamlMFI.StackProtector.SourceRange); MFI.setStackProtectorIndex(FI); } @@ -547,8 +557,7 @@ bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS, return false; unsigned Reg = 0; SMDiagnostic Error; - if (parseNamedRegisterReference(PFS, Reg, SM, RegisterSource.Value, IRSlots, - Error)) + if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error)) return error(Error, RegisterSource.SourceRange); CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx)); return false; @@ -597,7 +606,7 @@ bool MIRParserImpl::parseMDNode(const PerFunctionMIParsingState &PFS, if (Source.Value.empty()) return false; SMDiagnostic Error; - if (llvm::parseMDNode(PFS, Node, SM, Source.Value, IRSlots, Error)) + if (llvm::parseMDNode(PFS, Node, Source.Value, Error)) return error(Error, Source.SourceRange); return false; } @@ -652,7 +661,7 @@ bool MIRParserImpl::parseMBBReference(const PerFunctionMIParsingState &PFS, MachineBasicBlock *&MBB, const yaml::StringValue &Source) { SMDiagnostic Error; - if (llvm::parseMBBReference(PFS, MBB, SM, Source.Value, IRSlots, Error)) + if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error)) return error(Error, Source.SourceRange); return false; } -- 2.7.4