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/ADT/Hashing.h"
20 #include "llvm/IR/InlineAsm.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Operator.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
29 template<class ValType>
30 struct ConstantTraits;
32 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
33 /// behind the scenes to implement unary constant exprs.
34 class UnaryConstantExpr : public ConstantExpr {
35 void anchor() override;
36 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
38 // allocate space for exactly one operand
39 void *operator new(size_t s) {
40 return User::operator new(s, 1);
42 UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
43 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
46 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
49 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
50 /// behind the scenes to implement binary constant exprs.
51 class BinaryConstantExpr : public ConstantExpr {
52 void anchor() override;
53 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
55 // allocate space for exactly two operands
56 void *operator new(size_t s) {
57 return User::operator new(s, 2);
59 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
61 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
64 SubclassOptionalData = Flags;
66 /// Transparently provide more efficient getOperand methods.
67 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
70 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
71 /// behind the scenes to implement select constant exprs.
72 class SelectConstantExpr : public ConstantExpr {
73 void anchor() override;
74 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
76 // allocate space for exactly three operands
77 void *operator new(size_t s) {
78 return User::operator new(s, 3);
80 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
81 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
86 /// Transparently provide more efficient getOperand methods.
87 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
90 /// ExtractElementConstantExpr - This class is private to
91 /// Constants.cpp, and is used behind the scenes to implement
92 /// extractelement constant exprs.
93 class ExtractElementConstantExpr : public ConstantExpr {
94 void anchor() override;
95 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
97 // allocate space for exactly two operands
98 void *operator new(size_t s) {
99 return User::operator new(s, 2);
101 ExtractElementConstantExpr(Constant *C1, Constant *C2)
102 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
103 Instruction::ExtractElement, &Op<0>(), 2) {
107 /// Transparently provide more efficient getOperand methods.
108 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
111 /// InsertElementConstantExpr - This class is private to
112 /// Constants.cpp, and is used behind the scenes to implement
113 /// insertelement constant exprs.
114 class InsertElementConstantExpr : public ConstantExpr {
115 void anchor() override;
116 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
118 // allocate space for exactly three operands
119 void *operator new(size_t s) {
120 return User::operator new(s, 3);
122 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
123 : ConstantExpr(C1->getType(), Instruction::InsertElement,
129 /// Transparently provide more efficient getOperand methods.
130 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
133 /// ShuffleVectorConstantExpr - This class is private to
134 /// Constants.cpp, and is used behind the scenes to implement
135 /// shufflevector constant exprs.
136 class ShuffleVectorConstantExpr : public ConstantExpr {
137 void anchor() override;
138 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
140 // allocate space for exactly three operands
141 void *operator new(size_t s) {
142 return User::operator new(s, 3);
144 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
145 : ConstantExpr(VectorType::get(
146 cast<VectorType>(C1->getType())->getElementType(),
147 cast<VectorType>(C3->getType())->getNumElements()),
148 Instruction::ShuffleVector,
154 /// Transparently provide more efficient getOperand methods.
155 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
158 /// ExtractValueConstantExpr - This class is private to
159 /// Constants.cpp, and is used behind the scenes to implement
160 /// extractvalue constant exprs.
161 class ExtractValueConstantExpr : public ConstantExpr {
162 void anchor() override;
163 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
165 // allocate space for exactly one operand
166 void *operator new(size_t s) {
167 return User::operator new(s, 1);
169 ExtractValueConstantExpr(Constant *Agg,
170 const SmallVector<unsigned, 4> &IdxList,
172 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
177 /// Indices - These identify which value to extract.
178 const SmallVector<unsigned, 4> Indices;
180 /// Transparently provide more efficient getOperand methods.
181 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
184 /// InsertValueConstantExpr - This class is private to
185 /// Constants.cpp, and is used behind the scenes to implement
186 /// insertvalue constant exprs.
187 class InsertValueConstantExpr : public ConstantExpr {
188 void anchor() override;
189 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
191 // allocate space for exactly one operand
192 void *operator new(size_t s) {
193 return User::operator new(s, 2);
195 InsertValueConstantExpr(Constant *Agg, Constant *Val,
196 const SmallVector<unsigned, 4> &IdxList,
198 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
204 /// Indices - These identify the position for the insertion.
205 const SmallVector<unsigned, 4> Indices;
207 /// Transparently provide more efficient getOperand methods.
208 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
212 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
213 /// used behind the scenes to implement getelementpr constant exprs.
214 class GetElementPtrConstantExpr : public ConstantExpr {
215 void anchor() override;
216 GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
219 static GetElementPtrConstantExpr *Create(Constant *C,
220 ArrayRef<Constant*> IdxList,
223 GetElementPtrConstantExpr *Result =
224 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
225 Result->SubclassOptionalData = Flags;
228 /// Transparently provide more efficient getOperand methods.
229 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
232 // CompareConstantExpr - This class is private to Constants.cpp, and is used
233 // behind the scenes to implement ICmp and FCmp constant expressions. This is
234 // needed in order to store the predicate value for these instructions.
235 class CompareConstantExpr : public ConstantExpr {
236 void anchor() override;
237 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
239 // allocate space for exactly two operands
240 void *operator new(size_t s) {
241 return User::operator new(s, 2);
243 unsigned short predicate;
244 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
245 unsigned short pred, Constant* LHS, Constant* RHS)
246 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
250 /// Transparently provide more efficient getOperand methods.
251 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
255 struct OperandTraits<UnaryConstantExpr> :
256 public FixedNumOperandTraits<UnaryConstantExpr, 1> {
258 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
261 struct OperandTraits<BinaryConstantExpr> :
262 public FixedNumOperandTraits<BinaryConstantExpr, 2> {
264 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
267 struct OperandTraits<SelectConstantExpr> :
268 public FixedNumOperandTraits<SelectConstantExpr, 3> {
270 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
273 struct OperandTraits<ExtractElementConstantExpr> :
274 public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
276 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
279 struct OperandTraits<InsertElementConstantExpr> :
280 public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
282 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
285 struct OperandTraits<ShuffleVectorConstantExpr> :
286 public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
288 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
291 struct OperandTraits<ExtractValueConstantExpr> :
292 public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
294 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
297 struct OperandTraits<InsertValueConstantExpr> :
298 public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
300 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
303 struct OperandTraits<GetElementPtrConstantExpr> :
304 public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
307 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
311 struct OperandTraits<CompareConstantExpr> :
312 public FixedNumOperandTraits<CompareConstantExpr, 2> {
314 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
316 struct ExprMapKeyType {
317 ExprMapKeyType(unsigned opc,
318 ArrayRef<Constant*> ops,
319 unsigned short flags = 0,
320 unsigned short optionalflags = 0,
321 ArrayRef<unsigned> inds = None)
322 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
323 operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {}
325 uint8_t subclassoptionaldata;
326 uint16_t subclassdata;
327 std::vector<Constant*> operands;
328 SmallVector<unsigned, 4> indices;
329 bool operator==(const ExprMapKeyType& that) const {
330 return this->opcode == that.opcode &&
331 this->subclassdata == that.subclassdata &&
332 this->subclassoptionaldata == that.subclassoptionaldata &&
333 this->operands == that.operands &&
334 this->indices == that.indices;
336 bool operator<(const ExprMapKeyType & that) const {
337 return std::tie(opcode, operands, subclassdata, subclassoptionaldata,
339 std::tie(that.opcode, that.operands, that.subclassdata,
340 that.subclassoptionaldata, that.indices);
343 bool operator!=(const ExprMapKeyType& that) const {
344 return !(*this == that);
348 struct InlineAsmKeyType {
349 InlineAsmKeyType(StringRef AsmString,
350 StringRef Constraints, bool hasSideEffects,
351 bool isAlignStack, InlineAsm::AsmDialect asmDialect)
352 : asm_string(AsmString), constraints(Constraints),
353 has_side_effects(hasSideEffects), is_align_stack(isAlignStack),
354 asm_dialect(asmDialect) {}
355 std::string asm_string;
356 std::string constraints;
357 bool has_side_effects;
359 InlineAsm::AsmDialect asm_dialect;
360 bool operator==(const InlineAsmKeyType& that) const {
361 return this->asm_string == that.asm_string &&
362 this->constraints == that.constraints &&
363 this->has_side_effects == that.has_side_effects &&
364 this->is_align_stack == that.is_align_stack &&
365 this->asm_dialect == that.asm_dialect;
367 bool operator<(const InlineAsmKeyType& that) const {
368 return std::tie(asm_string, constraints, has_side_effects, is_align_stack,
370 std::tie(that.asm_string, that.constraints, that.has_side_effects,
371 that.is_align_stack, that.asm_dialect);
374 bool operator!=(const InlineAsmKeyType& that) const {
375 return !(*this == that);
379 // The number of operands for each ConstantCreator::create method is
380 // determined by the ConstantTraits template.
381 // ConstantCreator - A class that is used to create constants by
382 // ConstantUniqueMap*. This class should be partially specialized if there is
383 // something strange that needs to be done to interface to the ctor for the
386 template<typename T, typename Alloc>
387 struct ConstantTraits< std::vector<T, Alloc> > {
388 static unsigned uses(const std::vector<T, Alloc>& v) {
394 struct ConstantTraits<Constant *> {
395 static unsigned uses(Constant * const & v) {
400 template<class ConstantClass, class TypeClass, class ValType>
401 struct ConstantCreator {
402 static ConstantClass *create(TypeClass *Ty, const ValType &V) {
403 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
407 template<class ConstantClass, class TypeClass>
408 struct ConstantArrayCreator {
409 static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) {
410 return new(V.size()) ConstantClass(Ty, V);
414 template<class ConstantClass>
415 struct ConstantKeyData {
416 typedef void ValType;
417 static ValType getValType(ConstantClass *C) {
418 llvm_unreachable("Unknown Constant type!");
423 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
424 static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V,
425 unsigned short pred = 0) {
426 if (Instruction::isCast(V.opcode))
427 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
428 if ((V.opcode >= Instruction::BinaryOpsBegin &&
429 V.opcode < Instruction::BinaryOpsEnd))
430 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
431 V.subclassoptionaldata);
432 if (V.opcode == Instruction::Select)
433 return new SelectConstantExpr(V.operands[0], V.operands[1],
435 if (V.opcode == Instruction::ExtractElement)
436 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
437 if (V.opcode == Instruction::InsertElement)
438 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
440 if (V.opcode == Instruction::ShuffleVector)
441 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
443 if (V.opcode == Instruction::InsertValue)
444 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
446 if (V.opcode == Instruction::ExtractValue)
447 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
448 if (V.opcode == Instruction::GetElementPtr) {
449 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
450 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
451 V.subclassoptionaldata);
454 // The compare instructions are weird. We have to encode the predicate
455 // value and it is combined with the instruction opcode by multiplying
456 // the opcode by one hundred. We must decode this to get the predicate.
457 if (V.opcode == Instruction::ICmp)
458 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
459 V.operands[0], V.operands[1]);
460 if (V.opcode == Instruction::FCmp)
461 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
462 V.operands[0], V.operands[1]);
463 llvm_unreachable("Invalid ConstantExpr!");
468 struct ConstantKeyData<ConstantExpr> {
469 typedef ExprMapKeyType ValType;
470 static ValType getValType(ConstantExpr *CE) {
471 std::vector<Constant*> Operands;
472 Operands.reserve(CE->getNumOperands());
473 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
474 Operands.push_back(cast<Constant>(CE->getOperand(i)));
475 return ExprMapKeyType(CE->getOpcode(), Operands,
476 CE->isCompare() ? CE->getPredicate() : 0,
477 CE->getRawSubclassOptionalData(),
479 CE->getIndices() : ArrayRef<unsigned>());
484 struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
485 static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
486 return new InlineAsm(Ty, Key.asm_string, Key.constraints,
487 Key.has_side_effects, Key.is_align_stack,
493 struct ConstantKeyData<InlineAsm> {
494 typedef InlineAsmKeyType ValType;
495 static ValType getValType(InlineAsm *Asm) {
496 return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
497 Asm->hasSideEffects(), Asm->isAlignStack(),
502 template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
503 bool HasLargeKey = false /*true for arrays and structs*/ >
504 class ConstantUniqueMap {
506 typedef std::pair<TypeClass*, ValType> MapKey;
507 typedef std::map<MapKey, ConstantClass *> MapTy;
508 typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy;
510 /// Map - This is the main map from the element descriptor to the Constants.
511 /// This is the primary way we avoid creating two of the same shape
515 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
516 /// from the constants to their element in Map. This is important for
517 /// removal of constants from the array, which would otherwise have to scan
518 /// through the map with very large keys.
519 InverseMapTy InverseMap;
522 typename MapTy::iterator map_begin() { return Map.begin(); }
523 typename MapTy::iterator map_end() { return Map.end(); }
525 void freeConstants() {
526 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
528 // Asserts that use_empty().
533 /// InsertOrGetItem - Return an iterator for the specified element.
534 /// If the element exists in the map, the returned iterator points to the
535 /// entry and Exists=true. If not, the iterator points to the newly
536 /// inserted entry and returns Exists=false. Newly inserted entries have
537 /// I->second == 0, and should be filled in.
538 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
541 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
547 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
549 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
550 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
551 IMI->second->second == CP &&
552 "InverseMap corrupt!");
556 typename MapTy::iterator I =
557 Map.find(MapKey(static_cast<TypeClass*>(CP->getType()),
558 ConstantKeyData<ConstantClass>::getValType(CP)));
559 if (I == Map.end() || I->second != CP) {
560 // FIXME: This should not use a linear scan. If this gets to be a
561 // performance problem, someone should look at this.
562 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
568 ConstantClass *Create(TypeClass *Ty, ValRefType V,
569 typename MapTy::iterator I) {
570 ConstantClass* Result =
571 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
573 assert(Result->getType() == Ty && "Type specified is not correct!");
574 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
576 if (HasLargeKey) // Remember the reverse mapping if needed.
577 InverseMap.insert(std::make_pair(Result, I));
583 /// getOrCreate - Return the specified constant from the map, creating it if
585 ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) {
586 MapKey Lookup(Ty, V);
587 ConstantClass* Result = 0;
589 typename MapTy::iterator I = Map.find(Lookup);
595 // If no preexisting value, create one now...
596 Result = Create(Ty, V, I);
602 void remove(ConstantClass *CP) {
603 typename MapTy::iterator I = FindExistingElement(CP);
604 assert(I != Map.end() && "Constant not found in constant table!");
605 assert(I->second == CP && "Didn't find correct element?");
607 if (HasLargeKey) // Remember the reverse mapping if needed.
608 InverseMap.erase(CP);
613 /// MoveConstantToNewSlot - If we are about to change C to be the element
614 /// specified by I, update our internal data structures to reflect this
616 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
617 // First, remove the old location of the specified constant in the map.
618 typename MapTy::iterator OldI = FindExistingElement(C);
619 assert(OldI != Map.end() && "Constant not found in constant table!");
620 assert(OldI->second == C && "Didn't find correct element?");
622 // Remove the old entry from the map.
625 // Update the inverse map so that we know that this constant is now
626 // located at descriptor I.
628 assert(I->second == C && "Bad inversemap entry!");
634 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
638 // Unique map for aggregate constants
639 template<class TypeClass, class ConstantClass>
640 class ConstantAggrUniqueMap {
642 typedef ArrayRef<Constant*> Operands;
643 typedef std::pair<TypeClass*, Operands> LookupKey;
646 typedef DenseMapInfo<ConstantClass*> ConstantClassInfo;
647 typedef DenseMapInfo<Constant*> ConstantInfo;
648 typedef DenseMapInfo<TypeClass*> TypeClassInfo;
649 static inline ConstantClass* getEmptyKey() {
650 return ConstantClassInfo::getEmptyKey();
652 static inline ConstantClass* getTombstoneKey() {
653 return ConstantClassInfo::getTombstoneKey();
655 static unsigned getHashValue(const ConstantClass *CP) {
656 SmallVector<Constant*, 8> CPOperands;
657 CPOperands.reserve(CP->getNumOperands());
658 for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I)
659 CPOperands.push_back(CP->getOperand(I));
660 return getHashValue(LookupKey(CP->getType(), CPOperands));
662 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
665 static unsigned getHashValue(const LookupKey &Val) {
666 return hash_combine(Val.first, hash_combine_range(Val.second.begin(),
669 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
670 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
672 if (LHS.first != RHS->getType()
673 || LHS.second.size() != RHS->getNumOperands())
675 for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) {
676 if (LHS.second[I] != RHS->getOperand(I))
683 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
686 /// Map - This is the main map from the element descriptor to the Constants.
687 /// This is the primary way we avoid creating two of the same shape
692 typename MapTy::iterator map_begin() { return Map.begin(); }
693 typename MapTy::iterator map_end() { return Map.end(); }
695 void freeConstants() {
696 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
698 // Asserts that use_empty().
704 typename MapTy::iterator findExistingElement(ConstantClass *CP) {
708 ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) {
709 ConstantClass* Result =
710 ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V);
712 assert(Result->getType() == Ty && "Type specified is not correct!");
719 /// getOrCreate - Return the specified constant from the map, creating it if
721 ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
722 LookupKey Lookup(Ty, V);
723 ConstantClass* Result = 0;
725 typename MapTy::iterator I = Map.find_as(Lookup);
731 // If no preexisting value, create one now...
732 Result = Create(Ty, V, I);
738 /// Find the constant by lookup key.
739 typename MapTy::iterator find(LookupKey Lookup) {
740 return Map.find_as(Lookup);
743 /// Insert the constant into its proper slot.
744 void insert(ConstantClass *CP) {
748 /// Remove this constant from the map
749 void remove(ConstantClass *CP) {
750 typename MapTy::iterator I = findExistingElement(CP);
751 assert(I != Map.end() && "Constant not found in constant table!");
752 assert(I->first == CP && "Didn't find correct element?");
757 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");