CSE411

CSE411: Introduction to Compilers

Dataflow Analysis 2

Jooyong Yi (UNIST)

2026 Fall
CSE411

Dataflow Analysis

  • 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}
    
  • IN[B] = OUT[P]

  • OUT[B] = (IN[B])

2026 Fall
CSE411

Dead-Store Elimination

int x;                       int x;
x = 10;             --->     
x = 20;                      x = 20;
printf("%d", x);             printf("%d", x);
2026 Fall
CSE411

Dead-Store Elimination

                  ┌───────────────┐
                  │  l1: x = 5;   │
                  └───────┬───────┘
                          │
                          ▼
                    ┌────────────┐
                    │ l2: if (b) │
                    └──┬─────┬───┘
                 true  │     │ false
                       ▼     ▼
          ┌──────────────┐ ┌──────────────┐
          │ l3: x = 10;  │ │ l4: x = 20;  │
          └─────────┬────┘ └────┬─────────┘
                    │           │
                    └─────┬─────┘
                          │
                          ▼
                  ┌────────────────┐
                  │ l5: y = x + 1; │
                  └────────────────┘
  • What condition must be satisfied to eliminate a dead store?
2026 Fall
CSE411

Definition and Use

  • l1: x = 5
    • We say that variable x is defined at l1.
  • l2: if (x > 0)
    • We say that variable x is used at l2.
  • l3: x = x + 1
    • Variable x is used and redefined at l3.
2026 Fall
CSE411

Reaching Definition

l1: x = 5;
l2: z = 10;
l3: x = x + 1;
l4: y = x + z;
  • The definition of variable x at location l1 reaches location l3.
  • We say that l1 is the reaching definition of x at l3.
  • However, l1 is not the reaching definition of x at l4.
    • Instead, l4 is the reaching definition of x at l4.
2026 Fall
CSE411

Dead-Store Elimination

                  ┌───────────────┐
                  │  l1: x = 5;   │
                  └───────┬───────┘
                          │
                          ▼
                    ┌────────────┐
                    │ l2: if (b) │
                    └──┬─────┬───┘
                 true  │     │ false
                       ▼     ▼
          ┌──────────────┐ ┌──────────────┐
          │ l3: x = 10;  │ │ l4: x = 20;  │
          └─────────┬────┘ └────┬─────────┘
                    │           │
                    └─────┬─────┘
                          │
                          ▼
                  ┌────────────────┐
                  │ l5: y = x + 1; │
                  └────────────────┘
  • What condition must be satisfied to eliminate a dead store for variable x?
2026 Fall
CSE411

Dead-Store Elimination

                  ┌───────────────┐
                  │  l1: x = 5;   │
                  └───────┬───────┘
                          │
                          ▼
                    ┌────────────┐
                    │ l2: if (b) │
                    └──┬─────┬───┘
                 true  │     │ false
                       ▼     ▼
          ┌──────────────┐ ┌──────────────┐
          │ l3: x = 10;  │ │ l4: x = 20;  │
          └─────────┬────┘ └────┬─────────┘
                    │           │
                    └─────┬─────┘
                          │
                          ▼
                  ┌────────────────┐
                  │ l5: y = x + 1; │
                  └────────────────┘
  • What condition must be satisfied to eliminate a dead store for variable x?
  • Uses(x) =
2026 Fall
CSE411

Dead-Store Elimination

                  ┌───────────────┐
                  │  l1: x = 5;   │
                  └───────┬───────┘
                          │
                          ▼
                    ┌────────────┐
                    │ l2: if (b) │
                    └──┬─────┬───┘
                 true  │     │ false
                       ▼     ▼
          ┌──────────────┐ ┌──────────────┐
          │ l3: x = 10;  │ │ l4: x = 20;  │
          └─────────┬────┘ └────┬─────────┘
                    │           │
                    └─────┬─────┘
                          │
                          ▼
                  ┌────────────────┐
                  │ l5: y = x + 1; │
                  └────────────────┘
  • What condition must be satisfied to eliminate a dead store for variable x?
  • Uses(x) = {l5}, Uses(b) = {l2}
2026 Fall
CSE411

Dead-Store Elimination

                  ┌───────────────┐
                  │  l1: x = 5;   │
                  └───────┬───────┘
                          │
                          ▼
                    ┌────────────┐
                    │ l2: if (b) │
                    └──┬─────┬───┘
                 true  │     │ false
                       ▼     ▼
          ┌──────────────┐ ┌──────────────┐
          │ l3: x = 10;  │ │ l4: x = 20;  │
          └─────────┬────┘ └────┬─────────┘
                    │           │
                    └─────┬─────┘
                          │
                          ▼
                  ┌────────────────┐
                  │ l5: y = x + 1; │
                  └────────────────┘
  • What condition must be satisfied to eliminate a dead store for variable x?
  • Uses(x) = {l5}, Uses(b) = {l2}
  • RD(l5) =
2026 Fall
CSE411

Dead-Store Elimination

                  ┌───────────────┐
                  │  l1: x = 5;   │
                  └───────┬───────┘
                          │
                          ▼
                    ┌────────────┐
                    │ l2: if (b) │
                    └──┬─────┬───┘
                 true  │     │ false
                       ▼     ▼
          ┌──────────────┐ ┌──────────────┐
          │ l3: x = 10;  │ │ l4: x = 20;  │
          └─────────┬────┘ └────┬─────────┘
                    │           │
                    └─────┬─────┘
                          │
                          ▼
                  ┌────────────────┐
                  │ l5: y = x + 1; │
                  └────────────────┘
  • What condition must be satisfied to eliminate a dead store for variable x?
  • Uses(x) = {l5}, Uses(b) = {l2}
  • RD(l5) = {l3, l4}
    • l1 can be safely eliminated.
2026 Fall
CSE411

DFA for Reaching Definitions

  • Information of interest: Set of definitions that may reach each program point

  • Transfer function

    • OUT[B] = (IN[B] - KILL[B]) ∪ GEN[B]
      • GEN[B]: Definitions generated in B that reach the exit of B.
      • KILL[B]: Definitions invalidated by assignments in B.
  • Join function

    • IN[B] =
2026 Fall
CSE411

DFA for Reaching Definitions

  • Information of interest: Set of definitions that may reach each program point

  • Transfer function

    • OUT[B] = (IN[B] - KILL[B]) ∪ GEN[B]
      • GEN[B]: Definitions generated in B that reach the exit of B.
      • KILL[B]: Definitions invalidated by assignments in B.
  • Join function

    • IN[B] =
2026 Fall
CSE411

May and Must Analyses

  • 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}
    
    • IN[B] = OUT[P]
    • OUT[B] = (IN[B])
  • Reaching Definitions

    
                      ┌───────────────┐
                      │  l1: x = 5;   │
                      └───────┬───────┘
                              │ {l1}
                              ▼
                        ┌────────────┐
                        │ l2: if (b) │
                        └──┬─────┬───┘
                     {l1}  │     │ {l1}
                           ▼     ▼
              ┌──────────────┐ ┌──────────────┐
              │ l3: x = 10;  │ │ l4: x = 20;  │
          {l3}└─────────┬────┘ └────┬─────────┘ {l4}
                        │           │
                        └─────┬─────┘
                              │
                              ▼ 
                      ┌────────────────┐ {l3, l4}
                      │ l5: y = x + 1; │
                      └────────────────┘ {l3, l4, l5}                    
    
    • IN[B] = OUT[P]
    • OUT[B] = (IN[B])
2026 Fall
CSE411

Forward and Backward Analyses

  • Forward Analysis

    • OUT[B] = (IN[B])
    • IN[B] =
  • Backward Analysis

    • IN[B] = (OUT[B])
    • OUT[B] =
2026 Fall
CSE411

Live Variables

  • A variable x is live at program point p if there exists a path from p to a use of x along which x is not redefined.
l1: x1 = 5;       
                  {x1}
l2: y1 = 10;      
                  {x1, y1}
l3: z1 = x1 + 1;  
                  {y1} 
l4: x2 = 20;      
                  {y1}
l5: w1 = y1 + 1;
                  {}
2026 Fall
CSE411

Live Variables

                  ┌────────────────┐ {b}
                  │  l1: x1 = 5;   │
                  └───────┬────────┘ {x1, b}
                          │ 
                          ▼
                    ┌────────────┐ {x1, b}
                    │ l2: if (b) │
                    └──┬─────┬───┘ {x1}
                       │     │ 
                       ▼     ▼
    {x1}  ┌────────────────┐ ┌────────────────┐ {}
          │ l3: y1=x1 + 1; │ │ l4: y1 = 10;   │
    {y1}  └─────────┬──────┘ └──────┬─────────┘ {y1}                                           
                    │               │ 
                    └──────┬────────┘
                           │
                           ▼ 
                  ┌────────────────┐ {y1}
                  │ l5: z1=y1 * 2; │
                  └────────────────┘ {}
                            
2026 Fall
CSE411

DFA for Live Variables

  • Information of interest: Set of live variables

  • Transfer function

    • IN[B] = (OUT[B] - DEF[B]) ∪ USE[B]
      • DEF[B]: Variables defined in B.
      • USE[B]: Variables used in B before being defined in B.
  • Join function

    • OUT[B] =
2026 Fall
CSE411

Classification of Data-Flow Analyses

Forward Backward
may Reaching Definitions Live Variables
must Constant Propagation
2026 Fall
CSE411

Classification of Data-Flow Analyses

Forward Backward
may Reaching Definitions Live Variables
must Constant Propagation Very Busy Expression
2026 Fall
CSE411

Very Busy Expressions

  • An expression e is very busy at program point p if e is evaluated on every path from p to program exit without any of its operands being redefined.
                    ┌────────────┐ {a+b}
                    │ l1: if (c) │
                    └──┬─────┬───┘ {a+b}
                       │     │
                       ▼     ▼
    {a+b} ┌────────────────┐ ┌────────────────┐ {a+b}
          │ l2: x = a + b; │ │ l3: y = a + b; │ 
          └─────────┬──────┘ └──────┬─────────┘
                    │               │ 
                    └──────┬────────┘
                           ▼
                  ┌────────────────┐
                  │    l4: ...     │
                  └────────────────┘
2026 Fall
CSE411

DFA for Very Busy Expressions

  • Information of interest: Set of expressions that are very busy at each program point

  • Transfer function

    • IN[B] = (OUT[B] - KILL[B]) ∪ GEN[B]
      • GEN[B]: Expressions evaluated in B before any of their operands are redefined.
      • KILL[B]: Expressions whose operands are redefined in B.
  • Join function

    • OUT[B] =
2026 Fall
CSE411

Code Hoisting

if (c) {                  t = a + b;
    x = a + b;            if (c) {
} else {          ---->       x = t;
    y = a + b;            } else {
}                             y = t;
                          }
2026 Fall