Description
cmd/compile/internal/ssa/gen/rulegen.go
is a program that generates the code for all the SSA rewrite rules.
The way it currently works, the code is almost always generated directly. This keeps the code simple, but also limits it quite a bit. For example, it's hard to tell if a variable will be used or not, so declarations like typ := &config.Types
tend to be followed by _ = typ
.
It also means we tend to write repetitive and verbose code. For example, these two if statements could be collapsed like if X || Y { break }
:
if z1.Op != OpAMD64SHRLconst {
break
}
if z1.AuxInt != 31 {
break
}
@josharian tried to do this last bit with string handling in https://go-review.googlesource.com/c/go/+/167438, but that turns into spaghetti code that noone can maintain.
There are other ways we could post-process the code to simplify it, too. For example, we could move declarations closer to their uses, and we could remove unused variable declarations instead of using so many _ = foo
safeguards.
We could use go/ast
for this, but that's probably too complex for our needs. The generated code only uses a tiny subset of Go, and we could have nodes for higher-level concepts like "rewrite this value".
The last step of the program would be to stringify the node tree, and pass it through go/format
as usual.
The purpose of this issue is first to gather feedback. If it sounds like a good idea, I'd like to begin work on this soon, hopefully before the freeze is here again. The first incremental step would probably be to rewrite rulegen to use the intermediate node tree, without changing the generated code at all.