[llvm-branch-commits] [llvm-branch] r84951 - in /llvm/branches/Apple/Leela: include/llvm/ include/llvm/CodeGen/ include/llvm/Support/ lib/AsmParser/ lib/Bitcode/Reader/ lib/Bitcode/Writer/ lib/Transforms/IPO/ lib/VMCore/

Devang Patel dpatel at apple.com
Fri Oct 23 09:40:52 PDT 2009


Author: dpatel
Date: Fri Oct 23 11:40:52 2009
New Revision: 84951

URL: http://llvm.org/viewvc/llvm-project?rev=84951&view=rev
Log:
Merge metadata cleanup work from trunk. This includes following revisions.

84463
84465
84471
84689 
84765
84784
84786 
84790
84794 
84797 
84801 
84804 
84811 
84820 
84878 
84884 
84885 
84886 


Modified:
    llvm/branches/Apple/Leela/include/llvm/CodeGen/MachineModuleInfo.h
    llvm/branches/Apple/Leela/include/llvm/LLVMContext.h
    llvm/branches/Apple/Leela/include/llvm/Metadata.h
    llvm/branches/Apple/Leela/include/llvm/Support/IRBuilder.h
    llvm/branches/Apple/Leela/include/llvm/Value.h
    llvm/branches/Apple/Leela/lib/AsmParser/LLParser.cpp
    llvm/branches/Apple/Leela/lib/AsmParser/LLToken.h
    llvm/branches/Apple/Leela/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/branches/Apple/Leela/lib/Bitcode/Writer/BitcodeWriter.cpp
    llvm/branches/Apple/Leela/lib/Bitcode/Writer/ValueEnumerator.cpp
    llvm/branches/Apple/Leela/lib/Transforms/IPO/GlobalDCE.cpp
    llvm/branches/Apple/Leela/lib/Transforms/IPO/StripSymbols.cpp
    llvm/branches/Apple/Leela/lib/VMCore/AsmWriter.cpp
    llvm/branches/Apple/Leela/lib/VMCore/LLVMContext.cpp
    llvm/branches/Apple/Leela/lib/VMCore/LLVMContextImpl.h
    llvm/branches/Apple/Leela/lib/VMCore/Metadata.cpp
    llvm/branches/Apple/Leela/lib/VMCore/Pass.cpp

Modified: llvm/branches/Apple/Leela/include/llvm/CodeGen/MachineModuleInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/include/llvm/CodeGen/MachineModuleInfo.h?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/include/llvm/CodeGen/MachineModuleInfo.h (original)
+++ llvm/branches/Apple/Leela/include/llvm/CodeGen/MachineModuleInfo.h Fri Oct 23 11:40:52 2009
@@ -150,7 +150,8 @@
 public:
   static char ID; // Pass identification, replacement for typeid
 
-  typedef SmallVector< std::pair< WeakMetadataVH, unsigned>, 4 > VariableDbgInfoMapTy;
+  typedef SmallVector< std::pair<TrackingVH<MDNode>, unsigned>, 4 > 
+    VariableDbgInfoMapTy;
   VariableDbgInfoMapTy VariableDbgInfo;
 
   MachineModuleInfo();

Modified: llvm/branches/Apple/Leela/include/llvm/LLVMContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/include/llvm/LLVMContext.h?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/include/llvm/LLVMContext.h (original)
+++ llvm/branches/Apple/Leela/include/llvm/LLVMContext.h Fri Oct 23 11:40:52 2009
@@ -33,7 +33,6 @@
 public:
   LLVMContextImpl* const pImpl;
   MetadataContext &getMetadata();
-  bool RemoveDeadMetadata();
   LLVMContext();
   ~LLVMContext();
 };

Modified: llvm/branches/Apple/Leela/include/llvm/Metadata.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/include/llvm/Metadata.h?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/include/llvm/Metadata.h (original)
+++ llvm/branches/Apple/Leela/include/llvm/Metadata.h Fri Oct 23 11:40:52 2009
@@ -16,14 +16,11 @@
 #ifndef LLVM_MDNODE_H
 #define LLVM_MDNODE_H
 
-#include "llvm/User.h"
+#include "llvm/Value.h"
 #include "llvm/Type.h"
 #include "llvm/OperandTraits.h"
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/SmallPtrSet.h"
-#include "llvm/ADT/DenseMap.h"
-#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/ilist_node.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/ValueHandle.h"
@@ -32,27 +29,16 @@
 class Constant;
 class Instruction;
 class LLVMContext;
+class MetadataContextImpl;
 
 //===----------------------------------------------------------------------===//
 // MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
-class MetadataBase : public User {
-private:
-  /// ReservedSpace - The number of operands actually allocated.  NumOperands is
-  /// the number actually in use.
-  unsigned ReservedSpace;
-
+class MetadataBase : public Value {
 protected:
   MetadataBase(const Type *Ty, unsigned scid)
-    : User(Ty, scid, NULL, 0), ReservedSpace(0) {}
+    : Value(Ty, scid) {}
 
-  void resizeOperands(unsigned NumOps);
 public:
-  /// isNullValue - Return true if this is the value that would be returned by
-  /// getNullValue.  This always returns false because getNullValue will never
-  /// produce metadata.
-  virtual bool isNullValue() const {
-    return false;
-  }
 
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const MetadataBase *) { return true; }
@@ -68,32 +54,28 @@
 /// MDString is always unnamd.
 class MDString : public MetadataBase {
   MDString(const MDString &);            // DO NOT IMPLEMENT
-  void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
-  unsigned getNumOperands();             // DO NOT IMPLEMENT
 
   StringRef Str;
 protected:
-  explicit MDString(LLVMContext &C, const char *begin, unsigned l)
-    : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(begin, l) {}
+  explicit MDString(LLVMContext &C, StringRef S)
+    : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
 
 public:
-  // Do not allocate any space for operands.
-  void *operator new(size_t s) {
-    return User::operator new(s, 0);
-  }
-  static MDString *get(LLVMContext &Context, const StringRef &Str);
+  static MDString *get(LLVMContext &Context, StringRef Str);
   
   StringRef getString() const { return Str; }
 
-  unsigned length() const { return Str.size(); }
+  unsigned getLength() const { return Str.size(); }
 
+  typedef StringRef::iterator iterator;
+  
   /// begin() - Pointer to the first byte of the string.
   ///
-  const char *begin() const { return Str.begin(); }
+  iterator begin() const { return Str.begin(); }
 
   /// end() - Pointer to one byte past the end of the string.
   ///
-  const char *end() const { return Str.end(); }
+  iterator end() const { return Str.end(); }
 
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const MDString *) { return true; }
@@ -108,14 +90,12 @@
 /// MDNode is always unnamed.
 class MDNode : public MetadataBase, public FoldingSetNode {
   MDNode(const MDNode &);                // DO NOT IMPLEMENT
-  void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
-  // getNumOperands - Make this only available for private uses.
-  unsigned getNumOperands() { return User::getNumOperands();  }
 
   friend class ElementVH;
   // Use CallbackVH to hold MDNOde elements.
   struct ElementVH : public CallbackVH {
     MDNode *Parent;
+    ElementVH() {}
     ElementVH(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
     ~ElementVH() {}
 
@@ -130,61 +110,32 @@
   // Replace each instance of F from the element list of this node with T.
   void replaceElement(Value *F, Value *T);
 
-  SmallVector<ElementVH, 4> Node;
+  ElementVH *Node;
+  unsigned NodeSize;
 
 protected:
-  explicit MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals);
+  explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals);
 public:
-  // Do not allocate any space for operands.
-  void *operator new(size_t s) {
-    return User::operator new(s, 0);
-  }
   // Constructors and destructors.
   static MDNode *get(LLVMContext &Context, 
-                     Value* const* Vals, unsigned NumVals);
-
-  /// dropAllReferences - Remove all uses and clear node vector.
-  void dropAllReferences();
+                     Value *const *Vals, unsigned NumVals);
 
   /// ~MDNode - Destroy MDNode.
   ~MDNode();
   
   /// getElement - Return specified element.
   Value *getElement(unsigned i) const {
-    assert (getNumElements() > i && "Invalid element number!");
+    assert(i < getNumElements() && "Invalid element number!");
     return Node[i];
   }
 
   /// getNumElements - Return number of MDNode elements.
-  unsigned getNumElements() const {
-    return Node.size();
-  }
-
-  // Element access
-  typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator;
-  typedef SmallVectorImpl<ElementVH>::iterator elem_iterator;
-  /// elem_empty - Return true if MDNode is empty.
-  bool elem_empty() const                { return Node.empty(); }
-  const_elem_iterator elem_begin() const { return Node.begin(); }
-  const_elem_iterator elem_end() const   { return Node.end();   }
-  elem_iterator elem_begin()             { return Node.begin(); }
-  elem_iterator elem_end()               { return Node.end();   }
-
-  /// isNullValue - Return true if this is the value that would be returned by
-  /// getNullValue.  This always returns false because getNullValue will never
-  /// produce metadata.
-  virtual bool isNullValue() const {
-    return false;
-  }
+  unsigned getNumElements() const { return NodeSize; }
 
   /// Profile - calculate a unique identifier for this MDNode to collapse
   /// duplicates
   void Profile(FoldingSetNodeID &ID) const;
 
-  virtual void replaceUsesOfWithOnConstant(Value *, Value *, Use *) {
-    llvm_unreachable("This should never be called because MDNodes have no ops");
-  }
-
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const MDNode *) { return true; }
   static bool classof(const Value *V) {
@@ -193,23 +144,6 @@
 };
 
 //===----------------------------------------------------------------------===//
-/// WeakMetadataVH - a weak value handle for metadata.
-class WeakMetadataVH : public WeakVH {
-public:
-  WeakMetadataVH() : WeakVH() {}
-  WeakMetadataVH(MetadataBase *M) : WeakVH(M) {}
-  WeakMetadataVH(const WeakMetadataVH &RHS) : WeakVH(RHS) {}
-  
-  operator Value*() const {
-    llvm_unreachable("WeakMetadataVH only handles Metadata");
-  }
-
-  operator MetadataBase*() const {
-   return dyn_cast_or_null<MetadataBase>(getValPtr());
-  }
-};
-
-//===----------------------------------------------------------------------===//
 /// NamedMDNode - a tuple of other metadata. 
 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
 template<typename ValueSubClass, typename ItemParentClass>
@@ -220,24 +154,17 @@
   friend class LLVMContextImpl;
 
   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
-  void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
-  // getNumOperands - Make this only available for private uses.
-  unsigned getNumOperands() { return User::getNumOperands();  }
 
   Module *Parent;
-  SmallVector<WeakMetadataVH, 4> Node;
-  typedef SmallVectorImpl<WeakMetadataVH>::iterator elem_iterator;
+  SmallVector<TrackingVH<MetadataBase>, 4> Node;
 
+  void setParent(Module *M) { Parent = M; }
 protected:
-  explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const* Vals, 
+  explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals, 
                        unsigned NumVals, Module *M = 0);
 public:
-  // Do not allocate any space for operands.
-  void *operator new(size_t s) {
-    return User::operator new(s, 0);
-  }
   static NamedMDNode *Create(LLVMContext &C, const Twine &N, 
-                             MetadataBase*const*MDs, 
+                             MetadataBase *const *MDs, 
                              unsigned NumMDs, Module *M = 0) {
     return new NamedMDNode(C, N, MDs, NumMDs, M);
   }
@@ -257,11 +184,10 @@
   /// getParent - Get the module that holds this named metadata collection.
   inline Module *getParent() { return Parent; }
   inline const Module *getParent() const { return Parent; }
-  void setParent(Module *M) { Parent = M; }
 
   /// getElement - Return specified element.
   MetadataBase *getElement(unsigned i) const {
-    assert (getNumElements() > i && "Invalid element number!");
+    assert(i < getNumElements() && "Invalid element number!");
     return Node[i];
   }
 
@@ -272,30 +198,18 @@
 
   /// addElement - Add metadata element.
   void addElement(MetadataBase *M) {
-    resizeOperands(0);
-    OperandList[NumOperands++] = M;
-    Node.push_back(WeakMetadataVH(M));
+    Node.push_back(TrackingVH<MetadataBase>(M));
   }
 
-  typedef SmallVectorImpl<WeakMetadataVH>::const_iterator const_elem_iterator;
+  typedef SmallVectorImpl<TrackingVH<MetadataBase> >::iterator elem_iterator;
+  typedef SmallVectorImpl<TrackingVH<MetadataBase> >::const_iterator 
+    const_elem_iterator;
   bool elem_empty() const                { return Node.empty(); }
   const_elem_iterator elem_begin() const { return Node.begin(); }
   const_elem_iterator elem_end() const   { return Node.end();   }
   elem_iterator elem_begin()             { return Node.begin(); }
   elem_iterator elem_end()               { return Node.end();   }
 
-  /// isNullValue - Return true if this is the value that would be returned by
-  /// getNullValue.  This always returns false because getNullValue will never
-  /// produce metadata.
-  virtual bool isNullValue() const {
-    return false;
-  }
-
-  virtual void replaceUsesOfWithOnConstant(Value *, Value *, Use *) {
-    llvm_unreachable(
-                "This should never be called because NamedMDNodes have no ops");
-  }
-
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const NamedMDNode *) { return true; }
   static bool classof(const Value *V) {
@@ -310,61 +224,55 @@
 /// must start with an alphabet. The regular expression used to check name
 /// is [a-zA-Z$._][a-zA-Z$._0-9]*
 class MetadataContext {
-public:
-  typedef std::pair<unsigned, WeakVH> MDPairTy;
-  typedef SmallVector<MDPairTy, 2> MDMapTy;
-  typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
-  friend class BitcodeReader;
-private:
-
-  /// MetadataStore - Collection of metadata used in this context.
-  MDStoreTy MetadataStore;
-
-  /// MDHandlerNames - Map to hold metadata handler names.
-  StringMap<unsigned> MDHandlerNames;
+  // DO NOT IMPLEMENT
+  MetadataContext(MetadataContext&);
+  void operator=(MetadataContext&);
 
+  MetadataContextImpl *const pImpl;
 public:
-  /// RegisterMDKind - Register a new metadata kind and return its ID.
+  MetadataContext();
+  ~MetadataContext();
+
+  /// registerMDKind - Register a new metadata kind and return its ID.
   /// A metadata kind can be registered only once. 
-  unsigned RegisterMDKind(const char *Name);
+  unsigned registerMDKind(StringRef Name);
 
   /// getMDKind - Return metadata kind. If the requested metadata kind
   /// is not registered then return 0.
-  unsigned getMDKind(const char *Name);
+  unsigned getMDKind(StringRef Name) const;
 
-  /// validName - Return true if Name is a valid custom metadata handler name.
-  bool validName(const char *Name);
+  /// isValidName - Return true if Name is a valid custom metadata handler name.
+  static bool isValidName(StringRef Name);
 
-  /// getMD - Get the metadata of given kind attached with an Instruction.
+  /// getMD - Get the metadata of given kind attached to an Instruction.
   /// If the metadata is not found then return 0.
   MDNode *getMD(unsigned Kind, const Instruction *Inst);
 
-  /// getMDs - Get the metadata attached with an Instruction.
-  const MDMapTy *getMDs(const Instruction *Inst);
+  /// getMDs - Get the metadata attached to an Instruction.
+  void getMDs(const Instruction *Inst, 
+        SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const;
 
-  /// addMD - Attach the metadata of given kind with an Instruction.
+  /// addMD - Attach the metadata of given kind to an Instruction.
   void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
   
   /// removeMD - Remove metadata of given kind attached with an instuction.
   void removeMD(unsigned Kind, Instruction *Inst);
   
-  /// removeMDs - Remove all metadata attached with an instruction.
-  void removeMDs(const Instruction *Inst);
+  /// removeAllMetadata - Remove all metadata attached with an instruction.
+  void removeAllMetadata(Instruction *Inst);
 
   /// copyMD - If metadata is attached with Instruction In1 then attach
   /// the same metadata to In2.
   void copyMD(Instruction *In1, Instruction *In2);
 
-  /// getHandlerNames - Get handler names. This is used by bitcode
-  /// writer.
-  const StringMap<unsigned> *getHandlerNames();
+  /// getHandlerNames - Populate client supplied smallvector using custome
+  /// metadata name and ID.
+  void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
 
   /// ValueIsDeleted - This handler is used to update metadata store
   /// when a value is deleted.
   void ValueIsDeleted(const Value *) {}
-  void ValueIsDeleted(const Instruction *Inst) {
-    removeMDs(Inst);
-  }
+  void ValueIsDeleted(Instruction *Inst);
   void ValueIsRAUWd(Value *V1, Value *V2);
 
   /// ValueIsCloned - This handler is used to update metadata store

Modified: llvm/branches/Apple/Leela/include/llvm/Support/IRBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/include/llvm/Support/IRBuilder.h?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/include/llvm/Support/IRBuilder.h (original)
+++ llvm/branches/Apple/Leela/include/llvm/Support/IRBuilder.h Fri Oct 23 11:40:52 2009
@@ -139,7 +139,7 @@
     if (MDKind == 0) 
       MDKind = Context.getMetadata().getMDKind("dbg");
     if (MDKind == 0)
-      MDKind = Context.getMetadata().RegisterMDKind("dbg");
+      MDKind = Context.getMetadata().registerMDKind("dbg");
     CurDbgLocation = L;
   }
 

Modified: llvm/branches/Apple/Leela/include/llvm/Value.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/include/llvm/Value.h?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/include/llvm/Value.h (original)
+++ llvm/branches/Apple/Leela/include/llvm/Value.h Fri Oct 23 11:40:52 2009
@@ -42,7 +42,7 @@
 class AssemblyAnnotationWriter;
 class ValueHandleBase;
 class LLVMContext;
-class MetadataContext;
+class MetadataContextImpl;
 
 //===----------------------------------------------------------------------===//
 //                                 Value Class
@@ -83,7 +83,7 @@
   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
   friend class SymbolTable;      // Allow SymbolTable to directly poke Name.
   friend class ValueHandleBase;
-  friend class MetadataContext;
+  friend class MetadataContextImpl;
   friend class AbstractTypeUser;
   ValueName *Name;
 

Modified: llvm/branches/Apple/Leela/lib/AsmParser/LLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/AsmParser/LLParser.cpp?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/AsmParser/LLParser.cpp (original)
+++ llvm/branches/Apple/Leela/lib/AsmParser/LLParser.cpp Fri Oct 23 11:40:52 2009
@@ -1029,14 +1029,12 @@
 ///   ::= /* empty */
 ///   ::= !dbg !42
 bool LLParser::ParseOptionalCustomMetadata() {
-
-  std::string Name;
-  if (Lex.getKind() == lltok::NamedOrCustomMD) {
-    Name = Lex.getStrVal();
-    Lex.Lex();
-  } else
+  if (Lex.getKind() != lltok::NamedOrCustomMD)
     return false;
 
+  std::string Name = Lex.getStrVal();
+  Lex.Lex();
+
   if (Lex.getKind() != lltok::Metadata)
     return TokError("Expected '!' here");
   Lex.Lex();
@@ -1047,7 +1045,7 @@
   MetadataContext &TheMetadata = M->getContext().getMetadata();
   unsigned MDK = TheMetadata.getMDKind(Name.c_str());
   if (!MDK)
-    MDK = TheMetadata.RegisterMDKind(Name.c_str());
+    MDK = TheMetadata.registerMDKind(Name.c_str());
   MDsOnInst.push_back(std::make_pair(MDK, cast<MDNode>(Node)));
 
   return false;
@@ -2857,8 +2855,6 @@
   if (ParseType(Ty, true /*void allowed*/)) return true;
 
   if (Ty->isVoidTy()) {
-    if (EatIfPresent(lltok::comma))
-      if (ParseOptionalCustomMetadata()) return true;
     Inst = ReturnInst::Create(Context);
     return false;
   }
@@ -2894,8 +2890,6 @@
       }
     }
   }
-  if (EatIfPresent(lltok::comma))
-    if (ParseOptionalCustomMetadata()) return true;
 
   Inst = ReturnInst::Create(Context, RV);
   return false;

Modified: llvm/branches/Apple/Leela/lib/AsmParser/LLToken.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/AsmParser/LLToken.h?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/AsmParser/LLToken.h (original)
+++ llvm/branches/Apple/Leela/lib/AsmParser/LLToken.h Fri Oct 23 11:40:52 2009
@@ -63,7 +63,6 @@
     kw_asm,
     kw_sideeffect,
     kw_gc,
-    kw_dbg,
     kw_c,
 
     kw_cc, kw_ccc, kw_fastcc, kw_coldcc,

Modified: llvm/branches/Apple/Leela/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/Bitcode/Reader/BitcodeReader.cpp?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/branches/Apple/Leela/lib/Bitcode/Reader/BitcodeReader.cpp Fri Oct 23 11:40:52 2009
@@ -840,7 +840,15 @@
       for (unsigned i = 1; i != RecordLength; ++i)
         Name[i-1] = Record[i];
       MetadataContext &TheMetadata = Context.getMetadata();
-      TheMetadata.MDHandlerNames[Name.str()] = Kind;
+      unsigned ExistingKind = TheMetadata.getMDKind(Name.str());
+      if (ExistingKind == 0) {
+        unsigned NewKind = TheMetadata.registerMDKind(Name.str());
+        assert (Kind == NewKind 
+                && "Unable to handle custom metadata mismatch!");
+      } else {
+        assert (ExistingKind == Kind 
+                && "Unable to handle custom metadata mismatch!");
+      }
       break;
     }
     }

Modified: llvm/branches/Apple/Leela/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/branches/Apple/Leela/lib/Bitcode/Writer/BitcodeWriter.cpp Fri Oct 23 11:40:52 2009
@@ -517,9 +517,7 @@
       }
 
       // Code: [strchar x N]
-      const char *StrBegin = MDS->begin();
-      for (unsigned i = 0, e = MDS->length(); i != e; ++i)
-        Record.push_back(StrBegin[i]);
+      Record.append(MDS->begin(), MDS->end());
 
       // Emit the finished record.
       Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
@@ -563,22 +561,22 @@
   // Write metadata attachments
   // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
   MetadataContext &TheMetadata = F.getContext().getMetadata();
+  typedef SmallVector<std::pair<unsigned, TrackingVH<MDNode> >, 2> MDMapTy;
+  MDMapTy MDs;
   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
          I != E; ++I) {
-      const MetadataContext::MDMapTy *P = TheMetadata.getMDs(I);
-      if (!P) continue;
+      MDs.clear();
+      TheMetadata.getMDs(I, MDs);
       bool RecordedInstruction = false;
-      for (MetadataContext::MDMapTy::const_iterator PI = P->begin(), 
-             PE = P->end(); PI != PE; ++PI) {
-        if (MDNode *ND = dyn_cast_or_null<MDNode>(PI->second)) {
-          if (RecordedInstruction == false) {
-            Record.push_back(VE.getInstructionID(I));
-            RecordedInstruction = true;
-          }
-          Record.push_back(PI->first);
-          Record.push_back(VE.getValueID(ND));
+      for (MDMapTy::const_iterator PI = MDs.begin(), PE = MDs.end();
+             PI != PE; ++PI) {
+        if (RecordedInstruction == false) {
+          Record.push_back(VE.getInstructionID(I));
+          RecordedInstruction = true;
         }
+        Record.push_back(PI->first);
+        Record.push_back(VE.getValueID(PI->second));
       }
       if (!Record.empty()) {
         if (!StartedMetadataBlock)  {
@@ -604,11 +602,13 @@
   // Write metadata kinds
   // METADATA_KIND - [n x [id, name]]
   MetadataContext &TheMetadata = M->getContext().getMetadata();
-  const StringMap<unsigned> *Kinds = TheMetadata.getHandlerNames();
-  for (StringMap<unsigned>::const_iterator
-         I = Kinds->begin(), E = Kinds->end(); I != E; ++I) {
-    Record.push_back(I->second);
-    StringRef KName = I->first();
+  SmallVector<std::pair<unsigned, StringRef>, 4> Names;
+  TheMetadata.getHandlerNames(Names);
+  for (SmallVector<std::pair<unsigned, StringRef>, 4>::iterator 
+         I = Names.begin(),
+         E = Names.end(); I != E; ++I) {
+    Record.push_back(I->first);
+    StringRef KName = I->second;
     for (unsigned i = 0, e = KName.size(); i != e; ++i)
       Record.push_back(KName[i]);
     if (!StartedMetadataBlock)  {

Modified: llvm/branches/Apple/Leela/lib/Bitcode/Writer/ValueEnumerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/Bitcode/Writer/ValueEnumerator.cpp?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/Bitcode/Writer/ValueEnumerator.cpp (original)
+++ llvm/branches/Apple/Leela/lib/Bitcode/Writer/ValueEnumerator.cpp Fri Oct 23 11:40:52 2009
@@ -87,6 +87,8 @@
       EnumerateType(I->getType());
 
     MetadataContext &TheMetadata = F->getContext().getMetadata();
+    typedef SmallVector<std::pair<unsigned, TrackingVH<MDNode> >, 2> MDMapTy;
+    MDMapTy MDs;
     for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
       for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
         for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
@@ -99,12 +101,11 @@
           EnumerateAttributes(II->getAttributes());
 
         // Enumerate metadata attached with this instruction.
-        const MetadataContext::MDMapTy *MDs = TheMetadata.getMDs(I);
-        if (MDs)
-          for (MetadataContext::MDMapTy::const_iterator MI = MDs->begin(),
-                 ME = MDs->end(); MI != ME; ++MI)
-            if (MDNode *MDN = dyn_cast_or_null<MDNode>(MI->second))
-              EnumerateMetadata(MDN);
+        MDs.clear();
+        TheMetadata.getMDs(I, MDs);
+        for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME;
+             ++MI)
+          EnumerateMetadata(MI->second);
       }
   }
 
@@ -214,10 +215,9 @@
     MDValues.push_back(std::make_pair(MD, 1U));
     MDValueMap[MD] = MDValues.size();
     MDValueID = MDValues.size();
-    for (MDNode::const_elem_iterator I = N->elem_begin(), E = N->elem_end();
-         I != E; ++I) {
-      if (*I)
-        EnumerateValue(*I);
+    for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {    
+      if (Value *V = N->getElement(i))
+        EnumerateValue(V);
       else
         EnumerateType(Type::getVoidTy(MD->getContext()));
     }

Modified: llvm/branches/Apple/Leela/lib/Transforms/IPO/GlobalDCE.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/Transforms/IPO/GlobalDCE.cpp?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/Transforms/IPO/GlobalDCE.cpp (original)
+++ llvm/branches/Apple/Leela/lib/Transforms/IPO/GlobalDCE.cpp Fri Oct 23 11:40:52 2009
@@ -149,9 +149,6 @@
   // Make sure that all memory is released
   AliveGlobals.clear();
 
-  // Remove dead metadata.
-  // FIXME - Enable this.
-  // Changed |= M.getContext().RemoveDeadMetadata();
   return Changed;
 }
 

Modified: llvm/branches/Apple/Leela/lib/Transforms/IPO/StripSymbols.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/Transforms/IPO/StripSymbols.cpp?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/Transforms/IPO/StripSymbols.cpp (original)
+++ llvm/branches/Apple/Leela/lib/Transforms/IPO/StripSymbols.cpp Fri Oct 23 11:40:52 2009
@@ -250,8 +250,6 @@
   if (NMD)
     NMD->eraseFromParent();
 
-  // Remove dead metadata.
-  M.getContext().RemoveDeadMetadata();
   return true;
 }
 

Modified: llvm/branches/Apple/Leela/lib/VMCore/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/VMCore/AsmWriter.cpp?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/VMCore/AsmWriter.cpp (original)
+++ llvm/branches/Apple/Leela/lib/VMCore/AsmWriter.cpp Fri Oct 23 11:40:52 2009
@@ -680,6 +680,8 @@
   ST_DEBUG("Inserting Instructions:\n");
 
   MetadataContext &TheMetadata = TheFunction->getContext().getMetadata();
+  typedef SmallVector<std::pair<unsigned, TrackingVH<MDNode> >, 2> MDMapTy;
+  MDMapTy MDs;
 
   // Add all of the basic blocks and instructions with no names.
   for (Function::const_iterator BB = TheFunction->begin(),
@@ -696,12 +698,11 @@
           CreateMetadataSlot(N);
 
       // Process metadata attached with this instruction.
-      const MetadataContext::MDMapTy *MDs = TheMetadata.getMDs(I);
-      if (MDs)
-        for (MetadataContext::MDMapTy::const_iterator MI = MDs->begin(),
-               ME = MDs->end(); MI != ME; ++MI)
-          if (MDNode *MDN = dyn_cast_or_null<MDNode>(MI->second))
-            CreateMetadataSlot(MDN);
+      MDs.clear();
+      TheMetadata.getMDs(I, MDs);
+      for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME; 
+           ++MI)
+        CreateMetadataSlot(MI->second);
     }
   }
 
@@ -818,9 +819,8 @@
   unsigned DestSlot = mdnNext++;
   mdnMap[N] = DestSlot;
 
-  for (MDNode::const_elem_iterator MDI = N->elem_begin(),
-         MDE = N->elem_end(); MDI != MDE; ++MDI) {
-    const Value *TV = *MDI;
+  for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
+    const Value *TV = N->getElement(i);
     if (TV)
       if (const MDNode *N2 = dyn_cast<MDNode>(TV))
         CreateMetadataSlot(N2);
@@ -906,9 +906,8 @@
     Out << '!' << i << " = metadata ";
     const MDNode *Node = Nodes[i];
     Out << "!{";
-    for (MDNode::const_elem_iterator NI = Node->elem_begin(),
-           NE = Node->elem_end(); NI != NE;) {
-      const Value *V = *NI;
+    for (unsigned mi = 0, me = Node->getNumElements(); mi != me; ++mi) {
+      const Value *V = Node->getElement(mi);
       if (!V)
         Out << "null";
       else if (const MDNode *N = dyn_cast<MDNode>(V)) {
@@ -916,11 +915,12 @@
         Out << '!' << Machine.getMetadataSlot(N);
       }
       else {
-        TypePrinter.print((*NI)->getType(), Out);
+        TypePrinter.print(V->getType(), Out);
         Out << ' ';
-        WriteAsOperandInternal(Out, *NI, &TypePrinter, &Machine);
+        WriteAsOperandInternal(Out, Node->getElement(mi), 
+                               &TypePrinter, &Machine);
       }
-      if (++NI != NE)
+      if (mi + 1 != me)
         Out << ", ";
     }
 
@@ -1294,7 +1294,7 @@
   TypePrinting TypePrinter;
   AssemblyAnnotationWriter *AnnotationWriter;
   std::vector<const Type*> NumberedTypes;
-  DenseMap<unsigned, const char *> MDNames;
+  DenseMap<unsigned, StringRef> MDNames;
 
 public:
   inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
@@ -1305,11 +1305,12 @@
     // FIXME: Provide MDPrinter
     if (M) {
       MetadataContext &TheMetadata = M->getContext().getMetadata();
-      const StringMap<unsigned> *Names = TheMetadata.getHandlerNames();
-      for (StringMapConstIterator<unsigned> I = Names->begin(),
-             E = Names->end(); I != E; ++I) {
-        const StringMapEntry<unsigned> &Entry = *I;
-        MDNames[I->second] = Entry.getKeyData();
+      SmallVector<std::pair<unsigned, StringRef>, 4> Names;
+      TheMetadata.getHandlerNames(Names);
+      for (SmallVector<std::pair<unsigned, StringRef>, 4>::iterator 
+             I = Names.begin(),
+             E = Names.end(); I != E; ++I) {
+      MDNames[I->first] = I->second;
       }
     }
   }
@@ -2033,13 +2034,13 @@
   // Print Metadata info
   if (!MDNames.empty()) {
     MetadataContext &TheMetadata = I.getContext().getMetadata();
-    const MetadataContext::MDMapTy *MDMap = TheMetadata.getMDs(&I);
-    if (MDMap)
-      for (MetadataContext::MDMapTy::const_iterator MI = MDMap->begin(),
-             ME = MDMap->end(); MI != ME; ++MI)
-        if (const MDNode *MD = dyn_cast_or_null<MDNode>(MI->second))
-          Out << ", !" << MDNames[MI->first]
-              << " !" << Machine.getMetadataSlot(MD);
+    typedef SmallVector<std::pair<unsigned, TrackingVH<MDNode> >, 2> MDMapTy;
+    MDMapTy MDs;
+    TheMetadata.getMDs(&I, MDs);
+    for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME; 
+         ++MI)
+      Out << ", !" << MDNames[MI->first]
+          << " !" << Machine.getMetadataSlot(MI->second);
   }
   printInfoComment(I);
 }

Modified: llvm/branches/Apple/Leela/lib/VMCore/LLVMContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/VMCore/LLVMContext.cpp?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/VMCore/LLVMContext.cpp (original)
+++ llvm/branches/Apple/Leela/lib/VMCore/LLVMContext.cpp Fri Oct 23 11:40:52 2009
@@ -45,32 +45,6 @@
     OperandList[i+1] = IdxList[i];
 }
 
-bool LLVMContext::RemoveDeadMetadata() {
-  std::vector<WeakVH> DeadMDNodes;
-  bool Changed = false;
-  while (1) {
-
-    for (FoldingSet<MDNode>::iterator 
-           I = pImpl->MDNodeSet.begin(),
-           E = pImpl->MDNodeSet.end(); I != E; ++I) {
-      MDNode *N = &(*I);
-      if (N->use_empty()) 
-        DeadMDNodes.push_back(WeakVH(N));
-    }
-    
-    if (DeadMDNodes.empty())
-      return Changed;
-
-    while (!DeadMDNodes.empty()) {
-      Value *V = DeadMDNodes.back(); DeadMDNodes.pop_back();
-      if (const MDNode *N = dyn_cast_or_null<MDNode>(V))
-        if (N->use_empty())
-          delete N;
-    }
-  }
-  return Changed;
-}
-
 MetadataContext &LLVMContext::getMetadata() {
   return pImpl->TheMetadata;
 }

Modified: llvm/branches/Apple/Leela/lib/VMCore/LLVMContextImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/VMCore/LLVMContextImpl.h?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/VMCore/LLVMContextImpl.h (original)
+++ llvm/branches/Apple/Leela/lib/VMCore/LLVMContextImpl.h Fri Oct 23 11:40:52 2009
@@ -204,9 +204,6 @@
     AggZeroConstants.freeConstants();
     NullPtrConstants.freeConstants();
     UndefValueConstants.freeConstants();
-    for (FoldingSet<MDNode>::iterator I = MDNodeSet.begin(), 
-           E = MDNodeSet.end(); I != E; ++I)
-      I->dropAllReferences();
     for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end(); 
          I != E; ++I) {
       if (I->second->use_empty())
@@ -217,6 +214,7 @@
       if (I->second->use_empty())
         delete I->second;
     }
+    MDNodeSet.clear();
   }
 };
 

Modified: llvm/branches/Apple/Leela/lib/VMCore/Metadata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/VMCore/Metadata.cpp?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/VMCore/Metadata.cpp (original)
+++ llvm/branches/Apple/Leela/lib/VMCore/Metadata.cpp Fri Oct 23 11:40:52 2009
@@ -16,73 +16,45 @@
 #include "llvm/LLVMContext.h"
 #include "llvm/Module.h"
 #include "llvm/Instruction.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/StringMap.h"
 #include "SymbolTableListTraitsImpl.h"
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
-//MetadataBase implementation
+// MetadataBase implementation.
 //
 
-/// resizeOperands - Metadata keeps track of other metadata uses using 
-/// OperandList. Resize this list to hold anticipated number of metadata
-/// operands.
-void MetadataBase::resizeOperands(unsigned NumOps) {
-  unsigned e = getNumOperands();
-  if (NumOps == 0) {
-    NumOps = e*2;
-    if (NumOps < 2) NumOps = 2;  
-  } else if (NumOps > NumOperands) {
-    // No resize needed.
-    if (ReservedSpace >= NumOps) return;
-  } else if (NumOps == NumOperands) {
-    if (ReservedSpace == NumOps) return;
-  } else {
-    return;
-  }
-
-  ReservedSpace = NumOps;
-  Use *OldOps = OperandList;
-  Use *NewOps = allocHungoffUses(NumOps);
-  std::copy(OldOps, OldOps + e, NewOps);
-  OperandList = NewOps;
-  if (OldOps) Use::zap(OldOps, OldOps + e, true);
-}
 //===----------------------------------------------------------------------===//
-//MDString implementation
+// MDString implementation.
 //
-MDString *MDString::get(LLVMContext &Context, const StringRef &Str) {
+MDString *MDString::get(LLVMContext &Context, StringRef Str) {
   LLVMContextImpl *pImpl = Context.pImpl;
   sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
   StringMapEntry<MDString *> &Entry = 
     pImpl->MDStringCache.GetOrCreateValue(Str);
   MDString *&S = Entry.getValue();
-  if (!S) S = new MDString(Context, Entry.getKeyData(),
-                           Entry.getKeyLength());
-
-  return S;
+  if (S) return S;
+  
+  return S = 
+    new MDString(Context, Entry.getKey());
 }
 
 //===----------------------------------------------------------------------===//
-//MDNode implementation
+// MDNode implementation.
 //
-MDNode::MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals)
+MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals)
   : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
-  NumOperands = 0;
-  resizeOperands(NumVals);
-  for (unsigned i = 0; i != NumVals; ++i) {
-    // Only record metadata uses.
-    if (MetadataBase *MB = dyn_cast_or_null<MetadataBase>(Vals[i]))
-      OperandList[NumOperands++] = MB;
-    else if(Vals[i] && 
-            Vals[i]->getType()->getTypeID() == Type::MetadataTyID)
-      OperandList[NumOperands++] = Vals[i];
-    Node.push_back(ElementVH(Vals[i], this));
-  }
+  NodeSize = NumVals;
+  Node = new ElementVH[NodeSize];
+  ElementVH *Ptr = Node;
+  for (unsigned i = 0; i != NumVals; ++i) 
+    *Ptr++ = ElementVH(Vals[i], this);
 }
 
 void MDNode::Profile(FoldingSetNodeID &ID) const {
-  for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
-    ID.AddPointer(*I);
+  for (unsigned i = 0, e = getNumElements(); i != e; ++i)
+    ID.AddPointer(getElement(i));
 }
 
 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
@@ -91,37 +63,34 @@
   for (unsigned i = 0; i != NumVals; ++i)
     ID.AddPointer(Vals[i]);
 
-  pImpl->ConstantsLock.reader_acquire();
   void *InsertPoint;
-  MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
-  pImpl->ConstantsLock.reader_release();
+  MDNode *N;
+  {
+    sys::SmartScopedReader<true> Reader(pImpl->ConstantsLock);
+    N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
+  }  
+  if (N) return N;
   
+  sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
+  N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
   if (!N) {
-    sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
-    N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
-    if (!N) {
-      // InsertPoint will have been set by the FindNodeOrInsertPos call.
-      N = new MDNode(Context, Vals, NumVals);
-      pImpl->MDNodeSet.InsertNode(N, InsertPoint);
-    }
+    // InsertPoint will have been set by the FindNodeOrInsertPos call.
+    N = new MDNode(Context, Vals, NumVals);
+    pImpl->MDNodeSet.InsertNode(N, InsertPoint);
   }
 
   return N;
 }
 
-/// dropAllReferences - Remove all uses and clear node vector.
-void MDNode::dropAllReferences() {
-  User::dropAllReferences();
-  Node.clear();
-}
-
+/// ~MDNode - Destroy MDNode.
 MDNode::~MDNode() {
   {
     LLVMContextImpl *pImpl = getType()->getContext().pImpl;
     sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
     pImpl->MDNodeSet.RemoveNode(this);
   }
-  dropAllReferences();
+  delete [] Node;
+  Node = NULL;
 }
 
 // Replace value from this node's element list.
@@ -136,9 +105,8 @@
   // From in this MDNode's element list.
   SmallVector<unsigned, 4> Indexes;
   unsigned Index = 0;
-  for (SmallVector<ElementVH, 4>::iterator I = Node.begin(),
-         E = Node.end(); I != E; ++I, ++Index) {
-    Value *V = *I;
+  for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) {
+    Value *V = getElement(i);
     if (V && V == From) 
       Indexes.push_back(Index);
   }
@@ -152,27 +120,6 @@
     pImpl->MDNodeSet.RemoveNode(this);
   }
 
-  // MDNode only lists metadata elements in operand list, because MDNode
-  // used by MDNode is considered a valid use. However on the side, MDNode
-  // using a non-metadata value is not considered a "use" of non-metadata
-  // value.
-  SmallVector<unsigned, 4> OpIndexes;
-  unsigned OpIndex = 0;
-  for (User::op_iterator OI = op_begin(), OE = op_end();
-       OI != OE; ++OI, OpIndex++) {
-    if (*OI == From)
-      OpIndexes.push_back(OpIndex);
-  }
-  if (MetadataBase *MDTo = dyn_cast_or_null<MetadataBase>(To)) {
-    for (SmallVector<unsigned, 4>::iterator OI = OpIndexes.begin(),
-           OE = OpIndexes.end(); OI != OE; ++OI)
-      setOperand(*OI, MDTo);
-  } else {
-    for (SmallVector<unsigned, 4>::iterator OI = OpIndexes.begin(),
-           OE = OpIndexes.end(); OI != OE; ++OI)
-      setOperand(*OI, 0);
-  }
-
   // Replace From element(s) in place.
   for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end(); 
        I != E; ++I) {
@@ -209,27 +156,23 @@
 }
 
 //===----------------------------------------------------------------------===//
-//NamedMDNode implementation
+// NamedMDNode implementation.
 //
 NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
-                         MetadataBase*const* MDs, 
+                         MetadataBase *const *MDs, 
                          unsigned NumMDs, Module *ParentModule)
   : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
   setName(N);
-  NumOperands = 0;
-  resizeOperands(NumMDs);
 
-  for (unsigned i = 0; i != NumMDs; ++i) {
-    if (MDs[i])
-      OperandList[NumOperands++] = MDs[i];
-    Node.push_back(WeakMetadataVH(MDs[i]));
-  }
+  for (unsigned i = 0; i != NumMDs; ++i)
+    Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
+
   if (ParentModule)
     ParentModule->getNamedMDList().push_back(this);
 }
 
 NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
-  assert (NMD && "Invalid source NamedMDNode!");
+  assert(NMD && "Invalid source NamedMDNode!");
   SmallVector<MetadataBase *, 4> Elems;
   for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
     Elems.push_back(NMD->getElement(i));
@@ -245,7 +188,6 @@
 
 /// dropAllReferences - Remove all uses and clear node vector.
 void NamedMDNode::dropAllReferences() {
-  User::dropAllReferences();
   Node.clear();
 }
 
@@ -254,65 +196,100 @@
 }
 
 //===----------------------------------------------------------------------===//
-//Metadata implementation
+// MetadataContextImpl implementation.
 //
+namespace llvm {
+class MetadataContextImpl {
+public:
+  typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
+  typedef SmallVector<MDPairTy, 2> MDMapTy;
+  typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
+  friend class BitcodeReader;
+private:
+
+  /// MetadataStore - Collection of metadata used in this context.
+  MDStoreTy MetadataStore;
+
+  /// MDHandlerNames - Map to hold metadata handler names.
+  StringMap<unsigned> MDHandlerNames;
+
+public:
+  /// registerMDKind - Register a new metadata kind and return its ID.
+  /// A metadata kind can be registered only once. 
+  unsigned registerMDKind(StringRef Name);
+
+  /// getMDKind - Return metadata kind. If the requested metadata kind
+  /// is not registered then return 0.
+  unsigned getMDKind(StringRef Name) const;
+
+  /// getMD - Get the metadata of given kind attached to an Instruction.
+  /// If the metadata is not found then return 0.
+  MDNode *getMD(unsigned Kind, const Instruction *Inst);
 
-/// RegisterMDKind - Register a new metadata kind and return its ID.
-/// A metadata kind can be registered only once. 
-unsigned MetadataContext::RegisterMDKind(const char *Name) {
-  assert (validName(Name) && "Invalid custome metadata name!");
-  unsigned Count = MDHandlerNames.size();
-  assert(MDHandlerNames.find(Name) == MDHandlerNames.end() 
-         && "Already registered MDKind!");
-  MDHandlerNames[Name] = Count + 1;
-  return Count + 1;
-}
+  /// getMDs - Get the metadata attached to an Instruction.
+  void getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const;
 
-/// validName - Return true if Name is a valid custom metadata handler name.
-bool MetadataContext::validName(const char *Name) {
-  if (!Name)
-    return false;
+  /// addMD - Attach the metadata of given kind to an Instruction.
+  void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
+  
+  /// removeMD - Remove metadata of given kind attached with an instuction.
+  void removeMD(unsigned Kind, Instruction *Inst);
+  
+  /// removeAllMetadata - Remove all metadata attached with an instruction.
+  void removeAllMetadata(Instruction *Inst);
 
-  if (!isalpha(*Name))
-    return false;
+  /// copyMD - If metadata is attached with Instruction In1 then attach
+  /// the same metadata to In2.
+  void copyMD(Instruction *In1, Instruction *In2);
 
-  unsigned Length = strlen(Name);  
-  unsigned Count = 1;
-  ++Name;
-  while (Name &&
-         (isalnum(*Name) || *Name == '_' || *Name == '-' || *Name == '.')) {
-    ++Name;
-    ++Count;
+  /// getHandlerNames - Populate client supplied smallvector using custome
+  /// metadata name and ID.
+  void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
+
+  /// ValueIsDeleted - This handler is used to update metadata store
+  /// when a value is deleted.
+  void ValueIsDeleted(const Value *) {}
+  void ValueIsDeleted(Instruction *Inst) {
+    removeAllMetadata(Inst);
   }
-  if (Length != Count)
-    return false;
-  return true;
+  void ValueIsRAUWd(Value *V1, Value *V2);
+
+  /// ValueIsCloned - This handler is used to update metadata store
+  /// when In1 is cloned to create In2.
+  void ValueIsCloned(const Instruction *In1, Instruction *In2);
+};
+}
+
+/// registerMDKind - Register a new metadata kind and return its ID.
+/// A metadata kind can be registered only once. 
+unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
+  unsigned Count = MDHandlerNames.size();
+  assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
+  return MDHandlerNames[Name] = Count + 1;
 }
 
 /// getMDKind - Return metadata kind. If the requested metadata kind
 /// is not registered then return 0.
-unsigned MetadataContext::getMDKind(const char *Name) {
-  assert (validName(Name) && "Invalid custome metadata name!");
-  StringMap<unsigned>::iterator I = MDHandlerNames.find(Name);
+unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
+  StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
   if (I == MDHandlerNames.end())
     return 0;
 
   return I->getValue();
 }
 
-/// addMD - Attach the metadata of given kind with an Instruction.
-void MetadataContext::addMD(unsigned MDKind, MDNode *Node, Instruction *Inst) {
-  assert (Node && "Unable to add custome metadata");
+/// addMD - Attach the metadata of given kind to an Instruction.
+void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node, 
+                                Instruction *Inst) {
+  assert(Node && "Invalid null MDNode");
   Inst->HasMetadata = true;
-  MDStoreTy::iterator I = MetadataStore.find(Inst);
-  if (I == MetadataStore.end()) {
-    MDMapTy Info;
+  MDMapTy &Info = MetadataStore[Inst];
+  if (Info.empty()) {
     Info.push_back(std::make_pair(MDKind, Node));
     MetadataStore.insert(std::make_pair(Inst, Info));
     return;
   }
 
-  MDMapTy &Info = I->second;
   // If there is an entry for this MDKind then replace it.
   for (unsigned i = 0, e = Info.size(); i != e; ++i) {
     MDPairTy &P = Info[i];
@@ -324,11 +301,10 @@
 
   // Otherwise add a new entry.
   Info.push_back(std::make_pair(MDKind, Node));
-  return;
 }
 
 /// removeMD - Remove metadata of given kind attached with an instuction.
-void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
+void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
   MDStoreTy::iterator I = MetadataStore.find(Inst);
   if (I == MetadataStore.end())
     return;
@@ -341,87 +317,80 @@
       return;
     }
   }
-
-  return;
 }
   
-/// removeMDs - Remove all metadata attached with an instruction.
-void MetadataContext::removeMDs(const Instruction *Inst) {
-  // Find Metadata handles for this instruction.
-  MDStoreTy::iterator I = MetadataStore.find(Inst);
-  assert (I != MetadataStore.end() && "Invalid custom metadata info!");
-  MDMapTy &Info = I->second;
-  
-  // FIXME : Give all metadata handlers a chance to adjust.
-  
-  // Remove the entries for this instruction.
-  Info.clear();
-  MetadataStore.erase(I);
+/// removeAllMetadata - Remove all metadata attached with an instruction.
+void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
+  MetadataStore.erase(Inst);
+  Inst->HasMetadata = false;
 }
 
 /// copyMD - If metadata is attached with Instruction In1 then attach
 /// the same metadata to In2.
-void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
-  assert (In1 && In2 && "Invalid instruction!");
-   MDStoreTy::iterator I = MetadataStore.find(In1);
-  if (I == MetadataStore.end())
+void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
+  assert(In1 && In2 && "Invalid instruction!");
+  MDMapTy &In1Info = MetadataStore[In1];
+  if (In1Info.empty())
     return;
 
-  MDMapTy &In1Info = I->second;
-  MDMapTy In2Info;
   for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
-    if (MDNode *MD = dyn_cast_or_null<MDNode>(I->second))
-      addMD(I->first, MD, In2);
+    addMD(I->first, I->second, In2);
 }
 
-/// getMD - Get the metadata of given kind attached with an Instruction.
+/// getMD - Get the metadata of given kind attached to an Instruction.
 /// If the metadata is not found then return 0.
-MDNode *MetadataContext::getMD(unsigned MDKind, const Instruction *Inst) {
-  MDStoreTy::iterator I = MetadataStore.find(Inst);
-  if (I == MetadataStore.end())
+MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
+  MDMapTy &Info = MetadataStore[Inst];
+  if (Info.empty())
     return NULL;
-  
-  MDMapTy &Info = I->second;
+
   for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
     if (I->first == MDKind)
-      return dyn_cast_or_null<MDNode>(I->second);
+      return I->second;
   return NULL;
 }
 
-/// getMDs - Get the metadata attached with an Instruction.
-const MetadataContext::MDMapTy *MetadataContext::getMDs(const Instruction *Inst) {
+/// getMDs - Get the metadata attached to an Instruction.
+void MetadataContextImpl::
+getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const {
   MDStoreTy::iterator I = MetadataStore.find(Inst);
   if (I == MetadataStore.end())
-    return NULL;
-  
-  return &(I->second);
+    return;
+  for (MDMapTy::iterator MI = I->second.begin(), ME = I->second.end();
+       MI != ME; ++MI)
+    MDs.push_back(std::make_pair(MI->first, MI->second));
+  std::sort(MDs.begin(), MDs.end());
 }
 
-/// getHandlerNames - Get handler names. This is used by bitcode
-/// writer.
-const StringMap<unsigned> *MetadataContext::getHandlerNames() {
-  return &MDHandlerNames;
+/// getHandlerNames - Populate client supplied smallvector using custome
+/// metadata name and ID.
+void MetadataContextImpl::
+getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
+  for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
+         E = MDHandlerNames.end(); I != E; ++I) 
+    Names.push_back(std::make_pair(I->second, I->first()));
+  std::sort(Names.begin(), Names.end());
 }
 
 /// ValueIsCloned - This handler is used to update metadata store
 /// when In1 is cloned to create In2.
-void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
+void MetadataContextImpl::ValueIsCloned(const Instruction *In1, 
+                                        Instruction *In2) {
   // Find Metadata handles for In1.
   MDStoreTy::iterator I = MetadataStore.find(In1);
-  assert (I != MetadataStore.end() && "Invalid custom metadata info!");
+  assert(I != MetadataStore.end() && "Invalid custom metadata info!");
 
   // FIXME : Give all metadata handlers a chance to adjust.
 
   MDMapTy &In1Info = I->second;
   MDMapTy In2Info;
   for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
-    if (MDNode *MD = dyn_cast_or_null<MDNode>(I->second))
-      addMD(I->first, MD, In2);
+    addMD(I->first, I->second, In2);
 }
 
 /// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
 /// V2.
-void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
+void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
   Instruction *I1 = dyn_cast<Instruction>(V1);
   Instruction *I2 = dyn_cast<Instruction>(V2);
   if (!I1 || !I2)
@@ -431,3 +400,94 @@
   ValueIsCloned(I1, I2);
 }
 
+//===----------------------------------------------------------------------===//
+// MetadataContext implementation.
+//
+MetadataContext::MetadataContext() 
+  : pImpl(new MetadataContextImpl()) { }
+MetadataContext::~MetadataContext() { delete pImpl; }
+
+/// isValidName - Return true if Name is a valid custom metadata handler name.
+bool MetadataContext::isValidName(StringRef MDName) {
+  if (MDName.empty())
+    return false;
+
+  if (!isalpha(MDName[0]))
+    return false;
+
+  for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
+       ++I) {
+    if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
+        return false;
+  }
+  return true;
+}
+
+/// registerMDKind - Register a new metadata kind and return its ID.
+/// A metadata kind can be registered only once. 
+unsigned MetadataContext::registerMDKind(StringRef Name) {
+  assert(isValidName(Name) && "Invalid custome metadata name!");
+  return pImpl->registerMDKind(Name);
+}
+
+/// getMDKind - Return metadata kind. If the requested metadata kind
+/// is not registered then return 0.
+unsigned MetadataContext::getMDKind(StringRef Name) const {
+  return pImpl->getMDKind(Name);
+}
+
+/// getMD - Get the metadata of given kind attached to an Instruction.
+/// If the metadata is not found then return 0.
+MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
+  return pImpl->getMD(Kind, Inst);
+}
+
+/// getMDs - Get the metadata attached to an Instruction.
+void MetadataContext::
+getMDs(const Instruction *Inst, 
+       SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const {
+  return pImpl->getMDs(Inst, MDs);
+}
+
+/// addMD - Attach the metadata of given kind to an Instruction.
+void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
+  pImpl->addMD(Kind, Node, Inst);
+}
+  
+/// removeMD - Remove metadata of given kind attached with an instuction.
+void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
+  pImpl->removeMD(Kind, Inst);
+}
+  
+/// removeAllMetadata - Remove all metadata attached with an instruction.
+void MetadataContext::removeAllMetadata(Instruction *Inst) {
+  pImpl->removeAllMetadata(Inst);
+}
+
+/// copyMD - If metadata is attached with Instruction In1 then attach
+/// the same metadata to In2.
+void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
+  pImpl->copyMD(In1, In2);
+}
+
+/// getHandlerNames - Populate client supplied smallvector using custome
+/// metadata name and ID.
+void MetadataContext::
+getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
+  pImpl->getHandlerNames(N);
+}
+
+/// ValueIsDeleted - This handler is used to update metadata store
+/// when a value is deleted.
+void MetadataContext::ValueIsDeleted(Instruction *Inst) {
+  pImpl->ValueIsDeleted(Inst);
+}
+void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
+  pImpl->ValueIsRAUWd(V1, V2);
+}
+
+/// ValueIsCloned - This handler is used to update metadata store
+/// when In1 is cloned to create In2.
+void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
+  pImpl->ValueIsCloned(In1, In2);
+}

Modified: llvm/branches/Apple/Leela/lib/VMCore/Pass.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Leela/lib/VMCore/Pass.cpp?rev=84951&r1=84950&r2=84951&view=diff

==============================================================================
--- llvm/branches/Apple/Leela/lib/VMCore/Pass.cpp (original)
+++ llvm/branches/Apple/Leela/lib/VMCore/Pass.cpp Fri Oct 23 11:40:52 2009
@@ -18,6 +18,7 @@
 #include "llvm/Module.h"
 #include "llvm/ModuleProvider.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/System/Atomic.h"





More information about the llvm-branch-commits mailing list