Revert recent bitcode writer patches.
authorDevang Patel <dpatel@apple.com>
Tue, 4 Aug 2009 05:01:35 +0000 (05:01 +0000)
committerDevang Patel <dpatel@apple.com>
Tue, 4 Aug 2009 05:01:35 +0000 (05:01 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78053 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Bitcode/Writer/BitcodeWriter.cpp
test/Bitcode/metadata.ll [deleted file]

index 724e1aeb15c96cb5d6b369cf315fcb27fc70a0d4..6bd40ba0e5bb45af566e1d81421086596ec76a17 100644 (file)
@@ -473,46 +473,50 @@ static uint64_t GetOptimizationFlags(const Value *V) {
   return Flags;
 }
 
-/// WriteValues - Write Constants and Metadata.
-/// This function could use some refactoring help.
-static void WriteValues(unsigned FirstVal, unsigned LastVal,
+static void WriteMDNode(const MDNode *N,
                         const ValueEnumerator &VE,
-                        BitstreamWriter &Stream, bool isGlobal) {
-  if (FirstVal == LastVal) return;
-
-  // MODULE_BLOCK_ID is 0, which is not handled here. So it is OK to use
-  // 0 as the initializer to indicate that block is not set.
-  enum bitc::BlockIDs LastBlockID = bitc::MODULE_BLOCK_ID;
+                        BitstreamWriter &Stream,
+                        SmallVector<uint64_t, 64> &Record) {
+  for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
+    if (N->getElement(i)) {
+      Record.push_back(VE.getTypeID(N->getElement(i)->getType()));
+      Record.push_back(VE.getValueID(N->getElement(i)));
+    } else {
+      Record.push_back(VE.getTypeID(Type::VoidTy));
+      Record.push_back(0);
+    }
+  }
+  Stream.EmitRecord(bitc::METADATA_NODE, Record, 0);
+  Record.clear();
+}
 
-  unsigned AggregateAbbrev = 0;
-  unsigned String8Abbrev = 0;
-  unsigned CString7Abbrev = 0;
-  unsigned CString6Abbrev = 0;
+static void WriteModuleMetadata(const ValueEnumerator &VE,
+                                BitstreamWriter &Stream) {
+  const ValueEnumerator::ValueList &Vals = VE.getValues();
+  bool StartedMetadataBlock = false;
   unsigned MDSAbbrev = 0;
-
   SmallVector<uint64_t, 64> Record;
-
-  const ValueEnumerator::ValueList &Vals = VE.getValues();
-  const Type *LastTy = 0;
-  for (unsigned i = FirstVal; i != LastVal; ++i) {
-    const Value *V = Vals[i].first;
-    if (isa<MetadataBase>(V)) {
-      if (LastBlockID != bitc::METADATA_BLOCK_ID) {
-        // Exit privious block.
-        if (LastBlockID != bitc::MODULE_BLOCK_ID)
-          Stream.ExitBlock();
-        
-        LastBlockID = bitc::METADATA_BLOCK_ID;
+  for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
+    
+    if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
+      if (!StartedMetadataBlock) {
+        Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
+        StartedMetadataBlock = true;
+      }
+      WriteMDNode(N, VE, Stream, Record);
+    } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
+      if (!StartedMetadataBlock)  {
         Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
+        
         // Abbrev for METADATA_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;
       }
-    }
-    if (const MDString *MDS = dyn_cast<MDString>(V)) {
+      
       // Code: [strchar x N]
       const char *StrBegin = MDS->begin();
       for (unsigned i = 0, e = MDS->length(); i != e; ++i)
@@ -521,21 +525,12 @@ static void WriteValues(unsigned FirstVal, unsigned LastVal,
       // Emit the finished record.
       Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
       Record.clear();
-      continue;
-    } else if (const MDNode *N = dyn_cast<MDNode>(V)) {
-      for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
-        if (N->getElement(i)) {
-          Record.push_back(VE.getTypeID(N->getElement(i)->getType()));
-          Record.push_back(VE.getValueID(N->getElement(i)));
-        } else {
-          Record.push_back(VE.getTypeID(Type::VoidTy));
-          Record.push_back(0);
-        }
+    } else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(Vals[i].first)) {
+      if (!StartedMetadataBlock)  {
+        Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
+        StartedMetadataBlock = true;
       }
-      Stream.EmitRecord(bitc::METADATA_NODE, Record, 0);
-      Record.clear();
-      continue;
-    } else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V)) {
+
       // Write name.
       std::string Str = NMD->getNameStr();
       const char *StrBegin = Str.c_str();
@@ -543,7 +538,7 @@ static void WriteValues(unsigned FirstVal, unsigned LastVal,
         Record.push_back(StrBegin[i]);
       Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
       Record.clear();
-      
+
       // Write named metadata elements.
       for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
         if (NMD->getElement(i)) 
@@ -553,49 +548,61 @@ static void WriteValues(unsigned FirstVal, unsigned LastVal,
       }
       Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
       Record.clear();
-      continue;
     }
+  }
+  
+  if (StartedMetadataBlock)
+    Stream.ExitBlock();    
+}
 
-    // If we need to switch block, do so now.
-    if (LastBlockID != bitc::CONSTANTS_BLOCK_ID) {
-      // Exit privious block.
-      if (LastBlockID != bitc::MODULE_BLOCK_ID)
-        Stream.ExitBlock();        
-
-      LastBlockID = bitc::CONSTANTS_BLOCK_ID;
-      Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
-      // If this is a constant pool for the module, emit module-specific abbrevs.
-      if (isGlobal) {
-        // Abbrev for CST_CODE_AGGREGATE.
-        BitCodeAbbrev *Abbv = new BitCodeAbbrev();
-        Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
-        Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
-        Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
-        AggregateAbbrev = Stream.EmitAbbrev(Abbv);
-        
-        // Abbrev for CST_CODE_STRING.
-        Abbv = new BitCodeAbbrev();
-        Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
-        Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
-        Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
-        String8Abbrev = Stream.EmitAbbrev(Abbv);
+static void WriteConstants(unsigned FirstVal, unsigned LastVal,
+                           const ValueEnumerator &VE,
+                           BitstreamWriter &Stream, bool isGlobal) {
+  if (FirstVal == LastVal) return;
+  
+  Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
 
-        // Abbrev for CST_CODE_CSTRING.
-        Abbv = new BitCodeAbbrev();
-        Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
-        Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
-        Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
-        CString7Abbrev = Stream.EmitAbbrev(Abbv);
+  unsigned AggregateAbbrev = 0;
+  unsigned String8Abbrev = 0;
+  unsigned CString7Abbrev = 0;
+  unsigned CString6Abbrev = 0;
+  // If this is a constant pool for the module, emit module-specific abbrevs.
+  if (isGlobal) {
+    // Abbrev for CST_CODE_AGGREGATE.
+    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
+    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
+    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
+    AggregateAbbrev = Stream.EmitAbbrev(Abbv);
 
-        // Abbrev for CST_CODE_CSTRING.
-        Abbv = new BitCodeAbbrev();
-        Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
-        Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
-        Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
-        CString6Abbrev = Stream.EmitAbbrev(Abbv);
-      }  
+    // Abbrev for CST_CODE_STRING.
+    Abbv = new BitCodeAbbrev();
+    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
+    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
+    String8Abbrev = Stream.EmitAbbrev(Abbv);
+    // Abbrev for CST_CODE_CSTRING.
+    Abbv = new BitCodeAbbrev();
+    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
+    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
+    CString7Abbrev = Stream.EmitAbbrev(Abbv);
+    // Abbrev for CST_CODE_CSTRING.
+    Abbv = new BitCodeAbbrev();
+    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
+    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
+    CString6Abbrev = Stream.EmitAbbrev(Abbv);
+  }  
+  
+  SmallVector<uint64_t, 64> Record;
 
-    }
+  const ValueEnumerator::ValueList &Vals = VE.getValues();
+  const Type *LastTy = 0;
+  for (unsigned i = FirstVal; i != LastVal; ++i) {
+    const Value *V = Vals[i].first;
+    if (isa<MetadataBase>(V))
+      continue;
     // If we need to switch types, do so now.
     if (V->getType() != LastTy) {
       LastTy = V->getType();
@@ -795,7 +802,7 @@ static void WriteModuleConstants(const ValueEnumerator &VE,
   // We know globalvalues have been emitted by WriteModuleInfo.
   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
     if (!isa<GlobalValue>(Vals[i].first)) {
-      WriteValues(i, Vals.size(), VE, Stream, true);
+      WriteConstants(i, Vals.size(), VE, Stream, true);
       return;
     }
   }
@@ -1124,7 +1131,7 @@ static void WriteFunction(const Function &F, ValueEnumerator &VE,
   // If there are function-local constants, emit them now.
   unsigned CstStart, CstEnd;
   VE.getFunctionConstantRange(CstStart, CstEnd);
-  WriteValues(CstStart, CstEnd, VE, Stream, false);
+  WriteConstants(CstStart, CstEnd, VE, Stream, false);
   
   // Keep a running idea of what the instruction ID is. 
   unsigned InstID = CstEnd;
@@ -1377,6 +1384,9 @@ static void WriteModule(const Module *M, BitstreamWriter &Stream) {
   // Emit constants.
   WriteModuleConstants(VE, Stream);
 
+  // Emit metadata.
+  WriteModuleMetadata(VE, Stream);
+
   // Emit function bodies.
   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
     if (!I->isDeclaration())
@@ -1387,7 +1397,7 @@ static void WriteModule(const Module *M, BitstreamWriter &Stream) {
   
   // Emit names for globals/functions etc.
   WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
-
+  
   Stream.ExitBlock();
 }
 
diff --git a/test/Bitcode/metadata.ll b/test/Bitcode/metadata.ll
deleted file mode 100644 (file)
index d72221a..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-; RUN: llvm-as < %s | llvm-dis -f -o /dev/null
-
-!llvm.foo = !{!0}
-!0 = metadata !{i32 42}
-@my.str = internal constant [4 x i8] c"foo\00"