1 ; RUN: opt -indvars -instcombine -S < %s | FileCheck %s
3 ;; Test that loop's exit value is rewritten to its initial
4 ;; value from loop preheader
5 define i32 @test1(i32* %var) {
8 %cond = icmp eq i32* %var, null
12 %phi_indvar = phi i32 [0, %entry], [%indvar, %loop]
13 br i1 %cond, label %loop, label %exit
16 %indvar = add i32 %phi_indvar, 1
25 ;; Test that inner loop's exit value is first rewritten to outer
26 ;; loop's induction variable, and then further rewritten to a
27 ;; constant when process outer loop.
28 define i32 @test2(i32* %var1, i32* %var2) {
31 %cond1 = icmp eq i32* %var1, null
32 %cond2 = icmp eq i32* %var2, null
33 br label %outer_header
36 %phi_outer = phi i32 [0, %entry], [%indvar_outer, %inner_exit]
37 br label %inner_header
40 %phi_inner = phi i32 [%phi_outer, %outer_header], [%indvar_inner, %loop]
41 br i1 %cond1, label %loop, label %exit
44 %indvar_inner = add i32 %phi_inner, 1
45 br i1 %cond2, label %inner_header, label %inner_exit
48 %indvar_outer = add i32 %phi_outer, 1
49 br label %outer_header
52 ;; %phi_inner is first rewritten to %phi_outer
53 ;; and then %phi_outer is rewritten to 0
54 %ret_val = add i32 %phi_inner, %phi_outer
59 ;; Test that we can not rewrite loop exit value if it's not
60 ;; a phi node (%indvar is an add instruction in this test).
61 define i32 @test3(i32* %var) {
64 %cond = icmp eq i32* %var, null
68 %phi_indvar = phi i32 [0, %entry], [%indvar, %header]
69 %indvar = add i32 %phi_indvar, 1
70 br i1 %cond, label %header, label %exit
73 ; CHECK: ret i32 %indvar