[all-commits] [llvm/llvm-project] c1cfa1: [Clang] Emit TBAA info for enums in C (#73326)

David Sherwood via All-commits all-commits at lists.llvm.org
Fri Dec 8 04:58:53 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: c1cfa1757c208cd15efec3541aadea6bec52092d
      https://github.com/llvm/llvm-project/commit/c1cfa1757c208cd15efec3541aadea6bec52092d
  Author: David Sherwood <57997763+david-arm at users.noreply.github.com>
  Date:   2023-12-08 (Fri, 08 Dec 2023)

  Changed paths:
    M clang/docs/ReleaseNotes.rst
    M clang/lib/CodeGen/CodeGenTBAA.cpp
    A clang/test/CodeGen/tbaa.c

  Log Message:
  -----------
  [Clang] Emit TBAA info for enums in C (#73326)

When emitting TBAA information for enums in C code we currently just
treat the data as an 'omnipotent char'. However, with C strict aliasing
this means we fail to optimise certain cases. For example, in the
SPEC2017 xz benchmark there are structs that contain arrays of enums,
and clang pessmistically assumes that accesses to those enums could
alias with other struct members that have a different type.

According to

https://en.cppreference.com/w/c/language/enum

enums should be treated as 'int' types unless explicitly specified (C23)
or if 'int' would not be large enough to hold all the enumerated values.
In the latter case the compiler is free to choose a suitable integer
that would hold all such values.

When compiling C code this patch generates TBAA information for the enum
by using an equivalent integer of the size clang has already chosen for
the enum. I have ignored C++ for now because the rules are more complex.

New test added here:

  clang/test/CodeGen/tbaa.c




More information about the All-commits mailing list