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:
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.