Introduce MetadataBase, a base class for MDString and MDNode.
Derive MDString directly from MetadataBase.
Introduce new bitcode block to hold metadata.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76759 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp
index d461a8d..9f708a8 100644
--- a/lib/AsmParser/LLParser.cpp
+++ b/lib/AsmParser/LLParser.cpp
@@ -42,7 +42,8 @@
t_Null, t_Undef, t_Zero, // No value.
t_EmptyArray, // No value: []
t_Constant, // Value in ConstantVal.
- t_InlineAsm // Value in StrVal/StrVal2/UIntVal.
+ t_InlineAsm, // Value in StrVal/StrVal2/UIntVal.
+ t_Metadata // Value in MetadataVal.
} Kind;
LLParser::LocTy Loc;
@@ -51,6 +52,7 @@
APSInt APSIntVal;
APFloat APFloatVal;
Constant *ConstantVal;
+ MetadataBase *MetadataVal;
ValID() : APFloatVal(0.0) {}
};
}
@@ -368,7 +370,7 @@
// MDString:
// ::= '!' STRINGCONSTANT
-bool LLParser::ParseMDString(Constant *&MDS) {
+bool LLParser::ParseMDString(MetadataBase *&MDS) {
std::string Str;
if (ParseStringConstant(Str)) return true;
MDS = Context.getMDString(Str.data(), Str.data() + Str.size());
@@ -1694,7 +1696,8 @@
// MDString:
// ::= '!' STRINGCONSTANT
- if (ParseMDString(ID.ConstantVal)) return true;
+ if (ParseMDString(ID.MetadataVal)) return true;
+ ID.Kind = ValID::t_Metadata;
return false;
}
case lltok::APSInt:
@@ -2107,7 +2110,9 @@
return Error(ID.Loc, "functions are not values, refer to them as pointers");
switch (ID.Kind) {
- default: llvm_unreachable("Unknown ValID!");
+ default: llvm_unreachable("Unknown ValID!");
+ case ValID::t_Metadata:
+ return Error(ID.Loc, "invalid use of metadata");
case ValID::t_LocalID:
case ValID::t_LocalName:
return Error(ID.Loc, "invalid use of function-local name");
@@ -2224,6 +2229,8 @@
return Error(ID.Loc, "invalid type for inline asm constraint string");
V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal);
return false;
+ } else if (ID.Kind == ValID::t_Metadata) {
+ V = ID.MetadataVal;
} else {
Constant *C;
if (ConvertGlobalValIDToValue(Ty, ID, C)) return true;
@@ -3451,9 +3458,23 @@
Lex.Lex();
V = 0;
} else {
- Constant *C;
- if (ParseGlobalTypeAndValue(C)) return true;
- V = C;
+ PATypeHolder Ty(Type::VoidTy);
+ if (ParseType(Ty)) return true;
+ if (Lex.getKind() == lltok::Metadata) {
+ Lex.Lex();
+ Constant *Node = 0;
+ if (!ParseMDNode(Node))
+ V = Node;
+ else {
+ MetadataBase *MDS = 0;
+ if (ParseMDString(MDS)) return true;
+ V = MDS;
+ }
+ } else {
+ Constant *C;
+ if (ParseGlobalValue(Ty, C)) return true;
+ V = C;
+ }
}
Elts.push_back(V);
} while (EatIfPresent(lltok::comma));
diff --git a/lib/AsmParser/LLParser.h b/lib/AsmParser/LLParser.h
index 57a1906..1a0b6cc 100644
--- a/lib/AsmParser/LLParser.h
+++ b/lib/AsmParser/LLParser.h
@@ -28,6 +28,7 @@
class Instruction;
class Constant;
class GlobalValue;
+ class MetadataBase;
class MDString;
class MDNode;
struct ValID;
@@ -147,7 +148,7 @@
bool HasLinkage, unsigned Visibility);
bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
bool ParseStandaloneMetadata();
- bool ParseMDString(Constant *&S);
+ bool ParseMDString(MetadataBase *&S);
bool ParseMDNode(Constant *&N);
// Type Parsing.
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp
index 95276b1..d22d467 100644
--- a/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -699,6 +699,56 @@
}
}
+bool BitcodeReader::ParseMetadata() {
+ unsigned NextValueNo = ValueList.size();
+
+ if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
+ return Error("Malformed block record");
+
+ SmallVector<uint64_t, 64> Record;
+
+ // Read all the records.
+ while (1) {
+ unsigned Code = Stream.ReadCode();
+ if (Code == bitc::END_BLOCK) {
+ if (Stream.ReadBlockEnd())
+ return Error("Error at end of PARAMATTR block");
+ return false;
+ }
+
+ if (Code == bitc::ENTER_SUBBLOCK) {
+ // No known subblocks, always skip them.
+ Stream.ReadSubBlockID();
+ if (Stream.SkipBlock())
+ return Error("Malformed block record");
+ continue;
+ }
+
+ if (Code == bitc::DEFINE_ABBREV) {
+ Stream.ReadAbbrevRecord();
+ continue;
+ }
+
+ // Read a record.
+ Record.clear();
+ switch (Stream.ReadRecord(Code, Record)) {
+ default: // Default behavior: ignore.
+ break;
+ case bitc::METADATA_STRING: {
+ unsigned MDStringLength = Record.size();
+ SmallString<8> String;
+ String.resize(MDStringLength);
+ for (unsigned i = 0; i != MDStringLength; ++i)
+ String[i] = Record[i];
+ Value *V =
+ Context.getMDString(String.c_str(), String.c_str() + MDStringLength);
+ ValueList.AssignValue(V, NextValueNo++);
+ break;
+ }
+ }
+ }
+}
+
/// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in
/// the LSB for dense VBR encoding.
static uint64_t DecodeSignRotatedValue(uint64_t V) {
@@ -1028,15 +1078,6 @@
AsmStr, ConstrStr, HasSideEffects);
break;
}
- case bitc::CST_CODE_MDSTRING: {
- unsigned MDStringLength = Record.size();
- SmallString<8> String;
- String.resize(MDStringLength);
- for (unsigned i = 0; i != MDStringLength; ++i)
- String[i] = Record[i];
- V = Context.getMDString(String.c_str(), String.c_str() + MDStringLength);
- break;
- }
case bitc::CST_CODE_MDNODE: {
if (Record.empty() || Record.size() % 2 == 1)
return Error("Invalid CST_MDNODE record");
@@ -1171,6 +1212,10 @@
if (ParseConstants() || ResolveGlobalAndAliasInits())
return true;
break;
+ case bitc::METADATA_BLOCK_ID:
+ if (ParseMetadata())
+ return true;
+ break;
case bitc::FUNCTION_BLOCK_ID:
// If this is the first function body we've seen, reverse the
// FunctionsWithBodies list.
diff --git a/lib/Bitcode/Reader/BitcodeReader.h b/lib/Bitcode/Reader/BitcodeReader.h
index bbbe4ec..77dabde 100644
--- a/lib/Bitcode/Reader/BitcodeReader.h
+++ b/lib/Bitcode/Reader/BitcodeReader.h
@@ -210,6 +210,7 @@
bool RememberAndSkipFunctionBody();
bool ParseFunctionBody(Function *F);
bool ResolveGlobalAndAliasInits();
+ bool ParseMetadata();
};
} // End llvm namespace
diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp
index d245e5bc..467762e 100644
--- a/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -473,6 +473,42 @@
return Flags;
}
+ static void WriteModuleMetadata(const ValueEnumerator &VE,
+ BitstreamWriter &Stream) {
+ const ValueEnumerator::ValueList &Vals = VE.getValues();
+ bool StartedMetadataBlock = false;
+ unsigned MDSAbbrev = 0;
+ for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
+ if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
+ if (!StartedMetadataBlock) {
+ Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
+
+ // Abbrev for CST_CODE_STRING.
+ BitCodeAbbrev *Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
+ MDSAbbrev = Stream.EmitAbbrev(Abbv);
+ StartedMetadataBlock = true;
+ }
+
+ SmallVector<unsigned, 64> StrVals;
+ StrVals.clear();
+ // Code: [strchar x N]
+ const char *StrBegin = MDS->begin();
+ for (unsigned i = 0, e = MDS->size(); i != e; ++i)
+ StrVals.push_back(StrBegin[i]);
+
+ // Emit the finished record.
+ Stream.EmitRecord(bitc::METADATA_STRING, StrVals, MDSAbbrev);
+ }
+ }
+
+ if (StartedMetadataBlock)
+ Stream.ExitBlock();
+}
+
+
static void WriteConstants(unsigned FirstVal, unsigned LastVal,
const ValueEnumerator &VE,
BitstreamWriter &Stream, bool isGlobal) {
@@ -484,8 +520,6 @@
unsigned String8Abbrev = 0;
unsigned CString7Abbrev = 0;
unsigned CString6Abbrev = 0;
- unsigned MDString8Abbrev = 0;
- unsigned MDString6Abbrev = 0;
// If this is a constant pool for the module, emit module-specific abbrevs.
if (isGlobal) {
// Abbrev for CST_CODE_AGGREGATE.
@@ -513,19 +547,6 @@
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
CString6Abbrev = Stream.EmitAbbrev(Abbv);
-
- // Abbrev for CST_CODE_MDSTRING.
- Abbv = new BitCodeAbbrev();
- Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_MDSTRING));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
- MDString8Abbrev = Stream.EmitAbbrev(Abbv);
- // Abbrev for CST_CODE_MDSTRING.
- Abbv = new BitCodeAbbrev();
- Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_MDSTRING));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
- Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
- MDString6Abbrev = Stream.EmitAbbrev(Abbv);
}
SmallVector<uint64_t, 64> Record;
@@ -534,6 +555,8 @@
const Type *LastTy = 0;
for (unsigned i = FirstVal; i != LastVal; ++i) {
const Value *V = Vals[i].first;
+ if (isa<MDString>(V))
+ continue;
// If we need to switch types, do so now.
if (V->getType() != LastTy) {
LastTy = V->getType();
@@ -713,16 +736,6 @@
Record.push_back(CE->getPredicate());
break;
}
- } else if (const MDString *S = dyn_cast<MDString>(C)) {
- Code = bitc::CST_CODE_MDSTRING;
- AbbrevToUse = MDString6Abbrev;
- for (unsigned i = 0, e = S->size(); i != e; ++i) {
- char V = S->begin()[i];
- Record.push_back(V);
-
- if (!BitCodeAbbrevOp::isChar6(V))
- AbbrevToUse = MDString8Abbrev;
- }
} else if (const MDNode *N = dyn_cast<MDNode>(C)) {
Code = bitc::CST_CODE_MDNODE;
for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
@@ -1328,7 +1341,10 @@
// Emit top-level description of module, including target triple, inline asm,
// descriptors for global variables, and function prototype info.
WriteModuleInfo(M, VE, Stream);
-
+
+ // Emit metadata.
+ WriteModuleMetadata(VE, Stream);
+
// Emit constants.
WriteModuleConstants(VE, Stream);
diff --git a/lib/Bitcode/Writer/ValueEnumerator.cpp b/lib/Bitcode/Writer/ValueEnumerator.cpp
index 32b2819..a31a492 100644
--- a/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -259,10 +259,14 @@
EnumerateOperandType(C->getOperand(i));
if (const MDNode *N = dyn_cast<MDNode>(V)) {
- for (unsigned i = 0, e = N->getNumElements(); i != e; ++i)
- EnumerateOperandType(N->getElement(i));
+ for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
+ Value *Elem = N->getElement(i);
+ if (Elem)
+ EnumerateOperandType(Elem);
+ }
}
- }
+ } else if (const MDString *MDS = dyn_cast<MDString>(V))
+ EnumerateValue(V);
}
void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp
index a091a93..17d0478 100644
--- a/lib/VMCore/AsmWriter.cpp
+++ b/lib/VMCore/AsmWriter.cpp
@@ -1063,13 +1063,6 @@
return;
}
- if (const MDString *S = dyn_cast<MDString>(CV)) {
- Out << "!\"";
- PrintEscapedString(S->begin(), S->size(), Out);
- Out << '"';
- return;
- }
-
if (const MDNode *Node = dyn_cast<MDNode>(CV)) {
Out << "!" << Machine->getMetadataSlot(Node);
return;
@@ -1138,7 +1131,16 @@
Out << '"';
return;
}
-
+
+ if (const MDString *MDS = dyn_cast<MDString>(V)) {
+ TypePrinter.print(MDS->getType(), Out);
+ Out << ' ';
+ Out << "!\"";
+ PrintEscapedString(MDS->begin(), MDS->size(), Out);
+ Out << '"';
+ return;
+ }
+
char Prefix = '%';
int Slot;
if (Machine) {
@@ -1973,6 +1975,13 @@
SlotTracker SlotTable(GV->getParent());
AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
W.write(GV);
+ } else if (const MDString *MDS = dyn_cast<MDString>(this)) {
+ TypePrinting TypePrinter;
+ TypePrinter.print(MDS->getType(), OS);
+ OS << ' ';
+ OS << "!\"";
+ PrintEscapedString(MDS->begin(), MDS->size(), OS);
+ OS << '"';
} else if (const MDNode *N = dyn_cast<MDNode>(this)) {
SlotTracker SlotTable(N);
TypePrinting TypePrinter;
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp
index 2eb7339..db40592 100644
--- a/lib/VMCore/Constants.cpp
+++ b/lib/VMCore/Constants.cpp
@@ -1323,18 +1323,6 @@
destroyConstantImpl();
}
-//---- MDString::get() implementation
-//
-
-MDString::MDString(const char *begin, const char *end)
- : Constant(Type::MetadataTy, MDStringVal, 0, 0),
- StrBegin(begin), StrEnd(end) {}
-
-void MDString::destroyConstant() {
- getType()->getContext().erase(this);
- destroyConstantImpl();
-}
-
//---- MDNode::get() implementation
//
diff --git a/lib/VMCore/Value.cpp b/lib/VMCore/Value.cpp
index 07fbfc6..b3c0692 100644
--- a/lib/VMCore/Value.cpp
+++ b/lib/VMCore/Value.cpp
@@ -18,6 +18,7 @@
#include "llvm/Instructions.h"
#include "llvm/Operator.h"
#include "llvm/Module.h"
+#include "llvm/MDNode.h"
#include "llvm/ValueSymbolTable.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
@@ -140,7 +141,9 @@
} else if (Argument *A = dyn_cast<Argument>(V)) {
if (Function *P = A->getParent())
ST = &P->getValueSymbolTable();
- } else {
+ } else if (isa<MDString>(V))
+ return true;
+ else {
assert(isa<Constant>(V) && "Unknown value type!");
return true; // no name is setable for this.
}