Bug 15350 - [tree-ssa] Convert if ((1 << a) & 1) into if (a == 0).
Summary: [tree-ssa] Convert if ((1 << a) & 1) into if (a == 0).
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: tree-ssa
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on: 15459
Blocks: 19987
  Show dependency treegraph
 
Reported: 2004-05-09 19:50 UTC by Kazu Hirata
Modified: 2023-12-30 01:15 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 5.1.0
Known to fail: 4.9.2
Last reconfirmed: 2012-01-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kazu Hirata 2004-05-09 19:50:47 UTC
Convert foo() into baz().

void bar (void);

void
foo (int a)
{
  int b = 1 << a;
  if (b & 1)
    bar ();
}

void
baz (int a)
{
  if (a == 0)
    bar ();
}

You might think this is so artificial but actually comes from
simplify_comparison() in combine.c, a part of which looks like

<L344>:;
  T.4338_976 = (int)T.4337_966;
  T.4546_977 = T.4338_976 - 1;
  T.4547_978 = 1 << T.4546_977;
  T.4548_979 = T.4547_978 & 1;
  T.4549_980 = (_Bool)T.4548_979;
  if (T.4549_980 == 0) goto <L555>; else goto <L345>;
Comment 1 Andrew Pinski 2004-05-09 20:37:47 UTC
Confirmed.
Comment 2 Kazu Hirata 2004-05-15 11:39:45 UTC
As of today, the last tree-ssa form of foo() looks like:

foo (a)
{
  int b;
  _Bool T.1;
  int T.0;

<bb 0>:
  b_2 = 1 << a_1;
  T.0_3 = b_2 & 1;
  if (T.0_3 != 0) goto <L0>; else goto <L1>;

<L0>:;
  bar () [tail call];

<L1>:;
  return;

}
Comment 3 Andrew Pinski 2004-05-17 01:57:03 UTC
When fold could optimize (1 << a) & 1, PR 15495 will fix the testcase in this bug.
Comment 4 Andrew Pinski 2005-05-08 19:08:11 UTC
Hmm, we do optimize this with ashiftrt but not with ashift.
We also fold ((1 << a) & 1) != 0  into ((1 >> a & 1) != 0) which gets optimized on the RTL level.
Comment 5 Marc Glisse 2015-06-09 17:23:44 UTC
This was fixed in gcc-5.