Preserve comparison operand order when if-combining
Fixes FP exception trapping by ensuring comparison operands maintain their original order during if-combining.
This commit corrects the order of comparison operands passed to maybe_fold_and_comparisons during the ifcombine_ifandif optimization. Incorrect operand order could lead to the loss of IEEE exception trapping for outer comparisons, which is essential for floating-point correctness. The change ensures that comparisons are processed in their original order, preserving trapping behavior, while noting that further refactoring might be needed for and_comparisons_1 due to its reliance on SSA defs.
In Details
The ifcombine_ifandif function within the tree-ssa optimizer combines two conditional branches into a single conditional branch with a logical AND. Previously, it passed comparison operands to maybe_fold_and_comparisons in an order that could be reversed by and_comparisons_1. This commit ensures ifcombine_ifandif passes the inner and outer conditions in their correct, original order to maybe_fold_and_comparisons, which then calls and_comparisons_1. This preserves the trapping behavior of outer comparisons, which is critical for IEEE arithmetic compliance, as and_comparisons_1's…
- if-combining
- A compiler optimization that merges two conditional branches, typically of the form
if (A) ...; if (B) ...;, into a single conditional branch, such asif (A && B) ...;. This can simplify control flow and expose further optimization opportunities. - IEEE exceptions
- Exceptional conditions defined by the IEEE 754 standard for floating-point arithmetic, such as invalid operation, division by zero, overflow, underflow, and inexact result. Compilers may need to preserve the trapping behavior of these exceptions.
- SSA
- Static Single Assignment form. An intermediate representation where every variable is assigned exactly once. It simplifies many dataflow analyses and optimizations.
- SSA defs
- In the context of SSA form, a 'def' refers to the definition or assignment of a value to a variable. Analyses that rely on SSA defs track the origin of values.