[llvm] Support non-malloc functions in malloc+memset->calloc fold (PR #138299)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri May 2 13:13:54 PDT 2025
================
@@ -2057,15 +2067,38 @@ struct DSEState {
if (Malloc->getOperand(0) != MemSet->getLength())
return false;
- if (!shouldCreateCalloc(Malloc, MemSet) ||
- !DT.dominates(Malloc, MemSet) ||
+ if (!shouldCreateCalloc(Malloc, MemSet) || !DT.dominates(Malloc, MemSet) ||
!memoryIsNotModifiedBetween(Malloc, MemSet, BatchAA, DL, &DT))
return false;
IRBuilder<> IRB(Malloc);
- Type *SizeTTy = Malloc->getArgOperand(0)->getType();
- auto *Calloc =
- emitCalloc(ConstantInt::get(SizeTTy, 1), Malloc->getArgOperand(0), IRB,
- TLI, Malloc->getType()->getPointerAddressSpace());
+ assert(Func == LibFunc_malloc || ZeroedVariantName.has_value());
+ Value *Calloc = nullptr;
+ if (ZeroedVariantName.has_value()) {
+ auto *ZeroedVariant =
+ Malloc->getModule()->getFunction(*ZeroedVariantName);
+ if (!ZeroedVariant)
----------------
nikic wrote:
I don't think we should require the declaration to already exist -- even if the frontend injects it, an unused declaration will get DCEd.
If the declaration doesn't exist, we can insert it with the same function type and attributes as the original allocation function, only with zeroed included in the allockind.
https://github.com/llvm/llvm-project/pull/138299
More information about the llvm-commits
mailing list