|
| 1 | +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir |
| 2 | +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR |
| 3 | +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll |
| 4 | +// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG |
| 5 | + |
| 6 | +int shouldNotGenBranchRet(int x) { |
| 7 | + if (x > 5) |
| 8 | + goto err; |
| 9 | + return 0; |
| 10 | +err: |
| 11 | + return -1; |
| 12 | +} |
| 13 | +// CIR: cir.func dso_local @_Z21shouldNotGenBranchReti |
| 14 | +// CIR: cir.if {{.*}} { |
| 15 | +// CIR: cir.goto "err" |
| 16 | +// CIR: } |
| 17 | +// CIR: [[ZERO:%.*]] = cir.const #cir.int<0> : !s32i |
| 18 | +// CIR: cir.store [[ZERO]], [[RETVAL:%.*]] : !s32i, !cir.ptr<!s32i> |
| 19 | +// CIR: cir.br ^bb1 |
| 20 | +// CIR: ^bb1: |
| 21 | +// CIR: [[RET:%.*]] = cir.load [[RETVAL]] : !cir.ptr<!s32i>, !s32i |
| 22 | +// CIR: cir.return [[RET]] : !s32i |
| 23 | +// CIR: ^bb2: |
| 24 | +// CIR: cir.label "err" |
| 25 | +// CIR: [[ONE:%.*]] = cir.const #cir.int<1> : !s32i |
| 26 | +// CIR: [[MINUS:%.*]] = cir.unary(minus, [[ONE]]) nsw : !s32i, !s32i |
| 27 | +// CIR: cir.store [[MINUS]], [[RETVAL]] : !s32i, !cir.ptr<!s32i> |
| 28 | +// CIR: cir.br ^bb1 |
| 29 | + |
| 30 | +// OGCG: define dso_local noundef i32 @_Z21shouldNotGenBranchReti |
| 31 | +// OGCG: if.then: |
| 32 | +// OGCG: br label %err |
| 33 | +// OGCG: if.end: |
| 34 | +// OGCG: br label %return |
| 35 | +// OGCG: err: |
| 36 | +// OGCG: br label %return |
| 37 | +// OGCG: return: |
| 38 | + |
| 39 | +int shouldGenBranch(int x) { |
| 40 | + if (x > 5) |
| 41 | + goto err; |
| 42 | + x++; |
| 43 | +err: |
| 44 | + return -1; |
| 45 | +} |
| 46 | +// CIR: cir.func dso_local @_Z15shouldGenBranchi |
| 47 | +// CIR: cir.if {{.*}} { |
| 48 | +// CIR: cir.goto "err" |
| 49 | +// CIR: } |
| 50 | +// CIR: cir.br ^bb1 |
| 51 | +// CIR: ^bb1: |
| 52 | +// CIR: cir.label "err" |
| 53 | + |
| 54 | +// OGCG: define dso_local noundef i32 @_Z15shouldGenBranchi |
| 55 | +// OGCG: if.then: |
| 56 | +// OGCG: br label %err |
| 57 | +// OGCG: if.end: |
| 58 | +// OGCG: br label %err |
| 59 | +// OGCG: err: |
| 60 | +// OGCG: ret |
| 61 | + |
| 62 | +void severalLabelsInARow(int a) { |
| 63 | + int b = a; |
| 64 | + goto end1; |
| 65 | + b = b + 1; |
| 66 | + goto end2; |
| 67 | +end1: |
| 68 | +end2: |
| 69 | + b = b + 2; |
| 70 | +} |
| 71 | +// CIR: cir.func dso_local @_Z19severalLabelsInARowi |
| 72 | +// CIR: cir.goto "end1" |
| 73 | +// CIR: ^bb[[#BLK1:]] |
| 74 | +// CIR: cir.goto "end2" |
| 75 | +// CIR: ^bb[[#BLK2:]]: |
| 76 | +// CIR: cir.label "end1" |
| 77 | +// CIR: cir.br ^bb[[#BLK3:]] |
| 78 | +// CIR: ^bb[[#BLK3]]: |
| 79 | +// CIR: cir.label "end2" |
| 80 | + |
| 81 | +// OGCG: define dso_local void @_Z19severalLabelsInARowi |
| 82 | +// OGCG: br label %end1 |
| 83 | +// OGCG: end1: |
| 84 | +// OGCG: br label %end2 |
| 85 | +// OGCG: end2: |
| 86 | +// OGCG: ret |
| 87 | + |
| 88 | +void severalGotosInARow(int a) { |
| 89 | + int b = a; |
| 90 | + goto end; |
| 91 | + goto end; |
| 92 | +end: |
| 93 | + b = b + 2; |
| 94 | +} |
| 95 | +// CIR: cir.func dso_local @_Z18severalGotosInARowi |
| 96 | +// CIR: cir.goto "end" |
| 97 | +// CIR: ^bb[[#BLK1:]]: |
| 98 | +// CIR: cir.goto "end" |
| 99 | +// CIR: ^bb[[#BLK2:]]: |
| 100 | +// CIR: cir.label "end" |
| 101 | + |
| 102 | +// OGCG: define dso_local void @_Z18severalGotosInARowi(i32 noundef %a) #0 { |
| 103 | +// OGCG: br label %end |
| 104 | +// OGCG: end: |
| 105 | +// OGCG: ret void |
| 106 | + |
| 107 | +extern "C" void action1(); |
| 108 | +extern "C" void action2(); |
| 109 | +extern "C" void multiple_non_case(int v) { |
| 110 | + switch (v) { |
| 111 | + default: |
| 112 | + action1(); |
| 113 | + l2: |
| 114 | + action2(); |
| 115 | + break; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// CIR: cir.func dso_local @multiple_non_case |
| 120 | +// CIR: cir.switch |
| 121 | +// CIR: cir.case(default, []) { |
| 122 | +// CIR: cir.call @action1() |
| 123 | +// CIR: cir.br ^[[BB1:[a-zA-Z0-9]+]] |
| 124 | +// CIR: ^[[BB1]]: |
| 125 | +// CIR: cir.label |
| 126 | +// CIR: cir.call @action2() |
| 127 | +// CIR: cir.break |
| 128 | + |
| 129 | +// OGCG: define dso_local void @multiple_non_case |
| 130 | +// OGCG: sw.default: |
| 131 | +// OGCG: call void @action1() |
| 132 | +// OGCG: br label %l2 |
| 133 | +// OGCG: l2: |
| 134 | +// OGCG: call void @action2() |
| 135 | +// OGCG: br label [[BREAK:%.*]] |
| 136 | + |
| 137 | +extern "C" void case_follow_label(int v) { |
| 138 | + switch (v) { |
| 139 | + case 1: |
| 140 | + label: |
| 141 | + case 2: |
| 142 | + action1(); |
| 143 | + break; |
| 144 | + default: |
| 145 | + action2(); |
| 146 | + goto label; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// CIR: cir.func dso_local @case_follow_label |
| 151 | +// CIR: cir.switch |
| 152 | +// CIR: cir.case(equal, [#cir.int<1> : !s32i]) { |
| 153 | +// CIR: cir.label "label" |
| 154 | +// CIR: cir.case(equal, [#cir.int<2> : !s32i]) { |
| 155 | +// CIR: cir.call @action1() |
| 156 | +// CIR: cir.break |
| 157 | +// CIR: cir.case(default, []) { |
| 158 | +// CIR: cir.call @action2() |
| 159 | +// CIR: cir.goto "label" |
| 160 | + |
| 161 | +// OGCG: define dso_local void @case_follow_label |
| 162 | +// OGCG: sw.bb: |
| 163 | +// OGCG: br label %label |
| 164 | +// OGCG: label: |
| 165 | +// OGCG: br label %sw.bb1 |
| 166 | +// OGCG: sw.bb1: |
| 167 | +// OGCG: call void @action1() |
| 168 | +// OGCG: br label %sw.epilog |
| 169 | +// OGCG: sw.default: |
| 170 | +// OGCG: call void @action2() |
| 171 | +// OGCG: br label %label |
| 172 | +// OGCG: sw.epilog: |
| 173 | +// OGCG: ret void |
| 174 | + |
| 175 | +extern "C" void default_follow_label(int v) { |
| 176 | + switch (v) { |
| 177 | + case 1: |
| 178 | + case 2: |
| 179 | + action1(); |
| 180 | + break; |
| 181 | + label: |
| 182 | + default: |
| 183 | + action2(); |
| 184 | + goto label; |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +// CIR: cir.func dso_local @default_follow_label |
| 189 | +// CIR: cir.switch |
| 190 | +// CIR: cir.case(equal, [#cir.int<1> : !s32i]) { |
| 191 | +// CIR: cir.yield |
| 192 | +// CIR: cir.case(equal, [#cir.int<2> : !s32i]) { |
| 193 | +// CIR: cir.call @action1() |
| 194 | +// CIR: cir.break |
| 195 | +// CIR: cir.label "label" |
| 196 | +// CIR: cir.case(default, []) { |
| 197 | +// CIR: cir.call @action2() |
| 198 | +// CIR: cir.goto "label" |
| 199 | + |
| 200 | +// OGCG: define dso_local void @default_follow_label |
| 201 | +// OGCG: sw.bb: |
| 202 | +// OGCG: call void @action1() |
| 203 | +// OGCG: br label %sw.epilog |
| 204 | +// OGCG: label: |
| 205 | +// OGCG: br label %sw.default |
| 206 | +// OGCG: sw.default: |
| 207 | +// OGCG: call void @action2() |
| 208 | +// OGCG: br label %label |
| 209 | +// OGCG: sw.epilog: |
| 210 | +// OGCG: ret void |
0 commit comments