- void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
-};
-
-// Unique map for aggregate constants
-template<class TypeClass, class ConstantClass>
-class ConstantAggrUniqueMap {
-public:
- typedef ArrayRef<Constant*> Operands;
- typedef std::pair<TypeClass*, Operands> LookupKey;
-private:
- struct MapInfo {
- typedef DenseMapInfo<ConstantClass*> ConstantClassInfo;
- typedef DenseMapInfo<Constant*> ConstantInfo;
- typedef DenseMapInfo<TypeClass*> TypeClassInfo;
- static inline ConstantClass* getEmptyKey() {
- return ConstantClassInfo::getEmptyKey();
- }
- static inline ConstantClass* getTombstoneKey() {
- return ConstantClassInfo::getTombstoneKey();
- }
- static unsigned getHashValue(const ConstantClass *CP) {
- SmallVector<Constant*, 8> 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<ConstantClass *, char, MapInfo> 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<ConstantClass,TypeClass>::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?