[clang] [CIR] Implement switch case simplify (PR #140649)
via cfe-commits
cfe-commits at lists.llvm.org
Mon May 19 17:19:03 PDT 2025
Andres-Salamanca wrote:
@andykaylor @erichkeane @bcardosolopes
While implementing this simplification, I noticed a difference compared to the previous behavior where the folding happened during codegen.
Consider the following test case:
```cpp
void sw7(int a) {
switch (a) {
case 0:
case 1:
case 2:
int x;
case 3:
case 4:
case 5:
break;
}
}
```
In the [previous implementation](https://github.com/llvm/clangir/blob/1fb346d9c05fa676b76f512eded5bba4010b4a74/clang/test/CIR/CodeGen/switch.cpp#L145), the codegen phase performed the folding. Variable `x` was **hoisted outside** the switch and the generated CIR looked like this:
```
cir.case(anyof, [#cir.int<0> : !s32i, #cir.int<1> : !s32i, #cir.int<2> : !s32i]) {
cir.yield
}
cir.case(anyof, [#cir.int<3> : !s32i, #cir.int<4> : !s32i, #cir.int<5> : !s32i]) {
cir.break
}
```
Now, without codegen folding and before applying the new pass, the output is:
```mlir
%1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x"] {alignment = 4 : i64} // x hoisted out as before
%2 = cir.load %0 : !cir.ptr<!s32i>, !s32i
cir.switch (%2 : !s32i) {
cir.case(equal, [#cir.int<0> : !s32i]) {
cir.yield
}
cir.case(equal, [#cir.int<1> : !s32i]) {
cir.yield
}
cir.case(equal, [#cir.int<2> : !s32i]) {
cir.yield
}
cir.case(equal, [#cir.int<3> : !s32i]) {
cir.yield
}
cir.case(equal, [#cir.int<4> : !s32i]) {
cir.yield
}
cir.case(equal, [#cir.int<5> : !s32i]) {
cir.break
}
cir.yield
}
```
After applying the new CIR simplify pass, the output is:
```mlir
cir.case(anyof, [#cir.int<0> : !s32i, #cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i, #cir.int<5> : !s32i]) {
cir.break
}
```
https://github.com/llvm/llvm-project/pull/140649
More information about the cfe-commits
mailing list