Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Barthelmann V.VBCC compiler system.2004.pdf
Скачиваний:
12
Добавлен:
23.08.2013
Размер:
390.02 Кб
Скачать

Chapter 3: The Compiler

19

will be reused instead of recomputing it. Also, memory operands will be loaded into registers and reused instead of being reloaded, if possible.

For example, the following code void f(int x, int y)

{

q(x * y, x * y);

}

will be optimized like:

void f(int x, int y)

{

int tmp;

tmp = x * y; q(tmp, tmp);

}

Depending on the optimization level, vbcc will perform this optimization only locally within basic blocks or globally across an entire function.

As this optimization requires detecting whether operand of an expression may have changed, it will be a ected by other optimizations. See Section 3.4.14 [Alias Analysis], page 27, Section 3.4.15 [Inter-Procedural Analysis], page 29 and Section 3.4.16 [Cross-Module Optimizations], page 29.

3.4.4 Copy Propagation

If a variable is assigned to another one, the original variable will be used as long as it is not modified. This is especially useful in conjunction with other optimizations, e.g. common subexpression elimination.

For example, the following code int y;

int f()

{

int x; x = y;

return x;

}

will be optimized like: int y;

int f()

{

return y;

}

Depending on the optimization level, vbcc will perform this optimization only locally within basic blocks or globally across an entire function.

20

vbcc manual

As this optimization requires detecting whether a variable may have changed, it will be a ected by other optimizations. See Section 3.4.14 [Alias Analysis], page 27, Section 3.4.15 [Inter-Procedural Analysis], page 29 and Section 3.4.16 [Cross-Module Optimizations], page 29.

3.4.5 Constant Propagation

If a variable is known to have a constant value (this includes addresses of objects) at some use, it will be replaced by the constant.

For example, the following code

int f()

{

int x; x = 1;

return x;

}

will be optimized like:

int f()

{

return 1;

}

Depending on the optimization level, vbcc will perform this optimization only locally within basic blocks or globally across an entire function.

As this optimization requires detecting whether a variable may have changed, it will be a ected by other optimizations. See Section 3.4.14 [Alias Analysis], page 27, Section 3.4.15 [Inter-Procedural Analysis], page 29 and Section 3.4.16 [Cross-Module Optimizations], page 29.

3.4.6 Dead Code Elimination

If a variable is assigned a value which is never used (either because it is overwritten or its lifetime ends), the assignment will be removed. This optimization is crucial to remove code which has become dead due to other optimizations.

For example, the following code

int x;

void f()

{

int y; x = 1; y = 2; x = 3;

}

will be optimized like:

Chapter 3: The Compiler

21

int x;

void f()

{

x = 3;

}

As this optimization requires detecting whether a variable may be read, it will be a ected by other optimizations. See Section 3.4.14 [Alias Analysis], page 27, Section 3.4.15 [InterProcedural Analysis], page 29 and Section 3.4.16 [Cross-Module Optimizations], page 29.

3.4.7 Loop-Invariant Code Motion

If the operands of a computation within a loop will not change during iterations, the computation will be moved outside of the loop.

For example, the following code void f(int x, int y)

{

int i;

for (i = 0; i < 100; i++) q(x * y);

}

will be optimized like:

void f(int x, int y)

{

int i, tmp = x * y;

for (i = 0; i < 100; i++) q(tmp);

}

As this optimization requires detecting whether operands of an expression may have changed, it will be a ected by other optimizations. See Section 3.4.14 [Alias Analysis], page 27, Section 3.4.15 [Inter-Procedural Analysis], page 29 and Section 3.4.16 [Cross-Module Optimizations], page 29.

3.4.8 Strength Reduction

This is an optimization applied to loops in order to replace more costly operations (usually multiplications) by cheaper ones (typically additions). Linear functions of an induction variable (a variable which is changed by a loop-invariant value in every iteration) will be replaced by new induction variables. If possible, the original induction variable will be eliminated.

As array accesses are actually composed of multiplications and additions, they often benefit significantly by this optimization.

For example, the following code

22

vbcc manual

void f(int *p)

{

int i;

for (i = 0; i < 100; i++) p[i] = i;

}

will be optimized like: void f(int *p)

{

int i;

for (i = 0; i < 100; i++) *p++ = i;

}

As this optimization requires detecting whether operands of an expression may have changed, it will be a ected by other optimizations. See Section 3.4.14 [Alias

Analysis], page 27, Section 3.4.15 [Inter-Procedural Analysis], page 29 and Section 3.4.16

[Cross-Module Optimizations], page 29.

3.4.9 Induction Variable Elimination

If an induction variable is only used to determine the number of iterations through the loop, it will be removed. Instead, a new variable will be created which counts down to zero. This is generally faster and often enables special decrement-and-branch or decrement-and-compare instructions.

For example, the following code

void f(int n)

{

int i;

for (i = 0; i < n; i++) puts("hello");

}

will be optimized like:

void f(int n)

{

int tmp;

for(tmp = n; tmp > 0; tmp--) puts("hello");

 

}

 

 

 

 

As

this optimization

requires detecting whether operands

of

an expression

may

have

changed, it will

be a ected by other optimizations.

See

Section 3.4.14

[Alias