Closed withzombies closed 6 years ago
Initial work here: 8edd7768e9983220047da3ee30ee680dcfe6fe4c
If you had a global variable of all SSAConcreteValue
s, then your constant folding could work as follows:
1) Add all users of all concrete values to a work list
2) Initialize a next work list, and a progress var (you have it called dirty
now)
3) For everything in our current work list:
i) Get the list of users of the thing
ii) Call peephole_optimize (I suggest renaming to fold_constant_expressions
).
iii) If folding happened, then extend the next work list with the users from (3.ii) and mark progress as being made
4) If progress was made, then set the work list to be the next work list from (2), and go to (2).
Peephole optimization is typically done on short sequences of instructions, e.g. converting an shl
or imul
and an add
with an lea
.
Your approach to constant folding is to prevent excess work, right? It shouldn't be difficult, I could just modify the SSAConcreteValue
constructor and destructor to manage the list membership.
The approach I'm using now works but has to loop over all the instructions dozens of times. I also have a max count to prevent infinite loops due to folding bugs.
There are many constant operations that we don't optimize.
Pretty much every address load has 2-5 operations that can be optimized: