<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64059>64059</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SimplifyCFG] Miss lookup table optimization even for simple switch
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
XChy
</td>
</tr>
</table>
<pre>
Shell: `opt -S -passes='simplifycfg<switch-to-lookup>' src.ll.`
Lookup table optimization has no effect for examples below:
```llvm
define i8 @src(i8 noundef %arg) {
start:
switch i8 %arg, label %unreachable [
i8 0, label %case0
i8 1, label %case1
i8 2, label %case2
i8 3, label %case3
]
unreachable:
unreachable
case0:
br label %case3
case1:
br label %case3
case2:
br label %case3
case3:
%phi = phi i8 [0, %case0], [ 1, %case1 ], [ 2, %case2 ], [3, %start]
ret i8 %phi
}
```
This should be folded into `ret i8 %arg` with "SingleValue" lookup table, but NOT in fact.
Neither for ` %phi = phi i8 [1, %case0], [ 2, %case1 ], [ 3, %case2 ], [4, %start]`, which should be folded into `ret(add %arg 1)` with "LinearMapKind" lookup table.
To find the reason why the IR above do not fold under switch-to-lookup optimization, I debugged with these IR.
Finally, I found that the function **isTypeLegalForLookupTable** determines that i8 is illegal for DataLayout (So are i16, i32, i64).
Maybe this is because I do not specify DataLayout, but from my point of view, such SingleValue/LinearMap optimization should be machine-independent.
#63876 provides a proof about transformation.
It would be great if someone could explain whether it's a miscompilation and fix it if yes.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVVFv4zYM_jXKC5HAkWI7echD2yzDYb0bsBbDXmWLjrjJkiHJzWW_fpCctk7vbjigaCx91CfyI0XKEOhkEfesvGflYSHHqJ3f__WgL4vGqcv-SaMxTNwBqwo3RFg-wXKQIWBg4sB4HagfDHWXtjsx8RDOFFu9jG5pnPtnHJj4hfEagm9XxqxYVbDiwIq7xwxClI1BcEOknv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