Electronic – Extraneous loop produced by PIC compiler from MicroChip

chi-tech-compilermicrochippic

We're using the Lite version of the MicroChip PIC compiler so maybe that's the reason, but this simple one-bit shift is generating a loop where none is necessary. Since the shift count is 1 (a constant), I would expect the compiler to create no loop—an elementary optimization.

Is there a compiler optimization switch that would alleviate the loop?
Here is the code for the shift:

    long foo;   // a 32-bit value

// Shift foo one bit. 
// A one-iteration loop is created!

    foo >>= 1;

And here is the compiler-generated code. As you can see, the shift is wrapped with a one-iteration loop.

  07F6    3001     MOVLW 0x1
  07F7    00F2     MOVWF 0x72
  07F8    37F6     ASRF 0x76, F
  07F9    0CF5     RRF 0x75, F
  07FA    0CF4     RRF 0x74, F
  07FB    0CF3     RRF 0x73, F
  07FC    0BF2     DECFSZ 0x72, F

EDIT

Compiler Version: HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode) V9.81

Best Answer

Maybe this is caused by the lack of the optimization of the lite version.
You could try,

foo /= 2;

and see if it helps.