Docsity

Program Representations - Advanced Compiler - Lecture Slides , Slides of Computer Science

Partial preview of the text

Download Program Representations - Advanced Compiler - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Program Representations

Representing programs

  • Goals

Option 1: high-level syntax based IR

  • Represent source-level structures and expressions directly
  • Example: Abstract Syntax Tree

Option 2: low-level IR

  • Translate input programs into low-level primitive chunks, often close to the target machine
  • Examples: assembly code, virtual machine code (e.g. stack machines), three- address code, register- transfer language (RTL) - Standard RTL instrs:

Comparison

Comparison

  • Advantages of high-level rep
    • analysis can exploit high-level knowledge of constructs
    • easy to map to source code (debugging, profiling)
  • Advantages of low-level rep
    • can do low-level, machine specific reasoning
    • can be language-independent
  • Can mix multiple reps in the same compiler

Control dependencies

  • Option 1: high-level representation
    • control implicit in semantics of AST nodes
  • Option 2: control flow graph (CFG)
    • nodes are individual instructions
    • edges represent control flow between instructions
  • Options 2b: CFG with basic blocks
    • basic block: sequence of instructions that don’t have any branches, and that have a single entry point
    • BB can make analysis more efficient: compute flow functions for an entire BB before start of analysis

Control dependencies

  • CFG does not capture loops very well
  • Some fancier options include:
    • the Control Dependence Graph
    • the Program Dependence Graph
  • More on this later. Let’s first look at data dependencies

Def/use chains

  • Directly captures dataflow
    • works well for things like constant prop
  • But...
  • Ignores control flow
    • misses some opt opportunities since conservatively considers all paths
    • not executable by itself (for example, need to keep CFG around)
    • not appropriate for code motion transformations
  • Must update after each transformation
  • Space consuming

SSA

  • Static Single Assignment
    • invariant: each use of a variable has only one def

SSA

  • Create a new variable for each def
  • Insert φ pseudo-assignments at merge points
  • Adjust uses to refer to appropriate new names
  • Question: how can one figure out where to insert φ nodes using a liveness analysis and a reaching defns analysis.

Converting back from SSA

  • Semantics of x 3 := φ(x 1 , x 2 )
    • set x 3 to xi if execution came from ith predecessor
  • How to implement φ nodes?

Common Sub-expression Elim

  • Want to compute when an expression is available in a var
  • Domain:

Flow functions

X := Y op Z

in

out

FX := Y op Z (in) =

X := Y

in

out

FX := Y(in) =