Skip to content

Commit b2d43d2

Browse files
committed
Add optimization to conditionals that always/never jump.
1 parent 5784545 commit b2d43d2

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

src/compiler/peephole.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,49 @@ class peephole
13131313
del(3);
13141314
}
13151315
}
1316+
// x == 0 -> 1 / 0
1317+
if(mtok(0, "TOK_NUM") && mword(1) && mtok(2, "TOK_COMP_0"))
1318+
{
1319+
set_w(1, val(1) ? 1 : 0);
1320+
del(2);
1321+
continue;
1322+
}
1323+
// x / TOK_CNJUMP -> remove or always jump
1324+
if(mtok(0, "TOK_NUM") && mword(1) && mtok(2, "TOK_CNJUMP"))
1325+
{
1326+
if(!val(1))
1327+
{
1328+
del(3);
1329+
del(2);
1330+
del(1);
1331+
del(0);
1332+
}
1333+
else
1334+
{
1335+
set_tok(2, "TOK_JUMP");
1336+
del(1);
1337+
del(0);
1338+
}
1339+
continue;
1340+
}
1341+
// x / TOK_CJUMP -> remove or always jump
1342+
if(mtok(0, "TOK_NUM") && mword(1) && mtok(2, "TOK_CJUMP"))
1343+
{
1344+
if(val(1))
1345+
{
1346+
del(3);
1347+
del(2);
1348+
del(1);
1349+
del(0);
1350+
}
1351+
else
1352+
{
1353+
set_tok(2, "TOK_JUMP");
1354+
del(1);
1355+
del(0);
1356+
}
1357+
continue;
1358+
}
13161359
// CALL xxxxx / RETURN -> JUMP xxxxx
13171360
// TOK_CALL / x / TOK_RET -> TOK_JUMP / x
13181361
if(mtok(0, "TOK_CALL") && mtok(2, "TOK_RET"))

testsuite/tests/testloop.bas

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,31 @@ for i=1 to 10
9898
next i
9999
? "="; a
100100

101+
' Check "void" loop optimizations
102+
repeat
103+
? "repeat until 1"
104+
until 1
105+
106+
while 0
107+
? "while 0"
108+
wend
109+
110+
' Check "always" loop optimizations
111+
i = 0
112+
repeat
113+
? "repeat until 0"
114+
inc i
115+
if i > 4 then exit
116+
until 0
117+
118+
i = 0
119+
while 1
120+
? "while 1"
121+
inc i
122+
if i > 4 then exit
123+
wend
124+
125+
' Check IF/FOR special
126+
if 0 then ? "if 0"
127+
if 1 then ? "if 1"
128+
for I=1 to 0 : ? "for 0" : next

testsuite/tests/testloop.chk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,15 @@ Test nested FOR with EXIT
5151
9 10 =2
5252
10 =3
5353
=295
54+
repeat until 1
55+
repeat until 0
56+
repeat until 0
57+
repeat until 0
58+
repeat until 0
59+
repeat until 0
60+
while 1
61+
while 1
62+
while 1
63+
while 1
64+
while 1
65+
if 1

0 commit comments

Comments
 (0)