Recent

Author Topic: [CLOSED] How optimization works ?  (Read 1576 times)

julkas

  • Guest
[CLOSED] How optimization works ?
« on: December 19, 2019, 04:32:00 pm »
Following code -
Code: Pascal  [Select][+][-]
  1. const
  2.   XOFF = 32;
  3.  
  4. var
  5.   bf: array[0..511] of byte;
  6.   bp: PByte;  
  7.  
  8. begin
  9.   bp := @bf + XOFF - 13;
  10. ...
  11.  

compiled with -O3 generates asm
Code: ASM  [Select][+][-]
  1.         mov     r2,r18
  2.         ldi     r18,32
  3.         add     r19,r18
  4.         adc     r2,r1
  5.         subi    r19,13
  6.         sbc     r2,r1
  7. # Var bp located in register r3
  8.  

How optimization works here ?
« Last Edit: December 21, 2019, 02:19:11 pm by julkas »

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: How optimization works ?
« Reply #1 on: December 19, 2019, 08:27:39 pm »
Some assembler code seems to be missing.  If I compile (-O3) only the code snippet you showed in a simple program then I get the following:
Code: ASM  [Select][+][-]
  1.  [11] bp := @bf + XOFF - 13;
  2.         ldi     r18,lo8(U_sPsTEST_AVR_ss_BF)
  3.         ldi     r19,hi8(U_sPsTEST_AVR_ss_BF)
  4.         ldi     r20,32
  5.         add     r18,r20
  6.         adc     r19,r1
  7.         subi    r18,13
  8.         sbc     r19,r1
  9. # Var bp located in register r18

For this simple case the compiler optimized away the storage of bp and it is located in two registers (since a pointer is 16 bits): r18 & r19.  bf is stored in SRAM (the linker still needs to resolve the symbol U_sPsTEST_AVR_ss_BF, this is assembler before it was linked by passing -al as option to the compiler).

Lines 2, 3 above loads the hi and lo bytes of the memory address of bf into bp.
Lines 4..6 adds XOF (32) to r18,r19 with carry
Lines 7,8 subtracts 13 from r18,r19 with borrow (if that is the term used?)

I guess the interesting bit is that 32 - 13 is not combined into a new constant, 19. This is because the compiler evaluates left to right.  To help the compiler one can use parentheses so that the diference between two constants are pre-evaluated:
Code: Pascal  [Select][+][-]
  1.   bp := @bf + (XOFF - 13);

The resulting assembler:
Code: Pascal  [Select][+][-]
  1. # [11] bp := @bf + (XOFF - 13);
  2.         ldi     r18,lo8(U_sPsTEST_AVR_ss_BF)
  3.         ldi     r19,hi8(U_sPsTEST_AVR_ss_BF)
  4.         ldi     r20,19
  5.         add     r18,r20
  6.         adc     r19,r1
  7. # Var bp located in register r18

julkas

  • Guest
Re: How optimization works ?
« Reply #2 on: December 19, 2019, 08:51:41 pm »
I guess the interesting bit is that 32 - 13 is not combined into a new constant, 19. This is because the compiler evaluates left to right.  To help the compiler one can use parentheses so that the diference between two constants are pre-evaluated:
Code: Pascal  [Select][+][-]
  1.   bp := @bf + (XOFF - 13);

The resulting assembler:
Code: Pascal  [Select][+][-]
  1. # [11] bp := @bf + (XOFF - 13);
  2.         ldi     r18,lo8(U_sPsTEST_AVR_ss_BF)
  3.         ldi     r19,hi8(U_sPsTEST_AVR_ss_BF)
  4.         ldi     r20,19
  5.         add     r18,r20
  6.         adc     r19,r1
  7. # Var bp located in register r18
Exactly! Great diff for embedded.
I think compiler can / must optimize expression.
Thanks.

 

TinyPortal © 2005-2018