Skip to content

LLVM does dead code elimination using pointer validity assumptions #1669

Description

@haved

In the following program, clang is able to fully remove the second if statement. Instead it creates a select using a[0] to select between constant 1 and constant 2. The final load of a[1] is completely removed.

int func(int* a)
{
  if (a[0])
    return 1;

  if (a)
    return 2;

  return a[1];
}

When we load from a[0], we can assume that a is not a nullptr, and LLVM is able to use this assumption to determine that the "else"-branch of the second if is unreachable. We could also do this. For reference, the relevant RVSDG regions of f are shown here:

Image

In the same vein, clang is able to convert the below code into a single ret 2:

int func(int* a)
{
  if (a)
    return 2;

  return a[0];
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions