[llvm] r275342 - MIRParser: Move SlotMapping and SourceMgr refs to PFS; NFC

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 13 16:27:51 PDT 2016


Author: matze
Date: Wed Jul 13 18:27:50 2016
New Revision: 275342

URL: http://llvm.org/viewvc/llvm-project?rev=275342&view=rev
Log:
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.

Modified:
    llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp
    llvm/trunk/lib/CodeGen/MIRParser/MIParser.h
    llvm/trunk/lib/CodeGen/MIRParser/MIRParser.cpp

Modified: llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp?rev=275342&r1=275341&r2=275342&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp Wed Jul 13 18:27:50 2016
@@ -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<unsigned> Names2InstrOpCodes;
   /// Maps from register names to registers.
@@ -88,8 +86,8 @@ class MIParser {
   StringMap<unsigned> 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
   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::it
   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(GlobalVa
     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(Stri
 
 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);
 }

Modified: llvm/trunk/lib/CodeGen/MIRParser/MIParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MIParser.h?rev=275342&r1=275341&r2=275342&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MIParser.h (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MIParser.h Wed Jul 13 18:27:50 2016
@@ -32,6 +32,8 @@ class SourceMgr;
 
 struct PerFunctionMIParsingState {
   MachineFunction &MF;
+  SourceMgr *SM;
+  const SlotMapping &IRSlots;
 
   DenseMap<unsigned, MachineBasicBlock *> MBBSlots;
   DenseMap<unsigned, unsigned> VirtualRegisterSlots;
@@ -42,7 +44,8 @@ struct PerFunctionMIParsingState {
   /// Hold the generic virtual registers.
   SmallSet<unsigned, 8> 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(P
 ///
 /// 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
 

Modified: llvm/trunk/lib/CodeGen/MIRParser/MIRParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MIRParser.cpp?rev=275342&r1=275341&r2=275342&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MIRParser.cpp (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MIRParser.cpp Wed Jul 13 18:27:50 2016
@@ -292,7 +292,7 @@ bool MIRParserImpl::initializeMachineFun
   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::initializeMachineFun
       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::initializeMachineFun
     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::initializeRegisterIn
                        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::initializeRegisterIn
   // 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::initializeRegisterIn
     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(
   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::parseCalleeSavedRegi
     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 Pe
   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(co
                                       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;
 }




More information about the llvm-commits mailing list