[llvm-commits] CVS: llvm/lib/VMCore/Constants.cpp

Chris Lattner lattner at cs.uiuc.edu
Sat Feb 14 20:47:00 PST 2004


Changes in directory llvm/lib/VMCore:

Constants.cpp updated: 1.74 -> 1.75

---
Log message:

Keep a cache of non-abstract null arrays and structs.  This speeds up llvm-dis
from 16.57 -> 13.46s on 129.compress.


---
Diffs of the changes:  (+19 -2)

Index: llvm/lib/VMCore/Constants.cpp
diff -u llvm/lib/VMCore/Constants.cpp:1.74 llvm/lib/VMCore/Constants.cpp:1.75
--- llvm/lib/VMCore/Constants.cpp:1.74	Sun Feb  8 22:37:31 2004
+++ llvm/lib/VMCore/Constants.cpp	Sat Feb 14 20:46:46 2004
@@ -64,6 +64,8 @@
   delete this;
 }
 
+static std::map<const Type *, Constant*> NullValues;
+
 // Static constructor to create a '0' constant of arbitrary type...
 Constant *Constant::getNullValue(const Type *Ty) {
   switch (Ty->getPrimitiveID()) {
@@ -117,18 +119,33 @@
     return ConstantPointerNull::get(cast<PointerType>(Ty));
 
   case Type::StructTyID: {
+    if (!Ty->isAbstract())
+      if (Constant *V = NullValues[Ty])
+        return V;
+
     const StructType *ST = cast<StructType>(Ty);
     std::vector<Constant*> Elements;
     Elements.resize(ST->getNumElements());
     for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i)
       Elements[i] = Constant::getNullValue(ST->getElementType(i));
-    return ConstantStruct::get(ST, Elements);
+    Constant *Ret = ConstantStruct::get(ST, Elements);
+    if (!Ty->isAbstract())
+      NullValues[Ty] = Ret;
+    return Ret;
   }
   case Type::ArrayTyID: {
+    if (!Ty->isAbstract())
+      if (Constant *V = NullValues[Ty])
+        return V;
+
     const ArrayType *AT = cast<ArrayType>(Ty);
     Constant *El = Constant::getNullValue(AT->getElementType());
     unsigned NumElements = AT->getNumElements();
-    return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El));
+    Constant *Ret = ConstantArray::get(AT,
+                                       std::vector<Constant*>(NumElements, El));
+    if (!Ty->isAbstract())
+      NullValues[Ty] = Ret;
+    return Ret;
   }
   default:
     // Function, Type, Label, or Opaque type?





More information about the llvm-commits mailing list