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 #define DEBUG_TYPE "ir"
32 template<class ValType>
33 struct ConstantTraits;
35 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
36 /// behind the scenes to implement unary constant exprs.
37 class UnaryConstantExpr : public ConstantExpr {
38 void anchor() override;
39 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
41 // allocate space for exactly one operand
42 void *operator new(size_t s) {
43 return User::operator new(s, 1);
45 UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
46 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
49 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
52 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
53 /// behind the scenes to implement binary constant exprs.
54 class BinaryConstantExpr : public ConstantExpr {
55 void anchor() override;
56 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
58 // allocate space for exactly two operands
59 void *operator new(size_t s) {
60 return User::operator new(s, 2);
62 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
64 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
67 SubclassOptionalData = Flags;
69 /// Transparently provide more efficient getOperand methods.
70 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
73 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
74 /// behind the scenes to implement select constant exprs.
75 class SelectConstantExpr : public ConstantExpr {
76 void anchor() override;
77 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
79 // allocate space for exactly three operands
80 void *operator new(size_t s) {
81 return User::operator new(s, 3);
83 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
84 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
89 /// Transparently provide more efficient getOperand methods.
90 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
93 /// ExtractElementConstantExpr - This class is private to
94 /// Constants.cpp, and is used behind the scenes to implement
95 /// extractelement constant exprs.
96 class ExtractElementConstantExpr : public ConstantExpr {
97 void anchor() override;
98 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
100 // allocate space for exactly two operands
101 void *operator new(size_t s) {
102 return User::operator new(s, 2);
104 ExtractElementConstantExpr(Constant *C1, Constant *C2)
105 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
106 Instruction::ExtractElement, &Op<0>(), 2) {
110 /// Transparently provide more efficient getOperand methods.
111 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
114 /// InsertElementConstantExpr - This class is private to
115 /// Constants.cpp, and is used behind the scenes to implement
116 /// insertelement constant exprs.
117 class InsertElementConstantExpr : public ConstantExpr {
118 void anchor() override;
119 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
121 // allocate space for exactly three operands
122 void *operator new(size_t s) {
123 return User::operator new(s, 3);
125 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
126 : ConstantExpr(C1->getType(), Instruction::InsertElement,
132 /// Transparently provide more efficient getOperand methods.
133 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
136 /// ShuffleVectorConstantExpr - This class is private to
137 /// Constants.cpp, and is used behind the scenes to implement
138 /// shufflevector constant exprs.
139 class ShuffleVectorConstantExpr : public ConstantExpr {
140 void anchor() override;
141 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
143 // allocate space for exactly three operands
144 void *operator new(size_t s) {
145 return User::operator new(s, 3);
147 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
148 : ConstantExpr(VectorType::get(
149 cast<VectorType>(C1->getType())->getElementType(),
150 cast<VectorType>(C3->getType())->getNumElements()),
151 Instruction::ShuffleVector,
157 /// Transparently provide more efficient getOperand methods.
158 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
161 /// ExtractValueConstantExpr - This class is private to
162 /// Constants.cpp, and is used behind the scenes to implement
163 /// extractvalue constant exprs.
164 class ExtractValueConstantExpr : public ConstantExpr {
165 void anchor() override;
166 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
168 // allocate space for exactly one operand
169 void *operator new(size_t s) {
170 return User::operator new(s, 1);
172 ExtractValueConstantExpr(Constant *Agg,
173 const SmallVector<unsigned, 4> &IdxList,
175 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
180 /// Indices - These identify which value to extract.
181 const SmallVector<unsigned, 4> Indices;
183 /// Transparently provide more efficient getOperand methods.
184 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
187 /// InsertValueConstantExpr - This class is private to
188 /// Constants.cpp, and is used behind the scenes to implement
189 /// insertvalue constant exprs.
190 class InsertValueConstantExpr : public ConstantExpr {
191 void anchor() override;
192 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
194 // allocate space for exactly one operand
195 void *operator new(size_t s) {
196 return User::operator new(s, 2);
198 InsertValueConstantExpr(Constant *Agg, Constant *Val,
199 const SmallVector<unsigned, 4> &IdxList,
201 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
207 /// Indices - These identify the position for the insertion.
208 const SmallVector<unsigned, 4> Indices;
210 /// Transparently provide more efficient getOperand methods.
211 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
215 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
216 /// used behind the scenes to implement getelementpr constant exprs.
217 class GetElementPtrConstantExpr : public ConstantExpr {
218 void anchor() override;
219 GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
222 static GetElementPtrConstantExpr *Create(Constant *C,
223 ArrayRef<Constant*> IdxList,
226 GetElementPtrConstantExpr *Result =
227 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
228 Result->SubclassOptionalData = Flags;
231 /// Transparently provide more efficient getOperand methods.
232 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
235 // CompareConstantExpr - This class is private to Constants.cpp, and is used
236 // behind the scenes to implement ICmp and FCmp constant expressions. This is
237 // needed in order to store the predicate value for these instructions.
238 class CompareConstantExpr : public ConstantExpr {
239 void anchor() override;
240 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
242 // allocate space for exactly two operands
243 void *operator new(size_t s) {
244 return User::operator new(s, 2);
246 unsigned short predicate;
247 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
248 unsigned short pred, Constant* LHS, Constant* RHS)
249 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
253 /// Transparently provide more efficient getOperand methods.
254 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
258 struct OperandTraits<UnaryConstantExpr> :
259 public FixedNumOperandTraits<UnaryConstantExpr, 1> {
261 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
264 struct OperandTraits<BinaryConstantExpr> :
265 public FixedNumOperandTraits<BinaryConstantExpr, 2> {
267 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
270 struct OperandTraits<SelectConstantExpr> :
271 public FixedNumOperandTraits<SelectConstantExpr, 3> {
273 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
276 struct OperandTraits<ExtractElementConstantExpr> :
277 public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
279 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
282 struct OperandTraits<InsertElementConstantExpr> :
283 public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
285 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
288 struct OperandTraits<ShuffleVectorConstantExpr> :
289 public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
291 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
294 struct OperandTraits<ExtractValueConstantExpr> :
295 public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
297 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
300 struct OperandTraits<InsertValueConstantExpr> :
301 public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
303 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
306 struct OperandTraits<GetElementPtrConstantExpr> :
307 public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
310 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
314 struct OperandTraits<CompareConstantExpr> :
315 public FixedNumOperandTraits<CompareConstantExpr, 2> {
317 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
319 struct ExprMapKeyType {
320 ExprMapKeyType(unsigned opc,
321 ArrayRef<Constant*> ops,
322 unsigned short flags = 0,
323 unsigned short optionalflags = 0,
324 ArrayRef<unsigned> inds = None)
325 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
326 operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {}
328 uint8_t subclassoptionaldata;
329 uint16_t subclassdata;
330 std::vector<Constant*> operands;
331 SmallVector<unsigned, 4> indices;
332 bool operator==(const ExprMapKeyType& that) const {
333 return this->opcode == that.opcode &&
334 this->subclassdata == that.subclassdata &&
335 this->subclassoptionaldata == that.subclassoptionaldata &&
336 this->operands == that.operands &&
337 this->indices == that.indices;
339 bool operator<(const ExprMapKeyType & that) const {
340 return std::tie(opcode, operands, subclassdata, subclassoptionaldata,
342 std::tie(that.opcode, that.operands, that.subclassdata,
343 that.subclassoptionaldata, 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,
354 bool isAlignStack, InlineAsm::AsmDialect asmDialect)
355 : asm_string(AsmString), constraints(Constraints),
356 has_side_effects(hasSideEffects), is_align_stack(isAlignStack),
357 asm_dialect(asmDialect) {}
358 std::string asm_string;
359 std::string constraints;
360 bool has_side_effects;
362 InlineAsm::AsmDialect asm_dialect;
363 bool operator==(const InlineAsmKeyType& that) const {
364 return this->asm_string == that.asm_string &&
365 this->constraints == that.constraints &&
366 this->has_side_effects == that.has_side_effects &&
367 this->is_align_stack == that.is_align_stack &&
368 this->asm_dialect == that.asm_dialect;
370 bool operator<(const InlineAsmKeyType& that) const {
371 return std::tie(asm_string, constraints, has_side_effects, is_align_stack,
373 std::tie(that.asm_string, that.constraints, that.has_side_effects,
374 that.is_align_stack, that.asm_dialect);
377 bool operator!=(const InlineAsmKeyType& that) const {
378 return !(*this == that);
382 // The number of operands for each ConstantCreator::create method is
383 // determined by the ConstantTraits template.
384 // ConstantCreator - A class that is used to create constants by
385 // ConstantUniqueMap*. This class should be partially specialized if there is
386 // something strange that needs to be done to interface to the ctor for the
389 template<typename T, typename Alloc>
390 struct ConstantTraits< std::vector<T, Alloc> > {
391 static unsigned uses(const std::vector<T, Alloc>& v) {
397 struct ConstantTraits<Constant *> {
398 static unsigned uses(Constant * const & v) {
403 template<class ConstantClass, class TypeClass, class ValType>
404 struct ConstantCreator {
405 static ConstantClass *create(TypeClass *Ty, const ValType &V) {
406 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
410 template<class ConstantClass, class TypeClass>
411 struct ConstantArrayCreator {
412 static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) {
413 return new(V.size()) ConstantClass(Ty, V);
417 template<class ConstantClass>
418 struct ConstantKeyData {
419 typedef void ValType;
420 static ValType getValType(ConstantClass *C) {
421 llvm_unreachable("Unknown Constant type!");
426 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
427 static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V,
428 unsigned short pred = 0) {
429 if (Instruction::isCast(V.opcode))
430 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
431 if ((V.opcode >= Instruction::BinaryOpsBegin &&
432 V.opcode < Instruction::BinaryOpsEnd))
433 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
434 V.subclassoptionaldata);
435 if (V.opcode == Instruction::Select)
436 return new SelectConstantExpr(V.operands[0], V.operands[1],
438 if (V.opcode == Instruction::ExtractElement)
439 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
440 if (V.opcode == Instruction::InsertElement)
441 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
443 if (V.opcode == Instruction::ShuffleVector)
444 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
446 if (V.opcode == Instruction::InsertValue)
447 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
449 if (V.opcode == Instruction::ExtractValue)
450 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
451 if (V.opcode == Instruction::GetElementPtr) {
452 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
453 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
454 V.subclassoptionaldata);
457 // The compare instructions are weird. We have to encode the predicate
458 // value and it is combined with the instruction opcode by multiplying
459 // the opcode by one hundred. We must decode this to get the predicate.
460 if (V.opcode == Instruction::ICmp)
461 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
462 V.operands[0], V.operands[1]);
463 if (V.opcode == Instruction::FCmp)
464 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
465 V.operands[0], V.operands[1]);
466 llvm_unreachable("Invalid ConstantExpr!");
471 struct ConstantKeyData<ConstantExpr> {
472 typedef ExprMapKeyType ValType;
473 static ValType getValType(ConstantExpr *CE) {
474 std::vector<Constant*> Operands;
475 Operands.reserve(CE->getNumOperands());
476 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
477 Operands.push_back(cast<Constant>(CE->getOperand(i)));
478 return ExprMapKeyType(CE->getOpcode(), Operands,
479 CE->isCompare() ? CE->getPredicate() : 0,
480 CE->getRawSubclassOptionalData(),
482 CE->getIndices() : ArrayRef<unsigned>());
487 struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
488 static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
489 return new InlineAsm(Ty, Key.asm_string, Key.constraints,
490 Key.has_side_effects, Key.is_align_stack,
496 struct ConstantKeyData<InlineAsm> {
497 typedef InlineAsmKeyType ValType;
498 static ValType getValType(InlineAsm *Asm) {
499 return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
500 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 = nullptr;
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 SmallVector<Constant*, 8> CPOperands;
660 CPOperands.reserve(CP->getNumOperands());
661 for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I)
662 CPOperands.push_back(CP->getOperand(I));
663 return getHashValue(LookupKey(CP->getType(), CPOperands));
665 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
668 static unsigned getHashValue(const LookupKey &Val) {
669 return hash_combine(Val.first, hash_combine_range(Val.second.begin(),
672 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
673 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
675 if (LHS.first != RHS->getType()
676 || LHS.second.size() != RHS->getNumOperands())
678 for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) {
679 if (LHS.second[I] != RHS->getOperand(I))
686 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
689 /// Map - This is the main map from the element descriptor to the Constants.
690 /// This is the primary way we avoid creating two of the same shape
695 typename MapTy::iterator map_begin() { return Map.begin(); }
696 typename MapTy::iterator map_end() { return Map.end(); }
698 void freeConstants() {
699 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
701 // Asserts that use_empty().
707 typename MapTy::iterator findExistingElement(ConstantClass *CP) {
711 ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) {
712 ConstantClass* Result =
713 ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V);
715 assert(Result->getType() == Ty && "Type specified is not correct!");
722 /// getOrCreate - Return the specified constant from the map, creating it if
724 ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
725 LookupKey Lookup(Ty, V);
726 ConstantClass* Result = nullptr;
728 typename MapTy::iterator I = Map.find_as(Lookup);
734 // If no preexisting value, create one now...
735 Result = Create(Ty, V, I);
741 /// Find the constant by lookup key.
742 typename MapTy::iterator find(LookupKey Lookup) {
743 return Map.find_as(Lookup);
746 /// Insert the constant into its proper slot.
747 void insert(ConstantClass *CP) {
751 /// Remove this constant from the map
752 void remove(ConstantClass *CP) {
753 typename MapTy::iterator I = findExistingElement(CP);
754 assert(I != Map.end() && "Constant not found in constant table!");
755 assert(I->first == CP && "Didn't find correct element?");
760 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");