optimization blocks (csapp chapter 5.1)
7633 ワード
p_511
コンパイラは指示なしに「safe optimization」を行うため、パラメータのない指示でコードを最適化しない最適化もあるので、コンパイラの最適化を妨げるため、プログラムではコードのクラスを避けるべきです.
optimization blocks: aspects of programs that can severely limit the opportunities for a compiler to generate optimized code;
2種類のoptimization blocks:
1、memory aliasing
pointers may generate the same memory location is known as memory aliasing. In performing only safe optimizations, the compiler must assume that different pointers may be aliased, limiting the set of possible optimizations.
2、procedure calls
Most compilers do not try to determine whether a function is free of side effects and hence is a candidate for optimizations.Instead,the compiler assumes the worst case and leaves function call intact(そのまま)
コンパイラは指示なしに「safe optimization」を行うため、パラメータのない指示でコードを最適化しない最適化もあるので、コンパイラの最適化を妨げるため、プログラムではコードのクラスを避けるべきです.
optimization blocks: aspects of programs that can severely limit the opportunities for a compiler to generate optimized code;
2種類のoptimization blocks:
1、memory aliasing
pointers may generate the same memory location is known as memory aliasing. In performing only safe optimizations, the compiler must assume that different pointers may be aliased, limiting the set of possible optimizations.
// from cmu
/* Sum rows is of n X n matrix a
and store in vector b */
void sum_rows1(int *a, int *b, int n) {
int i, j;
for (i = 0; i < n; i++) {
b[i] = 0;
for (j = 0; j < n; j++)
b[i] += a[i*n + j];
}
}
/*
int A[9];
int* B = A + 3;
sum_rows1(A, B);
,
void sum_rows2(int *a, int *b, int n) {
int i, j;
for (i = 0; i < n; i++) {
double val = 0;
for (j = 0; j < n; j++)
val += a[i*n + j];
b[i] = val;
}
}*/
/*
sum_rows1:
.L4:
movl %ebp, %ecx
movl $0, (%edx,%ebp,4)
movl $0, %eax
.L3:
movl (%esi,%eax,4), %ebx ;(%esi, %eax, 4) : &a[i*n + j]
addl %ebx, (%edx,%ecx,4) ;(%edx, %ecx, 4) : &b[i]
addl $1, %eax ;%eax : j
cmpl %edi, %eax ;%edi : n
jne .L3
addl $1, %ebp ;%ebp : i
addl (%esp), %esi
cmpl %edi, %ebp
jne .L4
*/
//
// xp = yp ,twiddle1 twiddle2 , , twiddle1 twiddle2
void twiddle1(int *xp, int* yp)
{
*xp += *yp;
*xp += *yp;
}
void twiddle2(int *xp, int* yp)
{
*xp += 2* *yp;
}
2、procedure calls
Most compilers do not try to determine whether a function is free of side effects and hence is a candidate for optimizations.Instead,the compiler assumes the worst case and leaves function call intact(そのまま)
// (from cmu)
void lower(char* s)
{
int i = 0;
for (i = 0; i < strlen(s); i++)
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] -= ('A' - 'a');
}
// strlen(s) , , ?
//Why couldn’t compiler move strlen out of inner loop?
// (1)Procedure may have side effects
// Alters global state each time called
// (2)Function may not return same value for given arguments
// Depends on other parts of global state
// Procedure lower could interact with strlen
//Warning:
(1)Compiler treats procedure call as a black box
(2)Weak optimizations near them
//
int f(void);
int func1(void)
{
return f() + f() + f() + f();
//procedure call 4*f(),
}
int func2(void)
{
return 4 * f();
}
int counter = 0;
int f(void)
{
return counter++;
}