1; RUN: opt -indvars -instcombine -S < %s | FileCheck %s 2 3;; Test that loop's exit value is rewritten to its initial 4;; value from loop preheader 5define i32 @test1(i32* %var) { 6; CHECK-LABEL: @test1 7entry: 8 %cond = icmp eq i32* %var, null 9 br label %header 10 11header: 12 %phi_indvar = phi i32 [0, %entry], [%indvar, %loop] 13 br i1 %cond, label %loop, label %exit 14 15loop: 16 %indvar = add i32 %phi_indvar, 1 17 br label %header 18 19exit: 20; CHECK: ret i32 0 21 ret i32 %phi_indvar 22} 23 24;; Test that we can not rewrite loop exit value if it's not 25;; a phi node (%indvar is an add instruction in this test). 26define i32 @test2(i32* %var) { 27; CHECK-LABEL: @test2 28entry: 29 %cond = icmp eq i32* %var, null 30 br label %header 31 32header: 33 %phi_indvar = phi i32 [0, %entry], [%indvar, %header] 34 %indvar = add i32 %phi_indvar, 1 35 br i1 %cond, label %header, label %exit 36 37exit: 38; CHECK: ret i32 %indvar 39 ret i32 %indvar 40} 41 42;; Test that we can not rewrite loop exit value if the condition 43;; is not in loop header. 44define i32 @test3(i32* %var) { 45; CHECK-LABEL: @test3 46entry: 47 %cond1 = icmp eq i32* %var, null 48 br label %header 49 50header: 51 %phi_indvar = phi i32 [0, %entry], [%indvar, %header], [%indvar, %body] 52 %indvar = add i32 %phi_indvar, 1 53 %cond2 = icmp eq i32 %indvar, 10 54 br i1 %cond2, label %header, label %body 55 56body: 57 br i1 %cond1, label %header, label %exit 58 59exit: 60; CHECK: ret i32 %phi_indvar 61 ret i32 %phi_indvar 62} 63 64