Will the optimizer output mathematical expressions based on compile time constants?

If I have mathematical equations that rely on inputs that can be zero or non-zero (a template argument known at compile time), the optimizer evaluates the equations and optimizes the expressions that it knows it will evaluate to 0 or 1.

For instance:

double x = y * Eval<type>::value;

if Eval<type>::value- 0, xalways will be 0.

double x = exp(y * Eval<type>::value);

if Eval<type>::value 0, xalways will be 1.

Can the optimizer understand this and replace it xwith 0or 1elsewhere in the code, or will these calculations be performed at runtime?

I am using gcc 4.7 with -O3

+5
source share
1

EDIT: , .

gcc 4.6.3 -03, , , .

:

#include <cstdio>
inline int x(double y)
{
   if (y == 0)
      printf("Hello bob3\n");
   else
      printf("Why do I bother\n");

};

const int c = 0;

int main()
{
   int f;
   scanf("%d",&f);

   x(f * c);
}

    .file   "foo.cpp"
    .section    .rodata.str1.1,"aMS",@progbits,1
.LC0:
    .string "%d"
.LC1:
    .string "Hello bob3"
    .section    .text.startup,"ax",@progbits
    .p2align 4,,15
    .globl  main
    .type   main, @function
main:
.LFB13:
    .cfi_startproc
    subq    $24, %rsp
    .cfi_def_cfa_offset 32
    movl    $.LC0, %edi
    xorl    %eax, %eax
    leaq    12(%rsp), %rsi
    call    scanf
    movl    $.LC1, %edi
    call    puts
    xorl    %eax, %eax
    addq    $24, %rsp
    .cfi_def_cfa_offset 8
    ret
    .cfi_endproc
.LFE13:
    .size   main, .-main
    .ident  "GCC: (Debian 4.6.3-1) 4.6.3"
    .section    .note.GNU-stack,"",@progbits
+1

All Articles