簡體   English   中英

為什么gcc4沒有展開這個循環?

[英]Why does gcc4 not unroll this loop?

gcc 4.4.6文檔中說明:

 -funroll-all-loops: Unroll all loops, even if their number of iterations is uncertain when the loop isentered. 

我正在編譯這段代碼:

int unroll(){
  int i = 0;
  int array[1000];
  do {
    use(i,array);
    i++;
  }while(i<1000);
  return(0);
}

void use(int i, int *array){
   int x = i*5;
   array[i] = x;
}

...一次使用funroll-all-loops優化,一次沒有:

OPT = -funroll-all-loops
NOOPT = -O0

然后我使用diff來比較每個的匯編代碼(使用-S -fverbose-asm )。

生成的代碼是相同的。

嘗試將循環更改為do while ; 調整循環計數器(最多100); 更改循環體內的語句。

我能錯過什么? 為什么這個循環沒有展開?

更新

Nikos C建議使用--param max-unroll-times=N來提高循環注冊參數,其中N是上限。 雖然這是一個明智的建議,但並沒有改變行為。 我還將循環迭代降低到只有10.還更新了代碼以實際“做”某些事情,沒有變化。

因為你已經在“OPT”情況下禁用了所有其他優化,你告訴編譯器展開所有循環然后拒絕他這樣做的方法,比如循環歸納等。試試

NOOPT = -O2
OPT = -O2 -funroll-all-loops

如果我翻譯片段(稍微更改為extern函數以避免任何內聯和死代碼消除)

void use(int i, int *array);

int unroll(){
  int i = 0;
  int array[1000];
  do {
    use(i,array);
    i++;
  }while(i<1000);
  return(0);
}

使用gcc -O2 -funroll-all-loops test.c -o test2.o -c ,生成的目標代碼展開八次:

  0000000000000000 <unroll>:
  0:   53                      push   %rbx
  1:   31 db                   xor    %ebx,%ebx
  3:   48 81 ec a0 0f 00 00    sub    $0xfa0,%rsp
  a:   66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)
  10:   89 df                   mov    %ebx,%edi
  12:   48 89 e6                mov    %rsp,%rsi
  15:   e8 00 00 00 00          callq  1a <unroll+0x1a>
  1a:   8d 7b 01                lea    0x1(%rbx),%edi
  1d:   48 89 e6                mov    %rsp,%rsi
  20:   e8 00 00 00 00          callq  25 <unroll+0x25>
  25:   8d 7b 02                lea    0x2(%rbx),%edi
  28:   48 89 e6                mov    %rsp,%rsi
  2b:   e8 00 00 00 00          callq  30 <unroll+0x30>
  30:   8d 7b 03                lea    0x3(%rbx),%edi
  33:   48 89 e6                mov    %rsp,%rsi
  36:   e8 00 00 00 00          callq  3b <unroll+0x3b>
  3b:   8d 7b 04                lea    0x4(%rbx),%edi
  3e:   48 89 e6                mov    %rsp,%rsi
  41:   e8 00 00 00 00          callq  46 <unroll+0x46>
  46:   8d 7b 05                lea    0x5(%rbx),%edi
  49:   48 89 e6                mov    %rsp,%rsi
  4c:   e8 00 00 00 00          callq  51 <unroll+0x51>
  51:   8d 7b 06                lea    0x6(%rbx),%edi
  54:   48 89 e6                mov    %rsp,%rsi
  57:   e8 00 00 00 00          callq  5c <unroll+0x5c>
  5c:   8d 7b 07                lea    0x7(%rbx),%edi
  5f:   48 89 e6                mov    %rsp,%rsi
  62:   83 c3 08                add    $0x8,%ebx
  65:   e8 00 00 00 00          callq  6a <unroll+0x6a>
  6a:   81 fb e8 03 00 00       cmp    $0x3e8,%ebx
  70:   75 9e                   jne    10 <unroll+0x10>
  72:   48 81 c4 a0 0f 00 00    add    $0xfa0,%rsp
  79:   31 c0                   xor    %eax,%eax
  7b:   5b                      pop    %rbx
  7c:   c3                      retq   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM