Difference between revisions of "Optimization Topics"

From Wiki**3

(Machine-independent optimizations)
Line 21: Line 21:
  
 
Direct evaluation of expressions using only literal and known values:
 
Direct evaluation of expressions using only literal and known values:
 
+
<c>
 
  int *x = (int *)malloc(1024 * 4);
 
  int *x = (int *)malloc(1024 * 4);
 
  int  y = 3;
 
  int  y = 3;
 
  int  z = 20 * y + 10;
 
  int  z = 20 * y + 10;
 +
</c>
  
 
Becomes:
 
Becomes:
 
+
<c>
 
  int *x = (int *)malloc(4096);
 
  int *x = (int *)malloc(4096);
 
  int  y = 3;
 
  int  y = 3;
 
  int  z = 70;
 
  int  z = 70;
 +
</c>
  
 
== Elimination of Common Subexpressions ==
 
== Elimination of Common Subexpressions ==
  
 
Using temporary variables to store common results:
 
Using temporary variables to store common results:
 
+
<c>
 
   d = c * (a + b)
 
   d = c * (a + b)
 
   e = (a + b) / 2
 
   e = (a + b) / 2
 +
</c>
  
 
Becomes:
 
Becomes:
 
+
<c>
 
   t = a + b
 
   t = a + b
 
   d = c * t
 
   d = c * t
 
   e = t / 2
 
   e = t / 2
 +
</c>
  
 
== Cycles / Loops ==
 
== Cycles / Loops ==
  
 
=== Loop unrolling ===
 
=== Loop unrolling ===
 
+
<c>
 
   for (i = 1; i < n; i++)
 
   for (i = 1; i < n; i++)
 
     a[i] = b[i] + 10;
 
     a[i] = b[i] + 10;
 +
</c>
  
 
Becomes:
 
Becomes:
 
+
<c>
 
   for (i = 1; i < (n/4)*4; i+=4) {
 
   for (i = 1; i < (n/4)*4; i+=4) {
 
     a[i]  = b[i]  + 10;
 
     a[i]  = b[i]  + 10;
Line 64: Line 69:
 
   for (; i < n; i++)
 
   for (; i < n; i++)
 
     a[i] = b[i] + 10;
 
     a[i] = b[i] + 10;
 +
</c>
  
 
=== Moving invariant code ===
 
=== Moving invariant code ===
 
+
<c>
 
   for (i = 1; i < n; i++) {
 
   for (i = 1; i < n; i++) {
 
     a[i] = b[i] + c * d;
 
     a[i] = b[i] + c * d;
 
     e = g[k];
 
     e = g[k];
 
   }
 
   }
 +
</c>
  
 
Becomes:
 
Becomes:
 
+
<c>
 
   t = c * d;
 
   t = c * d;
 
   for (i = 1; i < n; i++) {
 
   for (i = 1; i < n; i++) {
Line 79: Line 86:
 
   }
 
   }
 
   e = g[k];
 
   e = g[k];
 +
</c>
  
 
=== Variable induction ===
 
=== Variable induction ===
 
+
<c>
 
   for (i = 1; i < n; i++)
 
   for (i = 1; i < n; i++)
 
     k = i * 4 + m;
 
     k = i * 4 + m;
 +
</c>
  
 
Becomes:
 
Becomes:
 
+
<c>
 
   k = m;
 
   k = m;
 
   for (i = 1; i < n; i++)
 
   for (i = 1; i < n; i++)
 
     k = k + 4;
 
     k = k + 4;
 +
</c>
  
 
=== Other ===
 
=== Other ===

Revision as of 10:22, 3 March 2015

Compiladores
Introdução ao Desenvolvimento de Compiladores
Aspectos Teóricos de Análise Lexical
A Ferramenta Flex
Introdução à Sintaxe
Análise Sintáctica Descendente
Gramáticas Atributivas
A Ferramenta YACC
Análise Sintáctica Ascendente
Análise Semântica
Geração de Código
Tópicos de Optimização

Basic Blocks

Instruction sequence where control flow starts at the first instruction and ends at the last, without jumps (except at the last instruction).

Transformations in a basic block maintain the block semantics (considered as an atomic entity).

Optimization Levels

  • User - algorithm- and application-level optimizations
  • Generic - considers intermediate code (processor-independent)
  • Peephole - window over a series of consecutive instructions
  • Local - optimization within a basic block
  • Inter-block (information flow between blocks, mainly cycles)
  • Global - multiple jumps (jumps to jumps, jumps to the next instruction)

Machine-independent optimizations

Constant folding

Direct evaluation of expressions using only literal and known values: <c>

int *x = (int *)malloc(1024 * 4);
int  y = 3;
int  z = 20 * y + 10;

</c>

Becomes: <c>

int *x = (int *)malloc(4096);
int  y = 3;
int  z = 70;

</c>

Elimination of Common Subexpressions

Using temporary variables to store common results: <c>

 d = c * (a + b)
 e = (a + b) / 2

</c>

Becomes: <c>

 t = a + b
 d = c * t
 e = t / 2

</c>

Cycles / Loops

Loop unrolling

<c>

 for (i = 1; i < n; i++)
   a[i] = b[i] + 10;

</c>

Becomes: <c>

 for (i = 1; i < (n/4)*4; i+=4) {
   a[i]   = b[i]   + 10;
   a[i+1] = b[i+1] + 10;
   a[i+2] = b[i+2] + 10;
   a[i+3] = b[i+3] + 10;
 }

 // rest of the cycle
 for (; i < n; i++)
   a[i] = b[i] + 10;

</c>

Moving invariant code

<c>

 for (i = 1; i < n; i++) {
   a[i] = b[i] + c * d;
   e = g[k];
 }

</c>

Becomes: <c>

 t = c * d;
 for (i = 1; i < n; i++) {
   a[i] = b[i] + t;
 }
 e = g[k];

</c>

Variable induction

<c>

 for (i = 1; i < n; i++)
   k = i * 4 + m;

</c>

Becomes: <c>

 k = m;
 for (i = 1; i < n; i++)
   k = k + 4;

</c>

Other

Machine-dependent Optimizations

Algebraic Simplification

xor eax,eax

instead of

mov 0,eax

Strength Reduction

x << 2 + x

instead of

x * 5

Instruction Reordering

Reorder independent instructions in order to avoid register spilling.

Other

Exercises