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_LIB_IR_CONSTANTSCONTEXT_H
16 #define LLVM_LIB_IR_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, ArrayRef<unsigned> IdxList,
174 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
175 Indices(IdxList.begin(), IdxList.end()) {
179 /// Indices - These identify which value to extract.
180 const SmallVector<unsigned, 4> Indices;
182 /// Transparently provide more efficient getOperand methods.
183 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
186 /// InsertValueConstantExpr - This class is private to
187 /// Constants.cpp, and is used behind the scenes to implement
188 /// insertvalue constant exprs.
189 class InsertValueConstantExpr : public ConstantExpr {
190 void anchor() override;
191 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
193 // allocate space for exactly one operand
194 void *operator new(size_t s) {
195 return User::operator new(s, 2);
197 InsertValueConstantExpr(Constant *Agg, Constant *Val,
198 ArrayRef<unsigned> IdxList, Type *DestTy)
199 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
200 Indices(IdxList.begin(), IdxList.end()) {
205 /// Indices - These identify the position for the insertion.
206 const SmallVector<unsigned, 4> Indices;
208 /// Transparently provide more efficient getOperand methods.
209 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
213 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
214 /// used behind the scenes to implement getelementpr constant exprs.
215 class GetElementPtrConstantExpr : public ConstantExpr {
216 void anchor() override;
217 GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
220 static GetElementPtrConstantExpr *Create(Constant *C,
221 ArrayRef<Constant*> IdxList,
224 GetElementPtrConstantExpr *Result =
225 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
226 Result->SubclassOptionalData = Flags;
229 /// Transparently provide more efficient getOperand methods.
230 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
233 // CompareConstantExpr - This class is private to Constants.cpp, and is used
234 // behind the scenes to implement ICmp and FCmp constant expressions. This is
235 // needed in order to store the predicate value for these instructions.
236 class CompareConstantExpr : public ConstantExpr {
237 void anchor() override;
238 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
240 // allocate space for exactly two operands
241 void *operator new(size_t s) {
242 return User::operator new(s, 2);
244 unsigned short predicate;
245 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
246 unsigned short pred, Constant* LHS, Constant* RHS)
247 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
251 /// Transparently provide more efficient getOperand methods.
252 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
256 struct OperandTraits<UnaryConstantExpr> :
257 public FixedNumOperandTraits<UnaryConstantExpr, 1> {
259 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
262 struct OperandTraits<BinaryConstantExpr> :
263 public FixedNumOperandTraits<BinaryConstantExpr, 2> {
265 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
268 struct OperandTraits<SelectConstantExpr> :
269 public FixedNumOperandTraits<SelectConstantExpr, 3> {
271 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
274 struct OperandTraits<ExtractElementConstantExpr> :
275 public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
277 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
280 struct OperandTraits<InsertElementConstantExpr> :
281 public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
283 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
286 struct OperandTraits<ShuffleVectorConstantExpr> :
287 public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
289 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
292 struct OperandTraits<ExtractValueConstantExpr> :
293 public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
295 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
298 struct OperandTraits<InsertValueConstantExpr> :
299 public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
301 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
304 struct OperandTraits<GetElementPtrConstantExpr> :
305 public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
308 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
312 struct OperandTraits<CompareConstantExpr> :
313 public FixedNumOperandTraits<CompareConstantExpr, 2> {
315 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
317 struct ExprMapKeyType {
318 ExprMapKeyType(unsigned opc,
319 ArrayRef<Constant*> ops,
320 unsigned short flags = 0,
321 unsigned short optionalflags = 0,
322 ArrayRef<unsigned> inds = None)
323 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
324 operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {}
326 uint8_t subclassoptionaldata;
327 uint16_t subclassdata;
328 std::vector<Constant*> operands;
329 SmallVector<unsigned, 4> indices;
330 bool operator==(const ExprMapKeyType& that) const {
331 return this->opcode == that.opcode &&
332 this->subclassdata == that.subclassdata &&
333 this->subclassoptionaldata == that.subclassoptionaldata &&
334 this->operands == that.operands &&
335 this->indices == that.indices;
337 bool operator<(const ExprMapKeyType & that) const {
338 return std::tie(opcode, operands, subclassdata, subclassoptionaldata,
340 std::tie(that.opcode, that.operands, that.subclassdata,
341 that.subclassoptionaldata, that.indices);
344 bool operator!=(const ExprMapKeyType& that) const {
345 return !(*this == that);
349 struct InlineAsmKeyType {
350 InlineAsmKeyType(StringRef AsmString,
351 StringRef Constraints, bool hasSideEffects,
352 bool isAlignStack, InlineAsm::AsmDialect asmDialect)
353 : asm_string(AsmString), constraints(Constraints),
354 has_side_effects(hasSideEffects), is_align_stack(isAlignStack),
355 asm_dialect(asmDialect) {}
356 std::string asm_string;
357 std::string constraints;
358 bool has_side_effects;
360 InlineAsm::AsmDialect asm_dialect;
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 &&
366 this->asm_dialect == that.asm_dialect;
368 bool operator<(const InlineAsmKeyType& that) const {
369 return std::tie(asm_string, constraints, has_side_effects, is_align_stack,
371 std::tie(that.asm_string, that.constraints, that.has_side_effects,
372 that.is_align_stack, that.asm_dialect);
375 bool operator!=(const InlineAsmKeyType& that) const {
376 return !(*this == that);
380 // The number of operands for each ConstantCreator::create method is
381 // determined by the ConstantTraits template.
382 // ConstantCreator - A class that is used to create constants by
383 // ConstantUniqueMap*. This class should be partially specialized if there is
384 // something strange that needs to be done to interface to the ctor for the
387 template<typename T, typename Alloc>
388 struct ConstantTraits< std::vector<T, Alloc> > {
389 static unsigned uses(const std::vector<T, Alloc>& v) {
395 struct ConstantTraits<Constant *> {
396 static unsigned uses(Constant * const & v) {
401 template<class ConstantClass, class TypeClass, class ValType>
402 struct ConstantCreator {
403 static ConstantClass *create(TypeClass *Ty, const ValType &V) {
404 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
408 template<class ConstantClass, class TypeClass>
409 struct ConstantArrayCreator {
410 static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) {
411 return new(V.size()) ConstantClass(Ty, V);
415 template<class ConstantClass>
416 struct ConstantKeyData {
417 typedef void ValType;
418 static ValType getValType(ConstantClass *C) {
419 llvm_unreachable("Unknown Constant type!");
424 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
425 static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V,
426 unsigned short pred = 0) {
427 if (Instruction::isCast(V.opcode))
428 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
429 if ((V.opcode >= Instruction::BinaryOpsBegin &&
430 V.opcode < Instruction::BinaryOpsEnd))
431 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
432 V.subclassoptionaldata);
433 if (V.opcode == Instruction::Select)
434 return new SelectConstantExpr(V.operands[0], V.operands[1],
436 if (V.opcode == Instruction::ExtractElement)
437 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
438 if (V.opcode == Instruction::InsertElement)
439 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
441 if (V.opcode == Instruction::ShuffleVector)
442 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
444 if (V.opcode == Instruction::InsertValue)
445 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
447 if (V.opcode == Instruction::ExtractValue)
448 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
449 if (V.opcode == Instruction::GetElementPtr) {
450 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
451 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
452 V.subclassoptionaldata);
455 // The compare instructions are weird. We have to encode the predicate
456 // value and it is combined with the instruction opcode by multiplying
457 // the opcode by one hundred. We must decode this to get the predicate.
458 if (V.opcode == Instruction::ICmp)
459 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
460 V.operands[0], V.operands[1]);
461 if (V.opcode == Instruction::FCmp)
462 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
463 V.operands[0], V.operands[1]);
464 llvm_unreachable("Invalid ConstantExpr!");
469 struct ConstantKeyData<ConstantExpr> {
470 typedef ExprMapKeyType ValType;
471 static ValType getValType(ConstantExpr *CE) {
472 std::vector<Constant*> Operands;
473 Operands.reserve(CE->getNumOperands());
474 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
475 Operands.push_back(cast<Constant>(CE->getOperand(i)));
476 return ExprMapKeyType(CE->getOpcode(), Operands,
477 CE->isCompare() ? CE->getPredicate() : 0,
478 CE->getRawSubclassOptionalData(),
480 CE->getIndices() : ArrayRef<unsigned>());
485 struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
486 static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
487 return new InlineAsm(Ty, Key.asm_string, Key.constraints,
488 Key.has_side_effects, Key.is_align_stack,
494 struct ConstantKeyData<InlineAsm> {
495 typedef InlineAsmKeyType ValType;
496 static ValType getValType(InlineAsm *Asm) {
497 return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
498 Asm->hasSideEffects(), Asm->isAlignStack(),
503 template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
504 bool HasLargeKey = false /*true for arrays and structs*/ >
505 class ConstantUniqueMap {
507 typedef std::pair<TypeClass*, ValType> MapKey;
508 typedef std::map<MapKey, ConstantClass *> MapTy;
509 typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy;
511 /// Map - This is the main map from the element descriptor to the Constants.
512 /// This is the primary way we avoid creating two of the same shape
516 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
517 /// from the constants to their element in Map. This is important for
518 /// removal of constants from the array, which would otherwise have to scan
519 /// through the map with very large keys.
520 InverseMapTy InverseMap;
523 typename MapTy::iterator map_begin() { return Map.begin(); }
524 typename MapTy::iterator map_end() { return Map.end(); }
526 void freeConstants() {
527 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
529 // Asserts that use_empty().
534 /// InsertOrGetItem - Return an iterator for the specified element.
535 /// If the element exists in the map, the returned iterator points to the
536 /// entry and Exists=true. If not, the iterator points to the newly
537 /// inserted entry and returns Exists=false. Newly inserted entries have
538 /// I->second == 0, and should be filled in.
539 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
542 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
548 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
550 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
551 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
552 IMI->second->second == CP &&
553 "InverseMap corrupt!");
557 typename MapTy::iterator I =
558 Map.find(MapKey(static_cast<TypeClass*>(CP->getType()),
559 ConstantKeyData<ConstantClass>::getValType(CP)));
560 if (I == Map.end() || I->second != CP) {
561 // FIXME: This should not use a linear scan. If this gets to be a
562 // performance problem, someone should look at this.
563 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
569 ConstantClass *Create(TypeClass *Ty, ValRefType V,
570 typename MapTy::iterator I) {
571 ConstantClass* Result =
572 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
574 assert(Result->getType() == Ty && "Type specified is not correct!");
575 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
577 if (HasLargeKey) // Remember the reverse mapping if needed.
578 InverseMap.insert(std::make_pair(Result, I));
584 /// getOrCreate - Return the specified constant from the map, creating it if
586 ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) {
587 MapKey Lookup(Ty, V);
588 ConstantClass* Result = nullptr;
590 typename MapTy::iterator I = Map.find(Lookup);
596 // If no preexisting value, create one now...
597 Result = Create(Ty, V, I);
603 void remove(ConstantClass *CP) {
604 typename MapTy::iterator I = FindExistingElement(CP);
605 assert(I != Map.end() && "Constant not found in constant table!");
606 assert(I->second == CP && "Didn't find correct element?");
608 if (HasLargeKey) // Remember the reverse mapping if needed.
609 InverseMap.erase(CP);
614 /// MoveConstantToNewSlot - If we are about to change C to be the element
615 /// specified by I, update our internal data structures to reflect this
617 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
618 // First, remove the old location of the specified constant in the map.
619 typename MapTy::iterator OldI = FindExistingElement(C);
620 assert(OldI != Map.end() && "Constant not found in constant table!");
621 assert(OldI->second == C && "Didn't find correct element?");
623 // Remove the old entry from the map.
626 // Update the inverse map so that we know that this constant is now
627 // located at descriptor I.
629 assert(I->second == C && "Bad inversemap entry!");
635 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
639 // Unique map for aggregate constants
640 template<class TypeClass, class ConstantClass>
641 class ConstantAggrUniqueMap {
643 typedef ArrayRef<Constant*> Operands;
644 typedef std::pair<TypeClass*, Operands> LookupKey;
647 typedef DenseMapInfo<ConstantClass*> ConstantClassInfo;
648 typedef DenseMapInfo<Constant*> ConstantInfo;
649 typedef DenseMapInfo<TypeClass*> TypeClassInfo;
650 static inline ConstantClass* getEmptyKey() {
651 return ConstantClassInfo::getEmptyKey();
653 static inline ConstantClass* getTombstoneKey() {
654 return ConstantClassInfo::getTombstoneKey();
656 static unsigned getHashValue(const ConstantClass *CP) {
657 SmallVector<Constant*, 8> CPOperands;
658 CPOperands.reserve(CP->getNumOperands());
659 for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I)
660 CPOperands.push_back(CP->getOperand(I));
661 return getHashValue(LookupKey(CP->getType(), CPOperands));
663 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
666 static unsigned getHashValue(const LookupKey &Val) {
667 return hash_combine(Val.first, hash_combine_range(Val.second.begin(),
670 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
671 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
673 if (LHS.first != RHS->getType()
674 || LHS.second.size() != RHS->getNumOperands())
676 for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) {
677 if (LHS.second[I] != RHS->getOperand(I))
684 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
687 /// Map - This is the main map from the element descriptor to the Constants.
688 /// This is the primary way we avoid creating two of the same shape
693 typename MapTy::iterator map_begin() { return Map.begin(); }
694 typename MapTy::iterator map_end() { return Map.end(); }
696 void freeConstants() {
697 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
699 // Asserts that use_empty().
705 typename MapTy::iterator findExistingElement(ConstantClass *CP) {
709 ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) {
710 ConstantClass* Result =
711 ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V);
713 assert(Result->getType() == Ty && "Type specified is not correct!");
720 /// getOrCreate - Return the specified constant from the map, creating it if
722 ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
723 LookupKey Lookup(Ty, V);
724 ConstantClass* Result = nullptr;
726 typename MapTy::iterator I = Map.find_as(Lookup);
732 // If no preexisting value, create one now...
733 Result = Create(Ty, V, I);
739 /// Find the constant by lookup key.
740 typename MapTy::iterator find(LookupKey Lookup) {
741 return Map.find_as(Lookup);
744 /// Insert the constant into its proper slot.
745 void insert(ConstantClass *CP) {
749 /// Remove this constant from the map
750 void remove(ConstantClass *CP) {
751 typename MapTy::iterator I = findExistingElement(CP);
752 assert(I != Map.end() && "Constant not found in constant table!");
753 assert(I->first == CP && "Didn't find correct element?");
758 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
762 } // end namespace llvm