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
This commit is contained in:
Duncan P. N. Exon Smith
2014-08-19 16:39:58 +00:00
parent 8c20a93bac
commit 7116af637c
8 changed files with 433 additions and 497 deletions

View File

@@ -48,6 +48,8 @@ protected:
: User(ty, vty, Ops, NumOps) {} : User(ty, vty, Ops, NumOps) {}
void destroyConstantImpl(); void destroyConstantImpl();
void replaceUsesOfWithOnConstantImpl(Constant *Replacement);
public: public:
/// isNullValue - Return true if this is the value that would be returned by /// isNullValue - Return true if this is the value that would be returned by
/// getNullValue. /// getNullValue.

View File

@@ -37,12 +37,8 @@ class PointerType;
class VectorType; class VectorType;
class SequentialType; class SequentialType;
template<class ConstantClass, class TypeClass, class ValType> struct ConstantExprKeyType;
struct ConstantCreator; template <class ConstantClass> struct ConstantAggrKeyType;
template<class ConstantClass, class TypeClass>
struct ConstantArrayCreator;
template<class ConstantClass, class TypeClass>
struct ConvertConstantType;
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// This is the shared class of boolean and integer constants. This class /// This is the shared class of boolean and integer constants. This class
@@ -338,7 +334,7 @@ public:
/// ConstantArray - Constant Array Declarations /// ConstantArray - Constant Array Declarations
/// ///
class ConstantArray : public Constant { class ConstantArray : public Constant {
friend struct ConstantArrayCreator<ConstantArray, ArrayType>; friend struct ConstantAggrKeyType<ConstantArray>;
ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION; ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION;
protected: protected:
ConstantArray(ArrayType *T, ArrayRef<Constant *> Val); ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
@@ -346,6 +342,10 @@ public:
// ConstantArray accessors // ConstantArray accessors
static Constant *get(ArrayType *T, ArrayRef<Constant*> V); static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
private:
static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
public:
/// Transparently provide more efficient getOperand methods. /// Transparently provide more efficient getOperand methods.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
@@ -376,7 +376,7 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
// ConstantStruct - Constant Struct Declarations // ConstantStruct - Constant Struct Declarations
// //
class ConstantStruct : public Constant { class ConstantStruct : public Constant {
friend struct ConstantArrayCreator<ConstantStruct, StructType>; friend struct ConstantAggrKeyType<ConstantStruct>;
ConstantStruct(const ConstantStruct &) LLVM_DELETED_FUNCTION; ConstantStruct(const ConstantStruct &) LLVM_DELETED_FUNCTION;
protected: protected:
ConstantStruct(StructType *T, ArrayRef<Constant *> Val); ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
@@ -435,7 +435,7 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
/// ConstantVector - Constant Vector Declarations /// ConstantVector - Constant Vector Declarations
/// ///
class ConstantVector : public Constant { class ConstantVector : public Constant {
friend struct ConstantArrayCreator<ConstantVector, VectorType>; friend struct ConstantAggrKeyType<ConstantVector>;
ConstantVector(const ConstantVector &) LLVM_DELETED_FUNCTION; ConstantVector(const ConstantVector &) LLVM_DELETED_FUNCTION;
protected: protected:
ConstantVector(VectorType *T, ArrayRef<Constant *> Val); ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
@@ -443,6 +443,10 @@ public:
// ConstantVector accessors // ConstantVector accessors
static Constant *get(ArrayRef<Constant*> V); static Constant *get(ArrayRef<Constant*> V);
private:
static Constant *getImpl(ArrayRef<Constant *> V);
public:
/// getSplat - Return a ConstantVector with the specified constant in each /// getSplat - Return a ConstantVector with the specified constant in each
/// element. /// element.
static Constant *getSplat(unsigned NumElts, Constant *Elt); static Constant *getSplat(unsigned NumElts, Constant *Elt);
@@ -794,9 +798,7 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
/// constant expressions. The Opcode field for the ConstantExpr class is /// constant expressions. The Opcode field for the ConstantExpr class is
/// maintained in the Value::SubclassData field. /// maintained in the Value::SubclassData field.
class ConstantExpr : public Constant { class ConstantExpr : public Constant {
friend struct ConstantCreator<ConstantExpr,Type, friend struct ConstantExprKeyType;
std::pair<unsigned, std::vector<Constant*> > >;
friend struct ConvertConstantType<ConstantExpr, Type>;
protected: protected:
ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps) ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
@@ -1113,6 +1115,12 @@ private:
void setValueSubclassData(unsigned short D) { void setValueSubclassData(unsigned short D) {
Value::setValueSubclassData(D); Value::setValueSubclassData(D);
} }
/// \brief Check whether this can become its replacement.
///
/// For use during \a replaceUsesOfWithOnConstant(), check whether we know
/// how to turn this into \a Replacement, thereby reducing RAUW traffic.
bool canBecomeReplacement(const Constant *Replacement) const;
}; };
template <> template <>

View File

@@ -25,12 +25,9 @@ namespace llvm {
class PointerType; class PointerType;
class FunctionType; class FunctionType;
class Module; class Module;
struct InlineAsmKeyType; struct InlineAsmKeyType;
template<class ValType, class ValRefType, class TypeClass, class ConstantClass, template <class ConstantClass> class ConstantUniqueMap;
bool HasLargeKey>
class ConstantUniqueMap;
template<class ConstantClass, class TypeClass, class ValType>
struct ConstantCreator;
class InlineAsm : public Value { class InlineAsm : public Value {
public: public:
@@ -40,9 +37,8 @@ public:
}; };
private: private:
friend struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType>; friend struct InlineAsmKeyType;
friend class ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, friend class ConstantUniqueMap<InlineAsm>;
PointerType, InlineAsm, false>;
InlineAsm(const InlineAsm &) LLVM_DELETED_FUNCTION; InlineAsm(const InlineAsm &) LLVM_DELETED_FUNCTION;
void operator=(const InlineAsm&) LLVM_DELETED_FUNCTION; void operator=(const InlineAsm&) LLVM_DELETED_FUNCTION;

View File

@@ -803,6 +803,11 @@ ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
} }
Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) { Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
if (Constant *C = getImpl(Ty, V))
return C;
return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
}
Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
// Empty arrays are canonicalized to ConstantAggregateZero. // Empty arrays are canonicalized to ConstantAggregateZero.
if (V.empty()) if (V.empty())
return ConstantAggregateZero::get(Ty); return ConstantAggregateZero::get(Ty);
@@ -811,7 +816,6 @@ Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
assert(V[i]->getType() == Ty->getElementType() && assert(V[i]->getType() == Ty->getElementType() &&
"Wrong type in array element initializer"); "Wrong type in array element initializer");
} }
LLVMContextImpl *pImpl = Ty->getContext().pImpl;
// If this is an all-zero array, return a ConstantAggregateZero object. If // If this is an all-zero array, return a ConstantAggregateZero object. If
// all undef, return an UndefValue, if "all simple", then return a // all undef, return an UndefValue, if "all simple", then return a
@@ -893,7 +897,7 @@ Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
} }
// Otherwise, we really do want to create a ConstantArray. // Otherwise, we really do want to create a ConstantArray.
return pImpl->ArrayConstants.getOrCreate(Ty, V); return nullptr;
} }
/// getTypeForElements - Return an anonymous struct type to use for a constant /// getTypeForElements - Return an anonymous struct type to use for a constant
@@ -981,9 +985,14 @@ ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
// ConstantVector accessors. // ConstantVector accessors.
Constant *ConstantVector::get(ArrayRef<Constant*> V) { Constant *ConstantVector::get(ArrayRef<Constant*> V) {
if (Constant *C = getImpl(V))
return C;
VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
}
Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
assert(!V.empty() && "Vectors can't be empty"); assert(!V.empty() && "Vectors can't be empty");
VectorType *T = VectorType::get(V.front()->getType(), V.size()); VectorType *T = VectorType::get(V.front()->getType(), V.size());
LLVMContextImpl *pImpl = T->getContext().pImpl;
// If this is an all-undef or all-zero vector, return a // If this is an all-undef or all-zero vector, return a
// ConstantAggregateZero or UndefValue. // ConstantAggregateZero or UndefValue.
@@ -1075,7 +1084,7 @@ Constant *ConstantVector::get(ArrayRef<Constant*> V) {
// Otherwise, the element type isn't compatible with ConstantDataVector, or // Otherwise, the element type isn't compatible with ConstantDataVector, or
// the operand list constants a ConstantExpr or something else strange. // the operand list constants a ConstantExpr or something else strange.
return pImpl->VectorConstants.getOrCreate(T, V); return nullptr;
} }
Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) { Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
@@ -1469,27 +1478,21 @@ void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
// and return early. // and return early.
BlockAddress *&NewBA = BlockAddress *&NewBA =
getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)]; getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
if (!NewBA) { if (NewBA) {
getBasicBlock()->AdjustBlockAddressRefCount(-1); replaceUsesOfWithOnConstantImpl(NewBA);
// Remove the old entry, this can't cause the map to rehash (just a
// tombstone will get added).
getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
getBasicBlock()));
NewBA = this;
setOperand(0, NewF);
setOperand(1, NewBB);
getBasicBlock()->AdjustBlockAddressRefCount(1);
return; return;
} }
// Otherwise, I do need to replace this with an existing value. getBasicBlock()->AdjustBlockAddressRefCount(-1);
assert(NewBA != this && "I didn't contain From!");
// Everyone using this now uses the replacement. // Remove the old entry, this can't cause the map to rehash (just a
replaceAllUsesWith(NewBA); // tombstone will get added).
getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
destroyConstant(); getBasicBlock()));
NewBA = this;
setOperand(0, NewF);
setOperand(1, NewBB);
getBasicBlock()->AdjustBlockAddressRefCount(1);
} }
//---- ConstantExpr::get() implementations. //---- ConstantExpr::get() implementations.
@@ -1507,7 +1510,7 @@ static inline Constant *getFoldedCast(
LLVMContextImpl *pImpl = Ty->getContext().pImpl; LLVMContextImpl *pImpl = Ty->getContext().pImpl;
// Look up the constant in the table first to ensure uniqueness. // Look up the constant in the table first to ensure uniqueness.
ExprMapKeyType Key(opc, C); ConstantExprKeyType Key(opc, C);
return pImpl->ExprConstants.getOrCreate(Ty, Key); return pImpl->ExprConstants.getOrCreate(Ty, Key);
} }
@@ -1842,7 +1845,7 @@ Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
return FC; // Fold a few common cases. return FC; // Fold a few common cases.
Constant *ArgVec[] = { C1, C2 }; Constant *ArgVec[] = { C1, C2 };
ExprMapKeyType Key(Opcode, ArgVec, 0, Flags); ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
LLVMContextImpl *pImpl = C1->getContext().pImpl; LLVMContextImpl *pImpl = C1->getContext().pImpl;
return pImpl->ExprConstants.getOrCreate(C1->getType(), Key); return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
@@ -1919,7 +1922,7 @@ Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2) {
return SC; // Fold common cases return SC; // Fold common cases
Constant *ArgVec[] = { C, V1, V2 }; Constant *ArgVec[] = { C, V1, V2 };
ExprMapKeyType Key(Instruction::Select, ArgVec); ConstantExprKeyType Key(Instruction::Select, ArgVec);
LLVMContextImpl *pImpl = C->getContext().pImpl; LLVMContextImpl *pImpl = C->getContext().pImpl;
return pImpl->ExprConstants.getOrCreate(V1->getType(), Key); return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
@@ -1954,8 +1957,8 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef<Value *> Idxs,
"getelementptr index type missmatch"); "getelementptr index type missmatch");
ArgVec.push_back(cast<Constant>(Idxs[i])); ArgVec.push_back(cast<Constant>(Idxs[i]));
} }
const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0, const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
InBounds ? GEPOperator::IsInBounds : 0); InBounds ? GEPOperator::IsInBounds : 0);
LLVMContextImpl *pImpl = C->getContext().pImpl; LLVMContextImpl *pImpl = C->getContext().pImpl;
return pImpl->ExprConstants.getOrCreate(ReqTy, Key); return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
@@ -1973,7 +1976,7 @@ ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) {
// Look up the constant in the table first to ensure uniqueness // Look up the constant in the table first to ensure uniqueness
Constant *ArgVec[] = { LHS, RHS }; Constant *ArgVec[] = { LHS, RHS };
// Get the key type with both the opcode and predicate // Get the key type with both the opcode and predicate
const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred); const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
Type *ResultTy = Type::getInt1Ty(LHS->getContext()); Type *ResultTy = Type::getInt1Ty(LHS->getContext());
if (VectorType *VT = dyn_cast<VectorType>(LHS->getType())) if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
@@ -1994,7 +1997,7 @@ ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) {
// Look up the constant in the table first to ensure uniqueness // Look up the constant in the table first to ensure uniqueness
Constant *ArgVec[] = { LHS, RHS }; Constant *ArgVec[] = { LHS, RHS };
// Get the key type with both the opcode and predicate // Get the key type with both the opcode and predicate
const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred); const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
Type *ResultTy = Type::getInt1Ty(LHS->getContext()); Type *ResultTy = Type::getInt1Ty(LHS->getContext());
if (VectorType *VT = dyn_cast<VectorType>(LHS->getType())) if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
@@ -2015,7 +2018,7 @@ Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
// Look up the constant in the table first to ensure uniqueness // Look up the constant in the table first to ensure uniqueness
Constant *ArgVec[] = { Val, Idx }; Constant *ArgVec[] = { Val, Idx };
const ExprMapKeyType Key(Instruction::ExtractElement, ArgVec); const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
LLVMContextImpl *pImpl = Val->getContext().pImpl; LLVMContextImpl *pImpl = Val->getContext().pImpl;
Type *ReqTy = Val->getType()->getVectorElementType(); Type *ReqTy = Val->getType()->getVectorElementType();
@@ -2035,7 +2038,7 @@ Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
return FC; // Fold a few common cases. return FC; // Fold a few common cases.
// Look up the constant in the table first to ensure uniqueness // Look up the constant in the table first to ensure uniqueness
Constant *ArgVec[] = { Val, Elt, Idx }; Constant *ArgVec[] = { Val, Elt, Idx };
const ExprMapKeyType Key(Instruction::InsertElement, ArgVec); const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
LLVMContextImpl *pImpl = Val->getContext().pImpl; LLVMContextImpl *pImpl = Val->getContext().pImpl;
return pImpl->ExprConstants.getOrCreate(Val->getType(), Key); return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
@@ -2055,7 +2058,7 @@ Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
// Look up the constant in the table first to ensure uniqueness // Look up the constant in the table first to ensure uniqueness
Constant *ArgVec[] = { V1, V2, Mask }; Constant *ArgVec[] = { V1, V2, Mask };
const ExprMapKeyType Key(Instruction::ShuffleVector, ArgVec); const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
LLVMContextImpl *pImpl = ShufTy->getContext().pImpl; LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
return pImpl->ExprConstants.getOrCreate(ShufTy, Key); return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
@@ -2075,7 +2078,7 @@ Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
return FC; return FC;
Constant *ArgVec[] = { Agg, Val }; Constant *ArgVec[] = { Agg, Val };
const ExprMapKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs); const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
LLVMContextImpl *pImpl = Agg->getContext().pImpl; LLVMContextImpl *pImpl = Agg->getContext().pImpl;
return pImpl->ExprConstants.getOrCreate(ReqTy, Key); return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
@@ -2096,7 +2099,7 @@ Constant *ConstantExpr::getExtractValue(Constant *Agg,
return FC; return FC;
Constant *ArgVec[] = { Agg }; Constant *ArgVec[] = { Agg };
const ExprMapKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs); const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
LLVMContextImpl *pImpl = Agg->getContext().pImpl; LLVMContextImpl *pImpl = Agg->getContext().pImpl;
return pImpl->ExprConstants.getOrCreate(ReqTy, Key); return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
@@ -2652,6 +2655,17 @@ Constant *ConstantDataVector::getSplatValue() const {
/// work, but would be really slow because it would have to unique each updated /// work, but would be really slow because it would have to unique each updated
/// array instance. /// array instance.
/// ///
void Constant::replaceUsesOfWithOnConstantImpl(Constant *Replacement) {
// I do need to replace this with an existing value.
assert(Replacement != this && "I didn't contain From!");
// Everyone using this now uses the replacement.
replaceAllUsesWith(Replacement);
// Delete the old constant!
destroyConstant();
}
void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Use *U) { Use *U) {
assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
@@ -2678,52 +2692,51 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
AllSame &= Val == ToC; AllSame &= Val == ToC;
} }
Constant *Replacement = nullptr;
if (AllSame && ToC->isNullValue()) { if (AllSame && ToC->isNullValue()) {
Replacement = ConstantAggregateZero::get(getType()); replaceUsesOfWithOnConstantImpl(ConstantAggregateZero::get(getType()));
} else if (AllSame && isa<UndefValue>(ToC)) { return;
Replacement = UndefValue::get(getType()); }
} else { if (AllSame && isa<UndefValue>(ToC)) {
// Check to see if we have this array type already. replaceUsesOfWithOnConstantImpl(UndefValue::get(getType()));
LLVMContextImpl::ArrayConstantsTy::LookupKey Lookup( return;
cast<ArrayType>(getType()), makeArrayRef(Values));
LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
pImpl->ArrayConstants.find(Lookup);
if (I != pImpl->ArrayConstants.map_end()) {
Replacement = I->first;
} else {
// Okay, the new shape doesn't exist in the system yet. Instead of
// creating a new constant array, inserting it, replaceallusesof'ing the
// old with the new, then deleting the old... just update the current one
// in place!
pImpl->ArrayConstants.remove(this);
// Update to the new value. Optimize for the case when we have a single
// operand that we're changing, but handle bulk updates efficiently.
if (NumUpdated == 1) {
unsigned OperandToUpdate = U - OperandList;
assert(getOperand(OperandToUpdate) == From &&
"ReplaceAllUsesWith broken!");
setOperand(OperandToUpdate, ToC);
} else {
for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
if (getOperand(i) == From)
setOperand(i, ToC);
}
pImpl->ArrayConstants.insert(this);
return;
}
} }
// Otherwise, I do need to replace this with an existing value. // Check for any other type of constant-folding.
assert(Replacement != this && "I didn't contain From!"); if (Constant *C = getImpl(getType(), Values)) {
replaceUsesOfWithOnConstantImpl(C);
return;
}
// Everyone using this now uses the replacement. // Check to see if we have this array type already.
replaceAllUsesWith(Replacement); LLVMContextImpl::ArrayConstantsTy::LookupKey Lookup(
cast<ArrayType>(getType()), makeArrayRef(Values));
LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
pImpl->ArrayConstants.find(Lookup);
// Delete the old constant! if (I != pImpl->ArrayConstants.map_end()) {
destroyConstant(); replaceUsesOfWithOnConstantImpl(I->first);
return;
}
// Okay, the new shape doesn't exist in the system yet. Instead of
// creating a new constant array, inserting it, replaceallusesof'ing the
// old with the new, then deleting the old... just update the current one
// in place!
pImpl->ArrayConstants.remove(this);
// Update to the new value. Optimize for the case when we have a single
// operand that we're changing, but handle bulk updates efficiently.
if (NumUpdated == 1) {
unsigned OperandToUpdate = U - OperandList;
assert(getOperand(OperandToUpdate) == From &&
"ReplaceAllUsesWith broken!");
setOperand(OperandToUpdate, ToC);
} else {
for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
if (getOperand(I) == From)
setOperand(I, ToC);
}
pImpl->ArrayConstants.insert(this);
} }
void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
@@ -2763,63 +2776,75 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
LLVMContextImpl *pImpl = getContext().pImpl; LLVMContextImpl *pImpl = getContext().pImpl;
Constant *Replacement = nullptr;
if (isAllZeros) { if (isAllZeros) {
Replacement = ConstantAggregateZero::get(getType()); replaceUsesOfWithOnConstantImpl(ConstantAggregateZero::get(getType()));
} else if (isAllUndef) { return;
Replacement = UndefValue::get(getType()); }
} else { if (isAllUndef) {
// Check to see if we have this struct type already. replaceUsesOfWithOnConstantImpl(UndefValue::get(getType()));
LLVMContextImpl::StructConstantsTy::LookupKey Lookup( return;
cast<StructType>(getType()), makeArrayRef(Values));
LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
pImpl->StructConstants.find(Lookup);
if (I != pImpl->StructConstants.map_end()) {
Replacement = I->first;
} else {
// Okay, the new shape doesn't exist in the system yet. Instead of
// creating a new constant struct, inserting it, replaceallusesof'ing the
// old with the new, then deleting the old... just update the current one
// in place!
pImpl->StructConstants.remove(this);
// Update to the new value.
setOperand(OperandToUpdate, ToC);
pImpl->StructConstants.insert(this);
return;
}
} }
assert(Replacement != this && "I didn't contain From!"); // Check to see if we have this struct type already.
LLVMContextImpl::StructConstantsTy::LookupKey Lookup(
cast<StructType>(getType()), makeArrayRef(Values));
LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
pImpl->StructConstants.find(Lookup);
// Everyone using this now uses the replacement. if (I != pImpl->StructConstants.map_end()) {
replaceAllUsesWith(Replacement); replaceUsesOfWithOnConstantImpl(I->first);
return;
}
// Delete the old constant! // Okay, the new shape doesn't exist in the system yet. Instead of
destroyConstant(); // creating a new constant struct, inserting it, replaceallusesof'ing the
// old with the new, then deleting the old... just update the current one
// in place!
pImpl->StructConstants.remove(this);
// Update to the new value.
setOperand(OperandToUpdate, ToC);
pImpl->StructConstants.insert(this);
} }
void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To, void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Use *U) { Use *U) {
assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Constant *ToC = cast<Constant>(To);
SmallVector<Constant*, 8> Values; SmallVector<Constant*, 8> Values;
Values.reserve(getNumOperands()); // Build replacement array... Values.reserve(getNumOperands()); // Build replacement array...
unsigned NumUpdated = 0;
for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Constant *Val = getOperand(i); Constant *Val = getOperand(i);
if (Val == From) Val = cast<Constant>(To); if (Val == From) {
++NumUpdated;
Val = ToC;
}
Values.push_back(Val); Values.push_back(Val);
} }
Constant *Replacement = get(Values); if (Constant *C = getImpl(Values)) {
assert(Replacement != this && "I didn't contain From!"); replaceUsesOfWithOnConstantImpl(C);
return;
}
// Everyone using this now uses the replacement. // Update to the new value. Optimize for the case when we have a single
replaceAllUsesWith(Replacement); // operand that we're changing, but handle bulk updates efficiently.
auto &pImpl = getType()->getContext().pImpl;
pImpl->VectorConstants.remove(this);
// Delete the old constant! if (NumUpdated == 1) {
destroyConstant(); unsigned OperandToUpdate = U - OperandList;
assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
setOperand(OperandToUpdate, ToC);
} else {
for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
if (getOperand(I) == From)
setOperand(I, ToC);
}
pImpl->VectorConstants.insert(this);
} }
void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
@@ -2836,6 +2861,25 @@ void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Constant *Replacement = getWithOperands(NewOps); Constant *Replacement = getWithOperands(NewOps);
assert(Replacement != this && "I didn't contain From!"); assert(Replacement != this && "I didn't contain From!");
// Check if Replacement has no users (and is the same type). Ideally, this
// check would be done *before* creating Replacement, but threading this
// through constant-folding isn't trivial.
if (canBecomeReplacement(Replacement)) {
// Avoid unnecessary RAUW traffic.
auto &ExprConstants = getType()->getContext().pImpl->ExprConstants;
ExprConstants.remove(this);
auto *CE = cast<ConstantExpr>(Replacement);
for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
// Only set the operands that have actually changed.
if (getOperand(I) != CE->getOperand(I))
setOperand(I, CE->getOperand(I));
CE->destroyConstant();
ExprConstants.insert(this);
return;
}
// Everyone using this now uses the replacement. // Everyone using this now uses the replacement.
replaceAllUsesWith(Replacement); replaceAllUsesWith(Replacement);
@@ -2843,6 +2887,31 @@ void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
destroyConstant(); destroyConstant();
} }
bool ConstantExpr::canBecomeReplacement(const Constant *Replacement) const {
// If Replacement already has users, use it regardless.
if (!Replacement->use_empty())
return false;
// Check for anything that could have changed during constant-folding.
if (getValueID() != Replacement->getValueID())
return false;
const auto *CE = cast<ConstantExpr>(Replacement);
if (getOpcode() != CE->getOpcode())
return false;
if (getNumOperands() != CE->getNumOperands())
return false;
if (getRawSubclassOptionalData() != CE->getRawSubclassOptionalData())
return false;
if (isCompare())
if (getPredicate() != CE->getPredicate())
return false;
if (hasIndices())
if (getIndices() != CE->getIndices())
return false;
return true;
}
Instruction *ConstantExpr::getAsInstruction() { Instruction *ConstantExpr::getAsInstruction() {
SmallVector<Value*,4> ValueOperands; SmallVector<Value*,4> ValueOperands;
for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)

View File

@@ -29,8 +29,6 @@
#define DEBUG_TYPE "ir" #define DEBUG_TYPE "ir"
namespace llvm { namespace llvm {
template<class ValType>
struct ConstantTraits;
/// UnaryConstantExpr - This class is private to Constants.cpp, and is used /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
/// behind the scenes to implement unary constant exprs. /// behind the scenes to implement unary constant exprs.
@@ -314,379 +312,234 @@ struct OperandTraits<CompareConstantExpr> :
}; };
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value) DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
struct ExprMapKeyType { template <class ConstantClass> struct ConstantAggrKeyType;
ExprMapKeyType(unsigned opc, struct InlineAsmKeyType;
ArrayRef<Constant*> ops, struct ConstantExprKeyType;
unsigned short flags = 0,
unsigned short optionalflags = 0, template <class ConstantClass> struct ConstantInfo;
ArrayRef<unsigned> inds = None) template <> struct ConstantInfo<ConstantExpr> {
: opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags), typedef ConstantExprKeyType ValType;
operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {} typedef Type TypeClass;
uint8_t opcode; };
uint8_t subclassoptionaldata; template <> struct ConstantInfo<InlineAsm> {
uint16_t subclassdata; typedef InlineAsmKeyType ValType;
std::vector<Constant*> operands; typedef PointerType TypeClass;
SmallVector<unsigned, 4> indices; };
bool operator==(const ExprMapKeyType& that) const { template <> struct ConstantInfo<ConstantArray> {
return this->opcode == that.opcode && typedef ConstantAggrKeyType<ConstantArray> ValType;
this->subclassdata == that.subclassdata && typedef ArrayType TypeClass;
this->subclassoptionaldata == that.subclassoptionaldata && };
this->operands == that.operands && template <> struct ConstantInfo<ConstantStruct> {
this->indices == that.indices; typedef ConstantAggrKeyType<ConstantStruct> ValType;
} typedef StructType TypeClass;
bool operator<(const ExprMapKeyType & that) const { };
return std::tie(opcode, operands, subclassdata, subclassoptionaldata, template <> struct ConstantInfo<ConstantVector> {
indices) < typedef ConstantAggrKeyType<ConstantVector> ValType;
std::tie(that.opcode, that.operands, that.subclassdata, typedef VectorType TypeClass;
that.subclassoptionaldata, that.indices); };
template <class ConstantClass> struct ConstantAggrKeyType {
ArrayRef<Constant *> Operands;
ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {}
ConstantAggrKeyType(const ConstantClass *C,
SmallVectorImpl<Constant *> &Storage) {
assert(Storage.empty() && "Expected empty storage");
for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
Storage.push_back(C->getOperand(I));
Operands = Storage;
} }
bool operator!=(const ExprMapKeyType& that) const { bool operator==(const ConstantAggrKeyType &X) const {
return !(*this == that); return Operands == X.Operands;
}
bool operator==(const ConstantClass *C) const {
if (Operands.size() != C->getNumOperands())
return false;
for (unsigned I = 0, E = Operands.size(); I != E; ++I)
if (Operands[I] != C->getOperand(I))
return false;
return true;
}
unsigned getHash() const {
return hash_combine_range(Operands.begin(), Operands.end());
}
typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
ConstantClass *create(TypeClass *Ty) const {
return new (Operands.size()) ConstantClass(Ty, Operands);
} }
}; };
struct InlineAsmKeyType { struct InlineAsmKeyType {
InlineAsmKeyType(StringRef AsmString, StringRef AsmString;
StringRef Constraints, bool hasSideEffects, StringRef Constraints;
bool isAlignStack, InlineAsm::AsmDialect asmDialect) bool HasSideEffects;
: asm_string(AsmString), constraints(Constraints), bool IsAlignStack;
has_side_effects(hasSideEffects), is_align_stack(isAlignStack), InlineAsm::AsmDialect AsmDialect;
asm_dialect(asmDialect) {}
std::string asm_string; InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
std::string constraints; bool HasSideEffects, bool IsAlignStack,
bool has_side_effects; InlineAsm::AsmDialect AsmDialect)
bool is_align_stack; : AsmString(AsmString), Constraints(Constraints),
InlineAsm::AsmDialect asm_dialect; HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
bool operator==(const InlineAsmKeyType& that) const { AsmDialect(AsmDialect) {}
return this->asm_string == that.asm_string && InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &)
this->constraints == that.constraints && : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
this->has_side_effects == that.has_side_effects && HasSideEffects(Asm->hasSideEffects()),
this->is_align_stack == that.is_align_stack && IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {}
this->asm_dialect == that.asm_dialect;
bool operator==(const InlineAsmKeyType &X) const {
return HasSideEffects == X.HasSideEffects &&
IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
AsmString == X.AsmString && Constraints == X.Constraints;
} }
bool operator<(const InlineAsmKeyType& that) const { bool operator==(const InlineAsm *Asm) const {
return std::tie(asm_string, constraints, has_side_effects, is_align_stack, return HasSideEffects == Asm->hasSideEffects() &&
asm_dialect) < IsAlignStack == Asm->isAlignStack() &&
std::tie(that.asm_string, that.constraints, that.has_side_effects, AsmDialect == Asm->getDialect() &&
that.is_align_stack, that.asm_dialect); AsmString == Asm->getAsmString() &&
Constraints == Asm->getConstraintString();
}
unsigned getHash() const {
return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
AsmDialect);
} }
bool operator!=(const InlineAsmKeyType& that) const { typedef ConstantInfo<InlineAsm>::TypeClass TypeClass;
return !(*this == that); InlineAsm *create(TypeClass *Ty) const {
return new InlineAsm(Ty, AsmString, Constraints, HasSideEffects,
IsAlignStack, AsmDialect);
} }
}; };
// The number of operands for each ConstantCreator::create method is struct ConstantExprKeyType {
// determined by the ConstantTraits template. uint8_t Opcode;
// ConstantCreator - A class that is used to create constants by uint8_t SubclassOptionalData;
// ConstantUniqueMap*. This class should be partially specialized if there is uint16_t SubclassData;
// something strange that needs to be done to interface to the ctor for the ArrayRef<Constant *> Ops;
// constant. ArrayRef<unsigned> Indexes;
//
template<typename T, typename Alloc>
struct ConstantTraits< std::vector<T, Alloc> > {
static unsigned uses(const std::vector<T, Alloc>& v) {
return v.size();
}
};
template<> ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops,
struct ConstantTraits<Constant *> { unsigned short SubclassData = 0,
static unsigned uses(Constant * const & v) { unsigned short SubclassOptionalData = 0,
return 1; ArrayRef<unsigned> Indexes = None)
: Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
SubclassData(SubclassData), Ops(Ops), Indexes(Indexes) {}
ConstantExprKeyType(const ConstantExpr *CE,
SmallVectorImpl<Constant *> &Storage)
: Opcode(CE->getOpcode()),
SubclassOptionalData(CE->getRawSubclassOptionalData()),
SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {
assert(Storage.empty() && "Expected empty storage");
for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
Storage.push_back(CE->getOperand(I));
Ops = Storage;
} }
};
template<class ConstantClass, class TypeClass, class ValType> bool operator==(const ConstantExprKeyType &X) const {
struct ConstantCreator { return Opcode == X.Opcode && SubclassData == X.SubclassData &&
static ConstantClass *create(TypeClass *Ty, const ValType &V) { SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V); Indexes == X.Indexes;
} }
};
template<class ConstantClass, class TypeClass> bool operator==(const ConstantExpr *CE) const {
struct ConstantArrayCreator { if (Opcode != CE->getOpcode())
static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) { return false;
return new(V.size()) ConstantClass(Ty, V); if (SubclassOptionalData != CE->getRawSubclassOptionalData())
return false;
if (Ops.size() != CE->getNumOperands())
return false;
if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
return false;
for (unsigned I = 0, E = Ops.size(); I != E; ++I)
if (Ops[I] != CE->getOperand(I))
return false;
if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()))
return false;
return true;
} }
};
template<class ConstantClass> unsigned getHash() const {
struct ConstantKeyData { return hash_combine(Opcode, SubclassOptionalData, SubclassData,
typedef void ValType; hash_combine_range(Ops.begin(), Ops.end()),
static ValType getValType(ConstantClass *C) { hash_combine_range(Indexes.begin(), Indexes.end()));
llvm_unreachable("Unknown Constant type!");
} }
};
template<> typedef ConstantInfo<ConstantExpr>::TypeClass TypeClass;
struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { ConstantExpr *create(TypeClass *Ty) const {
static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V, switch (Opcode) {
unsigned short pred = 0) { default:
if (Instruction::isCast(V.opcode)) if (Instruction::isCast(Opcode))
return new UnaryConstantExpr(V.opcode, V.operands[0], Ty); return new UnaryConstantExpr(Opcode, Ops[0], Ty);
if ((V.opcode >= Instruction::BinaryOpsBegin && if ((Opcode >= Instruction::BinaryOpsBegin &&
V.opcode < Instruction::BinaryOpsEnd)) Opcode < Instruction::BinaryOpsEnd))
return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1], return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
V.subclassoptionaldata); SubclassOptionalData);
if (V.opcode == Instruction::Select) llvm_unreachable("Invalid ConstantExpr!");
return new SelectConstantExpr(V.operands[0], V.operands[1], case Instruction::Select:
V.operands[2]); return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
if (V.opcode == Instruction::ExtractElement) case Instruction::ExtractElement:
return new ExtractElementConstantExpr(V.operands[0], V.operands[1]); return new ExtractElementConstantExpr(Ops[0], Ops[1]);
if (V.opcode == Instruction::InsertElement) case Instruction::InsertElement:
return new InsertElementConstantExpr(V.operands[0], V.operands[1], return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
V.operands[2]); case Instruction::ShuffleVector:
if (V.opcode == Instruction::ShuffleVector) return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]);
return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1], case Instruction::InsertValue:
V.operands[2]); return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
if (V.opcode == Instruction::InsertValue) case Instruction::ExtractValue:
return new InsertValueConstantExpr(V.operands[0], V.operands[1], return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
V.indices, Ty); case Instruction::GetElementPtr:
if (V.opcode == Instruction::ExtractValue) return GetElementPtrConstantExpr::Create(Ops[0], Ops.slice(1), Ty,
return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty); SubclassOptionalData);
if (V.opcode == Instruction::GetElementPtr) { case Instruction::ICmp:
std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end()); return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty, Ops[0], Ops[1]);
V.subclassoptionaldata); case Instruction::FCmp:
return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
Ops[0], Ops[1]);
} }
// The compare instructions are weird. We have to encode the predicate
// value and it is combined with the instruction opcode by multiplying
// the opcode by one hundred. We must decode this to get the predicate.
if (V.opcode == Instruction::ICmp)
return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
V.operands[0], V.operands[1]);
if (V.opcode == Instruction::FCmp)
return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
V.operands[0], V.operands[1]);
llvm_unreachable("Invalid ConstantExpr!");
} }
}; };
template<> template <class ConstantClass> class ConstantUniqueMap {
struct ConstantKeyData<ConstantExpr> {
typedef ExprMapKeyType ValType;
static ValType getValType(ConstantExpr *CE) {
std::vector<Constant*> Operands;
Operands.reserve(CE->getNumOperands());
for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
Operands.push_back(cast<Constant>(CE->getOperand(i)));
return ExprMapKeyType(CE->getOpcode(), Operands,
CE->isCompare() ? CE->getPredicate() : 0,
CE->getRawSubclassOptionalData(),
CE->hasIndices() ?
CE->getIndices() : ArrayRef<unsigned>());
}
};
template<>
struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
return new InlineAsm(Ty, Key.asm_string, Key.constraints,
Key.has_side_effects, Key.is_align_stack,
Key.asm_dialect);
}
};
template<>
struct ConstantKeyData<InlineAsm> {
typedef InlineAsmKeyType ValType;
static ValType getValType(InlineAsm *Asm) {
return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
Asm->hasSideEffects(), Asm->isAlignStack(),
Asm->getDialect());
}
};
template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
bool HasLargeKey = false /*true for arrays and structs*/ >
class ConstantUniqueMap {
public: public:
typedef std::pair<TypeClass*, ValType> MapKey; typedef typename ConstantInfo<ConstantClass>::ValType ValType;
typedef std::map<MapKey, ConstantClass *> MapTy; typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy; typedef std::pair<TypeClass *, ValType> LookupKey;
private:
/// Map - This is the main map from the element descriptor to the Constants.
/// This is the primary way we avoid creating two of the same shape
/// constant.
MapTy Map;
/// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
/// from the constants to their element in Map. This is important for
/// removal of constants from the array, which would otherwise have to scan
/// through the map with very large keys.
InverseMapTy InverseMap;
public:
typename MapTy::iterator map_begin() { return Map.begin(); }
typename MapTy::iterator map_end() { return Map.end(); }
void freeConstants() {
for (typename MapTy::iterator I=Map.begin(), E=Map.end();
I != E; ++I) {
// Asserts that use_empty().
delete I->second;
}
}
/// InsertOrGetItem - Return an iterator for the specified element.
/// If the element exists in the map, the returned iterator points to the
/// entry and Exists=true. If not, the iterator points to the newly
/// inserted entry and returns Exists=false. Newly inserted entries have
/// I->second == 0, and should be filled in.
typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
&InsertVal,
bool &Exists) {
std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Exists = !IP.second;
return IP.first;
}
private:
typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
if (HasLargeKey) {
typename InverseMapTy::iterator IMI = InverseMap.find(CP);
assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
IMI->second->second == CP &&
"InverseMap corrupt!");
return IMI->second;
}
typename MapTy::iterator I =
Map.find(MapKey(static_cast<TypeClass*>(CP->getType()),
ConstantKeyData<ConstantClass>::getValType(CP)));
if (I == Map.end() || I->second != CP) {
// FIXME: This should not use a linear scan. If this gets to be a
// performance problem, someone should look at this.
for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
/* empty */;
}
return I;
}
ConstantClass *Create(TypeClass *Ty, ValRefType V,
typename MapTy::iterator I) {
ConstantClass* Result =
ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
assert(Result->getType() == Ty && "Type specified is not correct!");
I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
if (HasLargeKey) // Remember the reverse mapping if needed.
InverseMap.insert(std::make_pair(Result, I));
return Result;
}
public:
/// getOrCreate - Return the specified constant from the map, creating it if
/// necessary.
ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) {
MapKey Lookup(Ty, V);
ConstantClass* Result = nullptr;
typename MapTy::iterator I = Map.find(Lookup);
// Is it in the map?
if (I != Map.end())
Result = I->second;
if (!Result) {
// If no preexisting value, create one now...
Result = Create(Ty, V, I);
}
return Result;
}
void remove(ConstantClass *CP) {
typename MapTy::iterator I = FindExistingElement(CP);
assert(I != Map.end() && "Constant not found in constant table!");
assert(I->second == CP && "Didn't find correct element?");
if (HasLargeKey) // Remember the reverse mapping if needed.
InverseMap.erase(CP);
Map.erase(I);
}
/// MoveConstantToNewSlot - If we are about to change C to be the element
/// specified by I, update our internal data structures to reflect this
/// fact.
void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
// First, remove the old location of the specified constant in the map.
typename MapTy::iterator OldI = FindExistingElement(C);
assert(OldI != Map.end() && "Constant not found in constant table!");
assert(OldI->second == C && "Didn't find correct element?");
// Remove the old entry from the map.
Map.erase(OldI);
// Update the inverse map so that we know that this constant is now
// located at descriptor I.
if (HasLargeKey) {
assert(I->second == C && "Bad inversemap entry!");
InverseMap[C] = I;
}
}
void dump() const {
DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
}
};
// Unique map for aggregate constants
template<class TypeClass, class ConstantClass>
class ConstantAggrUniqueMap {
public:
typedef ArrayRef<Constant*> Operands;
typedef std::pair<TypeClass*, Operands> LookupKey;
private: private:
struct MapInfo { struct MapInfo {
typedef DenseMapInfo<ConstantClass*> ConstantClassInfo; typedef DenseMapInfo<ConstantClass *> ConstantClassInfo;
typedef DenseMapInfo<Constant*> ConstantInfo; static inline ConstantClass *getEmptyKey() {
typedef DenseMapInfo<TypeClass*> TypeClassInfo;
static inline ConstantClass* getEmptyKey() {
return ConstantClassInfo::getEmptyKey(); return ConstantClassInfo::getEmptyKey();
} }
static inline ConstantClass* getTombstoneKey() { static inline ConstantClass *getTombstoneKey() {
return ConstantClassInfo::getTombstoneKey(); return ConstantClassInfo::getTombstoneKey();
} }
static unsigned getHashValue(const ConstantClass *CP) { static unsigned getHashValue(const ConstantClass *CP) {
SmallVector<Constant*, 8> CPOperands; SmallVector<Constant *, 8> Storage;
CPOperands.reserve(CP->getNumOperands()); return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I)
CPOperands.push_back(CP->getOperand(I));
return getHashValue(LookupKey(CP->getType(), CPOperands));
} }
static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) { static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
return LHS == RHS; return LHS == RHS;
} }
static unsigned getHashValue(const LookupKey &Val) { static unsigned getHashValue(const LookupKey &Val) {
return hash_combine(Val.first, hash_combine_range(Val.second.begin(), return hash_combine(Val.first, Val.second.getHash());
Val.second.end()));
} }
static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) { static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
if (RHS == getEmptyKey() || RHS == getTombstoneKey()) if (RHS == getEmptyKey() || RHS == getTombstoneKey())
return false; return false;
if (LHS.first != RHS->getType() if (LHS.first != RHS->getType())
|| LHS.second.size() != RHS->getNumOperands())
return false; return false;
for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) { return LHS.second == RHS;
if (LHS.second[I] != RHS->getOperand(I))
return false;
}
return true;
} }
}; };
public: public:
typedef DenseMap<ConstantClass *, char, MapInfo> MapTy; typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
private: private:
/// Map - This is the main map from the element descriptor to the Constants.
/// This is the primary way we avoid creating two of the same shape
/// constant.
MapTy Map; MapTy Map;
public: public:
@@ -694,44 +547,33 @@ public:
typename MapTy::iterator map_end() { return Map.end(); } typename MapTy::iterator map_end() { return Map.end(); }
void freeConstants() { void freeConstants() {
for (typename MapTy::iterator I=Map.begin(), E=Map.end(); for (auto &I : Map)
I != E; ++I) {
// Asserts that use_empty(). // Asserts that use_empty().
delete I->first; delete I.first;
}
} }
private: private:
typename MapTy::iterator findExistingElement(ConstantClass *CP) { ConstantClass *create(TypeClass *Ty, ValType V) {
return Map.find(CP); ConstantClass *Result = V.create(Ty);
}
ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) {
ConstantClass* Result =
ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V);
assert(Result->getType() == Ty && "Type specified is not correct!"); assert(Result->getType() == Ty && "Type specified is not correct!");
Map[Result] = '\0'; insert(Result);
return Result; return Result;
} }
public: public:
/// Return the specified constant from the map, creating it if necessary.
/// getOrCreate - Return the specified constant from the map, creating it if ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
/// necessary.
ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
LookupKey Lookup(Ty, V); LookupKey Lookup(Ty, V);
ConstantClass* Result = nullptr; ConstantClass *Result = nullptr;
typename MapTy::iterator I = Map.find_as(Lookup); auto I = find(Lookup);
// Is it in the map? if (I == Map.end())
if (I != Map.end()) Result = create(Ty, V);
else
Result = I->first; Result = I->first;
assert(Result && "Unexpected nullptr");
if (!Result) {
// If no preexisting value, create one now...
Result = Create(Ty, V, I);
}
return Result; return Result;
} }
@@ -742,21 +584,17 @@ public:
} }
/// Insert the constant into its proper slot. /// Insert the constant into its proper slot.
void insert(ConstantClass *CP) { void insert(ConstantClass *CP) { Map[CP] = '\0'; }
Map[CP] = '\0';
}
/// Remove this constant from the map /// Remove this constant from the map
void remove(ConstantClass *CP) { void remove(ConstantClass *CP) {
typename MapTy::iterator I = findExistingElement(CP); typename MapTy::iterator I = Map.find(CP);
assert(I != Map.end() && "Constant not found in constant table!"); assert(I != Map.end() && "Constant not found in constant table!");
assert(I->first == CP && "Didn't find correct element?"); assert(I->first == CP && "Didn't find correct element?");
Map.erase(I); Map.erase(I);
} }
void dump() const { void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
}
}; };
} // end namespace llvm } // end namespace llvm

View File

@@ -75,7 +75,7 @@ LLVMContextImpl::~LLVMContextImpl() {
// Free the constants. This is important to do here to ensure that they are // Free the constants. This is important to do here to ensure that they are
// freed before the LeakDetector is torn down. // freed before the LeakDetector is torn down.
std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
DropReferences()); DropFirst());
std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
DropFirst()); DropFirst());
std::for_each(StructConstants.map_begin(), StructConstants.map_end(), std::for_each(StructConstants.map_begin(), StructConstants.map_end(),

View File

@@ -272,13 +272,13 @@ public:
DenseMap<Type*, ConstantAggregateZero*> CAZConstants; DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy; typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
ArrayConstantsTy ArrayConstants; ArrayConstantsTy ArrayConstants;
typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy; typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
StructConstantsTy StructConstants; StructConstantsTy StructConstants;
typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy; typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
VectorConstantsTy VectorConstants; VectorConstantsTy VectorConstants;
DenseMap<PointerType*, ConstantPointerNull*> CPNConstants; DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
@@ -289,12 +289,10 @@ public:
DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *> DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
BlockAddresses; BlockAddresses;
ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr> ConstantUniqueMap<ConstantExpr> ExprConstants;
ExprConstants;
ConstantUniqueMap<InlineAsm> InlineAsms;
ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
InlineAsm> InlineAsms;
ConstantInt *TheTrueVal; ConstantInt *TheTrueVal;
ConstantInt *TheFalseVal; ConstantInt *TheFalseVal;

View File

@@ -274,5 +274,30 @@ TEST(ConstantsTest, ReplaceWithConstantTest) {
#undef CHECK #undef CHECK
TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
LLVMContext Context;
std::unique_ptr<Module> M(new Module("MyModule", Context));
Type *IntTy = Type::getInt8Ty(Context);
ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
ConstantInt::get(IntTy, 1)};
Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
Constant *Global = new GlobalVariable(*M, IntTy, false,
GlobalValue::ExternalLinkage, nullptr);
Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
ASSERT_NE(A01, A0G);
GlobalVariable *RefArray =
new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
ASSERT_EQ(A0G, RefArray->getInitializer());
GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
ASSERT_EQ(A01, RefArray->getInitializer());
}
} // end anonymous namespace } // end anonymous namespace
} // end namespace llvm } // end namespace llvm