1 Übungblatt 10 
~~~~~~~~~~~~~~~~

1.1 Languages in a Compiler except source and target language? 
===============================================================
   Intermediate Languages
* 
    - Why useful? 
      1. better suited for certain implementation tasks
      2. common representation of different source languages
* 
    - Why not? 
      - Maybe too expensive to create (e.g. for JIT Compilers)
* 
    - Name a common intermediate language 
      3-Address-Code (3AC)
        + 
            * What? 
              - common intermediate language
              - commands can have 3 operands
              - commands have labels
              - e.g. x = a+b, call m
              
        + 
            * Name some properties of 3AC 
              - only elementary data
              - no nested expressions
              - sequential execution
              - jumps to labels
              - named variables as in a high level language
              - unbounded number of temporary variables
        + 
            * Can there be procedure call commands, like call m? 
              - yes
        + 
            * Advantages of 3AC compared to Source language? 
              - easier to reorder commands (explicit variables, no stack)
              - easier to assign registers
              - ...
    - 
        + What is a basic block ? 
          maximal sequence of commands such that
          - at the end of B, exactly one jump, procedure call, or
            return command occurs
          - labels only occur at the first command of a basic block
            * 
                - Why useful? 
                  - control flow is explicit
                  - rearrangement and exchange of commands easier
                  - basis for many optimizations and analysis
            * 
                - How are branches, jumps handled? 
                  - by control flow graph
                  - i.e. introduce transitions between basic blocks
* 
    - What is Static Single Assignment? 
      - is a refinement of 3AC with the restriction that
        each variable has exactly one definition
        + 
            * Why useful? 
              - relationship between application and definition is explicit
        + 
            * How transform 3AC to SSA? 
              - add index to variables
                - 
                    + Transform the following 3AC to SSA 
                      a = x + y
                      b = a - 1
                      a = y + b
                      b = x * 4
                      a = a + b
                        * Answer 
                          a1 = x0 + y0
                          b1 = a1 - 1
                          a2 = y0 + b1
                          b2 = x0 * 4
                          a3 = a2 + b2
        + 
            * How are join points in control flows handled in SSA? 
              introduce phi-function to get new variables
              a3 = phi(a2,a1)

1.2 
====
* Name at least 3 compiler optimizations 
    - constant propagation 
      - replace constant variables by constants
    - constant folding 
      - replace constant expressions by constants
    - copy propagation 
      - eliminate variables that have the same value
    - common subexpression eliminiation 
      - evaluate common subexpression only once
    - algebraic optimizations 
      - apply algebraic laws at compile time (communtativity, associativity)
    - strength reduction 
      - replace expensive operations by more efficient ones
    - inline expansion of procedure calls 
      - replace non-recursive procedures calls by their body
    - dead code elimination 
      - remove unreachable code
    - code motion 
      - move commands between branches to execute them less often
        + partial redundancy eliminiation 
          - remove/move assignments/expressions that are already executed on all/some paths
        + code motion from loops 
          - move operations out of loops
            * 
                - What are Loop Invariants? 
                  - something that does not change during the execution of a loop
                  - can be variables or expressions
                    
            * 
                - What are Explicit Loop Variables? 
                  - a variable inside a loop with exactly one definition
                    i = i+c, where c is a loop invariant
                - What are Derived Loop Variables? 
                  - there is exactly one definition
                    k = j * c or
                    k = j + c
                    where j is loop variable and c is a loop invariant
            * 
                - What is Loop unrolling? 
                  - loop body is copied several times

1.3 
====
* 
    - When is a variable v live? 
      - v is live at program position S if
        there is an execution path from S to a use of v 
        on which there is no definition of v
* 
    - What does liveness analysis? 
      - determines for all positions S in P which variables are live
* 
    - what is out(B) and in(B)? 
      - out(B): live variables at the end of basic block B
      - in(B): live variables at the beginning of B
    - what is kill(B)? 
      - the set of variables that are defined in B
    - what is gen(B)? 
      - the set of variables that are applied in B without a prior definition
* 
    - How can the set of live variables in(B) be defined if out(B) is given? 
      - in(B) = gen(B) U (out(B) \ kill(B))

1.4 
====
* What is a Points-To-Analysis? 
  - Analysis for programs with pointers and for object-oriented programs
  - goal: compute which references to which records/objects a variable can hold
* 
    - Why useful? 
      - Basis for optimizations like
        - resolution of virtual method calls
        - escape analysis
        - alias informations for code motion
        - ...
* 
    - What means flow-insensitive? 
      - the control flow of the program has no influence on the analysis result
* 
    - What means context-insensitive? 
      - method calls at different program points are not distinguished
* 
    - What are the nodes and edges of a Points-To-Graph? 
      - nodes: abstract variables and abstract objects
      - edges: abstract variable may have a reference to an abstract object
* 
    - Can objects have edges to other objects? 
      - yes in that case an attribute refers to the object
      - edge has label with name of attribute