IRA aligns equivalence rejection with LRA policies
The IRA pass in GCC now anticipates LRA's rejection of certain register equivalences, reducing unnecessary spilling and improving code quality for PIC.
The Instruction Register Allocator (IRA) in GCC now harmonizes its handling of register equivalences with the Local Register Allocator (LRA). Previously, IRA might unnecessarily spill pseudo-registers with equivalences that LRA would ultimately reject, especially in Position-Independent Code (PIC) scenarios. By aligning IRA’s costing function to reject these equivalences earlier, the compiler avoids generating inefficient code that creates stack frames solely for these spills, resulting in smaller and faster binaries.
In Details
The Instruction Register Allocator (IRA) and Local Register Allocator (LRA) are distinct phases of register allocation within GCC's backend, operating on RTL. IRA performs a global allocation, while LRA refines selections at the basic block level. The core issue here is IRA's previous tendency to spill pseudo-registers that had equivalences, even when LRA would later reject those equivalences (e.g., due to PIC requirements for addressing constants relative to the program counter). The fix modifies find_costs_and_classes in ira-costs.cc to anticipate LRA's rejection. This prevents IRA from…
For Context
Compilers like GCC break down the complex task of turning your code into an executable program into many stages. Two important stages deal with 'register allocation' – assigning bits of data to tiny, fast storage locations (registers) inside the CPU. The first is called the Instruction Register Allocator (IRA), and a later one is the Local Register Allocator (LRA). Sometimes, these stages have different ideas about how to handle 'equivalences' – situations where different pieces of data are considered interchangeable. This commit addresses a problem where the IRA would sometimes make a pessimistic choice, storing data on the slower main memory ('spilling' it), even though the LRA would later decide that the equivalence was invalid anyway (especially for Position-Independent Code, or PIC, which is common). By making IRA smarter about what LRA will eventually reject, it avoids unnecessary memory accesses, resulting in programs that are often smaller and run faster.