Skip to content

Commit 60bffe2

Browse files
committed
[InstCombine] Handle commuted variant of sqrt transform
1 parent 83bf1d6 commit 60bffe2

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,14 +2758,17 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
27582758
// Note: We don't bother looking any deeper than this first level or for
27592759
// variations of this pattern because instcombine's visitFMUL and/or the
27602760
// reassociation pass should give us this form.
2761-
Value *OtherMul0, *OtherMul1;
2762-
if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) {
2763-
// Pattern: sqrt((x * y) * z)
2764-
if (OtherMul0 == OtherMul1 && cast<Instruction>(Op0)->isFast()) {
2765-
// Matched: sqrt((x * x) * z)
2766-
RepeatOp = OtherMul0;
2767-
OtherOp = Op1;
2768-
}
2761+
Value *MulOp;
2762+
if (match(Op0, m_FMul(m_Value(MulOp), m_Deferred(MulOp))) &&
2763+
cast<Instruction>(Op0)->isFast()) {
2764+
// Pattern: sqrt((x * x) * z)
2765+
RepeatOp = MulOp;
2766+
OtherOp = Op1;
2767+
} else if (match(Op1, m_FMul(m_Value(MulOp), m_Deferred(MulOp))) &&
2768+
cast<Instruction>(Op1)->isFast()) {
2769+
// Pattern: sqrt(z * (x * x))
2770+
RepeatOp = MulOp;
2771+
OtherOp = Op0;
27692772
}
27702773
}
27712774
if (!RepeatOp)

llvm/test/Transforms/InstCombine/fast-math.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,9 @@ define double @sqrt_intrinsic_three_args5(double %x, double %y) {
712712
define double @sqrt_intrinsic_three_args6(double %x, ptr %yp) {
713713
; CHECK-LABEL: @sqrt_intrinsic_three_args6(
714714
; CHECK-NEXT: [[Y:%.*]] = load double, ptr [[YP:%.*]], align 8
715-
; CHECK-NEXT: [[MUL:%.*]] = fmul fast double [[X:%.*]], [[X]]
716-
; CHECK-NEXT: [[MUL2:%.*]] = fmul fast double [[Y]], [[MUL]]
717-
; CHECK-NEXT: [[SQRT:%.*]] = call fast double @llvm.sqrt.f64(double [[MUL2]])
715+
; CHECK-NEXT: [[FABS:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
716+
; CHECK-NEXT: [[SQRT1:%.*]] = call fast double @llvm.sqrt.f64(double [[Y]])
717+
; CHECK-NEXT: [[SQRT:%.*]] = fmul fast double [[FABS]], [[SQRT1]]
718718
; CHECK-NEXT: ret double [[SQRT]]
719719
;
720720
%y = load double, ptr %yp ; thwart complexity-based canonicalization

0 commit comments

Comments
 (0)