I did some experimenting with doing this transform here: 655376: cmd/compile: loop rotate for bottom testing |
https://go-review.googlesource.com/c/go/+/655376 . Current version maybe would need some limiting by heuristic, to avoid code size increase. It is also before prove and does not support multi exit with breaks (but exits through panics from bound checks should work fine). The most challenging thing seems to be that subsequent passes do not expect such form and may need update (I did some updates to schedule, loopbce but some still need to be done, probably nil check opt, register allocator (at least sometimes I noticed the shuffling code does not work well in transformed loop, adding unnecessary copies). And I guess my change broke the downwards counting loop transform in prove as well 🙂
Feel free to cherry pick changes from it, or I could update/extract any changes if they work in your case.