llvm.org GIT mirror llvm / e39dba9
Bitcode: Add METADATA_NODE and METADATA_VALUE This reflects the typelessness of `Metadata` in the bitcode format, removing types from all metadata operands. `METADATA_VALUE` represents a `ValueAsMetadata`, and always has two fields: the type and the value. `METADATA_NODE` represents an `MDNode`, and unlike `METADATA_OLD_NODE`, doesn't store types. It stores operands at their ID+1 so that `0` can reference `nullptr` operands. Part of PR21532. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224073 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 5 years ago
7 changed file(s) with 62 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
136136
137137 enum MetadataCodes {
138138 METADATA_STRING = 1, // MDSTRING: [values]
139 // 2 is unused.
140 // 3 is unused.
139 METADATA_VALUE = 2, // VALUE: [type num, value num]
140 METADATA_NODE = 3, // NODE: [n x md num]
141141 METADATA_NAME = 4, // STRING: [values]
142142 // 5 is unused.
143143 METADATA_KIND = 6, // [n x [id, name]]
11651165 MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++);
11661166 break;
11671167 }
1168 case bitc::METADATA_VALUE: {
1169 if (Record.size() != 2)
1170 return Error(BitcodeError::InvalidRecord);
1171
1172 Type *Ty = getTypeByID(Record[0]);
1173 if (Ty->isMetadataTy() || Ty->isVoidTy())
1174 return Error(BitcodeError::InvalidRecord);
1175
1176 MDValueList.AssignValue(
1177 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
1178 NextMDValueNo++);
1179 break;
1180 }
1181 case bitc::METADATA_NODE: {
1182 SmallVector Elts;
1183 Elts.reserve(Record.size());
1184 for (unsigned ID : Record)
1185 Elts.push_back(ID ? MDValueList.getValueFwdRef(ID - 1) : nullptr);
1186 MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++);
1187 break;
1188 }
11681189 case bitc::METADATA_STRING: {
11691190 std::string String(Record.begin(), Record.end());
11701191 llvm::UpgradeMDStringConstant(String);
736736 return Flags;
737737 }
738738
739 static void WriteValueAsMetadataImpl(const ValueAsMetadata *MD,
740 const ValueEnumerator &VE,
741 BitstreamWriter &Stream,
742 SmallVectorImpl &Record,
743 unsigned Code) {
739 static void WriteValueAsMetadata(const ValueAsMetadata *MD,
740 const ValueEnumerator &VE,
741 BitstreamWriter &Stream,
742 SmallVectorImpl &Record) {
744743 // Mimic an MDNode with a value as one operand.
745744 Value *V = MD->getValue();
746745 Record.push_back(VE.getTypeID(V->getType()));
747746 Record.push_back(VE.getValueID(V));
748 Stream.EmitRecord(Code, Record, 0);
747 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
749748 Record.clear();
750 }
751
752 static void WriteLocalAsMetadata(const LocalAsMetadata *MD,
753 const ValueEnumerator &VE,
754 BitstreamWriter &Stream,
755 SmallVectorImpl &Record) {
756 WriteValueAsMetadataImpl(MD, VE, Stream, Record, bitc::METADATA_OLD_FN_NODE);
757 }
758
759 static void WriteConstantAsMetadata(const ConstantAsMetadata *MD,
760 const ValueEnumerator &VE,
761 BitstreamWriter &Stream,
762 SmallVectorImpl &Record) {
763 WriteValueAsMetadataImpl(MD, VE, Stream, Record, bitc::METADATA_OLD_NODE);
764749 }
765750
766751 static void WriteMDNode(const MDNode *N,
770755 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
771756 Metadata *MD = N->getOperand(i);
772757 if (!MD) {
773 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
774758 Record.push_back(0);
775759 continue;
776760 }
777 if (auto *V = dyn_cast(MD)) {
778 Record.push_back(VE.getTypeID(V->getValue()->getType()));
779 Record.push_back(VE.getValueID(V->getValue()));
780 continue;
781 }
782761 assert(!isa(MD) && "Unexpected function-local metadata");
783 Record.push_back(VE.getTypeID(Type::getMetadataTy(N->getContext())));
784 Record.push_back(VE.getMetadataID(MD));
785 }
786 Stream.EmitRecord(bitc::METADATA_OLD_NODE, Record, 0);
762 Record.push_back(VE.getMetadataID(MD) + 1);
763 }
764 Stream.EmitRecord(bitc::METADATA_NODE, Record);
787765 Record.clear();
788766 }
789767
806784 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
807785 StartedMetadataBlock = true;
808786 }
809 WriteConstantAsMetadata(MDC, VE, Stream, Record);
787 WriteValueAsMetadata(MDC, VE, Stream, Record);
810788 } else if (const MDString *MDS = dyn_cast(MDs[i])) {
811789 if (!StartedMetadataBlock) {
812790 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
869847 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
870848 StartedMetadataBlock = true;
871849 }
872 WriteLocalAsMetadata(MDs[i], VE, Stream, Record);
850 WriteValueAsMetadata(MDs[i], VE, Stream, Record);
873851 }
874852
875853 if (StartedMetadataBlock)
520520 void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
521521 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
522522 Metadata *MD = N->getOperand(i);
523 if (!MD) {
524 EnumerateType(Type::getVoidTy(N->getContext()));
523 if (!MD)
525524 continue;
526 }
527525 assert(!isa(MD) && "MDNodes cannot be function-local");
528 if (auto *C = dyn_cast(MD)) {
529 EnumerateValue(C->getValue());
530 continue;
531 }
532526 EnumerateMetadata(MD);
533527 }
534528 }
0 ; RUN: llvm-dis < %s.bc | FileCheck %s
1
2 ; Check that metadata encoded in 3.5 is correctly understood going forward.
3 ;
4 ; Bitcode assembled by llvm-as v3.5.0.
5
6 define void @foo(i32 %v) {
7 ; CHECK: entry:
8 entry:
9 ; CHECK-NEXT: call void @llvm.bar(metadata !0)
10 call void @llvm.bar(metadata !0)
11
12 ; CHECK-NEXT: ret void, !baz !1
13 ret void, !baz !1
14 }
15
16 declare void @llvm.bar(metadata)
17
18 @global = global i32 0
19
20 ; CHECK: !0 = metadata !{metadata !1, metadata !2, i32* @global, null}
21 ; CHECK: !1 = metadata !{metadata !2, null}
22 ; CHECK: !2 = metadata !{}
23 !0 = metadata !{metadata !1, metadata !2, i32* @global, null}
24 !1 = metadata !{metadata !2, null}
25 !2 = metadata !{}
265265 case bitc::METADATA_STRING: return "METADATA_STRING";
266266 case bitc::METADATA_NAME: return "METADATA_NAME";
267267 case bitc::METADATA_KIND: return "METADATA_KIND";
268 case bitc::METADATA_NODE: return "METADATA_NODE";
269 case bitc::METADATA_VALUE: return "METADATA_VALUE";
268270 case bitc::METADATA_OLD_NODE: return "METADATA_OLD_NODE";
269271 case bitc::METADATA_OLD_FN_NODE: return "METADATA_OLD_FN_NODE";
270272 case bitc::METADATA_NAMED_NODE: return "METADATA_NAMED_NODE";