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 @@ -129,8 +129,15 @@ } void LLVMContextImpl::dropTriviallyDeadConstantArrays() { - SmallSetVector WorkList(ArrayConstants.begin(), - ArrayConstants.end()); + SmallSetVector WorkList; + + // When ArrayConstants are of substantial size and only a few in them are + // dead, starting WorkList with all elements of ArrayConstants can be + // wasteful. Instead, starting WorkList with only elements that have empty + // uses. + for (ConstantArray *C : ArrayConstants) + if (C->use_empty()) + WorkList.insert(C); while (!WorkList.empty()) { ConstantArray *C = WorkList.pop_back_val();