CSE411

CSE411: Introduction to Compilers

Dataflow Analysis

Jooyong Yi (UNIST)

2026 Fall
CSE411

Constant Propagation

    v ∈ use(S)  val(v) = c
 --------------------------------  c is a constant
      S -trans-> S [v ↦ c]
x = 5;
y = x * 2;  -- Optimization --> y = 5 * 2;  -- Optimization --> z = a[10];
z = a[y];                       z = a[y];
2026 Fall
CSE411

How does compilers perform constant propagation?

2026 Fall
CSE411

Which information are we interested in?

x = 5;
y = x * 2;
z = a[y];
2026 Fall
CSE411

Which information are we interested in?

              // Information (Known constant values)
x = 5;        x = 5
y = x * 2;    
z = a[y];     
2026 Fall
CSE411

Which information are we interested in?

              // Information (Known constant values)
x = 5;        x = 5
y = x * 2;    x = 5, y = 10
z = a[y];     
2026 Fall
CSE411

Which information are we interested in?

              // Information (Known constant values)
x = 5;        x = 5
y = x * 2;    x = 5, y = 10
z = a[y];     x = 5, y = 10
2026 Fall
CSE411

Example 2

                     // Information (Known constant values)
x = 5;              x = 5
y = 0;              x = 5, y = 0

if (b) {
    y = x + 1;      
} else {
    y = x + 2;
}

z = x * 2;
w = y * 2;
2026 Fall
CSE411

Example 2

                     // Information (Known constant values)
x = 5;               x = 5
y = 0;               x = 5, y = 0

if (b) {
    y = x + 1;       x = 5, y = 6
} else {
    y = x + 2;       x = 5, y = 7
}

z = x * 2;           x = 5, z = 10
w = y * 2;           x = 5, z = 10
2026 Fall
CSE411

SSA

                     // Information (Known constant values)
x1 = 5;              x1 = 5
y1 = 0;              y1 = 0

if (b) {
    y2 = x1 + 1;     x1 = 5, y2 = 6
} else {
    y3 = x1 + 2;     x1 = 5, y3 = 7
}

y4 = φ(y2, y3);      x1 = 5, y2 = 6, y3 = 7
z1 = x1 * 2;         x1 = 5, y2 = 6, y3 = 7, z1 = 10
w1 = y4 * 2;         x1 = 5, y2 = 6, y3 = 7
2026 Fall
CSE411

How Does the Compiler Track the Information?

                     // Information (Known constant values)
x = 5;               x = 5
y = 0;               x = 5, y = 0

if (b) {
    y = x + 1;       x = 5, y = 6
} else {
    y = x + 2;       x = 5, y = 7
}

z = x * 2;           x = 5, z = 10
w = y * 2;           x = 5, z = 10
2026 Fall
CSE411

CFG (Control-flow Graph)

                     ┌───────────────┐
                     │    x = 5;     │
                     │    y = 0;     │
                     └───────┬───────┘
                             │
                             ▼
                       ┌───────────┐
                       │   if (b)  │
                       └──┬─────┬──┘
                     true │     │ false
                          ▼     ▼
             ┌──────────────┐ ┌──────────────┐
             │ y = x + 1;   │ │ y = x + 2;   │
             └─────────┬────┘ └────┬─────────┘
                       │           │
                       └─────┬─────┘
                             │
                             ▼
                    ┌────────────────┐
                    │  z = x * 2;    │
                    │  w = y * 2;    │
                    └────────────────┘
2026 Fall
CSE411

CFG (Control-flow Graph)

                     ┌───────────────┐
                     │    x = 5;     │
                     │    y = 0;     │
                     └───────┬───────┘
                             │ {x = 5, y = 0}
                             ▼
                       ┌───────────┐
                       │   if (b)  │
                       └──┬─────┬──┘
           {x = 5, y = 0} │     │ {x = 5, y = 0}
                          ▼     ▼
             ┌──────────────┐ ┌──────────────┐
             │ y = x + 1;   │ │ y = x + 2;   │
             └─────────┬────┘ └────┬─────────┘
       {x = 5, y = 6}  │           │ {x = 5, y = 7}
                       └─────┬─────┘
                             │ 
                             ▼
                    ┌────────────────┐ {x = 5}
                    │  z = x * 2;    │
                    │  w = y * 2;    │
                    └────────────────┘ {x = 5, z = 10}
2026 Fall
CSE411

Example 3

                     ┌───────────────┐
                     │    x = 5;     │
                     │    y = 0;     │
                     └───────┬───────┘
                             │
                             ▼
                     ┌───────────────┐
              ┌─────►│   while (b)   │──────────┐
              │      └───────┬───────┘          │
              │              │ true             │ false
              │              ▼                  │
              │      ┌──────────────────┐       │
              │      │   y = y + x;     │       │
              │      └────────┬─────────┘       │
              │               │                 │
              └───────────────┘                 │
                                                │
                       ┌────────────────────────┘
                       │
                       ▼
              ┌───────────────────┐
              │    z = x * 2;     │
              │    w = y * 2;     │
              └───────────────────┘
2026 Fall
CSE411

Example 3

                     ┌───────────────┐
                     │    x = 5;     │
                     │    y = 0;     │
                     └───────┬───────┘
                             │ { x = 5, y = 0 }
                             ▼
                     ┌───────────────┐
              ┌─────►│   while (b)   │──────────┐
              │      └───────┬───────┘          │
              │   {x=5, y=0} │                  │ { x = 5, y = 0 }
              │              ▼                  │
              │      ┌──────────────────┐       │
              │      │   y = y + x;     │       │
              │      └────────┬─────────┘       │
              │   {x=5, y=5}  │                 │
              └───────────────┘                 │
                                                │
                       ┌────────────────────────┘
                       │
                       ▼
              ┌───────────────────┐
              │    z = x * 2;     │
              │    w = y * 2;     │
              └───────────────────┘ { x = 5, y = 0, z = 10, w = 0 }
2026 Fall
CSE411

Example 3

                     ┌───────────────┐
                     │    x = 5;     │
                     │    y = 0;     │
                     └───────┬───────┘
                             │ { x = 5, y = 0 }
                             ▼
                     ┌───────────────┐
              ┌─────►│   while (b)   │──────────┐
              │      └───────┬───────┘          │
              │        {x=5} │                  │ { x = 5 }
              │              ▼                  │
              │      ┌──────────────────┐       │
              │      │   y = y + x;     │       │
              │      └────────┬─────────┘       │
              │        {x=5}  │                 │
              └───────────────┘                 │
                                                │
                       ┌────────────────────────┘
                       │
                       ▼
              ┌───────────────────┐
              │    z = x * 2;     │
              │    w = y * 2;     │
              └───────────────────┘ { x = 5, z = 10 }
2026 Fall
CSE411

Common Subexpression Elimination

          (e, t) ∈ avail(S)           // t holds a reusable value of e before S
    --------------------------------
          S  -trans->  S[e ↦ t]          
for (int i = 0; i < n; i++) {                        for (int i = 0; i < n; i++) {
    a[i] = x * y + i;          -- Optimization -->       t = x * y;
    b[i] = x * y - i;                                    a[i] = t + i;
}                                                        b[i] = t - i;
                                                     }
2026 Fall
CSE411

Common Subexpression Elimination

                     ┌────────────────┐
                     │   t = x + y;   │
                     └────────┬───────┘
                              │ {x+y ↦ t}
                              ▼
                        ┌───────────┐
                        │   if (b)  │
                        └──┬─────┬──┘
             {x+y ↦ t}     │     │    {x+y ↦ t}
                           ▼     ▼
              ┌──────────────┐ ┌──────────────┐
              │ a = x + y;   │ │ c = x + y;   │
              └─────────┬────┘ └────┬─────────┘
                        │           │
    {x+y ↦ t, x+y ↦ a}  │           │ {x+y ↦ t, x+y ↦ c}
                        └─────┬─────┘
                              │ 
                              ▼
                     ┌────────────────┐ {x+y ↦ t}
                     │   z = x + y;   │
                     └────────────────┘{x+y ↦ t, x+y ↦ z}
2026 Fall
CSE411

Common Subexpression Elimination

                     ┌────────────────┐
                     │   t = x + y;   │
                     └────────┬───────┘
                              │ {x+y ↦ t}
                              ▼
                        ┌───────────┐
                        │   if (b)  │
                        └──┬─────┬──┘
             {x+y ↦ t}     │     │    {x+y ↦ t}
                           ▼     ▼
              ┌──────────────┐ ┌──────────────┐
              │ a = x + y;   │ │ c = x + y;   │
              │              │ │ x = 5;       │
              └─────────┬────┘ └────┬─────────┘
                        │           │
    {x+y ↦ t, x+y ↦ a}  │           │ {}
                        └─────┬─────┘
                              │ 
                              ▼
                     ┌────────────────┐ {}
                     │   z = x + y;   │
                     └────────────────┘ {x+y ↦ z}
2026 Fall
CSE411

Can you identify a general pattern?

  • Constant propagation

                         ┌───────────────┐
                         │  l1: x = 5;   │
                         │  l2: y = 0;   │
                         └───────┬───────┘
                                 │ {x = 5, y = 0}
                                 ▼
                           ┌────────────┐
                           │ l3: if (b) │
                           └──┬─────┬───┘
               {x = 5, y = 0} │     │ {x = 5, y = 0}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
                 │ l4: y=x + 1; │ │ l5: y=x + 2; │
                 └─────────┬────┘ └────┬─────────┘
           {x = 5, y = 6}  │           │ {x = 5, y = 7}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {x = 5}
                        │ l6: z=x*2;     │
                        │ l7: w=y*2;     │
                        └────────────────┘ {x = 5, z = 10}
    
  • Common subexpression elimination

                         ┌────────────────┐
                         │ l13: t = x + y;│
                         └────────┬───────┘
                                  │ {x+y ↦ t}
                                  ▼
                            ┌───────────┐
                            │ l14:if (b)│
                            └──┬─────┬──┘
                 {x+y ↦ t}     │     │    {x+y ↦ t}
                               ▼     ▼
                  ┌──────────────┐ ┌──────────────┐
                  │ l15: a=x + y;│ │ l16: c=x + y;│
                  │              │ │ l17: x=5;    │
                  └─────────┬────┘ └────┬─────────┘
                            │           │
        {x+y ↦ t, x+y ↦ a}  │           │ {}
                            └─────┬─────┘
                                  │ 
                                  ▼
                         ┌────────────────┐ {}
                         │ l18: z = x + y;│
                         └────────────────┘ {x+y ↦ z}
    
2026 Fall
CSE411

Can you identify a general pattern?

  • Constant propagation

                         ┌───────────────┐
                         │  l1: x = 5;   │
                         │  l2: y = 0;   │
                         └───────┬───────┘
                                 │ {x = 5, y = 0}
                                 ▼
                           ┌────────────┐
                           │ l3: if (b) │
                           └──┬─────┬───┘
               {x = 5, y = 0} │     │ {x = 5, y = 0}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
                 │ l4: y=x + 1; │ │ l5: y=x + 2; │
                 └─────────┬────┘ └────┬─────────┘
           {x = 5, y = 6}  │           │ {x = 5, y = 7}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {x = 5}
                        │ l6: z=x*2;     │
                        │ l7: w=y*2;     │
                        └────────────────┘ {x = 5, z = 10}
    
  • Common subexpression elimination

                        ┌────────────────┐
                        │ l13: t = x + y;│
                        └────────┬───────┘
                                 │ {x+y ↦ t}
                                 ▼
                           ┌───────────┐
                           │ l14:if (b)│
                           └──┬─────┬──┘
                {x+y ↦ t}     │     │    {x+y ↦ t}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
                 │ l15: a=x + y;│ │ l16: c=x + y;│
                 │              │ │ l17: x=5;    │
                 └─────────┬────┘ └────┬─────────┘
                           │           │
       {x+y ↦ t, x+y ↦ a}  │           │ {}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {}
                        │ l18: z = x + y;│
                        └────────────────┘ {x+y ↦ z}
    
  • OUT[l4] =

2026 Fall
CSE411

Can you identify a general pattern?

  • Constant propagation

                         ┌───────────────┐
                         │  l1: x = 5;   │
                         │  l2: y = 0;   │
                         └───────┬───────┘
                                 │ {x = 5, y = 0}
                                 ▼
                           ┌────────────┐
                           │ l3: if (b) │
                           └──┬─────┬───┘
               {x = 5, y = 0} │     │ {x = 5, y = 0}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
                 │ l4: y=x + 1; │ │ l5: y=x + 2; │
                 └─────────┬────┘ └────┬─────────┘
           {x = 5, y = 6}  │           │ {x = 5, y = 7}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {x = 5}
                        │ l6: z=x*2;     │
                        │ l7: w=y*2;     │
                        └────────────────┘ {x = 5, z = 10}
    
  • Common subexpression elimination

                         ┌────────────────┐
                         │ l13: t = x + y;│
                         └────────┬───────┘
                                  │ {x+y ↦ t}
                                  ▼
                            ┌───────────┐
                            │ l14:if (b)│
                            └──┬─────┬──┘
                 {x+y ↦ t}     │     │    {x+y ↦ t}
                               ▼     ▼
                  ┌──────────────┐ ┌──────────────┐
                  │ l15: a=x + y;│ │ l16: c=x + y;│
                  │              │ │ l17: x=5;    │
                  └─────────┬────┘ └────┬─────────┘
                            │           │
        {x+y ↦ t, x+y ↦ a}  │           │ {}
                            └─────┬─────┘
                                  │ 
                                  ▼
                         ┌────────────────┐ {}
                         │ l18: z = x + y;│
                         └────────────────┘ {x+y ↦ z}
    
  • OUT[l4] = ({x = 5, y = 0} - {y = 0}) ∪ {y = 6}

2026 Fall
CSE411

Can you identify a general pattern?

  • Constant propagation

                         ┌───────────────┐
                         │  l1: x = 5;   │
                         │  l2: y = 0;   │
                         └───────┬───────┘
                                 │ {x = 5, y = 0}
                                 ▼
                           ┌────────────┐
                           │ l3: if (b) │
                           └──┬─────┬───┘
               {x = 5, y = 0} │     │ {x = 5, y = 0}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
                 │ l4: y=x + 1; │ │ l5: y=x + 2; │
                 └─────────┬────┘ └────┬─────────┘
           {x = 5, y = 6}  │           │ {x = 5, y = 7}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {x = 5}
                        │ l6: z=x*2;     │
                        │ l7: w=y*2;     │
                        └────────────────┘ {x = 5, z = 10}
    
  • Common subexpression elimination

                         ┌────────────────┐
                         │ l13: t = x + y;│
                         └────────┬───────┘
                                  │ {x+y ↦ t}
                                  ▼
                            ┌───────────┐
                            │ l14:if (b)│
                            └──┬─────┬──┘
                 {x+y ↦ t}     │     │    {x+y ↦ t}
                               ▼     ▼
                  ┌──────────────┐ ┌──────────────┐
                  │ l15: a=x + y;│ │ l16: c=x + y;│
                  │              │ │ l17: x=5;    │
                  └─────────┬────┘ └────┬─────────┘
                            │           │
        {x+y ↦ t, x+y ↦ a}  │           │ {}
                            └─────┬─────┘
                                  │ 
                                  ▼
                         ┌────────────────┐ {}
                         │ l18: z = x + y;│
                         └────────────────┘ {x+y ↦ z}
    
  • OUT[l4] = ({x = 5, y = 0} - {y = 0}) ∪ {y = 6}

  • OUT[l17]= ({...} - {...}) ∪ {...}

2026 Fall
CSE411

Can you identify a general pattern?

  • Constant propagation

                         ┌───────────────┐
                         │  l1: x = 5;   │
                         │  l2: y = 0;   │
                         └───────┬───────┘
                                 │ {x = 5, y = 0}
                                 ▼
                           ┌────────────┐
                           │ l3: if (b) │
                           └──┬─────┬───┘
               {x = 5, y = 0} │     │ {x = 5, y = 0}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
                 │ l4: y=x + 1; │ │ l5: y=x + 2; │
                 └─────────┬────┘ └────┬─────────┘
           {x = 5, y = 6}  │           │ {x = 5, y = 7}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {x = 5}
                        │ l6: z=x*2;     │
                        │ l7: w=y*2;     │
                        └────────────────┘ {x = 5, z = 10}
    
  • Common subexpression elimination

                         ┌────────────────┐
                         │ l13: t = x + y;│
                         └────────┬───────┘
                                  │ {x+y ↦ t}
                                  ▼
                            ┌───────────┐
                            │ l14:if (b)│
                            └──┬─────┬──┘
                 {x+y ↦ t}     │     │    {x+y ↦ t}
                               ▼     ▼
                  ┌──────────────┐ ┌──────────────┐
                  │ l15: a=x + y;│ │ l16: c=x + y;│
                  │              │ │ l17: x=5;    │
                  └─────────┬────┘ └────┬─────────┘
                            │           │
        {x+y ↦ t, x+y ↦ a}  │           │ {}
                            └─────┬─────┘
                                  │ 
                                  ▼
                         ┌────────────────┐ {}
                         │ l18: z = x + y;│
                         └────────────────┘ {x+y ↦ z}
    
  • OUT[l4] = ({x = 5, y = 0} - {y = 0}) ∪ {y = 6}

  • OUT[l17]= ({x+y ↦ t, x+y ↦ c} - {x+y ↦ t, x+y ↦ c}) ∪ {}

2026 Fall
CSE411

Can you identify a general pattern?

  • Constant propagation

                         ┌───────────────┐
                         │  l1: x = 5;   │
                         │  l2: y = 0;   │
                         └───────┬───────┘
                                 │ {x = 5, y = 0}
                                 ▼
                           ┌────────────┐
                           │ l3: if (b) │
                           └──┬─────┬───┘
               {x = 5, y = 0} │     │ {x = 5, y = 0}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
                 │ l4: y=x + 1; │ │ l5: y=x + 2; │
                 └─────────┬────┘ └────┬─────────┘
           {x = 5, y = 6}  │           │ {x = 5, y = 7}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {x = 5}
                        │ l6: z=x*2;     │
                        │ l7: w=y*2;     │
                        └────────────────┘ {x = 5, z = 10}
    
  • Common subexpression elimination

                         ┌────────────────┐
                         │ l13: t = x + y;│
                         └────────┬───────┘
                                  │ {x+y ↦ t}
                                  ▼
                            ┌───────────┐
                            │ l14:if (b)│
                            └──┬─────┬──┘
                 {x+y ↦ t}     │     │    {x+y ↦ t}
                               ▼     ▼
                  ┌──────────────┐ ┌──────────────┐
                  │ l15: a=x + y;│ │ l16: c=x + y;│
                  │              │ │ l17: x=5;    │
                  └─────────┬────┘ └────┬─────────┘
                            │           │
        {x+y ↦ t, x+y ↦ a}  │           │ {}
                            └─────┬─────┘
                                  │ 
                                  ▼
                         ┌────────────────┐ {}
                         │ l18: z = x + y;│
                         └────────────────┘ {x+y ↦ z}
    
  • OUT[l4] = ({x = 5, y = 0} - {y = 0}) ∪ {y = 6}

  • OUT[l17]= ({x+y ↦ t, x+y ↦ c} - {x+y ↦ t, x+y ↦ c}) ∪ {}

  • OUT[l] = (IN[l] - KILL[l]) ∪ GEN[l, IN[l]]

2026 Fall
CSE411

Can you identify a general pattern?

  • Constant propagation

                         ┌───────────────┐
                      B1 │  l1: x = 5;   │
                         │  l2: y = 0;   │
                         └───────┬───────┘
                                 │ {x = 5, y = 0}
                                 ▼
                           ┌────────────┐
                         B2│ l3: if (b) │
                           └──┬─────┬───┘
               {x = 5, y = 0} │     │ {x = 5, y = 0}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
              B3 │ l4: y=x + 1; │ │ l5: y=x + 2; │ B4
                 └─────────┬────┘ └────┬─────────┘
           {x = 5, y = 6}  │           │ {x = 5, y = 7}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {x = 5}
                     B5 │ l6: z=x*2;     │
                        │ l7: w=y*2;     │
                        └────────────────┘ {x = 5, z = 10}
    
  • Common subexpression elimination

                         ┌────────────────┐
                      B6 │ l13: t = x + y;│
                         └────────┬───────┘
                                  │ {x+y ↦ t}
                                  ▼
                            ┌───────────┐
                         B7 │ l14:if (b)│
                            └──┬─────┬──┘
                 {x+y ↦ t}     │     │    {x+y ↦ t}
                               ▼     ▼
                  ┌──────────────┐ ┌──────────────┐
               B8 │ l15: a=x + y;│ │ l16: c=x + y;│ B9
                  │              │ │ l17: x=5;    │
                  └─────────┬────┘ └────┬─────────┘
                            │           │
        {x+y ↦ t, x+y ↦ a}  │           │ {}
                            └─────┬─────┘
                                  │ 
                                  ▼
                         ┌────────────────┐ {}
                     B10 │ l18: z = x + y;│
                         └────────────────┘ {x+y ↦ z}
    
  • IN[B5] =

2026 Fall
CSE411

Can you identify a general pattern?

  • Constant propagation

                         ┌───────────────┐
                      B1 │  l1: x = 5;   │
                         │  l2: y = 0;   │
                         └───────┬───────┘
                                 │ {x = 5, y = 0}
                                 ▼
                           ┌────────────┐
                         B2│ l3: if (b) │
                           └──┬─────┬───┘
               {x = 5, y = 0} │     │ {x = 5, y = 0}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
              B3 │ l4: y=x + 1; │ │ l5: y=x + 2; │ B4
                 └─────────┬────┘ └────┬─────────┘
           {x = 5, y = 6}  │           │ {x = 5, y = 7}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {x = 5}
                     B5 │ l6: z=x*2;     │
                        │ l7: w=y*2;     │
                        └────────────────┘ {x = 5, z = 10}
    
  • Common subexpression elimination

                         ┌────────────────┐
                      B6 │ l13: t = x + y;│
                         └────────┬───────┘
                                  │ {x+y ↦ t}
                                  ▼
                            ┌───────────┐
                         B7 │ l14:if (b)│
                            └──┬─────┬──┘
                 {x+y ↦ t}     │     │    {x+y ↦ t}
                               ▼     ▼
                  ┌──────────────┐ ┌──────────────┐
               B8 │ l15: a=x + y;│ │ l16: c=x + y;│ B9
                  │              │ │ l17: x=5;    │
                  └─────────┬────┘ └────┬─────────┘
                            │           │
        {x+y ↦ t, x+y ↦ a}  │           │ {}
                            └─────┬─────┘
                                  │ 
                                  ▼
                         ┌────────────────┐ {}
                     B10 │ l18: z = x + y;│
                         └────────────────┘ {x+y ↦ z}
    
  • IN[B5] = {x = 5, y = 6} ∩ {x = 5, y = 7}

2026 Fall
CSE411

Can you identify a general pattern?

  • Constant propagation

                         ┌───────────────┐
                      B1 │  l1: x = 5;   │
                         │  l2: y = 0;   │
                         └───────┬───────┘
                                 │ {x = 5, y = 0}
                                 ▼
                           ┌────────────┐
                         B2│ l3: if (b) │
                           └──┬─────┬───┘
               {x = 5, y = 0} │     │ {x = 5, y = 0}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
              B3 │ l4: y=x + 1; │ │ l5: y=x + 2; │ B4
                 └─────────┬────┘ └────┬─────────┘
           {x = 5, y = 6}  │           │ {x = 5, y = 7}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {x = 5}
                     B5 │ l6: z=x*2;     │
                        │ l7: w=y*2;     │
                        └────────────────┘ {x = 5, z = 10}
    
  • Common subexpression elimination

                         ┌────────────────┐
                      B6 │ l13: t = x + y;│
                         └────────┬───────┘
                                  │ {x+y ↦ t}
                                  ▼
                            ┌───────────┐
                         B7 │ l14:if (b)│
                            └──┬─────┬──┘
                 {x+y ↦ t}     │     │    {x+y ↦ t}
                               ▼     ▼
                  ┌──────────────┐ ┌──────────────┐
               B8 │ l15: a=x + y;│ │ l16: c=x + y;│ B9
                  │              │ │ l17: x=5;    │
                  └─────────┬────┘ └────┬─────────┘
                            │           │
        {x+y ↦ t, x+y ↦ a}  │           │ {}
                            └─────┬─────┘
                                  │ 
                                  ▼
                         ┌────────────────┐ {}
                     B10 │ l18: z = x + y;│
                         └────────────────┘ {x+y ↦ z}
    
  • IN[B5] = {x = 5, y = 6} ∩ {x = 5, y = 7}

  • IN[B10]= {x+y ↦ t, x+y ↦ a} ∩ {}

2026 Fall
CSE411

Can you identify a general pattern?

  • Constant propagation

                         ┌───────────────┐
                      B1 │  l1: x = 5;   │
                         │  l2: y = 0;   │
                         └───────┬───────┘
                                 │ {x = 5, y = 0}
                                 ▼
                           ┌────────────┐
                         B2│ l3: if (b) │
                           └──┬─────┬───┘
               {x = 5, y = 0} │     │ {x = 5, y = 0}
                              ▼     ▼
                 ┌──────────────┐ ┌──────────────┐
              B3 │ l4: y=x + 1; │ │ l5: y=x + 2; │ B4
                 └─────────┬────┘ └────┬─────────┘
           {x = 5, y = 6}  │           │ {x = 5, y = 7}
                           └─────┬─────┘
                                 │ 
                                 ▼
                        ┌────────────────┐ {x = 5}
                     B5 │ l6: z=x*2;     │
                        │ l7: w=y*2;     │
                        └────────────────┘ {x = 5, z = 10}
    
  • Common subexpression elimination

                         ┌────────────────┐
                      B6 │ l13: t = x + y;│
                         └────────┬───────┘
                                  │ {x+y ↦ t}
                                  ▼
                            ┌───────────┐
                         B7 │ l14:if (b)│
                            └──┬─────┬──┘
                 {x+y ↦ t}     │     │    {x+y ↦ t}
                               ▼     ▼
                  ┌──────────────┐ ┌──────────────┐
               B8 │ l15: a=x + y;│ │ l16: c=x + y;│ B9
                  │              │ │ l17: x=5;    │
                  └─────────┬────┘ └────┬─────────┘
                            │           │
        {x+y ↦ t, x+y ↦ a}  │           │ {}
                            └─────┬─────┘
                                  │ 
                                  ▼
                         ┌────────────────┐ {}
                     B10 │ l18: z = x + y;│
                         └────────────────┘ {x+y ↦ z}
    
  • IN[B5] = {x = 5, y = 6} ∩ {x = 5, y = 7}

  • IN[B10]= {x+y ↦ t, x+y ↦ a} ∩ {}

  • IN[B] = OUT[P]

2026 Fall
CSE411

Dataflow Analysis

  • A general framework for statically collecting information about a program.
  • The collected information can be used for code optimization.
  • General formula:
    • IN[B] = OUT[P]
    • OUT[B] = (IN[B])
      • e.g., OUT[l] = (IN[l] - KILL[l]) ∪ GEN[l, IN[l]]
2026 Fall
CSE411

Fixed-Point Computation

                     ┌───────────────┐
                     │    x = 5;     │
                     │    y = 0;     │
                     └───────┬───────┘
                             │
                             ▼
                     ┌───────────────┐
              ┌─────►│   while (b)   │──────────┐
              │      └───────┬───────┘          │
              │              │ true             │ false
              │              ▼                  │
              │      ┌──────────────────┐       │
              │      │   y = y + x;     │       │
              │      └────────┬─────────┘       │
              │               │                 │
              └───────────────┘                 │
                                                │
                       ┌────────────────────────┘
                       │
                       ▼
              ┌───────────────────┐
              │    z = x * 2;     │
              │    w = y * 2;     │
              └───────────────────┘
  • Dataflow analysis propagates information along the CFG.
  • Fixed-point computation: Repeatedly update dataflow information until no information changes.
    • IN[B] = OUT[P]
    • OUT[B] = (IN[B])
  • Fixed point: A state where all dataflow equations are satisfied.
    • and for every block .
  • Is fixed-point computation guaranteed to terminate?
2026 Fall
CSE411

Soundness

  • ⊢ (int x; int y; y = x;): unit ⇒ ⊧ (int x; int y; y = x;): unit
  • The compiler says the given program is type-safe. Is the program really type-safe?
2026 Fall
CSE411

Soundness for constant propagation

2026 Fall