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,18 +129,24 @@ } 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(); - if (C->use_empty()) { - for (const Use &Op : C->operands()) { - if (auto *COp = dyn_cast(Op)) - WorkList.insert(COp); - } - C->destroyConstant(); - } + if (!C->use_empty()) + continue; + for (const Use &Op : C->operands()) + if (auto *COp = dyn_cast(Op)) + WorkList.insert(COp); + C->destroyConstant(); } }