| 1 |
= CombineConversions |
| 2 |
|
| 3 |
<<CombineConversions#>> is an optimization pass for the <<SSA#>> |
| 4 |
<<IntermediateLanguage#>>, invoked from <<SSASimplify#>>. |
| 5 |
|
| 6 |
== Description |
| 7 |
|
| 8 |
This pass looks for and simplifies nested calls to (signed) |
| 9 |
extension/truncation. |
| 10 |
|
| 11 |
== Implementation |
| 12 |
|
| 13 |
* https://github.com/MLton/mlton/blob/master/mlton/ssa/combine-conversions.fun[`combine-conversions.fun`] |
| 14 |
|
| 15 |
== Details and Notes |
| 16 |
|
| 17 |
It processes each block in dfs order (visiting definitions before uses): |
| 18 |
|
| 19 |
* If the statement is not a `PrimApp` with `Word_extdToWord`, skip it. |
| 20 |
* After processing a conversion, it tags the `Var` for subsequent use. |
| 21 |
* When inspecting a conversion, check if the `Var` operand is also the |
| 22 |
result of a conversion. If it is, try to combine the two operations. |
| 23 |
Repeatedly simplify until hitting either a non-conversion `Var` or a |
| 24 |
case where the conversion cannot be simplified. |
| 25 |
|
| 26 |
The optimization rules are very simple: |
| 27 |
---- |
| 28 |
x1 = ... |
| 29 |
x2 = Word_extdToWord (W1, W2, {signed=s1}) x1 |
| 30 |
x3 = Word_extdToWord (W2, W3, {signed=s2}) x2 |
| 31 |
---- |
| 32 |
|
| 33 |
* If `W1 = W2`, then there is no conversions before `x_1`. |
| 34 |
+ |
| 35 |
This is guaranteed because `W2 = W3` will always trigger optimization. |
| 36 |
|
| 37 |
* Case `W1 ≤ W3 ≤ W2`: |
| 38 |
+ |
| 39 |
---- |
| 40 |
x3 = Word_extdToWord (W1, W3, {signed=s1}) x1 |
| 41 |
---- |
| 42 |
|
| 43 |
* Case `W1 < W2 < W3 AND ((NOT s1) OR s2)`: |
| 44 |
+ |
| 45 |
---- |
| 46 |
x3 = Word_extdToWord (W1, W3, {signed=s1}) x1 |
| 47 |
---- |
| 48 |
|
| 49 |
* Case `W1 = W2 < W3`: |
| 50 |
+ |
| 51 |
unoptimized, because there are no conversions past `W1` and `x2 = x1` |
| 52 |
|
| 53 |
* Case `W3 ≤ W2 ≤ W1 OR W3 ≤ W1 ≤ W2`: |
| 54 |
+ |
| 55 |
---- |
| 56 |
x_3 = Word_extdToWord (W1, W3, {signed=_}) x1 |
| 57 |
---- |
| 58 |
+ |
| 59 |
because `W3 ≤ W1 && W3 ≤ W2`, just clip `x1` |
| 60 |
|
| 61 |
* Case `W2 < W1 ≤ W3 OR W2 < W3 ≤ W1`: |
| 62 |
+ |
| 63 |
unoptimized, because `W2 < W1 && W2 < W3`, has truncation effect |
| 64 |
|
| 65 |
* Case `W1 < W2 < W3 AND (s1 AND (NOT s2))`: |
| 66 |
+ |
| 67 |
unoptimized, because each conversion affects the result separately |