Privatize the ConstantVector tables.
[oota-llvm.git] / lib / VMCore / LLVMContextImpl.cpp
1 //===--------------- LLVMContextImpl.cpp - Implementation ------*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements LLVMContextImpl, the opaque implementation 
11 //  of LLVMContext.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "LLVMContextImpl.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/MDNode.h"
20 using namespace llvm;
21
22 static char getValType(ConstantAggregateZero *CPZ) { return 0; }
23
24 static std::vector<Constant*> getValType(ConstantArray *CA) {
25   std::vector<Constant*> Elements;
26   Elements.reserve(CA->getNumOperands());
27   for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
28     Elements.push_back(cast<Constant>(CA->getOperand(i)));
29   return Elements;
30 }
31
32 static std::vector<Constant*> getValType(ConstantStruct *CS) {
33   std::vector<Constant*> Elements;
34   Elements.reserve(CS->getNumOperands());
35   for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
36     Elements.push_back(cast<Constant>(CS->getOperand(i)));
37   return Elements;
38 }
39
40 static std::vector<Constant*> getValType(ConstantVector *CP) {
41   std::vector<Constant*> Elements;
42   Elements.reserve(CP->getNumOperands());
43   for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
44     Elements.push_back(CP->getOperand(i));
45   return Elements;
46 }
47
48 namespace llvm {
49 template<typename T, typename Alloc>
50 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
51   static unsigned uses(const std::vector<T, Alloc>& v) {
52     return v.size();
53   }
54 };
55
56 template<class ConstantClass, class TypeClass, class ValType>
57 struct VISIBILITY_HIDDEN ConstantCreator {
58   static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
59     return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
60   }
61 };
62
63 template<class ConstantClass, class TypeClass>
64 struct VISIBILITY_HIDDEN ConvertConstantType {
65   static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
66     llvm_unreachable("This type cannot be converted!");
67   }
68 };
69
70 // ConstantAggregateZero does not take extra "value" argument...
71 template<class ValType>
72 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
73   static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
74     return new ConstantAggregateZero(Ty);
75   }
76 };
77
78 template<>
79 struct ConvertConstantType<ConstantAggregateZero, Type> {
80   static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
81     // Make everyone now use a constant of the new type...
82     Constant *New = NewTy->getContext().getConstantAggregateZero(NewTy);
83     assert(New != OldC && "Didn't replace constant??");
84     OldC->uncheckedReplaceAllUsesWith(New);
85     OldC->destroyConstant();     // This constant is now dead, destroy it.
86   }
87 };
88
89 template<>
90 struct ConvertConstantType<ConstantArray, ArrayType> {
91   static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
92     // Make everyone now use a constant of the new type...
93     std::vector<Constant*> C;
94     for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
95       C.push_back(cast<Constant>(OldC->getOperand(i)));
96     Constant *New = NewTy->getContext().getConstantArray(NewTy, C);
97     assert(New != OldC && "Didn't replace constant??");
98     OldC->uncheckedReplaceAllUsesWith(New);
99     OldC->destroyConstant();    // This constant is now dead, destroy it.
100   }
101 };
102
103 template<>
104 struct ConvertConstantType<ConstantStruct, StructType> {
105   static void convert(ConstantStruct *OldC, const StructType *NewTy) {
106     // Make everyone now use a constant of the new type...
107     std::vector<Constant*> C;
108     for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
109       C.push_back(cast<Constant>(OldC->getOperand(i)));
110     Constant *New = NewTy->getContext().getConstantStruct(NewTy, C);
111     assert(New != OldC && "Didn't replace constant??");
112
113     OldC->uncheckedReplaceAllUsesWith(New);
114     OldC->destroyConstant();    // This constant is now dead, destroy it.
115   }
116 };
117
118 template<>
119 struct ConvertConstantType<ConstantVector, VectorType> {
120   static void convert(ConstantVector *OldC, const VectorType *NewTy) {
121     // Make everyone now use a constant of the new type...
122     std::vector<Constant*> C;
123     for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
124       C.push_back(cast<Constant>(OldC->getOperand(i)));
125     Constant *New = OldC->getContext().getConstantVector(NewTy, C);
126     assert(New != OldC && "Didn't replace constant??");
127     OldC->uncheckedReplaceAllUsesWith(New);
128     OldC->destroyConstant();    // This constant is now dead, destroy it.
129   }
130 };
131 }
132   
133 template<class ValType, class TypeClass, class ConstantClass,
134          bool HasLargeKey  /*true for arrays and structs*/ >
135 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
136 public:
137   typedef std::pair<const Type*, ValType> MapKey;
138   typedef std::map<MapKey, Constant *> MapTy;
139   typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
140   typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
141 private:
142   /// Map - This is the main map from the element descriptor to the Constants.
143   /// This is the primary way we avoid creating two of the same shape
144   /// constant.
145   MapTy Map;
146     
147   /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
148   /// from the constants to their element in Map.  This is important for
149   /// removal of constants from the array, which would otherwise have to scan
150   /// through the map with very large keys.
151   InverseMapTy InverseMap;
152
153   /// AbstractTypeMap - Map for abstract type constants.
154   ///
155   AbstractTypeMapTy AbstractTypeMap;
156     
157   /// ValueMapLock - Mutex for this map.
158   sys::SmartMutex<true> ValueMapLock;
159
160 public:
161   // NOTE: This function is not locked.  It is the caller's responsibility
162   // to enforce proper synchronization.
163   typename MapTy::iterator map_end() { return Map.end(); }
164     
165   /// InsertOrGetItem - Return an iterator for the specified element.
166   /// If the element exists in the map, the returned iterator points to the
167   /// entry and Exists=true.  If not, the iterator points to the newly
168   /// inserted entry and returns Exists=false.  Newly inserted entries have
169   /// I->second == 0, and should be filled in.
170   /// NOTE: This function is not locked.  It is the caller's responsibility
171   // to enforce proper synchronization.
172   typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
173                                  &InsertVal,
174                                  bool &Exists) {
175     std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
176     Exists = !IP.second;
177     return IP.first;
178   }
179     
180 private:
181   typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
182     if (HasLargeKey) {
183       typename InverseMapTy::iterator IMI = InverseMap.find(CP);
184       assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
185              IMI->second->second == CP &&
186              "InverseMap corrupt!");
187       return IMI->second;
188     }
189       
190     typename MapTy::iterator I =
191       Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
192                       getValType(CP)));
193     if (I == Map.end() || I->second != CP) {
194       // FIXME: This should not use a linear scan.  If this gets to be a
195       // performance problem, someone should look at this.
196       for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
197         /* empty */;
198     }
199     return I;
200   }
201     
202   ConstantClass* Create(const TypeClass *Ty, const ValType &V,
203                         typename MapTy::iterator I) {
204     ConstantClass* Result =
205       ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
206
207     assert(Result->getType() == Ty && "Type specified is not correct!");
208     I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
209
210     if (HasLargeKey)  // Remember the reverse mapping if needed.
211       InverseMap.insert(std::make_pair(Result, I));
212
213     // If the type of the constant is abstract, make sure that an entry
214     // exists for it in the AbstractTypeMap.
215     if (Ty->isAbstract()) {
216       typename AbstractTypeMapTy::iterator TI = 
217                                                AbstractTypeMap.find(Ty);
218
219       if (TI == AbstractTypeMap.end()) {
220         // Add ourselves to the ATU list of the type.
221         cast<DerivedType>(Ty)->addAbstractTypeUser(this);
222
223         AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
224       }
225     }
226       
227     return Result;
228   }
229 public:
230     
231   /// getOrCreate - Return the specified constant from the map, creating it if
232   /// necessary.
233   ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
234     sys::SmartScopedLock<true> Lock(ValueMapLock);
235     MapKey Lookup(Ty, V);
236     ConstantClass* Result = 0;
237     
238     typename MapTy::iterator I = Map.find(Lookup);
239     // Is it in the map?  
240     if (I != Map.end())
241       Result = static_cast<ConstantClass *>(I->second);
242         
243     if (!Result) {
244       // If no preexisting value, create one now...
245       Result = Create(Ty, V, I);
246     }
247         
248     return Result;
249   }
250
251   void remove(ConstantClass *CP) {
252     sys::SmartScopedLock<true> Lock(ValueMapLock);
253     typename MapTy::iterator I = FindExistingElement(CP);
254     assert(I != Map.end() && "Constant not found in constant table!");
255     assert(I->second == CP && "Didn't find correct element?");
256
257     if (HasLargeKey)  // Remember the reverse mapping if needed.
258       InverseMap.erase(CP);
259       
260     // Now that we found the entry, make sure this isn't the entry that
261     // the AbstractTypeMap points to.
262     const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
263     if (Ty->isAbstract()) {
264       assert(AbstractTypeMap.count(Ty) &&
265              "Abstract type not in AbstractTypeMap?");
266       typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
267       if (ATMEntryIt == I) {
268         // Yes, we are removing the representative entry for this type.
269         // See if there are any other entries of the same type.
270         typename MapTy::iterator TmpIt = ATMEntryIt;
271
272         // First check the entry before this one...
273         if (TmpIt != Map.begin()) {
274           --TmpIt;
275           if (TmpIt->first.first != Ty) // Not the same type, move back...
276             ++TmpIt;
277         }
278
279         // If we didn't find the same type, try to move forward...
280         if (TmpIt == ATMEntryIt) {
281           ++TmpIt;
282           if (TmpIt == Map.end() || TmpIt->first.first != Ty)
283             --TmpIt;   // No entry afterwards with the same type
284         }
285
286         // If there is another entry in the map of the same abstract type,
287         // update the AbstractTypeMap entry now.
288         if (TmpIt != ATMEntryIt) {
289           ATMEntryIt = TmpIt;
290         } else {
291           // Otherwise, we are removing the last instance of this type
292           // from the table.  Remove from the ATM, and from user list.
293           cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
294           AbstractTypeMap.erase(Ty);
295         }
296       }
297     }
298
299     Map.erase(I);
300   }
301
302     
303   /// MoveConstantToNewSlot - If we are about to change C to be the element
304   /// specified by I, update our internal data structures to reflect this
305   /// fact.
306   /// NOTE: This function is not locked. It is the responsibility of the
307   /// caller to enforce proper synchronization if using this method.
308   void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
309     // First, remove the old location of the specified constant in the map.
310     typename MapTy::iterator OldI = FindExistingElement(C);
311     assert(OldI != Map.end() && "Constant not found in constant table!");
312     assert(OldI->second == C && "Didn't find correct element?");
313       
314     // If this constant is the representative element for its abstract type,
315     // update the AbstractTypeMap so that the representative element is I.
316     if (C->getType()->isAbstract()) {
317       typename AbstractTypeMapTy::iterator ATI =
318           AbstractTypeMap.find(C->getType());
319       assert(ATI != AbstractTypeMap.end() &&
320              "Abstract type not in AbstractTypeMap?");
321       if (ATI->second == OldI)
322         ATI->second = I;
323     }
324       
325     // Remove the old entry from the map.
326     Map.erase(OldI);
327     
328     // Update the inverse map so that we know that this constant is now
329     // located at descriptor I.
330     if (HasLargeKey) {
331       assert(I->second == C && "Bad inversemap entry!");
332       InverseMap[C] = I;
333     }
334   }
335     
336   void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
337     sys::SmartScopedLock<true> Lock(ValueMapLock);
338     typename AbstractTypeMapTy::iterator I =
339       AbstractTypeMap.find(cast<Type>(OldTy));
340
341     assert(I != AbstractTypeMap.end() &&
342            "Abstract type not in AbstractTypeMap?");
343
344     // Convert a constant at a time until the last one is gone.  The last one
345     // leaving will remove() itself, causing the AbstractTypeMapEntry to be
346     // eliminated eventually.
347     do {
348       ConvertConstantType<ConstantClass,
349                           TypeClass>::convert(
350                               static_cast<ConstantClass *>(I->second->second),
351                                               cast<TypeClass>(NewTy));
352
353       I = AbstractTypeMap.find(cast<Type>(OldTy));
354     } while (I != AbstractTypeMap.end());
355   }
356
357   // If the type became concrete without being refined to any other existing
358   // type, we just remove ourselves from the ATU list.
359   void typeBecameConcrete(const DerivedType *AbsTy) {
360     AbsTy->removeAbstractTypeUser(this);
361   }
362
363   void dump() const {
364     DOUT << "Constant.cpp: ValueMap\n";
365   }
366 };
367
368 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) :
369     Context(C), TheTrueVal(0), TheFalseVal(0) {
370   AggZeroConstants = new ValueMap<char, Type, ConstantAggregateZero>();
371   ArrayConstants = new ArrayConstantsTy();
372   StructConstants = new StructConstantsTy();
373   VectorConstants = new VectorConstantsTy();
374 }
375
376 LLVMContextImpl::~LLVMContextImpl() {
377   delete AggZeroConstants;
378   delete ArrayConstants;
379   delete StructConstants;
380   delete VectorConstants;
381 }
382
383 // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap 
384 // as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
385 // operator== and operator!= to ensure that the DenseMap doesn't attempt to
386 // compare APInt's of different widths, which would violate an APInt class
387 // invariant which generates an assertion.
388 ConstantInt *LLVMContextImpl::getConstantInt(const APInt& V) {
389   // Get the corresponding integer type for the bit width of the value.
390   const IntegerType *ITy = Context.getIntegerType(V.getBitWidth());
391   // get an existing value or the insertion position
392   DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
393   
394   ConstantsLock.reader_acquire();
395   ConstantInt *&Slot = IntConstants[Key]; 
396   ConstantsLock.reader_release();
397     
398   if (!Slot) {
399     sys::SmartScopedWriter<true> Writer(ConstantsLock);
400     ConstantInt *&NewSlot = IntConstants[Key]; 
401     if (!Slot) {
402       NewSlot = new ConstantInt(ITy, V);
403     }
404     
405     return NewSlot;
406   } else {
407     return Slot;
408   }
409 }
410
411 ConstantFP *LLVMContextImpl::getConstantFP(const APFloat &V) {
412   DenseMapAPFloatKeyInfo::KeyTy Key(V);
413   
414   ConstantsLock.reader_acquire();
415   ConstantFP *&Slot = FPConstants[Key];
416   ConstantsLock.reader_release();
417     
418   if (!Slot) {
419     sys::SmartScopedWriter<true> Writer(ConstantsLock);
420     ConstantFP *&NewSlot = FPConstants[Key];
421     if (!NewSlot) {
422       const Type *Ty;
423       if (&V.getSemantics() == &APFloat::IEEEsingle)
424         Ty = Type::FloatTy;
425       else if (&V.getSemantics() == &APFloat::IEEEdouble)
426         Ty = Type::DoubleTy;
427       else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
428         Ty = Type::X86_FP80Ty;
429       else if (&V.getSemantics() == &APFloat::IEEEquad)
430         Ty = Type::FP128Ty;
431       else {
432         assert(&V.getSemantics() == &APFloat::PPCDoubleDouble && 
433                "Unknown FP format");
434         Ty = Type::PPC_FP128Ty;
435       }
436       NewSlot = new ConstantFP(Ty, V);
437     }
438     
439     return NewSlot;
440   }
441   
442   return Slot;
443 }
444
445 MDString *LLVMContextImpl::getMDString(const char *StrBegin,
446                                        unsigned StrLength) {
447   sys::SmartScopedWriter<true> Writer(ConstantsLock);
448   StringMapEntry<MDString *> &Entry = 
449     MDStringCache.GetOrCreateValue(StringRef(StrBegin, StrLength));
450   MDString *&S = Entry.getValue();
451   if (!S) S = new MDString(Entry.getKeyData(),
452                            Entry.getKeyLength());
453
454   return S;
455 }
456
457 MDNode *LLVMContextImpl::getMDNode(Value*const* Vals, unsigned NumVals) {
458   FoldingSetNodeID ID;
459   for (unsigned i = 0; i != NumVals; ++i)
460     ID.AddPointer(Vals[i]);
461
462   ConstantsLock.reader_acquire();
463   void *InsertPoint;
464   MDNode *N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
465   ConstantsLock.reader_release();
466   
467   if (!N) {
468     sys::SmartScopedWriter<true> Writer(ConstantsLock);
469     N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
470     if (!N) {
471       // InsertPoint will have been set by the FindNodeOrInsertPos call.
472       N = new MDNode(Vals, NumVals);
473       MDNodeSet.InsertNode(N, InsertPoint);
474     }
475   }
476
477   return N;
478 }
479
480 ConstantAggregateZero*
481 LLVMContextImpl::getConstantAggregateZero(const Type *Ty) {
482   assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
483          "Cannot create an aggregate zero of non-aggregate type!");
484
485   // Implicitly locked.
486   return AggZeroConstants->getOrCreate(Ty, 0);
487 }
488
489 Constant *LLVMContextImpl::getConstantArray(const ArrayType *Ty,
490                              const std::vector<Constant*> &V) {
491   // If this is an all-zero array, return a ConstantAggregateZero object
492   if (!V.empty()) {
493     Constant *C = V[0];
494     if (!C->isNullValue()) {
495       // Implicitly locked.
496       return ArrayConstants->getOrCreate(Ty, V);
497     }
498     for (unsigned i = 1, e = V.size(); i != e; ++i)
499       if (V[i] != C) {
500         // Implicitly locked.
501         return ArrayConstants->getOrCreate(Ty, V);
502       }
503   }
504   
505   return Context.getConstantAggregateZero(Ty);
506 }
507
508 Constant *LLVMContextImpl::getConstantStruct(const StructType *Ty,
509                               const std::vector<Constant*> &V) {
510   // Create a ConstantAggregateZero value if all elements are zeros...
511   for (unsigned i = 0, e = V.size(); i != e; ++i)
512     if (!V[i]->isNullValue())
513       // Implicitly locked.
514       return StructConstants->getOrCreate(Ty, V);
515
516   return Context.getConstantAggregateZero(Ty);
517 }
518
519 Constant *LLVMContextImpl::getConstantVector(const VectorType *Ty,
520                               const std::vector<Constant*> &V) {
521   assert(!V.empty() && "Vectors can't be empty");
522   // If this is an all-undef or alll-zero vector, return a
523   // ConstantAggregateZero or UndefValue.
524   Constant *C = V[0];
525   bool isZero = C->isNullValue();
526   bool isUndef = isa<UndefValue>(C);
527
528   if (isZero || isUndef) {
529     for (unsigned i = 1, e = V.size(); i != e; ++i)
530       if (V[i] != C) {
531         isZero = isUndef = false;
532         break;
533       }
534   }
535   
536   if (isZero)
537     return Context.getConstantAggregateZero(Ty);
538   if (isUndef)
539     return Context.getUndef(Ty);
540     
541   // Implicitly locked.
542   return VectorConstants->getOrCreate(Ty, V);
543 }
544
545 // *** erase methods ***
546
547 void LLVMContextImpl::erase(MDString *M) {
548   sys::SmartScopedWriter<true> Writer(ConstantsLock);
549   MDStringCache.erase(MDStringCache.find(StringRef(M->StrBegin, 
550                                                    M->length())));
551 }
552
553 void LLVMContextImpl::erase(MDNode *M) {
554   sys::SmartScopedWriter<true> Writer(ConstantsLock);
555   MDNodeSet.RemoveNode(M);
556 }
557
558 void LLVMContextImpl::erase(ConstantAggregateZero *Z) {
559   AggZeroConstants->remove(Z);
560 }
561
562 void LLVMContextImpl::erase(ConstantArray *C) {
563   ArrayConstants->remove(C);
564 }
565
566 void LLVMContextImpl::erase(ConstantStruct *S) {
567   StructConstants->remove(S);
568 }
569
570 void LLVMContextImpl::erase(ConstantVector *V) {
571   VectorConstants->remove(V);
572 }
573
574 // *** RAUW helpers ***
575
576 Constant *LLVMContextImpl::replaceUsesOfWithOnConstant(ConstantArray *CA,
577                                                Value *From, Value *To, Use *U) {
578   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
579   Constant *ToC = cast<Constant>(To);
580
581   std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
582   Lookup.first.first = CA->getType();
583   Lookup.second = CA;
584
585   std::vector<Constant*> &Values = Lookup.first.second;
586   Values.reserve(CA->getNumOperands());  // Build replacement array.
587
588   // Fill values with the modified operands of the constant array.  Also, 
589   // compute whether this turns into an all-zeros array.
590   bool isAllZeros = false;
591   unsigned NumUpdated = 0;
592   if (!ToC->isNullValue()) {
593     for (Use *O = CA->OperandList, *E = CA->OperandList + CA->getNumOperands();
594          O != E; ++O) {
595       Constant *Val = cast<Constant>(O->get());
596       if (Val == From) {
597         Val = ToC;
598         ++NumUpdated;
599       }
600       Values.push_back(Val);
601     }
602   } else {
603     isAllZeros = true;
604     for (Use *O = CA->OperandList, *E = CA->OperandList + CA->getNumOperands();
605          O != E; ++O) {
606       Constant *Val = cast<Constant>(O->get());
607       if (Val == From) {
608         Val = ToC;
609         ++NumUpdated;
610       }
611       Values.push_back(Val);
612       if (isAllZeros) isAllZeros = Val->isNullValue();
613     }
614   }
615   
616   Constant *Replacement = 0;
617   if (isAllZeros) {
618     Replacement = Context.getConstantAggregateZero(CA->getType());
619   } else {
620     // Check to see if we have this array type already.
621     sys::SmartScopedWriter<true> Writer(ConstantsLock);
622     bool Exists;
623     ArrayConstantsTy::MapTy::iterator I =
624       ArrayConstants->InsertOrGetItem(Lookup, Exists);
625     
626     if (Exists) {
627       Replacement = I->second;
628     } else {
629       // Okay, the new shape doesn't exist in the system yet.  Instead of
630       // creating a new constant array, inserting it, replaceallusesof'ing the
631       // old with the new, then deleting the old... just update the current one
632       // in place!
633       ArrayConstants->MoveConstantToNewSlot(CA, I);
634       
635       // Update to the new value.  Optimize for the case when we have a single
636       // operand that we're changing, but handle bulk updates efficiently.
637       if (NumUpdated == 1) {
638         unsigned OperandToUpdate = U - CA->OperandList;
639         assert(CA->getOperand(OperandToUpdate) == From &&
640                "ReplaceAllUsesWith broken!");
641         CA->setOperand(OperandToUpdate, ToC);
642       } else {
643         for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
644           if (CA->getOperand(i) == From)
645             CA->setOperand(i, ToC);
646       }
647       return 0;
648     }
649   }
650   
651   return Replacement;
652 }
653
654 Constant *LLVMContextImpl::replaceUsesOfWithOnConstant(ConstantStruct *CS,
655                                                Value *From, Value *To, Use *U) {
656   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
657   Constant *ToC = cast<Constant>(To);
658
659   unsigned OperandToUpdate = U - CS->OperandList;
660   assert(CS->getOperand(OperandToUpdate) == From &&
661          "ReplaceAllUsesWith broken!");
662
663   std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
664   Lookup.first.first = CS->getType();
665   Lookup.second = CS;
666   std::vector<Constant*> &Values = Lookup.first.second;
667   Values.reserve(CS->getNumOperands());  // Build replacement struct.
668   
669   
670   // Fill values with the modified operands of the constant struct.  Also, 
671   // compute whether this turns into an all-zeros struct.
672   bool isAllZeros = false;
673   if (!ToC->isNullValue()) {
674     for (Use *O = CS->OperandList, *E = CS->OperandList + CS->getNumOperands(); 
675          O != E; ++O)
676       Values.push_back(cast<Constant>(O->get()));
677   } else {
678     isAllZeros = true;
679     for (Use *O = CS->OperandList, *E = CS->OperandList + CS->getNumOperands(); 
680          O != E; ++O) {
681       Constant *Val = cast<Constant>(O->get());
682       Values.push_back(Val);
683       if (isAllZeros) isAllZeros = Val->isNullValue();
684     }
685   }
686   Values[OperandToUpdate] = ToC;
687   
688   Constant *Replacement = 0;
689   if (isAllZeros) {
690     Replacement = Context.getConstantAggregateZero(CS->getType());
691   } else {
692     // Check to see if we have this array type already.
693     sys::SmartScopedWriter<true> Writer(ConstantsLock);
694     bool Exists;
695     StructConstantsTy::MapTy::iterator I =
696       StructConstants->InsertOrGetItem(Lookup, Exists);
697     
698     if (Exists) {
699       Replacement = I->second;
700     } else {
701       // Okay, the new shape doesn't exist in the system yet.  Instead of
702       // creating a new constant struct, inserting it, replaceallusesof'ing the
703       // old with the new, then deleting the old... just update the current one
704       // in place!
705       StructConstants->MoveConstantToNewSlot(CS, I);
706       
707       // Update to the new value.
708       CS->setOperand(OperandToUpdate, ToC);
709       return 0;
710     }
711   }
712   
713   assert(Replacement != CS && "I didn't contain From!");
714   
715   return Replacement;
716 }