Bug 97519 - builtin_constant_p (x + cst) should be optimized to builtin_constant_p (x)
Summary: builtin_constant_p (x + cst) should be optimized to builtin_constant_p (x)
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 11.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: 97445
  Show dependency treegraph
 
Reported: 2020-10-21 15:14 UTC by Jan Hubicka
Modified: 2020-11-03 10:09 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-10-21 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Hubicka 2020-10-21 15:14:08 UTC
As discussed in PR97445 we should optimize builtins_constant_p (var+cst) and similar cases.
Comment 1 Andrew Pinski 2020-10-21 16:23:27 UTC
Confirmed, the generic is X OP CST (for / and %: CST OP X) into X inside __builtin_constant_p.

Though the question should we do this on the gimple level or before hand?
Comment 2 Richard Biener 2020-10-22 07:02:51 UTC
Implemented in match.pd it will cover both.  SSA backprop would be the most
natural classical pass candidate but that runs after IPA.  The other candidate
is of course forwprop where it would be "cheaper" (not so many new patterns
to match in match.pd), "chewing" ops during re-processing of processed stmts.
Comment 3 Jakub Jelinek 2020-10-22 08:23:53 UTC
We would certainly need to punt on gimple_could_trap_p assignments, as can be seen e.g. on
static inline int
foo (int x)
{
  return __builtin_constant_p ((x + 32) / 0);
}

int
bar (void)
{
  return foo (35);
}
with -O2 -fno-early-inlining.
Anyway, I'm actually not sure if it is safe to perform this change at all.
Consider:
int
foo (int x)
{
  if (x < __INT_MAX__ - 30)
    return 23;
  int y = x + 30;
  if (__builtin_constant_p (y))
    return y;
  return 42;
}
with -O2 -fdisable-tree-evrp
y is constant (__INT_MAX__), but x is certainly not constant.