CSE411

CSE411: Introduction to Compilers

Intermediate Representation

Jooyong Yi (UNIST)

2026 Fall
CSE411

Where are we?

         |-------|            |--------|           |----------|        |-----------|
Source → | Lexer | → tokens → | Parser | → parse → | Semantic |→ AST → |   IR      | → IR
Program  |       |            |        |   tree    | Analyzer |        | Generator |
         |-------|            |--------|           |----------|        |-----------|
                 |-----------|        |----------|
Source Program → | Front End | → IR → | Back End | → Target Program
                 |-----------|        |----------|
                                  ↑
  • IR: Intermediate Representation (or Intermediate Code)
2026 Fall
CSE411

Example of IR

  • LLVM IR
int sum(int x, int y) { int z = x + y; return z; }
define i32 @sum(i32 noundef %0, i32 noundef %1) { ; noundef: no undefined value
  %3 = alloca i32, align 4 ; allocate the memory on the current call stack
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4
  store i32 %1, ptr %4, align 4
  %6 = load i32, ptr %3, align 4
  %7 = load i32, ptr %4, align 4
  %8 = add nsw i32 %6, %7 ; nsw: no signed wrap
  store i32 %8, ptr %5, align 4
  %9 = load i32, ptr %5, align 4
  ret i32 %9
}
2026 Fall
CSE411

Another Example of IR

  • Java Bytecode
static int sum(int x, int y) { int z = x + y; return z; }
  static int sum(int, int);
    descriptor: (II)I
    flags: (0x0008) ACC_STATIC
    Code:
      stack=2, locals=3, args_size=2
         0: iload_0
         1: iload_1
         2: iadd
         3: istore_2
         4: iload_2
         5: ireturn
      LineNumberTable:
        line 3: 0
        line 4: 4
2026 Fall
CSE411

Why IR?

  • Why not directly translate AST to the target program?
         |-------|            |--------|           |----------|        |-----------|
Source → | Lexer | → tokens → | Parser | → parse → | Semantic |→ AST → |   Code    | → Target
Program  |       |            |        |   tree    | Analyzer |        | Generator |   Program
         |-------|            |--------|           |----------|        |-----------|
2026 Fall
CSE411

Why IR? Portability and Maintenance

2026 Fall
CSE411

Why IR? Portability and Maintenance

2026 Fall
CSE411

Why IR? Portability and Maintenance

2026 Fall
CSE411

Why IR? Portability and Maintenance

2026 Fall
CSE411

Why IR? Portability and Maintenance

2026 Fall
CSE411

Why IR? Reusability

        

  • Code optimizations implemented on a common IR can be reused across different source languages
2026 Fall
CSE411

Why IR? Separation of Concerns

  • Directly translating an AST into target code requires considering both language semantics and machine-specific details simultaneously.
    • How should source-level variables be mapped to machine registers or memory locations?
    • How should source-level expressions and statements be translated into machine instructions?
      • Example: x = (a + b) * (c - d);
        t1 = a + b
        t2 = c - d
        t3 = t1 * t2
        x = t3
        
2026 Fall
CSE411

Three-Address Code

t1 = a + b
t2 = c - d
t3 = t1 * t2
x = t3
  • A general form: x = y op z
2026 Fall
CSE411

Simple Code Optimization

x = 10
y = x + 1    -->
x = 20
z = x + 1
2026 Fall
CSE411

Simple Code Optimization

x = 10             x = 10
y = x + 1    -->   y = 11
x = 20             x = 20
z = x + 1          z = 21
  • How does the compiler determine which value of x is used in each expression?
2026 Fall
CSE411

Static Single Assignment (SSA)

x = 10                   x0 = 10
y = x + 1    -- SSA -->  y0 = x0 + 1
x = 20                   x1 = 20
z = x + 1                z0 = x1 + 1
  • SSA: an IR form in which each variable is assigned exactly once in the code.
2026 Fall
CSE411

SSA for conditional branching

    if x > y goto L1                      if x0 > y0 goto L1
    goto L2                               goto L2

L1:                                   L1:
    x = 10                                x1 = 10
    goto L3                               goto L3

L2:                     -- SSA -->    L2:
    x = 20                                x2 = 20
    goto L3                               goto L3

L3:                                   L3:
    x = x + y                             ??
2026 Fall
CSE411

SSA for conditional branching

    if x > y goto L1                      if x0 > y0 goto L1
    goto L2                               goto L2

L1:                                   L1:
    x = 10                                x1 = 10
    goto L3                               goto L3

L2:                     -- SSA -->    L2:
    x = 20                                x2 = 20
    goto L3                               goto L3

L3:                                   L3:
    x = x + y                             x3 = φ(x1, x2)
                                          x4 = x3 + y0
2026 Fall
CSE411

SSA for loops

entry:                                  entry:
    i = 0                                   i0 = 0
    goto loop                               goto loop

loop:                       -- SSA -->  loop:
    if i < 3 goto body
    goto exit


body:
    i = i + 1
    goto loop

exit:
    return i
2026 Fall
CSE411

SSA for loops

entry:                                  entry:
    i = 0                                   i0 = 0
    goto loop                               goto loop

loop:                       -- SSA -->  loop:
    if i < 3 goto body                     i1 = φ(i0, i2)
    goto exit                              if i1 < 3 goto body
                                            goto exit

body:                                   body:
    i = i + 1                              i2 = i1 + 1
    goto loop                               goto loop

exit:                                   exit:
    return i                                return i1
2026 Fall
CSE411

LLVM IR Example

int i;
boolean b;

i = 0;
b = true;
%local.i = alloca i32
%local.b = alloca i1
store i32 0, ptr %local.i  
store i1 true, ptr %local.b
2026 Fall
CSE411

LLVM IR Example

if (a > b) { a = 1; } else { a = 2; } b = b + a;
  %t0 = load i32, ptr %local.a
  %t1 = load i32, ptr %local.b
  %t2 = icmp sgt i32 %t0, %t1
  br i1 %t2, label %if.then.0, label %if.else.1
if.then.0:
  store i32 1, ptr %local.a
  br label %if.end.2
if.else.1:
  store i32 2, ptr %local.a
  br label %if.end.2
if.end.2:
  %t3 = load i32, ptr %local.b
  %t4 = load i32, ptr %local.a
  %t5 = add i32 %t3, %t4
  store i32 %t5, ptr %local.b
2026 Fall
CSE411

LLVM IR Example

int i; int j; i = 0; while (i < 10) { i++; } j = i;
  %local.i = alloca i32
  %local.j = alloca i32
  store i32 0, ptr %local.i
  br label %while.test.0
while.test.0:
  %t0 = load i32, ptr %local.i
  %t1 = icmp slt i32 %t0, 10
  br i1 %t1, label %while.body.1, label %while.end.2
while.body.1:
  %t2 = load i32, ptr %local.i
  %t3 = add i32 %t2, 1
  store i32 %t3, ptr %local.i
  br label %while.test.0
while.end.2:
  %t4 = load i32, ptr %local.i
  store i32 %t4, ptr %local.j
2026 Fall
CSE411

LLVM IR Example

x = inc(x);
  %t0 = load i32, ptr %local.x
  %t1 = call i32 @"ProcedureCall.inc"(i32 %t0)
  store i32 %t1, ptr %local.x
2026 Fall

\begin{tikzpicture}[node distance=1cm and 10cm] \node (c) { $C$ }; \node (cpp) [below of=c] { $C++$ }; \node (rust) [below of=cpp] { $Rust$ }; \node (Julia) [below of=rust] { $Julia$ }; \node (x86) [right=4cm of c] { $x86$ }; \node (arm) [below of=x86] { $ARM$ }; \node (mips) [below of=arm] { $MIPS$ }; \node (alpha) [below of=mips] { $Alpha$ }; \draw [arrow] (c) -- (x86); \draw [arrow] (c) -- (arm); \draw [arrow] (c) -- (mips); \draw [arrow] (c) -- (alpha); \draw [arrow] (cpp) -- (x86); \draw [arrow] (cpp) -- (arm); \draw [arrow] (cpp) -- (mips); \draw [arrow] (cpp) -- (alpha); \draw [arrow] (rust) -- (x86); \draw [arrow] (rust) -- (arm); \draw [arrow] (rust) -- (mips); \draw [arrow] (rust) -- (alpha); \draw [arrow] (Julia) -- (x86); \draw [arrow] (Julia) -- (arm); \draw [arrow] (Julia) -- (mips); \draw [arrow] (Julia) -- (alpha); \end{tikzpicture}

\begin{tikzpicture}[node distance=1cm and 10cm] \node (c) { $C$ }; \node (cpp) [below of=c] { $C++$ }; \node (rust) [below of=cpp] { $Rust$ }; \node (julia) [below of=rust] { $Julia$ }; \node (llvm) [right=2.5cm of c, below of=c] { LLVM IR }; \node (x86) [right=4.5cm of c] { $x86$ }; \node (arm) [below of=x86] { $ARM$ }; \node (mips) [below of=arm] { $MIPS$ }; \node (alpha) [below of=mips] { $Alpha$ }; \draw [arrow] (c) -- (llvm); \draw [arrow] (cpp) -- (llvm); \draw [arrow] (rust) -- (llvm); \draw [arrow] (julia) -- (llvm); \draw [arrow] (llvm) -- (x86); \draw [arrow] (llvm) -- (arm); \draw [arrow] (llvm) -- (mips); \draw [arrow] (llvm) -- (alpha); \end{tikzpicture}

\begin{tikzpicture}[node distance=1cm and 10cm] \node (java) { $Java$ }; \node (kotlin) [below of=java] { $Kotlin$ }; \node (scala) [below of=kotlin] { $Scala$ }; \node (closure) [below of=scala] { $Closure$ }; \node (x86) [right=4cm of java] { $x86$ }; \node (arm) [below of=x86] { $ARM$ }; \node (mips) [below of=arm] { $MIPS$ }; \node (alpha) [below of=mips] { $Alpha$ }; \draw [arrow] (java) -- (x86); \draw [arrow] (java) -- (arm); \draw [arrow] (java) -- (mips); \draw [arrow] (java) -- (alpha); \draw [arrow] (kotlin) -- (x86); \draw [arrow] (kotlin) -- (arm); \draw [arrow] (kotlin) -- (mips); \draw [arrow] (kotlin) -- (alpha); \draw [arrow] (scala) -- (x86); \draw [arrow] (scala) -- (arm); \draw [arrow] (scala) -- (mips); \draw [arrow] (scala) -- (alpha); \draw [arrow] (closure) -- (x86); \draw [arrow] (closure) -- (arm); \draw [arrow] (closure) -- (mips); \draw [arrow] (closure) -- (alpha); \end{tikzpicture}

\begin{tikzpicture}[node distance=1cm and 10cm] \node (c) { $Java$ }; \node (cpp) [below of=c] { $Kotlin$ }; \node (rust) [below of=cpp] { $Scala$ }; \node (julia) [below of=rust] { $Closure$ }; \node (llvm) [right=2.5cm of c, below of=c] { JVM Bytecode }; \node (x86) [right=4.5cm of c] { $x86$ }; \node (arm) [below of=x86] { $ARM$ }; \node (mips) [below of=arm] { $MIPS$ }; \node (alpha) [below of=mips] { $Alpha$ }; \draw [arrow] (c) -- (llvm); \draw [arrow] (cpp) -- (llvm); \draw [arrow] (rust) -- (llvm); \draw [arrow] (julia) -- (llvm); \draw [arrow] (llvm) -- (x86); \draw [arrow] (llvm) -- (arm); \draw [arrow] (llvm) -- (mips); \draw [arrow] (llvm) -- (alpha); \end{tikzpicture}