If you've done any C or C++ development on Fedora or Red Hat Enterprise Linux (RHEL), you'll have used GCC, the GNU Compiler Collection.
Red Hat has long been a leading contributor to GCC, and this continues as we work with others in the "upstream" GCC community on the next major release: GCC 5.
In this post I'll talk about some of the deep architectural changes I've been making to GCC. You won't directly see these changes unless you look at GCC's own source code, but they make GCC more robust - you'll be less likely to see an "Internal Compiler Error", and they make GCC development easier.
Under the hood: GCC's backend
The code-generation part of the compiler (the "backend") uses an internal representation called Register Transfer Language (RTL). The core data structure is a hierarchy of expressions of type "rtx", organized into trees.
For example, given the trivial C function:
double product (double x, double y) { return x * y; }
the multiplication might be expressed as this pattern (printed using a
Lisp-like syntax):
(set (reg:DF 63 [ D.1732 ]) (mult:DF (reg/v:DF 61 [ x ]) (reg/v:DF 62 [ y ])))
What does this mean? We have:
(set
which describes assigning a value to a destination from a source. In this case, the destination is a write to register 63:
(reg:DF 63 [ D.1732 ])
using the result of multiplying registers 61 and 62 as the source:
(mult:DF (reg/v:DF 61 [ x ]) (reg/v:DF 62 [ y ]))
The "DF" means that we're dealing with double-precision floats.
You can also see annotation nodes attached to the main tree nodes, recording which higher-level constructs they relate to: in this case the three registers correspond to a temporary value with the internal name "D.1732", and to the function parameters "x" and "y". This gets used when writing out the debuginfo, for use when stepping through the code in
gdb
.
Making the backend easier to hack on
In the above representation, everything is an rtx node, with a list of operands. It's flexible and powerful - for example it's used for writing CPU descriptions, expressing the kinds of instructions that are available on a given CPU, and these descriptions are used in numerous ways by the backend, such as for selecting the most efficient opcode available for a given operation.
The drawback of the "everything is just an rtx node" approach is that it can be awkward to work with when writing optimization passes. Since nodes in the tree are built and accessed as just a list of numbered operands, there is no type-checking when the nodes are accessed. There are other data structures built using this framework that aren't tree-like: in particular, linked lists of instructions, used throughout the backend. Plenty of routines in the backend expect to receive an rtx node of a particular kind, and if they don't get what they expect, you'd see an "Internal Compiler Error".
So one of many internal changes we've been working on for GCC 5 is to express the kinds of rtx nodes as types in a C++ inheritance hierarchy, so that type errors of this kind become build-time errors when the compiler itself is built, rather than a run-time failure.
I've written and committed over 250 patches implementing such cleanups to the latest development branch of GCC. Doing this uncovered two bugs where optimizations were being missed, albeit on CPU architectures not supported by RHEL, which I've now fixed.
This is my favorite kind of bug-fixing: eliminating an entire category of mistake, so that bugs of that kind can't occur again.
As well as reducing the likelihood of you seeing "Internal Compiler Error", this approach also leads to more readable code for the compiler's internals. For example, previously this loop from the instruction-scheduling code might be rather mystifying to a newcomer to GCC development:
for (rtx link = insn_queue[q]; link; link = XEXP (link, 1)) { rtx x = XEXP (link, 0); QUEUE_INDEX (x) = QUEUE_NOWHERE; INSN_TICK (x) = INVALID_TICK; }
You might reasonably wonder what those "XEXP (link, 1)
" and "XEXP (link, 0)
" mean. Knowing that this means accessing operands 1 and 0 respectively of "link
" isn't necessarily very enlightening.
Using rtx subclasses it can be rewritten as:
for (rtx_insn_list *link = insn_queue[q]; link; link = link->next ()) { rtx_insn *x = link->insn (); QUEUE_INDEX (x) = QUEUE_NOWHERE; INSN_TICK (x) = INVALID_TICK; }
replacing the "XEXP (link, 1)
" with "link->next ()
" to make it clear we're simply walking down a linked list, operating on the nodes.
What's next
The changes described above will make GCC's backend more robust, and easier to hack on. Indeed, the simpler implementation code should help us to make GCC generate faster code. Stay tuned for more posts on the work we're doing in GCC 5 (targeting 2015 upstream), and the kinds of improvements that the above work enables.
Last updated: February 23, 2024