CSE411

CSE411: Introduction to Compilers

Code Optimization

Jooyong Yi (UNIST)

2026 Fall
CSE411

What is code optimization?

  • Code optimization is a form of program transformation
    • with objectives such as
      • improving execution speed
      • reducing resource consumption (e.g., memory, energy, code size)
    • subject to the constraint of
      • preserving the semantics of the original program.
2026 Fall
CSE411

Optimization Examples

2026 Fall
CSE411

Example 1

x = (2+3) * y; -- Optimization --> 
2026 Fall
CSE411

Example 1

x = (2+3) * y; -- Optimization --> x = 5 * y;
2026 Fall
CSE411

Example 1: Constant Folding

  • If operands are known at compile type, perform the operation statically.
x = (2+3) * y; -- Optimization --> x = 5 * y;
2026 Fall
CSE411

Constant Folding

    e = c1 ⊙ c2     c1 ⊙ c2 = c3
 --------------------------------  c1, c2, and c3 are constants
      S -trans-> S[e ↦ c3]
x = (2+3) * y; -- Optimization --> x = 5 * y;
2026 Fall
CSE411

Example 2

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

Example 2

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

Example 2

x = 5;
y = x * 2;  -- Optimization --> y = 5 * 2;  -- Optimization --> z = a[10];
z = a[y];                       z = a[y];
2026 Fall
CSE411

Example 2: 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

Example 3

for (int i = 0; i < n; i++) {
    a[i] = x * y + i;
    b[i] = x * y - i;
}
2026 Fall
CSE411

Example 3

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

Example 3: 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

Example 4

for (i = 0; i < n; i++) {
    t = x * y;             -- Optimization -->
    a[i] = t + i;
}
2026 Fall
CSE411

Example 4

for (i = 0; i < n; i++) {                       t = x * y;
    t = x * y;             -- Optimization -->  for (i = 0; i < n; i++) {
    a[i] = t + i;                                   a[i] = t + i;
}                                               }
2026 Fall
CSE411

Example 4: Loop-Invariant Code Motion

for (i = 0; i < n; i++) {                       t = x * y;
    t = x * y;             -- Optimization -->  for (i = 0; i < n; i++) {
    a[i] = t + i;                                   a[i] = t + i;
}                                               }
2026 Fall
CSE411

Example 4: Loop-Invariant Code Motion

              e ∈ invariant(L)  safe(e, L)
 -----------------------------------------------------------
      L: while(C) {t = e; S} -trans-> t = e; while(C) {S}
for (i = 0; i < n; i++) {                       t = x * y;
    t = x * y;             -- Optimization -->  for (i = 0; i < n; i++) {
    a[i] = t + i;                                   a[i] = t + i;
}                                               }
2026 Fall
CSE411

Example 4: Loop-Invariant Code Motion

              e ∈ invariant(L)  safe(e, L)
 -----------------------------------------------------------
      L: while(C) {t = e; S} -trans-> t = e; while(C) {S}
for (i = 0; i < n; i++) {                       t = x / y; // what if y can be 0?
    t = x / y;             -- Optimization -->  for (i = 0; i < n; i++) {
    a[i] = t + i;                                   a[i] = t + i;
}                                               }
2026 Fall
CSE411

Example 5

for (i = 0; i < n; i++) {
    a[i] = b[i] + 1;
}

for (i = 0; i < n; i++) {
    c[i] = a[i] * 2;
}
2026 Fall
CSE411

Example 5

for (i = 0; i < n; i++) {               for (i = 0; i < n; i++) {
    a[i] = b[i] + 1;                       a[i] = b[i] + 1;
}                                          c[i] = a[i] * 2;
                               --->     }
for (i = 0; i < n; i++) {
    c[i] = a[i] * 2;
}
2026 Fall
CSE411

Example 5: Loop Fusion

    compatible(L1, L2)   safeToFuse(L1, L2)
 ---------------------------------------------
      L1; L2  -trans-> fuse(L1, L2)
for (i = 0; i < n; i++) {               for (i = 0; i < n; i++) {
    a[i] = b[i] + 1;                       a[i] = b[i] + 1;
}                                          c[i] = a[i] * 2;
                               --->     }
for (i = 0; i < n; i++) {
    c[i] = a[i] * 2;
}
2026 Fall
CSE411

Example 6

for (int i = 0; i < n; i++) {
    a[i] = b[i] * 2;
    c[i] = a[i] + 1;
}
2026 Fall
CSE411

Example 6

for (int i = 0; i < n; i++) {          for (int i = 0; i < n; i++) {
    a[i] = b[i] * 2;            --->       int t = b[i] * 2;
    c[i] = a[i] + 1;                       a[i] = t;
}                                          c[i] = t + 1;
                                       }
2026 Fall
CSE411

Example 6: Scalar Replacement

    sameLocation(m1, m2)    noInterveningWrite(m1, m2)
-------------------------------------------------------
       m1 = e;   -trans->  t = e; 
       x = m2;             m1 = t;
                           x = t;
for (int i = 0; i < n; i++) {          for (int i = 0; i < n; i++) {
    a[i] = b[i] * 2;            --->       int t = b[i] * 2;
    c[i] = a[i] + 1;                       a[i] = t;
}                                          c[i] = t + 1;
                                       }
2026 Fall
CSE411

Example 7

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

Example 7: Dead-Store Elimination

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

Example 8

int a[1000];
int b[1000];

for (int i = 0; i < 1000; i++) {
    b[i] = a[i] * 2;
}

for (int i = 0; i < 1000; i++) {
    a[i] = b[i] + 1;
}
2026 Fall
CSE411

Example 8

int a[1000];                                            int a[1000];
int b[1000];                                            int b[1000];               

for (int i = 0; i < 1000; i++) {                        for (int i = 0; i < 1000; i++) {
    b[i] = a[i] * 2;                                        b[i] = a[i] * 2; 
}                                  -- Loop Fusion -->       a[i] = b[i] + 1;
                                                        }
for (int i = 0; i < 1000; i++) {
    a[i] = b[i] + 1;
}
2026 Fall
CSE411

Example 8

int a[1000];                     
int b[1000];                     

for (int i = 0; i < 1000; i++) {  --->
    b[i] = a[i] * 2;             
    a[i] = b[i] + 1;             
}                                
                                 
2026 Fall
CSE411

Example 8

int a[1000];                                    int a[1000];
int b[1000];                                    int b[1000];

for (int i = 0; i < 1000; i++) {  -- S.R. -->   for (int i = 0; i < 1000; i++) {           
    b[i] = a[i] * 2;                                int t = a[i] * 2;
    a[i] = b[i] + 1;                                b[i] = t;
}                                                   a[i] = t + 1;
                                                }
2026 Fall
CSE411

Example 8

int a[1000];
int b[1000];

for (int i = 0; i < 1000; i++) {  --->
    int t = a[i] * 2;
    b[i] = t;
    a[i] = t + 1;
}    
2026 Fall
CSE411

Example 8

int a[1000];                                 int a[1000];
int b[1000];                                 int b[1000];

for (int i = 0; i < 1000; i++) {  --D.E-->   for (int i = 0; i < 1000; i++) {
    int t = a[i] * 2;                            int t = a[i] * 2;
    b[i] = t;
    a[i] = t + 1;                                a[i] = t + 1;
}                                            }
2026 Fall
CSE411

Example 8

int a[1000];                                                    int a[1000];
int b[1000];
                                    Dead-Object Elimination
for (int i = 0; i < 1000; i++) {  -------------------------->   for (int i = 0; i < 1000; i++) {
    int t = a[i] * 2;                                               int t = a[i] * 2;
    a[i] = t + 1;                                                   a[i] = t + 1;
}                                                               }
2026 Fall
CSE411

Optimization Passes

       |--------------|         |--------------|         |--------------|
IR1 →  | Optimization | → IR2 → | Optimization | → IR3 → | Optimization | → ... 
       |    Pass 1    |         |    Pass 2    |         |    Pass 3    |
       |--------------|         |--------------|         |--------------|
2026 Fall