Visual C ++是否支持循环非开关?

时间:2011-08-26 18:09:17

标签: c visual-c++ optimization

我正在尝试确定Visual C ++ 2008或2010是否支持loop unswitching compiler optimization。我知道gcc支持它。

2 个答案:

答案 0 :(得分:1)

AFAIK,“不”:

http://cboard.cprogramming.com/c-programming/126756-lack-compiler-loop-optimization-loop-unswitching.html

您的里程可能会有所不同,具体取决于:

  • 您是否拥有最新的编译器(MSVS 2010)

  • 您是否已购买MSVS Professional或更高版本

答案 1 :(得分:1)

所以我只是尝试使用VS2010:

void foo(int* x, int* y, int length, int w) {
    for (int i = 0; i < length; i++) {
        x[i] = x[i] + y[i];
        if (w) {
            y[i] = 0;
        }
    }
}

int main() {
    int x[1000];
    int y[1000];
    int w;
    scanf("%d", &w);
    foo(x, y, 1000, w);
    int sum = 0;
    for (int i = 0; i < 1000; i++) {
        if (y[i])
            sum++;
    }
    printf("%d\n", sum);
    return 0;
}

比确保它不会优化功能所需要的要复杂一些。无论如何,这导致以下反汇编:

PUBLIC  ?foo@@YAXPAH0HH@Z               ; foo
; Function compile flags: /Ogtp
;   COMDAT ?foo@@YAXPAH0HH@Z
_TEXT   SEGMENT
?foo@@YAXPAH0HH@Z PROC                  ; foo, COMDAT
; _x$ = ecx
; _y$ = eax
; _w$ = esi
; Line 12
    sub ecx, eax
    mov edx, 1000               ; 000003e8H
    push    edi
$LL4@foo:
; Line 10
    mov edi, DWORD PTR [eax]
    add DWORD PTR [ecx+eax], edi
; Line 11
    test    esi, esi
    je  SHORT $LN3@foo
; Line 12
    mov DWORD PTR [eax], 0
$LN3@foo:
; Line 9
    add eax, 4
    dec edx
    jne SHORT $LL4@foo
    pop edi
; Line 15
    ret 0
?foo@@YAXPAH0HH@Z ENDP                  ; foo

编译为发布项目,在VS2010 Premium,W​​in7 x64下为32位。