Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
peephole
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Taddeüs Kroes
peephole
Commits
f7e3d0c6
Commit
f7e3d0c6
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Implemented constant folding.
parent
d890c575
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/optimize/advanced.py
+73
-26
73 additions, 26 deletions
src/optimize/advanced.py
tests/test_optimize_advanced.py
+2
-1
2 additions, 1 deletion
tests/test_optimize_advanced.py
with
75 additions
and
27 deletions
src/optimize/advanced.py
+
73
−
26
View file @
f7e3d0c6
...
...
@@ -65,43 +65,90 @@ def to_hex(value):
def
fold_constants
(
block
):
"""
Constant folding:
- An immidiate load defines a register value:
li $reg, XX -> register[$reg] = XX
- Integer variable definition is of the following form:
li $reg, XX
sw $reg, VAR
save this as:
reg[$reg] = XX
constants[VAR] = XX
li $reg, XX -> constants[VAR] = XX
sw $reg, VAR -> register[$reg] = XX
- When a variable is used, the following happens:
lw $reg, VAR
save this as:
reg[$reg] = constants[VAR]
lw $reg, VAR -> register[$reg] = constants[VAR]
"""
found
=
False
# Variable values
constants
=
{}
reg
=
{}
# Current known values in register
register
=
{}
while
not
block
.
end
():
s
=
block
.
read
()
if
s
.
is_load
():
constants
[
s
[
0
]]
=
s
[
1
]
elif
s
.
is_command
()
and
len
(
s
)
==
3
:
d
,
s
,
t
=
s
if
not
s
.
is_command
():
continue
if
s
.
name
==
'
li
'
:
# Save value in register
register
[
s
[
0
]]
=
int
(
s
[
1
],
16
)
elif
s
.
name
==
'
move
'
and
s
[
0
]
in
register
:
reg_to
,
reg_from
=
s
if
reg_from
in
register
:
# Other value is also known, copy its value
register
[
reg_to
]
=
register
[
reg_to
]
else
:
# Other value is unknown, delete the value
del
register
[
reg_to
]
elif
s
.
name
==
'
sw
'
and
s
[
0
]
in
register
:
# Constant variable definition, e.g. 'int a = 1;'
constants
[
s
[
1
]]
=
register
[
s
[
0
]]
elif
s
.
name
==
'
lw
'
and
s
[
1
]
in
constants
:
# Usage of variable with constant value
register
[
s
[
0
]]
=
constants
[
s
[
1
]]
elif
s
.
name
in
[
'
addu
'
,
'
subu
'
,
'
mult
'
,
'
div
'
]:
# Calculation with constants
rd
,
rs
,
rt
=
s
rs_known
=
rs
in
register
rt_known
=
rt
in
register
if
rs_known
and
rt_known
:
# a = 5 -> b = 15
# c = 10
# b = a + c
rs_val
=
register
[
rs
]
rt_val
=
register
[
rt
]
if
s
in
constants
and
t
in
constants
:
if
s
.
name
==
'
addu
'
:
result
=
s
+
t
elif
s
.
name
==
'
subu
'
:
result
=
s
-
t
elif
s
.
name
==
'
mult
'
:
result
=
s
*
t
elif
s
.
name
==
'
div
'
:
result
=
s
/
t
block
.
replace
(
1
,
[
S
(
'
command
'
,
'
li
'
,
to_hex
(
result
))])
constants
[
d
]
=
result
#else:
result
=
to_hex
(
rs_val
+
rt_val
)
return
False
if
s
.
name
==
'
subu
'
:
result
=
to_hex
(
rs_val
-
rt_val
)
if
s
.
name
==
'
mult
'
:
result
=
to_hex
(
rs_val
*
rt_val
)
if
s
.
name
==
'
div
'
:
result
=
to_hex
(
rs_val
/
rt_val
)
block
.
replace
(
1
,
[
S
(
'
command
'
,
'
li
'
,
result
)])
register
[
rd
]
=
result
found
=
True
elif
rt_known
:
# c = 10 -> b = a + 10
# b = c + a
s
[
2
]
=
register
[
rt
]
found
=
True
elif
rs_known
and
s
.
name
in
[
'
addu
'
,
'
mult
'
]:
# a = 10 -> b = c + 10
# b = c + a
s
[
1
]
=
rt
s
[
2
]
=
register
[
rs
]
found
=
True
elif
len
(
s
)
and
s
[
0
]
in
register
:
# Known register is overwritten, remove its value
del
register
[
s
[
0
]]
return
found
def
copy_propagation
(
block
):
...
...
This diff is collapsed.
Click to expand it.
tests/test_optimize_advanced.py
+
2
−
1
View file @
f7e3d0c6
import
unittest
from
src.optimize.advanced
import
eliminate_common_subexpressions
from
src.optimize.advanced
import
eliminate_common_subexpressions
,
\
fold_constants
,
copy_propagation
from
src.statement
import
Statement
as
S
,
Block
as
B
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment