diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -874,7 +874,9 @@ ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { // get an existing value or the insertion position LLVMContextImpl *pImpl = Context.pImpl; - std::unique_ptr &Slot = pImpl->IntConstants[V]; + std::unique_ptr &Slot = + V.isZero() ? pImpl->IntZeroConstants[V.getBitWidth()] + : pImpl->IntConstants[V]; if (!Slot) { // Get the corresponding integer type for the bit width of the value. IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h --- a/llvm/lib/IR/LLVMContextImpl.h +++ b/llvm/lib/IR/LLVMContextImpl.h @@ -1446,13 +1446,12 @@ DenseMap ValueNames; - using IntMapTy = - DenseMap, DenseMapAPIntKeyInfo>; - IntMapTy IntConstants; + DenseMap> IntZeroConstants; + DenseMap, DenseMapAPIntKeyInfo> + IntConstants; - using FPMapTy = - DenseMap, DenseMapAPFloatKeyInfo>; - FPMapTy FPConstants; + DenseMap, DenseMapAPFloatKeyInfo> + FPConstants; FoldingSet AttrsSet; FoldingSet AttrsLists; diff --git a/llvm/lib/IR/LLVMContextImpl.cpp b/llvm/lib/IR/LLVMContextImpl.cpp --- a/llvm/lib/IR/LLVMContextImpl.cpp +++ b/llvm/lib/IR/LLVMContextImpl.cpp @@ -116,6 +116,7 @@ CTNConstants.clear(); UVConstants.clear(); PVConstants.clear(); + IntZeroConstants.clear(); IntConstants.clear(); FPConstants.clear(); CDSConstants.clear();