[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)
+ return false;
+ auto Attributes = ZeroedVariant->getAttributes();
+ auto MallocFamily = getAllocationFamily(Malloc, &TLI);
+ if (MallocFamily &&
+ *MallocFamily !=
+ Attributes.getFnAttr("alloc-family").getValueAsString())
+ return false;
+ if (!Attributes.hasFnAttr(Attribute::AllocKind) ||
+ (Attributes.getAllocKind() & AllocFnKind::Zeroed) ==
+ AllocFnKind::Unknown)
----------------
nikic wrote:
For the case where a declaration does exist, I think it would be better to check these things in the IR verifier.
https://github.com/llvm/llvm-project/pull/138299
More information about the llvm-commits
mailing list