[PATCH] D101176: [SimplifyLibCalls] Transform malloc to calloc with redundant memsets elimination (PR25892)

Dawid Jurczak via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 23 09:15:18 PDT 2021


yurai007 created this revision.
yurai007 added reviewers: xbolva00, spatel, lebedev.ri, fhahn, JudyZhu.
Herald added a subscriber: hiraditya.
yurai007 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Before this change LLVM cannot perform any of following transformation:

  
  memset(malloc(x), 0, x) --> calloc(1, x)
  memset(calloc(1, x), 0, x) --> calloc(1, x)

However GCC is able to do that: https://godbolt.org/z/Ef94je4KP

Both transformations are connected especially when malloc is followed by more then one memset.
Therefore both transformations can be handled together in same code paths by SimplifyLibCalls.
It's very common to have malloc and memset in different blocks like in addressed PR:
https://bugs.llvm.org/show_bug.cgi?id=25892
This patch detects common pattern (icmp+br) and examine affected basic blocks conservatively looking for
intermediate store-like instructions. The change is loosely based on previous PR-related work:
https://reviews.llvm.org/D93360 and https://reviews.llvm.org/D16337.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101176

Files:
  llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
  llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
  llvm/test/Transforms/InstCombine/memset-1.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101176.340066.patch
Type: text/x-patch
Size: 12837 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210423/e744542c/attachment.bin>


More information about the llvm-commits mailing list