1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -indvars -instcombine -S < %s | FileCheck %s
4 ;; Test that loop's exit value is rewritten to its initial
5 ;; value from loop preheader
6 define i32 @test1(i32* %var) {
9 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i32* [[VAR:%.*]], null
10 ; CHECK-NEXT: br label [[HEADER:%.*]]
12 ; CHECK-NEXT: br i1 [[COND]], label [[LOOP:%.*]], label [[EXIT:%.*]]
14 ; CHECK-NEXT: br label [[HEADER]]
16 ; CHECK-NEXT: ret i32 0
19 %cond = icmp eq i32* %var, null
23 %phi_indvar = phi i32 [0, %entry], [%indvar, %loop]
24 br i1 %cond, label %loop, label %exit
27 %indvar = add i32 %phi_indvar, 1
34 ;; Test that we can not rewrite loop exit value if it's not
35 ;; a phi node (%indvar is an add instruction in this test).
36 define i32 @test2(i32* %var) {
37 ; CHECK-LABEL: @test2(
39 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i32* [[VAR:%.*]], null
40 ; CHECK-NEXT: br label [[HEADER:%.*]]
42 ; CHECK-NEXT: [[PHI_INDVAR:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INDVAR:%.*]], [[HEADER]] ]
43 ; CHECK-NEXT: [[INDVAR]] = add i32 [[PHI_INDVAR]], 1
44 ; CHECK-NEXT: br i1 [[COND]], label [[HEADER]], label [[EXIT:%.*]]
46 ; CHECK-NEXT: ret i32 [[INDVAR]]
49 %cond = icmp eq i32* %var, null
53 %phi_indvar = phi i32 [0, %entry], [%indvar, %header]
54 %indvar = add i32 %phi_indvar, 1
55 br i1 %cond, label %header, label %exit
61 ;; Test that we can not rewrite loop exit value if the condition
62 ;; is not in loop header.
63 define i32 @test3(i32* %var) {
64 ; CHECK-LABEL: @test3(
66 ; CHECK-NEXT: [[COND1:%.*]] = icmp eq i32* [[VAR:%.*]], null
67 ; CHECK-NEXT: br label [[HEADER:%.*]]
69 ; CHECK-NEXT: [[PHI_INDVAR:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INDVAR:%.*]], [[HEADER_BACKEDGE:%.*]] ]
70 ; CHECK-NEXT: [[INDVAR]] = add i32 [[PHI_INDVAR]], 1
71 ; CHECK-NEXT: [[COND2:%.*]] = icmp eq i32 [[INDVAR]], 10
72 ; CHECK-NEXT: br i1 [[COND2]], label [[HEADER_BACKEDGE]], label [[BODY:%.*]]
73 ; CHECK: header.backedge:
74 ; CHECK-NEXT: br label [[HEADER]]
76 ; CHECK-NEXT: br i1 [[COND1]], label [[HEADER_BACKEDGE]], label [[EXIT:%.*]]
78 ; CHECK-NEXT: ret i32 [[PHI_INDVAR]]
81 %cond1 = icmp eq i32* %var, null
85 %phi_indvar = phi i32 [0, %entry], [%indvar, %header], [%indvar, %body]
86 %indvar = add i32 %phi_indvar, 1
87 %cond2 = icmp eq i32 %indvar, 10
88 br i1 %cond2, label %header, label %body
91 br i1 %cond1, label %header, label %exit
98 ; Multiple exits dominating latch
99 define i32 @test4(i1 %cond1, i1 %cond2) {
100 ; CHECK-LABEL: @test4(
102 ; CHECK-NEXT: br label [[HEADER:%.*]]
104 ; CHECK-NEXT: br i1 [[COND1:%.*]], label [[LOOP:%.*]], label [[EXIT:%.*]]
106 ; CHECK-NEXT: br i1 [[COND2:%.*]], label [[HEADER]], label [[EXIT]]
108 ; CHECK-NEXT: ret i32 0
114 %phi_indvar = phi i32 [0, %entry], [%indvar, %loop]
115 br i1 %cond1, label %loop, label %exit
118 %indvar = add i32 %phi_indvar, 1
119 br i1 %cond2, label %header, label %exit
125 ; A conditionally executed exit.
126 define i32 @test5(i1* %addr, i1 %cond2) {
127 ; CHECK-LABEL: @test5(
129 ; CHECK-NEXT: br label [[HEADER:%.*]]
131 ; CHECK-NEXT: [[PHI_INDVAR:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INDVAR:%.*]], [[LOOP:%.*]] ]
132 ; CHECK-NEXT: [[COND1:%.*]] = load volatile i1, i1* [[ADDR:%.*]], align 1
133 ; CHECK-NEXT: br i1 [[COND1]], label [[LOOP]], label [[MAYBE:%.*]]
135 ; CHECK-NEXT: br i1 [[COND2:%.*]], label [[LOOP]], label [[EXIT:%.*]]
137 ; CHECK-NEXT: [[INDVAR]] = add i32 [[PHI_INDVAR]], 1
138 ; CHECK-NEXT: br label [[HEADER]]
140 ; CHECK-NEXT: ret i32 [[PHI_INDVAR]]
146 %phi_indvar = phi i32 [0, %entry], [%indvar, %loop]
147 %cond1 = load volatile i1, i1* %addr
148 br i1 %cond1, label %loop, label %maybe
151 br i1 %cond2, label %loop, label %exit
154 %indvar = add i32 %phi_indvar, 1