summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/AsmParser/LLParser.cpp120
-rw-r--r--llvm/lib/AsmParser/LLParser.h32
-rw-r--r--llvm/lib/Bitcode/Reader/BitcodeReader.cpp14
-rw-r--r--llvm/lib/Bitcode/Writer/BitcodeWriter.cpp39
-rw-r--r--llvm/lib/Bitcode/Writer/ValueEnumerator.cpp4
-rw-r--r--llvm/lib/Bitcode/Writer/ValueEnumerator.h2
6 files changed, 208 insertions, 3 deletions
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 5d4fb8b7946..d209a2087dd 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -587,8 +587,11 @@ bool LLParser::ParseStandaloneMetadata() {
return TokError("unexpected type in metadata definition");
bool IsDistinct = EatIfPresent(lltok::kw_distinct);
- if (ParseToken(lltok::exclaim, "Expected '!' here") ||
- ParseMDTuple(Init, IsDistinct))
+ if (Lex.getKind() == lltok::MetadataVar) {
+ if (ParseSpecializedMDNode(Init, IsDistinct))
+ return true;
+ } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
+ ParseMDTuple(Init, IsDistinct))
return true;
// See if this was forward referenced, if so, handle it.
@@ -2902,7 +2905,11 @@ bool LLParser::ParseMDTuple(MDNode *&MD, bool IsDistinct) {
/// MDNode:
/// ::= !{ ... }
/// ::= !7
+/// ::= !MDLocation(...)
bool LLParser::ParseMDNode(MDNode *&N) {
+ if (Lex.getKind() == lltok::MetadataVar)
+ return ParseSpecializedMDNode(N);
+
return ParseToken(lltok::exclaim, "expected '!' here") ||
ParseMDNodeTail(N);
}
@@ -2916,6 +2923,106 @@ bool LLParser::ParseMDNodeTail(MDNode *&N) {
return ParseMDNodeID(N);
}
+bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
+ MDUnsignedField<uint32_t> &Result) {
+ if (Result.Seen)
+ return Error(Loc,
+ "field '" + Name + "' cannot be specified more than once");
+
+ if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
+ return TokError("expected unsigned integer");
+ uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(Result.Max + 1ull);
+
+ if (Val64 > Result.Max)
+ return TokError("value for '" + Name + "' too large, limit is " +
+ Twine(Result.Max));
+ Result.assign(Val64);
+ Lex.Lex();
+ return false;
+}
+
+bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
+ if (Result.Seen)
+ return Error(Loc,
+ "field '" + Name + "' cannot be specified more than once");
+
+ Metadata *MD;
+ if (ParseMetadata(MD, nullptr))
+ return true;
+
+ Result.assign(MD);
+ return false;
+}
+
+template <class ParserTy>
+bool LLParser::ParseMDFieldsImpl(ParserTy parseField) {
+ assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
+ Lex.Lex();
+
+ if (ParseToken(lltok::lparen, "expected '(' here"))
+ return true;
+ if (EatIfPresent(lltok::rparen))
+ return false;
+
+ do {
+ if (Lex.getKind() != lltok::LabelStr)
+ return TokError("expected field label here");
+
+ if (parseField())
+ return true;
+ } while (EatIfPresent(lltok::comma));
+
+ return ParseToken(lltok::rparen, "expected ')' here");
+}
+
+bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
+ assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
+#define DISPATCH_TO_PARSER(CLASS) \
+ if (Lex.getStrVal() == #CLASS) \
+ return Parse##CLASS(N, IsDistinct);
+
+ DISPATCH_TO_PARSER(MDLocation);
+#undef DISPATCH_TO_PARSER
+
+ return TokError("expected metadata type");
+}
+
+#define PARSE_MD_FIELD(NAME) \
+ do { \
+ if (Lex.getStrVal() == #NAME) { \
+ LocTy Loc = Lex.getLoc(); \
+ Lex.Lex(); \
+ if (ParseMDField(Loc, #NAME, NAME)) \
+ return true; \
+ return false; \
+ } \
+ } while (0)
+
+/// ParseMDLocationFields:
+/// ::= !MDLocation(line: 43, column: 8, scope: !5, inlinedAt: !6)
+bool LLParser::ParseMDLocation(MDNode *&Result, bool IsDistinct) {
+ MDUnsignedField<uint32_t> line(0, ~0u >> 8);
+ MDUnsignedField<uint32_t> column(0, ~0u >> 24);
+ MDField scope;
+ MDField inlinedAt;
+ if (ParseMDFieldsImpl([&]() -> bool {
+ PARSE_MD_FIELD(line);
+ PARSE_MD_FIELD(column);
+ PARSE_MD_FIELD(scope);
+ PARSE_MD_FIELD(inlinedAt);
+ return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");
+ }))
+ return true;
+
+ if (!scope.Seen)
+ return TokError("missing required field 'scope'");
+
+ auto get = (IsDistinct ? MDLocation::getDistinct : MDLocation::get);
+ Result = get(Context, line.Val, column.Val, scope.Val, inlinedAt.Val);
+ return false;
+}
+#undef PARSE_MD_FIELD
+
/// ParseMetadataAsValue
/// ::= metadata i32 %local
/// ::= metadata i32 @global
@@ -2960,7 +3067,16 @@ bool LLParser::ParseValueAsMetadata(Metadata *&MD, PerFunctionState *PFS) {
/// ::= !42
/// ::= !{...}
/// ::= !"string"
+/// ::= !MDLocation(...)
bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) {
+ if (Lex.getKind() == lltok::MetadataVar) {
+ MDNode *N;
+ if (ParseSpecializedMDNode(N))
+ return true;
+ MD = N;
+ return false;
+ }
+
// ValueAsMetadata:
// <type> <value>
if (Lex.getKind() != lltok::exclaim)
diff --git a/llvm/lib/AsmParser/LLParser.h b/llvm/lib/AsmParser/LLParser.h
index 8607b70f312..220562d66fc 100644
--- a/llvm/lib/AsmParser/LLParser.h
+++ b/llvm/lib/AsmParser/LLParser.h
@@ -80,6 +80,31 @@ namespace llvm {
}
};
+ /// Structure to represent an optional metadata field.
+ template <class FieldTy> struct MDFieldImpl {
+ typedef MDFieldImpl ImplTy;
+ FieldTy Val;
+ bool Seen;
+
+ void assign(FieldTy Val) {
+ Seen = true;
+ this->Val = Val;
+ }
+
+ explicit MDFieldImpl(FieldTy Default) : Val(Default), Seen(false) {}
+ };
+ template <class NumTy> struct MDUnsignedField : public MDFieldImpl<NumTy> {
+ typedef typename MDUnsignedField::ImplTy ImplTy;
+ NumTy Max;
+
+ MDUnsignedField(NumTy Default = 0,
+ NumTy Max = std::numeric_limits<NumTy>::max())
+ : ImplTy(Default), Max(Max) {}
+ };
+ struct MDField : public MDFieldImpl<Metadata *> {
+ MDField() : ImplTy(nullptr) {}
+ };
+
class LLParser {
public:
typedef LLLexer::LocTy LocTy;
@@ -393,6 +418,13 @@ namespace llvm {
bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &MDs);
bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
+ bool ParseMDField(LocTy Loc, StringRef Name,
+ MDUnsignedField<uint32_t> &Result);
+ bool ParseMDField(LocTy Loc, StringRef Name, MDField &Result);
+ template <class ParserTy> bool ParseMDFieldsImpl(ParserTy parseField);
+ bool ParseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
+ bool ParseMDLocation(MDNode *&Result, bool IsDistinct);
+
// Function Parsing.
struct ArgInfo {
LocTy Loc;
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 6f58f2241a6..60a380d5d26 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1267,6 +1267,20 @@ std::error_code BitcodeReader::ParseMetadata() {
NextMDValueNo++);
break;
}
+ case bitc::METADATA_LOCATION: {
+ if (Record.size() != 5)
+ return Error("Invalid record");
+
+ auto get = Record[0] ? MDLocation::getDistinct : MDLocation::get;
+ unsigned Line = Record[1];
+ unsigned Column = Record[2];
+ MDNode *Scope = cast<MDNode>(MDValueList.getValueFwdRef(Record[3]));
+ Metadata *InlinedAt =
+ Record[4] ? MDValueList.getValueFwdRef(Record[4] - 1) : nullptr;
+ MDValueList.AssignValue(get(Context, Line, Column, Scope, InlinedAt),
+ NextMDValueNo++);
+ break;
+ }
case bitc::METADATA_STRING: {
std::string String(Record.begin(), Record.end());
llvm::UpgradeMDStringConstant(String);
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 9d14a1afb61..a96e866ed2c 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -779,6 +779,25 @@ static void WriteMDNode(const MDNode *N,
Record.clear();
}
+static void WriteMDLocation(const MDLocation *N, const ValueEnumerator &VE,
+ BitstreamWriter &Stream,
+ SmallVectorImpl<uint64_t> &Record,
+ unsigned Abbrev) {
+ Record.push_back(N->isDistinct());
+ Record.push_back(N->getLine());
+ Record.push_back(N->getColumn());
+ Record.push_back(VE.getMetadataID(N->getScope()));
+
+ // Always emit the inlined-at location, even though it's optional.
+ if (Metadata *InlinedAt = N->getInlinedAt())
+ Record.push_back(VE.getMetadataID(InlinedAt) + 1);
+ else
+ Record.push_back(0);
+
+ Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
+ Record.clear();
+}
+
static void WriteModuleMetadata(const Module *M,
const ValueEnumerator &VE,
BitstreamWriter &Stream) {
@@ -798,6 +817,22 @@ static void WriteModuleMetadata(const Module *M,
MDSAbbrev = Stream.EmitAbbrev(Abbv);
}
+ unsigned LocAbbrev = 0;
+ if (VE.hasMDLocation()) {
+ // Abbrev for METADATA_LOCATION.
+ //
+ // Assume the column is usually under 128, and always output the inlined-at
+ // location (it's never more expensive than building an array size 1).
+ BitCodeAbbrev *Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
+ LocAbbrev = Stream.EmitAbbrev(Abbv);
+ }
+
unsigned NameAbbrev = 0;
if (!M->named_metadata_empty()) {
// Abbrev for METADATA_NAME.
@@ -810,6 +845,10 @@ static void WriteModuleMetadata(const Module *M,
SmallVector<uint64_t, 64> Record;
for (const Metadata *MD : MDs) {
+ if (const MDLocation *Loc = dyn_cast<MDLocation>(MD)) {
+ WriteMDLocation(Loc, VE, Stream, Record, LocAbbrev);
+ continue;
+ }
if (const MDNode *N = dyn_cast<MDNode>(MD)) {
WriteMDNode(N, VE, Stream, Record);
continue;
diff --git a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
index b117d7bb49a..27a63d8fefc 100644
--- a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -282,7 +282,8 @@ static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
return V.first->getType()->isIntOrIntVectorTy();
}
-ValueEnumerator::ValueEnumerator(const Module &M) : HasMDString(false) {
+ValueEnumerator::ValueEnumerator(const Module &M)
+ : HasMDString(false), HasMDLocation(false) {
if (shouldPreserveBitcodeUseListOrder())
UseListOrders = predictUseListOrder(M);
@@ -547,6 +548,7 @@ void ValueEnumerator::EnumerateMetadata(const Metadata *MD) {
EnumerateValue(C->getValue());
HasMDString |= isa<MDString>(MD);
+ HasMDLocation |= isa<MDLocation>(MD);
// Replace the dummy ID inserted above with the correct one. MDValueMap may
// have changed by inserting operands, so we need a fresh lookup here.
diff --git a/llvm/lib/Bitcode/Writer/ValueEnumerator.h b/llvm/lib/Bitcode/Writer/ValueEnumerator.h
index e63394fefa5..d363c1be0dd 100644
--- a/llvm/lib/Bitcode/Writer/ValueEnumerator.h
+++ b/llvm/lib/Bitcode/Writer/ValueEnumerator.h
@@ -65,6 +65,7 @@ private:
typedef DenseMap<const Metadata *, unsigned> MetadataMapType;
MetadataMapType MDValueMap;
bool HasMDString;
+ bool HasMDLocation;
typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
AttributeGroupMapType AttributeGroupMap;
@@ -111,6 +112,7 @@ public:
unsigned getMetadataID(const Metadata *V) const;
bool hasMDString() const { return HasMDString; }
+ bool hasMDLocation() const { return HasMDLocation; }
unsigned getTypeID(Type *T) const {
TypeMapType::const_iterator I = TypeMap.find(T);
OpenPOWER on IntegriCloud