Skip to content

Failure to optimize malloc+memset to calloc #46503

@GabrielRavier

Description

@GabrielRavier
Bugzilla Link 47159
Version trunk
OS Linux
CC @rotateright

Extended Description

struct S
{
    int a;
};

struct S *f()
{
    struct S *p = (struct S *)malloc(sizeof(struct S));
    memset(p, 0, sizeof(*p));
    return p;
}

This can be optimized to return calloc(sizeof(struct S), 1);. This transformation is done by GCC, but not by LLVM. See also https://godbolt.org/z/MGzfGf.

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