We have the getNegatibleCost/getNegatedExpression to evaluate the cost and negate the expression. However, during negating the expression, the cost might change as we are changing the DAG, and then, hit the assertion if we negated the wrong expression as the cost is not trustful anymore.
We are struggling to fix such issues on D75501, D70975, D76638, D76439 etc and I believe there are more potential issues here, as we make assumption that, the cost won't change during negating the expression.
This patch is target to remove the getNegatibleCost to avoid the out of sync with getNegatedExpression, and check the cost during negating the expression. It also reduce the duplicated code between getNegatibleCost and getNegatedExpression. And fix the crash for the test in D76638
The side effect is that, we have to create some nodes which will be removed in the next iteration of DAGCombine soon. And it will change the combine worklist as we always push the operands of the unused node into worklist when it is deleted. But it shouldn't have bad impact as we are designing to not to rely on the combine order.
This change can be a parent commit since it's not related to this refactoring.