[HWCleanup] If condition hoist + CaseZ opportunity
- Dominant language
- C++
- Stars
- 2.2k
- Forks
- 524
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 46
Description
The following firrtl is very common in many designs.
```scala
when a:
c[b] <= d
```
Currently the output is:
```sv
if(a&b==0)
c_0 <= d
if(a&b==1)
c_1 <= d
if(a&b==2)
c_2 <= d
— very long if chains —
```
Optimally, we want to get the following output:
```sv
if(a){
Casez b {
Case 0: c_0 <= d
Case 1: c_1 <= d
Case 2: c_2 <= d
…
}
}
```
I guess it is very hard to create a generic algorithm for this optimization so pragmatically it would be a good start only considering this shape which is very common. I think this algorithm can be implemented by 2 step procedure in HWCleanup.
1. By looking at successive if-statements, extract common conjunction condition values and hoist them to outer if-statement. This optimization is potentially useful to clean printfs or assertions.
```sv
if(a&b&c){ e1 }
if(a&c&d){ e2 }
==>
if (a&c) {
if(b){ e1 }
if(d){ e2 }
}
```
2. Detect successive if statements that have `%a == constant` as a first element of conjunction conditions and convert them into casez.
```sv
if (a) {
if(b==0){ e1 }
if(b==1){ e2 }
...
}
==>
if (a) {
casez (b)
0: e1
1: e2
...
}
```
Minimal example
```scala
circuit Test :
module Test :
input clock: Clock
input a : UInt<1>
input b : UInt<2>
input d : UInt<1>
reg c : UInt<1>[3], clock
when a:
c[b] <= d
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.