cpik performs many optimizations, but not all possible optimizations. Optimizations can be performed during code analysis, intermediate code generation, asm code generation and suprisingly after the code generation.
Most expressions that have no effect are simply removed. For example i = i + 0 ; does not produce any code.
Value of W register is tracked whenever possible, so it is not reloaded when it contains the proper value. This feature only concern the W register, but I plan to extend it to FSR1 register.
Most constant subexpressions are computed by the compiler, so complex expressions are often compiled as a single constant (eg: x= (1+2*4) « 1 ;). However, a lot of constant foldings are done by the peephole optimizer or the expression simplifier (eg: st.a.b.c = 34 ; is exactly compiled like x = 34 ;)
Intermediate code is analyzed by grouping instructions into slices of 2, 3 or 4 items. Each slice is compared against a library of possible simplifications or transformations. Then, the simplified code is simplifed again, and so on. This kind of optimization may lead to 25% to 30% gain.
This is the only phase that depends on the target architecture. Bit operations are excellent candidates for optimization. For example, consider the following macro to reset a single bit:
#define CLRBIT(var,bit) ((var) &= ~(1 << (bit)))so
CLRBIT(i,3) ;is expanded as
((i) &= ~(1 << (3))) ;which is optimally translated as:
bcf INDF0,3,0
This example is a combination of constant folding and code generator optimizations.
cpik takes into account constants when testing boolean conditions. For example, instuctions like
if(0) { ... }or
while(0) { ... }do not generate any code. In the same way,
while(1) { .. }generates a genuine infinite loop with no test, exactly like
for(;;) { .. }
does.
However cpik does not perform a global analysis of code, so common subexpression removal are out of scope at this time.
cpik contains a special optional7 optimizer that allows to use short jumps instead of long ones, whenever possible. This step is executed after the asm source code generation and can reduce the memory size by .
AG 2013-04-10