1 ! RUN: bbc -emit-fir -hlfir=false -o - %s | FileCheck %s
2 ! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -o - %s | FileCheck %s
3 ! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -flang-experimental-integer-overflow -o - %s | FileCheck %s --check-prefix=NSW
5 ! Tests for infinite loop.
7 subroutine empty_infinite()
11 ! CHECK-LABEL: empty_infinite
12 ! CHECK: cf.br ^[[BODY:.*]]
14 ! CHECK: cf.br ^[[BODY]]
16 subroutine simple_infinite(i
)
22 ! CHECK-LABEL: simple_infinite
23 ! CHECK-SAME: %[[I_REF:.*]]: !fir.ref<i32>
24 ! CHECK: cf.br ^[[BODY1:.*]]
26 ! CHECK: %[[I:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
27 ! CHECK: %[[C100:.*]] = arith.constant 100 : i32
28 ! CHECK: %[[COND:.*]] = arith.cmpi sgt, %[[I]], %[[C100]] : i32
29 ! CHECK: cf.cond_br %[[COND]], ^[[EXIT:.*]], ^[[BODY1:.*]]
31 ! CHECK: cf.br ^[[RETURN:.*]]
36 subroutine infinite_with_two_body_blocks(i
)
44 ! CHECK-LABEL: infinite_with_two_body_blocks
45 ! CHECK-SAME: %[[I_REF:.*]]: !fir.ref<i32>
46 ! CHECK: cf.br ^[[BODY1:.*]]
48 ! CHECK: %[[I:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
49 ! CHECK: %[[C1:.*]] = arith.constant 1 : i32
50 ! CHECK: %[[I_NEXT:.*]] = arith.addi %[[I]], %[[C1]] : i32
51 ! CHECK: fir.store %[[I_NEXT]] to %[[I_REF]] : !fir.ref<i32>
52 ! CHECK: %[[I:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
53 ! CHECK: %[[C100:.*]] = arith.constant 100 : i32
54 ! CHECK: %[[COND:.*]] = arith.cmpi sgt, %[[I]], %[[C100]] : i32
55 ! CHECK: cf.cond_br %[[COND]], ^[[EXIT:.*]], ^[[BODY2:.*]]
57 ! CHECK: cf.br ^[[RETURN:.*]]
59 ! CHECK: %[[C2:.*]] = arith.constant 2 : i32
60 ! CHECK: %[[I:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
61 ! CHECK: %[[I_NEXT:.*]] = arith.muli %[[C2]], %[[I]] : i32
62 ! CHECK: fir.store %[[I_NEXT]] to %[[I_REF]] : !fir.ref<i32>
63 ! CHECK: cf.br ^[[BODY1]]
68 subroutine structured_loop_in_infinite(i
)
77 ! CHECK-LABEL: structured_loop_in_infinite
78 ! CHECK-SAME: %[[I_REF:.*]]: !fir.ref<i32>
79 ! CHECK: %[[J_REF:.*]] = fir.alloca i32 {bindc_name = "j", uniq_name = "_QFstructured_loop_in_infiniteEj"}
80 ! CHECK: cf.br ^[[BODY1:.*]]
82 ! CHECK: %[[I:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
83 ! CHECK: %[[C100:.*]] = arith.constant 100 : i32
84 ! CHECK: %[[COND:.*]] = arith.cmpi sgt, %[[I]], %[[C100]] : i32
85 ! CHECK: cf.cond_br %[[COND]], ^[[EXIT:.*]], ^[[BODY2:.*]]
87 ! CHECK: cf.br ^[[RETURN:.*]]
88 ! CHECK: ^[[BODY2:.*]]:
89 ! CHECK: %[[C1:.*]] = arith.constant 1 : i32
90 ! CHECK: %[[C1_INDEX:.*]] = fir.convert %[[C1]] : (i32) -> index
91 ! CHECK: %[[C10:.*]] = arith.constant 10 : i32
92 ! CHECK: %[[C10_INDEX:.*]] = fir.convert %[[C10]] : (i32) -> index
93 ! CHECK: %[[C1_1:.*]] = arith.constant 1 : index
94 ! CHECK: %[[J_LB:.*]] = fir.convert %[[C1_INDEX]] : (index) -> i32
95 ! CHECK: %[[J_FINAL:.*]]:2 = fir.do_loop %[[J:[^ ]*]] =
96 ! CHECK-SAME: %[[C1_INDEX]] to %[[C10_INDEX]] step %[[C1_1]]
97 ! CHECK-SAME: iter_args(%[[J_IV:.*]] = %[[J_LB]]) -> (index, i32) {
98 ! CHECK: fir.store %[[J_IV]] to %[[J_REF]] : !fir.ref<i32>
99 ! CHECK: %[[J_NEXT:.*]] = arith.addi %[[J]], %[[C1_1]] : index
100 ! CHECK: %[[J_STEPCAST:.*]] = fir.convert %[[C1_1]] : (index) -> i32
101 ! CHECK: %[[J_IVLOAD:.*]] = fir.load %[[J_REF]] : !fir.ref<i32>
102 ! CHECK: %[[J_IVINC:.*]] = arith.addi %[[J_IVLOAD]], %[[J_STEPCAST]] : i32
103 ! CHECK: fir.result %[[J_NEXT]], %[[J_IVINC]] : index, i32
105 ! CHECK: fir.store %[[J_FINAL]]#1 to %[[J_REF]] : !fir.ref<i32>
106 ! CHECK: cf.br ^[[BODY1]]
107 ! CHECK: ^[[RETURN]]:
110 ! NSW-LABEL: structured_loop_in_infinite
111 ! NSW-SAME: %[[I_REF:.*]]: !fir.ref<i32>
112 ! NSW: %[[J_REF:.*]] = fir.alloca i32 {bindc_name = "j", uniq_name = "_QFstructured_loop_in_infiniteEj"}
113 ! NSW: cf.br ^[[BODY1:.*]]
115 ! NSW: %[[I:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
116 ! NSW: %[[C100:.*]] = arith.constant 100 : i32
117 ! NSW: %[[COND:.*]] = arith.cmpi sgt, %[[I]], %[[C100]] : i32
118 ! NSW: cf.cond_br %[[COND]], ^[[EXIT:.*]], ^[[BODY2:.*]]
120 ! NSW: cf.br ^[[RETURN:.*]]
121 ! NSW: ^[[BODY2:.*]]:
122 ! NSW: %[[C1:.*]] = arith.constant 1 : i32
123 ! NSW: %[[C1_INDEX:.*]] = fir.convert %[[C1]] : (i32) -> index
124 ! NSW: %[[C10:.*]] = arith.constant 10 : i32
125 ! NSW: %[[C10_INDEX:.*]] = fir.convert %[[C10]] : (i32) -> index
126 ! NSW: %[[C1_1:.*]] = arith.constant 1 : index
127 ! NSW: %[[J_LB:.*]] = fir.convert %[[C1_INDEX]] : (index) -> i32
128 ! NSW: %[[J_FINAL:.*]]:2 = fir.do_loop %[[J:[^ ]*]] =
129 ! NSW-SAME: %[[C1_INDEX]] to %[[C10_INDEX]] step %[[C1_1]]
130 ! NSW-SAME: iter_args(%[[J_IV:.*]] = %[[J_LB]]) -> (index, i32) {
131 ! NSW: fir.store %[[J_IV]] to %[[J_REF]] : !fir.ref<i32>
132 ! NSW: %[[J_NEXT:.*]] = arith.addi %[[J]], %[[C1_1]] overflow<nsw> : index
133 ! NSW: %[[J_STEPCAST:.*]] = fir.convert %[[C1_1]] : (index) -> i32
134 ! NSW: %[[J_IVLOAD:.*]] = fir.load %[[J_REF]] : !fir.ref<i32>
135 ! NSW: %[[J_IVINC:.*]] = arith.addi %[[J_IVLOAD]], %[[J_STEPCAST]] overflow<nsw> : i32
136 ! NSW: fir.result %[[J_NEXT]], %[[J_IVINC]] : index, i32
138 ! NSW: fir.store %[[J_FINAL]]#1 to %[[J_REF]] : !fir.ref<i32>
139 ! NSW: cf.br ^[[BODY1]]
143 subroutine empty_infinite_in_while(i
)
151 ! CHECK-LABEL: empty_infinite_in_while
152 ! CHECK-SAME: %[[I_REF:.*]]: !fir.ref<i32>
155 ! CHECK: %[[I:.*]] = fir.load %[[I_REF]] : !fir.ref<i32>
156 ! CHECK: %[[C50:.*]] = arith.constant 50 : i32
157 ! CHECK: %[[COND:.*]] = arith.cmpi sgt, %[[I]], %[[C50]] : i32
158 ! CHECK: cf.cond_br %[[COND]], ^[[INF_HEADER:.*]], ^[[EXIT:.*]]
159 ! CHECK: ^[[INF_HEADER]]:
160 ! CHECK: cf.br ^[[INF_BODY:.*]]
161 ! CHECK: ^[[INF_BODY]]:
162 ! CHECK: cf.br ^[[INF_HEADER]]