While this is probably not in a state where it is ready to merge, I decided to submit this for review as per discussion with @RKSimon .
We want to move the DAGCombiner toward processing the node in topological order. Inf act, implementing such as change in the combiner itself is not a major challenge, and I have patches which can do so for quite some time now.
However, finding a path from were we are to where we want to be proved to be fairly challenging:
- Changing the order in which the nodes are processed affects the register allocator and instruction scheduling, so we get a ton of parasitic changes in the test suite.
- The combines, backends and test cases tends to overfit the current traversal order.
To quote @RKSimon:
You're hitting a much more severe version of the issues I encountered trying to improve the SimplifyDemandedBits folds, such as:
- different / missing canonicalization for some ops vs the InstCombine equivalent
- some weird orders of pattern matches that leave some folds almost impossible to hit
- some patterns need rewriting to use value tracking instead of matching specific instructions (usually sext/zext patterns of some kind)
- a lot of very custom DAG folds, usually for one target instruction pattern, in generic and target-specific combines, that seem to have been hacks from the very beginning (I know I'm guilty of that.....)
- the introduction of new instrinics abs/min/max etc. has meant that some of the DAG folds haven't been updated to account for them
- we have tests for IR that DAG shouldn't actually ever meet because the middle end should have canonicalized it away and the DAG won't generate those patterns.
- undef/poison issues are becoming more common as the middle end handling of poison matures
- early signs of bitrot in some targets - some targets get little attention, others like aarch64/amdgpu are transitioning away from DAG development to GISel (very very slowly.....)
It is obvious that there needs to be some agreed upons trategy to go through this.
This patch is only part of the change required, but is likely enough to notice the problem and come up with a plan.