Is the optimizer hyperactive?

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Guys,

please help me out in the following matter:

i wanted to compile the world's simplest code to bring my uc3c1512c to life:

int main(void)
{
  tUI32 tick;
  tBOOL sw_on;

  sw_on = FALSE;
  tick = 0U;

  while (1U)
  {
    tick++;
    if (tick >= 2000U)
    {
      sw_on = !sw_on;
      if (sw_on)
      {
        GPIO.port[1U].ovr.s = MASK(6U);
      }
      else
      {
        GPIO.port[1U].ovr.c = MASK(6U);
      }
      tick = 0U;
    }
  }
  return 0;
}

The compiled result with -O2 optimization level misses some vital code parts:

Disassembly of section .text.main:

80000124 
: 80000124: 30 08 mov r8,0 80000126: 30 1b mov r11,1 80000128: fe 7a 20 00 mov r10,-57344 8000012c: 34 09 mov r9,64 8000012e: f6 08 18 00 cp.b r8,r11 80000132: f5 f9 1a 95 st.wne r10[0x254],r9 80000136: f9 b8 01 01 movne r8,1 8000013a: f5 f9 0a 96 st.weq r10[0x258],r9 8000013e: f9 b8 00 00 moveq r8,0 80000142: cf 6b rjmp 8000012e

If i modify the source code by moving the increase instruction to the end of the block:

...
  while (1U)
  {
    if (tick >= 2000U)
    {
      sw_on = !sw_on;
      if (sw_on)
      {
        GPIO.port[1U].ovr.s = MASK(6U);
      }
      else
      {
        GPIO.port[1U].ovr.c = MASK(6U);
      }
      tick = 0U;
    }
    tick++;
  }
...

I get a working assembly code with the same optimization level (-O2):

Disassembly of section .text.main:

80000124 
: 80000124: 30 09 mov r9,0 80000126: fe 7b 20 00 mov r11,-57344 8000012a: 12 98 mov r8,r9 8000012c: 34 0a mov r10,64 8000012e: 2f f8 sub r8,-1 80000130: e0 48 07 cf cp.w r8,1999 80000134: fe 98 ff fd brls 8000012e 80000138: 58 09 cp.w r9,0 8000013a: f7 fa 0a 95 st.weq r11[0x254],r10 8000013e: f9 b9 00 01 moveq r9,1 80000142: f9 b8 00 00 moveq r8,0 80000146: f7 fa 1a 96 st.wne r11[0x258],r10 8000014a: f9 b9 01 00 movne r9,0 8000014e: f2 08 17 10 movne r8,r9 80000152: 2f f8 sub r8,-1 80000154: e0 48 07 cf cp.w r8,1999 80000158: fe 9b ff f0 brhi 80000138 8000015c: ce 9b rjmp 8000012e

The version numbers of the toolchain i use:

AVR32/GNU C Compiler version: (AVR_32_bit_GNU_Toolchain_3.4.0_332) 4.4.3

AVR32/GNU Assembler version: GNU assembler (AVR_32_bit_GNU_Toolchain_3.4.0_332) 2.22

AVR32/GNU Linker version: (AVR_32_bit_GNU_Toolchain_3.4.0_332) 4.4.3

Is the optimizer right or something is going on here?

Thank you very much for your help in advance.
BR,
Menahem
[/code]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think it is !.

Using AVR32_Toolchain_Version:3.4.2.435 GCC_VERSION:4.4.7 and optimisation -O2 or -O1 I can recreate your problem.
Seems that tick is being totally removed.

80000090 
: int main( void ) { uint32_t tick; bool sw_on; sw_on = 0; tick = 0; 80000090: 30 08 mov r8,0 80000092: fe 7a 20 00 mov r10,-57344 80000096: 34 09 mov r9,64 while (1) { tick ++; if (tick >= 2000U) { sw_on = !sw_on; 80000098: ec 18 00 01 eorl r8,0x1 if (sw_on) { AVR32_GPIO.port[1].ovrs = 1 << 6; 8000009c: f5 f9 1a 95 st.wne r10[0x254],r9 } else { AVR32_GPIO.port[1].ovrc = 1U << 6; 800000a0: f5 f9 0a 96 st.weq r10[0x258],r9 800000a4: cf ab rjmp 80000098

Moving tick++ to the end of the while(1) works OK.
Declaring tick as volatile uint32_t works OK.
Using -O0 works OK.

It seems that the tick = 0; inside the if() is upsetting the optimiser.

This also works ;

	while(1)
		{
		tick ++;
		if ( ( tick % 2000 ) == 0 )
			{
			sw_on = !sw_on;
			if (sw_on)
				{
				AVR32_GPIO.port[1].ovrs = 1 << 6;
				}
			else
				{
				AVR32_GPIO.port[1].ovrc = 1 << 6;
				}
			}
		}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Mikech,

many thanks for checking the issue.

Okay, i can live with these hot fixes (i also thank you for them) but when you are debugging a 2K-line source code you want to be sure that you made something wrong, not the tool. :)

Is there any official forum where such kind of potentional bugs could be reported?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The only forum I know about is at http://asf.atmel.com/bugzilla , which is for ASF issues, but it does have some compiler items in there.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Mikech, i've just done it (BUG3204).
BR,
Menahem