[llvm-commits] CVS: llvm/lib/Bitcode/Reader/BitcodeReader.cpp BitcodeReader.h

Chris Lattner sabre at nondot.org
Thu May 17 21:03:04 PDT 2007



Changes in directory llvm/lib/Bitcode/Reader:

BitcodeReader.cpp updated: 1.49 -> 1.50
BitcodeReader.h updated: 1.20 -> 1.21
---
Log message:

Fix PR1434: http://llvm.org/PR1434  and test/Linker/link-archive.ll, this is a regression from 1.9.


---
Diffs of the changes:  (+91 -54)

 BitcodeReader.cpp |  135 ++++++++++++++++++++++++++++++++----------------------
 BitcodeReader.h   |   10 +++-
 2 files changed, 91 insertions(+), 54 deletions(-)


Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp
diff -u llvm/lib/Bitcode/Reader/BitcodeReader.cpp:1.49 llvm/lib/Bitcode/Reader/BitcodeReader.cpp:1.50
--- llvm/lib/Bitcode/Reader/BitcodeReader.cpp:1.49	Tue May 15 01:29:44 2007
+++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp	Thu May 17 23:02:46 2007
@@ -24,8 +24,15 @@
 #include "llvm/Support/MemoryBuffer.h"
 using namespace llvm;
 
-BitcodeReader::~BitcodeReader() {
+void BitcodeReader::FreeState() {
   delete Buffer;
+  Buffer = 0;
+  std::vector<PATypeHolder>().swap(TypeList);
+  ValueList.clear();
+  std::vector<const ParamAttrsList*>().swap(ParamAttrs);
+  std::vector<BasicBlock*>().swap(FunctionBBs);
+  std::vector<Function*>().swap(FunctionsWithBodies);
+  DeferredFunctionInfo.clear();
 }
 
 //===----------------------------------------------------------------------===//
@@ -1102,53 +1109,6 @@
 }
 
 
-bool BitcodeReader::materializeFunction(Function *F, std::string *ErrInfo) {
-  // If it already is material, ignore the request.
-  if (!F->hasNotBeenReadFromBytecode()) return false;
-
-  DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator DFII = 
-    DeferredFunctionInfo.find(F);
-  assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
-  
-  // Move the bit stream to the saved position of the deferred function body and
-  // restore the real linkage type for the function.
-  Stream.JumpToBit(DFII->second.first);
-  F->setLinkage((GlobalValue::LinkageTypes)DFII->second.second);
-  
-  if (ParseFunctionBody(F)) {
-    if (ErrInfo) *ErrInfo = ErrorString;
-    return true;
-  }
-  
-  return false;
-}
-
-void BitcodeReader::dematerializeFunction(Function *F) {
-  // If this function isn't materialized, or if it is a proto, this is a noop.
-  if (F->hasNotBeenReadFromBytecode() || F->isDeclaration())
-    return;
-  
-  assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
-  
-  // Just forget the function body, we can remat it later.
-  F->deleteBody();
-  F->setLinkage(GlobalValue::GhostLinkage);
-}
-
-
-Module *BitcodeReader::materializeModule(std::string *ErrInfo) {
-  for (DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator I = 
-       DeferredFunctionInfo.begin(), E = DeferredFunctionInfo.end(); I != E;
-       ++I) {
-    Function *F = I->first;
-    if (F->hasNotBeenReadFromBytecode() &&
-        materializeFunction(F, ErrInfo))
-      return 0;
-  }
-  return TheModule;
-}
-
-
 /// ParseFunctionBody - Lazily parse the specified function body block.
 bool BitcodeReader::ParseFunctionBody(Function *F) {
   if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
@@ -1597,6 +1557,69 @@
   return false;
 }
 
+//===----------------------------------------------------------------------===//
+// ModuleProvider implementation
+//===----------------------------------------------------------------------===//
+
+
+bool BitcodeReader::materializeFunction(Function *F, std::string *ErrInfo) {
+  // If it already is material, ignore the request.
+  if (!F->hasNotBeenReadFromBytecode()) return false;
+  
+  DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator DFII = 
+    DeferredFunctionInfo.find(F);
+  assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
+  
+  // Move the bit stream to the saved position of the deferred function body and
+  // restore the real linkage type for the function.
+  Stream.JumpToBit(DFII->second.first);
+  F->setLinkage((GlobalValue::LinkageTypes)DFII->second.second);
+  
+  if (ParseFunctionBody(F)) {
+    if (ErrInfo) *ErrInfo = ErrorString;
+    return true;
+  }
+  
+  return false;
+}
+
+void BitcodeReader::dematerializeFunction(Function *F) {
+  // If this function isn't materialized, or if it is a proto, this is a noop.
+  if (F->hasNotBeenReadFromBytecode() || F->isDeclaration())
+    return;
+  
+  assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
+  
+  // Just forget the function body, we can remat it later.
+  F->deleteBody();
+  F->setLinkage(GlobalValue::GhostLinkage);
+}
+
+
+Module *BitcodeReader::materializeModule(std::string *ErrInfo) {
+  for (DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator I = 
+       DeferredFunctionInfo.begin(), E = DeferredFunctionInfo.end(); I != E;
+       ++I) {
+    Function *F = I->first;
+    if (F->hasNotBeenReadFromBytecode() &&
+        materializeFunction(F, ErrInfo))
+      return 0;
+  }
+  return TheModule;
+}
+
+
+/// This method is provided by the parent ModuleProvde class and overriden
+/// here. It simply releases the module from its provided and frees up our
+/// state.
+/// @brief Release our hold on the generated module
+Module *BitcodeReader::releaseModule(std::string *ErrInfo) {
+  // Since we're losing control of this Module, we must hand it back complete
+  Module *M = ModuleProvider::releaseModule(ErrInfo);
+  FreeState();
+  return M;
+}
+
 
 //===----------------------------------------------------------------------===//
 // External interface
@@ -1626,12 +1649,18 @@
   R = static_cast<BitcodeReader*>(getBitcodeModuleProvider(Buffer, ErrMsg));
   if (!R) return 0;
   
-  // Read the whole module, get a pointer to it, tell ModuleProvider not to
-  // delete it when its dtor is run.
-  Module *M = R->releaseModule(ErrMsg);
-  
-  // Don't let the BitcodeReader dtor delete 'Buffer'.
+  // Read in the entire module.
+  Module *M = R->materializeModule(ErrMsg);
+
+  // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
+  // there was an error.
   R->releaseMemoryBuffer();
+  
+  // If there was no error, tell ModuleProvider not to delete it when its dtor
+  // is run.
+  if (M)
+    M = R->releaseModule(ErrMsg);
+  
   delete R;
   return M;
 }


Index: llvm/lib/Bitcode/Reader/BitcodeReader.h
diff -u llvm/lib/Bitcode/Reader/BitcodeReader.h:1.20 llvm/lib/Bitcode/Reader/BitcodeReader.h:1.21
--- llvm/lib/Bitcode/Reader/BitcodeReader.h:1.20	Tue May 15 01:29:44 2007
+++ llvm/lib/Bitcode/Reader/BitcodeReader.h	Thu May 17 23:02:46 2007
@@ -39,6 +39,10 @@
     ++NumOperands;
   }
   
+  void clear() {
+    std::vector<Use>().swap(Uses);
+  }
+  
   Value *operator[](unsigned i) const { return getOperand(i); }
   
   Value *back() const { return Uses.back(); }
@@ -111,8 +115,11 @@
   BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
     HasReversedFunctionsWithBodies = false;
   }
-  ~BitcodeReader();
+  ~BitcodeReader() {
+    FreeState();
+  }
   
+  void FreeState();
   
   /// releaseMemoryBuffer - This causes the reader to completely forget about
   /// the memory buffer it contains, which prevents the buffer from being
@@ -124,6 +131,7 @@
   virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
   virtual Module *materializeModule(std::string *ErrInfo = 0);
   virtual void dematerializeFunction(Function *F);
+  virtual Module *releaseModule(std::string *ErrInfo = 0);
 
   bool Error(const char *Str) {
     ErrorString = Str;






More information about the llvm-commits mailing list