llvm.org GIT mirror llvm / aa99314
Read and write NamedMDNode. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77517 91177308-0d34-0410-b5e6-96231b3b80d8 Devang Patel 11 years ago
5 changed file(s) with 78 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
107107 };
108108
109109 enum MetadataCodes {
110 METADATA_STRING = 1, // MDString: [values]
111 METADATA_NODE = 2 // MDNODE: [n x (type num, value num)]
110 METADATA_STRING = 1, // MDSTRING: [values]
111 METADATA_NODE = 2, // MDNODE: [n x (type num, value num)]
112 METADATA_NAME = 3, // STRING: [values]
113 METADATA_NAMED_NODE = 4 // NAMEDMDNODE: [n x mdnodes]
112114 };
113115 // The constants block (CONSTANTS_BLOCK_ID) describes emission for each
114116 // constant and maintains an implicit current type value.
733733 switch (Stream.ReadRecord(Code, Record)) {
734734 default: // Default behavior: ignore.
735735 break;
736 case bitc::METADATA_NAME: {
737 // Read named of the named metadata.
738 unsigned NameLength = Record.size();
739 SmallString<8> Name;
740 Name.resize(NameLength);
741 for (unsigned i = 0; i != NameLength; ++i)
742 Name[i] = Record[i];
743 Record.clear();
744 Code = Stream.ReadCode();
745
746 // METADATA_NAME is always followed by METADATA_NAMED_NODE.
747 if (Stream.ReadRecord(Code, Record) != bitc::METADATA_NAMED_NODE)
748 assert ( 0 && "Inavlid Named Metadata record");
749
750 // Read named metadata elements.
751 unsigned Size = Record.size();
752 SmallVector Elts;
753 for (unsigned i = 0; i != Size; ++i) {
754 Value *MD = ValueList.getValueFwdRef(Record[i], Type::MetadataTy);
755 if (MetadataBase *B = dyn_cast(MD))
756 Elts.push_back(B);
757 }
758 Value *V = NamedMDNode::Create(Name.c_str(), Elts.data(), Elts.size(),
759 TheModule);
760 ValueList.AssignValue(V, NextValueNo++);
761 break;
762 }
736763 case bitc::METADATA_NODE: {
737764 if (Record.empty() || Record.size() % 2 == 1)
738765 return Error("Invalid METADATA_NODE record");
494494 const ValueEnumerator::ValueList &Vals = VE.getValues();
495495 bool StartedMetadataBlock = false;
496496 unsigned MDSAbbrev = 0;
497 unsigned String8Abbrev = 0;
497498 SmallVector Record;
498499 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
499500
524525 // Emit the finished record.
525526 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
526527 Record.clear();
528 } else if (const NamedMDNode *NMD = dyn_cast(Vals[i].first)) {
529 if (!StartedMetadataBlock) {
530 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
531 StartedMetadataBlock = true;
532 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
533 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
534 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
535 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
536 String8Abbrev = Stream.EmitAbbrev(Abbv);
537 }
538
539 // Write name.
540 const char *StrBegin = NMD->getName().data();
541 for (unsigned i = 0, e = NMD->getName().size(); i != e; ++i)
542 Record.push_back(StrBegin[i]);
543 Stream.EmitRecord(bitc::METADATA_NAME, Record, String8Abbrev);
544 Record.clear();
545
546 // Write named metadata elements.
547 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
548 if (NMD->getElement(i))
549 Record.push_back(VE.getValueID(NMD->getElement(i)));
550 else
551 Record.push_back(0);
552 }
553 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
554 Record.clear();
527555 }
528556 }
529557
577605 const Type *LastTy = 0;
578606 for (unsigned i = FirstVal; i != LastVal; ++i) {
579607 const Value *V = Vals[i].first;
580 if (isaDString>(V) || isae>(V))
608 if (isaetadataBase>(V))
581609 continue;
582610 // If we need to switch types, do so now.
583611 if (V->getType() != LastTy) {
220220 return;
221221 }
222222
223 if (const NamedMDNode *N = dyn_cast(V)) {
224 Values.push_back(std::make_pair(V, 1U));
225 ValueMap[V] = Values.size();
226 ValueID = Values.size();
227 for(NamedMDNode::const_elem_iterator I = N->elem_begin(),
228 E = N->elem_end(); I != E; ++I) {
229 MetadataBase *M = *I;
230 EnumerateValue(M);
231 }
232 return;
233 }
234
223235 // Add the value.
224236 Values.push_back(std::make_pair(V, 1U));
225237 ValueID = Values.size();
0 ; RUN: llvm-as < %s | llvm-dis | grep "llvm.stuff = "
1
2 ;; Simple NamedMDNode
3 !0 = metadata !{i32 42}
4 !1 = metadata !{metadata !"foo"}
5 !llvm.stuff = !{!0, !1}