Not for review.
Just to demonstrate what one example approach might look like.
target datalayout = "n8:16:32:64"
define i32 @test2(i32 %n) {
entry:
br label %loop
loop:
%iv = phi i32 [0, %entry], [%iv.next, %loop] %iv.next = add i32 %iv, 1 %cmp = icmp ult i32 %iv, %n br i1 %cmp, label %loop, label %exit
exit:
ret i32 %n ;; rewrites this to %iv
}
The hard part is figuring out the right heuristic here.
Is the "loop context" really needed for expressions? Aren't dominance checks done later sufficient to know whether it's legal to replace one value with another or not?