llvm.org GIT mirror llvm / 93710f0
Reverting r215966, r215965, r215964, r215963, r215960, r215959, r215958, and r215957 (these commits all rely on previous commits) due to build breakage. These commits cause failed assertions when testing Clang using MSVC 2013. The asserts are triggered from the std::equal call within ArrayRef::equals due to being passed invalid input (ArrayRef.begin() is returning a nullptr which is problematic). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215981 91177308-0d34-0410-b5e6-96231b3b80d8 Aaron Ballman 6 years ago
8 changed file(s) with 534 addition(s) and 470 deletion(s). Raw diff Collapse all Expand all
4747 : User(ty, vty, Ops, NumOps) {}
4848
4949 void destroyConstantImpl();
50 void replaceUsesOfWithOnConstantImpl(Constant *Replacement);
51
5250 public:
5351 /// isNullValue - Return true if this is the value that would be returned by
5452 /// getNullValue.
3636 class VectorType;
3737 class SequentialType;
3838
39 struct ConstantExprKeyType;
40 template struct ConstantAggrKeyType;
39 template
40 struct ConstantCreator;
41 template
42 struct ConstantArrayCreator;
43 template
44 struct ConvertConstantType;
4145
4246 //===----------------------------------------------------------------------===//
4347 /// This is the shared class of boolean and integer constants. This class
333337 /// ConstantArray - Constant Array Declarations
334338 ///
335339 class ConstantArray : public Constant {
336 friend struct ConstantAggrKeyType>;
340 friend struct ConstantArrayCreator>;
337341 ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION;
338342 protected:
339343 ConstantArray(ArrayType *T, ArrayRef Val);
341345 // ConstantArray accessors
342346 static Constant *get(ArrayType *T, ArrayRef V);
343347
344 private:
345 static Constant *getImpl(ArrayType *T, ArrayRef V);
346
347 public:
348348 /// Transparently provide more efficient getOperand methods.
349349 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
350350
375375 // ConstantStruct - Constant Struct Declarations
376376 //
377377 class ConstantStruct : public Constant {
378 friend struct ConstantAggrKeyType>;
378 friend struct ConstantArrayCreator>;
379379 ConstantStruct(const ConstantStruct &) LLVM_DELETED_FUNCTION;
380380 protected:
381381 ConstantStruct(StructType *T, ArrayRef Val);
434434 /// ConstantVector - Constant Vector Declarations
435435 ///
436436 class ConstantVector : public Constant {
437 friend struct ConstantAggrKeyType>;
437 friend struct ConstantArrayCreator>;
438438 ConstantVector(const ConstantVector &) LLVM_DELETED_FUNCTION;
439439 protected:
440440 ConstantVector(VectorType *T, ArrayRef Val);
442442 // ConstantVector accessors
443443 static Constant *get(ArrayRef V);
444444
445 private:
446 static Constant *getImpl(ArrayRef V);
447
448 public:
449445 /// getSplat - Return a ConstantVector with the specified constant in each
450446 /// element.
451447 static Constant *getSplat(unsigned NumElts, Constant *Elt);
797793 /// constant expressions. The Opcode field for the ConstantExpr class is
798794 /// maintained in the Value::SubclassData field.
799795 class ConstantExpr : public Constant {
800 friend struct ConstantExprKeyType;
796 friend struct ConstantCreator
797 std::pair > >;
798 friend struct ConvertConstantType;
801799
802800 protected:
803801 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
11141112 void setValueSubclassData(unsigned short D) {
11151113 Value::setValueSubclassData(D);
11161114 }
1117
1118 /// \brief Check whether this can become its replacement.
1119 ///
1120 /// For use during \a replaceUsesOfWithOnConstant(), check whether we know
1121 /// how to turn this into \a Replacement, thereby reducing RAUW traffic.
1122 bool canBecomeReplacement(const Constant *Replacement) const;
11231115 };
11241116
11251117 template <>
2424 class PointerType;
2525 class FunctionType;
2626 class Module;
27
2827 struct InlineAsmKeyType;
29 template class ConstantUniqueMap;
28 template
29 bool HasLargeKey>
30 class ConstantUniqueMap;
31 template
32 struct ConstantCreator;
3033
3134 class InlineAsm : public Value {
3235 public:
3639 };
3740
3841 private:
39 friend struct InlineAsmKeyType;
40 friend class ConstantUniqueMap>;
42 friend struct ConstantCreator>;
43 friend class ConstantUniqueMap
44 PointerType, InlineAsm, false>;
4145
4246 InlineAsm(const InlineAsm &) LLVM_DELETED_FUNCTION;
4347 void operator=(const InlineAsm&) LLVM_DELETED_FUNCTION;
802802 }
803803
804804 Constant *ConstantArray::get(ArrayType *Ty, ArrayRef V) {
805 if (Constant *C = getImpl(Ty, V))
806 return C;
807 return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
808 }
809 Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef V) {
810805 // Empty arrays are canonicalized to ConstantAggregateZero.
811806 if (V.empty())
812807 return ConstantAggregateZero::get(Ty);
815810 assert(V[i]->getType() == Ty->getElementType() &&
816811 "Wrong type in array element initializer");
817812 }
813 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
818814
819815 // If this is an all-zero array, return a ConstantAggregateZero object. If
820816 // all undef, return an UndefValue, if "all simple", then return a
896892 }
897893
898894 // Otherwise, we really do want to create a ConstantArray.
899 return nullptr;
895 return pImpl->ArrayConstants.getOrCreate(Ty, V);
900896 }
901897
902898 /// getTypeForElements - Return an anonymous struct type to use for a constant
984980
985981 // ConstantVector accessors.
986982 Constant *ConstantVector::get(ArrayRef V) {
987 if (Constant *C = getImpl(V))
988 return C;
989 VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
990 return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
991 }
992 Constant *ConstantVector::getImpl(ArrayRef V) {
993983 assert(!V.empty() && "Vectors can't be empty");
994984 VectorType *T = VectorType::get(V.front()->getType(), V.size());
985 LLVMContextImpl *pImpl = T->getContext().pImpl;
995986
996987 // If this is an all-undef or all-zero vector, return a
997988 // ConstantAggregateZero or UndefValue.
10831074
10841075 // Otherwise, the element type isn't compatible with ConstantDataVector, or
10851076 // the operand list constants a ConstantExpr or something else strange.
1086 return nullptr;
1077 return pImpl->VectorConstants.getOrCreate(T, V);
10871078 }
10881079
10891080 Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
14771468 // and return early.
14781469 BlockAddress *&NewBA =
14791470 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
1480 if (NewBA) {
1481 replaceUsesOfWithOnConstantImpl(NewBA);
1471 if (!NewBA) {
1472 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1473
1474 // Remove the old entry, this can't cause the map to rehash (just a
1475 // tombstone will get added).
1476 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1477 getBasicBlock()));
1478 NewBA = this;
1479 setOperand(0, NewF);
1480 setOperand(1, NewBB);
1481 getBasicBlock()->AdjustBlockAddressRefCount(1);
14821482 return;
14831483 }
14841484
1485 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1486
1487 // Remove the old entry, this can't cause the map to rehash (just a
1488 // tombstone will get added).
1489 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1490 getBasicBlock()));
1491 NewBA = this;
1492 setOperand(0, NewF);
1493 setOperand(1, NewBB);
1494 getBasicBlock()->AdjustBlockAddressRefCount(1);
1485 // Otherwise, I do need to replace this with an existing value.
1486 assert(NewBA != this && "I didn't contain From!");
1487
1488 // Everyone using this now uses the replacement.
1489 replaceAllUsesWith(NewBA);
1490
1491 destroyConstant();
14951492 }
14961493
14971494 //---- ConstantExpr::get() implementations.
15091506 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
15101507
15111508 // Look up the constant in the table first to ensure uniqueness.
1512 ConstantExprKeyType Key(opc, C);
1509 ExprMapKeyType Key(opc, C);
15131510
15141511 return pImpl->ExprConstants.getOrCreate(Ty, Key);
15151512 }
18441841 return FC; // Fold a few common cases.
18451842
18461843 Constant *ArgVec[] = { C1, C2 };
1847 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
1844 ExprMapKeyType Key(Opcode, ArgVec, 0, Flags);
18481845
18491846 LLVMContextImpl *pImpl = C1->getContext().pImpl;
18501847 return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
19211918 return SC; // Fold common cases
19221919
19231920 Constant *ArgVec[] = { C, V1, V2 };
1924 ConstantExprKeyType Key(Instruction::Select, ArgVec);
1921 ExprMapKeyType Key(Instruction::Select, ArgVec);
19251922
19261923 LLVMContextImpl *pImpl = C->getContext().pImpl;
19271924 return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
19561953 "getelementptr index type missmatch");
19571954 ArgVec.push_back(cast(Idxs[i]));
19581955 }
1959 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
1960 InBounds ? GEPOperator::IsInBounds : 0);
1956 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
1957 InBounds ? GEPOperator::IsInBounds : 0);
19611958
19621959 LLVMContextImpl *pImpl = C->getContext().pImpl;
19631960 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
19751972 // Look up the constant in the table first to ensure uniqueness
19761973 Constant *ArgVec[] = { LHS, RHS };
19771974 // Get the key type with both the opcode and predicate
1978 const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
1975 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
19791976
19801977 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
19811978 if (VectorType *VT = dyn_cast(LHS->getType()))
19961993 // Look up the constant in the table first to ensure uniqueness
19971994 Constant *ArgVec[] = { LHS, RHS };
19981995 // Get the key type with both the opcode and predicate
1999 const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
1996 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
20001997
20011998 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
20021999 if (VectorType *VT = dyn_cast(LHS->getType()))
20172014
20182015 // Look up the constant in the table first to ensure uniqueness
20192016 Constant *ArgVec[] = { Val, Idx };
2020 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
2017 const ExprMapKeyType Key(Instruction::ExtractElement, ArgVec);
20212018
20222019 LLVMContextImpl *pImpl = Val->getContext().pImpl;
20232020 Type *ReqTy = Val->getType()->getVectorElementType();
20372034 return FC; // Fold a few common cases.
20382035 // Look up the constant in the table first to ensure uniqueness
20392036 Constant *ArgVec[] = { Val, Elt, Idx };
2040 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
2037 const ExprMapKeyType Key(Instruction::InsertElement, ArgVec);
20412038
20422039 LLVMContextImpl *pImpl = Val->getContext().pImpl;
20432040 return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
20572054
20582055 // Look up the constant in the table first to ensure uniqueness
20592056 Constant *ArgVec[] = { V1, V2, Mask };
2060 const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
2057 const ExprMapKeyType Key(Instruction::ShuffleVector, ArgVec);
20612058
20622059 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
20632060 return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
20772074 return FC;
20782075
20792076 Constant *ArgVec[] = { Agg, Val };
2080 const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
2077 const ExprMapKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
20812078
20822079 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
20832080 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
20982095 return FC;
20992096
21002097 Constant *ArgVec[] = { Agg };
2101 const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
2098 const ExprMapKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
21022099
21032100 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
21042101 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
26542651 /// work, but would be really slow because it would have to unique each updated
26552652 /// array instance.
26562653 ///
2657 void Constant::replaceUsesOfWithOnConstantImpl(Constant *Replacement) {
2658 // I do need to replace this with an existing value.
2659 assert(Replacement != this && "I didn't contain From!");
2660
2661 // Everyone using this now uses the replacement.
2662 replaceAllUsesWith(Replacement);
2663
2664 // Delete the old constant!
2665 destroyConstant();
2666 }
2667
26682654 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
26692655 Use *U) {
26702656 assert(isa(To) && "Cannot make Constant refer to non-constant!");
26912677 AllSame &= Val == ToC;
26922678 }
26932679
2680 Constant *Replacement = nullptr;
26942681 if (AllSame && ToC->isNullValue()) {
2695 replaceUsesOfWithOnConstantImpl(ConstantAggregateZero::get(getType()));
2696 return;
2697 }
2698 if (AllSame && isa(ToC)) {
2699 replaceUsesOfWithOnConstantImpl(UndefValue::get(getType()));
2700 return;
2701 }
2702
2703 // Check for any other type of constant-folding.
2704 if (Constant *C = getImpl(getType(), Values)) {
2705 replaceUsesOfWithOnConstantImpl(C);
2706 return;
2707 }
2708
2709 // Check to see if we have this array type already.
2710 LLVMContextImpl::ArrayConstantsTy::LookupKey Lookup(
2711 cast(getType()), makeArrayRef(Values));
2712 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2713 pImpl->ArrayConstants.find(Lookup);
2714
2715 if (I != pImpl->ArrayConstants.map_end()) {
2716 replaceUsesOfWithOnConstantImpl(I->first);
2717 return;
2718 }
2719
2720 // Okay, the new shape doesn't exist in the system yet. Instead of
2721 // creating a new constant array, inserting it, replaceallusesof'ing the
2722 // old with the new, then deleting the old... just update the current one
2723 // in place!
2724 pImpl->ArrayConstants.remove(this);
2725
2726 // Update to the new value. Optimize for the case when we have a single
2727 // operand that we're changing, but handle bulk updates efficiently.
2728 if (NumUpdated == 1) {
2729 unsigned OperandToUpdate = U - OperandList;
2730 assert(getOperand(OperandToUpdate) == From &&
2731 "ReplaceAllUsesWith broken!");
2732 setOperand(OperandToUpdate, ToC);
2682 Replacement = ConstantAggregateZero::get(getType());
2683 } else if (AllSame && isa(ToC)) {
2684 Replacement = UndefValue::get(getType());
27332685 } else {
2734 for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
2735 if (getOperand(I) == From)
2736 setOperand(I, ToC);
2737 }
2738 pImpl->ArrayConstants.insert(this);
2686 // Check to see if we have this array type already.
2687 LLVMContextImpl::ArrayConstantsTy::LookupKey Lookup(
2688 cast(getType()), makeArrayRef(Values));
2689 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2690 pImpl->ArrayConstants.find(Lookup);
2691
2692 if (I != pImpl->ArrayConstants.map_end()) {
2693 Replacement = I->first;
2694 } else {
2695 // Okay, the new shape doesn't exist in the system yet. Instead of
2696 // creating a new constant array, inserting it, replaceallusesof'ing the
2697 // old with the new, then deleting the old... just update the current one
2698 // in place!
2699 pImpl->ArrayConstants.remove(this);
2700
2701 // Update to the new value. Optimize for the case when we have a single
2702 // operand that we're changing, but handle bulk updates efficiently.
2703 if (NumUpdated == 1) {
2704 unsigned OperandToUpdate = U - OperandList;
2705 assert(getOperand(OperandToUpdate) == From &&
2706 "ReplaceAllUsesWith broken!");
2707 setOperand(OperandToUpdate, ToC);
2708 } else {
2709 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2710 if (getOperand(i) == From)
2711 setOperand(i, ToC);
2712 }
2713 pImpl->ArrayConstants.insert(this);
2714 return;
2715 }
2716 }
2717
2718 // Otherwise, I do need to replace this with an existing value.
2719 assert(Replacement != this && "I didn't contain From!");
2720
2721 // Everyone using this now uses the replacement.
2722 replaceAllUsesWith(Replacement);
2723
2724 // Delete the old constant!
2725 destroyConstant();
27392726 }
27402727
27412728 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
27752762
27762763 LLVMContextImpl *pImpl = getContext().pImpl;
27772764
2765 Constant *Replacement = nullptr;
27782766 if (isAllZeros) {
2779 replaceUsesOfWithOnConstantImpl(ConstantAggregateZero::get(getType()));
2780 return;
2781 }
2782 if (isAllUndef) {
2783 replaceUsesOfWithOnConstantImpl(UndefValue::get(getType()));
2784 return;
2785 }
2786
2787 // Check to see if we have this struct type already.
2788 LLVMContextImpl::StructConstantsTy::LookupKey Lookup(
2789 cast(getType()), makeArrayRef(Values));
2790 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2767 Replacement = ConstantAggregateZero::get(getType());
2768 } else if (isAllUndef) {
2769 Replacement = UndefValue::get(getType());
2770 } else {
2771 // Check to see if we have this struct type already.
2772 LLVMContextImpl::StructConstantsTy::LookupKey Lookup(
2773 cast(getType()), makeArrayRef(Values));
2774 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
27912775 pImpl->StructConstants.find(Lookup);
27922776
2793 if (I != pImpl->StructConstants.map_end()) {
2794 replaceUsesOfWithOnConstantImpl(I->first);
2795 return;
2796 }
2797
2798 // Okay, the new shape doesn't exist in the system yet. Instead of
2799 // creating a new constant struct, inserting it, replaceallusesof'ing the
2800 // old with the new, then deleting the old... just update the current one
2801 // in place!
2802 pImpl->StructConstants.remove(this);
2803
2804 // Update to the new value.
2805 setOperand(OperandToUpdate, ToC);
2806 pImpl->StructConstants.insert(this);
2777 if (I != pImpl->StructConstants.map_end()) {
2778 Replacement = I->first;
2779 } else {
2780 // Okay, the new shape doesn't exist in the system yet. Instead of
2781 // creating a new constant struct, inserting it, replaceallusesof'ing the
2782 // old with the new, then deleting the old... just update the current one
2783 // in place!
2784 pImpl->StructConstants.remove(this);
2785
2786 // Update to the new value.
2787 setOperand(OperandToUpdate, ToC);
2788 pImpl->StructConstants.insert(this);
2789 return;
2790 }
2791 }
2792
2793 assert(Replacement != this && "I didn't contain From!");
2794
2795 // Everyone using this now uses the replacement.
2796 replaceAllUsesWith(Replacement);
2797
2798 // Delete the old constant!
2799 destroyConstant();
28072800 }
28082801
28092802 void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
28102803 Use *U) {
28112804 assert(isa(To) && "Cannot make Constant refer to non-constant!");
2812 Constant *ToC = cast(To);
28132805
28142806 SmallVector Values;
28152807 Values.reserve(getNumOperands()); // Build replacement array...
2816 unsigned NumUpdated = 0;
28172808 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
28182809 Constant *Val = getOperand(i);
2819 if (Val == From) {
2820 ++NumUpdated;
2821 Val = ToC;
2822 }
2810 if (Val == From) Val = cast(To);
28232811 Values.push_back(Val);
28242812 }
28252813
2826 if (Constant *C = getImpl(Values)) {
2827 replaceUsesOfWithOnConstantImpl(C);
2828 return;
2829 }
2830
2831 // Update to the new value. Optimize for the case when we have a single
2832 // operand that we're changing, but handle bulk updates efficiently.
2833 auto &pImpl = getType()->getContext().pImpl;
2834 pImpl->VectorConstants.remove(this);
2835
2836 if (NumUpdated == 1) {
2837 unsigned OperandToUpdate = U - OperandList;
2838 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2839 setOperand(OperandToUpdate, ToC);
2840 } else {
2841 for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
2842 if (getOperand(I) == From)
2843 setOperand(I, ToC);
2844 }
2845
2846 pImpl->VectorConstants.insert(this);
2814 Constant *Replacement = get(Values);
2815 assert(Replacement != this && "I didn't contain From!");
2816
2817 // Everyone using this now uses the replacement.
2818 replaceAllUsesWith(Replacement);
2819
2820 // Delete the old constant!
2821 destroyConstant();
28472822 }
28482823
28492824 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
28602835 Constant *Replacement = getWithOperands(NewOps);
28612836 assert(Replacement != this && "I didn't contain From!");
28622837
2863 // Check if Replacement has no users (and is the same type). Ideally, this
2864 // check would be done *before* creating Replacement, but threading this
2865 // through constant-folding isn't trivial.
2866 if (canBecomeReplacement(Replacement)) {
2867 // Avoid unnecessary RAUW traffic.
2868 auto &ExprConstants = getType()->getContext().pImpl->ExprConstants;
2869 ExprConstants.remove(this);
2870
2871 auto *CE = cast(Replacement);
2872 for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
2873 // Only set the operands that have actually changed.
2874 if (getOperand(I) != CE->getOperand(I))
2875 setOperand(I, CE->getOperand(I));
2876
2877 CE->destroyConstant();
2878 ExprConstants.insert(this);
2879 return;
2880 }
2881
28822838 // Everyone using this now uses the replacement.
28832839 replaceAllUsesWith(Replacement);
28842840
28852841 // Delete the old constant!
28862842 destroyConstant();
2887 }
2888
2889 bool ConstantExpr::canBecomeReplacement(const Constant *Replacement) const {
2890 // If Replacement already has users, use it regardless.
2891 if (!Replacement->use_empty())
2892 return false;
2893
2894 // Check for anything that could have changed during constant-folding.
2895 if (getValueID() != Replacement->getValueID())
2896 return false;
2897 const auto *CE = cast(Replacement);
2898 if (getOpcode() != CE->getOpcode())
2899 return false;
2900 if (getNumOperands() != CE->getNumOperands())
2901 return false;
2902 if (getRawSubclassOptionalData() != CE->getRawSubclassOptionalData())
2903 return false;
2904 if (isCompare())
2905 if (getPredicate() != CE->getPredicate())
2906 return false;
2907 if (hasIndices())
2908 if (getIndices() != CE->getIndices())
2909 return false;
2910
2911 return true;
29122843 }
29132844
29142845 Instruction *ConstantExpr::getAsInstruction() {
2828 #define DEBUG_TYPE "ir"
2929
3030 namespace llvm {
31 template
32 struct ConstantTraits;
3133
3234 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
3335 /// behind the scenes to implement unary constant exprs.
311313 };
312314 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
313315
314 template struct ConstantAggrKeyType;
315 struct InlineAsmKeyType;
316 struct ConstantExprKeyType;
317
318 template struct ConstantInfo;
319 template <> struct ConstantInfo {
320 typedef ConstantExprKeyType ValType;
321 typedef Type TypeClass;
322 };
323 template <> struct ConstantInfo {
316 struct ExprMapKeyType {
317 ExprMapKeyType(unsigned opc,
318 ArrayRef ops,
319 unsigned short flags = 0,
320 unsigned short optionalflags = 0,
321 ArrayRef inds = None)
322 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
323 operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {}
324 uint8_t opcode;
325 uint8_t subclassoptionaldata;
326 uint16_t subclassdata;
327 std::vector operands;
328 SmallVector indices;
329 bool operator==(const ExprMapKeyType& that) const {
330 return this->opcode == that.opcode &&
331 this->subclassdata == that.subclassdata &&
332 this->subclassoptionaldata == that.subclassoptionaldata &&
333 this->operands == that.operands &&
334 this->indices == that.indices;
335 }
336 bool operator<(const ExprMapKeyType & that) const {
337 return std::tie(opcode, operands, subclassdata, subclassoptionaldata,
338 indices) <
339 std::tie(that.opcode, that.operands, that.subclassdata,
340 that.subclassoptionaldata, that.indices);
341 }
342
343 bool operator!=(const ExprMapKeyType& that) const {
344 return !(*this == that);
345 }
346 };
347
348 struct InlineAsmKeyType {
349 InlineAsmKeyType(StringRef AsmString,
350 StringRef Constraints, bool hasSideEffects,
351 bool isAlignStack, InlineAsm::AsmDialect asmDialect)
352 : asm_string(AsmString), constraints(Constraints),
353 has_side_effects(hasSideEffects), is_align_stack(isAlignStack),
354 asm_dialect(asmDialect) {}
355 std::string asm_string;
356 std::string constraints;
357 bool has_side_effects;
358 bool is_align_stack;
359 InlineAsm::AsmDialect asm_dialect;
360 bool operator==(const InlineAsmKeyType& that) const {
361 return this->asm_string == that.asm_string &&
362 this->constraints == that.constraints &&
363 this->has_side_effects == that.has_side_effects &&
364 this->is_align_stack == that.is_align_stack &&
365 this->asm_dialect == that.asm_dialect;
366 }
367 bool operator<(const InlineAsmKeyType& that) const {
368 return std::tie(asm_string, constraints, has_side_effects, is_align_stack,
369 asm_dialect) <
370 std::tie(that.asm_string, that.constraints, that.has_side_effects,
371 that.is_align_stack, that.asm_dialect);
372 }
373
374 bool operator!=(const InlineAsmKeyType& that) const {
375 return !(*this == that);
376 }
377 };
378
379 // The number of operands for each ConstantCreator::create method is
380 // determined by the ConstantTraits template.
381 // ConstantCreator - A class that is used to create constants by
382 // ConstantUniqueMap*. This class should be partially specialized if there is
383 // something strange that needs to be done to interface to the ctor for the
384 // constant.
385 //
386 template
387 struct ConstantTraits< std::vector > {
388 static unsigned uses(const std::vector& v) {
389 return v.size();
390 }
391 };
392
393 template<>
394 struct ConstantTraits {
395 static unsigned uses(Constant * const & v) {
396 return 1;
397 }
398 };
399
400 template
401 struct ConstantCreator {
402 static ConstantClass *create(TypeClass *Ty, const ValType &V) {
403 return new(ConstantTraits::uses(V)) ConstantClass(Ty, V);
404 }
405 };
406
407 template
408 struct ConstantArrayCreator {
409 static ConstantClass *create(TypeClass *Ty, ArrayRef V) {
410 return new(V.size()) ConstantClass(Ty, V);
411 }
412 };
413
414 template
415 struct ConstantKeyData {
416 typedef void ValType;
417 static ValType getValType(ConstantClass *C) {
418 llvm_unreachable("Unknown Constant type!");
419 }
420 };
421
422 template<>
423 struct ConstantCreator {
424 static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V,
425 unsigned short pred = 0) {
426 if (Instruction::isCast(V.opcode))
427 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
428 if ((V.opcode >= Instruction::BinaryOpsBegin &&
429 V.opcode < Instruction::BinaryOpsEnd))
430 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
431 V.subclassoptionaldata);
432 if (V.opcode == Instruction::Select)
433 return new SelectConstantExpr(V.operands[0], V.operands[1],
434 V.operands[2]);
435 if (V.opcode == Instruction::ExtractElement)
436 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
437 if (V.opcode == Instruction::InsertElement)
438 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
439 V.operands[2]);
440 if (V.opcode == Instruction::ShuffleVector)
441 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
442 V.operands[2]);
443 if (V.opcode == Instruction::InsertValue)
444 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
445 V.indices, Ty);
446 if (V.opcode == Instruction::ExtractValue)
447 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
448 if (V.opcode == Instruction::GetElementPtr) {
449 std::vector IdxList(V.operands.begin()+1, V.operands.end());
450 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
451 V.subclassoptionaldata);
452 }
453
454 // The compare instructions are weird. We have to encode the predicate
455 // value and it is combined with the instruction opcode by multiplying
456 // the opcode by one hundred. We must decode this to get the predicate.
457 if (V.opcode == Instruction::ICmp)
458 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
459 V.operands[0], V.operands[1]);
460 if (V.opcode == Instruction::FCmp)
461 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
462 V.operands[0], V.operands[1]);
463 llvm_unreachable("Invalid ConstantExpr!");
464 }
465 };
466
467 template<>
468 struct ConstantKeyData {
469 typedef ExprMapKeyType ValType;
470 static ValType getValType(ConstantExpr *CE) {
471 std::vector Operands;
472 Operands.reserve(CE->getNumOperands());
473 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
474 Operands.push_back(cast(CE->getOperand(i)));
475 return ExprMapKeyType(CE->getOpcode(), Operands,
476 CE->isCompare() ? CE->getPredicate() : 0,
477 CE->getRawSubclassOptionalData(),
478 CE->hasIndices() ?
479 CE->getIndices() : ArrayRef());
480 }
481 };
482
483 template<>
484 struct ConstantCreator {
485 static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
486 return new InlineAsm(Ty, Key.asm_string, Key.constraints,
487 Key.has_side_effects, Key.is_align_stack,
488 Key.asm_dialect);
489 }
490 };
491
492 template<>
493 struct ConstantKeyData {
324494 typedef InlineAsmKeyType ValType;
325 typedef PointerType TypeClass;
326 };
327 template <> struct ConstantInfo {
328 typedef ConstantAggrKeyType ValType;
329 typedef ArrayType TypeClass;
330 };
331 template <> struct ConstantInfo {
332 typedef ConstantAggrKeyType ValType;
333 typedef StructType TypeClass;
334 };
335 template <> struct ConstantInfo {
336 typedef ConstantAggrKeyType ValType;
337 typedef VectorType TypeClass;
338 };
339
340 template struct ConstantAggrKeyType {
341 ArrayRef Operands;
342 ConstantAggrKeyType(ArrayRef Operands) : Operands(Operands) {}
343 ConstantAggrKeyType(const ConstantClass *C,
344 SmallVectorImpl &Storage) {
345 assert(Storage.empty() && "Expected empty storage");
346 for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
347 Storage.push_back(C->getOperand(I));
348 Operands = Storage;
349 }
350
351 bool operator==(const ConstantAggrKeyType &X) const {
352 return Operands == X.Operands;
353 }
354 bool operator==(const ConstantClass *C) const {
355 if (Operands.size() != C->getNumOperands())
356 return false;
357 for (unsigned I = 0, E = Operands.size(); I != E; ++I)
358 if (Operands[I] != C->getOperand(I))
359 return false;
360 return true;
361 }
362 unsigned getHash() const {
363 return hash_combine_range(Operands.begin(), Operands.end());
364 }
365
366 typedef typename ConstantInfo::TypeClass TypeClass;
367 ConstantClass *create(TypeClass *Ty) const {
368 return new (Operands.size()) ConstantClass(Ty, Operands);
369 }
370 };
371
372 struct InlineAsmKeyType {
373 StringRef AsmString;
374 StringRef Constraints;
375 bool HasSideEffects;
376 bool IsAlignStack;
377 InlineAsm::AsmDialect AsmDialect;
378
379 InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
380 bool HasSideEffects, bool IsAlignStack,
381 InlineAsm::AsmDialect AsmDialect)
382 : AsmString(AsmString), Constraints(Constraints),
383 HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
384 AsmDialect(AsmDialect) {}
385 InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl &)
386 : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
387 HasSideEffects(Asm->hasSideEffects()),
388 IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {}
389
390 bool operator==(const InlineAsmKeyType &X) const {
391 return HasSideEffects == X.HasSideEffects &&
392 IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
393 AsmString == X.AsmString && Constraints == X.Constraints;
394 }
395 bool operator==(const InlineAsm *Asm) const {
396 return HasSideEffects == Asm->hasSideEffects() &&
397 IsAlignStack == Asm->isAlignStack() &&
398 AsmDialect == Asm->getDialect() &&
399 AsmString == Asm->getAsmString() &&
400 Constraints == Asm->getConstraintString();
401 }
402 unsigned getHash() const {
403 return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
404 AsmDialect);
405 }
406
407 typedef ConstantInfo::TypeClass TypeClass;
408 InlineAsm *create(TypeClass *Ty) const {
409 return new InlineAsm(Ty, AsmString, Constraints, HasSideEffects,
410 IsAlignStack, AsmDialect);
411 }
412 };
413
414 struct ConstantExprKeyType {
415 uint8_t Opcode;
416 uint8_t SubclassOptionalData;
417 uint16_t SubclassData;
418 ArrayRef Ops;
419 ArrayRef Indexes;
420
421 ConstantExprKeyType(unsigned Opcode, ArrayRef Ops,
422 unsigned short SubclassData = 0,
423 unsigned short SubclassOptionalData = 0,
424 ArrayRef Indexes = None)
425 : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
426 SubclassData(SubclassData), Ops(Ops), Indexes(Indexes) {}
427 ConstantExprKeyType(const ConstantExpr *CE,
428 SmallVectorImpl &Storage)
429 : Opcode(CE->getOpcode()),
430 SubclassOptionalData(CE->getRawSubclassOptionalData()),
431 SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
432 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef()) {
433 assert(Storage.empty() && "Expected empty storage");
434 for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
435 Storage.push_back(CE->getOperand(I));
436 Ops = Storage;
437 }
438
439 bool operator==(const ConstantExprKeyType &X) const {
440 return Opcode == X.Opcode && SubclassData == X.SubclassData &&
441 SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
442 Indexes == X.Indexes;
443 }
444
445 bool operator==(const ConstantExpr *CE) const {
446 if (Opcode != CE->getOpcode())
447 return false;
448 if (SubclassOptionalData != CE->getRawSubclassOptionalData())
449 return false;
450 if (Ops.size() != CE->getNumOperands())
451 return false;
452 if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
453 return false;
454 for (unsigned I = 0, E = Ops.size(); I != E; ++I)
455 if (Ops[I] != CE->getOperand(I))
456 return false;
457 if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef()))
458 return false;
459 return true;
460 }
461
462 unsigned getHash() const {
463 return hash_combine(Opcode, SubclassOptionalData, SubclassData,
464 hash_combine_range(Ops.begin(), Ops.end()),
465 hash_combine_range(Indexes.begin(), Indexes.end()));
466 }
467
468 typedef ConstantInfo::TypeClass TypeClass;
469 ConstantExpr *create(TypeClass *Ty) const {
470 switch (Opcode) {
471 default:
472 if (Instruction::isCast(Opcode))
473 return new UnaryConstantExpr(Opcode, Ops[0], Ty);
474 if ((Opcode >= Instruction::BinaryOpsBegin &&
475 Opcode < Instruction::BinaryOpsEnd))
476 return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
477 SubclassOptionalData);
478 llvm_unreachable("Invalid ConstantExpr!");
479 case Instruction::Select:
480 return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
481 case Instruction::ExtractElement:
482 return new ExtractElementConstantExpr(Ops[0], Ops[1]);
483 case Instruction::InsertElement:
484 return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
485 case Instruction::ShuffleVector:
486 return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]);
487 case Instruction::InsertValue:
488 return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
489 case Instruction::ExtractValue:
490 return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
491 case Instruction::GetElementPtr:
492 return GetElementPtrConstantExpr::Create(Ops[0], Ops.slice(1), Ty,
493 SubclassOptionalData);
494 case Instruction::ICmp:
495 return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
496 Ops[0], Ops[1]);
497 case Instruction::FCmp:
498 return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
499 Ops[0], Ops[1]);
500 }
501 }
502 };
503
504 template class ConstantUniqueMap {
505 public:
506 typedef typename ConstantInfo::ValType ValType;
507 typedef typename ConstantInfo::TypeClass TypeClass;
508 typedef std::pair LookupKey;
509
495 static ValType getValType(InlineAsm *Asm) {
496 return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
497 Asm->hasSideEffects(), Asm->isAlignStack(),
498 Asm->getDialect());
499 }
500 };
501
502 template
503 bool HasLargeKey = false /*true for arrays and structs*/ >
504 class ConstantUniqueMap {
505 public:
506 typedef std::pair MapKey;
507 typedef std::map MapTy;
508 typedef std::map InverseMapTy;
509 private:
510 /// Map - This is the main map from the element descriptor to the Constants.
511 /// This is the primary way we avoid creating two of the same shape
512 /// constant.
513 MapTy Map;
514
515 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
516 /// from the constants to their element in Map. This is important for
517 /// removal of constants from the array, which would otherwise have to scan
518 /// through the map with very large keys.
519 InverseMapTy InverseMap;
520
521 public:
522 typename MapTy::iterator map_begin() { return Map.begin(); }
523 typename MapTy::iterator map_end() { return Map.end(); }
524
525 void freeConstants() {
526 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
527 I != E; ++I) {
528 // Asserts that use_empty().
529 delete I->second;
530 }
531 }
532
533 /// InsertOrGetItem - Return an iterator for the specified element.
534 /// If the element exists in the map, the returned iterator points to the
535 /// entry and Exists=true. If not, the iterator points to the newly
536 /// inserted entry and returns Exists=false. Newly inserted entries have
537 /// I->second == 0, and should be filled in.
538 typename MapTy::iterator InsertOrGetItem(std::pair
539 &InsertVal,
540 bool &Exists) {
541 std::pair IP = Map.insert(InsertVal);
542 Exists = !IP.second;
543 return IP.first;
544 }
545
546 private:
547 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
548 if (HasLargeKey) {
549 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
550 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
551 IMI->second->second == CP &&
552 "InverseMap corrupt!");
553 return IMI->second;
554 }
555
556 typename MapTy::iterator I =
557 Map.find(MapKey(static_cast(CP->getType()),
558 ConstantKeyData::getValType(CP)));
559 if (I == Map.end() || I->second != CP) {
560 // FIXME: This should not use a linear scan. If this gets to be a
561 // performance problem, someone should look at this.
562 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
563 /* empty */;
564 }
565 return I;
566 }
567
568 ConstantClass *Create(TypeClass *Ty, ValRefType V,
569 typename MapTy::iterator I) {
570 ConstantClass* Result =
571 ConstantCreator::create(Ty, V);
572
573 assert(Result->getType() == Ty && "Type specified is not correct!");
574 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
575
576 if (HasLargeKey) // Remember the reverse mapping if needed.
577 InverseMap.insert(std::make_pair(Result, I));
578
579 return Result;
580 }
581 public:
582
583 /// getOrCreate - Return the specified constant from the map, creating it if
584 /// necessary.
585 ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) {
586 MapKey Lookup(Ty, V);
587 ConstantClass* Result = nullptr;
588
589 typename MapTy::iterator I = Map.find(Lookup);
590 // Is it in the map?
591 if (I != Map.end())
592 Result = I->second;
593
594 if (!Result) {
595 // If no preexisting value, create one now...
596 Result = Create(Ty, V, I);
597 }
598
599 return Result;
600 }
601
602 void remove(ConstantClass *CP) {
603 typename MapTy::iterator I = FindExistingElement(CP);
604 assert(I != Map.end() && "Constant not found in constant table!");
605 assert(I->second == CP && "Didn't find correct element?");
606
607 if (HasLargeKey) // Remember the reverse mapping if needed.
608 InverseMap.erase(CP);
609
610 Map.erase(I);
611 }
612
613 /// MoveConstantToNewSlot - If we are about to change C to be the element
614 /// specified by I, update our internal data structures to reflect this
615 /// fact.
616 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
617 // First, remove the old location of the specified constant in the map.
618 typename MapTy::iterator OldI = FindExistingElement(C);
619 assert(OldI != Map.end() && "Constant not found in constant table!");
620 assert(OldI->second == C && "Didn't find correct element?");
621
622 // Remove the old entry from the map.
623 Map.erase(OldI);
624
625 // Update the inverse map so that we know that this constant is now
626 // located at descriptor I.
627 if (HasLargeKey) {
628 assert(I->second == C && "Bad inversemap entry!");
629 InverseMap[C] = I;
630 }
631 }
632
633 void dump() const {
634 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
635 }
636 };
637
638 // Unique map for aggregate constants
639 template
640 class ConstantAggrUniqueMap {
641 public:
642 typedef ArrayRef Operands;
643 typedef std::pair LookupKey;
510644 private:
511645 struct MapInfo {
512 typedef DenseMapInfo ConstantClassInfo;
513 static inline ConstantClass *getEmptyKey() {
646 typedef DenseMapInfo ConstantClassInfo;
647 typedef DenseMapInfo ConstantInfo;
648 typedef DenseMapInfo TypeClassInfo;
649 static inline ConstantClass* getEmptyKey() {
514650 return ConstantClassInfo::getEmptyKey();
515651 }
516 static inline ConstantClass *getTombstoneKey() {
652 static inline ConstantClass* getTombstoneKey() {
517653 return ConstantClassInfo::getTombstoneKey();
518654 }
519655 static unsigned getHashValue(const ConstantClass *CP) {
520 SmallVector Storage;
521 return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
656 SmallVector CPOperands;
657 CPOperands.reserve(CP->getNumOperands());
658 for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I)
659 CPOperands.push_back(CP->getOperand(I));
660 return getHashValue(LookupKey(CP->getType(), CPOperands));
522661 }
523662 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
524663 return LHS == RHS;
525664 }
526665 static unsigned getHashValue(const LookupKey &Val) {
527 return hash_combine(Val.first, Val.second.getHash());
666 return hash_combine(Val.first, hash_combine_range(Val.second.begin(),
667 Val.second.end()));
528668 }
529669 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
530670 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
531671 return false;
532 if (LHS.first != RHS->getType())
672 if (LHS.first != RHS->getType()
673 || LHS.second.size() != RHS->getNumOperands())
533674 return false;
534 return LHS.second == RHS;
675 for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) {
676 if (LHS.second[I] != RHS->getOperand(I))
677 return false;
678 }
679 return true;
535680 }
536681 };
537
538682 public:
539683 typedef DenseMap MapTy;
540684
541685 private:
686 /// Map - This is the main map from the element descriptor to the Constants.
687 /// This is the primary way we avoid creating two of the same shape
688 /// constant.
542689 MapTy Map;
543690
544691 public:
546693 typename MapTy::iterator map_end() { return Map.end(); }
547694
548695 void freeConstants() {
549 for (auto &I : Map)
696 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
697 I != E; ++I) {
550698 // Asserts that use_empty().
551 delete I.first;
699 delete I->first;
700 }
552701 }
553702
554703 private:
555 ConstantClass *create(TypeClass *Ty, ValType V) {
556 ConstantClass *Result = V.create(Ty);
704 typename MapTy::iterator findExistingElement(ConstantClass *CP) {
705 return Map.find(CP);
706 }
707
708 ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) {
709 ConstantClass* Result =
710 ConstantArrayCreator::create(Ty, V);
557711
558712 assert(Result->getType() == Ty && "Type specified is not correct!");
559 insert(Result);
713 Map[Result] = '\0';
560714
561715 return Result;
562716 }
563
564 public:
565 /// Return the specified constant from the map, creating it if necessary.
566 ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
717 public:
718
719 /// getOrCreate - Return the specified constant from the map, creating it if
720 /// necessary.
721 ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
567722 LookupKey Lookup(Ty, V);
568 ConstantClass *Result = nullptr;
569
570 auto I = find(Lookup);
571 if (I == Map.end())
572 Result = create(Ty, V);
573 else
723 ConstantClass* Result = nullptr;
724
725 typename MapTy::iterator I = Map.find_as(Lookup);
726 // Is it in the map?
727 if (I != Map.end())
574728 Result = I->first;
575 assert(Result && "Unexpected nullptr");
729
730 if (!Result) {
731 // If no preexisting value, create one now...
732 Result = Create(Ty, V, I);
733 }
576734
577735 return Result;
578736 }
583741 }
584742
585743 /// Insert the constant into its proper slot.
586 void insert(ConstantClass *CP) { Map[CP] = '\0'; }
744 void insert(ConstantClass *CP) {
745 Map[CP] = '\0';
746 }
587747
588748 /// Remove this constant from the map
589749 void remove(ConstantClass *CP) {
590 typename MapTy::iterator I = Map.find(CP);
750 typename MapTy::iterator I = findExistingElement(CP);
591751 assert(I != Map.end() && "Constant not found in constant table!");
592752 assert(I->first == CP && "Didn't find correct element?");
593753 Map.erase(I);
594754 }
595755
596 void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
756 void dump() const {
757 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
758 }
597759 };
598760
599761 } // end namespace llvm
7474 // Free the constants. This is important to do here to ensure that they are
7575 // freed before the LeakDetector is torn down.
7676 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
77 DropFirst());
77 DropReferences());
7878 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
7979 DropFirst());
8080 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
271271
272272 DenseMap CAZConstants;
273273
274 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
274 typedef ConstantAggrUniqueMapConstantArray> ArrayConstantsTy;
275275 ArrayConstantsTy ArrayConstants;
276276
277 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
277 typedef ConstantAggrUniqueMapConstantStruct> StructConstantsTy;
278278 StructConstantsTy StructConstants;
279279
280 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
280 typedef ConstantAggrUniqueMapConstantVector> VectorConstantsTy;
281281 VectorConstantsTy VectorConstants;
282282
283283 DenseMap CPNConstants;
288288
289289 DenseMap, BlockAddress *>
290290 BlockAddresses;
291 ConstantUniqueMap ExprConstants;
292
293 ConstantUniqueMap InlineAsms;
294
291 ConstantUniqueMap
292 ExprConstants;
293
294 ConstantUniqueMap
295 InlineAsm> InlineAsms;
296
295297 ConstantInt *TheTrueVal;
296298 ConstantInt *TheFalseVal;
297299
273273
274274 #undef CHECK
275275
276 TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
277 LLVMContext Context;
278 std::unique_ptr M(new Module("MyModule", Context));
279
280 Type *IntTy = Type::getInt8Ty(Context);
281 ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
282 Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
283 ConstantInt::get(IntTy, 1)};
284 Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
285
286 Constant *Global = new GlobalVariable(*M, IntTy, false,
287 GlobalValue::ExternalLinkage, nullptr);
288 Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
289 Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
290 Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
291 ASSERT_NE(A01, A0G);
292
293 GlobalVariable *RefArray =
294 new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
295 ASSERT_EQ(A0G, RefArray->getInitializer());
296
297 GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
298 ASSERT_EQ(A01, RefArray->getInitializer());
299 }
300
301276 } // end anonymous namespace
302277 } // end namespace llvm