5.1 GENERIC, GIMPLE and RTL: three representations, and why there are three
Source references in this chapter are to GCC 15.2.0 (releases/gcc-15.2.0).
You ask cc1 to show its work on a nine-line function, and you get three files
that do not look like the same program. In the first, your for loop is already
a set of labels and gotos. In the second, variables that you never declared
appear, with names such as _1, and your && is gone. In the third, there is no
% at all, but there is an instruction with two results and a register that
your code never mentioned.
Each file is in a different language, and cc1 uses all three on purpose.
GENERIC has the shape of the source. GIMPLE is flat and has the same form on
every target. RTL describes the target's instructions. Two conversions connect
them, and nearly everything else in the compiler works inside one of the three.
This chapter assumes Chapter 1.1, where the driver runs
cc1 and cc1 writes the .s file. Everything in this chapter happens inside
that one process.
count.c: the example for all of Part V
Every chapter in Part V uses this one function, so that their dumps agree:
int
count_divisible (const long long *v, int n, long long d)
{
int count = 0;
for (int i = 0; i < n; i++)
if (v[i] != 0 && v[i] % d == 0)
count++;
return count;
}
It has a loop, a short-circuit &&, and a 64-bit remainder. On x86-64 the
remainder is one instruction. On a Cortex-M4 it is a call into libgcc. Chapters
5.5 and 5.6 are built on that difference, and this chapter shows it once, in the
RTL section.
Ask for one dump from each representation:
$ gcc -O2 -c -fdump-tree-original -fdump-tree-gimple -fdump-rtl-expand count.c
$ ls count.c.*
Each flag writes one file next to the object file. Its name is the source file
name, a pass number, a phase letter and the dump name, so the three files end in
.original, .gimple and .expand. The GCC manual documents the dump options
and this naming rule under Developer Options.
Chapter 5.2 explains where the numbers come
from.
Add -raw to a tree dump, as in -fdump-tree-original-raw, to see the data
structures instead of C-like text. The same manual page documents raw as "a
raw representation of the tree". In that form the original dump prints each
node with its name from tree.def (tree-dump.cc:257-262).
The gimple dump prints each assignment as gimple_assign, followed by the
name of its operation in angle brackets
(gimple-pretty-print.cc:654-656).
No compiler ran while this chapter was written, so it shows no dump text. Each description of a dump below comes from the code that prints it.
GENERIC: nested tree nodes with codes from tree.def
A front end parses your function into tree nodes. Every node starts with a
code (tree-core.h:1116-1117), and the macro TREE_CODE reads it
(tree.h:324). An expression node also holds its operands:
struct GTY(()) tree_exp {
struct tree_typed typed;
location_t locus;
tree GTY ((length ("TREE_OPERAND_LENGTH ((tree)&%h)"))) operands[1];
};
Listing 5.1-1: the operands of an expression are more trees
(gcc/tree-core.h:1670-1674).
An operand can be any tree, including another expression. So an expression
nests as deep as the source does. The C parser turns % into TRUNC_MOD_EXPR,
== into EQ_EXPR and && into TRUTH_ANDIF_EXPR
(c/c-parser.cc:10277-10336). A subscript on a pointer becomes
a load through a pointer sum (c/c-typeck.cc:3429-3431), and the
front end scales the index by the size of the element
(c-family/c-common.cc:3484-3500). The right half of the
condition in count.c, v[i] % d == 0, is one tree. This sketch leaves out the
type conversions:
EQ_EXPR
├── TRUNC_MOD_EXPR
│ ├── INDIRECT_REF
│ │ └── POINTER_PLUS_EXPR
│ │ ├── PARM_DECL v
│ │ └── i times sizeof (long long)
│ └── PARM_DECL d
└── INTEGER_CST 0
Each code is one line in tree.def, with a printable name and the number of
operands:
/* Four kinds of remainder that go with the four kinds of division: */
/* The sign of the remainder is that of the dividend. */
DEFTREECODE (TRUNC_MOD_EXPR, "trunc_mod_expr", tcc_binary, 2)
Listing 5.1-2: C's % in the tree vocabulary
(gcc/tree.def:750-753).
GENERIC knows what the source language knows: types, declarations, scopes, and
expressions with side effects in any position. The internals manual defines it
by its vocabulary: "If you can express it with the codes in gcc/tree.def, it's
GENERIC" (GENERIC).
Why GENERIC: every front end hands over the same thing
While a front end parses, it may use any representation of its own (Parsing pass). What it hands to the rest of the compiler is GENERIC. The C front end does that at the end of every function body:
/* Genericize before inlining. Delay genericizing nested functions
until their parent function is genericized. Since finalizing
requires GENERIC, delay that as well. */
if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
&& !undef_nested_function)
{
if (!decl_function_context (fndecl))
{
invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
c_genericize (fndecl);
...
cgraph_node::finalize_function (fndecl, false);
}
Listing 5.1-3: the hand-off from the C front end, abridged
(gcc/c/c-decl.cc:11521-11541).
c_genericize converts "from C frontend trees to GENERIC"
(c-family/c-gimplify.cc:747-751). The C parser builds a for
loop as a FOR_STMT (c/c-parser.cc:9299). That code comes from
c-common.def (:63), not from tree.def, so it is not
GENERIC. c_genericize rewrites the loop into labels and gotos
(Chapter 5.4 shows how), and only then writes the
original dump
(:785-805). That is why the original dump shows no for.
The Fortran front end builds GENERIC from a private representation of its own.
It writes the same dump, under the comment "Output the GENERIC tree"
(fortran/trans-decl.cc:8198-8199), and then calls the same
cgraph_node::finalize_function (:8228). After that call, no code
needs to know which language the function came from.
GENERIC is the contract between a front end and the rest of GCC. Each front end can produce it without the others, and every pass downstream is shared. That is why Part V never opens a front end.
GIMPLE: gimple tuples whose operands are still trees
The first arrow, from GENERIC to GIMPLE, is gimplify_function_tree in
gimplify.cc. It is not a pass in passes.def. cgraph_node::analyze calls it
when the call graph analyses a function, just before the lowering passes run
(cgraphunit.cc:684-699). It replaces the body:
/* The tree body of the function is no longer needed, replace it
with the new GIMPLE body. */
seq = NULL;
gimple_seq_add_stmt (&seq, bind);
gimple_set_body (fndecl, seq);
Listing 5.1-4: the GENERIC body is dropped
(gcc/gimplify.cc:21055-21059).
At its end, the same function writes the gimple dump
(gimplify.cc:21155-21160).
Chapter 5.4 follows the conversion itself.
A GIMPLE statement is a gimple object from gimple.h, not a tree. Its first
field is a code from gimple.def (gimple.h:220-226), such as
GIMPLE_ASSIGN, GIMPLE_COND or GIMPLE_CALL. The rule that makes GIMPLE flat
is in the comment on the assignment:
/* GIMPLE_ASSIGN <SUBCODE, LHS, RHS1[, RHS2]> represents the assignment
statement
LHS = RHS1 SUBCODE RHS2.
SUBCODE is the tree code for the expression computed by the RHS of the
assignment. It must be one of the tree codes accepted by
get_gimple_rhs_class. If LHS is not a gimple register according to
is_gimple_reg, SUBCODE must be of class GIMPLE_SINGLE_RHS.
LHS is the operand on the LHS of the assignment. It must be a tree node
accepted by is_gimple_lvalue.
RHS1 is the first operand on the RHS of the assignment. It must always be
present. It must be a tree node accepted by is_gimple_val.
RHS2 is the second operand on the RHS of the assignment. It must be a tree
node accepted by is_gimple_val. This argument exists only if SUBCODE is
of class GIMPLE_BINARY_RHS. */
DEFGSCODE(GIMPLE_ASSIGN, "gimple_assign", GSS_WITH_MEM_OPS)
Listing 5.1-5: one operation, simple operands
(gcc/gimple.def:82-101).
is_gimple_val accepts "an identifier or a constant"
(gimple-expr.cc:833-850). An operand cannot be an expression.
So a GIMPLE assignment does one operation, and the gimplifier moves each inner
operation into a statement of its own, with a temporary for its result. The
internals manual gives the smallest case under Operands:
a = b + c + d
T1 = b + c;
a = T1 + d;
Listing 5.1-6: the manual's before and after
(gcc/doc/gimple.texi:563-568).
Apply the same rule to the tree for v[i] % d == 0. Each interior node becomes a
statement: the scaled index, the pointer sum, the load, the remainder. The
comparison with zero does not become an assignment. A GIMPLE_COND holds a
comparison code, two simple operands, and the labels for its two outcomes
(gimple.def:33-46). Its code "must be of class
tcc_comparison". TRUTH_ANDIF_EXPR is of class tcc_expression
(tree.def:830), so a && cannot be the condition of a GIMPLE_COND.
The gimplifier breaks it "into the equivalent sequence of conditionals"
(gimplify.cc:5108-5114), and Chapter 5.4 follows that code.
Two facts about GIMPLE confuse people who read its source.
GIMPLE statements are tuples, but their operands are trees. The internals
manual says it in one line: "All gimple operands are of type tree"
(gimple.texi:656). A variable,
a constant and a temporary in GIMPLE are the same kind of tree node that
GENERIC used. The first GIMPLE was built from trees, as the manual records in
its GIMPLE chapter, and the tuples kept trees as their
operands. So code in the middle end calls TREE_CODE on GIMPLE operands, and
the functions in gimplify.cc pass tree * arguments.
GIMPLE uses the tree codes for its operations. The remainder in a GIMPLE
assignment is still TRUNC_MOD_EXPR, stored in the statement's subcode:
/* The SUBCODE field can be used for tuple-specific flags for tuples
that do not require subcodes. Note that SUBCODE should be at
least as wide as tree codes, as several tuples store tree codes
in there. */
unsigned int subcode : 16;
Listing 5.1-7: a tree code inside a GIMPLE tuple
(gcc/gimple.h:256-260).
gimple_assign_rhs_code returns that field as an enum tree_code
(gimple.h:2895-2910). So to find every place where the compiler
handles % before the target appears, search for TRUNC_MOD_EXPR. The one name
finds the front end, the gimplifier and the GIMPLE passes.
Why GIMPLE: one form for every target
GIMPLE has no nested expressions and no structured control flow, and its statements have the same form on every target. The internals manual names what that buys: "GIMPLE is used for target and language independent optimizations (e.g., inlining, constant propagation, tail call elimination, redundancy elimination, etc)" (Tree SSA). A pass that sees one operation per statement, with simple operands, finds every use of a value without a walk through nested trees. It is written once, and it runs for x86-64 and for Arm.
The pass list shows where the weight is. passes.def has 381 NEXT_PASS lines.
285 come before pass_expand, where the function is GIMPLE, and 94 come after
it (passes.def:453).
"Target-independent" does not mean that GIMPLE knows nothing about the target.
The type sizes are the target's: the sizeof (long long) in the index
arithmetic came from it. Some passes also ask the target questions. For example,
the vectorizer asks whether masked stores are expensive
(tree-vectorizer.cc:1409-1410). What GIMPLE does not contain is
the target's instructions. TRUNC_MOD_EXPR is the same statement whether or not
the machine can divide.
SSA names: why the dumps say count_5, _3 and d_7(D)
Most GIMPLE passes need the function in static single assignment (SSA) form.
Each name is assigned in exactly one place. Where two control paths meet with
different values, a PHI node merges them into a new name. The internals
manual explains the form, with an example of a merge, under SSA.
An SSA name is a tree with the code SSA_NAME (tree.def:1031).
It holds the variable that it is a version of, and a pointer to the one GIMPLE
statement that sets it (tree-core.h:1690-1697). So the two
data structures point at each other: statements hold trees, and SSA names point
back at statements. The dump printer spells an SSA name like this:
case SSA_NAME:
if (SSA_NAME_IDENTIFIER (node))
{
...
}
pp_underscore (pp);
pp_decimal_int (pp, SSA_NAME_VERSION (node));
if (SSA_NAME_IS_DEFAULT_DEF (node))
pp_string (pp, "(D)");
if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (node))
pp_string (pp, "(ab)");
break;
Listing 5.1-8: how a dump spells an SSA name, abridged
(gcc/tree-pretty-print.cc:3740-3758).
The omitted lines print the name of the variable. Read the names in the title with that code:
count_5is a version of the variablecount: the variable's name, an underscore, and a version number._3has no name. It is a temporary with no user variable behind it (tree.h:2137-2143).d_7(D)is a default definition: the value thatdhas when the function starts. A default definition is made for a variable whose first use in the function is a read (tree.h:2176-2182). For a parameter, that value is the argument.
The version number does not count assignments to one variable. The comment on
SSA_NAME_VERSION says "in tree SSA, version numbers are not per variable and
may be recycled" (tree.h:2160-2162). The version is the
name's index in one table that holds every SSA name of the function
(tree-ssanames.cc:382-388). So count_3 and _3 never exist in
one function at the same time.
SSA is a property that GIMPLE is put into, and it happens in two steps. The
gimplifier makes its own temporaries SSA names at once. It opens its context
with SSA on (gimplify.cc:20807-20808), and each new temporary of a
register type becomes an SSA name:
if (allow_ssa
&& gimplify_ctxp->into_ssa
&& is_gimple_reg_type (TREE_TYPE (val)))
{
t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
if (! gimple_in_ssa_p (cfun))
{
const char *name = get_name (val);
if (name)
SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
}
}
Listing 5.1-9: temporaries are SSA names from the start
(gcc/gimplify.cc:686-697).
The inner if gives a temporary an identifier when its value has a name.
create_tmp_var_name builds that identifier from the name and a counter
(gimple-expr.cc:421-436), so not every temporary prints as a bare
_3.
Your own variables, count and i, are still plain variables in the gimple
dump. A later pass named ssa renames them and inserts the PHI nodes
(tree-into-ssa.cc:2458-2469, at passes.def:56).
Chapter 5.4 follows that pass. Its descriptor declares that it provides
PROP_ssa, a bit that the pass manager keeps for each function
(tree-pass.h:212). The expand pass requires that bit, as
Listing 5.1-12 shows.
RTL: rtx expressions with a machine mode, in a chain of insns
RTL is a different data structure again. An rtx starts with two fields:
struct GTY((desc("0"), tag("0"),
chain_next ("RTX_NEXT (&%h)"),
chain_prev ("RTX_PREV (&%h)"))) rtx_def {
/* The kind of value the expression has. */
ENUM_BITFIELD(machine_mode) mode : MACHINE_MODE_BITSIZE;
/* The kind of expression this is. */
ENUM_BITFIELD(rtx_code) code: RTX_CODE_BITSIZE;
Listing 5.1-10: every rtx has a mode and a code
(gcc/rtl.h:312-319).
The codes come from rtl.def, and the mode is a machine mode, such as DImode
for a 64-bit integer. In the dumps, the mode follows the code after a colon, so
(reg:SI 38) is a reg in SImode. The internals manual documents this
notation under Machine Modes. A function in RTL is a chain of
insns. Some insns are instructions, and others are labels, jump tables and notes
(Insns). Inside an instruction, every assignment is a SET, and
an instruction that assigns more than one location wraps its SETs in a
PARALLEL (rtl.def:289-295).
RTL knows three things that GIMPLE does not.
Registers. A reg with a number below FIRST_PSEUDO_REGISTER is a machine
register. Above it, the number names a pseudo register, and "the compiler's
strategy is to generate code assuming an unlimited number of such pseudo
registers, and later convert them into hard registers or into memory
references" (Regs and Memory). Expand writes pseudos. The
register allocator, pass_ira and pass_reload
(passes.def:511-512), turns them into machine registers.
Modes instead of types. long long becomes DImode. RTL records the size
and kind of a value, not the C type.
The target's instructions. An instruction in RTL must match a pattern that the target's machine description defines. Here is how the x86-64 port describes its divide instruction:
(define_expand "<u>divmod<mode>4"
[(parallel [(set (match_operand:SWIM248 0 "register_operand")
(any_div:SWIM248
(match_operand:SWIM248 1 "register_operand")
(match_operand:SWIM248 2 "nonimmediate_operand")))
(set (match_operand:SWIM248 3 "register_operand")
(<paired_mod>:SWIM248 (match_dup 1) (match_dup 2)))
(clobber (reg:CC FLAGS_REG))])])
Listing 5.1-11: one x86-64 instruction gives the quotient and the remainder
(gcc/config/i386/i386.md:11371-11378).
SWIM248 and any_div are iterators, so this one pattern covers several modes,
and signed and unsigned division. Chapter 5.6
explains iterators. Read the body as RTL: a PARALLEL of two SETs, one with
div and one with mod, and a clobber that says the flags register is
destroyed. The machine computes both results at once and changes the flags, and
RTL must say so, because the register allocator and the later passes rely on
it. The x86-64 port has no pattern that computes only a remainder, so for %
expand falls back to this one (expmed.cc:5281-5299). The
DI member of SWIM248 applies only with TARGET_64BIT
(i386.md:1191-1192), so with -m32 this pattern has no 64-bit form.
The Arm port defines integer division only in SImode, and only for cores with
a divide instruction (config/arm/arm.md:5129-5135). An
arm-none-eabi compiler uses the Arm EABI (config.gcc:1497-1503,
config/arm/bpabi.h:27-30), and for that ABI the port names a library
function for 64-bit division with remainder
(config/arm/arm.cc:2637-2639). With no instruction to use,
expand calls that function and keeps the remainder
(expmed.cc:5343-5361). So for
arm-none-eabi-gcc -O2 -mcpu=cortex-m4, the same GIMPLE statement becomes a call
to __aeabi_ldivmod in RTL. libgcc supplies that function
(libgcc/config/arm/bpabi.S:174-188).
Chapter 4.1 followed such helpers from the
library side, and Chapter 5.5 shows where expand makes
the choice.
Why RTL: allocation and instruction selection need the machine
The passes after expand choose instructions, assign registers, schedule, and
print assembly. Each of those needs the facts in Listing 5.1-11: which operands
must be registers, which registers an instruction destroys, which modes exist.
GIMPLE does not carry them, by design. RTL carries them in every insn. The
textual form of RTL is also the language of the machine description (RTL
Representation). Reading an RTL dump and reading a .md file is
the same skill, which Chapter 5.6 uses.
The two arrows: gimplify_function_tree and pass_expand
| GENERIC | GIMPLE | RTL | |
|---|---|---|---|
| Data structure | tree (tree-core.h) | gimple with tree operands (gimple.h) | rtx (rtl.h) |
| Codes | tree.def, plus front-end files such as c-common.def | gimple.def, plus tree codes for operations | rtl.def |
| Built by | the front end (c_genericize for C) | gimplify_function_tree | pass_expand |
| First dump | -fdump-tree-original | -fdump-tree-gimple | -fdump-rtl-expand |
| Target instructions | no | no | yes |
The first arrow is a function call, as Listing 5.1-4 showed. The second arrow is a pass, and its descriptor states the change of representation:
const pass_data pass_data_expand =
{
RTL_PASS, /* type */
"expand", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_EXPAND, /* tv_id */
( PROP_ssa | PROP_gimple_leh | PROP_cfg
| PROP_gimple_lcx
| PROP_gimple_lvec
| PROP_gimple_lva), /* properties_required */
PROP_rtl, /* properties_provided */
( PROP_ssa | PROP_gimple ), /* properties_destroyed */
0, /* todo_flags_start */
0, /* todo_flags_finish */
};
Listing 5.1-12: expand needs SSA GIMPLE, makes RTL, and destroys the GIMPLE
(gcc/cfgexpand.cc:6973-6987).
pass_expand is an RTL_PASS, and it is the line in passes.def that splits
the list in two:
NEXT_PASS (pass_warn_function_noreturn);
NEXT_PASS (pass_expand);
NEXT_PASS (pass_rest_of_compilation);
PUSH_INSERT_PASSES_WITHIN (pass_rest_of_compilation)
NEXT_PASS (pass_instantiate_virtual_regs);
Listing 5.1-13: the boundary in the pass list
(gcc/passes.def:451-457).
Every pass above that line works on GIMPLE. Every pass inside
pass_rest_of_compilation works on RTL, and the last of them, pass_final,
prints the assembly (passes.def:563). No pass converts back.
Chapter 5.3 explains how the pass manager reads
this file.
So when a dump surprises you, first ask which representation it is in. A
GENERIC or GIMPLE dump comes from a front end, from the gimplifier, or from a
pass that has no notion of instructions. An RTL dump comes from expand or from a
pass that sees the target. The answer tells you which half of passes.def to
read.
Three things to carry away
GENERIC is what every front end hands over, GIMPLE is what the shared optimisers work on, and RTL is what the target's instructions are written in.
There are two arrows: gimplify_function_tree, a function call, and
pass_expand, a pass. Every other pass works inside one representation.
GIMPLE is not a separate world from trees. Its operands are tree nodes, its
operations are tree codes, and an SSA name is a tree that points back at a
statement.
Things that surprise people
The original dump has no for loop. The C front end turns loops into
gotos in c_genericize, before it writes that dump.
_7 and count_7 cannot both exist. SSA version numbers index one table
for the whole function, and they say nothing about how often a variable
is assigned.
The gimple dump is half in SSA. Temporaries are SSA names from the start.
Your own variables become SSA names only in the later ssa pass.
Gimplification is not in passes.def. cgraph_node::analyze calls it
directly, so you will not find it in the pass list.
Documentation coverage
The internals manual is the primary source for this chapter, and it is strong.
It has a chapter for each representation: GENERIC
(gcc/doc/generic.texi:9), GIMPLE (gcc/doc/gimple.texi:6),
and RTL Representation (gcc/doc/rtl.texi:5). Its
Tree SSA chapter (gcc/doc/tree-ssa.texi:9) compares the
three, and its SSA node (:440) explains versions and PHI nodes.
The Passes chapter (gcc/doc/passes.texi:7) describes the
parsing, gimplification and RTL generation steps. This chapter adds the reasons
for three representations and the route from each dump to its source.
The GCC manual documents the dump options in
Developer Options (gcc/doc/invoke.texi:20326): the dump file name
(:20349-20362), -fdump-rtl-expand (:20496-20498), -fdump-tree- with its
options (:20897-20906), and raw (:20928-20930).
What is not documented:
- The dump names
originalandgimple. The GCC manual lists neither among its tree dumps. Only the internals manual mentions-fdump-tree-gimple(gimple.texi:68-69), and neither manual mentions-fdump-tree-original. - That the front end, not a pass, writes the
originaldump, and that the C front end has turned loops intogotos before it does. - That gimplification is a function call from the call graph and not a pass in
passes.def. - That temporaries are SSA names from the start of gimplification, while user
variables become SSA names in the
ssapass. - How a dump spells an SSA name:
name_N,_N,(D)and(ab). The rule that version numbers are shared across variables is only in a comment intree.h. - That expand requires
PROP_ssaand destroys the GIMPLE properties. - The internals manual says that the C and C++ front ends convert "directly from
front end trees to GIMPLE" and not first to GENERIC (
gimple.texi:56-66). At 15.2 the C front end callsc_genericize, which says it converts "from C frontend trees to GENERIC", and gimplification follows. The manual's paragraph describes an older design.
Next: how cc1 names its dump files, where the number in a name such as
count.c.NNNt.gimple comes from, and how to find the pass that changed your
code.
All source references in this chapter are to GCC 15.2.0
(releases/gcc-15.2.0). Line numbers in other releases will differ; the
surrounding code rarely does. Where behaviour itself changed across a major
version, it is flagged inline.