Noeud « Next »: , Noeud « Previous »: T5 Primitive Samples, Noeud « Up »: T5 Samples



4.7.2.2 T5 Optimizing Cascading If

Our compiler optimizes the number of jumps needed to compute nested if, using translate::Ix where a plain use of translate::Cx, Nx, and Ex is possible, but less efficient.

Consider the following sample:

     if 11 | 22 then print ("OK\n")
     File 69: boolean.tig

a naive implementation will probably produce too many successive cjump instructions:

     $ tc --hir-naive -H boolean.tig
     /* == High Level Intermediate representation. == */

     label l3
             "OK\n"
     # Routine: Main
     label Main
     # Prologue
     # Body
     seq
         cjump ne
             eseq
             seq
                 cjump ne
                     const 11
                     const 0
                     name l0
                     name l1
                 label l0
                 move
                     temp t0
                     const 1
                 jump
                     name l2
                 label l1
                 move
                     temp t0
                     const 22
                 jump
                     name l2
                 label l2
             seq end
                 temp t0
             const 0
             name l4
             name l5
         label l4
         sxp
             call
                 name print
                 name l3
             call end
         jump
             name l6
         label l5
         sxp
             const 0
         jump
             name l6
         label l6
     seq end
     # Epilogue
     label end
     Example 70: tc --hir-naive -H boolean.tig
     $ tc --hir-naive -H boolean.tig >boolean-1.hir
     Example 71: tc --hir-naive -H boolean.tig >boolean-1.hir
     $ havm --profile boolean-1.hir
     error-->/* Profiling.  */
     error-->fetches from temporary : 1
     error-->fetches from memory    : 0
     error-->binary operations      : 0
     error-->function calls         : 1
     error-->stores to temporary    : 1
     error-->stores to memory       : 0
     error-->jumps                  : 2
     error-->conditional jumps      : 2
     error-->/* Execution time.  */
     error-->number of cycles : 16
     OK
     Example 72: havm --profile boolean-1.hir

If you carefully analyze the cause of this pessimization, it is related to the computation of an intermediary expression (the value of 11 | 22) which is later decoded as a condition. A proper implementation will produce:

     $ tc -H boolean.tig
     /* == High Level Intermediate representation. == */

     label l0
             "OK\n"
     # Routine: Main
     label Main
     # Prologue
     # Body
     seq
         seq
             cjump ne
                 const 11
                 const 0
                 name l4
                 name l5
             label l4
             cjump ne
                 const 1
                 const 0
                 name l1
                 name l2
             label l5
             cjump ne
                 const 22
                 const 0
                 name l1
                 name l2
         seq end
         label l1
         sxp
             call
                 name print
                 name l0
             call end
         jump
             name l3
         label l2
         sxp
             const 0
         label l3
     seq end
     # Epilogue
     label end
     Example 73: tc -H boolean.tig
     $ tc -H boolean.tig >boolean-2.hir
     Example 74: tc -H boolean.tig >boolean-2.hir
     $ havm --profile boolean-2.hir
     error-->/* Profiling.  */
     error-->fetches from temporary : 0
     error-->fetches from memory    : 0
     error-->binary operations      : 0
     error-->function calls         : 1
     error-->stores to temporary    : 0
     error-->stores to memory       : 0
     error-->jumps                  : 1
     error-->conditional jumps      : 2
     error-->/* Execution time.  */
     error-->number of cycles : 13
     OK
     Example 75: havm --profile boolean-2.hir