Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
peephole
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
peephole
Commits
e755a9f6
Commit
e755a9f6
authored
Dec 30, 2011
by
Richard Torenvliet
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of github.com:taddeus/peephole
parents
31e9202c
069c57ce
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
788 additions
and
745 deletions
+788
-745
benchmarks/optimized/acron.s
benchmarks/optimized/acron.s
+223
-221
benchmarks/optimized/dhrystone.s
benchmarks/optimized/dhrystone.s
+473
-476
src/liveness.py
src/liveness.py
+23
-0
src/optimize/advanced.py
src/optimize/advanced.py
+67
-48
tests/test_statement.py
tests/test_statement.py
+2
-0
No files found.
benchmarks/optimized/acron.s
View file @
e755a9f6
.
file
1
"acron.c"
#
GNU
C
2
.7.2.3
[
AL
1
.1
,
MM
40
,
tma
0
.1
]
SimpleScalar
running
sstrix
compiled
by
GNU
C
#
Cc1
defaults
:
#
-
mgas
-
mgpOPT
#
Cc1
arguments
(-
G
value
=
8
,
Cpu
=
default
,
ISA
=
1
)
:
#
-
quiet
-
dumpbase
-
O0
-
o
gcc2_compiled
.:
__gnu_compiled_c
:
.
globl
w
...
...
@@ -72,25 +74,25 @@ is_vowel:
sb
$
3
,
0
(
$fp
)
move
$
2
,
$
0
lb
$
4
,
0
(
$fp
)
li
$
5
,
0x00000041
#
65
li
$
5
,
0x00000041
#
$
5
=
65
beq
$
4
,
$
5
,
$L3
lb
$
4
,
0
(
$fp
)
li
$
5
,
0x00000045
#
69
li
$
5
,
0x00000045
#
$
5
=
69
beq
$
4
,
$
5
,
$L3
lb
$
4
,
0
(
$fp
)
li
$
5
,
0x00000049
#
73
li
$
5
,
0x00000049
#
$
5
=
73
beq
$
4
,
$
5
,
$L3
lb
$
4
,
0
(
$fp
)
li
$
5
,
0x0000004f
#
79
li
$
5
,
0x0000004f
#
$
5
=
79
beq
$
4
,
$
5
,
$L3
lb
$
4
,
0
(
$fp
)
li
$
5
,
0x00000055
#
85
li
$
5
,
0x00000055
#
$
5
=
85
beq
$
4
,
$
5
,
$L3
lb
$
4
,
0
(
$fp
)
li
$
5
,
0x00000059
#
89
li
$
5
,
0x00000059
#
$
5
=
89
bne
$
4
,
$
5
,
$L2
$L3
:
li
$
2
,
0x00000001
#
1
li
$
2
,
0x00000001
#
$
2
=
1
$L2
:
j
$L1
$L1
:
...
...
@@ -117,7 +119,7 @@ do_perm:
sw
$
7
,
68
(
$fp
)
sw
$
0
,
24
(
$fp
)
lw
$
2
,
64
(
$fp
)
li
$
3
,
0x00000001
#
1
li
$
3
,
0x00000001
#
$
3
=
1
bne
$
2
,
$
3
,
$L5
lw
$
2
,
pindex
move
$
3
,
$
2
...
...
@@ -182,7 +184,7 @@ $L5:
sw
$
2
,
24
(
$fp
)
beq
$
2
,
$
0
,
$L8
lw
$
2
,
24
(
$fp
)
li
$
3
,
0x00000003
#
3
li
$
3
,
0x00000003
#
$
3
=
3
bne
$
2
,
$
3
,
$L7
$L8
:
j
$L4
...
...
@@ -222,8 +224,8 @@ $L13:
sll
$
2
,
$
3
,
2
lw
$
3
,
60
(
$fp
)
addu
$
2
,
$
2
,
$
3
li
$
3
,
0x00000001
#
1
sw
$
3
,
0
(
$
2
)
li
$
3
,
0x00000001
#
$
3
=
1
sw
$
3
,
0
(
$
2
)
#
0
(
$
2
)
=
1
lw
$
4
,
16
(
$fp
)
lw
$
5
,
60
(
$fp
)
lw
$
6
,
64
(
$fp
)
...
...
@@ -358,8 +360,8 @@ main:
sw
$fp
,
24
(
$sp
)
move
$fp
,
$sp
jal
__main
li
$
2
,
0x00000004
#
4
sw
$
2
,
20
(
$fp
)
li
$
2
,
0x00000004
#
$
2
=
4
sw
$
2
,
20
(
$fp
)
#
20
(
$fp
)
=
4
$L28
:
lw
$
2
,
20
(
$fp
)
slt
$
3
,
$
2
,
7
...
...
@@ -376,8 +378,8 @@ $L35:
sll
$
2
,
$
3
,
2
la
$
3
,
done
addu
$
2
,
$
2
,
$
3
li
$
3
,
0x00000001
#
1
sw
$
3
,
0
(
$
2
)
li
$
3
,
0x00000001
#
$
3
=
1
sw
$
3
,
0
(
$
2
)
#
0
(
$
2
)
=
1
lw
$
4
,
16
(
$fp
)
la
$
5
,
done
move
$
6
,
$
0
...
...
benchmarks/optimized/dhrystone.s
View file @
e755a9f6
.
file
1
"dhrystone.c"
#
GNU
C
2
.7.2.3
[
AL
1
.1
,
MM
40
,
tma
0
.1
]
SimpleScalar
running
sstrix
compiled
by
GNU
C
#
Cc1
defaults
:
#
-
mgas
-
mgpOPT
#
Cc1
arguments
(-
G
value
=
8
,
Cpu
=
default
,
ISA
=
1
)
:
#
-
quiet
-
dumpbase
-
O0
-
o
gcc2_compiled
.:
__gnu_compiled_c
:
.
globl
Version
...
...
@@ -96,7 +98,7 @@ Proc0:
sw
$fp
,
140
(
$sp
)
sw
$
16
,
136
(
$sp
)
move
$fp
,
$sp
addu
$
4
,
$
f
p
,
120
addu
$
4
,
$
s
p
,
120
jal
times
lw
$
2
,
120
(
$fp
)
sw
$
2
,
104
(
$fp
)
...
...
@@ -115,24 +117,23 @@ $L4:
lw
$
3
,
104
(
$fp
)
subu
$
2
,
$
2
,
$
3
sw
$
2
,
112
(
$fp
)
li
$
4
,
0x00000030
#
48
li
$
4
,
0x00000030
#
$
4
=
48
jal
malloc
sw
$
2
,
PtrGlbNext
li
$
4
,
0x00000030
#
48
li
$
4
,
0x00000030
#
$
4
=
48
jal
malloc
sw
$
2
,
PtrGlb
lw
$
2
,
PtrGlb
lw
$
3
,
PtrGlbNext
sw
$
3
,
0
(
$
2
)
lw
$
2
,
PtrGlb
sw
$
0
,
4
(
$
2
)
lw
$
2
,
PtrGlb
li
$
3
,
0x00000002
#
2
sw
$
3
,
8
(
$
2
)
li
$
3
,
0x00000002
#
$
3
=
2
sw
$
3
,
8
(
$
2
)
#
8
(
$
2
)
=
2
lw
$
2
,
PtrGlb
li
$
3
,
0x00000028
#
40
sw
$
3
,
12
(
$
2
)
lw
$
3
,
PtrGlb
li
$
3
,
0x00000028
#
$
3
=
40
sw
$
3
,
12
(
$
2
)
#
12
(
$
2
)
=
40
lw
$
3
,
PtrGlb
#
$
3
=
unknown
addu
$
2
,
$
3
,
16
move
$
4
,
$
2
la
$
5
,
$LC0
...
...
@@ -141,8 +142,8 @@ $L4:
move
$
4
,
$
2
la
$
5
,
$LC1
jal
strcpy
li
$
2
,
0x0000000a
#
10
sw
$
2
,
Array2Glob
+
166
0
li
$
2
,
0x0000000a
#
$
2
=
10
sw
$
2
,
Array2Glob
+
1660
#
Array2Glob
+
1660
=
1
0
addu
$
4
,
$fp
,
120
jal
times
lw
$
2
,
120
(
$fp
)
...
...
@@ -154,16 +155,16 @@ $L7:
$L10
:
jal
Proc5
jal
Proc4
li
$
2
,
0x00000002
#
2
sw
$
2
,
16
(
$fp
)
li
$
2
,
0x00000003
#
3
sw
$
2
,
20
(
$fp
)
li
$
2
,
0x00000002
#
$
2
=
2
sw
$
2
,
16
(
$fp
)
#
16
(
$fp
)
=
2
li
$
2
,
0x00000003
#
$
2
=
3
sw
$
2
,
20
(
$fp
)
#
20
(
$fp
)
=
3
addu
$
2
,
$fp
,
72
move
$
4
,
$
2
la
$
5
,
$LC2
jal
strcpy
li
$
2
,
0x00000001
#
1
sw
$
2
,
32
(
$fp
)
li
$
2
,
0x00000001
#
$
2
=
1
sw
$
2
,
32
(
$fp
)
#
32
(
$fp
)
=
1
addu
$
2
,
$fp
,
40
addu
$
3
,
$fp
,
72
move
$
4
,
$
2
...
...
@@ -179,8 +180,8 @@ $L11:
beq
$
2
,
$
0
,
$L12
$L13
:
lw
$
2
,
16
(
$fp
)
move
$
4
,
$
2
sll
$
3
,
$
4
,
2
#
Dead
code
:
move
$
4
,
$
2
sll
$
3
,
$
2
,
2
addu
$
3
,
$
3
,
$
2
lw
$
2
,
20
(
$fp
)
subu
$
3
,
$
3
,
$
2
...
...
@@ -203,7 +204,7 @@ $L12:
jal
Proc8
lw
$
4
,
PtrGlb
jal
Proc1
li
$
2
,
0x00000041
#
65
li
$
2
,
0x00000041
#
$
2
=
65
sb
$
2
,
29
(
$fp
)
$L14
:
lb
$
2
,
29
(
$fp
)
...
...
@@ -213,7 +214,7 @@ $L14:
$L17
:
lb
$
2
,
29
(
$fp
)
move
$
4
,
$
2
li
$
5
,
0x00000043
#
67
li
$
5
,
0x00000043
#
$
5
=
67
jal
Func1
lw
$
3
,
32
(
$fp
)
bne
$
3
,
$
2
,
$L18
...
...
@@ -234,15 +235,14 @@ $L15:
mult
$
2
,
$
3
mflo
$
2
sw
$
2
,
24
(
$fp
)
lw
$
2
,
24
(
$fp
)
lw
$
3
,
16
(
$fp
)
div
$
2
,
$
2
,
$
3
sw
$
2
,
20
(
$fp
)
lw
$
2
,
24
(
$fp
)
lw
$
3
,
20
(
$fp
)
subu
$
2
,
$
2
,
$
3
move
$
4
,
$
2
sll
$
3
,
$
4
,
3
#
Dead
code
:
move
$
4
,
$
2
sll
$
3
,
$
2
,
3
subu
$
3
,
$
3
,
$
2
lw
$
2
,
16
(
$fp
)
subu
$
3
,
$
3
,
$
2
...
...
@@ -261,22 +261,21 @@ $L8:
lw
$
3
,
112
(
$fp
)
subu
$
2
,
$
2
,
$
3
sw
$
2
,
108
(
$fp
)
lw
$
2
,
108
(
$fp
)
li
$
6
,
0x51eb851f
#
1374389535
li
$
6
,
0x51eb851f
#
$
6
=
1374389535
mult
$
2
,
$
6
mfhi
$
5
mflo
$
4
srl
$
6
,
$
5
,
0
move
$
7
,
$
0
#
Dead
code
:
mflo
$
4
srl
$
6
,
$
5
,
0
#
$
6
=
unknown
#
Dead
code
:
move
$
7
,
$
0
sra
$
3
,
$
6
,
5
sra
$
4
,
$
2
,
31
subu
$
2
,
$
3
,
$
4
la
$
4
,
$LC3
la
$
5
,
Version
li
$
6
,
0x00001388
#
5000
li
$
6
,
0x00001388
#
$
6
=
5000
move
$
7
,
$
2
jal
printf
li
$
2
,
0x0007a120
#
500000
li
$
2
,
0x0007a120
#
$
2
=
500000
lw
$
3
,
108
(
$fp
)
div
$
2
,
$
2
,
$
3
la
$
4
,
$LC4
...
...
@@ -306,12 +305,12 @@ Proc1:
lw
$
2
,
PtrGlb
move
$
4
,
$
3
move
$
5
,
$
2
li
$
6
,
0x00000030
#
48
li
$
6
,
0x00000030
#
$
6
=
48
jal
memcpy
lw
$
2
,
24
(
$fp
)
li
$
3
,
0x00000005
#
5
sw
$
3
,
12
(
$
2
)
lw
$
3
,
24
(
$fp
)
li
$
3
,
0x00000005
#
$
3
=
5
sw
$
3
,
12
(
$
2
)
#
12
(
$
2
)
=
5
lw
$
3
,
24
(
$fp
)
#
$
3
=
unknown
lw
$
2
,
0
(
$
3
)
lw
$
3
,
24
(
$fp
)
lw
$
4
,
12
(
$
3
)
...
...
@@ -331,9 +330,9 @@ Proc1:
bne
$
2
,
$
0
,
$L20
lw
$
2
,
24
(
$fp
)
lw
$
3
,
0
(
$
2
)
li
$
2
,
0x00000006
#
6
sw
$
2
,
12
(
$
3
)
lw
$
2
,
24
(
$fp
)
li
$
2
,
0x00000006
#
$
2
=
6
sw
$
2
,
12
(
$
3
)
#
12
(
$
3
)
=
6
lw
$
2
,
24
(
$fp
)
#
$
2
=
unknown
lw
$
3
,
24
(
$fp
)
lw
$
4
,
0
(
$
3
)
addu
$
3
,
$
4
,
8
...
...
@@ -351,7 +350,7 @@ Proc1:
lw
$
4
,
0
(
$
3
)
addu
$
3
,
$
4
,
12
lw
$
4
,
12
(
$
2
)
li
$
5
,
0x0000000a
#
10
li
$
5
,
0x0000000a
#
$
5
=
10
move
$
6
,
$
3
jal
Proc7
j
$L21
...
...
@@ -362,7 +361,7 @@ $L20:
move
$
3
,
$
4
move
$
4
,
$
2
move
$
5
,
$
3
li
$
6
,
0x00000030
#
48
li
$
6
,
0x00000030
#
$
6
=
48
jal
memcpy
$L21
:
$L19
:
...
...
@@ -388,7 +387,7 @@ Proc2:
sw
$
2
,
0
(
$fp
)
$L23
:
lb
$
2
,
Char1Glob
li
$
3
,
0x00000041
#
65
li
$
3
,
0x00000041
#
$
3
=
65
bne
$
2
,
$
3
,
$L26
lw
$
3
,
0
(
$fp
)
subu
$
2
,
$
3
,
1
...
...
@@ -432,12 +431,12 @@ Proc3:
sw
$
4
,
0
(
$
2
)
j
$L30
$L29
:
li
$
2
,
0x00000064
#
100
sw
$
2
,
IntGlob
li
$
2
,
0x00000064
#
$
2
=
100
sw
$
2
,
IntGlob
#
IntGlob
=
100
$L30
:
lw
$
3
,
PtrGlb
addu
$
2
,
$
3
,
12
li
$
4
,
0x0000000a
#
10
li
$
4
,
0x0000000a
#
$
4
=
10
lw
$
5
,
IntGlob
move
$
6
,
$
2
jal
Proc7
...
...
@@ -461,11 +460,10 @@ Proc4:
xori
$
3
,
$
2
,
0x0041
sltu
$
2
,
$
3
,
1
sw
$
2
,
0
(
$fp
)
lw
$
2
,
0
(
$fp
)
lw
$
3
,
BoolGlob
or
$
2
,
$
2
,
$
3
sw
$
2
,
0
(
$fp
)
li
$
2
,
0x00000042
#
66
li
$
2
,
0x00000042
#
$
2
=
66
sb
$
2
,
Char2Glob
$L31
:
move
$sp
,
$fp
#
sp
not
trusted
here
...
...
@@ -482,7 +480,7 @@ Proc5:
subu
$sp
,
$sp
,
8
sw
$fp
,
0
(
$sp
)
move
$fp
,
$sp
li
$
2
,
0x00000041
#
65
li
$
2
,
0x00000041
#
$
2
=
65
sb
$
2
,
Char1Glob
sw
$
0
,
BoolGlob
$L32
:
...
...
@@ -510,8 +508,8 @@ Proc6:
jal
Func3
bne
$
2
,
$
0
,
$L34
lw
$
2
,
28
(
$fp
)
li
$
3
,
0x00000003
#
3
sw
$
3
,
0
(
$
2
)
li
$
3
,
0x00000003
#
$
3
=
3
sw
$
3
,
0
(
$
2
)
#
0
(
$
2
)
=
3
$L34
:
lw
$
2
,
24
(
$fp
)
sltu
$
3
,
$
2
,
5
...
...
@@ -546,21 +544,21 @@ $L37:
j
$L39
$L38
:
lw
$
2
,
28
(
$fp
)
li
$
3
,
0x00000003
#
3
sw
$
3
,
0
(
$
2
)
li
$
3
,
0x00000003
#
$
3
=
3
sw
$
3
,
0
(
$
2
)
#
0
(
$
2
)
=
3
$L39
:
j
$L35
$L40
:
lw
$
2
,
28
(
$fp
)
li
$
3
,
0x00000001
#
1
sw
$
3
,
0
(
$
2
)
li
$
3
,
0x00000001
#
$
3
=
1
sw
$
3
,
0
(
$
2
)
#
0
(
$
2
)
=
1
j
$L35
$L41
:
j
$L35
$L42
:
lw
$
2
,
28
(
$fp
)
li
$
3
,
0x00000002
#
2
sw
$
3
,
0
(
$
2
)
li
$
3
,
0x00000002
#
$
3
=
2
sw
$
3
,
0
(
$
2
)
#
0
(
$
2
)
=
2
$L44
:
$L35
:
$L33
:
...
...
@@ -654,7 +652,7 @@ $L50:
move
$
4
,
$
2
sll
$
2
,
$
4
,
2
move
$
5
,
$
3
sll
$
4
,
$
5
,
1
sll
$
4
,
$
3
,
1
addu
$
4
,
$
4
,
$
3
sll
$
3
,
$
4
,
4
addu
$
4
,
$
4
,
$
3
...
...
@@ -671,26 +669,26 @@ $L49:
sw
$
3
,
4
(
$fp
)
j
$L47
$L48
:
#
Dead
code
:
lw
$
3
,
0
(
$fp
)
#
Dead
code
:
lw
$
2
,
0
(
$fp
)
#
Dead
code
:
move
$
4
,
$
2
#
Dead
code
:
sll
$
2
,
$
4
,
2
#
Dead
code
:
move
$
5
,
$
3
#
Dead
code
:
sll
$
4
,
$
3
,
1
#
Dead
code
:
addu
$
4
,
$
4
,
$
3
#
Dead
code
:
sll
$
3
,
$
4
,
4
#
Dead
code
:
addu
$
4
,
$
4
,
$
3
#
Dead
code
:
sll
$
3
,
$
4
,
2
#
Dead
code
:
lw
$
4
,
20
(
$fp
)
#
Dead
code
:
addu
$
3
,
$
3
,
$
4
#
Dead
code
:
addu
$
4
,
$
2
,
$
3
#
Dead
code
:
subu
$
2
,
$
4
,
4
lw
$
3
,
0
(
$fp
)
lw
$
2
,
0
(
$fp
)
move
$
4
,
$
2
sll
$
2
,
$
4
,
2
move
$
5
,
$
3
sll
$
4
,
$
5
,
1
addu
$
4
,
$
4
,
$
3
sll
$
3
,
$
4
,
4
addu
$
4
,
$
4
,
$
3
sll
$
3
,
$
4
,
2
lw
$
4
,
20
(
$fp
)
addu
$
3
,
$
3
,
$
4
addu
$
4
,
$
2
,
$
3
subu
$
2
,
$
4
,
4
lw
$
3
,
0
(
$fp
)
lw
$
2
,
0
(
$fp
)
move
$
4
,
$
2
sll
$
2
,
$
4
,
2
move
$
5
,
$
3
sll
$
4
,
$
5
,
1
#
Dead
code
:
move
$
5
,
$
3
sll
$
4
,
$
3
,
1
addu
$
4
,
$
4
,
$
3
sll
$
3
,
$
4
,
4
addu
$
4
,
$
4
,
$
3
...
...
@@ -704,7 +702,7 @@ $L48:
move
$
5
,
$
3
sll
$
3
,
$
5
,
2
move
$
6
,
$
4
sll
$
5
,
$
6
,
1
sll
$
5
,
$
4
,
1
addu
$
5
,
$
5
,
$
4
sll
$
4
,
$
5
,
4
addu
$
5
,
$
5
,
$
4
...
...
@@ -738,8 +736,8 @@ $L48:
addu
$
3
,
$
3
,
$
4
lw
$
4
,
0
(
$
3
)
sw
$
4
,
0
(
$
2
)
li
$
2
,
0x00000005
#
5
sw
$
2
,
IntGlob
li
$
2
,
0x00000005
#
$
2
=
5
sw
$
2
,
IntGlob
#
IntGlob
=
5
$L46
:
move
$sp
,
$fp
#
sp
not
trusted
here
lw
$fp
,
8
(
$sp
)
...
...
@@ -770,7 +768,7 @@ Func1:
j
$L51
j
$L53
$L52
:
li
$
2
,
0x00000001
#
1
li
$
2
,
0x00000001
#
$
2
=
1
j
$L51
$L53
:
$L51
:
...
...
@@ -791,8 +789,8 @@ Func2:
move
$fp
,
$sp
sw
$
4
,
32
(
$fp
)
sw
$
5
,
36
(
$fp
)
li
$
2
,
0x00000001
#
1
sw
$
2
,
16
(
$fp
)
li
$
2
,
0x00000001
#
$
2
=
1
sw
$
2
,
16
(
$fp
)
#
16
(
$fp
)
=
1
$L55
:
lw
$
2
,
16
(
$fp
)
slt
$
3
,
$
2
,
2
...
...
@@ -811,7 +809,7 @@ $L57:
move
$
5
,
$
3
jal
Func1
bne
$
2
,
$
0
,
$L58
li
$
2
,
0x00000041
#
65
li
$
2
,
0x00000041
#
$
2
=
65
sb
$
2
,
20
(
$fp
)
lw
$
3
,
16
(
$fp
)
addu
$
2
,
$
3
,
1
...
...
@@ -826,13 +824,13 @@ $L56:
lb
$
2
,
20
(
$fp
)
slt
$
3
,
$
2
,
91
beq
$
3
,
$
0
,
$L59
li
$
2
,
0x00000007
#
7
sw
$
2
,
16
(
$fp
)
li
$
2
,
0x00000007
#
$
2
=
7
sw
$
2
,
16
(
$fp
)
#
16
(
$fp
)
=
7
$L59
:
lb
$
2
,
20
(
$fp
)
li
$
3
,
0x00000058
#
88
li
$
3
,
0x00000058
#
$
3
=
88
bne
$
2
,
$
3
,
$L60
li
$
2
,
0x00000001
#
1
li
$
2
,
0x00000001
#
$
2
=
1
j
$L54
j
$L61
$L60
:
...
...
@@ -843,7 +841,7 @@ $L60:
lw
$
2
,
16
(
$fp
)
addu
$
3
,
$
2
,
7
sw
$
3
,
16
(
$fp
)
li
$
2
,
0x00000001
#
1
li
$
2
,
0x00000001
#
$
2
=
1
j
$L54
j
$L63
$L62
:
...
...
@@ -870,10 +868,9 @@ Func3:
sw
$
4
,
16
(
$fp
)
lw
$
2
,
16
(
$fp
)
sw
$
2
,
0
(
$fp
)
lw
$
2
,
0
(
$fp
)
li
$
3
,
0x00000002
#
2
li
$
3
,
0x00000002
#
$
3
=
2
bne
$
2
,
$
3
,
$L65
li
$
2
,
0x00000001
#
1
li
$
2
,
0x00000001
#
$
2
=
1
j
$L64
$L65
:
move
$
2
,
$
0
...
...
src/liveness.py
View file @
e755a9f6
from
copy
import
copy
RESERVED_REGISTERS
=
[
'$fp'
,
'$sp'
]
def
is_reg_dead_after
(
reg
,
block
,
index
):
"""Check if a register is dead after a certain point in a basic block."""
if
reg
in
RESERVED_REGISTERS
:
return
False
if
index
<
len
(
block
)
-
1
:
for
s
in
block
[
index
+
1
:]:
# If used, the previous definition is live
if
s
.
uses
(
reg
):
return
False
# If redefined, the previous definition is dead
if
s
.
defines
(
reg
):
return
True
# If dead within the same block, check if the register is in the block's
# live_out set
return
reg
not
in
block
.
live_out
def
create_use_def
(
block
):
used
=
set
()
defined
=
set
()
...
...
src/optimize/advanced.py
View file @
e755a9f6
from
src.statement
import
Statement
as
S
from
math
import
log
from
src.statement
import
Statement
as
S
from
src.liveness
import
is_reg_dead_after
def
reg_can_be_used_in
(
reg
,
block
,
start
,
end
):
"""Check if a register addres safely be used in a block section using local
...
...
@@ -173,14 +175,11 @@ def fold_constants(block):
# Move of `Hi' register to another register
register
[
s
[
0
]]
=
register
[
'$hi'
]
known
.
append
((
s
[
0
],
register
[
s
[
0
]]))
elif
s
.
name
in
[
'mult'
,
'div'
]
\
and
s
[
0
]
in
register
and
s
[
1
]
in
register
:
elif
s
.
name
==
'mult'
and
s
[
0
]
in
register
and
s
[
1
]
in
register
:
# Multiplication/division with constants
print
s
rs
,
rt
=
s
a
,
b
=
register
[
rs
],
register
[
rt
]
if
s
.
name
==
'mult'
:
if
not
a
or
not
b
:
# Multiplication by 0
hi
=
lo
=
to_hex
(
0
)
...
...
@@ -210,13 +209,11 @@ def fold_constants(block):
block
.
replace
(
1
,
[
S
(
'command'
,
'li'
,
'$hi'
,
hi
),
S
(
'command'
,
'li'
,
'$lo'
,
li
)],
message
=
message
)
elif
s
.
name
==
'div'
:
lo
,
hi
=
divmod
(
rs
,
rt
)
register
[
'$lo'
],
register
[
'$hi'
]
=
lo
,
hi
known
+=
[(
'$lo'
,
lo
),
(
'$hi'
,
hi
)]
changed
=
True
elif
s
.
name
in
[
'addu'
,
'subu'
]:
elif
s
.
name
in
[
'addu'
,
'subu'
,
'div'
]:
# Addition/subtraction with constants
rd
,
rs
,
rt
=
s
rs_known
=
rs
in
register
...
...
@@ -240,6 +237,11 @@ def fold_constants(block):
message
=
'Constant subtraction: %d - %d = %d'
\
%
(
rs_val
,
rt_val
,
result
)
if
s
.
name
==
'div'
:
result
=
rs_val
/
rt_val
message
=
'Constant division: %d - %d = %d'
\
%
(
rs_val
,
rt_val
,
result
)
block
.
replace
(
1
,
[
S
(
'command'
,
'li'
,
rd
,
to_hex
(
result
))],
message
=
message
)
register
[
rd
]
=
result
...
...
@@ -393,25 +395,42 @@ def eliminate_dead_code(block):
is not used in the rest of the block, and is not in the `out' set of the
block.
"""
# TODO: Finish
changed
=
False
unused
=
set
()
for
s
in
reversed
(
block
):
for
n
,
s
in
enumerate
(
block
):
for
reg
in
s
.
get_def
():
if
reg
in
unused
:
if
is_reg_dead_after
(
reg
,
block
,
n
)
:
# Statement is redefined later, so this statement is useless
if
block
.
debug
:
s
.
stype
=
'comment'
s
.
options
[
'block'
]
=
False
s
.
name
=
' Dead code: %s %s'
\
%
(
s
.
name
,
', '
.
join
(
map
(
str
,
s
)))
s
.
options
[
''
]
=
False
s
.
name
=
' Dead:
\
t
%s
\
t
%s
\
t
(dead register %s)'
\
%
(
s
.
name
,
','
.
join
(
map
(
str
,
s
)),
reg
)
else
:
s
.
remove
=
True
else
:
unused
.
add
(
reg
)
unused
-=
set
(
s
.
get_use
())
changed
=
True
#unused = set()
#for s in reversed(block):
# for reg in s.get_def():
# if reg in unused:
# # Statement is redefined later, so this statement is useless
# if block.debug:
# s.stype = 'comment'
# s.options['block'] = False
# s.name = ' Dead:\t%s\t%s' \
# % (s.name, ','.join(map(str, s)))
# else:
# s.remove = True
# changed = True
# else:
# unused.add(reg)
# unused -= set(s.get_use())
if
not
block
.
debug
:
block
.
apply_filter
(
lambda
s
:
not
hasattr
(
s
,
'remove'
))
...
...
tests/test_statement.py
View file @
e755a9f6
...
...
@@ -148,6 +148,8 @@ class TestStatement(unittest.TestCase):
arg2
)
self
.
assertEqual
(
S
(
'command'
,
's.s'
,
'$1'
,
'10($2)'
).
get_use
(),
\
arg2
)
self
.
assertEqual
(
S
(
'command'
,
'sw'
,
'$1'
,
'10($2)'
).
get_use
(),
\
arg2
)
self
.
assertEqual
(
S
(
'command'
,
'sb'
,
'$1'
,
'10($2)'
).
get_use
(),
\
arg2
)
self
.
assertEqual
(
S
(
'command'
,
'mtc1'
,
'$1'
,
'$2'
).
get_use
(),
arg1
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment