Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
cmd/compile: use bounds from prove pass to simplify shifts #25087
Shifts generally require extra code to correctly handle shifting by more than the width of the value.
We have a bunch of ad hoc rewrite rules to detect and handle this. But the prove pass might be able to do a better job of this, and more simply too.
CL 109358 uses the prove pass to simplify CtzNN. Maybe we should do something similar for shifts. (And anything else?)
The main issue with implementing this is that prove only learn relations from "if" statements. Your CL about adding CtzNonZero was easy because you were looking into using some information that was being stated as part of a condition, so it was already there and only had to be looked up.
In this case, the majority of code that computes a non constant shift amount probably never goes through an if statement. I don't think it's common to see code like:
which is where prove could be useful today.
Having this kind of knowledge would require a full VRP pass, where you go through every instruction in the function and check whether you can learn something about each and every SSA var. I think @dr2chase attempted this in SSA even before SSA was merged.
This is correct (that I took a shot at this), it might be worth revisiting, maybe integrate it into a rewrite pass (e.g., do rewrites in some variant of reverse post order and/or dominator tree visit so that predecessors are all visited, and carry obvious conditions forward and apply them in rewrites).
referenced this issue
Apr 26, 2018
Did a quick hack-up of this, and it turns out there are some if statements that bound shifts. 35 in std+cmd, and more if #25115 is fixed. Will try to polish and mail. So many things to try to get mailed before the freeze! :(