Debian Codegraph @

doc/guide/src/KnownCase.adoc

raw
1 = KnownCase
2
3 <<KnownCase#>> is an optimization pass for the <<SSA#>>
4 <<IntermediateLanguage#>>, invoked from <<SSASimplify#>>.
5
6 == Description
7
8 This pass duplicates and simplifies `Case` transfers when the
9 constructor of the scrutinee is known.
10
11 Uses <<Restore#>>.
12
13 For example, the program
14 [source,sml]
15 ----
16 val rec last =
17 fn [] => 0
18 | [x] => x
19 | _ :: l => last l
20
21 val _ = 1 + last [2, 3, 4, 5, 6, 7]
22 ----
23
24 gives rise to the <<SSA#>> function
25
26 ----
27 fun last_0 (x_142) = loopS_1 ()
28 loopS_1 ()
29 loop_11 (x_142)
30 loop_11 (x_143)
31 case x_143 of
32 nil_1 => L_73 | ::_0 => L_74
33 L_73 ()
34 return global_5
35 L_74 (x_145, x_144)
36 case x_145 of
37 nil_1 => L_75 | _ => L_76
38 L_75 ()
39 return x_144
40 L_76 ()
41 loop_11 (x_145)
42 ----
43
44 which is simplified to
45
46 ----
47 fun last_0 (x_142) = loopS_1 ()
48 loopS_1 ()
49 case x_142 of
50 nil_1 => L_73 | ::_0 => L_118
51 L_73 ()
52 return global_5
53 L_118 (x_230, x_229)
54 L_74 (x_230, x_229, x_142)
55 L_74 (x_145, x_144, x_232)
56 case x_145 of
57 nil_1 => L_75 | ::_0 => L_114
58 L_75 ()
59 return x_144
60 L_114 (x_227, x_226)
61 L_74 (x_227, x_226, x_145)
62 ----
63
64 == Implementation
65
66 * https://github.com/MLton/mlton/blob/master/mlton/ssa/known-case.fun[`known-case.fun`]
67
68 == Details and Notes
69
70 One interesting aspect of <<KnownCase#>>, is that it often has the
71 effect of unrolling list traversals by one iteration, moving the
72 `nil`/`::` check to the end of the loop, rather than the beginning.