←back to thread

Why SSA Compilers?

(mcyoung.xyz)
182 points transpute | 3 comments | | HN request time: 0.432s | source
Show context
mbauman ◴[] No.45675610[source]
Forget compilers, SSA is an immensely valuable readability improvement for humans, too.
replies(1): >>45678241 #
1. MeetingsBrowser ◴[] No.45678241[source]
Why have

    while (c < 10) { c *= 3; }
when you could have

  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %3, align 4
  br label %4, !dbg !18

  4:
  %5 = load i32, ptr %3, align 4, !dbg !19
  %6 = icmp slt i32 %5, 10, !dbg !20
  br i1 %6, label %7, label %10, !dbg !18

  7:
  %8 = load i32, ptr %3, align 4, !dbg !21
  %9 = mul nsw i32 %8, 3, !dbg !21
  store i32 %9, ptr %3, align 4, !dbg !21
  br label %4, !dbg !18
replies(2): >>45680346 #>>45680725 #
2. JonChesterfield ◴[] No.45680346[source]
Try mem2reg on that to get rid of the loads and stores.
3. James_K ◴[] No.45680725[source]
The second code snippet doesn't use SSA. It just translates the first loop into IR and mangles the variable names. Here is an SSA version of that in the Scheme language.

  (let loop ((c c)) (if (< c 10) (loop (* c 3)) c))
Notice that this is stateless and also returns the final value of “c” from the loop. People who use the below style have tended to find that it is much easier to reason about for more complicated looping structures.