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