[GlobalOpt] Generalize malloc-to-global for any allocation function
authorPhilip Reames <listmail@philipreames.com>
Mon, 17 Jan 2022 23:06:23 +0000 (15:06 -0800)
committerPhilip Reames <listmail@philipreames.com>
Mon, 17 Jan 2022 23:06:23 +0000 (15:06 -0800)
commit26049b8ce376a81acbaa0b4b86d7dcef1bc432bd
tree6301bb1edee8576e502663df075471e2e9505969
parent30715365d45c02669d7dc947ecf2a5e9321f26d0
[GlobalOpt] Generalize malloc-to-global for any allocation function

We can generalize the malloc-to-global transform for other allocation functions which are both a) removable, and b) have a known initialization value.

One subtlety that I want to point out - mostly because I hadn't realized it was true until I took a closer look - is that the existing code doesn't prove that initialization/malloc happens only once. The initialization function can be called multiple times. This is correct without special handling for malloc as undef can map to any value previously written, but a non-undef initializing allocation it means we may end up memseting the new global repeatedly. In particular, this means it's not legal to fold the memset into the initializer of the global.

Differential Revision: https://reviews.llvm.org/D117503
llvm/lib/Transforms/IPO/GlobalOpt.cpp
llvm/test/Transforms/GlobalOpt/calloc-promote.ll