MIR Parser: wrap 'MBBSlots' from the MI parsing functions in a struct. NFC.

This commit modifies the interface for the machine instruction parsing
functions by wrapping the parameter 'MBBSlots' in a new structure called
'PerFunctionMIParsingState'. This change is useful as in the future I will be
able to pass new parameters to the machine instruction parser just by modifying
the 'PerFunctionMIParsingState' structure instead of adding a new parameter to
each function.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241607 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alex Lorenz
2015-07-07 17:46:43 +00:00
parent 7b7c81cd35
commit ddc6f0a433
3 changed files with 32 additions and 33 deletions

View File

@ -47,8 +47,7 @@ class MIParser {
SMDiagnostic &Error; SMDiagnostic &Error;
StringRef Source, CurrentSource; StringRef Source, CurrentSource;
MIToken Token; MIToken Token;
/// Maps from basic block numbers to MBBs. const PerFunctionMIParsingState &PFS;
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots;
/// Maps from indices to unnamed global values and metadata nodes. /// Maps from indices to unnamed global values and metadata nodes.
const SlotMapping &IRSlots; const SlotMapping &IRSlots;
/// Maps from instruction names to op codes. /// Maps from instruction names to op codes.
@ -60,8 +59,7 @@ class MIParser {
public: public:
MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error, MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
StringRef Source, StringRef Source, const PerFunctionMIParsingState &PFS,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots,
const SlotMapping &IRSlots); const SlotMapping &IRSlots);
void lex(); void lex();
@ -122,12 +120,10 @@ private:
} // end anonymous namespace } // end anonymous namespace
MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error, MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
StringRef Source, StringRef Source, const PerFunctionMIParsingState &PFS,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots,
const SlotMapping &IRSlots) const SlotMapping &IRSlots)
: SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source), : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Token(MIToken::Error, StringRef()), MBBSlots(MBBSlots), IRSlots(IRSlots) { Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
}
void MIParser::lex() { void MIParser::lex() {
CurrentSource = lexMIToken( CurrentSource = lexMIToken(
@ -362,8 +358,8 @@ bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
unsigned Number; unsigned Number;
if (getUnsigned(Number)) if (getUnsigned(Number))
return true; return true;
auto MBBInfo = MBBSlots.find(Number); auto MBBInfo = PFS.MBBSlots.find(Number);
if (MBBInfo == MBBSlots.end()) if (MBBInfo == PFS.MBBSlots.end())
return error(Twine("use of undefined machine basic block #") + return error(Twine("use of undefined machine basic block #") +
Twine(Number)); Twine(Number));
MBB = MBBInfo->second; MBB = MBBInfo->second;
@ -506,16 +502,16 @@ const uint32_t *MIParser::getRegMask(StringRef Identifier) {
return RegMaskInfo->getValue(); return RegMaskInfo->getValue();
} }
bool llvm::parseMachineInstr( bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
MachineInstr *&MI, SourceMgr &SM, MachineFunction &MF, StringRef Src, MachineFunction &MF, StringRef Src,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots, const PerFunctionMIParsingState &PFS,
const SlotMapping &IRSlots, SMDiagnostic &Error) { const SlotMapping &IRSlots, SMDiagnostic &Error) {
return MIParser(SM, MF, Error, Src, MBBSlots, IRSlots).parse(MI); return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
} }
bool llvm::parseMBBReference( bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
MachineBasicBlock *&MBB, SourceMgr &SM, MachineFunction &MF, StringRef Src, MachineFunction &MF, StringRef Src,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots, const PerFunctionMIParsingState &PFS,
const SlotMapping &IRSlots, SMDiagnostic &Error) { const SlotMapping &IRSlots, SMDiagnostic &Error) {
return MIParser(SM, MF, Error, Src, MBBSlots, IRSlots).parseMBB(MBB); return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseMBB(MBB);
} }

View File

@ -26,14 +26,17 @@ struct SlotMapping;
class SMDiagnostic; class SMDiagnostic;
class SourceMgr; class SourceMgr;
struct PerFunctionMIParsingState {
DenseMap<unsigned, MachineBasicBlock *> MBBSlots;
};
bool parseMachineInstr(MachineInstr *&MI, SourceMgr &SM, MachineFunction &MF, bool parseMachineInstr(MachineInstr *&MI, SourceMgr &SM, MachineFunction &MF,
StringRef Src, StringRef Src, const PerFunctionMIParsingState &PFS,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots,
const SlotMapping &IRSlots, SMDiagnostic &Error); const SlotMapping &IRSlots, SMDiagnostic &Error);
bool parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM, bool parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
MachineFunction &MF, StringRef Src, MachineFunction &MF, StringRef Src,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots, const PerFunctionMIParsingState &PFS,
const SlotMapping &IRSlots, SMDiagnostic &Error); const SlotMapping &IRSlots, SMDiagnostic &Error);
} // end namespace llvm } // end namespace llvm

View File

@ -90,10 +90,9 @@ public:
/// Initialize the machine basic block using it's YAML representation. /// Initialize the machine basic block using it's YAML representation.
/// ///
/// Return true if an error occurred. /// Return true if an error occurred.
bool initializeMachineBasicBlock( bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
MachineFunction &MF, MachineBasicBlock &MBB, const yaml::MachineBasicBlock &YamlMBB,
const yaml::MachineBasicBlock &YamlMBB, const PerFunctionMIParsingState &PFS);
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
bool initializeRegisterInfo(MachineRegisterInfo &RegInfo, bool initializeRegisterInfo(MachineRegisterInfo &RegInfo,
const yaml::MachineFunction &YamlMF); const yaml::MachineFunction &YamlMF);
@ -236,8 +235,8 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
if (initializeRegisterInfo(MF.getRegInfo(), YamlMF)) if (initializeRegisterInfo(MF.getRegInfo(), YamlMF))
return true; return true;
PerFunctionMIParsingState PFS;
const auto &F = *MF.getFunction(); const auto &F = *MF.getFunction();
DenseMap<unsigned, MachineBasicBlock *> MBBSlots;
for (const auto &YamlMBB : YamlMF.BasicBlocks) { for (const auto &YamlMBB : YamlMF.BasicBlocks) {
const BasicBlock *BB = nullptr; const BasicBlock *BB = nullptr;
if (!YamlMBB.Name.empty()) { if (!YamlMBB.Name.empty()) {
@ -249,7 +248,8 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
} }
auto *MBB = MF.CreateMachineBasicBlock(BB); auto *MBB = MF.CreateMachineBasicBlock(BB);
MF.insert(MF.end(), MBB); MF.insert(MF.end(), MBB);
bool WasInserted = MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second; bool WasInserted =
PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
if (!WasInserted) if (!WasInserted)
return error(Twine("redefinition of machine basic block with id #") + return error(Twine("redefinition of machine basic block with id #") +
Twine(YamlMBB.ID)); Twine(YamlMBB.ID));
@ -260,7 +260,7 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
unsigned I = 0; unsigned I = 0;
for (const auto &YamlMBB : YamlMF.BasicBlocks) { for (const auto &YamlMBB : YamlMF.BasicBlocks) {
if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB, if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
MBBSlots)) PFS))
return true; return true;
} }
return false; return false;
@ -269,7 +269,7 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
bool MIRParserImpl::initializeMachineBasicBlock( bool MIRParserImpl::initializeMachineBasicBlock(
MachineFunction &MF, MachineBasicBlock &MBB, MachineFunction &MF, MachineBasicBlock &MBB,
const yaml::MachineBasicBlock &YamlMBB, const yaml::MachineBasicBlock &YamlMBB,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) { const PerFunctionMIParsingState &PFS) {
MBB.setAlignment(YamlMBB.Alignment); MBB.setAlignment(YamlMBB.Alignment);
if (YamlMBB.AddressTaken) if (YamlMBB.AddressTaken)
MBB.setHasAddressTaken(); MBB.setHasAddressTaken();
@ -278,7 +278,7 @@ bool MIRParserImpl::initializeMachineBasicBlock(
// Parse the successors. // Parse the successors.
for (const auto &MBBSource : YamlMBB.Successors) { for (const auto &MBBSource : YamlMBB.Successors) {
MachineBasicBlock *SuccMBB = nullptr; MachineBasicBlock *SuccMBB = nullptr;
if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, MBBSlots, IRSlots, if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
Error)) Error))
return error(Error, MBBSource.SourceRange); return error(Error, MBBSource.SourceRange);
// TODO: Report an error when adding the same successor more than once. // TODO: Report an error when adding the same successor more than once.
@ -287,7 +287,7 @@ bool MIRParserImpl::initializeMachineBasicBlock(
// Parse the instructions. // Parse the instructions.
for (const auto &MISource : YamlMBB.Instructions) { for (const auto &MISource : YamlMBB.Instructions) {
MachineInstr *MI = nullptr; MachineInstr *MI = nullptr;
if (parseMachineInstr(MI, SM, MF, MISource.Value, MBBSlots, IRSlots, Error)) if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
return error(Error, MISource.SourceRange); return error(Error, MISource.SourceRange);
MBB.insert(MBB.end(), MI); MBB.insert(MBB.end(), MI);
} }