summaryrefslogtreecommitdiff
path: root/chapter24/src/test/java/com/seaofnodes/simple/Chapter11Test.java
blob: c56d914f04ae4435d8a95e416fffb1d567ec5d1f (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.print.IRPrinter;


import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class Chapter11Test {

    @Test
    public void testPrimes() {
        CodeGen code = new CodeGen(
"""
if( arg < 2 ) return 0;
int primeCount = 1;
int prime = 3;
while( prime <= arg ) {
    int isPrime = 1;
    // Check for even case, so the next loop need only check odds
    if( (prime/2)*2 == prime )
        continue;
    // Check odds up to sqrt of prime
    int j = 3;
    while( j*j <= prime ) {
        if( (prime/j)*j == prime ) {
            isPrime = 0;
            break;
        }
        j = j + 2;
    }
    if( isPrime )
        primeCount = primeCount + 1;
    prime = prime + 2;
}
return primeCount;
""");
        code.parse().opto();
        assertEquals("return Phi(Region,0,Phi(Loop,1,Phi(Region,Phi_primeCount,Phi_primeCount,(Phi_primeCount+1))));", code.print());
        assertEquals("0", Eval2.eval(code,  1)); // No primes 1 or below
        assertEquals("1", Eval2.eval(code,  2)); // 2
        assertEquals("2", Eval2.eval(code,  3)); // 2, 3
        assertEquals("2", Eval2.eval(code,  4)); // 2, 3
        assertEquals("3", Eval2.eval(code,  5)); // 2, 3, 5
        assertEquals("4", Eval2.eval(code, 10)); // 2, 3, 5, 7
    }

    @Test
    public void testAntiDeps1() {
        CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v=new S;
v.f = 2;
int i=new S.f;
i=v.f;
if (arg) v.f=1;
return i;
""");
        code.parse().opto();
        assertEquals("return 2;", code.print());
        assertEquals("2", Eval2.eval(code, 0));
        assertEquals("2", Eval2.eval(code, 1));
    }

    @Test
    public void testAntiDeps2() {
        CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
v.f = arg;
S !t = new S;
int i = 0;
if (arg) {
    if (arg+1) v = t;
    i = v.f;
} else {
    v.f = 2;
}
return i;
""");
        code.parse().opto();
        assertEquals("return Phi(Region,.f,0);", code.print());
    }

    @Test
    public void testAntiDeps3() {
        CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v0 = new S;
S? v1;
if (arg) v1 = new S;
if (v1) {
    v0.f = v1.f;
} else {
    v0.f = 2;
}
return v0;
""");
        code.parse().opto();
        assertEquals("return S;", code.print());
    }


    @Test
    public void testAntiDeps4() {
        CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
v.f = arg;
S t = new S;
int i = v.f;
if (arg+1) arg= 0;
while (arg) v.f = 2;
return i;
""");
        code.parse().opto();
        assertEquals("return arg;", code.print());
    }

    @Test
    public void testAntiDeps5() {
        CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
while(1) {
    while(arg+1) { arg=arg-1; }
    if (arg) break;
    v.f = 2;
}
return v;
""");
        code.parse().opto();
        assertEquals("return S;", code.print());
    }

    @Test
    public void testAntiDeps6() {
        CodeGen code = new CodeGen(
"""
struct s { int v; };
s !ptr=new s;
while( -arg )
  ptr = new s;
while(1)
  arg = arg+ptr.v;
""");
        code.parse().opto();
        assertEquals("return Top;", code.print());
    }

    @Test
    public void testAntiDeps7() {
        CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
S t = new S;
int i = v.f;
while (arg) {
    v.f = arg;
    arg = arg-1;
}
return i;
""");
        code.parse().opto();
        assertEquals("return 0;", code.print());
    }

    @Test
    public void testAntiDeps8() {
        CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
S t = new S;
while(arg) {
    arg=arg-1;
    int f = v.f;
    v.f = 2;
    if (arg) arg = f;
    else v.f = 3;
}
return arg;
""");
        code.parse().opto();
        assertEquals("return 0;", code.print());
    }

    @Test
    public void testAntiDeps9() {
        CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
S t = new S;
if (arg) {
    v.f=2;
    int i=t.f;
    v.f=i;
}
return v;
""");
        code.parse().opto();
        assertEquals("return S;", code.print());
    }

    @Test
    public void testExample2() {
        CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
int i = arg;
while (arg > 0) {
    int j = i/3;
    if (arg == 5)
        v.f = j;
    arg = arg - 1;
}
return v;
                """);
        code.parse().opto();
        //assertEquals("return new S;", code.print());
    }

    @Test
    public void testScheduleUse() {
        CodeGen code = new CodeGen(
"""
int v0=0;
while(0>=0) {
    u1 v1=0;
    v1=v0;
    if(v1*0)
        v0=-v1;
}
return 0;
""");
        code.parse().opto();
        assertEquals("return Top;", code.print());
    }

    @Test
    public void testLoopCarriedDep() {
        CodeGen code = new CodeGen(
"""
u32 v0=0;
{
    int v1=0;
    while(v1) {
        v1=1>>>v0!=0;
        v0=v1/0;
    }
    return v1;
}
""");
        code.parse().opto();
        assertEquals("return 0;", code.print());
    }

}