llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.12k stars 12.01k forks source link

[regression] missed optimization for dead code elimination at -O3 (vs. -O2) #50483

Open thetheodor opened 3 years ago

thetheodor commented 3 years ago
Bugzilla Link 51139
Version trunk
OS Linux
CC @DMG862,@fhahn,@ttheodor

Extended Description

$./bin/clang -v
clang version 13.0.0 (https://github.com/llvm/llvm-project.git 96d8f2a1e0842b83b319a8daf3cc82dd5e1350fa)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /dev/shm/llvm-project/build/./bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;@m64
Selected multilib: .;@m64
$cat test.c
extern void foo(void);
extern void bar(void);
int a, b, d, e, f;
void g(int h);
static void c(int h) {
 g(b);
 while (1)
  while (a) {
   if (h)
    break;
   foo();
  }
}
void g(int h) {
 int i = 0;
 while (i < 1) {
  if (h)
   for (; i < 6; i++)
    ;
  while (d)
   while (f)
    while (e)
     bar();
 }
}
int main() {
 if (a)
  c(b);
 return 0;
}
$./bin/clang -O2 test.c -S -o O2.s     
$./bin/clang -O3 test.c -S -o O3.s     
$grep foo O2.s       
$grep foo O3.s
    callq   foo

The commit which introduced this regression is f3a27511c9f8

llvmbot commented 3 years ago

In this case, the inliner pass fails to inline the function g after unswitch because of the cost as below debug message.

NOT Inlining (cost=250, threshold=250), Call: call void @​g(i32 %2) #​3

Originally, after inlining function g, the JumpThreading pass made the block with call @​foo dead and SimplifyCFG pass deleted it.

If you add always_inline attribute to the function g's prototype as below, you can see the callfoo is gone.

void g(int h) attribute((always_inline));

Do we have to adjust the inline threshold?

I think it is not good to adjust the cost function in SimpleLoopUnswitch because it calculates the cost for duplicating the target loop rather than whole function.

llvmbot commented 3 years ago

In this case, the inliner pass fails to inline the function g after unswitch because of the cost as below debug message.

NOT Inlining (cost=250, threshold=250), Call: call void @​g(i32 %2) #​3

Originally, after inlining function g, the JumpThreading pass made the block with call @​foo dead and SimplifyCFG pass deleted it.

If you add always_inline attribute to the function g's prototype as below, you can see the callfoo is gone.

void g(int h) attribute((always_inline));

llvmbot commented 3 years ago

Review discussion Is here: https://reviews.llvm.org/D99354

It is related to llvm/llvm-bugzilla-archive#51141

fhahn commented 3 years ago

Review discussion Is here: https://reviews.llvm.org/D99354