[llvm-commits] CVS: llvm/include/llvm/DerivedTypes.h Type.h

Chris Lattner lattner at cs.uiuc.edu
Sun Feb 8 23:41:02 PST 2004


Changes in directory llvm/include/llvm:

DerivedTypes.h updated: 1.52 -> 1.53
Type.h updated: 1.37 -> 1.38

---
Log message:

Now that all of the derived types have disciplined interfaces, we can eliminate
all of the ad-hoc storage of contained types.  This allows getContainedType to
not be virtual, and allows us to entirely delete the TypeIterator class. 


---
Diffs of the changes:  (+35 -115)

Index: llvm/include/llvm/DerivedTypes.h
diff -u llvm/include/llvm/DerivedTypes.h:1.52 llvm/include/llvm/DerivedTypes.h:1.53
--- llvm/include/llvm/DerivedTypes.h:1.52	Sun Feb  8 22:36:50 2004
+++ llvm/include/llvm/DerivedTypes.h	Sun Feb  8 23:40:14 2004
@@ -19,7 +19,6 @@
 #define LLVM_DERIVED_TYPES_H
 
 #include "llvm/Type.h"
-#include <vector>
 
 namespace llvm {
 
@@ -57,7 +56,7 @@
   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
   // another (more concrete) type, we must eliminate all references to other
   // types, to avoid some circular reference problems.
-  virtual void dropAllTypeUses() = 0;
+  void dropAllTypeUses();
   
 public:
 
@@ -122,8 +121,6 @@
 
 class FunctionType : public DerivedType {
   friend class TypeMap<FunctionValType, FunctionType>;
-  PATypeHandle ResultType;
-  std::vector<PATypeHandle> ParamTys;
   bool isVarArgs;
 
   FunctionType(const FunctionType &);                   // Do not implement
@@ -137,11 +134,6 @@
   FunctionType(const Type *Result, const std::vector<const Type*> &Params, 
                bool IsVarArgs);
 
-  // dropAllTypeUses - When this (abstract) type is resolved to be equal to
-  // another (more concrete) type, we must eliminate all references to other
-  // types, to avoid some circular reference problems.
-  virtual void dropAllTypeUses();
-
 public:
   /// FunctionType::get - This static method is the primary way of constructing
   /// a FunctionType
@@ -150,25 +142,19 @@
                            bool isVarArg);
 
   inline bool isVarArg() const { return isVarArgs; }
-  inline const Type *getReturnType() const { return ResultType; }
+  inline const Type *getReturnType() const { return ContainedTys[0]; }
 
   typedef std::vector<PATypeHandle>::const_iterator param_iterator;
-  param_iterator param_begin() const { return ParamTys.begin(); }
-  param_iterator param_end() const { return ParamTys.end(); }
+  param_iterator param_begin() const { return ContainedTys.begin()+1; }
+  param_iterator param_end() const { return ContainedTys.end(); }
 
   // Parameter type accessors...
-  const Type *getParamType(unsigned i) const { return ParamTys[i]; }
+  const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
 
   // getNumParams - Return the number of fixed parameters this function type
   // requires.  This does not consider varargs.
   //
-  unsigned getNumParams() const { return ParamTys.size(); }
-
-
-  virtual const Type *getContainedType(unsigned i) const {
-    return i == 0 ? ResultType.get() : ParamTys[i-1].get();
-  }
-  virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
+  unsigned getNumParams() const { return ContainedTys.size()-1; }
 
   // Implement the AbstractTypeUser interface.
   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
@@ -213,8 +199,6 @@
 
 class StructType : public CompositeType {
   friend class TypeMap<StructValType, StructType>;
-  std::vector<PATypeHandle> ETypes;                 // Element types of struct
-
   StructType(const StructType &);                   // Do not implement
   const StructType &operator=(const StructType &);  // Do not implement
 
@@ -226,11 +210,6 @@
   // Private ctor - Only can be created by a static member...
   StructType(const std::vector<const Type*> &Types);
 
-  // dropAllTypeUses - When this (abstract) type is resolved to be equal to
-  // another (more concrete) type, we must eliminate all references to other
-  // types, to avoid some circular reference problems.
-  virtual void dropAllTypeUses();
-  
 public:
   /// StructType::get - This static method is the primary way to create a
   /// StructType.
@@ -238,20 +217,15 @@
 
   // Iterator access to the elements
   typedef std::vector<PATypeHandle>::const_iterator element_iterator;
-  element_iterator element_begin() const { return ETypes.begin(); }
-  element_iterator element_end() const { return ETypes.end(); }
+  element_iterator element_begin() const { return ContainedTys.begin(); }
+  element_iterator element_end() const { return ContainedTys.end(); }
 
   // Random access to the elements
-  unsigned getNumElements() const { return ETypes.size(); }
+  unsigned getNumElements() const { return ContainedTys.size(); }
   const Type *getElementType(unsigned N) const {
-    assert(N < ETypes.size() && "Element number out of range!");
-    return ETypes[N];
-  }
-
-  virtual const Type *getContainedType(unsigned i) const { 
-    return ETypes[i].get();
+    assert(N < ContainedTys.size() && "Element number out of range!");
+    return ContainedTys[N];
   }
-  virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
 
   // getTypeAtIndex - Given an index value into the type, return the type of the
   // element.  For a structure type, this must be a constant value...
@@ -284,25 +258,19 @@
   SequentialType(const SequentialType &);                  // Do not implement!
   const SequentialType &operator=(const SequentialType &); // Do not implement!
 protected:
-  PATypeHandle ElementType;
-
-  SequentialType(PrimitiveID TID, const Type *ElType)
-    : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
+  SequentialType(PrimitiveID TID, const Type *ElType) : CompositeType(TID) {
+    ContainedTys.reserve(1);
+    ContainedTys.push_back(PATypeHandle(ElType, this));
   }
 
 public:
-  inline const Type *getElementType() const { return ElementType; }
-
-  virtual const Type *getContainedType(unsigned i) const { 
-    return ElementType.get();
-  }
-  virtual unsigned getNumContainedTypes() const { return 1; }
+  inline const Type *getElementType() const { return ContainedTys[0]; }
 
   // getTypeAtIndex - Given an index value into the type, return the type of the
   // element.  For sequential types, there is only one subtype...
   //
   virtual const Type *getTypeAtIndex(const Value *V) const {
-    return ElementType.get();
+    return ContainedTys[0];
   }
   virtual bool indexValid(const Value *V) const {
     return V->getType()->isInteger();
@@ -334,11 +302,6 @@
   // Private ctor - Only can be created by a static member...
   ArrayType(const Type *ElType, unsigned NumEl);
 
-  // dropAllTypeUses - When this (abstract) type is resolved to be equal to
-  // another (more concrete) type, we must eliminate all references to other
-  // types, to avoid some circular reference problems.
-  virtual void dropAllTypeUses();
-
 public:
   /// ArrayType::get - This static method is the primary way to construct an
   /// ArrayType
@@ -374,10 +337,6 @@
   // Private ctor - Only can be created by a static member...
   PointerType(const Type *ElType);
 
-  // dropAllTypeUses - When this (abstract) type is resolved to be equal to
-  // another (more concrete) type, we must eliminate all references to other
-  // types, to avoid some circular reference problems.
-  virtual void dropAllTypeUses();
 public:
   /// PointerType::get - This is the only way to construct a new pointer type.
   static PointerType *get(const Type *ElementType);
@@ -407,13 +366,6 @@
 
   // Private ctor - Only can be created by a static member...
   OpaqueType();
-
-  // dropAllTypeUses - When this (abstract) type is resolved to be equal to
-  // another (more concrete) type, we must eliminate all references to other
-  // types, to avoid some circular reference problems.
-  virtual void dropAllTypeUses() {
-    // FIXME: THIS IS NOT AN ABSTRACT TYPE USER!
-  }  // No type uses
 
 public:
   // OpaqueType::get - Static factory method for the OpaqueType class...


Index: llvm/include/llvm/Type.h
diff -u llvm/include/llvm/Type.h:1.37 llvm/include/llvm/Type.h:1.38
--- llvm/include/llvm/Type.h:1.37	Tue Nov 11 16:41:30 2003
+++ llvm/include/llvm/Type.h	Sun Feb  8 23:40:14 2004
@@ -36,6 +36,7 @@
 #include "llvm/Value.h"
 #include "Support/GraphTraits.h"
 #include "Support/iterator"
+#include <vector>
 
 namespace llvm {
 
@@ -106,6 +107,15 @@
   /// to the more refined type.  Only abstract types can be forwarded.
   mutable const Type *ForwardType;
 
+  /// ContainedTys - The list of types contained by this one.  For example, this
+  /// includes the arguments of a function type, the elements of the structure,
+  /// the pointee of a pointer, etc.  Note that keeping this vector in the Type
+  /// class wastes some space for types that do not contain anything (such as
+  /// primitive types).  However, keeping it here allows the subtype_* members
+  /// to be implemented MUCH more efficiently, and dynamically very few types do
+  /// not contain any elements (most are derived).
+  std::vector<PATypeHandle> ContainedTys;
+
 public:
   virtual void print(std::ostream &O) const;
 
@@ -204,22 +214,22 @@
   //===--------------------------------------------------------------------===//
   // Type Iteration support
   //
-  class TypeIterator;
-  typedef TypeIterator subtype_iterator;
-  inline subtype_iterator subtype_begin() const;   // DEFINED BELOW
-  inline subtype_iterator subtype_end() const;     // DEFINED BELOW
+  typedef std::vector<PATypeHandle>::const_iterator subtype_iterator;
+  subtype_iterator subtype_begin() const { return ContainedTys.begin(); }
+  subtype_iterator subtype_end() const { return ContainedTys.end(); }
 
   /// getContainedType - This method is used to implement the type iterator
   /// (defined a the end of the file).  For derived types, this returns the
   /// types 'contained' in the derived type.
   ///
-  virtual const Type *getContainedType(unsigned i) const {
-    assert(0 && "No contained types!");
-    return 0;
+  const Type *getContainedType(unsigned i) const {
+    assert(i < ContainedTys.size() && "Index out of range!");
+    return ContainedTys[i];
   }
 
-  /// getNumContainedTypes - Return the number of types in the derived type
-  virtual unsigned getNumContainedTypes() const { return 0; }
+  /// getNumContainedTypes - Return the number of types in the derived type.
+  ///
+  unsigned getNumContainedTypes() const { return ContainedTys.size(); }
 
   //===--------------------------------------------------------------------===//
   // Static members exported by the Type class itself.  Useful for getting
@@ -249,49 +259,7 @@
   }
 
 #include "llvm/Type.def"
-
-private:
-  class TypeIterator : public bidirectional_iterator<const Type, ptrdiff_t> {
-    const Type * const Ty;
-    unsigned Idx;
-
-    typedef TypeIterator _Self;
-  public:
-    TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
-    ~TypeIterator() {}
-
-    const _Self &operator=(const _Self &RHS) {
-      assert(Ty == RHS.Ty && "Cannot assign from different types!");
-      Idx = RHS.Idx;
-      return *this;
-    }
-    
-    bool operator==(const _Self& x) const { return Idx == x.Idx; }
-    bool operator!=(const _Self& x) const { return !operator==(x); }
-    
-    pointer operator*() const { return Ty->getContainedType(Idx); }
-    pointer operator->() const { return operator*(); }
-    
-    _Self& operator++() { ++Idx; return *this; } // Preincrement
-    _Self operator++(int) { // Postincrement
-      _Self tmp = *this; ++*this; return tmp; 
-    }
-    
-    _Self& operator--() { --Idx; return *this; }  // Predecrement
-    _Self operator--(int) { // Postdecrement
-      _Self tmp = *this; --*this; return tmp;
-    }
-  };
 };
-
-inline Type::TypeIterator Type::subtype_begin() const {
-  return TypeIterator(this, 0);
-}
-
-inline Type::TypeIterator Type::subtype_end() const {
-  return TypeIterator(this, getNumContainedTypes());
-}
-
 
 // Provide specializations of GraphTraits to be able to treat a type as a 
 // graph of sub types...





More information about the llvm-commits mailing list