home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / M2V11-1.LHA / modula / docs / OPTOZS.DOC < prev    next >
Encoding:
Text File  |  1994-10-04  |  3.8 KB  |  100 lines

  1.             Compiler optimizations
  2.             ======================
  3.  
  4.    The compiler applies a few easy to implement optimizations to generate
  5.    small & fast code. The first two (as described below) give the Drystone
  6.    benchmark a considerable speed boost (almost twice as fast).
  7.    These optimizations increase compilation time, but because the compiler
  8.    compiles itself, the slowdown is more than made up for by the higher quality
  9.    code.
  10.    They are also performed by the DICE compiler and assembler.
  11.    The commercial M2Amiga compiler (I paid $200 back in 1991) used to
  12.    bootstrap m2c generates relatively larger and slower code becuase it
  13.    does not attempt to do any of the following.
  14.  
  15. Register Allocation
  16. ===================
  17.  
  18.    Simple local variables and parameters are allocated to registers based on
  19.    wieghted usage.
  20.    Parameters are copied off the stack and into registers at procedure entry.
  21.    Locals used inside nested loops are given highest priority.
  22.    Typically there are 6 data registers (d2-d7) and 4 address registers
  23.    (a2,a3,a5,a6) available.
  24.    Procedures that contain complex expressions may have less registers
  25.    available.
  26.    Low level procedures (that call no others) can also use d0,d1,a0,a1 to hold
  27.    locals.
  28.    Locals that are passed as VAR parameters to other procedures, arguments of
  29.    SYSTEM.ADR or locals referenced from within nested procedures
  30.    (so called non-local,non-global variables) cannot be allocated to registers.
  31.  
  32. LINK/UNLINK removal
  33. ===================
  34.  
  35.    If the compiler manages to allocate all parameters and variables for
  36.    a particular procedure to registers.Then it can remove the LINK & UNLINK
  37.    instruction pair which normally constructs/destructs a stack frame for
  38.    variables in that procedure.
  39.    If this can be done the procedure call overhead is greatly reduced. This is
  40.    specially important in small procedures eg. when using opaque types we
  41.    are sometimes forced to use a function just to read the contents of a field.
  42.  
  43. CASE statement
  44. ==============
  45.  
  46.    Generating code for the case statements is quite tricky. The compiler must
  47.    adequately cope with a variety of styles:
  48.  
  49.    CASE exp OF
  50.    |-42 : ..
  51.    |10000: ..    (* This is bad style but is still legal.                     *)
  52.    END         (* Some (inadequate) compilers will complain if you try this *)
  53.  
  54.  
  55.    or
  56.    ==
  57.  
  58.    CASE exp OF
  59.    |0: ..
  60.    |1: ..
  61.    |2: ..
  62.    |3: .. (* Case labels are contiguous                     *)
  63.    ...      (* This is really what the case statement is for *)
  64.    |100: ..
  65.    END ;
  66.  
  67.    or a combination of the 2 styles.
  68.  
  69.    The algorithm for the case statement is:
  70.  
  71.    If there are only a few case labels (less than 8 say) then perform a linear
  72.    search.
  73.    If the number of case labels is a high proportion (50 percent say) of
  74.    MAX(label)-MIN(label) then perform a table lookup to see where to branch to.
  75.    Otherwise subdivide the labels (into 2 havles) and recursively apply the
  76.    algorithm.
  77.  
  78.    The compiler generates code to implement this algorithm.
  79.  
  80. Branch Optimization
  81. ===================
  82.  
  83.    The M68000 provides a 16bit as well as 32bit branch instruction.
  84.    When the compiler generates a forward branch its impossible for it to know
  85.    which to use.To be safe it generates space for a 32bit instruction, if
  86.    subsequently it turns out that only a 16bit instruction was needed, the code
  87.    will contain a 2 bytes hole. The compiler keeps track of where these holes
  88.    occur and after code generation compacts all the code.
  89.    Branch optimization typically reduces code size by about 5 percent.
  90.  
  91. Future optimizations
  92. ====================
  93.  
  94.    The compiler does not implement registerized parameter passing, this may
  95.    be added. It doesn't do any 'global' optimizations either because I dont
  96.    know how :-) This is only my second compiler (the first was for a toy
  97.    language at college).
  98.  
  99.  
  100.