[llvm-commits] [llvm] r113764 - in /llvm/trunk/lib/Bitcode/Reader: BitcodeReader.cpp BitcodeReader.h

Dan Gohman gohman at apple.com
Mon Sep 13 11:00:48 PDT 2010


Author: djg
Date: Mon Sep 13 13:00:48 2010
New Revision: 113764

URL: http://llvm.org/viewvc/llvm-project?rev=113764&view=rev
Log:
Add full auto-upgrade support for LLVM 2.7 bitcode metadata.

Modified:
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.h

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=113764&r1=113763&r2=113764&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Mon Sep 13 13:00:48 2010
@@ -776,7 +776,8 @@
     bool IsFunctionLocal = false;
     // Read a record.
     Record.clear();
-    switch (Stream.ReadRecord(Code, Record)) {
+    Code = Stream.ReadRecord(Code, Record);
+    switch (Code) {
     default:  // Default behavior: ignore.
       break;
     case bitc::METADATA_NAME: {
@@ -790,11 +791,11 @@
       Code = Stream.ReadCode();
 
       // METADATA_NAME is always followed by METADATA_NAMED_NODE2.
+      // Or METADATA_NAMED_NODE in LLVM 2.7. FIXME: Remove this in LLVM 3.0.
       unsigned NextBitCode = Stream.ReadRecord(Code, Record);
-      // FIXME: LLVM 3.0: Remove this.
-      if (NextBitCode == bitc::METADATA_NAMED_NODE)
-        break;
-      if (NextBitCode != bitc::METADATA_NAMED_NODE2)
+      if (NextBitCode == bitc::METADATA_NAMED_NODE) {
+        LLVM2_7MetadataDetected = true;
+      } else if (NextBitCode != bitc::METADATA_NAMED_NODE2)
         assert ( 0 && "Inavlid Named Metadata record");
 
       // Read named metadata elements.
@@ -806,20 +807,27 @@
           return Error("Malformed metadata record");
         NMD->addOperand(MD);
       }
+      // Backwards compatibility hack: NamedMDValues used to be Values,
+      // and they got their own slots in the value numbering. They are no
+      // longer Values, however we still need to account for them in the
+      // numbering in order to be able to read old bitcode files.
+      // FIXME: Remove this in LLVM 3.0.
+      if (LLVM2_7MetadataDetected)
+        MDValueList.AssignValue(0, NextMDValueNo++);
       break;
     }
-    case bitc::METADATA_FN_NODE:
-      // FIXME: Legacy support for the old fn_node, where function-local
-      // metadata operands were bogus. Remove in LLVM 3.0.
-      break;
-    case bitc::METADATA_NODE:
-      // FIXME: Legacy support for the old node, where function-local
-      // metadata operands were bogus. Remove in LLVM 3.0.
-      break;
+    case bitc::METADATA_FN_NODE: // FIXME: Remove in LLVM 3.0.
     case bitc::METADATA_FN_NODE2:
       IsFunctionLocal = true;
       // fall-through
+    case bitc::METADATA_NODE:    // FIXME: Remove in LLVM 3.0.
     case bitc::METADATA_NODE2: {
+
+      // Detect 2.7-era metadata.
+      // FIXME: Remove in LLVM 3.0.
+      if (Code == bitc::METADATA_FN_NODE || Code == bitc::METADATA_NODE)
+        LLVM2_7MetadataDetected = true;
+
       if (Record.size() % 2 == 1)
         return Error("Invalid METADATA_NODE2 record");
 
@@ -1614,9 +1622,9 @@
     switch (Stream.ReadRecord(Code, Record)) {
     default:  // Default behavior: ignore.
       break;
+    // FIXME: Remove in LLVM 3.0.
     case bitc::METADATA_ATTACHMENT:
-      // LLVM 3.0: Remove this.
-      break;
+      LLVM2_7MetadataDetected = true;
     case bitc::METADATA_ATTACHMENT2: {
       unsigned RecordLength = Record.size();
       if (Record.empty() || (RecordLength - 1) % 2 == 1)
@@ -1730,10 +1738,9 @@
       I = 0;
       continue;
         
+    // FIXME: Remove this in LLVM 3.0.
     case bitc::FUNC_CODE_DEBUG_LOC:
-      // FIXME: Ignore. Remove this in LLVM 3.0.
-      continue;
-
+      LLVM2_7MetadataDetected = true;
     case bitc::FUNC_CODE_DEBUG_LOC2: {      // DEBUG_LOC: [line, col, scope, ia]
       I = 0;     // Get the last instruction emitted.
       if (CurBB && !CurBB->empty())
@@ -2196,7 +2203,7 @@
     }
     case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
       // For backward compatibility, tolerate a lack of an opty, and use i32.
-      // LLVM 3.0: Remove this.
+      // Remove this in LLVM 3.0.
       if (Record.size() < 3 || Record.size() > 4)
         return Error("Invalid ALLOCA record");
       unsigned OpNum = 0;
@@ -2249,12 +2256,10 @@
       InstructionList.push_back(I);
       break;
     }
+    // FIXME: Remove this in LLVM 3.0.
     case bitc::FUNC_CODE_INST_CALL:
+      LLVM2_7MetadataDetected = true;
     case bitc::FUNC_CODE_INST_CALL2: {
-      // FIXME: Legacy support for the old call instruction, where function-local
-      // metadata operands were bogus. Remove in LLVM 3.0.
-      bool DropMetadata = BitCode == bitc::FUNC_CODE_INST_CALL;
-
       // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
       if (Record.size() < 3)
         return Error("Invalid CALL record");
@@ -2278,13 +2283,7 @@
       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
         if (FTy->getParamType(i)->getTypeID()==Type::LabelTyID)
           Args.push_back(getBasicBlock(Record[OpNum]));
-        else if (DropMetadata &&
-                 FTy->getParamType(i)->getTypeID()==Type::MetadataTyID) {
-          // LLVM 2.7 compatibility: drop metadata arguments to null.
-          Value *Ops = 0;
-          Args.push_back(MDNode::get(Context, &Ops, 1));
-          continue;
-        } else
+        else
           Args.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i)));
         if (Args.back() == 0) return Error("Invalid CALL record");
       }
@@ -2379,9 +2378,21 @@
     BlockAddrFwdRefs.erase(BAFRI);
   }
   
+  // FIXME: Remove this in LLVM 3.0.
+  unsigned NewMDValueListSize = MDValueList.size();
+
   // Trim the value list down to the size it was before we parsed this function.
   ValueList.shrinkTo(ModuleValueListSize);
   MDValueList.shrinkTo(ModuleMDValueListSize);
+
+  // Backwards compatibility hack: Function-local metadata numbers
+  // were previously not reset between functions. This is now fixed,
+  // however we still need to understand the old numbering in order
+  // to be able to read old bitcode files.
+  // FIXME: Remove this in LLVM 3.0.
+  if (LLVM2_7MetadataDetected)
+    MDValueList.resize(NewMDValueListSize);
+
   std::vector<BasicBlock*>().swap(FunctionBBs);
 
   return false;

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.h?rev=113764&r1=113763&r2=113764&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.h (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.h Mon Sep 13 13:00:48 2010
@@ -173,11 +173,18 @@
   /// are resolved lazily when functions are loaded.
   typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
   DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
+
+  /// LLVM2_7MetadataDetected - True if metadata produced by LLVM 2.7 or
+  /// earlier was detected, in which case we behave slightly differently,
+  /// for compatibility.
+  /// FIXME: Remove in LLVM 3.0.
+  bool LLVM2_7MetadataDetected;
   
 public:
   explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
     : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
-      ErrorString(0), ValueList(C), MDValueList(C) {
+      ErrorString(0), ValueList(C), MDValueList(C),
+      LLVM2_7MetadataDetected(false) {
     HasReversedFunctionsWithBodies = false;
   }
   ~BitcodeReader() {





More information about the llvm-commits mailing list