User account creation filtered due to spam.

Bug 35341 - Early exit loop with short known trip count not unrolled
Summary: Early exit loop with short known trip count not unrolled
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.3.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2008-02-24 04:20 UTC by davidxl
Modified: 2015-03-24 00:55 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description davidxl 2008-02-24 04:20:54 UTC
Gcc fully unrolls short trip counted (known) loop if the unrolled loop body size d oes not exceed a threshold. However, if  the loop has early exit, this is not done -- leading to missing scalar opt later on.

Example:

int a[100];
int b[100];
int foo(void)
{
     int i, j;

     for (i = 0; i < 5; i ++)
     {
          a[2*i] += a[i];
          if (a[2*i] == 10) break;
     }

     return 0;
}
Comment 1 Bernhard Reutner-Fischer 2015-03-24 00:55:16 UTC
Did you forget to specify -funroll-loops?

gcc-4.2 -O2 -funroll-loops and gcc-4.4 as well as 5.0 with these options basically generate this optimized dump, which IIUC is what you want.

;; Function foo (foo)

Analyzing Edge Insertions.
foo ()
{
  int temp.34;
  int temp.31;
  int temp.29;
  int temp.26;
  int temp.25;

<bb 2>:
  temp.25 = a[0];
  temp.26 = temp.25 + temp.25;
  a[0] = temp.26;
  if (temp.26 == 10)
    goto <bb 7>;
  else
    goto <bb 3>;

<bb 3>:
  temp.29 = a[2] + a[1];
  a[2] = temp.29;
  if (temp.29 == 10)
    goto <bb 7>;
  else
    goto <bb 4>;

<bb 4>:
  temp.31 = temp.29 + a[4];
  a[4] = temp.31;
  if (temp.31 == 10)
    goto <bb 7>;
  else
    goto <bb 5>;

<bb 5>:
  temp.34 = a[6] + a[3];
  a[6] = temp.34;
  if (temp.34 == 10)
    goto <bb 7>;
  else
    goto <bb 6>;

<bb 6>:
  a[8] = [plus_expr] a[8] + a[4];

<bb 7>:
  return 0;

}