Premature code optimization case?

18 views
Skip to first unread message

Leonardo Hirokazu De Souza Hamada

unread,
Aug 14, 2021, 3:21:12 AM8/14/21
to jallist

Greetings to all,

I'm new to JAL, only using it a couple of month now.

So I have the following code:

;-------------------------------------------- START
-- Compiler: >=2.4q2

const Branch_A = 0
const Branch_B = 1
const Branch_C = 2

const byte strA[] = "A"
const byte strB[] = "B"
const byte strC[] = "C"

procedure optimize_me(byte in value) is
          case value of
               Branch_A: serial_hw_write(strA)
               Branch_B: serial_hw_write(strB)
               Branch_C: serial_hw_write(strC)
          end case
          forever loop
          end loop
end procedure

var bit maybe = false
if maybe then
   optimize_me(Branch_A)
end if
optimize_me(Branch_C)
;--------------------------------------------END


I noticed that as the compiler analyses the code path and sees the forever loop it tries to prune the case ramification for items that it considers unreachable.

Yet, what happens is that it tries to optimize the whole case block at the first call instance (Branch_A), leaving an incomplete compiled code for other cases, there are workarounds to it, this is just a comment to this behavior.

Thank-you,

Leonardo Hamada

The asm code it generates with default compiler options is:
; -----START ASM
l_optimize_me
;   55           case value of
;   56                Branch_A: serial_hw_write(strA)
                               movlw    65
                               call     l_serial_hw_write
                               goto     l__l413
;   57                Branch_B: serial_hw_write(strB)
;   58                Branch_C: serial_hw_write(strC)
l__l409
;   59           end case
l__l406
;   60           forever loop
l__l413
;   61           end loop
                               goto     l__l413
;   62 end procedure
l__l402
;   64 var bit maybe = false
                               bcf      v__bitbucket, 0,v__access ; maybe
;   65 if maybe then
                               btfsc    v__bitbucket, 0,v__access ; maybe
;   66    optimize_me(Branch_A)
                               call     l_optimize_me
;   67 end if
l__l416
;   68 optimize_me(Branch_C)
                               call     l_optimize_me
;  112 forever loop
l__l417
;  113 end loop
                               goto     l__l417
                               end

rob...@hotmail.com

unread,
Aug 14, 2021, 3:36:54 AM8/14/21
to jallist
Hi Leonardo,

Welcome to  JAL and Jallib!

Interesting case this optimization issue. Never seen it before. Need to find out why this happens.

Kind regards,

Rob


Op zaterdag 14 augustus 2021 om 09:21:12 UTC+2 schreef leonard...@ufra.edu.br:

Rob CJ

unread,
Aug 14, 2021, 4:00:30 AM8/14/21
to jal...@googlegroups.com
Hi Leonardo,

First analysis shows that it has seems it has to do something with the procedure call in the case statement. If you replace it by e.g. something like value = value + 1 then the problems is gone.


Given the following program, the compiler optimizes the code incorrectly, not generation code for Branch_B and Branch_C of the case statement. -- Sample program to show an optimization problem. inc...

Kind regards,

Rob



Van: jal...@googlegroups.com <jal...@googlegroups.com> namens rob...@hotmail.com <rob...@hotmail.com>
Verzonden: zaterdag 14 augustus 2021 09:36
Aan: jallist <jal...@googlegroups.com>
Onderwerp: [jallist] Re: Premature code optimization case?
 
--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallist/cff2707c-e128-4b22-86dc-e02ec656815en%40googlegroups.com.

Leonardo Hirokazu De Souza Hamada

unread,
Aug 16, 2021, 7:53:43 PM8/16/21
to jallist

Hello,

Thanks for the response.

I did not mention in the first post but before realizing what has happening, I wrote the case block (the example is just a short code pattern) equivalent using "if...elsif..else" construct and I remember not having success either.  So there's that.

Greetings,

Leonardo

Rob CJ

unread,
Aug 17, 2021, 1:15:33 PM8/17/21
to jal...@googlegroups.com
Hi Leonardo, 

If I use if-elsif then I it seem to be OK, so using this in the program:

procedure optimize_me(byte in value) is
          case value of
               Branch_A: do_something(value) 
               Branch_B: do_something(value) -- Incorrectly optimzed away             
               Branch_C: do_something(value) -- Incorrectly optimized way  
          end case
         forever loop
         end loop
end procedure

procedure optimize_me_too(byte in value) is
   if value == Branch_A then
      do_something(value) 
   elsif value == Branch_B then
      do_something(value) 
   elsif value == Branch_C then
      do_something(value) 
   end if 
end procedure

Results in this assembly code:
; d:\PIC\Compiler\Test\Issues\Issue#21\Optimize.jal
;    9 procedure do_something(byte in what) is
l_do_something
                               movwf    v___what_1
;   10    what = what + 1
                               incf     v___what_1,f
;   11 end procedure
                               return  
;   18 procedure optimize_me(byte in value) is
l_optimize_me
                               movlb    0
                               movwf    v___value_1
;   19           case value of
;   20                Branch_A: do_something(value)
                               call     l_do_something
                               movlp    HIGH l__l83
                               goto     l__l83
;   21                Branch_B: do_something(value) -- Incorrectly optimzed away            
;   22                Branch_C: do_something(value) -- Incorrectly optimized way  
l__l79
;   23           end case
l__l76
;   24          forever loop
l__l83
;   25          end loop
                               goto     l__l83
;   28 procedure optimize_me_too(byte in value) is
l_optimize_me_too
                               movlb    0
                               movwf    v___value_3
;   29    if value == Branch_A then
                               movf     v___value_3,w
                               btfss    v__status, v__z
                               goto     l__l88
;   30       do_something(value)
                               movf     v___value_3,w
                               goto     l_do_something
;   31    elsif value == Branch_B then
l__l88
                               decf     v___value_3,w
                               btfss    v__status, v__z
                               goto     l__l89
;   32       do_something(value)
                               movf     v___value_3,w
                               goto     l_do_something
;   33    elsif value == Branch_C then
l__l89
                               movlw    2
                               subwf    v___value_3,w
                               btfss    v__status, v__z
                               goto     l__l90
;   34       do_something(value)
                               movf     v___value_3,w
                               goto     l_do_something
;   35    end if
l__l90
l__l87
;   36 end procedure
                               return  

Kind regards,

Rob


Van: 'Leonardo Hirokazu De Souza Hamada' via jallist <jal...@googlegroups.com>
Verzonden: dinsdag 17 augustus 2021 01:53
Aan: jallist <jal...@googlegroups.com>
Onderwerp: Re: [jallist] Re: Premature code optimization case?
 
Reply all
Reply to author
Forward
0 new messages