X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FIR%2FConstantsContext.h;h=571dec2e0f6aaf10deccb0d2566cf11792768eef;hb=95d71357275a05a4bfc346f5b513688e26531a91;hp=bb43f55f2c7fdfec97d616d5232eebe42d601111;hpb=f2534dd8b9335fe82b22039c5973377e7db6ef10;p=oota-llvm.git diff --git a/lib/IR/ConstantsContext.h b/lib/IR/ConstantsContext.h index bb43f55f2c7..571dec2e0f6 100644 --- a/lib/IR/ConstantsContext.h +++ b/lib/IR/ConstantsContext.h @@ -29,8 +29,6 @@ #define DEBUG_TYPE "ir" namespace llvm { -template -struct ConstantTraits; /// UnaryConstantExpr - This class is private to Constants.cpp, and is used /// behind the scenes to implement unary constant exprs. @@ -314,6 +312,7 @@ struct OperandTraits : }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value) +template struct ConstantAggrKeyType; struct InlineAsmKeyType; struct ConstantExprKeyType; @@ -326,6 +325,52 @@ template <> struct ConstantInfo { typedef InlineAsmKeyType ValType; typedef PointerType TypeClass; }; +template <> struct ConstantInfo { + typedef ConstantAggrKeyType ValType; + typedef ArrayType TypeClass; +}; +template <> struct ConstantInfo { + typedef ConstantAggrKeyType ValType; + typedef StructType TypeClass; +}; +template <> struct ConstantInfo { + typedef ConstantAggrKeyType ValType; + typedef VectorType TypeClass; +}; + +template struct ConstantAggrKeyType { + ArrayRef Operands; + ConstantAggrKeyType(ArrayRef Operands) : Operands(Operands) {} + ConstantAggrKeyType(ArrayRef Operands, const ConstantClass *) + : Operands(Operands) {} + ConstantAggrKeyType(const ConstantClass *C, + SmallVectorImpl &Storage) { + assert(Storage.empty() && "Expected empty storage"); + for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I) + Storage.push_back(C->getOperand(I)); + Operands = Storage; + } + + bool operator==(const ConstantAggrKeyType &X) const { + return Operands == X.Operands; + } + bool operator==(const ConstantClass *C) const { + if (Operands.size() != C->getNumOperands()) + return false; + for (unsigned I = 0, E = Operands.size(); I != E; ++I) + if (Operands[I] != C->getOperand(I)) + return false; + return true; + } + unsigned getHash() const { + return hash_combine_range(Operands.begin(), Operands.end()); + } + + typedef typename ConstantInfo::TypeClass TypeClass; + ConstantClass *create(TypeClass *Ty) const { + return new (Operands.size()) ConstantClass(Ty, Operands); + } +}; struct InlineAsmKeyType { StringRef AsmString; @@ -382,6 +427,11 @@ struct ConstantExprKeyType { ArrayRef Indexes = None) : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData), SubclassData(SubclassData), Ops(Ops), Indexes(Indexes) {} + ConstantExprKeyType(ArrayRef Operands, const ConstantExpr *CE) + : Opcode(CE->getOpcode()), + SubclassOptionalData(CE->getRawSubclassOptionalData()), + SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands), + Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef()) {} ConstantExprKeyType(const ConstantExpr *CE, SmallVectorImpl &Storage) : Opcode(CE->getOpcode()), @@ -459,41 +509,6 @@ struct ConstantExprKeyType { } }; -// The number of operands for each ConstantCreator::create method is -// determined by the ConstantTraits template. -// ConstantCreator - A class that is used to create constants by -// ConstantUniqueMap*. This class should be partially specialized if there is -// something strange that needs to be done to interface to the ctor for the -// constant. -// -template -struct ConstantTraits< std::vector > { - static unsigned uses(const std::vector& v) { - return v.size(); - } -}; - -template<> -struct ConstantTraits { - static unsigned uses(Constant * const & v) { - return 1; - } -}; - -template -struct ConstantCreator { - static ConstantClass *create(TypeClass *Ty, const ValType &V) { - return new(ConstantTraits::uses(V)) ConstantClass(Ty, V); - } -}; - -template -struct ConstantArrayCreator { - static ConstantClass *create(TypeClass *Ty, ArrayRef V) { - return new(V.size()) ConstantClass(Ty, V); - } -}; - template class ConstantUniqueMap { public: typedef typename ConstantInfo::ValType ValType; @@ -586,130 +601,32 @@ public: Map.erase(I); } - void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); } -}; - -// Unique map for aggregate constants -template -class ConstantAggrUniqueMap { -public: - typedef ArrayRef Operands; - typedef std::pair LookupKey; -private: - struct MapInfo { - typedef DenseMapInfo ConstantClassInfo; - typedef DenseMapInfo ConstantInfo; - typedef DenseMapInfo TypeClassInfo; - static inline ConstantClass* getEmptyKey() { - return ConstantClassInfo::getEmptyKey(); - } - static inline ConstantClass* getTombstoneKey() { - return ConstantClassInfo::getTombstoneKey(); - } - static unsigned getHashValue(const ConstantClass *CP) { - SmallVector CPOperands; - CPOperands.reserve(CP->getNumOperands()); - for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I) - CPOperands.push_back(CP->getOperand(I)); - return getHashValue(LookupKey(CP->getType(), CPOperands)); - } - static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) { - return LHS == RHS; - } - static unsigned getHashValue(const LookupKey &Val) { - return hash_combine(Val.first, hash_combine_range(Val.second.begin(), - Val.second.end())); - } - static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) { - if (RHS == getEmptyKey() || RHS == getTombstoneKey()) - return false; - if (LHS.first != RHS->getType() - || LHS.second.size() != RHS->getNumOperands()) - return false; - for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) { - if (LHS.second[I] != RHS->getOperand(I)) - return false; - } - return true; - } - }; -public: - typedef DenseMap MapTy; - -private: - /// Map - This is the main map from the element descriptor to the Constants. - /// This is the primary way we avoid creating two of the same shape - /// constant. - MapTy Map; - -public: - typename MapTy::iterator map_begin() { return Map.begin(); } - typename MapTy::iterator map_end() { return Map.end(); } - - void freeConstants() { - for (typename MapTy::iterator I=Map.begin(), E=Map.end(); - I != E; ++I) { - // Asserts that use_empty(). - delete I->first; - } - } - -private: - typename MapTy::iterator findExistingElement(ConstantClass *CP) { - return Map.find(CP); - } - - ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) { - ConstantClass* Result = - ConstantArrayCreator::create(Ty, V); - - assert(Result->getType() == Ty && "Type specified is not correct!"); - Map[Result] = '\0'; - - return Result; - } -public: - - /// getOrCreate - Return the specified constant from the map, creating it if - /// necessary. - ConstantClass *getOrCreate(TypeClass *Ty, Operands V) { - LookupKey Lookup(Ty, V); - ConstantClass* Result = nullptr; - - typename MapTy::iterator I = Map.find_as(Lookup); - // Is it in the map? + ConstantClass *replaceOperandsInPlace(ArrayRef Operands, + ConstantClass *CP, Value *From, + Constant *To, unsigned NumUpdated = 0, + unsigned OperandNo = ~0u) { + LookupKey Lookup(CP->getType(), ValType(Operands, CP)); + auto I = find(Lookup); if (I != Map.end()) - Result = I->first; - - if (!Result) { - // If no preexisting value, create one now... - Result = Create(Ty, V, I); + return I->first; + + // Update to the new value. Optimize for the case when we have a single + // operand that we're changing, but handle bulk updates efficiently. + remove(CP); + if (NumUpdated == 1) { + assert(OperandNo < CP->getNumOperands() && "Invalid index"); + assert(CP->getOperand(OperandNo) != To && "I didn't contain From!"); + CP->setOperand(OperandNo, To); + } else { + for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I) + if (CP->getOperand(I) == From) + CP->setOperand(I, To); } - - return Result; - } - - /// Find the constant by lookup key. - typename MapTy::iterator find(LookupKey Lookup) { - return Map.find_as(Lookup); - } - - /// Insert the constant into its proper slot. - void insert(ConstantClass *CP) { - Map[CP] = '\0'; + insert(CP); + return nullptr; } - /// Remove this constant from the map - void remove(ConstantClass *CP) { - typename MapTy::iterator I = findExistingElement(CP); - assert(I != Map.end() && "Constant not found in constant table!"); - assert(I->first == CP && "Didn't find correct element?"); - Map.erase(I); - } - - void dump() const { - DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); - } + void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); } }; } // end namespace llvm