llvm.org GIT mirror llvm / 7116af6
Reapply r215966, r215965, r215964, r215963, r215960, r215959, r215958, and r215957 This reverts commit r215981, which reverted the above commits because MSVC std::equal asserts on nullptr iterators, and thes commits introduced an `ArrayRef::equals()` on empty ArrayRefs. ArrayRef was changed not to use std::equal in r215986. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215987 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 6 years ago
8 changed file(s) with 466 addition(s) and 530 deletion(s). Raw diff Collapse all Expand all
4747 : User(ty, vty, Ops, NumOps) {}
4848
4949 void destroyConstantImpl();
50 void replaceUsesOfWithOnConstantImpl(Constant *Replacement);
51
5052 public:
5153 /// isNullValue - Return true if this is the value that would be returned by
5254 /// getNullValue.
3636 class VectorType;
3737 class SequentialType;
3838
39 template
40 struct ConstantCreator;
41 template
42 struct ConstantArrayCreator;
43 template
44 struct ConvertConstantType;
39 struct ConstantExprKeyType;
40 template struct ConstantAggrKeyType;
4541
4642 //===----------------------------------------------------------------------===//
4743 /// This is the shared class of boolean and integer constants. This class
337333 /// ConstantArray - Constant Array Declarations
338334 ///
339335 class ConstantArray : public Constant {
340 friend struct ConstantArrayCreator>;
336 friend struct ConstantAggrKeyType>;
341337 ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION;
342338 protected:
343339 ConstantArray(ArrayType *T, ArrayRef Val);
345341 // ConstantArray accessors
346342 static Constant *get(ArrayType *T, ArrayRef V);
347343
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 ConstantArrayCreator>;
378 friend struct ConstantAggrKeyType>;
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 ConstantArrayCreator>;
437 friend struct ConstantAggrKeyType>;
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:
445449 /// getSplat - Return a ConstantVector with the specified constant in each
446450 /// element.
447451 static Constant *getSplat(unsigned NumElts, Constant *Elt);
793797 /// constant expressions. The Opcode field for the ConstantExpr class is
794798 /// maintained in the Value::SubclassData field.
795799 class ConstantExpr : public Constant {
796 friend struct ConstantCreator
797 std::pair > >;
798 friend struct ConvertConstantType;
800 friend struct ConstantExprKeyType;
799801
800802 protected:
801803 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
11121114 void setValueSubclassData(unsigned short D) {
11131115 Value::setValueSubclassData(D);
11141116 }
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;
11151123 };
11161124
11171125 template <>
2424 class PointerType;
2525 class FunctionType;
2626 class Module;
27
2728 struct InlineAsmKeyType;
28 template
29 bool HasLargeKey>
30 class ConstantUniqueMap;
31 template
32 struct ConstantCreator;
29 template class ConstantUniqueMap;
3330
3431 class InlineAsm : public Value {
3532 public:
3936 };
4037
4138 private:
42 friend struct ConstantCreator;
43 friend class ConstantUniqueMap
44 PointerType, InlineAsm, false>;
39 friend struct InlineAsmKeyType;
40 friend class ConstantUniqueMap;
4541
4642 InlineAsm(const InlineAsm &) LLVM_DELETED_FUNCTION;
4743 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) {
805810 // Empty arrays are canonicalized to ConstantAggregateZero.
806811 if (V.empty())
807812 return ConstantAggregateZero::get(Ty);
810815 assert(V[i]->getType() == Ty->getElementType() &&
811816 "Wrong type in array element initializer");
812817 }
813 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
814818
815819 // If this is an all-zero array, return a ConstantAggregateZero object. If
816820 // all undef, return an UndefValue, if "all simple", then return a
892896 }
893897
894898 // Otherwise, we really do want to create a ConstantArray.
895 return pImpl->ArrayConstants.getOrCreate(Ty, V);
899 return nullptr;
896900 }
897901
898902 /// getTypeForElements - Return an anonymous struct type to use for a constant
980984
981985 // ConstantVector accessors.
982986 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) {
983993 assert(!V.empty() && "Vectors can't be empty");
984994 VectorType *T = VectorType::get(V.front()->getType(), V.size());
985 LLVMContextImpl *pImpl = T->getContext().pImpl;
986995
987996 // If this is an all-undef or all-zero vector, return a
988997 // ConstantAggregateZero or UndefValue.
10741083
10751084 // Otherwise, the element type isn't compatible with ConstantDataVector, or
10761085 // the operand list constants a ConstantExpr or something else strange.
1077 return pImpl->VectorConstants.getOrCreate(T, V);
1086 return nullptr;
10781087 }
10791088
10801089 Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
14681477 // and return early.
14691478 BlockAddress *&NewBA =
14701479 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
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);
1480 if (NewBA) {
1481 replaceUsesOfWithOnConstantImpl(NewBA);
14821482 return;
14831483 }
14841484
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();
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);
14921495 }
14931496
14941497 //---- ConstantExpr::get() implementations.
15061509 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
15071510
15081511 // Look up the constant in the table first to ensure uniqueness.
1509 ExprMapKeyType Key(opc, C);
1512 ConstantExprKeyType Key(opc, C);
15101513
15111514 return pImpl->ExprConstants.getOrCreate(Ty, Key);
15121515 }
18411844 return FC; // Fold a few common cases.
18421845
18431846 Constant *ArgVec[] = { C1, C2 };
1844 ExprMapKeyType Key(Opcode, ArgVec, 0, Flags);
1847 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
18451848
18461849 LLVMContextImpl *pImpl = C1->getContext().pImpl;
18471850 return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
19181921 return SC; // Fold common cases
19191922
19201923 Constant *ArgVec[] = { C, V1, V2 };
1921 ExprMapKeyType Key(Instruction::Select, ArgVec);
1924 ConstantExprKeyType Key(Instruction::Select, ArgVec);
19221925
19231926 LLVMContextImpl *pImpl = C->getContext().pImpl;
19241927 return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
19531956 "getelementptr index type missmatch");
19541957 ArgVec.push_back(cast(Idxs[i]));
19551958 }
1956 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
1957 InBounds ? GEPOperator::IsInBounds : 0);
1959 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
1960 InBounds ? GEPOperator::IsInBounds : 0);
19581961
19591962 LLVMContextImpl *pImpl = C->getContext().pImpl;
19601963 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
19721975 // Look up the constant in the table first to ensure uniqueness
19731976 Constant *ArgVec[] = { LHS, RHS };
19741977 // Get the key type with both the opcode and predicate
1975 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
1978 const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
19761979
19771980 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
19781981 if (VectorType *VT = dyn_cast(LHS->getType()))
19931996 // Look up the constant in the table first to ensure uniqueness
19941997 Constant *ArgVec[] = { LHS, RHS };
19951998 // Get the key type with both the opcode and predicate
1996 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
1999 const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
19972000
19982001 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
19992002 if (VectorType *VT = dyn_cast(LHS->getType()))
20142017
20152018 // Look up the constant in the table first to ensure uniqueness
20162019 Constant *ArgVec[] = { Val, Idx };
2017 const ExprMapKeyType Key(Instruction::ExtractElement, ArgVec);
2020 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
20182021
20192022 LLVMContextImpl *pImpl = Val->getContext().pImpl;
20202023 Type *ReqTy = Val->getType()->getVectorElementType();
20342037 return FC; // Fold a few common cases.
20352038 // Look up the constant in the table first to ensure uniqueness
20362039 Constant *ArgVec[] = { Val, Elt, Idx };
2037 const ExprMapKeyType Key(Instruction::InsertElement, ArgVec);
2040 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
20382041
20392042 LLVMContextImpl *pImpl = Val->getContext().pImpl;
20402043 return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
20542057
20552058 // Look up the constant in the table first to ensure uniqueness
20562059 Constant *ArgVec[] = { V1, V2, Mask };
2057 const ExprMapKeyType Key(Instruction::ShuffleVector, ArgVec);
2060 const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
20582061
20592062 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
20602063 return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
20742077 return FC;
20752078
20762079 Constant *ArgVec[] = { Agg, Val };
2077 const ExprMapKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
2080 const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
20782081
20792082 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
20802083 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
20952098 return FC;
20962099
20972100 Constant *ArgVec[] = { Agg };
2098 const ExprMapKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
2101 const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
20992102
21002103 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
21012104 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
26512654 /// work, but would be really slow because it would have to unique each updated
26522655 /// array instance.
26532656 ///
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
26542668 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
26552669 Use *U) {
26562670 assert(isa(To) && "Cannot make Constant refer to non-constant!");
26772691 AllSame &= Val == ToC;
26782692 }
26792693
2680 Constant *Replacement = nullptr;
26812694 if (AllSame && ToC->isNullValue()) {
2682 Replacement = ConstantAggregateZero::get(getType());
2683 } else if (AllSame && isa(ToC)) {
2684 Replacement = UndefValue::get(getType());
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);
26852733 } else {
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();
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);
27262739 }
27272740
27282741 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
27622775
27632776 LLVMContextImpl *pImpl = getContext().pImpl;
27642777
2765 Constant *Replacement = nullptr;
27662778 if (isAllZeros) {
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 =
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 =
27752791 pImpl->StructConstants.find(Lookup);
27762792
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();
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);
28002807 }
28012808
28022809 void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
28032810 Use *U) {
28042811 assert(isa(To) && "Cannot make Constant refer to non-constant!");
2812 Constant *ToC = cast(To);
28052813
28062814 SmallVector Values;
28072815 Values.reserve(getNumOperands()); // Build replacement array...
2816 unsigned NumUpdated = 0;
28082817 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
28092818 Constant *Val = getOperand(i);
2810 if (Val == From) Val = cast(To);
2819 if (Val == From) {
2820 ++NumUpdated;
2821 Val = ToC;
2822 }
28112823 Values.push_back(Val);
28122824 }
28132825
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();
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);
28222847 }
28232848
28242849 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
28352860 Constant *Replacement = getWithOperands(NewOps);
28362861 assert(Replacement != this && "I didn't contain From!");
28372862
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
28382882 // Everyone using this now uses the replacement.
28392883 replaceAllUsesWith(Replacement);
28402884
28412885 // Delete the old constant!
28422886 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;
28432912 }
28442913
28452914 Instruction *ConstantExpr::getAsInstruction() {
2828 #define DEBUG_TYPE "ir"
2929
3030 namespace llvm {
31 template
32 struct ConstantTraits;
3331
3432 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
3533 /// behind the scenes to implement unary constant exprs.
313311 };
314312 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
315313
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);
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 {
324 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);
345369 }
346370 };
347371
348372 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);
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]);
452500 }
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 {
494 typedef InlineAsmKeyType ValType;
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;
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
644510 private:
645511 struct MapInfo {
646 typedef DenseMapInfo ConstantClassInfo;
647 typedef DenseMapInfo ConstantInfo;
648 typedef DenseMapInfo TypeClassInfo;
649 static inline ConstantClass* getEmptyKey() {
512 typedef DenseMapInfo ConstantClassInfo;
513 static inline ConstantClass *getEmptyKey() {
650514 return ConstantClassInfo::getEmptyKey();
651515 }
652 static inline ConstantClass* getTombstoneKey() {
516 static inline ConstantClass *getTombstoneKey() {
653517 return ConstantClassInfo::getTombstoneKey();
654518 }
655519 static unsigned getHashValue(const ConstantClass *CP) {
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));
520 SmallVector Storage;
521 return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
661522 }
662523 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
663524 return LHS == RHS;
664525 }
665526 static unsigned getHashValue(const LookupKey &Val) {
666 return hash_combine(Val.first, hash_combine_range(Val.second.begin(),
667 Val.second.end()));
527 return hash_combine(Val.first, Val.second.getHash());
668528 }
669529 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
670530 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
671531 return false;
672 if (LHS.first != RHS->getType()
673 || LHS.second.size() != RHS->getNumOperands())
532 if (LHS.first != RHS->getType())
674533 return false;
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;
534 return LHS.second == RHS;
680535 }
681536 };
537
682538 public:
683539 typedef DenseMap MapTy;
684540
685541 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.
689542 MapTy Map;
690543
691544 public:
693546 typename MapTy::iterator map_end() { return Map.end(); }
694547
695548 void freeConstants() {
696 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
697 I != E; ++I) {
549 for (auto &I : Map)
698550 // Asserts that use_empty().
699 delete I->first;
700 }
551 delete I.first;
701552 }
702553
703554 private:
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);
555 ConstantClass *create(TypeClass *Ty, ValType V) {
556 ConstantClass *Result = V.create(Ty);
711557
712558 assert(Result->getType() == Ty && "Type specified is not correct!");
713 Map[Result] = '\0';
559 insert(Result);
714560
715561 return Result;
716562 }
717 public:
718
719 /// getOrCreate - Return the specified constant from the map, creating it if
720 /// necessary.
721 ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
563
564 public:
565 /// Return the specified constant from the map, creating it if necessary.
566 ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
722567 LookupKey Lookup(Ty, V);
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())
568 ConstantClass *Result = nullptr;
569
570 auto I = find(Lookup);
571 if (I == Map.end())
572 Result = create(Ty, V);
573 else
728574 Result = I->first;
729
730 if (!Result) {
731 // If no preexisting value, create one now...
732 Result = Create(Ty, V, I);
733 }
575 assert(Result && "Unexpected nullptr");
734576
735577 return Result;
736578 }
741583 }
742584
743585 /// Insert the constant into its proper slot.
744 void insert(ConstantClass *CP) {
745 Map[CP] = '\0';
746 }
586 void insert(ConstantClass *CP) { Map[CP] = '\0'; }
747587
748588 /// Remove this constant from the map
749589 void remove(ConstantClass *CP) {
750 typename MapTy::iterator I = findExistingElement(CP);
590 typename MapTy::iterator I = Map.find(CP);
751591 assert(I != Map.end() && "Constant not found in constant table!");
752592 assert(I->first == CP && "Didn't find correct element?");
753593 Map.erase(I);
754594 }
755595
756 void dump() const {
757 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
758 }
596 void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
759597 };
760598
761599 } // 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 DropReferences());
77 DropFirst());
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 ConstantAggrUniqueMapConstantArray> ArrayConstantsTy;
274 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
275275 ArrayConstantsTy ArrayConstants;
276276
277 typedef ConstantAggrUniqueMapConstantStruct> StructConstantsTy;
277 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
278278 StructConstantsTy StructConstants;
279279
280 typedef ConstantAggrUniqueMapConstantVector> VectorConstantsTy;
280 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
281281 VectorConstantsTy VectorConstants;
282282
283283 DenseMap CPNConstants;
288288
289289 DenseMap, BlockAddress *>
290290 BlockAddresses;
291 ConstantUniqueMap
292 ExprConstants;
293
294 ConstantUniqueMap
295 InlineAsm> InlineAsms;
296
291 ConstantUniqueMap ExprConstants;
292
293 ConstantUniqueMap InlineAsms;
294
297295 ConstantInt *TheTrueVal;
298296 ConstantInt *TheFalseVal;
299297
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
276301 } // end anonymous namespace
277302 } // end namespace llvm