[llvm] [IR][DSE] Support non-malloc functions in malloc+memset->calloc fold (PR #138299)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat May 10 13:05:58 PDT 2025


================
@@ -2057,15 +2066,32 @@ 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()) {
+      LLVMContext &Ctx = Malloc->getContext();
+      AttributeList Attr = InnerCallee->getAttributes();
----------------
nikic wrote:

```suggestion
      AttributeList Attrs = InnerCallee->getAttributes();
```
To make it clear that this is not a single attribute.

https://github.com/llvm/llvm-project/pull/138299


More information about the llvm-commits mailing list