summaryrefslogtreecommitdiff
path: root/lib/CodeGen/MIRParser
diff options
context:
space:
mode:
Diffstat (limited to 'lib/CodeGen/MIRParser')
-rw-r--r--lib/CodeGen/MIRParser/MILexer.cpp68
-rw-r--r--lib/CodeGen/MIRParser/MILexer.h5
-rw-r--r--lib/CodeGen/MIRParser/MIParser.cpp263
-rw-r--r--lib/CodeGen/MIRParser/MIParser.h60
-rw-r--r--lib/CodeGen/MIRParser/MIRParser.cpp248
-rw-r--r--lib/CodeGen/MIRParser/Makefile13
6 files changed, 394 insertions, 263 deletions
diff --git a/lib/CodeGen/MIRParser/MILexer.cpp b/lib/CodeGen/MIRParser/MILexer.cpp
index 28f9d4e298f91..6e3de52f1a9ca 100644
--- a/lib/CodeGen/MIRParser/MILexer.cpp
+++ b/lib/CodeGen/MIRParser/MILexer.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "MILexer.h"
+#include "llvm/ADT/None.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Twine.h"
@@ -21,6 +22,9 @@ using namespace llvm;
namespace {
+typedef function_ref<void(StringRef::iterator Loc, const Twine &)>
+ ErrorCallbackType;
+
/// This class provides a way to iterate and get characters from the source
/// string.
class Cursor {
@@ -133,9 +137,7 @@ static std::string unescapeQuotedString(StringRef Value) {
}
/// Lex a string constant using the following regular expression: \"[^\"]*\"
-static Cursor lexStringConstant(
- Cursor C,
- function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+static Cursor lexStringConstant(Cursor C, ErrorCallbackType ErrorCallback) {
assert(C.peek() == '"');
for (C.advance(); C.peek() != '"'; C.advance()) {
if (C.isEOF() || isNewlineChar(C.peek())) {
@@ -149,9 +151,8 @@ static Cursor lexStringConstant(
return C;
}
-static Cursor lexName(
- Cursor C, MIToken &Token, MIToken::TokenKind Type, unsigned PrefixLength,
- function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+static Cursor lexName(Cursor C, MIToken &Token, MIToken::TokenKind Type,
+ unsigned PrefixLength, ErrorCallbackType ErrorCallback) {
auto Range = C;
C.advance(PrefixLength);
if (C.peek() == '"') {
@@ -241,9 +242,8 @@ static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
return C;
}
-static Cursor maybeLexMachineBasicBlock(
- Cursor C, MIToken &Token,
- function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
+ ErrorCallbackType ErrorCallback) {
bool IsReference = C.remaining().startswith("%bb.");
if (!IsReference && !C.remaining().startswith("bb."))
return None;
@@ -326,9 +326,17 @@ static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
}
-static Cursor maybeLexIRBlock(
- Cursor C, MIToken &Token,
- function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
+ ErrorCallbackType ErrorCallback) {
+ const StringRef Rule = "%subreg.";
+ if (!C.remaining().startswith(Rule))
+ return None;
+ return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
+ ErrorCallback);
+}
+
+static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
+ ErrorCallbackType ErrorCallback) {
const StringRef Rule = "%ir-block.";
if (!C.remaining().startswith(Rule))
return None;
@@ -337,9 +345,8 @@ static Cursor maybeLexIRBlock(
return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
}
-static Cursor maybeLexIRValue(
- Cursor C, MIToken &Token,
- function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
+ ErrorCallbackType ErrorCallback) {
const StringRef Rule = "%ir.";
if (!C.remaining().startswith(Rule))
return None;
@@ -373,9 +380,8 @@ static Cursor maybeLexRegister(Cursor C, MIToken &Token) {
return C;
}
-static Cursor maybeLexGlobalValue(
- Cursor C, MIToken &Token,
- function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
+ ErrorCallbackType ErrorCallback) {
if (C.peek() != '@')
return None;
if (!isdigit(C.peek(1)))
@@ -391,9 +397,8 @@ static Cursor maybeLexGlobalValue(
return C;
}
-static Cursor maybeLexExternalSymbol(
- Cursor C, MIToken &Token,
- function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
+ ErrorCallbackType ErrorCallback) {
if (C.peek() != '$')
return None;
return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
@@ -456,9 +461,8 @@ static MIToken::TokenKind getMetadataKeywordKind(StringRef Identifier) {
.Default(MIToken::Error);
}
-static Cursor maybeLexExlaim(
- Cursor C, MIToken &Token,
- function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+static Cursor maybeLexExlaim(Cursor C, MIToken &Token,
+ ErrorCallbackType ErrorCallback) {
if (C.peek() != '!')
return None;
auto Range = C;
@@ -497,6 +501,10 @@ static MIToken::TokenKind symbolToken(char C) {
return MIToken::plus;
case '-':
return MIToken::minus;
+ case '<':
+ return MIToken::less;
+ case '>':
+ return MIToken::greater;
default:
return MIToken::Error;
}
@@ -527,9 +535,8 @@ static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
return C;
}
-static Cursor maybeLexEscapedIRValue(
- Cursor C, MIToken &Token,
- function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
+ ErrorCallbackType ErrorCallback) {
if (C.peek() != '`')
return None;
auto Range = C;
@@ -551,9 +558,8 @@ static Cursor maybeLexEscapedIRValue(
return C;
}
-StringRef llvm::lexMIToken(
- StringRef Source, MIToken &Token,
- function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+StringRef llvm::lexMIToken(StringRef Source, MIToken &Token,
+ ErrorCallbackType ErrorCallback) {
auto C = skipComment(skipWhitespace(Cursor(Source)));
if (C.isEOF()) {
Token.reset(MIToken::Eof, C.remaining());
@@ -574,6 +580,8 @@ StringRef llvm::lexMIToken(
return R.remaining();
if (Cursor R = maybeLexConstantPoolItem(C, Token))
return R.remaining();
+ if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
+ return R.remaining();
if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
return R.remaining();
if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
diff --git a/lib/CodeGen/MIRParser/MILexer.h b/lib/CodeGen/MIRParser/MILexer.h
index ff54aa3554d87..32fc8ab271e62 100644
--- a/lib/CodeGen/MIRParser/MILexer.h
+++ b/lib/CodeGen/MIRParser/MILexer.h
@@ -45,6 +45,8 @@ struct MIToken {
rbrace,
plus,
minus,
+ less,
+ greater,
// Keywords
kw_implicit,
@@ -116,7 +118,8 @@ struct MIToken {
IRBlock,
NamedIRValue,
IRValue,
- QuotedIRValue // `<constant value>`
+ QuotedIRValue, // `<constant value>`
+ SubRegisterIndex
};
private:
diff --git a/lib/CodeGen/MIRParser/MIParser.cpp b/lib/CodeGen/MIRParser/MIParser.cpp
index f2f6584fb6c81..b3fd16f158895 100644
--- a/lib/CodeGen/MIRParser/MIParser.cpp
+++ b/lib/CodeGen/MIRParser/MIParser.cpp
@@ -17,24 +17,30 @@
#include "llvm/AsmParser/Parser.h"
#include "llvm/AsmParser/SlotMapping.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
-#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
+#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineMemOperand.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
-#include "llvm/IR/Instructions.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/IR/Constants.h"
+#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/IR/ValueSymbolTable.h"
-#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SourceMgr.h"
-#include "llvm/Target/TargetSubtargetInfo.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetInstrInfo.h"
+#include "llvm/Target/TargetSubtargetInfo.h"
using namespace llvm;
+PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
+ SourceMgr &SM, const SlotMapping &IRSlots)
+ : MF(MF), SM(&SM), IRSlots(IRSlots) {
+}
+
namespace {
/// A wrapper struct around the 'MachineOperand' struct that includes a source
@@ -55,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.
@@ -83,11 +86,12 @@ class MIParser {
StringMap<unsigned> Names2BitmaskTargetFlags;
public:
- MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
- StringRef Source, const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots);
+ MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
+ StringRef Source);
- void lex();
+ /// \p SkipChar gives the number of characters to skip before looking
+ /// for the next token.
+ void lex(unsigned SkipChar = 0);
/// Report an error at the current location with the given message.
///
@@ -119,12 +123,17 @@ public:
bool parseRegisterFlag(unsigned &Flags);
bool parseSubRegisterIndex(unsigned &SubReg);
bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
+ bool parseSize(unsigned &Size);
bool parseRegisterOperand(MachineOperand &Dest,
Optional<unsigned> &TiedDefIdx, bool IsDef = false);
bool parseImmediateOperand(MachineOperand &Dest);
bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
const Constant *&C);
bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
+ bool parseIRType(StringRef::iterator Loc, StringRef Source, unsigned &Read,
+ Type *&Ty);
+ // \p MustBeSized defines whether or not \p Ty must be sized.
+ bool parseIRType(StringRef::iterator Loc, Type *&Ty, bool MustBeSized = true);
bool parseTypedImmediateOperand(MachineOperand &Dest);
bool parseFPImmediateOperand(MachineOperand &Dest);
bool parseMBBReference(MachineBasicBlock *&MBB);
@@ -136,6 +145,7 @@ public:
bool parseGlobalValue(GlobalValue *&GV);
bool parseGlobalAddressOperand(MachineOperand &Dest);
bool parseConstantPoolIndexOperand(MachineOperand &Dest);
+ bool parseSubRegisterIndexOperand(MachineOperand &Dest);
bool parseJumpTableIndexOperand(MachineOperand &Dest);
bool parseExternalSymbolOperand(MachineOperand &Dest);
bool parseMDNode(MDNode *&Node);
@@ -155,7 +165,7 @@ public:
bool parseAlignment(unsigned &Alignment);
bool parseOperandsOffset(MachineOperand &Op);
bool parseIRValue(const Value *&V);
- bool parseMemoryOperandFlag(unsigned &Flags);
+ bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
bool parseMachinePointerInfo(MachinePointerInfo &Dest);
bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
@@ -244,21 +254,21 @@ private:
} // end anonymous namespace
-MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
- StringRef Source, const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots)
- : SM(SM), MF(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() {
+void MIParser::lex(unsigned SkipChar) {
CurrentSource = lexMIToken(
- CurrentSource, Token,
+ CurrentSource.data() + SkipChar, Token,
[this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
}
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()) {
@@ -587,6 +597,14 @@ bool MIParser::parse(MachineInstr *&MI) {
if (Token.isError() || parseInstruction(OpCode, Flags))
return true;
+ Type *Ty = nullptr;
+ if (isPreISelGenericOpcode(OpCode)) {
+ // For generic opcode, a type is mandatory.
+ auto Loc = Token.location();
+ if (parseIRType(Loc, Ty))
+ return true;
+ }
+
// Parse the remaining machine operands.
while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
@@ -642,6 +660,8 @@ bool MIParser::parse(MachineInstr *&MI) {
// TODO: Check for extraneous machine operands.
MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
MI->setFlags(Flags);
+ if (Ty)
+ MI->setType(Ty);
for (const auto &Operand : Operands)
MI->addOperand(MF, Operand.Operand);
if (assignRegisterTies(*MI, Operands))
@@ -876,6 +896,17 @@ bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
return false;
}
+bool MIParser::parseSize(unsigned &Size) {
+ if (Token.isNot(MIToken::IntegerLiteral))
+ return error("expected an integer literal for the size");
+ if (getUnsigned(Size))
+ return true;
+ lex();
+ if (expectAndConsume(MIToken::rparen))
+ return true;
+ return false;
+}
+
bool MIParser::assignRegisterTies(MachineInstr &MI,
ArrayRef<ParsedMachineOperand> Operands) {
SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
@@ -931,12 +962,31 @@ bool MIParser::parseRegisterOperand(MachineOperand &Dest,
if (Token.is(MIToken::colon)) {
if (parseSubRegisterIndex(SubReg))
return true;
+ if (!TargetRegisterInfo::isVirtualRegister(Reg))
+ return error("subregister index expects a virtual register");
}
- if ((Flags & RegState::Define) == 0 && consumeIfPresent(MIToken::lparen)) {
- unsigned Idx;
- if (parseRegisterTiedDefIndex(Idx))
+ if ((Flags & RegState::Define) == 0) {
+ if (consumeIfPresent(MIToken::lparen)) {
+ unsigned Idx;
+ if (parseRegisterTiedDefIndex(Idx))
+ return true;
+ TiedDefIdx = Idx;
+ }
+ } else if (consumeIfPresent(MIToken::lparen)) {
+ // Virtual registers may have a size with GlobalISel.
+ if (!TargetRegisterInfo::isVirtualRegister(Reg))
+ return error("unexpected size on physical register");
+ unsigned Size;
+ if (parseSize(Size))
return true;
- TiedDefIdx = Idx;
+
+ MachineRegisterInfo &MRI = MF.getRegInfo();
+ MRI.setSize(Reg, Size);
+ } else if (PFS.GenericVRegs.count(Reg)) {
+ // Generic virtual registers must have a size.
+ // If we end up here this means the size hasn't been specified and
+ // this is bad!
+ return error("generic virtual registers must have a size");
}
Dest = MachineOperand::CreateReg(
Reg, Flags & RegState::Define, Flags & RegState::Implicit,
@@ -961,7 +1011,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;
@@ -974,6 +1024,38 @@ bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
return false;
}
+bool MIParser::parseIRType(StringRef::iterator Loc, StringRef StringValue,
+ unsigned &Read, Type *&Ty) {
+ auto Source = StringValue.str(); // The source has to be null terminated.
+ SMDiagnostic Err;
+ Ty = parseTypeAtBeginning(Source.c_str(), Read, Err,
+ *MF.getFunction()->getParent(), &PFS.IRSlots);
+ if (!Ty)
+ return error(Loc + Err.getColumnNo(), Err.getMessage());
+ return false;
+}
+
+bool MIParser::parseIRType(StringRef::iterator Loc, Type *&Ty,
+ bool MustBeSized) {
+ // At this point we enter in the IR world, i.e., to get the correct type,
+ // we need to hand off the whole string, not just the current token.
+ // E.g., <4 x i64> would give '<' as a token and there is not much
+ // the IR parser can do with that.
+ unsigned Read = 0;
+ if (parseIRType(Loc, StringRef(Loc), Read, Ty))
+ return true;
+ // The type must be sized, otherwise there is not much the backend
+ // can do with it.
+ if (MustBeSized && !Ty->isSized())
+ return error("expected a sized type");
+ // The next token is Read characters from the Loc.
+ // However, the current location is not Loc, but Loc + the length of Token.
+ // Therefore, subtract the length of Token (range().end() - Loc) to the
+ // number of characters to skip before the next token.
+ lex(Read - (Token.range().end() - Loc));
+ return false;
+}
+
bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
assert(Token.is(MIToken::IntegerType));
auto Loc = Token.location();
@@ -1100,10 +1182,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:
@@ -1161,6 +1243,17 @@ bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
return false;
}
+bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
+ assert(Token.is(MIToken::SubRegisterIndex));
+ StringRef Name = Token.stringValue();
+ unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
+ if (SubRegIndex == 0)
+ return error(Twine("unknown subregister index '") + Name + "'");
+ lex();
+ Dest = MachineOperand::CreateImm(SubRegIndex);
+ return false;
+}
+
bool MIParser::parseMDNode(MDNode *&Node) {
assert(Token.is(MIToken::exclaim));
auto Loc = Token.location();
@@ -1170,8 +1263,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();
@@ -1406,6 +1499,8 @@ bool MIParser::parseMachineOperand(MachineOperand &Dest,
return parseJumpTableIndexOperand(Dest);
case MIToken::ExternalSymbol:
return parseExternalSymbolOperand(Dest);
+ case MIToken::SubRegisterIndex:
+ return parseSubRegisterIndexOperand(Dest);
case MIToken::exclaim:
return parseMetadataOperand(Dest);
case MIToken::kw_cfi_same_value:
@@ -1559,8 +1654,8 @@ bool MIParser::getUint64(uint64_t &Result) {
return false;
}
-bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
- const unsigned OldFlags = Flags;
+bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
+ const auto OldFlags = Flags;
switch (Token.kind()) {
case MIToken::kw_volatile:
Flags |= MachineMemOperand::MOVolatile;
@@ -1605,6 +1700,14 @@ bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
// The token was already consumed, so use return here instead of break.
return false;
}
+ case MIToken::StackObject: {
+ int FI;
+ if (parseStackFrameIndex(FI))
+ return true;
+ PSV = MF.getPSVManager().getFixedStack(FI);
+ // The token was already consumed, so use return here instead of break.
+ return false;
+ }
case MIToken::kw_call_entry: {
lex();
switch (Token.kind()) {
@@ -1636,7 +1739,8 @@ bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
- Token.is(MIToken::FixedStackObject) || Token.is(MIToken::kw_call_entry)) {
+ Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
+ Token.is(MIToken::kw_call_entry)) {
const PseudoSourceValue *PSV = nullptr;
if (parseMemoryPseudoSourceValue(PSV))
return true;
@@ -1667,7 +1771,7 @@ bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
if (expectAndConsume(MIToken::lparen))
return true;
- unsigned Flags = 0;
+ MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
while (Token.isMemoryOperandFlag()) {
if (parseMemoryOperandFlag(Flags))
return true;
@@ -1688,14 +1792,16 @@ bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
return true;
lex();
- const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
- if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
- return error(Twine("expected '") + Word + "'");
- lex();
-
MachinePointerInfo Ptr = MachinePointerInfo();
- if (parseMachinePointerInfo(Ptr))
- return true;
+ if (Token.is(MIToken::Identifier)) {
+ const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
+ if (Token.stringValue() != Word)
+ return error(Twine("expected '") + Word + "'");
+ lex();
+
+ if (parseMachinePointerInfo(Ptr))
+ return true;
+ }
unsigned BaseAlignment = Size;
AAMDNodes AAInfo;
MDNode *Range = nullptr;
@@ -1947,65 +2053,42 @@ bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
return false;
}
-bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
- PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots,
+bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
+ StringRef Src,
SMDiagnostic &Error) {
- SourceMgr SM;
- SM.AddNewSourceBuffer(
- MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
- SMLoc());
- return MIParser(SM, MF, Error, Src, PFS, IRSlots)
- .parseBasicBlockDefinitions(PFS.MBBSlots);
-}
-
-bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
- const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots,
- SMDiagnostic &Error) {
- SourceMgr SM;
- SM.AddNewSourceBuffer(
- MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
- SMLoc());
- return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
-}
-
-bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
- MachineFunction &MF, StringRef Src,
- const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots, SMDiagnostic &Error) {
- return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
-}
-
-bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
- MachineFunction &MF, StringRef Src,
- const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots,
+ return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
+}
+
+bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
+ StringRef Src, SMDiagnostic &Error) {
+ return MIParser(PFS, Error, Src).parseBasicBlocks();
+}
+
+bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
+ MachineBasicBlock *&MBB, StringRef Src,
+ SMDiagnostic &Error) {
+ return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
+}
+
+bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
+ unsigned &Reg, StringRef Src,
SMDiagnostic &Error) {
- return MIParser(SM, MF, Error, Src, PFS, IRSlots)
- .parseStandaloneNamedRegister(Reg);
+ return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
}
-bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
- MachineFunction &MF, StringRef Src,
- const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots,
+bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
+ unsigned &Reg, StringRef Src,
SMDiagnostic &Error) {
- return MIParser(SM, MF, Error, Src, PFS, IRSlots)
- .parseStandaloneVirtualRegister(Reg);
+ return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
}
-bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
- MachineFunction &MF, StringRef Src,
- const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots,
+bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
+ int &FI, StringRef Src,
SMDiagnostic &Error) {
- return MIParser(SM, MF, Error, Src, PFS, IRSlots)
- .parseStandaloneStackObject(FI);
+ return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
}
-bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
- StringRef Src, const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots, SMDiagnostic &Error) {
- return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);
+bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
+ MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
+ return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
}
diff --git a/lib/CodeGen/MIRParser/MIParser.h b/lib/CodeGen/MIRParser/MIParser.h
index 8aef704ab36ca..18895b9e54eb4 100644
--- a/lib/CodeGen/MIRParser/MIParser.h
+++ b/lib/CodeGen/MIRParser/MIParser.h
@@ -15,26 +15,37 @@
#define LLVM_LIB_CODEGEN_MIRPARSER_MIPARSER_H
#include "llvm/ADT/DenseMap.h"
-#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/SmallSet.h"
namespace llvm {
+class StringRef;
class BasicBlock;
class MachineBasicBlock;
-class MachineInstr;
class MachineFunction;
+class MachineInstr;
+class MachineRegisterInfo;
class MDNode;
struct SlotMapping;
class SMDiagnostic;
class SourceMgr;
struct PerFunctionMIParsingState {
+ MachineFunction &MF;
+ SourceMgr *SM;
+ const SlotMapping &IRSlots;
+
DenseMap<unsigned, MachineBasicBlock *> MBBSlots;
DenseMap<unsigned, unsigned> VirtualRegisterSlots;
DenseMap<unsigned, int> FixedStackObjectSlots;
DenseMap<unsigned, int> StackObjectSlots;
DenseMap<unsigned, unsigned> ConstantPoolSlots;
DenseMap<unsigned, unsigned> JumpTableSlots;
+ /// Hold the generic virtual registers.
+ SmallSet<unsigned, 8> GenericVRegs;
+
+ PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM,
+ const SlotMapping &IRSlots);
};
/// Parse the machine basic block definitions, and skip the machine
@@ -49,10 +60,8 @@ struct PerFunctionMIParsingState {
/// resolve the machine basic block references.
///
/// Return true if an error occurred.
-bool parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
- PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots,
- SMDiagnostic &Error);
+bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
+ StringRef Src, SMDiagnostic &Error);
/// Parse the machine instructions.
///
@@ -64,35 +73,26 @@ bool parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
/// on the given source string.
///
/// Return true if an error occurred.
-bool parseMachineInstructions(MachineFunction &MF, StringRef Src,
- const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots, SMDiagnostic &Error);
-
-bool parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
- MachineFunction &MF, StringRef Src,
- const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots, SMDiagnostic &Error);
-
-bool parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
- MachineFunction &MF, StringRef Src,
- const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots,
+bool parseMachineInstructions(const PerFunctionMIParsingState &PFS,
+ StringRef Src, SMDiagnostic &Error);
+
+bool parseMBBReference(const PerFunctionMIParsingState &PFS,
+ MachineBasicBlock *&MBB, StringRef Src,
+ SMDiagnostic &Error);
+
+bool parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
+ unsigned &Reg, StringRef Src,
SMDiagnostic &Error);
-bool parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
- MachineFunction &MF, StringRef Src,
- const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots,
+bool parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
+ unsigned &Reg, StringRef Src,
SMDiagnostic &Error);
-bool parseStackObjectReference(int &FI, SourceMgr &SM, MachineFunction &MF,
- StringRef Src,
- const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots, SMDiagnostic &Error);
+bool parseStackObjectReference(const PerFunctionMIParsingState &PFS,
+ int &FI, StringRef Src, SMDiagnostic &Error);
-bool parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
- StringRef Src, const PerFunctionMIParsingState &PFS,
- const SlotMapping &IRSlots, SMDiagnostic &Error);
+bool parseMDNode(const PerFunctionMIParsingState &PFS, MDNode *&Node,
+ StringRef Src, SMDiagnostic &Error);
} // end namespace llvm
diff --git a/lib/CodeGen/MIRParser/MIRParser.cpp b/lib/CodeGen/MIRParser/MIRParser.cpp
index 422efbc5ce573..4aa3df6326e96 100644
--- a/lib/CodeGen/MIRParser/MIRParser.cpp
+++ b/lib/CodeGen/MIRParser/MIRParser.cpp
@@ -15,27 +15,30 @@
#include "llvm/CodeGen/MIRParser/MIRParser.h"
#include "MIParser.h"
#include "llvm/ADT/DenseMap.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringRef.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/AsmParser/SlotMapping.h"
+#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
+#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
+#include "llvm/CodeGen/MIRYamlMapping.h"
#include "llvm/CodeGen/MachineConstantPool.h"
-#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
+#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
-#include "llvm/CodeGen/MIRYamlMapping.h"
#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/Support/LineIterator.h"
+#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/SourceMgr.h"
-#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/YAMLTraits.h"
#include <memory>
@@ -53,6 +56,8 @@ class MIRParserImpl {
SlotMapping IRSlots;
/// Maps from register class names to register classes.
StringMap<const TargetRegisterClass *> Names2RegClasses;
+ /// Maps from register bank names to register banks.
+ StringMap<const RegisterBank *> Names2RegBanks;
public:
MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
@@ -97,44 +102,38 @@ public:
/// Return true if error occurred.
bool initializeMachineFunction(MachineFunction &MF);
- bool initializeRegisterInfo(MachineFunction &MF,
- const yaml::MachineFunction &YamlMF,
- PerFunctionMIParsingState &PFS);
+ bool initializeRegisterInfo(PerFunctionMIParsingState &PFS,
+ const yaml::MachineFunction &YamlMF);
- void inferRegisterInfo(MachineFunction &MF,
+ void inferRegisterInfo(const PerFunctionMIParsingState &PFS,
const yaml::MachineFunction &YamlMF);
- bool initializeFrameInfo(MachineFunction &MF,
- const yaml::MachineFunction &YamlMF,
- PerFunctionMIParsingState &PFS);
+ bool initializeFrameInfo(PerFunctionMIParsingState &PFS,
+ const yaml::MachineFunction &YamlMF);
- bool parseCalleeSavedRegister(MachineFunction &MF,
- PerFunctionMIParsingState &PFS,
+ bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
std::vector<CalleeSavedInfo> &CSIInfo,
const yaml::StringValue &RegisterSource,
int FrameIdx);
- bool parseStackObjectsDebugInfo(MachineFunction &MF,
- PerFunctionMIParsingState &PFS,
+ bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
const yaml::MachineStackObject &Object,
int FrameIdx);
- bool initializeConstantPool(MachineConstantPool &ConstantPool,
- const yaml::MachineFunction &YamlMF,
- const MachineFunction &MF,
- DenseMap<unsigned, unsigned> &ConstantPoolSlots);
+ bool initializeConstantPool(PerFunctionMIParsingState &PFS,
+ MachineConstantPool &ConstantPool,
+ const yaml::MachineFunction &YamlMF);
- bool initializeJumpTableInfo(MachineFunction &MF,
- const yaml::MachineJumpTable &YamlJTI,
- PerFunctionMIParsingState &PFS);
+ bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
+ const yaml::MachineJumpTable &YamlJTI);
private:
- bool parseMDNode(MDNode *&Node, const yaml::StringValue &Source,
- MachineFunction &MF, const PerFunctionMIParsingState &PFS);
+ bool parseMDNode(const PerFunctionMIParsingState &PFS, MDNode *&Node,
+ const yaml::StringValue &Source);
- bool parseMBBReference(MachineBasicBlock *&MBB,
- const yaml::StringValue &Source, MachineFunction &MF,
- const PerFunctionMIParsingState &PFS);
+ bool parseMBBReference(const PerFunctionMIParsingState &PFS,
+ MachineBasicBlock *&MBB,
+ const yaml::StringValue &Source);
/// Return a MIR diagnostic converted from an MI string diagnostic.
SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
@@ -149,12 +148,18 @@ private:
void createDummyFunction(StringRef Name, Module &M);
void initNames2RegClasses(const MachineFunction &MF);
+ void initNames2RegBanks(const MachineFunction &MF);
/// Check if the given identifier is a name of a register class.
///
/// Return null if the name isn't a register class.
const TargetRegisterClass *getRegClass(const MachineFunction &MF,
StringRef Name);
+
+ /// Check if the given identifier is a name of a register bank.
+ ///
+ /// Return null if the name isn't a register bank.
+ const RegisterBank *getRegBank(const MachineFunction &MF, StringRef Name);
};
} // end namespace llvm
@@ -226,7 +231,7 @@ std::unique_ptr<Module> MIRParserImpl::parse() {
Context, &IRSlots);
if (!M) {
reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
- return M;
+ return nullptr;
}
In.nextDocument();
if (!In.setCurrentDocument())
@@ -285,46 +290,60 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
MF.setAlignment(YamlMF.Alignment);
MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
MF.setHasInlineAsm(YamlMF.HasInlineAsm);
- PerFunctionMIParsingState PFS;
- if (initializeRegisterInfo(MF, YamlMF, PFS))
+ if (YamlMF.AllVRegsAllocated)
+ MF.getProperties().set(MachineFunctionProperties::Property::AllVRegsAllocated);
+ PerFunctionMIParsingState PFS(MF, SM, IRSlots);
+ if (initializeRegisterInfo(PFS, YamlMF))
return true;
if (!YamlMF.Constants.empty()) {
auto *ConstantPool = MF.getConstantPool();
assert(ConstantPool && "Constant pool must be created");
- if (initializeConstantPool(*ConstantPool, YamlMF, MF,
- PFS.ConstantPoolSlots))
+ if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
return true;
}
+ StringRef BlockStr = YamlMF.Body.Value.Value;
SMDiagnostic Error;
- if (parseMachineBasicBlockDefinitions(MF, YamlMF.Body.Value.Value, PFS,
- 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()) +
"' requires at least one machine basic block in its body");
// Initialize the frame information after creating all the MBBs so that the
// MBB references in the frame information can be resolved.
- if (initializeFrameInfo(MF, YamlMF, PFS))
+ if (initializeFrameInfo(PFS, YamlMF))
return true;
// Initialize the jump table after creating all the MBBs so that the MBB
// references can be resolved.
if (!YamlMF.JumpTableInfo.Entries.empty() &&
- initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
+ initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
return true;
// Parse the machine instructions after creating all of the MBBs so that the
// parser can resolve the MBB references.
- if (parseMachineInstructions(MF, YamlMF.Body.Value.Value, PFS, 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);
@@ -332,9 +351,9 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
return false;
}
-bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
- const yaml::MachineFunction &YamlMF,
- PerFunctionMIParsingState &PFS) {
+bool MIRParserImpl::initializeRegisterInfo(PerFunctionMIParsingState &PFS,
+ const yaml::MachineFunction &YamlMF) {
+ MachineFunction &MF = PFS.MF;
MachineRegisterInfo &RegInfo = MF.getRegInfo();
assert(RegInfo.isSSA());
if (!YamlMF.IsSSA)
@@ -347,12 +366,28 @@ bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
SMDiagnostic Error;
// Parse the virtual register information.
for (const auto &VReg : YamlMF.VirtualRegisters) {
- const auto *RC = getRegClass(MF, VReg.Class.Value);
- if (!RC)
- return error(VReg.Class.SourceRange.Start,
- Twine("use of undefined register class '") +
- VReg.Class.Value + "'");
- unsigned Reg = RegInfo.createVirtualRegister(RC);
+ unsigned Reg;
+ if (StringRef(VReg.Class.Value).equals("_")) {
+ // This is a generic virtual register.
+ // The size will be set appropriately when we reach the definition.
+ Reg = RegInfo.createGenericVirtualRegister(/*Size*/ 1);
+ PFS.GenericVRegs.insert(Reg);
+ } else {
+ const auto *RC = getRegClass(MF, VReg.Class.Value);
+ if (RC) {
+ Reg = RegInfo.createVirtualRegister(RC);
+ } else {
+ const auto *RegBank = getRegBank(MF, VReg.Class.Value);
+ if (!RegBank)
+ return error(
+ VReg.Class.SourceRange.Start,
+ Twine("use of undefined register class or register bank '") +
+ VReg.Class.Value + "'");
+ Reg = RegInfo.createGenericVirtualRegister(/*Size*/ 1);
+ RegInfo.setRegBank(Reg, *RegBank);
+ PFS.GenericVRegs.insert(Reg);
+ }
+ }
if (!PFS.VirtualRegisterSlots.insert(std::make_pair(VReg.ID.Value, Reg))
.second)
return error(VReg.ID.SourceRange.Start,
@@ -360,9 +395,8 @@ bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
Twine(VReg.ID.Value) + "'");
if (!VReg.PreferredRegister.Value.empty()) {
unsigned PreferredReg = 0;
- if (parseNamedRegisterReference(PreferredReg, SM, MF,
- VReg.PreferredRegister.Value, PFS,
- IRSlots, Error))
+ if (parseNamedRegisterReference(PFS, PreferredReg,
+ VReg.PreferredRegister.Value, Error))
return error(Error, VReg.PreferredRegister.SourceRange);
RegInfo.setSimpleHint(Reg, PreferredReg);
}
@@ -371,13 +405,12 @@ bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
// Parse the liveins.
for (const auto &LiveIn : YamlMF.LiveIns) {
unsigned Reg = 0;
- if (parseNamedRegisterReference(Reg, SM, MF, LiveIn.Register.Value, PFS,
- 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(
- VReg, SM, MF, LiveIn.VirtualRegister.Value, PFS, IRSlots, Error))
+ if (parseVirtualRegisterReference(PFS, VReg, LiveIn.VirtualRegister.Value,
+ Error))
return error(Error, LiveIn.VirtualRegister.SourceRange);
}
RegInfo.addLiveIn(Reg, VReg);
@@ -389,8 +422,7 @@ bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
return false;
for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
unsigned Reg = 0;
- if (parseNamedRegisterReference(Reg, SM, MF, RegSource.Value, PFS, IRSlots,
- Error))
+ if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
return error(Error, RegSource.SourceRange);
CalleeSavedRegisterMask[Reg] = true;
}
@@ -398,24 +430,25 @@ bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
return false;
}
-void MIRParserImpl::inferRegisterInfo(MachineFunction &MF,
+void MIRParserImpl::inferRegisterInfo(const PerFunctionMIParsingState &PFS,
const yaml::MachineFunction &YamlMF) {
if (YamlMF.CalleeSavedRegisters)
return;
- for (const MachineBasicBlock &MBB : MF) {
+ MachineRegisterInfo &MRI = PFS.MF.getRegInfo();
+ for (const MachineBasicBlock &MBB : PFS.MF) {
for (const MachineInstr &MI : MBB) {
for (const MachineOperand &MO : MI.operands()) {
if (!MO.isRegMask())
continue;
- MF.getRegInfo().addPhysRegsUsedFromRegMask(MO.getRegMask());
+ MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
}
}
}
}
-bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF,
- const yaml::MachineFunction &YamlMF,
- PerFunctionMIParsingState &PFS) {
+bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
+ const yaml::MachineFunction &YamlMF) {
+ MachineFunction &MF = PFS.MF;
MachineFrameInfo &MFI = *MF.getFrameInfo();
const Function &F = *MF.getFunction();
const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
@@ -435,13 +468,13 @@ bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF,
MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
if (!YamlMFI.SavePoint.Value.empty()) {
MachineBasicBlock *MBB = nullptr;
- if (parseMBBReference(MBB, YamlMFI.SavePoint, MF, PFS))
+ if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
return true;
MFI.setSavePoint(MBB);
}
if (!YamlMFI.RestorePoint.Value.empty()) {
MachineBasicBlock *MBB = nullptr;
- if (parseMBBReference(MBB, YamlMFI.RestorePoint, MF, PFS))
+ if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
return true;
MFI.setRestorePoint(MBB);
}
@@ -462,7 +495,7 @@ bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF,
return error(Object.ID.SourceRange.Start,
Twine("redefinition of fixed stack object '%fixed-stack.") +
Twine(Object.ID.Value) + "'");
- if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
+ if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
ObjectIdx))
return true;
}
@@ -493,12 +526,12 @@ bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF,
return error(Object.ID.SourceRange.Start,
Twine("redefinition of stack object '%stack.") +
Twine(Object.ID.Value) + "'");
- if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
+ if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
ObjectIdx))
return true;
if (Object.LocalOffset)
MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
- if (parseStackObjectsDebugInfo(MF, PFS, Object, ObjectIdx))
+ if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
return true;
}
MFI.setCalleeSavedInfo(CSIInfo);
@@ -510,24 +543,21 @@ bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF,
if (!YamlMFI.StackProtector.Value.empty()) {
SMDiagnostic Error;
int FI;
- if (parseStackObjectReference(FI, SM, MF, YamlMFI.StackProtector.Value, PFS,
- IRSlots, Error))
+ if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
return error(Error, YamlMFI.StackProtector.SourceRange);
MFI.setStackProtectorIndex(FI);
}
return false;
}
-bool MIRParserImpl::parseCalleeSavedRegister(
- MachineFunction &MF, PerFunctionMIParsingState &PFS,
+bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
std::vector<CalleeSavedInfo> &CSIInfo,
const yaml::StringValue &RegisterSource, int FrameIdx) {
if (RegisterSource.Value.empty())
return false;
unsigned Reg = 0;
SMDiagnostic Error;
- if (parseNamedRegisterReference(Reg, SM, MF, RegisterSource.Value, PFS,
- IRSlots, Error))
+ if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
return error(Error, RegisterSource.SourceRange);
CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx));
return false;
@@ -548,16 +578,15 @@ static bool typecheckMDNode(T *&Result, MDNode *Node,
return false;
}
-bool MIRParserImpl::parseStackObjectsDebugInfo(
- MachineFunction &MF, PerFunctionMIParsingState &PFS,
+bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
const yaml::MachineStackObject &Object, int FrameIdx) {
// Debug information can only be attached to stack objects; Fixed stack
// objects aren't supported.
assert(FrameIdx >= 0 && "Expected a stack object frame index");
MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
- if (parseMDNode(Var, Object.DebugVar, MF, PFS) ||
- parseMDNode(Expr, Object.DebugExpr, MF, PFS) ||
- parseMDNode(Loc, Object.DebugLoc, MF, PFS))
+ if (parseMDNode(PFS, Var, Object.DebugVar) ||
+ parseMDNode(PFS, Expr, Object.DebugExpr) ||
+ parseMDNode(PFS, Loc, Object.DebugLoc))
return true;
if (!Var && !Expr && !Loc)
return false;
@@ -568,25 +597,24 @@ bool MIRParserImpl::parseStackObjectsDebugInfo(
typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
return true;
- MF.getMMI().setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc);
+ PFS.MF.getMMI().setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc);
return false;
}
-bool MIRParserImpl::parseMDNode(MDNode *&Node, const yaml::StringValue &Source,
- MachineFunction &MF,
- const PerFunctionMIParsingState &PFS) {
+bool MIRParserImpl::parseMDNode(const PerFunctionMIParsingState &PFS,
+ MDNode *&Node, const yaml::StringValue &Source) {
if (Source.Value.empty())
return false;
SMDiagnostic Error;
- if (llvm::parseMDNode(Node, SM, MF, Source.Value, PFS, IRSlots, Error))
+ if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
return error(Error, Source.SourceRange);
return false;
}
-bool MIRParserImpl::initializeConstantPool(
- MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF,
- const MachineFunction &MF,
- DenseMap<unsigned, unsigned> &ConstantPoolSlots) {
+bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
+ MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
+ DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
+ const MachineFunction &MF = PFS.MF;
const auto &M = *MF.getFunction()->getParent();
SMDiagnostic Error;
for (const auto &YamlConstant : YamlMF.Constants) {
@@ -608,15 +636,14 @@ bool MIRParserImpl::initializeConstantPool(
return false;
}
-bool MIRParserImpl::initializeJumpTableInfo(
- MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
- PerFunctionMIParsingState &PFS) {
- MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
+bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
+ const yaml::MachineJumpTable &YamlJTI) {
+ MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
for (const auto &Entry : YamlJTI.Entries) {
std::vector<MachineBasicBlock *> Blocks;
for (const auto &MBBSource : Entry.Blocks) {
MachineBasicBlock *MBB = nullptr;
- if (parseMBBReference(MBB, MBBSource.Value, MF, PFS))
+ if (parseMBBReference(PFS, MBB, MBBSource.Value))
return true;
Blocks.push_back(MBB);
}
@@ -630,12 +657,11 @@ bool MIRParserImpl::initializeJumpTableInfo(
return false;
}
-bool MIRParserImpl::parseMBBReference(MachineBasicBlock *&MBB,
- const yaml::StringValue &Source,
- MachineFunction &MF,
- const PerFunctionMIParsingState &PFS) {
+bool MIRParserImpl::parseMBBReference(const PerFunctionMIParsingState &PFS,
+ MachineBasicBlock *&MBB,
+ const yaml::StringValue &Source) {
SMDiagnostic Error;
- if (llvm::parseMBBReference(MBB, SM, MF, Source.Value, PFS, IRSlots, Error))
+ if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
return error(Error, Source.SourceRange);
return false;
}
@@ -698,6 +724,21 @@ void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
}
}
+void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
+ if (!Names2RegBanks.empty())
+ return;
+ const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
+ // If the target does not support GlobalISel, we may not have a
+ // register bank info.
+ if (!RBI)
+ return;
+ for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
+ const auto &RegBank = RBI->getRegBank(I);
+ Names2RegBanks.insert(
+ std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
+ }
+}
+
const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
StringRef Name) {
initNames2RegClasses(MF);
@@ -707,6 +748,15 @@ const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
return RegClassInfo->getValue();
}
+const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
+ StringRef Name) {
+ initNames2RegBanks(MF);
+ auto RegBankInfo = Names2RegBanks.find(Name);
+ if (RegBankInfo == Names2RegBanks.end())
+ return nullptr;
+ return RegBankInfo->getValue();
+}
+
MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
: Impl(std::move(Impl)) {}
diff --git a/lib/CodeGen/MIRParser/Makefile b/lib/CodeGen/MIRParser/Makefile
deleted file mode 100644
index c02d18806a9c2..0000000000000
--- a/lib/CodeGen/MIRParser/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-##===- lib/CodeGen/MIRParser/Makefile ----------------------*- Makefile -*-===##
-#
-# The LLVM Compiler Infrastructure
-#
-# This file is distributed under the University of Illinois Open Source
-# License. See LICENSE.TXT for details.
-#
-##===----------------------------------------------------------------------===##
-
-LEVEL = ../../..
-LIBRARYNAME = LLVMMIRParser
-
-include $(LEVEL)/Makefile.common