LLVM  mainline
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_LIB_IR_CONSTANTSCONTEXT_H
00016 #define LLVM_LIB_IR_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 
00033 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
00034 /// behind the scenes to implement unary constant exprs.
00035 class UnaryConstantExpr : public ConstantExpr {
00036   void anchor() override;
00037   void *operator new(size_t, unsigned) = delete;
00038 public:
00039   // allocate space for exactly one operand
00040   void *operator new(size_t s) {
00041     return User::operator new(s, 1);
00042   }
00043   UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
00044     : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
00045     Op<0>() = C;
00046   }
00047   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00048 };
00049 
00050 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
00051 /// behind the scenes to implement binary constant exprs.
00052 class BinaryConstantExpr : public ConstantExpr {
00053   void anchor() override;
00054   void *operator new(size_t, unsigned) = delete;
00055 public:
00056   // allocate space for exactly two operands
00057   void *operator new(size_t s) {
00058     return User::operator new(s, 2);
00059   }
00060   BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
00061                      unsigned Flags)
00062     : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
00063     Op<0>() = C1;
00064     Op<1>() = C2;
00065     SubclassOptionalData = Flags;
00066   }
00067   /// Transparently provide more efficient getOperand methods.
00068   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00069 };
00070 
00071 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
00072 /// behind the scenes to implement select constant exprs.
00073 class SelectConstantExpr : public ConstantExpr {
00074   void anchor() override;
00075   void *operator new(size_t, unsigned) = delete;
00076 public:
00077   // allocate space for exactly three operands
00078   void *operator new(size_t s) {
00079     return User::operator new(s, 3);
00080   }
00081   SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
00082     : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
00083     Op<0>() = C1;
00084     Op<1>() = C2;
00085     Op<2>() = C3;
00086   }
00087   /// Transparently provide more efficient getOperand methods.
00088   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00089 };
00090 
00091 /// ExtractElementConstantExpr - This class is private to
00092 /// Constants.cpp, and is used behind the scenes to implement
00093 /// extractelement constant exprs.
00094 class ExtractElementConstantExpr : public ConstantExpr {
00095   void anchor() override;
00096   void *operator new(size_t, unsigned) = delete;
00097 public:
00098   // allocate space for exactly two operands
00099   void *operator new(size_t s) {
00100     return User::operator new(s, 2);
00101   }
00102   ExtractElementConstantExpr(Constant *C1, Constant *C2)
00103     : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
00104                    Instruction::ExtractElement, &Op<0>(), 2) {
00105     Op<0>() = C1;
00106     Op<1>() = C2;
00107   }
00108   /// Transparently provide more efficient getOperand methods.
00109   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00110 };
00111 
00112 /// InsertElementConstantExpr - This class is private to
00113 /// Constants.cpp, and is used behind the scenes to implement
00114 /// insertelement constant exprs.
00115 class InsertElementConstantExpr : public ConstantExpr {
00116   void anchor() override;
00117   void *operator new(size_t, unsigned) = delete;
00118 public:
00119   // allocate space for exactly three operands
00120   void *operator new(size_t s) {
00121     return User::operator new(s, 3);
00122   }
00123   InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
00124     : ConstantExpr(C1->getType(), Instruction::InsertElement,
00125                    &Op<0>(), 3) {
00126     Op<0>() = C1;
00127     Op<1>() = C2;
00128     Op<2>() = C3;
00129   }
00130   /// Transparently provide more efficient getOperand methods.
00131   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00132 };
00133 
00134 /// ShuffleVectorConstantExpr - This class is private to
00135 /// Constants.cpp, and is used behind the scenes to implement
00136 /// shufflevector constant exprs.
00137 class ShuffleVectorConstantExpr : public ConstantExpr {
00138   void anchor() override;
00139   void *operator new(size_t, unsigned) = delete;
00140 public:
00141   // allocate space for exactly three operands
00142   void *operator new(size_t s) {
00143     return User::operator new(s, 3);
00144   }
00145   ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
00146   : ConstantExpr(VectorType::get(
00147                    cast<VectorType>(C1->getType())->getElementType(),
00148                    cast<VectorType>(C3->getType())->getNumElements()),
00149                  Instruction::ShuffleVector,
00150                  &Op<0>(), 3) {
00151     Op<0>() = C1;
00152     Op<1>() = C2;
00153     Op<2>() = C3;
00154   }
00155   /// Transparently provide more efficient getOperand methods.
00156   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00157 };
00158 
00159 /// ExtractValueConstantExpr - This class is private to
00160 /// Constants.cpp, and is used behind the scenes to implement
00161 /// extractvalue constant exprs.
00162 class ExtractValueConstantExpr : public ConstantExpr {
00163   void anchor() override;
00164   void *operator new(size_t, unsigned) = delete;
00165 public:
00166   // allocate space for exactly one operand
00167   void *operator new(size_t s) {
00168     return User::operator new(s, 1);
00169   }
00170   ExtractValueConstantExpr(Constant *Agg, ArrayRef<unsigned> IdxList,
00171                            Type *DestTy)
00172       : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
00173         Indices(IdxList.begin(), IdxList.end()) {
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   static bool classof(const ConstantExpr *CE) {
00184     return CE->getOpcode() == Instruction::ExtractValue;
00185   }
00186   static bool classof(const Value *V) {
00187     return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
00188   }
00189 };
00190 
00191 /// InsertValueConstantExpr - This class is private to
00192 /// Constants.cpp, and is used behind the scenes to implement
00193 /// insertvalue constant exprs.
00194 class InsertValueConstantExpr : public ConstantExpr {
00195   void anchor() override;
00196   void *operator new(size_t, unsigned) = delete;
00197 public:
00198   // allocate space for exactly one operand
00199   void *operator new(size_t s) {
00200     return User::operator new(s, 2);
00201   }
00202   InsertValueConstantExpr(Constant *Agg, Constant *Val,
00203                           ArrayRef<unsigned> IdxList, Type *DestTy)
00204       : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
00205         Indices(IdxList.begin(), IdxList.end()) {
00206     Op<0>() = Agg;
00207     Op<1>() = Val;
00208   }
00209 
00210   /// Indices - These identify the position for the insertion.
00211   const SmallVector<unsigned, 4> Indices;
00212 
00213   /// Transparently provide more efficient getOperand methods.
00214   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00215 
00216   static bool classof(const ConstantExpr *CE) {
00217     return CE->getOpcode() == Instruction::InsertValue;
00218   }
00219   static bool classof(const Value *V) {
00220     return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
00221   }
00222 };
00223 
00224 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
00225 /// used behind the scenes to implement getelementpr constant exprs.
00226 class GetElementPtrConstantExpr : public ConstantExpr {
00227   Type *SrcElementTy;
00228   Type *ResElementTy;
00229   void anchor() override;
00230   GetElementPtrConstantExpr(Type *SrcElementTy, Constant *C,
00231                             ArrayRef<Constant *> IdxList, Type *DestTy);
00232 
00233 public:
00234   static GetElementPtrConstantExpr *Create(Type *SrcElementTy, Constant *C,
00235                                            ArrayRef<Constant *> IdxList,
00236                                            Type *DestTy, unsigned Flags) {
00237     GetElementPtrConstantExpr *Result = new (IdxList.size() + 1)
00238         GetElementPtrConstantExpr(SrcElementTy, C, IdxList, DestTy);
00239     Result->SubclassOptionalData = Flags;
00240     return Result;
00241   }
00242   Type *getSourceElementType() const;
00243   Type *getResultElementType() const;
00244   /// Transparently provide more efficient getOperand methods.
00245   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00246 
00247   static bool classof(const ConstantExpr *CE) {
00248     return CE->getOpcode() == Instruction::GetElementPtr;
00249   }
00250   static bool classof(const Value *V) {
00251     return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
00252   }
00253 };
00254 
00255 // CompareConstantExpr - This class is private to Constants.cpp, and is used
00256 // behind the scenes to implement ICmp and FCmp constant expressions. This is
00257 // needed in order to store the predicate value for these instructions.
00258 class CompareConstantExpr : public ConstantExpr {
00259   void anchor() override;
00260   void *operator new(size_t, unsigned) = delete;
00261 public:
00262   // allocate space for exactly two operands
00263   void *operator new(size_t s) {
00264     return User::operator new(s, 2);
00265   }
00266   unsigned short predicate;
00267   CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
00268                       unsigned short pred,  Constant* LHS, Constant* RHS)
00269     : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
00270     Op<0>() = LHS;
00271     Op<1>() = RHS;
00272   }
00273   /// Transparently provide more efficient getOperand methods.
00274   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00275 
00276   static bool classof(const ConstantExpr *CE) {
00277     return CE->getOpcode() == Instruction::ICmp ||
00278            CE->getOpcode() == Instruction::FCmp;
00279   }
00280   static bool classof(const Value *V) {
00281     return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
00282   }
00283 };
00284 
00285 template <>
00286 struct OperandTraits<UnaryConstantExpr>
00287     : public FixedNumOperandTraits<UnaryConstantExpr, 1> {};
00288 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
00289 
00290 template <>
00291 struct OperandTraits<BinaryConstantExpr>
00292     : public FixedNumOperandTraits<BinaryConstantExpr, 2> {};
00293 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
00294 
00295 template <>
00296 struct OperandTraits<SelectConstantExpr>
00297     : public FixedNumOperandTraits<SelectConstantExpr, 3> {};
00298 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
00299 
00300 template <>
00301 struct OperandTraits<ExtractElementConstantExpr>
00302     : public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {};
00303 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
00304 
00305 template <>
00306 struct OperandTraits<InsertElementConstantExpr>
00307     : public FixedNumOperandTraits<InsertElementConstantExpr, 3> {};
00308 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
00309 
00310 template <>
00311 struct OperandTraits<ShuffleVectorConstantExpr>
00312     : public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {};
00313 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
00314 
00315 template <>
00316 struct OperandTraits<ExtractValueConstantExpr>
00317     : public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {};
00318 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
00319 
00320 template <>
00321 struct OperandTraits<InsertValueConstantExpr>
00322     : public FixedNumOperandTraits<InsertValueConstantExpr, 2> {};
00323 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
00324 
00325 template <>
00326 struct OperandTraits<GetElementPtrConstantExpr>
00327     : public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {};
00328 
00329 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
00330 
00331 template <>
00332 struct OperandTraits<CompareConstantExpr>
00333     : public FixedNumOperandTraits<CompareConstantExpr, 2> {};
00334 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
00335 
00336 template <class ConstantClass> struct ConstantAggrKeyType;
00337 struct InlineAsmKeyType;
00338 struct ConstantExprKeyType;
00339 
00340 template <class ConstantClass> struct ConstantInfo;
00341 template <> struct ConstantInfo<ConstantExpr> {
00342   typedef ConstantExprKeyType ValType;
00343   typedef Type TypeClass;
00344 };
00345 template <> struct ConstantInfo<InlineAsm> {
00346   typedef InlineAsmKeyType ValType;
00347   typedef PointerType TypeClass;
00348 };
00349 template <> struct ConstantInfo<ConstantArray> {
00350   typedef ConstantAggrKeyType<ConstantArray> ValType;
00351   typedef ArrayType TypeClass;
00352 };
00353 template <> struct ConstantInfo<ConstantStruct> {
00354   typedef ConstantAggrKeyType<ConstantStruct> ValType;
00355   typedef StructType TypeClass;
00356 };
00357 template <> struct ConstantInfo<ConstantVector> {
00358   typedef ConstantAggrKeyType<ConstantVector> ValType;
00359   typedef VectorType TypeClass;
00360 };
00361 
00362 template <class ConstantClass> struct ConstantAggrKeyType {
00363   ArrayRef<Constant *> Operands;
00364   ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {}
00365   ConstantAggrKeyType(ArrayRef<Constant *> Operands, const ConstantClass *)
00366       : Operands(Operands) {}
00367   ConstantAggrKeyType(const ConstantClass *C,
00368                       SmallVectorImpl<Constant *> &Storage) {
00369     assert(Storage.empty() && "Expected empty storage");
00370     for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
00371       Storage.push_back(C->getOperand(I));
00372     Operands = Storage;
00373   }
00374 
00375   bool operator==(const ConstantAggrKeyType &X) const {
00376     return Operands == X.Operands;
00377   }
00378   bool operator==(const ConstantClass *C) const {
00379     if (Operands.size() != C->getNumOperands())
00380       return false;
00381     for (unsigned I = 0, E = Operands.size(); I != E; ++I)
00382       if (Operands[I] != C->getOperand(I))
00383         return false;
00384     return true;
00385   }
00386   unsigned getHash() const {
00387     return hash_combine_range(Operands.begin(), Operands.end());
00388   }
00389 
00390   typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
00391   ConstantClass *create(TypeClass *Ty) const {
00392     return new (Operands.size()) ConstantClass(Ty, Operands);
00393   }
00394 };
00395 
00396 struct InlineAsmKeyType {
00397   StringRef AsmString;
00398   StringRef Constraints;
00399   FunctionType *FTy;
00400   bool HasSideEffects;
00401   bool IsAlignStack;
00402   InlineAsm::AsmDialect AsmDialect;
00403 
00404   InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
00405                    FunctionType *FTy, bool HasSideEffects, bool IsAlignStack,
00406                    InlineAsm::AsmDialect AsmDialect)
00407       : AsmString(AsmString), Constraints(Constraints), FTy(FTy),
00408         HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
00409         AsmDialect(AsmDialect) {}
00410   InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &)
00411       : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
00412         FTy(Asm->getFunctionType()), HasSideEffects(Asm->hasSideEffects()),
00413         IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {}
00414 
00415   bool operator==(const InlineAsmKeyType &X) const {
00416     return HasSideEffects == X.HasSideEffects &&
00417            IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
00418            AsmString == X.AsmString && Constraints == X.Constraints &&
00419            FTy == X.FTy;
00420   }
00421   bool operator==(const InlineAsm *Asm) const {
00422     return HasSideEffects == Asm->hasSideEffects() &&
00423            IsAlignStack == Asm->isAlignStack() &&
00424            AsmDialect == Asm->getDialect() &&
00425            AsmString == Asm->getAsmString() &&
00426            Constraints == Asm->getConstraintString() &&
00427            FTy == Asm->getFunctionType();
00428   }
00429   unsigned getHash() const {
00430     return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
00431                         AsmDialect, FTy);
00432   }
00433 
00434   typedef ConstantInfo<InlineAsm>::TypeClass TypeClass;
00435   InlineAsm *create(TypeClass *Ty) const {
00436     assert(PointerType::getUnqual(FTy) == Ty);
00437     return new InlineAsm(FTy, AsmString, Constraints, HasSideEffects,
00438                          IsAlignStack, AsmDialect);
00439   }
00440 };
00441 
00442 struct ConstantExprKeyType {
00443   uint8_t Opcode;
00444   uint8_t SubclassOptionalData;
00445   uint16_t SubclassData;
00446   ArrayRef<Constant *> Ops;
00447   ArrayRef<unsigned> Indexes;
00448   Type *ExplicitTy;
00449 
00450   ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops,
00451                       unsigned short SubclassData = 0,
00452                       unsigned short SubclassOptionalData = 0,
00453                       ArrayRef<unsigned> Indexes = None,
00454                       Type *ExplicitTy = nullptr)
00455       : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
00456         SubclassData(SubclassData), Ops(Ops), Indexes(Indexes),
00457         ExplicitTy(ExplicitTy) {}
00458   ConstantExprKeyType(ArrayRef<Constant *> Operands, const ConstantExpr *CE)
00459       : Opcode(CE->getOpcode()),
00460         SubclassOptionalData(CE->getRawSubclassOptionalData()),
00461         SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands),
00462         Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {}
00463   ConstantExprKeyType(const ConstantExpr *CE,
00464                       SmallVectorImpl<Constant *> &Storage)
00465       : Opcode(CE->getOpcode()),
00466         SubclassOptionalData(CE->getRawSubclassOptionalData()),
00467         SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
00468         Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {
00469     assert(Storage.empty() && "Expected empty storage");
00470     for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
00471       Storage.push_back(CE->getOperand(I));
00472     Ops = Storage;
00473   }
00474 
00475   bool operator==(const ConstantExprKeyType &X) const {
00476     return Opcode == X.Opcode && SubclassData == X.SubclassData &&
00477            SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
00478            Indexes == X.Indexes;
00479   }
00480 
00481   bool operator==(const ConstantExpr *CE) const {
00482     if (Opcode != CE->getOpcode())
00483       return false;
00484     if (SubclassOptionalData != CE->getRawSubclassOptionalData())
00485       return false;
00486     if (Ops.size() != CE->getNumOperands())
00487       return false;
00488     if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
00489       return false;
00490     for (unsigned I = 0, E = Ops.size(); I != E; ++I)
00491       if (Ops[I] != CE->getOperand(I))
00492         return false;
00493     if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()))
00494       return false;
00495     return true;
00496   }
00497 
00498   unsigned getHash() const {
00499     return hash_combine(Opcode, SubclassOptionalData, SubclassData,
00500                         hash_combine_range(Ops.begin(), Ops.end()),
00501                         hash_combine_range(Indexes.begin(), Indexes.end()));
00502   }
00503 
00504   typedef ConstantInfo<ConstantExpr>::TypeClass TypeClass;
00505   ConstantExpr *create(TypeClass *Ty) const {
00506     switch (Opcode) {
00507     default:
00508       if (Instruction::isCast(Opcode))
00509         return new UnaryConstantExpr(Opcode, Ops[0], Ty);
00510       if ((Opcode >= Instruction::BinaryOpsBegin &&
00511            Opcode < Instruction::BinaryOpsEnd))
00512         return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
00513                                       SubclassOptionalData);
00514       llvm_unreachable("Invalid ConstantExpr!");
00515     case Instruction::Select:
00516       return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
00517     case Instruction::ExtractElement:
00518       return new ExtractElementConstantExpr(Ops[0], Ops[1]);
00519     case Instruction::InsertElement:
00520       return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
00521     case Instruction::ShuffleVector:
00522       return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]);
00523     case Instruction::InsertValue:
00524       return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
00525     case Instruction::ExtractValue:
00526       return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
00527     case Instruction::GetElementPtr:
00528       return GetElementPtrConstantExpr::Create(
00529           ExplicitTy ? ExplicitTy
00530                      : cast<PointerType>(Ops[0]->getType()->getScalarType())
00531                            ->getElementType(),
00532           Ops[0], Ops.slice(1), Ty, SubclassOptionalData);
00533     case Instruction::ICmp:
00534       return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
00535                                      Ops[0], Ops[1]);
00536     case Instruction::FCmp:
00537       return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
00538                                      Ops[0], Ops[1]);
00539     }
00540   }
00541 };
00542 
00543 template <class ConstantClass> class ConstantUniqueMap {
00544 public:
00545   typedef typename ConstantInfo<ConstantClass>::ValType ValType;
00546   typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
00547   typedef std::pair<TypeClass *, ValType> LookupKey;
00548 
00549 private:
00550   struct MapInfo {
00551     typedef DenseMapInfo<ConstantClass *> ConstantClassInfo;
00552     static inline ConstantClass *getEmptyKey() {
00553       return ConstantClassInfo::getEmptyKey();
00554     }
00555     static inline ConstantClass *getTombstoneKey() {
00556       return ConstantClassInfo::getTombstoneKey();
00557     }
00558     static unsigned getHashValue(const ConstantClass *CP) {
00559       SmallVector<Constant *, 8> Storage;
00560       return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
00561     }
00562     static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
00563       return LHS == RHS;
00564     }
00565     static unsigned getHashValue(const LookupKey &Val) {
00566       return hash_combine(Val.first, Val.second.getHash());
00567     }
00568     static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
00569       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00570         return false;
00571       if (LHS.first != RHS->getType())
00572         return false;
00573       return LHS.second == RHS;
00574     }
00575   };
00576 
00577 public:
00578   typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
00579 
00580 private:
00581   MapTy Map;
00582 
00583 public:
00584   typename MapTy::iterator map_begin() { return Map.begin(); }
00585   typename MapTy::iterator map_end() { return Map.end(); }
00586 
00587   void freeConstants() {
00588     for (auto &I : Map)
00589       // Asserts that use_empty().
00590       delete I.first;
00591   }
00592 
00593 private:
00594   ConstantClass *create(TypeClass *Ty, ValType V) {
00595     ConstantClass *Result = V.create(Ty);
00596 
00597     assert(Result->getType() == Ty && "Type specified is not correct!");
00598     insert(Result);
00599 
00600     return Result;
00601   }
00602 
00603 public:
00604   /// Return the specified constant from the map, creating it if necessary.
00605   ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
00606     LookupKey Lookup(Ty, V);
00607     ConstantClass *Result = nullptr;
00608 
00609     auto I = find(Lookup);
00610     if (I == Map.end())
00611       Result = create(Ty, V);
00612     else
00613       Result = I->first;
00614     assert(Result && "Unexpected nullptr");
00615 
00616     return Result;
00617   }
00618 
00619   /// Find the constant by lookup key.
00620   typename MapTy::iterator find(LookupKey Lookup) {
00621     return Map.find_as(Lookup);
00622   }
00623 
00624   /// Insert the constant into its proper slot.
00625   void insert(ConstantClass *CP) { Map[CP] = '\0'; }
00626 
00627   /// Remove this constant from the map
00628   void remove(ConstantClass *CP) {
00629     typename MapTy::iterator I = Map.find(CP);
00630     assert(I != Map.end() && "Constant not found in constant table!");
00631     assert(I->first == CP && "Didn't find correct element?");
00632     Map.erase(I);
00633   }
00634 
00635   ConstantClass *replaceOperandsInPlace(ArrayRef<Constant *> Operands,
00636                                         ConstantClass *CP, Value *From,
00637                                         Constant *To, unsigned NumUpdated = 0,
00638                                         unsigned OperandNo = ~0u) {
00639     LookupKey Lookup(CP->getType(), ValType(Operands, CP));
00640     auto I = find(Lookup);
00641     if (I != Map.end())
00642       return I->first;
00643 
00644     // Update to the new value.  Optimize for the case when we have a single
00645     // operand that we're changing, but handle bulk updates efficiently.
00646     remove(CP);
00647     if (NumUpdated == 1) {
00648       assert(OperandNo < CP->getNumOperands() && "Invalid index");
00649       assert(CP->getOperand(OperandNo) != To && "I didn't contain From!");
00650       CP->setOperand(OperandNo, To);
00651     } else {
00652       for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I)
00653         if (CP->getOperand(I) == From)
00654           CP->setOperand(I, To);
00655     }
00656     insert(CP);
00657     return nullptr;
00658   }
00659 
00660   void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
00661 };
00662 
00663 } // end namespace llvm
00664 
00665 #endif