Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -8369,8 +8369,11 @@ SmallVector Users; // Find all FDIV users of the same divisor. for (auto *U : N1->uses()) { - if (U->getOpcode() == ISD::FDIV && U->getOperand(1) == N1) - Users.push_back(U); + if (U->getOpcode() == ISD::FDIV && U->getOperand(1) == N1) { + // Only count unique users; duplicates may be present in the list. + if (std::find(Users.begin(), Users.end(), U) == Users.end()) + Users.push_back(U); + } } if (TLI.combineRepeatedFPDivisors(Users.size())) { Index: test/CodeGen/X86/fdiv-combine.ll =================================================================== --- test/CodeGen/X86/fdiv-combine.ll +++ test/CodeGen/X86/fdiv-combine.ll @@ -44,5 +44,24 @@ ret double %ret } +define void @PR24141() #0 { +; CHECK-LABEL: PR24141: +; CHECK: callq +; CHECK-NEXT: divsd +; CHECK-NEXT: jmp +entry: + br label %while.body + +while.body: + %x.0 = phi double [ undef, %entry ], [ %div, %while.body ] + %call = call { double, double } @g(double %x.0) + %xv0 = extractvalue { double, double } %call, 0 + %xv1 = extractvalue { double, double } %call, 1 + %div = fdiv double %xv0, %xv1 + br label %while.body +} + +declare { double, double } @g(double) + ; FIXME: If the backend understands 'arcp', then this attribute is unnecessary. attributes #0 = { "unsafe-fp-math"="true" }