Skip to content

malloc/memset -> calloc optimization should happen even if there are BBs between the ptr check and the memset #134949

@pinskia

Description

@pinskia

take:

void *combine_alt (int s, bool c, bool *a)
{
  void *r = __builtin_malloc (s);
  if (!r)  return 0;
  if (a) *a = 1;
  __builtin_memset (r, 0, s);
  return r;
}

This should be optimized to:

void *combine_alt_tgt (int s, bool c, bool *a)
{
  void *r = __builtin_calloc (s, 1);
  if (!r)  return 0;
  if (a) *a = 1;
  return r;
}

Right now the check in shouldCreateCalloc is too restrictive. Instead of checking MemsetBB != FalseBB, it should be checking that MemsetBB post dominates the FalseBB.

Note if you remove the check for !r, shouldCreateCalloc is too restrictive there too. that is because MallocBB == MemsetBB check should be similarly a post dominate check.

Note I found this while fixing up GCC finally (https://gcc.gnu.org/PR83022). And I thought I would report the missed optimization back to LLVM.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions