llvm.org GIT mirror llvm / 0c5ff64
Revert "Add a new insert_as() method to DenseMap and use it for ConstantUniqueMap" This reverts commit r260458. It was backported on an internal branch and broke stage2 build. Since this can lead to weird random crash I'm reverting upstream as well while investigating. From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@260605 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 4 years ago
2 changed file(s) with 23 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
194194 true);
195195 }
196196
197 /// Alternate version of insert() which allows a different, and possibly
198 /// less expensive, key type.
199 /// The DenseMapInfo is responsible for supplying methods
200 /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key
201 /// type used.
202 template
203 std::pair insert_as(std::pair &&KV,
204 const LookupKeyT &Val) {
205 BucketT *TheBucket;
206 if (LookupBucketFor(Val, TheBucket))
207 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
208 false); // Already in map.
209
210 // Otherwise, insert the new element.
211 TheBucket = InsertIntoBucket(std::move(KV.first), std::move(KV.second), Val,
212 TheBucket);
213 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
214 true);
215 }
216
217197 /// insert - Range insertion of pairs.
218198 template
219199 void insert(InputIt I, InputIt E) {
418398
419399 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
420400 BucketT *TheBucket) {
421 TheBucket = InsertIntoBucketImpl(Key, Key, TheBucket);
401 TheBucket = InsertIntoBucketImpl(Key, TheBucket);
422402
423403 TheBucket->getFirst() = Key;
424404 ::new (&TheBucket->getSecond()) ValueT(Value);
427407
428408 BucketT *InsertIntoBucket(const KeyT &Key, ValueT &&Value,
429409 BucketT *TheBucket) {
430 TheBucket = InsertIntoBucketImpl(Key, Key, TheBucket);
410 TheBucket = InsertIntoBucketImpl(Key, TheBucket);
431411
432412 TheBucket->getFirst() = Key;
433413 ::new (&TheBucket->getSecond()) ValueT(std::move(Value));
435415 }
436416
437417 BucketT *InsertIntoBucket(KeyT &&Key, ValueT &&Value, BucketT *TheBucket) {
438 TheBucket = InsertIntoBucketImpl(Key, Key, TheBucket);
418 TheBucket = InsertIntoBucketImpl(Key, TheBucket);
439419
440420 TheBucket->getFirst() = std::move(Key);
441421 ::new (&TheBucket->getSecond()) ValueT(std::move(Value));
442422 return TheBucket;
443423 }
444424
445 template
446 BucketT *InsertIntoBucket(KeyT &&Key, ValueT &&Value, LookupKeyT &Lookup,
447 BucketT *TheBucket) {
448 TheBucket = InsertIntoBucketImpl(Key, Lookup, TheBucket);
449
450 TheBucket->getFirst() = std::move(Key);
451 ::new (&TheBucket->getSecond()) ValueT(std::move(Value));
452 return TheBucket;
453 }
454
455 template
456 BucketT *InsertIntoBucketImpl(const KeyT &Key, const LookupKeyT &Lookup,
457 BucketT *TheBucket) {
425 BucketT *InsertIntoBucketImpl(const KeyT &Key, BucketT *TheBucket) {
458426 incrementEpoch();
459427
460428 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
470438 unsigned NumBuckets = getNumBuckets();
471439 if (LLVM_UNLIKELY(NewNumEntries * 4 >= NumBuckets * 3)) {
472440 this->grow(NumBuckets * 2);
473 LookupBucketFor(Lookup, TheBucket);
441 LookupBucketFor(Key, TheBucket);
474442 NumBuckets = getNumBuckets();
475443 } else if (LLVM_UNLIKELY(NumBuckets-(NewNumEntries+getNumTombstones()) <=
476444 NumBuckets/8)) {
477445 this->grow(NumBuckets);
478 LookupBucketFor(Lookup, TheBucket);
446 LookupBucketFor(Key, TheBucket);
479447 }
480448 assert(TheBucket);
481449
545545 typedef typename ConstantInfo::TypeClass TypeClass;
546546 typedef std::pair LookupKey;
547547
548 /// Key and hash together, so that we compute the hash only once and reuse it.
549 typedef std::pair LookupKeyHashed;
550
551548 private:
552549 struct MapInfo {
553550 typedef DenseMapInfo ConstantClassInfo;
566563 }
567564 static unsigned getHashValue(const LookupKey &Val) {
568565 return hash_combine(Val.first, Val.second.getHash());
569 }
570 static unsigned getHashValue(const LookupKeyHashed &Val) {
571 return Val.first;
572566 }
573567 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
574568 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
577571 return false;
578572 return LHS.second == RHS;
579573 }
580 static bool isEqual(const LookupKeyHashed &LHS, const ConstantClass *RHS) {
581 return isEqual(LHS.second, RHS);
582 }
583574 };
584575
585576 public:
597588 // Asserts that use_empty().
598589 delete I.first;
599590 }
591
600592 private:
601 ConstantClass *create(TypeClass *Ty, ValType V, LookupKeyHashed &HashKey) {
593 ConstantClass *create(TypeClass *Ty, ValType V) {
602594 ConstantClass *Result = V.create(Ty);
603595
604596 assert(Result->getType() == Ty && "Type specified is not correct!");
605 Map.insert_as(std::make_pair(Result, '\0'), HashKey);
597 insert(Result);
606598
607599 return Result;
608600 }
610602 public:
611603 /// Return the specified constant from the map, creating it if necessary.
612604 ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
613 LookupKey Key(Ty, V);
614 /// Hash once, and reuse it for the lookup and the insertion if needed.
615 LookupKeyHashed Lookup(MapInfo::getHashValue(Key), Key);
616
605 LookupKey Lookup(Ty, V);
617606 ConstantClass *Result = nullptr;
618607
619 auto I = Map.find_as(Lookup);
608 auto I = find(Lookup);
620609 if (I == Map.end())
621 Result = create(Ty, V, Lookup);
610 Result = create(Ty, V);
622611 else
623612 Result = I->first;
624613 assert(Result && "Unexpected nullptr");
625614
626615 return Result;
627616 }
617
618 /// Find the constant by lookup key.
619 typename MapTy::iterator find(LookupKey Lookup) {
620 return Map.find_as(Lookup);
621 }
622
623 /// Insert the constant into its proper slot.
624 void insert(ConstantClass *CP) { Map[CP] = '\0'; }
628625
629626 /// Remove this constant from the map
630627 void remove(ConstantClass *CP) {
638635 ConstantClass *CP, Value *From,
639636 Constant *To, unsigned NumUpdated = 0,
640637 unsigned OperandNo = ~0u) {
641 LookupKey Key(CP->getType(), ValType(Operands, CP));
642 /// Hash once, and reuse it for the lookup and the insertion if needed.
643 LookupKeyHashed Lookup(MapInfo::getHashValue(Key), Key);
644
645 auto I = Map.find_as(Lookup);
638 LookupKey Lookup(CP->getType(), ValType(Operands, CP));
639 auto I = find(Lookup);
646640 if (I != Map.end())
647641 return I->first;
648642
658652 if (CP->getOperand(I) == From)
659653 CP->setOperand(I, To);
660654 }
661 Map.insert_as(std::make_pair(CP, '\0'), Lookup);
655 insert(CP);
662656 return nullptr;
663657 }
664658