-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathCopyPropagationSpec.hs
163 lines (149 loc) · 3.97 KB
/
CopyPropagationSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
{-# LANGUAGE OverloadedStrings, QuasiQuotes, ViewPatterns #-}
module Transformations.Optimising.CopyPropagationSpec where
import Transformations.Optimising.CopyPropagation
import Test.Hspec
import Grin.TH
import Test.Test hiding (newVar)
import Test.Assertions
runTests :: IO ()
runTests = hspec spec
spec :: Spec
spec = do
testExprContextE $ \ctx -> do
it "left unit law" $ do
let before = [expr|
a1 <- pure 1
a2 <- pure a1
a3 <- pure a2
pure a3
|]
let after = [expr|
a1 <- pure 1
pure a1
|]
copyPropagation (ctx before) `sameAs` (ctx after)
it "simple value" $ do
let before = [expr|
a1 <- pure 1
a2 <- pure a1
a3 <- pure a2
case a2 of
#default -> pure a3
|]
let after = [expr|
a1 <- pure 1
case a1 of
#default -> pure a1
|]
copyPropagation (ctx before) `sameAs` (ctx after)
it "does not propagate literal values" $ do
let before = [expr|
a1 <- pure 1
a2 <- pure 1
pure a2
|]
let after = [expr|
a1 <- pure 1
a2 <- pure 1
pure a2
|]
copyPropagation (ctx before) `sameAs` (ctx after)
it "node value - node pattern" $ do
let before = [expr|
a1 <- pure 1
n1 <- pure (CNode a1 0)
n2 <- pure n1
(CNode a2 b1) <- pure n2
b2 <- pure b1
(CNode a3 0) <- pure (CNode a2 0)
pure (CNode a3 b2)
|]
let after = [expr|
a1 <- pure 1
n1 <- pure (CNode a1 0)
b1 <- pure 0
pure (CNode a1 b1)
|]
copyPropagation (ctx before) `sameAs` (ctx after)
it "node value - var pattern" $ do
let before = [expr|
a1 <- pure 1
b1 <- pure 0
n1 <- pure (CNode a1 b1)
a2 <- pure a1
n2 <- pure (CNode a2 b1)
case n2 of
#default -> pure n2
|]
let after = [expr|
a1 <- pure 1
b1 <- pure 0
n1 <- pure (CNode a1 b1)
n2 <- pure (CNode a1 b1)
case n2 of
#default -> pure n2
|]
copyPropagation (ctx before) `sameAs` (ctx after)
it "right unit law" $ do
let before = [expr|
a1 <- pure 1
b1 <- pure 0
n1 <- pure (CNode a1 b1)
(CNode 1 0) <- pure (CNode a1 b1)
0 <- pure 0
(CNode 1 0) <- pure (CNode 1 0)
pure n1
|]
let after = [expr|
a1 <- pure 1
b1 <- pure 0
n1 <- pure (CNode a1 b1)
pure n1
|]
copyPropagation (ctx before) `sameAs` (ctx after)
it "literal pattern" $ do
let before = [expr|
a1 <- pure 1
a2 <- pure a1
0 <- pure a2
1 <- pure a2
pure a2
|]
let after = [expr|
a1 <- pure 1
0 <- pure 1
pure a1
|]
copyPropagation (ctx before) `sameAs` (ctx after)
it "bugfix - node pattern - var (infinite loop)" $ do
let before = [expr|
p1 <- store (CNode 1 1)
v1 <- fetch p1
(CNode p2 p3) <- pure v1
pure ()
|]
let after = [expr|
p1 <- store (CNode 1 1)
v1 <- fetch p1
(CNode p2 p3) <- pure v1
pure ()
|]
copyPropagation (ctx before) `sameAs` (ctx after)
it "node pattern mismatch" $ do
let before = [expr|
n1 <- pure (CPair 1 1)
(CNode v1 v2) <- pure n1
(CNode 1 1) <- pure n1
(CPair v3 v4) <- pure n1
(CPair 1 1) <- pure n1
pure ()
|]
let after = [expr|
n1 <- pure (CPair 1 1)
(CNode v1 v2) <- pure n1
(CNode 1 1) <- pure n1
v3 <- pure 1
v4 <- pure 1
pure ()
|]
copyPropagation (ctx before) `sameAs` (ctx after)