| 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 -fclangir -emit-llvm %s -o %t-cir.ll |
| 4 |
// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM |
| 5 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll |
| 6 |
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG |
| 7 |
|
| 8 |
int shouldNotGenBranchRet(int x) { |
| 9 |
if (x > 5) |
| 10 |
goto err; |
| 11 |
return 0; |
| 12 |
err: |
| 13 |
return -1; |
| 14 |
} |
| 15 |
// CIR: cir.func {{.*}} @_Z21shouldNotGenBranchReti |
| 16 |
// CIR: cir.if {{.*}} { |
| 17 |
// CIR: cir.goto "err" |
| 18 |
// CIR: } |
| 19 |
// CIR: [[ZERO:%.*]] = cir.const #cir.int<0> : !s32i |
| 20 |
// CIR: cir.store [[ZERO]], [[RETVAL:%.*]] : !s32i, !cir.ptr<!s32i> |
| 21 |
// CIR: cir.br ^bb1 |
| 22 |
// CIR: ^bb1: |
| 23 |
// CIR: [[RET:%.*]] = cir.load [[RETVAL]] : !cir.ptr<!s32i>, !s32i |
| 24 |
// CIR: cir.return [[RET]] : !s32i |
| 25 |
// CIR: ^bb2: |
| 26 |
// CIR: cir.label "err" |
| 27 |
// CIR: [[MINUS_ONE:%.*]] = cir.const #cir.int<-1> : !s32i |
| 28 |
// CIR: cir.store [[MINUS_ONE]], [[RETVAL]] : !s32i, !cir.ptr<!s32i> |
| 29 |
// CIR: cir.br ^bb1 |
| 30 |
|
| 31 |
// LLVM: define dso_local i32 @_Z21shouldNotGenBranchReti |
| 32 |
// LLVM: [[COND:%.*]] = load i32, ptr {{.*}}, align 4 |
| 33 |
// LLVM: [[CMP:%.*]] = icmp sgt i32 [[COND]], 5 |
| 34 |
// LLVM: br i1 [[CMP]], label %[[IFTHEN:.*]], label %[[IFEND:.*]] |
| 35 |
// LLVM: [[IFTHEN]]: |
| 36 |
// LLVM: br label %[[ERR:.*]] |
| 37 |
// LLVM: [[IFEND]]: |
| 38 |
// LLVM: br label %[[BB9:.*]] |
| 39 |
// LLVM: [[BB9]]: |
| 40 |
// LLVM: store i32 0, ptr %[[RETVAL:.*]], align 4 |
| 41 |
// LLVM: br label %[[BBRET:.*]] |
| 42 |
// LLVM: [[BBRET]]: |
| 43 |
// LLVM: [[RET:%.*]] = load i32, ptr %[[RETVAL]], align 4 |
| 44 |
// LLVM: ret i32 [[RET]] |
| 45 |
// LLVM: [[ERR]]: |
| 46 |
// LLVM: store i32 -1, ptr %[[RETVAL]], align 4 |
| 47 |
// LLVM: br label %10 |
| 48 |
|
| 49 |
// OGCG: define dso_local noundef i32 @_Z21shouldNotGenBranchReti |
| 50 |
// OGCG: if.then: |
| 51 |
// OGCG: br label %err |
| 52 |
// OGCG: if.end: |
| 53 |
// OGCG: br label %return |
| 54 |
// OGCG: err: |
| 55 |
// OGCG: br label %return |
| 56 |
// OGCG: return: |
| 57 |
|
| 58 |
int shouldGenBranch(int x) { |
| 59 |
if (x > 5) |
| 60 |
goto err; |
| 61 |
x++; |
| 62 |
err: |
| 63 |
return -1; |
| 64 |
} |
| 65 |
// CIR: cir.func {{.*}} @_Z15shouldGenBranchi |
| 66 |
// CIR: cir.if {{.*}} { |
| 67 |
// CIR: cir.goto "err" |
| 68 |
// CIR: } |
| 69 |
// CIR: cir.br ^bb1 |
| 70 |
// CIR: ^bb1: |
| 71 |
// CIR: cir.label "err" |
| 72 |
|
| 73 |
// LLVM: define dso_local i32 @_Z15shouldGenBranchi |
| 74 |
// LLVM: br i1 [[CMP:%.*]], label %[[IFTHEN:.*]], label %[[IFEND:.*]] |
| 75 |
// LLVM: [[IFTHEN]]: |
| 76 |
// LLVM: br label %[[ERR:.*]] |
| 77 |
// LLVM: [[IFEND]]: |
| 78 |
// LLVM: br label %[[BB9:.*]] |
| 79 |
// LLVM: [[BB9]]: |
| 80 |
// LLVM: br label %[[ERR]] |
| 81 |
// LLVM: [[ERR]]: |
| 82 |
// LLVM: ret i32 [[RET:%.*]] |
| 83 |
|
| 84 |
// OGCG: define dso_local noundef i32 @_Z15shouldGenBranchi |
| 85 |
// OGCG: if.then: |
| 86 |
// OGCG: br label %err |
| 87 |
// OGCG: if.end: |
| 88 |
// OGCG: br label %err |
| 89 |
// OGCG: err: |
| 90 |
// OGCG: ret |
| 91 |
|
| 92 |
void severalLabelsInARow(int a) { |
| 93 |
int b = a; |
| 94 |
goto end1; |
| 95 |
b = b + 1; |
| 96 |
goto end2; |
| 97 |
end1: |
| 98 |
end2: |
| 99 |
b = b + 2; |
| 100 |
} |
| 101 |
// CIR: cir.func {{.*}} @_Z19severalLabelsInARowi |
| 102 |
// CIR: cir.goto "end1" |
| 103 |
// CIR: ^bb[[#BLK1:]] |
| 104 |
// CIR: cir.goto "end2" |
| 105 |
// CIR: ^bb[[#BLK2:]]: |
| 106 |
// CIR: cir.label "end1" |
| 107 |
// CIR: cir.br ^bb[[#BLK3:]] |
| 108 |
// CIR: ^bb[[#BLK3]]: |
| 109 |
// CIR: cir.label "end2" |
| 110 |
|
| 111 |
// LLVM: define dso_local void @_Z19severalLabelsInARowi |
| 112 |
// LLVM: br label %[[END1:.*]] |
| 113 |
// LLVM: [[UNRE:.*]]: ; No predecessors! |
| 114 |
// LLVM: br label %[[END2:.*]] |
| 115 |
// LLVM: [[END1]]: |
| 116 |
// LLVM: br label %[[END2]] |
| 117 |
// LLVM: [[END2]]: |
| 118 |
// LLVM: ret |
| 119 |
|
| 120 |
// OGCG: define dso_local void @_Z19severalLabelsInARowi |
| 121 |
// OGCG: br label %end1 |
| 122 |
// OGCG: end1: |
| 123 |
// OGCG: br label %end2 |
| 124 |
// OGCG: end2: |
| 125 |
// OGCG: ret |
| 126 |
|
| 127 |
void severalGotosInARow(int a) { |
| 128 |
int b = a; |
| 129 |
goto end; |
| 130 |
goto end; |
| 131 |
end: |
| 132 |
b = b + 2; |
| 133 |
} |
| 134 |
// CIR: cir.func {{.*}} @_Z18severalGotosInARowi |
| 135 |
// CIR: cir.goto "end" |
| 136 |
// CIR: ^bb[[#BLK1:]]: |
| 137 |
// CIR: cir.goto "end" |
| 138 |
// CIR: ^bb[[#BLK2:]]: |
| 139 |
// CIR: cir.label "end" |
| 140 |
|
| 141 |
// LLVM: define dso_local void @_Z18severalGotosInARowi |
| 142 |
// LLVM: br label %[[END:.*]] |
| 143 |
// LLVM: [[UNRE:.*]]: ; No predecessors! |
| 144 |
// LLVM: br label %[[END]] |
| 145 |
// LLVM: [[END]]: |
| 146 |
// LLVM: ret void |
| 147 |
|
| 148 |
// OGCG: define dso_local void @_Z18severalGotosInARowi(i32 noundef %a) #0 { |
| 149 |
// OGCG: br label %end |
| 150 |
// OGCG: end: |
| 151 |
// OGCG: ret void |
| 152 |
|
| 153 |
extern "C" void action1(); |
| 154 |
extern "C" void action2(); |
| 155 |
extern "C" void multiple_non_case(int v) { |
| 156 |
switch (v) { |
| 157 |
default: |
| 158 |
action1(); |
| 159 |
l2: |
| 160 |
action2(); |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// CIR: cir.func {{.*}} @multiple_non_case |
| 166 |
// CIR: cir.switch |
| 167 |
// CIR: cir.case(default, []) { |
| 168 |
// CIR: cir.call @action1() |
| 169 |
// CIR: cir.br ^[[BB1:[a-zA-Z0-9]+]] |
| 170 |
// CIR: ^[[BB1]]: |
| 171 |
// CIR: cir.label |
| 172 |
// CIR: cir.call @action2() |
| 173 |
// CIR: cir.break |
| 174 |
|
| 175 |
// LLVM: define dso_local void @multiple_non_case |
| 176 |
// LLVM: [[SWDEFAULT:.*]]: |
| 177 |
// LLVM: call void @action1() |
| 178 |
// LLVM: br label %[[L2:.*]] |
| 179 |
// LLVM: [[L2]]: |
| 180 |
// LLVM: call void @action2() |
| 181 |
// LLVM: br label %[[BREAK:.*]] |
| 182 |
|
| 183 |
// OGCG: define dso_local void @multiple_non_case |
| 184 |
// OGCG: sw.default: |
| 185 |
// OGCG: call void @action1() |
| 186 |
// OGCG: br label %l2 |
| 187 |
// OGCG: l2: |
| 188 |
// OGCG: call void @action2() |
| 189 |
// OGCG: br label [[BREAK:%.*]] |
| 190 |
|
| 191 |
extern "C" void case_follow_label(int v) { |
| 192 |
switch (v) { |
| 193 |
case 1: |
| 194 |
label: |
| 195 |
case 2: |
| 196 |
action1(); |
| 197 |
break; |
| 198 |
default: |
| 199 |
action2(); |
| 200 |
goto label; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
// CIR: cir.func {{.*}} @case_follow_label |
| 205 |
// CIR: cir.switch |
| 206 |
// CIR: cir.case(equal, [#cir.int<1> : !s32i]) { |
| 207 |
// CIR: cir.br ^bb1 |
| 208 |
// CIR: ^bb1: |
| 209 |
// CIR: cir.label "label" |
| 210 |
// CIR: cir.case(equal, [#cir.int<2> : !s32i]) { |
| 211 |
// CIR: cir.call @action1() |
| 212 |
// CIR: cir.break |
| 213 |
// CIR: cir.case(default, []) { |
| 214 |
// CIR: cir.call @action2() |
| 215 |
// CIR: cir.goto "label" |
| 216 |
|
| 217 |
// LLVM: define dso_local void @case_follow_label |
| 218 |
// LLVM: switch i32 {{.*}}, label %[[SWDEFAULT:.*]] [ |
| 219 |
// LLVM: i32 1, label %[[CASE1:.*]] |
| 220 |
// LLVM: i32 2, label %[[CASE2:.*]] |
| 221 |
// LLVM: ] |
| 222 |
// LLVM: [[CASE1]]: |
| 223 |
// LLVM: br label %[[LABEL:.*]] |
| 224 |
// LLVM: [[LABEL]]: |
| 225 |
// LLVM: br label %[[CASE2]] |
| 226 |
// LLVM: [[CASE2]]: |
| 227 |
// LLVM: call void @action1() |
| 228 |
// LLVM: br label %[[BREAK:.*]] |
| 229 |
// LLVM: [[BREAK]]: |
| 230 |
// LLVM: br label %[[END:.*]] |
| 231 |
// LLVM: [[SWDEFAULT]]: |
| 232 |
// LLVM: call void @action2() |
| 233 |
// LLVM: br label %[[LABEL]] |
| 234 |
// LLVM: [[END]]: |
| 235 |
// LLVM: br label %[[RET:.*]] |
| 236 |
// LLVM: [[RET]]: |
| 237 |
// LLVM: ret void |
| 238 |
|
| 239 |
// OGCG: define dso_local void @case_follow_label |
| 240 |
// OGCG: sw.bb: |
| 241 |
// OGCG: br label %label |
| 242 |
// OGCG: label: |
| 243 |
// OGCG: br label %sw.bb1 |
| 244 |
// OGCG: sw.bb1: |
| 245 |
// OGCG: call void @action1() |
| 246 |
// OGCG: br label %sw.epilog |
| 247 |
// OGCG: sw.default: |
| 248 |
// OGCG: call void @action2() |
| 249 |
// OGCG: br label %label |
| 250 |
// OGCG: sw.epilog: |
| 251 |
// OGCG: ret void |
| 252 |
|
| 253 |
extern "C" void default_follow_label(int v) { |
| 254 |
switch (v) { |
| 255 |
case 1: |
| 256 |
case 2: |
| 257 |
action1(); |
| 258 |
break; |
| 259 |
label: |
| 260 |
default: |
| 261 |
action2(); |
| 262 |
goto label; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
// CIR: cir.func {{.*}} @default_follow_label |
| 267 |
// CIR: cir.switch |
| 268 |
// CIR: cir.case(equal, [#cir.int<1> : !s32i]) { |
| 269 |
// CIR: cir.yield |
| 270 |
// CIR: cir.case(equal, [#cir.int<2> : !s32i]) { |
| 271 |
// CIR: cir.call @action1() |
| 272 |
// CIR: cir.break |
| 273 |
// CIR: cir.label "label" |
| 274 |
// CIR: cir.case(default, []) { |
| 275 |
// CIR: cir.call @action2() |
| 276 |
// CIR: cir.goto "label" |
| 277 |
|
| 278 |
// LLVM: define dso_local void @default_follow_label |
| 279 |
// LLVM: [[CASE1:.*]]: |
| 280 |
// LLVM: br label %[[BB8:.*]] |
| 281 |
// LLVM: [[BB8]]: |
| 282 |
// LLVM: br label %[[CASE2:.*]] |
| 283 |
// LLVM: [[CASE2]]: |
| 284 |
// LLVM: call void @action1() |
| 285 |
// LLVM: br label %[[BREAK:.*]] |
| 286 |
// LLVM: [[LABEL:.*]]: |
| 287 |
// LLVM: br label %[[SWDEFAULT:.*]] |
| 288 |
// LLVM: [[SWDEFAULT]]: |
| 289 |
// LLVM: call void @action2() |
| 290 |
// LLVM: br label %[[BB9:.*]] |
| 291 |
// LLVM: [[BB9]]: |
| 292 |
// LLVM: br label %[[LABEL]] |
| 293 |
// LLVM: [[BREAK]]: |
| 294 |
// LLVM: br label %[[RET:.*]] |
| 295 |
// LLVM: [[RET]]: |
| 296 |
// LLVM: ret void |
| 297 |
|
| 298 |
// OGCG: define dso_local void @default_follow_label |
| 299 |
// OGCG: sw.bb: |
| 300 |
// OGCG: call void @action1() |
| 301 |
// OGCG: br label %sw.epilog |
| 302 |
// OGCG: label: |
| 303 |
// OGCG: br label %sw.default |
| 304 |
// OGCG: sw.default: |
| 305 |
// OGCG: call void @action2() |
| 306 |
// OGCG: br label %label |
| 307 |
// OGCG: sw.epilog: |
| 308 |
// OGCG: ret void |
| 309 |
|
| 310 |
void g3() { |
| 311 |
label: |
| 312 |
goto label; |
| 313 |
} |
| 314 |
|
| 315 |
// CIR: cir.func {{.*}} @_Z2g3v |
| 316 |
// CIR: cir.br ^bb1 |
| 317 |
// CIR: ^bb1: |
| 318 |
// CIR: cir.label "label" |
| 319 |
// CIR: cir.goto "label" |
| 320 |
|
| 321 |
// LLVM: define dso_local void @_Z2g3v() |
| 322 |
// LLVM: br label %1 |
| 323 |
// LLVM: 1: |
| 324 |
// LLVM: br label %1 |
| 325 |
|
| 326 |
// OGCG: define dso_local void @_Z2g3v() |
| 327 |
// OGCG: br label %label |
| 328 |
// OGCG: label: |
| 329 |
// OGCG: br label %label |