IR: de-duplicate two CmpInst routines (NFC) (#116866)
[llvm-project.git] / flang / test / Lower / goto-do-body.f90
blob910e55f1839fd2a6bfb53b2a201729c5ccdfc897
1 ! RUN: bbc %s -emit-fir -hlfir=false -o - | FileCheck %s
3 ! Test jumping to the body of a do loop.
4 subroutine sub1()
5 ! CHECK-LABEL: func @_QPsub1() {
6 implicit none
7 integer :: i
8 external foo
9 ! CHECK: %[[TRIP:.*]] = fir.alloca i32
10 ! CHECK: %[[I:.*]] = fir.alloca i32 {bindc_name = "i", {{.*}}}
12 do i = 1, 3
13 if (i .eq. 2) goto 70
14 ! CHECK: %[[TMP1:.*]] = fir.load %[[I]] : !fir.ref<i32>
15 ! CHECK: %[[C2_1:.*]] = arith.constant 2 : i32
16 ! CHECK: %[[COND:.*]] = arith.cmpi eq, %[[TMP1]], %[[C2_1]] : i32
17 ! CHECK: cf.cond_br %[[COND]], ^[[COND_TRUE:.*]], ^{{.*}}
18 ! CHECK: ^[[COND_TRUE]]:
19 ! CHECK: cf.br ^[[BODY:.*]]
21 end do
23 call foo
24 ! CHECK: fir.call @_QPfoo()
26 do i = 1, 2
27 ! CHECK: %[[C1_1:.*]] = arith.constant 1 : i32
28 ! CHECK: %[[C2_2:.*]] = arith.constant 2 : i32
29 ! CHECK: %[[C1_2:.*]] = arith.constant 1 : i32
30 ! CHECK: %[[TMP2:.*]] = arith.subi %[[C2_2]], %[[C1_1]] : i32
31 ! CHECK: %[[TMP3:.*]] = arith.addi %[[TMP2]], %[[C1_2]] : i32
32 ! CHECK: %[[TMP4:.*]] = arith.divsi %[[TMP3]], %[[C1_2]] : i32
33 ! CHECK: fir.store %[[TMP4]] to %[[TRIP]] : !fir.ref<i32>
34 ! CHECK: fir.store %[[C1_1]] to %[[I]] : !fir.ref<i32>
35 ! CHECK: cf.br ^[[HEADER:.*]]
36 ! CHECK: ^[[HEADER]]:
37 ! CHECK: %[[TMP5:.*]] = fir.load %[[TRIP]] : !fir.ref<i32>
38 ! CHECK: %[[C0_1:.*]] = arith.constant 0 : i32
39 ! CHECK: %[[TMP6:.*]] = arith.cmpi sgt, %[[TMP5]], %[[C0_1]] : i32
40 ! CHECK: cf.cond_br %[[TMP6]], ^[[BODY]], ^[[EXIT:.*]]
42 70 call foo
43 ! CHECK: ^[[BODY]]:
44 ! CHECK: fir.call @_QPfoo()
45 ! CHECK: %[[TMP7:.*]] = fir.load %[[TRIP]] : !fir.ref<i32>
46 ! CHECK: %[[C1_3:.*]] = arith.constant 1 : i32
47 ! CHECK: %[[TMP8:.*]] = arith.subi %[[TMP7]], %[[C1_3]] : i32
48 ! CHECK: fir.store %[[TMP8]] to %[[TRIP]] : !fir.ref<i32>
49 ! CHECK: %[[TMP9:.*]] = fir.load %[[I]] : !fir.ref<i32>
50 ! CHECK: %[[C1_4:.*]] = arith.constant 1 : i32
51 ! CHECK: %[[TMP10:.*]] = arith.addi %[[TMP9]], %[[C1_4]] : i32
52 ! CHECK: fir.store %[[TMP10]] to %[[I]] : !fir.ref<i32>
53 ! CHECK: cf.br ^[[HEADER]]
54 end do
55 end subroutine
56 ! CHECK: ^[[EXIT]]:
57 ! CHECK: return
58 ! CHECK: }
60 ! Test jumping to the body of a do loop with a step expression.
61 subroutine sub2()
62 ! CHECK-LABEL: func @_QPsub2() {
63 implicit none
64 integer :: i, j
65 external foo
66 ! CHECK: %[[TRIP:.*]] = fir.alloca i32
67 ! CHECK: %[[I:.*]] = fir.alloca i32 {bindc_name = "i", {{.*}}}
68 ! CHECK: %[[J:.*]] = fir.alloca i32 {bindc_name = "j", {{.*}}}
70 do i = 1, 3
71 if (i .eq. 2) goto 70
72 ! CHECK: %[[TMP1:.*]] = fir.load %[[I]] : !fir.ref<i32>
73 ! CHECK: %[[C2_1:.*]] = arith.constant 2 : i32
74 ! CHECK: %[[COND:.*]] = arith.cmpi eq, %[[TMP1]], %[[C2_1]] : i32
75 ! CHECK: cf.cond_br %[[COND]], ^[[COND_TRUE:.*]], ^{{.*}}
76 ! CHECK: ^[[COND_TRUE]]:
77 ! CHECK: cf.br ^[[BODY:.*]]
79 end do
81 call foo
82 ! CHECK: fir.call @_QPfoo()
84 j = 3
85 ! CHECK: %[[C3_1:.*]] = arith.constant 3 : i32
86 ! CHECK: fir.store %[[C3_1]] to %[[J]] : !fir.ref<i32>
88 do i = 1, 2, 3 * j - 8
89 ! CHECK: %[[C1_1:.*]] = arith.constant 1 : i32
90 ! CHECK: %[[C2_2:.*]] = arith.constant 2 : i32
91 ! CHECK: %[[C3_2:.*]] = arith.constant 3 : i32
92 ! CHECK: %[[TMP2:.*]] = fir.load %[[J]] : !fir.ref<i32>
93 ! CHECK: %[[TMP3:.*]] = arith.muli %[[C3_2]], %[[TMP2]] : i32
94 ! CHECK: %[[C8_1:.*]] = arith.constant 8 : i32
95 ! CHECK: %[[STEP:.*]] = arith.subi %[[TMP3]], %[[C8_1]] : i32
96 ! CHECK: fir.store %[[STEP]] to %[[STEP_VAR:.*]] : !fir.ref<i32>
97 ! CHECK: %[[TMP4:.*]] = arith.subi %[[C2_2]], %[[C1_1]] : i32
98 ! CHECK: %[[TMP5:.*]] = arith.addi %[[TMP4]], %[[STEP]] : i32
99 ! CHECK: %[[TMP6:.*]] = arith.divsi %[[TMP5]], %[[STEP]] : i32
100 ! CHECK: fir.store %[[TMP6]] to %[[TRIP]] : !fir.ref<i32>
101 ! CHECK: fir.store %[[C1_1]] to %[[I]] : !fir.ref<i32>
102 ! CHECK: cf.br ^[[HEADER:.*]]
103 ! CHECK: ^[[HEADER]]:
104 ! CHECK: %[[TMP7:.*]] = fir.load %[[TRIP]] : !fir.ref<i32>
105 ! CHECK: %[[C0_1:.*]] = arith.constant 0 : i32
106 ! CHECK: %[[TMP8:.*]] = arith.cmpi sgt, %[[TMP7]], %[[C0_1]] : i32
107 ! CHECK: cf.cond_br %[[TMP8]], ^[[BODY]], ^[[EXIT:.*]]
109 70 call foo
110 ! CHECK: ^[[BODY]]:
111 ! CHECK: fir.call @_QPfoo()
112 ! CHECK: %[[TMP9:.*]] = fir.load %[[TRIP]] : !fir.ref<i32>
113 ! CHECK: %[[C1_2:.*]] = arith.constant 1 : i32
114 ! CHECK: %[[TMP10:.*]] = arith.subi %[[TMP9]], %[[C1_2]] : i32
115 ! CHECK: fir.store %[[TMP10]] to %[[TRIP]] : !fir.ref<i32>
116 ! CHECK: %[[TMP11:.*]] = fir.load %[[I]] : !fir.ref<i32>
117 ! CHECK: %[[STEP_VAL:.*]] = fir.load %[[STEP_VAR]] : !fir.ref<i32>
118 ! CHECK: %[[TMP12:.*]] = arith.addi %[[TMP11]], %[[STEP_VAL]] : i32
119 ! CHECK: fir.store %[[TMP12]] to %[[I]] : !fir.ref<i32>
120 ! CHECK: cf.br ^[[HEADER]]
121 end do
122 end subroutine
123 ! CHECK: ^[[EXIT]]:
124 ! CHECK: return
125 ! CHECK: }