Skip to content

[clang] Constant "(a && 0)" not folded at -O0 #148955

@pbo-linaro

Description

@pbo-linaro

While working on QEMU, we recently exposed a missed constant folding with clang -O0.

Given this code:
$ cat main.c

extern int f(void);
int main(int argc) {
  if (argc && 0)
    f();
}
$ clang -O0 main.c
/usr/bin/ld: /tmp/main-19bcf0.o: in function `main':
main.c:(.text+0x21): undefined reference to `f'

Since argc && 0 is always false, it could be expected (though not required by the standard) that branch is eliminated and thus, f should not be referenced. Some code on QEMU expect dead code elimination to happen, to not cross reference some symbols.
When changing condition to (0 && argc), the branch is removed as expected.

Original discussion on QEMU side: https://lore.kernel.org/qemu-devel/[email protected]/

Metadata

Metadata

Assignees

No one assigned

    Labels

    clangClang issues not falling into any other categoryconfirmedVerified by a second party

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions