[instcombine] Infer alignment for aligned_alloc with potentially zero size
authorPhilip Reames <listmail@philipreames.com>
Mon, 10 Jan 2022 16:25:20 +0000 (08:25 -0800)
committerPhilip Reames <listmail@philipreames.com>
Mon, 10 Jan 2022 16:48:49 +0000 (08:48 -0800)
commitf4c54683d684100fffd307ddf773078429b83442
treee9ffbb41abbf6d1bbd8a0b663a8f67a9a4701f0b
parent4b5d59ffd073393b898b58c09a2dfd1d20f711e5
[instcombine] Infer alignment for aligned_alloc with potentially zero size

This change removes a previous restriction where we had to prove the allocation performed by aligned_alloc was non-zero in size before using the align parameter to annotate the result.  I believe this was conservatism around the C11 specification of this routine which allowed UB when size was not a multiple of alignment, but if so, it was a partial one at best.  (ex: align 32, size 16  was equally UB, but not restricted)  The spec has since been clarified to require nullptr return, not UB.

A nullptr - the documented return for this function on failure for all cases after UB mentioned above was removed - is trivially aligned for any power of two.  This isn't totally new behavior even for this transform, we'd previously annotate potentially failing allocs (e.g. huge sizes) meaning we were putting align on potentially null pointers anyways.  This change simpy does the same for all failure modes.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/test/Transforms/InstCombine/deref-alloc-fns.ll