# Programs from "Global Code Motion Global Value Numbering" by Cliff Click
# https://courses.cs.washington.edu/courses/cse501/06wi/reading/click-pldi95.pdf

# GCM program in Figure 1

function w $gcm_test(w %a){
@start
	%i.0 =w copy 0
@loop
	%i.1 =w phi @start %i.0, @loop %i.2
	%b =w add %a, 1                     # early schedule moves to @start
	%i.2 =w add %i.1, %b
	%c =w mul %i.2, 2                   #  late schedule moves to @end
	%x =w csltw %i.2, 10
	jnz %x, @loop, @end
@end
	ret %c
}

# GCM program in "Figure 3 x's definition does not dominate it's use"
#
# SSA contruction will insert phi instruction for "x" in @if_false
# preventing the "add" in @if_false from being moved to @if_true

function $gcm_test2 (w %a){
@start
	%f =w copy 1
	%x =w copy 0
	%s.0 =w copy 0
@loop
	%s.1 = w phi @start %s.0, @if_false %s.2
	jnz %a, @if, @end
@if
	jnz %f, @if_true, @if_false
@if_true
	%f =w copy 0
	%x =w add %x, 1
@if_false
	%s.2 =w add %s.1, %x
	jmp @loop
@end
	ret
}
