Issue 55394
Summary constant propagation can pessimize libcall optimizations.
Labels llvm:optimizations, missed-optimization
Assignees
Reporter nickdesaulniers
    This case just came up in the context of the Linux kernel.
https://lore.kernel.org/llvm/[email protected]/

Consider the following code:
```c
#define CONST 0x01000000

// try removing static
static unsigned ffs (int x) {
  int r;
  asm("bsfl %1,%0"
      : "=r" (r)
      : "rm" (x), "0" (-1));
  return r;
}

unsigned int foo(void) {
  return ffs(CONST);
}

unsigned int bar(void) {
  return __builtin_ffs(CONST);
}
```
It looks like constprop sinks `CONST` into ffs when ffs is static, but if it wasn't then constprop wouldn't sink CONST.  instcombine will fail to optimize `ffs(CONST)` into 25 since dead arg elimination will remove the parameter after const prop.

cc @fhahn @efriedma-quic 

We're fixing this in the kernel by changing our ffs to dispatch to __builtin_ffs after a __builtin_constant_p check.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to