LLVM API Documentation

ConstantsContext.h
Go to the documentation of this file.
00001 //===-- ConstantsContext.h - Constants-related Context Interals -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines various helper methods and classes used by
00011 // LLVMContextImpl for creating and managing constants.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CONSTANTSCONTEXT_H
00016 #define LLVM_CONSTANTSCONTEXT_H
00017 
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/ADT/Hashing.h"
00020 #include "llvm/IR/InlineAsm.h"
00021 #include "llvm/IR/Instructions.h"
00022 #include "llvm/IR/Operator.h"
00023 #include "llvm/Support/Debug.h"
00024 #include "llvm/Support/ErrorHandling.h"
00025 #include "llvm/Support/raw_ostream.h"
00026 #include <map>
00027 
00028 namespace llvm {
00029 template<class ValType>
00030 struct ConstantTraits;
00031 
00032 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
00033 /// behind the scenes to implement unary constant exprs.
00034 class UnaryConstantExpr : public ConstantExpr {
00035   void anchor() override;
00036   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00037 public:
00038   // allocate space for exactly one operand
00039   void *operator new(size_t s) {
00040     return User::operator new(s, 1);
00041   }
00042   UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
00043     : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
00044     Op<0>() = C;
00045   }
00046   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00047 };
00048 
00049 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
00050 /// behind the scenes to implement binary constant exprs.
00051 class BinaryConstantExpr : public ConstantExpr {
00052   void anchor() override;
00053   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00054 public:
00055   // allocate space for exactly two operands
00056   void *operator new(size_t s) {
00057     return User::operator new(s, 2);
00058   }
00059   BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
00060                      unsigned Flags)
00061     : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
00062     Op<0>() = C1;
00063     Op<1>() = C2;
00064     SubclassOptionalData = Flags;
00065   }
00066   /// Transparently provide more efficient getOperand methods.
00067   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00068 };
00069 
00070 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
00071 /// behind the scenes to implement select constant exprs.
00072 class SelectConstantExpr : public ConstantExpr {
00073   void anchor() override;
00074   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00075 public:
00076   // allocate space for exactly three operands
00077   void *operator new(size_t s) {
00078     return User::operator new(s, 3);
00079   }
00080   SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
00081     : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
00082     Op<0>() = C1;
00083     Op<1>() = C2;
00084     Op<2>() = C3;
00085   }
00086   /// Transparently provide more efficient getOperand methods.
00087   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00088 };
00089 
00090 /// ExtractElementConstantExpr - This class is private to
00091 /// Constants.cpp, and is used behind the scenes to implement
00092 /// extractelement constant exprs.
00093 class ExtractElementConstantExpr : public ConstantExpr {
00094   void anchor() override;
00095   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00096 public:
00097   // allocate space for exactly two operands
00098   void *operator new(size_t s) {
00099     return User::operator new(s, 2);
00100   }
00101   ExtractElementConstantExpr(Constant *C1, Constant *C2)
00102     : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(), 
00103                    Instruction::ExtractElement, &Op<0>(), 2) {
00104     Op<0>() = C1;
00105     Op<1>() = C2;
00106   }
00107   /// Transparently provide more efficient getOperand methods.
00108   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00109 };
00110 
00111 /// InsertElementConstantExpr - This class is private to
00112 /// Constants.cpp, and is used behind the scenes to implement
00113 /// insertelement constant exprs.
00114 class InsertElementConstantExpr : public ConstantExpr {
00115   void anchor() override;
00116   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00117 public:
00118   // allocate space for exactly three operands
00119   void *operator new(size_t s) {
00120     return User::operator new(s, 3);
00121   }
00122   InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
00123     : ConstantExpr(C1->getType(), Instruction::InsertElement, 
00124                    &Op<0>(), 3) {
00125     Op<0>() = C1;
00126     Op<1>() = C2;
00127     Op<2>() = C3;
00128   }
00129   /// Transparently provide more efficient getOperand methods.
00130   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00131 };
00132 
00133 /// ShuffleVectorConstantExpr - This class is private to
00134 /// Constants.cpp, and is used behind the scenes to implement
00135 /// shufflevector constant exprs.
00136 class ShuffleVectorConstantExpr : public ConstantExpr {
00137   void anchor() override;
00138   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00139 public:
00140   // allocate space for exactly three operands
00141   void *operator new(size_t s) {
00142     return User::operator new(s, 3);
00143   }
00144   ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
00145   : ConstantExpr(VectorType::get(
00146                    cast<VectorType>(C1->getType())->getElementType(),
00147                    cast<VectorType>(C3->getType())->getNumElements()),
00148                  Instruction::ShuffleVector, 
00149                  &Op<0>(), 3) {
00150     Op<0>() = C1;
00151     Op<1>() = C2;
00152     Op<2>() = C3;
00153   }
00154   /// Transparently provide more efficient getOperand methods.
00155   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00156 };
00157 
00158 /// ExtractValueConstantExpr - This class is private to
00159 /// Constants.cpp, and is used behind the scenes to implement
00160 /// extractvalue constant exprs.
00161 class ExtractValueConstantExpr : public ConstantExpr {
00162   void anchor() override;
00163   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00164 public:
00165   // allocate space for exactly one operand
00166   void *operator new(size_t s) {
00167     return User::operator new(s, 1);
00168   }
00169   ExtractValueConstantExpr(Constant *Agg,
00170                            const SmallVector<unsigned, 4> &IdxList,
00171                            Type *DestTy)
00172     : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
00173       Indices(IdxList) {
00174     Op<0>() = Agg;
00175   }
00176 
00177   /// Indices - These identify which value to extract.
00178   const SmallVector<unsigned, 4> Indices;
00179 
00180   /// Transparently provide more efficient getOperand methods.
00181   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00182 };
00183 
00184 /// InsertValueConstantExpr - This class is private to
00185 /// Constants.cpp, and is used behind the scenes to implement
00186 /// insertvalue constant exprs.
00187 class InsertValueConstantExpr : public ConstantExpr {
00188   void anchor() override;
00189   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00190 public:
00191   // allocate space for exactly one operand
00192   void *operator new(size_t s) {
00193     return User::operator new(s, 2);
00194   }
00195   InsertValueConstantExpr(Constant *Agg, Constant *Val,
00196                           const SmallVector<unsigned, 4> &IdxList,
00197                           Type *DestTy)
00198     : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
00199       Indices(IdxList) {
00200     Op<0>() = Agg;
00201     Op<1>() = Val;
00202   }
00203 
00204   /// Indices - These identify the position for the insertion.
00205   const SmallVector<unsigned, 4> Indices;
00206 
00207   /// Transparently provide more efficient getOperand methods.
00208   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00209 };
00210 
00211 
00212 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
00213 /// used behind the scenes to implement getelementpr constant exprs.
00214 class GetElementPtrConstantExpr : public ConstantExpr {
00215   void anchor() override;
00216   GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
00217                             Type *DestTy);
00218 public:
00219   static GetElementPtrConstantExpr *Create(Constant *C,
00220                                            ArrayRef<Constant*> IdxList,
00221                                            Type *DestTy,
00222                                            unsigned Flags) {
00223     GetElementPtrConstantExpr *Result =
00224       new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
00225     Result->SubclassOptionalData = Flags;
00226     return Result;
00227   }
00228   /// Transparently provide more efficient getOperand methods.
00229   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00230 };
00231 
00232 // CompareConstantExpr - This class is private to Constants.cpp, and is used
00233 // behind the scenes to implement ICmp and FCmp constant expressions. This is
00234 // needed in order to store the predicate value for these instructions.
00235 class CompareConstantExpr : public ConstantExpr {
00236   void anchor() override;
00237   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00238 public:
00239   // allocate space for exactly two operands
00240   void *operator new(size_t s) {
00241     return User::operator new(s, 2);
00242   }
00243   unsigned short predicate;
00244   CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
00245                       unsigned short pred,  Constant* LHS, Constant* RHS)
00246     : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
00247     Op<0>() = LHS;
00248     Op<1>() = RHS;
00249   }
00250   /// Transparently provide more efficient getOperand methods.
00251   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00252 };
00253 
00254 template <>
00255 struct OperandTraits<UnaryConstantExpr> :
00256   public FixedNumOperandTraits<UnaryConstantExpr, 1> {
00257 };
00258 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
00259 
00260 template <>
00261 struct OperandTraits<BinaryConstantExpr> :
00262   public FixedNumOperandTraits<BinaryConstantExpr, 2> {
00263 };
00264 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
00265 
00266 template <>
00267 struct OperandTraits<SelectConstantExpr> :
00268   public FixedNumOperandTraits<SelectConstantExpr, 3> {
00269 };
00270 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
00271 
00272 template <>
00273 struct OperandTraits<ExtractElementConstantExpr> :
00274   public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
00275 };
00276 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
00277 
00278 template <>
00279 struct OperandTraits<InsertElementConstantExpr> :
00280   public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
00281 };
00282 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
00283 
00284 template <>
00285 struct OperandTraits<ShuffleVectorConstantExpr> :
00286     public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
00287 };
00288 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
00289 
00290 template <>
00291 struct OperandTraits<ExtractValueConstantExpr> :
00292   public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
00293 };
00294 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
00295 
00296 template <>
00297 struct OperandTraits<InsertValueConstantExpr> :
00298   public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
00299 };
00300 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
00301 
00302 template <>
00303 struct OperandTraits<GetElementPtrConstantExpr> :
00304   public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
00305 };
00306 
00307 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
00308 
00309 
00310 template <>
00311 struct OperandTraits<CompareConstantExpr> :
00312   public FixedNumOperandTraits<CompareConstantExpr, 2> {
00313 };
00314 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
00315 
00316 struct ExprMapKeyType {
00317   ExprMapKeyType(unsigned opc,
00318       ArrayRef<Constant*> ops,
00319       unsigned short flags = 0,
00320       unsigned short optionalflags = 0,
00321       ArrayRef<unsigned> inds = None)
00322         : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
00323         operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {}
00324   uint8_t opcode;
00325   uint8_t subclassoptionaldata;
00326   uint16_t subclassdata;
00327   std::vector<Constant*> operands;
00328   SmallVector<unsigned, 4> indices;
00329   bool operator==(const ExprMapKeyType& that) const {
00330     return this->opcode == that.opcode &&
00331            this->subclassdata == that.subclassdata &&
00332            this->subclassoptionaldata == that.subclassoptionaldata &&
00333            this->operands == that.operands &&
00334            this->indices == that.indices;
00335   }
00336   bool operator<(const ExprMapKeyType & that) const {
00337     return std::tie(opcode, operands, subclassdata, subclassoptionaldata,
00338                     indices) <
00339            std::tie(that.opcode, that.operands, that.subclassdata,
00340                     that.subclassoptionaldata, that.indices);
00341   }
00342 
00343   bool operator!=(const ExprMapKeyType& that) const {
00344     return !(*this == that);
00345   }
00346 };
00347 
00348 struct InlineAsmKeyType {
00349   InlineAsmKeyType(StringRef AsmString,
00350                    StringRef Constraints, bool hasSideEffects,
00351                    bool isAlignStack, InlineAsm::AsmDialect asmDialect)
00352     : asm_string(AsmString), constraints(Constraints),
00353       has_side_effects(hasSideEffects), is_align_stack(isAlignStack),
00354       asm_dialect(asmDialect) {}
00355   std::string asm_string;
00356   std::string constraints;
00357   bool has_side_effects;
00358   bool is_align_stack;
00359   InlineAsm::AsmDialect asm_dialect;
00360   bool operator==(const InlineAsmKeyType& that) const {
00361     return this->asm_string == that.asm_string &&
00362            this->constraints == that.constraints &&
00363            this->has_side_effects == that.has_side_effects &&
00364            this->is_align_stack == that.is_align_stack &&
00365            this->asm_dialect == that.asm_dialect;
00366   }
00367   bool operator<(const InlineAsmKeyType& that) const {
00368     return std::tie(asm_string, constraints, has_side_effects, is_align_stack,
00369                     asm_dialect) <
00370            std::tie(that.asm_string, that.constraints, that.has_side_effects,
00371                     that.is_align_stack, that.asm_dialect);
00372   }
00373 
00374   bool operator!=(const InlineAsmKeyType& that) const {
00375     return !(*this == that);
00376   }
00377 };
00378 
00379 // The number of operands for each ConstantCreator::create method is
00380 // determined by the ConstantTraits template.
00381 // ConstantCreator - A class that is used to create constants by
00382 // ConstantUniqueMap*.  This class should be partially specialized if there is
00383 // something strange that needs to be done to interface to the ctor for the
00384 // constant.
00385 //
00386 template<typename T, typename Alloc>
00387 struct ConstantTraits< std::vector<T, Alloc> > {
00388   static unsigned uses(const std::vector<T, Alloc>& v) {
00389     return v.size();
00390   }
00391 };
00392 
00393 template<>
00394 struct ConstantTraits<Constant *> {
00395   static unsigned uses(Constant * const & v) {
00396     return 1;
00397   }
00398 };
00399 
00400 template<class ConstantClass, class TypeClass, class ValType>
00401 struct ConstantCreator {
00402   static ConstantClass *create(TypeClass *Ty, const ValType &V) {
00403     return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
00404   }
00405 };
00406 
00407 template<class ConstantClass, class TypeClass>
00408 struct ConstantArrayCreator {
00409   static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) {
00410     return new(V.size()) ConstantClass(Ty, V);
00411   }
00412 };
00413 
00414 template<class ConstantClass>
00415 struct ConstantKeyData {
00416   typedef void ValType;
00417   static ValType getValType(ConstantClass *C) {
00418     llvm_unreachable("Unknown Constant type!");
00419   }
00420 };
00421 
00422 template<>
00423 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
00424   static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V,
00425       unsigned short pred = 0) {
00426     if (Instruction::isCast(V.opcode))
00427       return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
00428     if ((V.opcode >= Instruction::BinaryOpsBegin &&
00429          V.opcode < Instruction::BinaryOpsEnd))
00430       return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
00431                                     V.subclassoptionaldata);
00432     if (V.opcode == Instruction::Select)
00433       return new SelectConstantExpr(V.operands[0], V.operands[1], 
00434                                     V.operands[2]);
00435     if (V.opcode == Instruction::ExtractElement)
00436       return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
00437     if (V.opcode == Instruction::InsertElement)
00438       return new InsertElementConstantExpr(V.operands[0], V.operands[1],
00439                                            V.operands[2]);
00440     if (V.opcode == Instruction::ShuffleVector)
00441       return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
00442                                            V.operands[2]);
00443     if (V.opcode == Instruction::InsertValue)
00444       return new InsertValueConstantExpr(V.operands[0], V.operands[1],
00445                                          V.indices, Ty);
00446     if (V.opcode == Instruction::ExtractValue)
00447       return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
00448     if (V.opcode == Instruction::GetElementPtr) {
00449       std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
00450       return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
00451                                                V.subclassoptionaldata);
00452     }
00453 
00454     // The compare instructions are weird. We have to encode the predicate
00455     // value and it is combined with the instruction opcode by multiplying
00456     // the opcode by one hundred. We must decode this to get the predicate.
00457     if (V.opcode == Instruction::ICmp)
00458       return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
00459                                      V.operands[0], V.operands[1]);
00460     if (V.opcode == Instruction::FCmp) 
00461       return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
00462                                      V.operands[0], V.operands[1]);
00463     llvm_unreachable("Invalid ConstantExpr!");
00464   }
00465 };
00466 
00467 template<>
00468 struct ConstantKeyData<ConstantExpr> {
00469   typedef ExprMapKeyType ValType;
00470   static ValType getValType(ConstantExpr *CE) {
00471     std::vector<Constant*> Operands;
00472     Operands.reserve(CE->getNumOperands());
00473     for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
00474       Operands.push_back(cast<Constant>(CE->getOperand(i)));
00475     return ExprMapKeyType(CE->getOpcode(), Operands,
00476         CE->isCompare() ? CE->getPredicate() : 0,
00477         CE->getRawSubclassOptionalData(),
00478         CE->hasIndices() ?
00479           CE->getIndices() : ArrayRef<unsigned>());
00480   }
00481 };
00482 
00483 template<>
00484 struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
00485   static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
00486     return new InlineAsm(Ty, Key.asm_string, Key.constraints,
00487                          Key.has_side_effects, Key.is_align_stack,
00488                          Key.asm_dialect);
00489   }
00490 };
00491 
00492 template<>
00493 struct ConstantKeyData<InlineAsm> {
00494   typedef InlineAsmKeyType ValType;
00495   static ValType getValType(InlineAsm *Asm) {
00496     return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
00497                             Asm->hasSideEffects(), Asm->isAlignStack(),
00498                             Asm->getDialect());
00499   }
00500 };
00501 
00502 template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
00503          bool HasLargeKey = false /*true for arrays and structs*/ >
00504 class ConstantUniqueMap {
00505 public:
00506   typedef std::pair<TypeClass*, ValType> MapKey;
00507   typedef std::map<MapKey, ConstantClass *> MapTy;
00508   typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy;
00509 private:
00510   /// Map - This is the main map from the element descriptor to the Constants.
00511   /// This is the primary way we avoid creating two of the same shape
00512   /// constant.
00513   MapTy Map;
00514     
00515   /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
00516   /// from the constants to their element in Map.  This is important for
00517   /// removal of constants from the array, which would otherwise have to scan
00518   /// through the map with very large keys.
00519   InverseMapTy InverseMap;
00520 
00521 public:
00522   typename MapTy::iterator map_begin() { return Map.begin(); }
00523   typename MapTy::iterator map_end() { return Map.end(); }
00524 
00525   void freeConstants() {
00526     for (typename MapTy::iterator I=Map.begin(), E=Map.end();
00527          I != E; ++I) {
00528       // Asserts that use_empty().
00529       delete I->second;
00530     }
00531   }
00532     
00533   /// InsertOrGetItem - Return an iterator for the specified element.
00534   /// If the element exists in the map, the returned iterator points to the
00535   /// entry and Exists=true.  If not, the iterator points to the newly
00536   /// inserted entry and returns Exists=false.  Newly inserted entries have
00537   /// I->second == 0, and should be filled in.
00538   typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
00539                                  &InsertVal,
00540                                  bool &Exists) {
00541     std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
00542     Exists = !IP.second;
00543     return IP.first;
00544   }
00545     
00546 private:
00547   typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
00548     if (HasLargeKey) {
00549       typename InverseMapTy::iterator IMI = InverseMap.find(CP);
00550       assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
00551              IMI->second->second == CP &&
00552              "InverseMap corrupt!");
00553       return IMI->second;
00554     }
00555       
00556     typename MapTy::iterator I =
00557       Map.find(MapKey(static_cast<TypeClass*>(CP->getType()),
00558                       ConstantKeyData<ConstantClass>::getValType(CP)));
00559     if (I == Map.end() || I->second != CP) {
00560       // FIXME: This should not use a linear scan.  If this gets to be a
00561       // performance problem, someone should look at this.
00562       for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
00563         /* empty */;
00564     }
00565     return I;
00566   }
00567 
00568   ConstantClass *Create(TypeClass *Ty, ValRefType V,
00569                         typename MapTy::iterator I) {
00570     ConstantClass* Result =
00571       ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
00572 
00573     assert(Result->getType() == Ty && "Type specified is not correct!");
00574     I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
00575 
00576     if (HasLargeKey)  // Remember the reverse mapping if needed.
00577       InverseMap.insert(std::make_pair(Result, I));
00578 
00579     return Result;
00580   }
00581 public:
00582     
00583   /// getOrCreate - Return the specified constant from the map, creating it if
00584   /// necessary.
00585   ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) {
00586     MapKey Lookup(Ty, V);
00587     ConstantClass* Result = 0;
00588     
00589     typename MapTy::iterator I = Map.find(Lookup);
00590     // Is it in the map?  
00591     if (I != Map.end())
00592       Result = I->second;
00593         
00594     if (!Result) {
00595       // If no preexisting value, create one now...
00596       Result = Create(Ty, V, I);
00597     }
00598         
00599     return Result;
00600   }
00601 
00602   void remove(ConstantClass *CP) {
00603     typename MapTy::iterator I = FindExistingElement(CP);
00604     assert(I != Map.end() && "Constant not found in constant table!");
00605     assert(I->second == CP && "Didn't find correct element?");
00606 
00607     if (HasLargeKey)  // Remember the reverse mapping if needed.
00608       InverseMap.erase(CP);
00609 
00610     Map.erase(I);
00611   }
00612 
00613   /// MoveConstantToNewSlot - If we are about to change C to be the element
00614   /// specified by I, update our internal data structures to reflect this
00615   /// fact.
00616   void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
00617     // First, remove the old location of the specified constant in the map.
00618     typename MapTy::iterator OldI = FindExistingElement(C);
00619     assert(OldI != Map.end() && "Constant not found in constant table!");
00620     assert(OldI->second == C && "Didn't find correct element?");
00621       
00622      // Remove the old entry from the map.
00623     Map.erase(OldI);
00624     
00625     // Update the inverse map so that we know that this constant is now
00626     // located at descriptor I.
00627     if (HasLargeKey) {
00628       assert(I->second == C && "Bad inversemap entry!");
00629       InverseMap[C] = I;
00630     }
00631   }
00632 
00633   void dump() const {
00634     DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
00635   }
00636 };
00637 
00638 // Unique map for aggregate constants
00639 template<class TypeClass, class ConstantClass>
00640 class ConstantAggrUniqueMap {
00641 public:
00642   typedef ArrayRef<Constant*> Operands;
00643   typedef std::pair<TypeClass*, Operands> LookupKey;
00644 private:
00645   struct MapInfo {
00646     typedef DenseMapInfo<ConstantClass*> ConstantClassInfo;
00647     typedef DenseMapInfo<Constant*> ConstantInfo;
00648     typedef DenseMapInfo<TypeClass*> TypeClassInfo;
00649     static inline ConstantClass* getEmptyKey() {
00650       return ConstantClassInfo::getEmptyKey();
00651     }
00652     static inline ConstantClass* getTombstoneKey() {
00653       return ConstantClassInfo::getTombstoneKey();
00654     }
00655     static unsigned getHashValue(const ConstantClass *CP) {
00656       SmallVector<Constant*, 8> CPOperands;
00657       CPOperands.reserve(CP->getNumOperands());
00658       for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I)
00659         CPOperands.push_back(CP->getOperand(I));
00660       return getHashValue(LookupKey(CP->getType(), CPOperands));
00661     }
00662     static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
00663       return LHS == RHS;
00664     }
00665     static unsigned getHashValue(const LookupKey &Val) {
00666       return hash_combine(Val.first, hash_combine_range(Val.second.begin(),
00667                                                         Val.second.end()));
00668     }
00669     static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
00670       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00671         return false;
00672       if (LHS.first != RHS->getType()
00673           || LHS.second.size() != RHS->getNumOperands())
00674         return false;
00675       for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) {
00676         if (LHS.second[I] != RHS->getOperand(I))
00677           return false;
00678       }
00679       return true;
00680     }
00681   };
00682 public:
00683   typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
00684 
00685 private:
00686   /// Map - This is the main map from the element descriptor to the Constants.
00687   /// This is the primary way we avoid creating two of the same shape
00688   /// constant.
00689   MapTy Map;
00690 
00691 public:
00692   typename MapTy::iterator map_begin() { return Map.begin(); }
00693   typename MapTy::iterator map_end() { return Map.end(); }
00694 
00695   void freeConstants() {
00696     for (typename MapTy::iterator I=Map.begin(), E=Map.end();
00697          I != E; ++I) {
00698       // Asserts that use_empty().
00699       delete I->first;
00700     }
00701   }
00702 
00703 private:
00704   typename MapTy::iterator findExistingElement(ConstantClass *CP) {
00705     return Map.find(CP);
00706   }
00707 
00708   ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) {
00709     ConstantClass* Result =
00710       ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V);
00711 
00712     assert(Result->getType() == Ty && "Type specified is not correct!");
00713     Map[Result] = '\0';
00714 
00715     return Result;
00716   }
00717 public:
00718 
00719   /// getOrCreate - Return the specified constant from the map, creating it if
00720   /// necessary.
00721   ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
00722     LookupKey Lookup(Ty, V);
00723     ConstantClass* Result = 0;
00724 
00725     typename MapTy::iterator I = Map.find_as(Lookup);
00726     // Is it in the map?
00727     if (I != Map.end())
00728       Result = I->first;
00729 
00730     if (!Result) {
00731       // If no preexisting value, create one now...
00732       Result = Create(Ty, V, I);
00733     }
00734 
00735     return Result;
00736   }
00737 
00738   /// Find the constant by lookup key.
00739   typename MapTy::iterator find(LookupKey Lookup) {
00740     return Map.find_as(Lookup);
00741   }
00742 
00743   /// Insert the constant into its proper slot.
00744   void insert(ConstantClass *CP) {
00745     Map[CP] = '\0';
00746   }
00747 
00748   /// Remove this constant from the map
00749   void remove(ConstantClass *CP) {
00750     typename MapTy::iterator I = findExistingElement(CP);
00751     assert(I != Map.end() && "Constant not found in constant table!");
00752     assert(I->first == CP && "Didn't find correct element?");
00753     Map.erase(I);
00754   }
00755 
00756   void dump() const {
00757     DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
00758   }
00759 };
00760 
00761 }
00762 
00763 #endif