top | item 45675610

(no title)

mbauman | 4 months ago

Forget compilers, SSA is an immensely valuable readability improvement for humans, too.

discuss

order

MeetingsBrowser|4 months ago

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

James_K|4 months ago

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.

JonChesterfield|4 months ago

Try mem2reg on that to get rid of the loads and stores.