diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -5737,6 +5737,11 @@ return SDValue(); } + auto RemoveDeadNode = [&](SDValue N) { + if (N && N.getNode()->use_empty()) + DAG.RemoveDeadNode(N.getNode()); + }; + SDLoc DL(Op); switch (Opcode) { @@ -5815,12 +5820,14 @@ // Negate the X if its cost is less or equal than Y. if (NegX && (CostX <= CostY)) { Cost = CostX; + RemoveDeadNode(NegY); return DAG.getNode(ISD::FSUB, DL, VT, NegX, Y, Flags); } // Negate the Y if it is not expensive. if (NegY) { Cost = CostY; + RemoveDeadNode(NegX); return DAG.getNode(ISD::FSUB, DL, VT, NegY, X, Flags); } break; @@ -5858,6 +5865,7 @@ // Negate the X if its cost is less or equal than Y. if (NegX && (CostX <= CostY)) { Cost = CostX; + RemoveDeadNode(NegY); return DAG.getNode(Opcode, DL, VT, NegX, Y, Flags); } @@ -5869,6 +5877,7 @@ // Negate the Y if it is not expensive. if (NegY) { Cost = CostY; + RemoveDeadNode(NegX); return DAG.getNode(Opcode, DL, VT, X, NegY, Flags); } break; @@ -5898,12 +5907,14 @@ // Negate the X if its cost is less or equal than Y. if (NegX && (CostX <= CostY)) { Cost = std::min(CostX, CostZ); + RemoveDeadNode(NegY); return DAG.getNode(Opcode, DL, VT, NegX, Y, NegZ, Flags); } // Negate the Y if it is not expensive. if (NegY) { Cost = std::min(CostY, CostZ); + RemoveDeadNode(NegX); return DAG.getNode(Opcode, DL, VT, X, NegY, NegZ, Flags); } break;