Skip to content

Commit 7c9f068

Browse files
committed
Approved by Chris:
$ svn merge -c 113557 https://llvm.org/svn/llvm-project/llvm/trunk --- Merging r113557 into '.': U include/llvm/Bitcode/LLVMBitCodes.h U tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp U lib/Bitcode/Reader/BitcodeReader.cpp U lib/Bitcode/Writer/BitcodeWriter.cpp $ svn merge -c 113764 https://llvm.org/svn/llvm-project/llvm/trunk --- Merging r113764 into '.': U lib/Bitcode/Reader/BitcodeReader.h G lib/Bitcode/Reader/BitcodeReader.cpp $ svn merge -c 113828 https://llvm.org/svn/llvm-project/llvm/trunk --- Merging r113828 into '.': U lib/VMCore/Metadata.cpp llvm-svn: 113853
1 parent df49eed commit 7c9f068

File tree

6 files changed

+111
-25
lines changed

6 files changed

+111
-25
lines changed

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,20 @@ namespace bitc {
110110

111111
enum MetadataCodes {
112112
METADATA_STRING = 1, // MDSTRING: [values]
113-
METADATA_NODE = 2, // MDNODE: [n x (type num, value num)]
114-
METADATA_FN_NODE = 3, // FN_MDNODE: [n x (type num, value num)]
113+
// FIXME: Remove NODE in favor of NODE2 in LLVM 3.0
114+
METADATA_NODE = 2, // NODE with potentially invalid metadata
115+
// FIXME: Remove FN_NODE in favor of FN_NODE2 in LLVM 3.0
116+
METADATA_FN_NODE = 3, // FN_NODE with potentially invalid metadata
115117
METADATA_NAME = 4, // STRING: [values]
116-
METADATA_NAMED_NODE = 5, // NAMEDMDNODE: [n x mdnodes]
118+
// FIXME: Remove NAMED_NODE in favor of NAMED_NODE2 in LLVM 3.0
119+
METADATA_NAMED_NODE = 5, // NAMED_NODE with potentially invalid metadata
117120
METADATA_KIND = 6, // [n x [id, name]]
118-
METADATA_ATTACHMENT = 7 // [m x [value, [n x [id, mdnode]]]
121+
// FIXME: Remove ATTACHMENT in favor of ATTACHMENT2 in LLVM 3.0
122+
METADATA_ATTACHMENT = 7, // ATTACHMENT with potentially invalid metadata
123+
METADATA_NODE2 = 8, // NODE2: [n x (type num, value num)]
124+
METADATA_FN_NODE2 = 9, // FN_NODE2: [n x (type num, value num)]
125+
METADATA_NAMED_NODE2 = 10, // NAMED_NODE2: [n x mdnodes]
126+
METADATA_ATTACHMENT2 = 11 // [m x [value, [n x [id, mdnode]]]
119127
};
120128
// The constants block (CONSTANTS_BLOCK_ID) describes emission for each
121129
// constant and maintains an implicit current type value.
@@ -223,7 +231,8 @@ namespace bitc {
223231
FUNC_CODE_INST_LOAD = 20, // LOAD: [opty, op, align, vol]
224232
// FIXME: Remove STORE in favor of STORE2 in LLVM 3.0
225233
FUNC_CODE_INST_STORE = 21, // STORE: [valty,val,ptr, align, vol]
226-
FUNC_CODE_INST_CALL = 22, // CALL: [attr, fnty, fnid, args...]
234+
// FIXME: Remove CALL in favor of CALL2 in LLVM 3.0
235+
FUNC_CODE_INST_CALL = 22, // CALL with potentially invalid metadata
227236
FUNC_CODE_INST_VAARG = 23, // VAARG: [valistty, valist, instty]
228237
// This store code encodes the pointer type, rather than the value type
229238
// this is so information only available in the pointer type (e.g. address
@@ -241,8 +250,13 @@ namespace bitc {
241250
FUNC_CODE_INST_INBOUNDS_GEP= 30, // INBOUNDS_GEP: [n x operands]
242251
FUNC_CODE_INST_INDIRECTBR = 31, // INDIRECTBR: [opty, op0, op1, ...]
243252

244-
FUNC_CODE_DEBUG_LOC = 32, // DEBUG_LOC: [Line,Col,ScopeVal, IAVal]
245-
FUNC_CODE_DEBUG_LOC_AGAIN = 33 // DEBUG_LOC_AGAIN
253+
// FIXME: Remove DEBUG_LOC in favor of DEBUG_LOC2 in LLVM 3.0
254+
FUNC_CODE_DEBUG_LOC = 32, // DEBUG_LOC with potentially invalid metadata
255+
FUNC_CODE_DEBUG_LOC_AGAIN = 33, // DEBUG_LOC_AGAIN
256+
257+
FUNC_CODE_INST_CALL2 = 34, // CALL2: [attr, fnty, fnid, args...]
258+
259+
FUNC_CODE_DEBUG_LOC2 = 35 // DEBUG_LOC2: [Line,Col,ScopeVal, IAVal]
246260
};
247261
} // End bitc namespace
248262
} // End llvm namespace

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,8 @@ bool BitcodeReader::ParseMetadata() {
774774
bool IsFunctionLocal = false;
775775
// Read a record.
776776
Record.clear();
777-
switch (Stream.ReadRecord(Code, Record)) {
777+
Code = Stream.ReadRecord(Code, Record);
778+
switch (Code) {
778779
default: // Default behavior: ignore.
779780
break;
780781
case bitc::METADATA_NAME: {
@@ -787,8 +788,12 @@ bool BitcodeReader::ParseMetadata() {
787788
Record.clear();
788789
Code = Stream.ReadCode();
789790

790-
// METADATA_NAME is always followed by METADATA_NAMED_NODE.
791-
if (Stream.ReadRecord(Code, Record) != bitc::METADATA_NAMED_NODE)
791+
// METADATA_NAME is always followed by METADATA_NAMED_NODE2.
792+
// Or METADATA_NAMED_NODE in LLVM 2.7. FIXME: Remove this in LLVM 3.0.
793+
unsigned NextBitCode = Stream.ReadRecord(Code, Record);
794+
if (NextBitCode == bitc::METADATA_NAMED_NODE) {
795+
LLVM2_7MetadataDetected = true;
796+
} else if (NextBitCode != bitc::METADATA_NAMED_NODE2)
792797
assert ( 0 && "Inavlid Named Metadata record");
793798

794799
// Read named metadata elements.
@@ -800,14 +805,29 @@ bool BitcodeReader::ParseMetadata() {
800805
return Error("Malformed metadata record");
801806
NMD->addOperand(MD);
802807
}
808+
// Backwards compatibility hack: NamedMDValues used to be Values,
809+
// and they got their own slots in the value numbering. They are no
810+
// longer Values, however we still need to account for them in the
811+
// numbering in order to be able to read old bitcode files.
812+
// FIXME: Remove this in LLVM 3.0.
813+
if (LLVM2_7MetadataDetected)
814+
MDValueList.AssignValue(0, NextMDValueNo++);
803815
break;
804816
}
805-
case bitc::METADATA_FN_NODE:
817+
case bitc::METADATA_FN_NODE: // FIXME: Remove in LLVM 3.0.
818+
case bitc::METADATA_FN_NODE2:
806819
IsFunctionLocal = true;
807820
// fall-through
808-
case bitc::METADATA_NODE: {
821+
case bitc::METADATA_NODE: // FIXME: Remove in LLVM 3.0.
822+
case bitc::METADATA_NODE2: {
823+
824+
// Detect 2.7-era metadata.
825+
// FIXME: Remove in LLVM 3.0.
826+
if (Code == bitc::METADATA_FN_NODE || Code == bitc::METADATA_NODE)
827+
LLVM2_7MetadataDetected = true;
828+
809829
if (Record.size() % 2 == 1)
810-
return Error("Invalid METADATA_NODE record");
830+
return Error("Invalid METADATA_NODE2 record");
811831

812832
unsigned Size = Record.size();
813833
SmallVector<Value*, 8> Elts;
@@ -1594,7 +1614,10 @@ bool BitcodeReader::ParseMetadataAttachment() {
15941614
switch (Stream.ReadRecord(Code, Record)) {
15951615
default: // Default behavior: ignore.
15961616
break;
1597-
case bitc::METADATA_ATTACHMENT: {
1617+
// FIXME: Remove in LLVM 3.0.
1618+
case bitc::METADATA_ATTACHMENT:
1619+
LLVM2_7MetadataDetected = true;
1620+
case bitc::METADATA_ATTACHMENT2: {
15981621
unsigned RecordLength = Record.size();
15991622
if (Record.empty() || (RecordLength - 1) % 2 == 1)
16001623
return Error ("Invalid METADATA_ATTACHMENT reader!");
@@ -1707,7 +1730,10 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
17071730
I = 0;
17081731
continue;
17091732

1710-
case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
1733+
// FIXME: Remove this in LLVM 3.0.
1734+
case bitc::FUNC_CODE_DEBUG_LOC:
1735+
LLVM2_7MetadataDetected = true;
1736+
case bitc::FUNC_CODE_DEBUG_LOC2: { // DEBUG_LOC: [line, col, scope, ia]
17111737
I = 0; // Get the last instruction emitted.
17121738
if (CurBB && !CurBB->empty())
17131739
I = &CurBB->back();
@@ -2169,7 +2195,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
21692195
}
21702196
case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
21712197
// For backward compatibility, tolerate a lack of an opty, and use i32.
2172-
// LLVM 3.0: Remove this.
2198+
// Remove this in LLVM 3.0.
21732199
if (Record.size() < 3 || Record.size() > 4)
21742200
return Error("Invalid ALLOCA record");
21752201
unsigned OpNum = 0;
@@ -2222,7 +2248,10 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
22222248
InstructionList.push_back(I);
22232249
break;
22242250
}
2225-
case bitc::FUNC_CODE_INST_CALL: {
2251+
// FIXME: Remove this in LLVM 3.0.
2252+
case bitc::FUNC_CODE_INST_CALL:
2253+
LLVM2_7MetadataDetected = true;
2254+
case bitc::FUNC_CODE_INST_CALL2: {
22262255
// CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
22272256
if (Record.size() < 3)
22282257
return Error("Invalid CALL record");
@@ -2341,9 +2370,21 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
23412370
BlockAddrFwdRefs.erase(BAFRI);
23422371
}
23432372

2373+
// FIXME: Remove this in LLVM 3.0.
2374+
unsigned NewMDValueListSize = MDValueList.size();
2375+
23442376
// Trim the value list down to the size it was before we parsed this function.
23452377
ValueList.shrinkTo(ModuleValueListSize);
23462378
MDValueList.shrinkTo(ModuleMDValueListSize);
2379+
2380+
// Backwards compatibility hack: Function-local metadata numbers
2381+
// were previously not reset between functions. This is now fixed,
2382+
// however we still need to understand the old numbering in order
2383+
// to be able to read old bitcode files.
2384+
// FIXME: Remove this in LLVM 3.0.
2385+
if (LLVM2_7MetadataDetected)
2386+
MDValueList.resize(NewMDValueListSize);
2387+
23472388
std::vector<BasicBlock*>().swap(FunctionBBs);
23482389

23492390
return false;

llvm/lib/Bitcode/Reader/BitcodeReader.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,18 @@ class BitcodeReader : public GVMaterializer {
173173
/// are resolved lazily when functions are loaded.
174174
typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
175175
DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
176+
177+
/// LLVM2_7MetadataDetected - True if metadata produced by LLVM 2.7 or
178+
/// earlier was detected, in which case we behave slightly differently,
179+
/// for compatibility.
180+
/// FIXME: Remove in LLVM 3.0.
181+
bool LLVM2_7MetadataDetected;
176182

177183
public:
178184
explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
179185
: Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
180-
ErrorString(0), ValueList(C), MDValueList(C) {
186+
ErrorString(0), ValueList(C), MDValueList(C),
187+
LLVM2_7MetadataDetected(false) {
181188
HasReversedFunctionsWithBodies = false;
182189
}
183190
~BitcodeReader() {

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ static void WriteMDNode(const MDNode *N,
485485
Record.push_back(0);
486486
}
487487
}
488-
unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE :
489-
bitc::METADATA_NODE;
488+
unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE2 :
489+
bitc::METADATA_NODE2;
490490
Stream.EmitRecord(MDCode, Record, 0);
491491
Record.clear();
492492
}
@@ -549,7 +549,7 @@ static void WriteModuleMetadata(const Module *M,
549549
// Write named metadata operands.
550550
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
551551
Record.push_back(VE.getValueID(NMD->getOperand(i)));
552-
Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
552+
Stream.EmitRecord(bitc::METADATA_NAMED_NODE2, Record, 0);
553553
Record.clear();
554554
}
555555

@@ -585,7 +585,7 @@ static void WriteMetadataAttachment(const Function &F,
585585
SmallVector<uint64_t, 64> Record;
586586

587587
// Write metadata attachments
588-
// METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
588+
// METADATA_ATTACHMENT2 - [m x [value, [n x [id, mdnode]]]
589589
SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
590590

591591
for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
@@ -603,7 +603,7 @@ static void WriteMetadataAttachment(const Function &F,
603603
Record.push_back(MDs[i].first);
604604
Record.push_back(VE.getValueID(MDs[i].second));
605605
}
606-
Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
606+
Stream.EmitRecord(bitc::METADATA_ATTACHMENT2, Record, 0);
607607
Record.clear();
608608
}
609609

@@ -1111,7 +1111,7 @@ static void WriteInstruction(const Instruction &I, unsigned InstID,
11111111
const PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType());
11121112
const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
11131113

1114-
Code = bitc::FUNC_CODE_INST_CALL;
1114+
Code = bitc::FUNC_CODE_INST_CALL2;
11151115

11161116
Vals.push_back(VE.getAttributeID(CI.getAttributes()));
11171117
Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()));
@@ -1255,7 +1255,7 @@ static void WriteFunction(const Function &F, ValueEnumerator &VE,
12551255
Vals.push_back(DL.getCol());
12561256
Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0);
12571257
Vals.push_back(IA ? VE.getValueID(IA)+1 : 0);
1258-
Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
1258+
Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC2, Vals);
12591259
Vals.clear();
12601260

12611261
LastDL = DL;

llvm/lib/VMCore/Metadata.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,22 @@ void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
354354

355355
// InsertPoint will have been set by the FindNodeOrInsertPos call.
356356
pImpl->MDNodeSet.InsertNode(this, InsertPoint);
357+
358+
// If this MDValue was previously function-local but no longer is, clear
359+
// its function-local flag.
360+
if (isFunctionLocal() && !isFunctionLocalValue(To)) {
361+
bool isStillFunctionLocal = false;
362+
for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
363+
Value *V = getOperand(i);
364+
if (!V) continue;
365+
if (isFunctionLocalValue(V)) {
366+
isStillFunctionLocal = true;
367+
break;
368+
}
369+
}
370+
if (!isStillFunctionLocal)
371+
setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
372+
}
357373
}
358374

359375
//===----------------------------------------------------------------------===//
@@ -387,6 +403,8 @@ MDNode *NamedMDNode::getOperand(unsigned i) const {
387403

388404
/// addOperand - Add metadata Operand.
389405
void NamedMDNode::addOperand(MDNode *M) {
406+
assert(!M->isFunctionLocal() &&
407+
"NamedMDNode operands must not be function-local!");
390408
getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
391409
}
392410

llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
236236
case bitc::FUNC_CODE_INST_VSELECT: return "INST_VSELECT";
237237
case bitc::FUNC_CODE_DEBUG_LOC: return "DEBUG_LOC";
238238
case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: return "DEBUG_LOC_AGAIN";
239+
case bitc::FUNC_CODE_INST_CALL2: return "INST_CALL2";
240+
case bitc::FUNC_CODE_DEBUG_LOC2: return "DEBUG_LOC2";
239241
}
240242
case bitc::TYPE_SYMTAB_BLOCK_ID:
241243
switch (CodeID) {
@@ -263,6 +265,10 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
263265
case bitc::METADATA_NAMED_NODE: return "METADATA_NAMED_NODE";
264266
case bitc::METADATA_KIND: return "METADATA_KIND";
265267
case bitc::METADATA_ATTACHMENT: return "METADATA_ATTACHMENT";
268+
case bitc::METADATA_NODE2: return "METADATA_NODE2";
269+
case bitc::METADATA_FN_NODE2: return "METADATA_FN_NODE2";
270+
case bitc::METADATA_NAMED_NODE2: return "METADATA_NAMED_NODE2";
271+
case bitc::METADATA_ATTACHMENT2: return "METADATA_ATTACHMENT2";
266272
}
267273
}
268274
}

0 commit comments

Comments
 (0)