1 //===-- ConstantsContext.h - Constants-related Context Interals -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines various helper methods and classes used by
11 // LLVMContextImpl for creating and managing constants.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CONSTANTSCONTEXT_H
16 #define LLVM_CONSTANTSCONTEXT_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/InlineAsm.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Operator.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
28 template<class ValType>
29 struct ConstantTraits;
31 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
32 /// behind the scenes to implement unary constant exprs.
33 class UnaryConstantExpr : public ConstantExpr {
34 virtual void anchor();
35 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
37 // allocate space for exactly one operand
38 void *operator new(size_t s) {
39 return User::operator new(s, 1);
41 UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
42 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
45 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
48 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
49 /// behind the scenes to implement binary constant exprs.
50 class BinaryConstantExpr : public ConstantExpr {
51 virtual void anchor();
52 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
54 // allocate space for exactly two operands
55 void *operator new(size_t s) {
56 return User::operator new(s, 2);
58 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
60 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
63 SubclassOptionalData = Flags;
65 /// Transparently provide more efficient getOperand methods.
66 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
69 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
70 /// behind the scenes to implement select constant exprs.
71 class SelectConstantExpr : public ConstantExpr {
72 virtual void anchor();
73 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
75 // allocate space for exactly three operands
76 void *operator new(size_t s) {
77 return User::operator new(s, 3);
79 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
80 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
85 /// Transparently provide more efficient getOperand methods.
86 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
89 /// ExtractElementConstantExpr - This class is private to
90 /// Constants.cpp, and is used behind the scenes to implement
91 /// extractelement constant exprs.
92 class ExtractElementConstantExpr : public ConstantExpr {
93 virtual void anchor();
94 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
96 // allocate space for exactly two operands
97 void *operator new(size_t s) {
98 return User::operator new(s, 2);
100 ExtractElementConstantExpr(Constant *C1, Constant *C2)
101 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
102 Instruction::ExtractElement, &Op<0>(), 2) {
106 /// Transparently provide more efficient getOperand methods.
107 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
110 /// InsertElementConstantExpr - This class is private to
111 /// Constants.cpp, and is used behind the scenes to implement
112 /// insertelement constant exprs.
113 class InsertElementConstantExpr : public ConstantExpr {
114 virtual void anchor();
115 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
117 // allocate space for exactly three operands
118 void *operator new(size_t s) {
119 return User::operator new(s, 3);
121 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
122 : ConstantExpr(C1->getType(), Instruction::InsertElement,
128 /// Transparently provide more efficient getOperand methods.
129 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
132 /// ShuffleVectorConstantExpr - This class is private to
133 /// Constants.cpp, and is used behind the scenes to implement
134 /// shufflevector constant exprs.
135 class ShuffleVectorConstantExpr : public ConstantExpr {
136 virtual void anchor();
137 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
139 // allocate space for exactly three operands
140 void *operator new(size_t s) {
141 return User::operator new(s, 3);
143 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
144 : ConstantExpr(VectorType::get(
145 cast<VectorType>(C1->getType())->getElementType(),
146 cast<VectorType>(C3->getType())->getNumElements()),
147 Instruction::ShuffleVector,
153 /// Transparently provide more efficient getOperand methods.
154 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
157 /// ExtractValueConstantExpr - This class is private to
158 /// Constants.cpp, and is used behind the scenes to implement
159 /// extractvalue constant exprs.
160 class ExtractValueConstantExpr : public ConstantExpr {
161 virtual void anchor();
162 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
164 // allocate space for exactly one operand
165 void *operator new(size_t s) {
166 return User::operator new(s, 1);
168 ExtractValueConstantExpr(Constant *Agg,
169 const SmallVector<unsigned, 4> &IdxList,
171 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
176 /// Indices - These identify which value to extract.
177 const SmallVector<unsigned, 4> Indices;
179 /// Transparently provide more efficient getOperand methods.
180 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
183 /// InsertValueConstantExpr - This class is private to
184 /// Constants.cpp, and is used behind the scenes to implement
185 /// insertvalue constant exprs.
186 class InsertValueConstantExpr : public ConstantExpr {
187 virtual void anchor();
188 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
190 // allocate space for exactly one operand
191 void *operator new(size_t s) {
192 return User::operator new(s, 2);
194 InsertValueConstantExpr(Constant *Agg, Constant *Val,
195 const SmallVector<unsigned, 4> &IdxList,
197 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
203 /// Indices - These identify the position for the insertion.
204 const SmallVector<unsigned, 4> Indices;
206 /// Transparently provide more efficient getOperand methods.
207 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
211 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
212 /// used behind the scenes to implement getelementpr constant exprs.
213 class GetElementPtrConstantExpr : public ConstantExpr {
214 virtual void anchor();
215 GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
218 static GetElementPtrConstantExpr *Create(Constant *C,
219 ArrayRef<Constant*> IdxList,
222 GetElementPtrConstantExpr *Result =
223 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
224 Result->SubclassOptionalData = Flags;
227 /// Transparently provide more efficient getOperand methods.
228 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
231 // CompareConstantExpr - This class is private to Constants.cpp, and is used
232 // behind the scenes to implement ICmp and FCmp constant expressions. This is
233 // needed in order to store the predicate value for these instructions.
234 class CompareConstantExpr : public ConstantExpr {
235 virtual void anchor();
236 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
238 // allocate space for exactly two operands
239 void *operator new(size_t s) {
240 return User::operator new(s, 2);
242 unsigned short predicate;
243 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
244 unsigned short pred, Constant* LHS, Constant* RHS)
245 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
249 /// Transparently provide more efficient getOperand methods.
250 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
254 struct OperandTraits<UnaryConstantExpr> :
255 public FixedNumOperandTraits<UnaryConstantExpr, 1> {
257 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
260 struct OperandTraits<BinaryConstantExpr> :
261 public FixedNumOperandTraits<BinaryConstantExpr, 2> {
263 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
266 struct OperandTraits<SelectConstantExpr> :
267 public FixedNumOperandTraits<SelectConstantExpr, 3> {
269 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
272 struct OperandTraits<ExtractElementConstantExpr> :
273 public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
275 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
278 struct OperandTraits<InsertElementConstantExpr> :
279 public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
281 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
284 struct OperandTraits<ShuffleVectorConstantExpr> :
285 public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
287 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
290 struct OperandTraits<ExtractValueConstantExpr> :
291 public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
293 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
296 struct OperandTraits<InsertValueConstantExpr> :
297 public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
299 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
302 struct OperandTraits<GetElementPtrConstantExpr> :
303 public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
306 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
310 struct OperandTraits<CompareConstantExpr> :
311 public FixedNumOperandTraits<CompareConstantExpr, 2> {
313 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
315 struct ExprMapKeyType {
316 ExprMapKeyType(unsigned opc,
317 ArrayRef<Constant*> ops,
318 unsigned short flags = 0,
319 unsigned short optionalflags = 0,
320 ArrayRef<unsigned> inds = ArrayRef<unsigned>())
321 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
322 operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {}
324 uint8_t subclassoptionaldata;
325 uint16_t subclassdata;
326 std::vector<Constant*> operands;
327 SmallVector<unsigned, 4> indices;
328 bool operator==(const ExprMapKeyType& that) const {
329 return this->opcode == that.opcode &&
330 this->subclassdata == that.subclassdata &&
331 this->subclassoptionaldata == that.subclassoptionaldata &&
332 this->operands == that.operands &&
333 this->indices == that.indices;
335 bool operator<(const ExprMapKeyType & that) const {
336 if (this->opcode != that.opcode) return this->opcode < that.opcode;
337 if (this->operands != that.operands) return this->operands < that.operands;
338 if (this->subclassdata != that.subclassdata)
339 return this->subclassdata < that.subclassdata;
340 if (this->subclassoptionaldata != that.subclassoptionaldata)
341 return this->subclassoptionaldata < that.subclassoptionaldata;
342 if (this->indices != that.indices) return this->indices < that.indices;
346 bool operator!=(const ExprMapKeyType& that) const {
347 return !(*this == that);
351 struct InlineAsmKeyType {
352 InlineAsmKeyType(StringRef AsmString,
353 StringRef Constraints, bool hasSideEffects,
355 : asm_string(AsmString), constraints(Constraints),
356 has_side_effects(hasSideEffects), is_align_stack(isAlignStack) {}
357 std::string asm_string;
358 std::string constraints;
359 bool has_side_effects;
361 bool operator==(const InlineAsmKeyType& that) const {
362 return this->asm_string == that.asm_string &&
363 this->constraints == that.constraints &&
364 this->has_side_effects == that.has_side_effects &&
365 this->is_align_stack == that.is_align_stack;
367 bool operator<(const InlineAsmKeyType& that) const {
368 if (this->asm_string != that.asm_string)
369 return this->asm_string < that.asm_string;
370 if (this->constraints != that.constraints)
371 return this->constraints < that.constraints;
372 if (this->has_side_effects != that.has_side_effects)
373 return this->has_side_effects < that.has_side_effects;
374 if (this->is_align_stack != that.is_align_stack)
375 return this->is_align_stack < that.is_align_stack;
379 bool operator!=(const InlineAsmKeyType& that) const {
380 return !(*this == that);
384 // The number of operands for each ConstantCreator::create method is
385 // determined by the ConstantTraits template.
386 // ConstantCreator - A class that is used to create constants by
387 // ConstantUniqueMap*. This class should be partially specialized if there is
388 // something strange that needs to be done to interface to the ctor for the
391 template<typename T, typename Alloc>
392 struct ConstantTraits< std::vector<T, Alloc> > {
393 static unsigned uses(const std::vector<T, Alloc>& v) {
399 struct ConstantTraits<Constant *> {
400 static unsigned uses(Constant * const & v) {
405 template<class ConstantClass, class TypeClass, class ValType>
406 struct ConstantCreator {
407 static ConstantClass *create(TypeClass *Ty, const ValType &V) {
408 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
412 template<class ConstantClass, class TypeClass>
413 struct ConstantArrayCreator {
414 static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) {
415 return new(V.size()) ConstantClass(Ty, V);
419 template<class ConstantClass>
420 struct ConstantKeyData {
421 typedef void ValType;
422 static ValType getValType(ConstantClass *C) {
423 llvm_unreachable("Unknown Constant type!");
428 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
429 static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V,
430 unsigned short pred = 0) {
431 if (Instruction::isCast(V.opcode))
432 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
433 if ((V.opcode >= Instruction::BinaryOpsBegin &&
434 V.opcode < Instruction::BinaryOpsEnd))
435 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
436 V.subclassoptionaldata);
437 if (V.opcode == Instruction::Select)
438 return new SelectConstantExpr(V.operands[0], V.operands[1],
440 if (V.opcode == Instruction::ExtractElement)
441 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
442 if (V.opcode == Instruction::InsertElement)
443 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
445 if (V.opcode == Instruction::ShuffleVector)
446 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
448 if (V.opcode == Instruction::InsertValue)
449 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
451 if (V.opcode == Instruction::ExtractValue)
452 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
453 if (V.opcode == Instruction::GetElementPtr) {
454 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
455 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
456 V.subclassoptionaldata);
459 // The compare instructions are weird. We have to encode the predicate
460 // value and it is combined with the instruction opcode by multiplying
461 // the opcode by one hundred. We must decode this to get the predicate.
462 if (V.opcode == Instruction::ICmp)
463 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
464 V.operands[0], V.operands[1]);
465 if (V.opcode == Instruction::FCmp)
466 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
467 V.operands[0], V.operands[1]);
468 llvm_unreachable("Invalid ConstantExpr!");
473 struct ConstantKeyData<ConstantExpr> {
474 typedef ExprMapKeyType ValType;
475 static ValType getValType(ConstantExpr *CE) {
476 std::vector<Constant*> Operands;
477 Operands.reserve(CE->getNumOperands());
478 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
479 Operands.push_back(cast<Constant>(CE->getOperand(i)));
480 return ExprMapKeyType(CE->getOpcode(), Operands,
481 CE->isCompare() ? CE->getPredicate() : 0,
482 CE->getRawSubclassOptionalData(),
484 CE->getIndices() : ArrayRef<unsigned>());
489 struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
490 static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
491 return new InlineAsm(Ty, Key.asm_string, Key.constraints,
492 Key.has_side_effects, Key.is_align_stack);
497 struct ConstantKeyData<InlineAsm> {
498 typedef InlineAsmKeyType ValType;
499 static ValType getValType(InlineAsm *Asm) {
500 return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
501 Asm->hasSideEffects(), Asm->isAlignStack());
505 template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
506 bool HasLargeKey = false /*true for arrays and structs*/ >
507 class ConstantUniqueMap {
509 typedef std::pair<TypeClass*, ValType> MapKey;
510 typedef std::map<MapKey, ConstantClass *> MapTy;
511 typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy;
513 /// Map - This is the main map from the element descriptor to the Constants.
514 /// This is the primary way we avoid creating two of the same shape
518 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
519 /// from the constants to their element in Map. This is important for
520 /// removal of constants from the array, which would otherwise have to scan
521 /// through the map with very large keys.
522 InverseMapTy InverseMap;
525 typename MapTy::iterator map_begin() { return Map.begin(); }
526 typename MapTy::iterator map_end() { return Map.end(); }
528 void freeConstants() {
529 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
531 // Asserts that use_empty().
536 /// InsertOrGetItem - Return an iterator for the specified element.
537 /// If the element exists in the map, the returned iterator points to the
538 /// entry and Exists=true. If not, the iterator points to the newly
539 /// inserted entry and returns Exists=false. Newly inserted entries have
540 /// I->second == 0, and should be filled in.
541 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
544 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
550 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
552 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
553 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
554 IMI->second->second == CP &&
555 "InverseMap corrupt!");
559 typename MapTy::iterator I =
560 Map.find(MapKey(static_cast<TypeClass*>(CP->getType()),
561 ConstantKeyData<ConstantClass>::getValType(CP)));
562 if (I == Map.end() || I->second != CP) {
563 // FIXME: This should not use a linear scan. If this gets to be a
564 // performance problem, someone should look at this.
565 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
571 ConstantClass *Create(TypeClass *Ty, ValRefType V,
572 typename MapTy::iterator I) {
573 ConstantClass* Result =
574 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
576 assert(Result->getType() == Ty && "Type specified is not correct!");
577 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
579 if (HasLargeKey) // Remember the reverse mapping if needed.
580 InverseMap.insert(std::make_pair(Result, I));
586 /// getOrCreate - Return the specified constant from the map, creating it if
588 ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) {
589 MapKey Lookup(Ty, V);
590 ConstantClass* Result = 0;
592 typename MapTy::iterator I = Map.find(Lookup);
598 // If no preexisting value, create one now...
599 Result = Create(Ty, V, I);
605 void remove(ConstantClass *CP) {
606 typename MapTy::iterator I = FindExistingElement(CP);
607 assert(I != Map.end() && "Constant not found in constant table!");
608 assert(I->second == CP && "Didn't find correct element?");
610 if (HasLargeKey) // Remember the reverse mapping if needed.
611 InverseMap.erase(CP);
616 /// MoveConstantToNewSlot - If we are about to change C to be the element
617 /// specified by I, update our internal data structures to reflect this
619 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
620 // First, remove the old location of the specified constant in the map.
621 typename MapTy::iterator OldI = FindExistingElement(C);
622 assert(OldI != Map.end() && "Constant not found in constant table!");
623 assert(OldI->second == C && "Didn't find correct element?");
625 // Remove the old entry from the map.
628 // Update the inverse map so that we know that this constant is now
629 // located at descriptor I.
631 assert(I->second == C && "Bad inversemap entry!");
637 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
641 // Unique map for aggregate constants
642 template<class TypeClass, class ConstantClass>
643 class ConstantAggrUniqueMap {
645 typedef ArrayRef<Constant*> Operands;
646 typedef std::pair<TypeClass*, Operands> LookupKey;
649 typedef DenseMapInfo<ConstantClass*> ConstantClassInfo;
650 typedef DenseMapInfo<Constant*> ConstantInfo;
651 typedef DenseMapInfo<TypeClass*> TypeClassInfo;
652 static inline ConstantClass* getEmptyKey() {
653 return ConstantClassInfo::getEmptyKey();
655 static inline ConstantClass* getTombstoneKey() {
656 return ConstantClassInfo::getTombstoneKey();
658 static unsigned getHashValue(const ConstantClass *CP) {
659 // This is adapted from SuperFastHash by Paul Hsieh.
660 unsigned Hash = TypeClassInfo::getHashValue(CP->getType());
661 for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I) {
662 unsigned Data = ConstantInfo::getHashValue(CP->getOperand(I));
663 Hash += Data & 0xFFFF;
664 unsigned Tmp = ((Data >> 16) << 11) ^ Hash;
665 Hash = (Hash << 16) ^ Tmp;
669 // Force "avalanching" of final 127 bits.
678 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
681 static unsigned getHashValue(const LookupKey &Val) {
682 // This is adapted from SuperFastHash by Paul Hsieh.
683 unsigned Hash = TypeClassInfo::getHashValue(Val.first);
684 for (Operands::const_iterator
685 I = Val.second.begin(), E = Val.second.end(); I != E; ++I) {
686 unsigned Data = ConstantInfo::getHashValue(*I);
687 Hash += Data & 0xFFFF;
688 unsigned Tmp = ((Data >> 16) << 11) ^ Hash;
689 Hash = (Hash << 16) ^ Tmp;
693 // Force "avalanching" of final 127 bits.
702 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
703 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
705 if (LHS.first != RHS->getType()
706 || LHS.second.size() != RHS->getNumOperands())
708 for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) {
709 if (LHS.second[I] != RHS->getOperand(I))
716 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
719 /// Map - This is the main map from the element descriptor to the Constants.
720 /// This is the primary way we avoid creating two of the same shape
725 typename MapTy::iterator map_begin() { return Map.begin(); }
726 typename MapTy::iterator map_end() { return Map.end(); }
728 void freeConstants() {
729 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
731 // Asserts that use_empty().
737 typename MapTy::iterator findExistingElement(ConstantClass *CP) {
741 ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) {
742 ConstantClass* Result =
743 ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V);
745 assert(Result->getType() == Ty && "Type specified is not correct!");
752 /// getOrCreate - Return the specified constant from the map, creating it if
754 ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
755 LookupKey Lookup(Ty, V);
756 ConstantClass* Result = 0;
758 typename MapTy::iterator I = Map.find_as(Lookup);
764 // If no preexisting value, create one now...
765 Result = Create(Ty, V, I);
771 /// Find the constant by lookup key.
772 typename MapTy::iterator find(LookupKey Lookup) {
773 return Map.find_as(Lookup);
776 /// Insert the constant into its proper slot.
777 void insert(ConstantClass *CP) {
781 /// Remove this constant from the map
782 void remove(ConstantClass *CP) {
783 typename MapTy::iterator I = findExistingElement(CP);
784 assert(I != Map.end() && "Constant not found in constant table!");
785 assert(I->first == CP && "Didn't find correct element?");
790 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");