This was proposed as an IR transform in D49306, but it was not clearly justifiable as a canonicalization. Here, we only do the transform when the target tells us that sqrt can be lowered with inline code.
This is the basic case. I noted the potential enhancements that I imagined with TODO comments:
- Generalize the transform for other exponents (allow more than 2 sqrt calcs if that's really cheaper).
- If we have less fast-math-flags, generate code to avoid -0.0 and/or INF.
- Allow the transform when optimizing/minimizing size (might require a target hook to get that right).
Note that by default x86 converts single-precision sqrt calcs into sqrt reciprocal estimate with refinement. That codegen is controlled by CPU attributes and can be manually overridden. We have plenty of test coverage for that already, so I didn't bother to include extra testing for that here. AArch uses its full-precision ops in all cases (not sure if that's the intended behavior or not, but that should also be covered by existing tests).
A follow-on patch can extend this to handle the other pattern that we deferred: pow(x,1/3) --> cbrt. But that requires a bit more work because we don't currently have a FCBRT DAG node defined.