For PR1146:
Make ParamAttrsList objects unique. You can no longer directly create or
destroy them but instead must go through the ParamAttrsList::get()
interface.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36327 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AsmParser/llvmAsmParser.y b/lib/AsmParser/llvmAsmParser.y
index a0c2e25..9b5cc5a 100644
--- a/lib/AsmParser/llvmAsmParser.y
+++ b/lib/AsmParser/llvmAsmParser.y
@@ -1299,24 +1299,28 @@
   }
   | Types '(' ArgTypeListI ')' OptFuncAttrs {
     std::vector<const Type*> Params;
-    ParamAttrsList Attrs;
-    if ($5 != ParamAttr::None)
-      Attrs.addAttributes(0, $5);
+    ParamAttrsVector Attrs;
+    if ($5 != ParamAttr::None) {
+      ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
+      Attrs.push_back(X);
+    }
     unsigned index = 1;
     TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
     for (; I != E; ++I, ++index) {
       const Type *Ty = I->Ty->get();
       Params.push_back(Ty);
       if (Ty != Type::VoidTy)
-        if (I->Attrs != ParamAttr::None)
-          Attrs.addAttributes(index, I->Attrs);
+        if (I->Attrs != ParamAttr::None) {
+          ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
+          Attrs.push_back(X);
+        }
     }
     bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
     if (isVarArg) Params.pop_back();
 
     ParamAttrsList *ActualAttrs = 0;
     if (!Attrs.empty())
-      ActualAttrs = new ParamAttrsList(Attrs);
+      ActualAttrs = ParamAttrsList::get(Attrs);
     FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
     delete $3;   // Delete the argument list
     delete $1;   // Delete the return type handle
@@ -1325,24 +1329,28 @@
   }
   | VOID '(' ArgTypeListI ')' OptFuncAttrs {
     std::vector<const Type*> Params;
-    ParamAttrsList Attrs;
-    if ($5 != ParamAttr::None)
-      Attrs.addAttributes(0, $5);
+    ParamAttrsVector Attrs;
+    if ($5 != ParamAttr::None) {
+      ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
+      Attrs.push_back(X);
+    }
     TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
     unsigned index = 1;
     for ( ; I != E; ++I, ++index) {
       const Type* Ty = I->Ty->get();
       Params.push_back(Ty);
       if (Ty != Type::VoidTy)
-        if (I->Attrs != ParamAttr::None)
-          Attrs.addAttributes(index, I->Attrs);
+        if (I->Attrs != ParamAttr::None) {
+          ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
+          Attrs.push_back(X);
+        }
     }
     bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
     if (isVarArg) Params.pop_back();
 
     ParamAttrsList *ActualAttrs = 0;
     if (!Attrs.empty())
-      ActualAttrs = new ParamAttrsList(Attrs);
+      ActualAttrs = ParamAttrsList::get(Attrs);
 
     FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
     delete $3;      // Delete the argument list
@@ -2135,9 +2143,11 @@
     GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
 
   std::vector<const Type*> ParamTypeList;
-  ParamAttrsList ParamAttrs;
-  if ($7 != ParamAttr::None)
-    ParamAttrs.addAttributes(0, $7);
+  ParamAttrsVector Attrs;
+  if ($7 != ParamAttr::None) {
+    ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
+    Attrs.push_back(PAWI);
+  }
   if ($5) {   // If there are arguments...
     unsigned index = 1;
     for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
@@ -2146,20 +2156,21 @@
         GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
       ParamTypeList.push_back(Ty);
       if (Ty != Type::VoidTy)
-        if (I->Attrs != ParamAttr::None)
-          ParamAttrs.addAttributes(index, I->Attrs);
+        if (I->Attrs != ParamAttr::None) {
+          ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
+          Attrs.push_back(PAWI);
+        }
     }
   }
 
   bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
   if (isVarArg) ParamTypeList.pop_back();
 
-  ParamAttrsList *ActualAttrs = 0;
-  if (!ParamAttrs.empty())
-    ActualAttrs = new ParamAttrsList(ParamAttrs);
+  ParamAttrsList *PAL = 0;
+  if (!Attrs.empty())
+    PAL = ParamAttrsList::get(Attrs);
 
-  FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, 
-                                       ActualAttrs);
+  FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
   const PointerType *PFT = PointerType::get(FT);
   delete $2;
 
@@ -2490,9 +2501,11 @@
         !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
       // Pull out the types of all of the arguments...
       std::vector<const Type*> ParamTypes;
-      ParamAttrsList ParamAttrs;
-      if ($8 != ParamAttr::None)
-        ParamAttrs.addAttributes(0, $8);
+      ParamAttrsVector Attrs;
+      if ($8 != ParamAttr::None) {
+        ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = 8;
+        Attrs.push_back(PAWI);
+      }
       ValueRefList::iterator I = $6->begin(), E = $6->end();
       unsigned index = 1;
       for (; I != E; ++I, ++index) {
@@ -2500,14 +2513,16 @@
         if (Ty == Type::VoidTy)
           GEN_ERROR("Short call syntax cannot be used with varargs");
         ParamTypes.push_back(Ty);
-        if (I->Attrs != ParamAttr::None)
-          ParamAttrs.addAttributes(index, I->Attrs);
+        if (I->Attrs != ParamAttr::None) {
+          ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
+          Attrs.push_back(PAWI);
+        }
       }
 
-      ParamAttrsList *Attrs = 0;
-      if (!ParamAttrs.empty())
-        Attrs = new ParamAttrsList(ParamAttrs);
-      Ty = FunctionType::get($3->get(), ParamTypes, false, Attrs);
+      ParamAttrsList *PAL = 0;
+      if (!Attrs.empty())
+        PAL = ParamAttrsList::get(Attrs);
+      Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
       PFTy = PointerType::get(Ty);
     }
 
@@ -2796,9 +2811,11 @@
         !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
       // Pull out the types of all of the arguments...
       std::vector<const Type*> ParamTypes;
-      ParamAttrsList ParamAttrs;
-      if ($8 != ParamAttr::None)
-        ParamAttrs.addAttributes(0, $8);
+      ParamAttrsVector Attrs;
+      if ($8 != ParamAttr::None) {
+        ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
+        Attrs.push_back(PAWI);
+      }
       unsigned index = 1;
       ValueRefList::iterator I = $6->begin(), E = $6->end();
       for (; I != E; ++I, ++index) {
@@ -2806,15 +2823,17 @@
         if (Ty == Type::VoidTy)
           GEN_ERROR("Short call syntax cannot be used with varargs");
         ParamTypes.push_back(Ty);
-        if (I->Attrs != ParamAttr::None)
-          ParamAttrs.addAttributes(index, I->Attrs);
+        if (I->Attrs != ParamAttr::None) {
+          ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
+          Attrs.push_back(PAWI);
+        }
       }
 
-      ParamAttrsList *Attrs = 0;
-      if (!ParamAttrs.empty())
-        Attrs = new ParamAttrsList(ParamAttrs);
+      ParamAttrsList *PAL = 0;
+      if (!Attrs.empty())
+        PAL = ParamAttrsList::get(Attrs);
 
-      Ty = FunctionType::get($3->get(), ParamTypes, false, Attrs);
+      Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
       PFTy = PointerType::get(Ty);
     }
 
diff --git a/lib/Bytecode/Reader/Reader.cpp b/lib/Bytecode/Reader/Reader.cpp
index 4979269..4cb67c3 100644
--- a/lib/Bytecode/Reader/Reader.cpp
+++ b/lib/Bytecode/Reader/Reader.cpp
@@ -1078,16 +1078,18 @@
 
 ParamAttrsList *BytecodeReader::ParseParamAttrsList() {
   unsigned NumAttrs = read_vbr_uint();
-  ParamAttrsList *Attrs = 0;
+  ParamAttrsList *PAL = 0;
   if (NumAttrs) {
-    Attrs = new ParamAttrsList();
+    ParamAttrsVector Attrs;
+    ParamAttrsWithIndex PAWI;
     while (NumAttrs--) {
-      uint16_t index = read_vbr_uint();
-      uint16_t attrs = read_vbr_uint();
-      Attrs->addAttributes(index, attrs);
+      PAWI.index = read_vbr_uint();
+      PAWI.attrs = read_vbr_uint();
+      Attrs.push_back(PAWI);
     }
+    PAL = ParamAttrsList::get(Attrs);
   }
-  return Attrs;
+  return PAL;
 }
 
 
diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp
index da3b1d7..63ec8e4 100644
--- a/lib/VMCore/AsmWriter.cpp
+++ b/lib/VMCore/AsmWriter.cpp
@@ -1383,6 +1383,17 @@
 // Located here because so much of the needed functionality is here.
 void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
 
+void
+ParamAttrsList::dump() const {
+  cerr << "PAL[ ";
+  for (unsigned i = 0; i < attrs.size(); ++i) {
+    uint16_t index = getParamIndex(i);
+    uint16_t attrs = getParamAttrs(index);
+    cerr << "{" << index << "," << attrs << "} ";
+  }
+  cerr << "]\n";
+}
+
 //===----------------------------------------------------------------------===//
 //                         SlotMachine Implementation
 //===----------------------------------------------------------------------===//
diff --git a/lib/VMCore/Function.cpp b/lib/VMCore/Function.cpp
index dbd2148..b6ff70d 100644
--- a/lib/VMCore/Function.cpp
+++ b/lib/VMCore/Function.cpp
@@ -16,6 +16,7 @@
 #include "llvm/ParameterAttributes.h"
 #include "llvm/IntrinsicInst.h"
 #include "llvm/Support/LeakDetector.h"
+#include "llvm/Support/ManagedStatic.h"
 #include "SymbolTableListTraitsImpl.h"
 #include "llvm/ADT/StringExtras.h"
 using namespace llvm;
@@ -103,35 +104,29 @@
   return Result;
 }
 
-void
-ParamAttrsList::addAttributes(uint16_t Index, uint16_t Attrs) {
-  // First, try to replace an existing one
-  for (unsigned i = 0; i < attrs.size(); ++i)
-    if (attrs[i].index == Index) {
-      attrs[i].attrs |= Attrs;
-      return;
-    }
-
-  // If not found, add a new one
-  ParamAttrsWithIndex Val;
-  Val.attrs = Attrs;
-  Val.index = Index;
-  attrs.push_back(Val);
+void 
+ParamAttrsList::Profile(FoldingSetNodeID &ID) const {
+  for (unsigned i = 0; i < attrs.size(); ++i) {
+    unsigned val = attrs[i].attrs << 16 | attrs[i].index;
+    ID.AddInteger(val);
+  }
 }
 
-void
-ParamAttrsList::removeAttributes(uint16_t Index, uint16_t Attrs) {
-  // Find the index from which to remove the attributes
-  for (unsigned i = 0; i < attrs.size(); ++i)
-    if (attrs[i].index == Index) {
-      attrs[i].attrs &= ~Attrs;
-      if (attrs[i].attrs == ParamAttr::None)
-        attrs.erase(&attrs[i]);
-      return;
-    }
+static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
 
-  // The index wasn't found above
-  assert(0 && "Index not found for removeAttributes");
+ParamAttrsList *
+ParamAttrsList::get(const ParamAttrsVector &attrVec) {
+  assert(!attrVec.empty() && "Illegal to create empty ParamAttrsList");
+  ParamAttrsList key(attrVec);
+  FoldingSetNodeID ID;
+  key.Profile(ID);
+  void *InsertPos;
+  ParamAttrsList* PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
+  if (!PAL) {
+    PAL = new ParamAttrsList(attrVec);
+    ParamAttrsLists->InsertNode(PAL, InsertPos);
+  }
+  return PAL;
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp
index bfda46d..3bb565d 100644
--- a/lib/VMCore/Instructions.cpp
+++ b/lib/VMCore/Instructions.cpp
@@ -186,7 +186,6 @@
 
 CallInst::~CallInst() {
   delete [] OperandList;
-  delete ParamAttrs; // FIXME: ParamAttrsList should be uniqued!
 }
 
 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
@@ -354,7 +353,6 @@
 
 InvokeInst::~InvokeInst() {
   delete [] OperandList;
-  delete ParamAttrs; // FIXME: ParamAttrsList should be uniqued!
 }
 
 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
diff --git a/lib/VMCore/Type.cpp b/lib/VMCore/Type.cpp
index 01232c4..3e5b7eb 100644
--- a/lib/VMCore/Type.cpp
+++ b/lib/VMCore/Type.cpp
@@ -643,20 +643,13 @@
       return false;
     const ParamAttrsList *Attrs1 = FTy->getParamAttrs();
     const ParamAttrsList *Attrs2 = FTy2->getParamAttrs();
-    if ((!Attrs1 && Attrs2 && !Attrs2->empty()) ||
-        (!Attrs2 && Attrs1 && !Attrs1->empty()) ||
+    if ((!Attrs1 && Attrs2) || (!Attrs2 && Attrs1) ||
         (Attrs1 && Attrs2 && (Attrs1->size() != Attrs2->size() ||
-         (Attrs1->size() > 0 && 
-          Attrs1->getParamAttrs(0) != Attrs2->getParamAttrs(0)))))
+         (Attrs1->getParamAttrs(0) != Attrs2->getParamAttrs(0)))))
       return false;
-    ParamAttrsList PAL1;
-    if (Attrs1)
-      PAL1 = *Attrs1;
-    ParamAttrsList PAL2;
-    if (Attrs2)
-      PAL2 = *Attrs2;
+
     for (unsigned i = 0, e = FTy2->getNumParams(); i != e; ++i) {
-      if (PAL1.getParamAttrs(i+1) != PAL2.getParamAttrs(i+1))
+      if (Attrs1 && Attrs1->getParamAttrs(i+1) != Attrs2->getParamAttrs(i+1))
         return false;
       if (!TypesEqual(FTy->getParamType(i), FTy2->getParamType(i), EqTypes))
         return false;
@@ -1065,15 +1058,10 @@
     if (ParamAttrs)
       if (MTV.ParamAttrs)
         return *ParamAttrs < *MTV.ParamAttrs;
-      else if (ParamAttrs->empty())
-        return true;
       else
         return false;
     else if (MTV.ParamAttrs)
-      if (MTV.ParamAttrs->empty())
-        return false;
-      else
-        return true;
+      return true;
     return false;
   }
 };
@@ -1100,26 +1088,20 @@
                                 ParamAttrsList *Attrs) {
 
   FunctionValType VT(ReturnType, Params, isVarArg, Attrs);
-  FunctionType *MT = FunctionTypes->get(VT);
-  if (MT) { 
-    delete Attrs; // not needed any more
-    return MT;
+  FunctionType *FT = FunctionTypes->get(VT);
+  if (FT) { 
+    return FT;
   }
 
-
-  MT = (FunctionType*) new char[sizeof(FunctionType) + 
+  FT = (FunctionType*) new char[sizeof(FunctionType) + 
                                 sizeof(PATypeHandle)*(Params.size()+1)];
-  new (MT) FunctionType(ReturnType, Params, isVarArg, Attrs);
-  FunctionTypes->add(VT, MT);
+  new (FT) FunctionType(ReturnType, Params, isVarArg, Attrs);
+  FunctionTypes->add(VT, FT);
 
 #ifdef DEBUG_MERGE_TYPES
-  DOUT << "Derived new type: " << MT << "\n";
+  DOUT << "Derived new type: " << FT << "\n";
 #endif
-  return MT;
-}
-
-FunctionType::~FunctionType() {
-  delete ParamAttrs;
+  return FT;
 }
 
 bool FunctionType::isStructReturn() const {