Reassoc pass avoids inserting range tests in basic block ending statements
The reassociation pass now correctly handles statements that end a basic block, preventing crashes and ensuring correctness when inserting range tests.
The reassoc optimization pass has been fixed to prevent incorrect insertion of range tests. Previously, when a statement defining an operand also ended its basic block (e.g., due to a function call with exceptions or a trapping comparison), inserting a range test immediately after it would lead to internal compiler errors. The pass now treats such definitions like default definitions, improving stability and correctness for code involving complex control flow or exception handling.
In Details
The update_range_test function within the reassoc pass previously inserted range tests immediately following the definition of an operand. This fails when the definition occurs in a statement that terminates the basic block (e.g., a call that may throw, or a comparison that can trap with -fnon-call-exceptions). The fix modifies update_range_test to handle these cases by treating the definition as a default definition, thus avoiding the insertion of a new statement within a completed block and preventing compiler crashes or incorrect code generation.
- reassoc
- Short for reassociation, an optimization pass in GCC that rearranges arithmetic operations to potentially improve performance or enable other optimizations. It reorders operations like
(a + b) + ctoa + (b + c)if beneficial. - range test
- An optimization technique that checks if a value falls within a certain range. This can help the compiler make assumptions about the value, potentially enabling further optimizations.
- basic block
- A sequence of instructions in compiled code that has a single entry point and a single exit point. Control flow only enters at the beginning and leaves at the end, without branches in between.
- SSA
- Static Single Assignment form. An intermediate representation where every variable is assigned a value exactly once. This form simplifies many data-flow analyses and optimizations.
- tree-ssa-reassoc.cc
- The C++ source file in GCC that implements the reassociation optimization pass using the SSA (Static Single Assignment) intermediate representation.