Code transformations

Started by Tim Frink March 10, 2008

I want to reduce code size of my DSP code. The Infineon TriCore processor
I'm using supports two different types of instructions, a 16 and a 32 bit
version. There are no equivalent 16bit instructions for all 32bit
instruction and the 16bit instructions are obviously more constrained in
terms of their operands. So, for example for jumps, the maximal
displacement can be 8bit large while the 32bit jumps support up to 16bit.
For my question, I however assume that the size of the displacement
operand is not an issue.

What I want to do in particular is the substitution of 32bit jumps
by equivalent 16bit jumps. This is a piece of assembly code I often
find in my program and I would like to optimize:

        move     d1, 10
        move     d2, 1000
        jge      d2, d1, target

So, the constants 10 and 1000 are stored into registers d1 and d2
and finally the jump (jump greater/equal) is taken to "target" if
the value of register d2 >= d1.

To reduce code size, I'd like to replace this 32bit jump by a
16bit jump which is defined as:

	jz/jnz dX, target

So, whenever the value of register dX is zero (jz) or is not
zero (jnz), the branch is taken.

Now I'm wondering how the first code can be efficiently transformed
into an semantically equal code where jz/jnz is used. Obviously,
jz/jnz has a slightly different meaning compared to jge but I hope
that there is a way to transform the second code without using too
many additional instructions (which would undo the code size 
reduction I achieve with the jump instruction replacement).

A possible solution (but not applicable due to an additional code size
increase) could look like:

   move d1, 10
   move d2, 1000    
   sub  d3, d2, d1
   ge   d4, d2, 0  # d4 becomes 1 if d2 >= 0
   jnz  d4, target

Do you see any better solution. It's fully OK if you give an
example in any pseudo assembler code.

Thank you.