5.3 The pass manager: from passes.def to ccp2

GCC 15.2.0. To run the commands: an installed compiler, and a GCC checkout for the two grep searches.

At -O2, the dump directory from Chapter 5.2 holds four files whose names end in ccp1 to ccp4, the dumps of ccp, conditional constant propagation. You want to switch off the second one and compare the output. -fdump-tree-ccp worked a moment ago, so you try -fdisable-tree-ccp. cc1 reports "unknown pass tree-ccp specified in -fdisable". Both options take a pass name. Only one of them accepts ccp.

The pass manager builds, names and numbers every pass once, when cc1 starts, from gcc/passes.def. After that it only walks the tree it built, and asks each pass whether it wants to run. Every pass name that an option accepts comes from that startup code.

Four pieces of code do this work. gen-pass-instances.awk counts the NEXT_PASS lines of passes.def when GCC is built. When cc1 starts, the pass_manager constructor builds one object for each line, and register_one_dump_file gives each object the names that options match, which explains the -fdisable-tree-ccp error. execute_one_pass then asks the gates, for each function.

This chapter assumes Chapter 5.1, for GIMPLE, RTL and the count.c example, and Chapter 5.2, for the dump files, their numbers and the options that write them. It also uses Chapter 1.13 for -flto and lto1, Chapter 2.1 for the target makefile fragments, and Chapter 2.4 for the files that the build generates.

passes.def holds five lists, and each list is a tree

gcc/passes.def fills the five pass lists of cc1, and its macros nest passes inside other passes, so each list is a tree. The tree fixes the order in which a function meets the passes, and the nesting decides which of them run. One macro in pass_manager.h names the five lists:

#define GCC_PASS_LISTS \
  DEF_PASS_LIST (all_lowering_passes) \
  DEF_PASS_LIST (all_small_ipa_passes) \
  DEF_PASS_LIST (all_regular_ipa_passes) \
  DEF_PASS_LIST (all_late_ipa_passes) \
  DEF_PASS_LIST (all_passes)

Listing 5.3-1: the five pass lists (gcc/pass_manager.h:28-33).

Three of the lists hold IPA (interprocedural analysis) passes, which see the whole translation unit at once. The other two, all_lowering_passes and all_passes, work on one function at a time.

In passes.def, each list opens with INSERT_PASSES_AFTER and closes with TERMINATE_PASS_LIST, and each NEXT_PASS adds one pass to the end of the current list. PUSH_INSERT_PASSES_WITHIN opens a sub-list under the last pass added, and POP_INSERT_PASSES closes it. The first pass_ccp is two levels down:

  NEXT_PASS (pass_local_optimization_passes);
  PUSH_INSERT_PASSES_WITHIN (pass_local_optimization_passes)
      NEXT_PASS (pass_fixup_cfg);
      NEXT_PASS (pass_rebuild_cgraph_edges);
      NEXT_PASS (pass_local_fn_summary);
      NEXT_PASS (pass_early_inline);
      NEXT_PASS (pass_warn_recursion);
      NEXT_PASS (pass_all_early_optimizations);
      PUSH_INSERT_PASSES_WITHIN (pass_all_early_optimizations)
	  NEXT_PASS (pass_remove_cgraph_callee_edges);
	  NEXT_PASS (pass_early_object_sizes);
	  /* Don't record nonzero bits before IPA to avoid
	     using too much memory.  */
	  NEXT_PASS (pass_ccp, false /* nonzero_p */);

Listing 5.3-2: the early local passes, with the first pass_ccp (gcc/passes.def:67-80).

pass_local_optimization_passes and pass_all_early_optimizations are containers, passes that do no work of their own. The passes under a container run only if the container agrees to run.

cgraphunit.cc: when each pass list runs

gcc/cgraphunit.cc, the call graph (cgraph) code, runs the five lists at different points, not one after the other. The order matters when you read dumps: ccp1 runs on every function in the file before IPA inlining starts, and all_passes then takes one function at a time. A comment above the constructor of the pass manager gives the order, and each arrow in it points from a routine to the list that the routine runs:

/* Construct the pass tree.  The sequencing of passes is driven by
   the cgraph routines:

   finalize_compilation_unit ()
       for each node N in the cgraph
	   cgraph_analyze_function (N)
	       cgraph_lower_function (N) -> all_lowering_passes

   If we are optimizing, compile is then invoked:

   compile ()
       ipa_passes () 			-> all_small_ipa_passes
					-> Analysis of all_regular_ipa_passes
	* possible LTO streaming at compilation time *
					-> Execution of all_regular_ipa_passes
	* possible LTO streaming at link time *
					-> all_late_ipa_passes
       expand_all_functions ()
           for each node N in the cgraph
	       expand_function (N)      -> Transformation of all_regular_ipa_passes
				        -> all_passes
*/

Listing 5.3-3: who runs which list (gcc/passes.cc:1555-1576).

Three function names in that comment no longer exist in gcc/cgraphunit.cc. In GCC 15.2.0, these calls in that file run the lists, in this order:

ListRun bySource
all_lowering_passescgraph_node::analyze, for each functioncgraphunit.cc:699
all_small_ipa_passesipa_passescgraphunit.cc:2226-2231
all_regular_ipa_passesipa_passescgraphunit.cc:2283-2286
all_late_ipa_passessymbol_table::compilecgraphunit.cc:2381
all_passescgraph_node::expand, for each functioncgraphunit.cc:1859

An IPA list still reaches single functions. When an IPA pass has a sub-list of GIMPLE passes, the pass manager runs that sub-list on each function in turn (passes.cc:3114-3121). The early passes of Listing 5.3-2, ccp1 among them, form such a sub-list.

gen-pass-instances.awk numbers every NEXT_PASS

gen-pass-instances.awk gives every NEXT_PASS line an instance number, and the pass manager reads its output, pass-instances.def, instead of passes.def. That output exists only in the build directory, and a target's own passes join the generic ones there. The make rule shows both inputs:

pass-instances.def: $(srcdir)/passes.def $(PASSES_EXTRA) \
		    $(srcdir)/gen-pass-instances.awk
	$(AWK) -f $(srcdir)/gen-pass-instances.awk \
	  $(srcdir)/passes.def $(PASSES_EXTRA) > pass-instances.def

Listing 5.3-4: the rule for pass-instances.def (gcc/Makefile.in:2681-2684).

The script counts the instances of each pass and adds the count as a second argument. Its header comment gives an example: NEXT_PASS (pass_warn_unused_result); becomes NEXT_PASS (pass_warn_unused_result, 1); (gcc/gen-pass-instances.awk:17-26). A line with extra arguments becomes NEXT_PASS_WITH_ARG or NEXT_PASS_WITH_ARGS (:222-235). The five NEXT_PASS (pass_ccp, ...) lines become instances 1 to 5, in the order in which they appear in the file (:214-220). An -O2 compile writes only ccp1 to ccp4, because the fifth instance sits in the -Og group, whose gate says no at -O2.

A pass runs again only if passes.def names it again, because the walk over a list goes from each pass to the next and never back (Listing 5.3-9). Each occurrence runs the same code at a different point in the tree, sometimes with a different parameter, and the comments next to them say why. The first ccp runs before IPA with nonzero_p false: "Don't record nonzero bits before IPA to avoid using too much memory." (Listing 5.3-2). The fourth has its own reason: "Run CCP to compute alignment and nonzero bits." (passes.def:353-354).

$(PASSES_EXTRA) is how a target adds its own passes. The x86 makefile fragment appends config/i386/i386-passes.def to it (t-i386:23). That file places each pass relative to a generic pass and one of its instances:

  INSERT_PASS_AFTER (pass_postreload_cse, 1, pass_insert_vzeroupper);
  INSERT_PASS_AFTER (pass_late_combine, 1, pass_stv, false /* timode_p */);
  /* Run the 64-bit STV pass before the CSE pass so that CONST0_RTX and
     CONSTM1_RTX generated by the STV pass can be CSEed.  */
  INSERT_PASS_BEFORE (pass_cse2, 1, pass_stv, true /* timode_p */);

Listing 5.3-5: x86 passes placed among the generic ones (gcc/config/i386/i386-passes.def:27-31).

Each line names a generic pass, the instance of it to follow or precede, the new pass and its argument. The second line adds pass_stv, with timode_p false, after the first instance of pass_late_combine. The script writes each inserted pass into its output at that point (gen-pass-instances.awk:129-144) and counts it like any NEXT_PASS line, so pass_stv has two instances. The instance number of a target pass counts the lines that the script wrote, not only the lines in passes.def. The Arm and AArch64 fragments do the same (t-arm:205, t-aarch64:152), so the pass tree of an x86-64 cc1 is not the pass tree of an arm-none-eabi cc1.

The pass_manager constructor: make_pass_ccp once, clone () after

The pass_manager constructor builds one pass object for each line of pass-instances.def. The factory function of a pass, here make_pass_ccp, makes the first instance, and every later instance is a clone () of the first. Each object later gets its own names and answers its own gate, so the number of ccp passes in cc1 is fixed here.

pass-instances.def is included three times. In pass_manager.h it declares one field for each instance, so pass_ccp_1 to pass_ccp_5 are members of the class (pass_manager.h:111-136). The constructor in passes.cc includes it first to set every field to NULL (passes.cc:1586-1594), and then to build the tree, with these definitions. Watch the test on NUM, which picks the factory or the clone:

#define NEXT_PASS(PASS, NUM) \
  do { \
    gcc_assert (PASS ## _ ## NUM == NULL); \
    if ((NUM) == 1)                              \
      PASS ## _1 = make_##PASS (m_ctxt);          \
    else                                         \
      {                                          \
        gcc_assert (PASS ## _1);                 \
        PASS ## _ ## NUM = PASS ## _1->clone (); \
      }                                          \
    p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1);  \
  } while (0)

#define NEXT_PASS_WITH_ARG(PASS, NUM, ARG)		\
    do {						\
      NEXT_PASS (PASS, NUM);				\
      PASS ## _ ## NUM->set_pass_param (0, ARG);	\
    } while (0)

Listing 5.3-6: what each NEXT_PASS does at startup (gcc/passes.cc:1620-1637).

next_pass_1 links the new object into the current list (passes.cc:1350-1360), and PUSH_INSERT_PASSES_WITHIN points the current list at the sub field of its pass (:1613-1615). An extra argument in passes.def becomes a call to set_pass_param. The base class makes both clone and set_pass_param an internal error (passes.cc:81-92), so a pass that occurs twice in passes.def must override clone, and a pass that takes an argument must override set_pass_param.

Chapter 5.2 showed that the dump numbers are fixed before cc1 reads -O2, and the constructor is the reason. It runs from general_init (toplev.cc:1153-1154), which toplev::main calls at :2296, and toplev::main decodes the command line later, at :2331. So the pass tree exists before cc1 reads your options. -O0 and -O2 get the same tree, the same instance numbers and the same dump numbers, and the options decide only which passes agree to run.

pass_data and opt_pass: the descriptor and the methods of a pass

Every pass is a pass_data descriptor, shared by its instances, and an opt_pass object for each instance. The descriptor holds the name that the dump options and -fdisable- match, and the object holds the gate that decides whether the instance runs. Chapter 5.2 found pass_data_ccp by searching for its name.

struct pass_data is the "Metadata for a pass, non-varying across all instances of a pass" (tree-pass.h:39-64). class opt_pass derives from it and adds the methods and the links of one instance (:71-113). In the ccp pass, watch name, properties_required and the four methods of the class:

const pass_data pass_data_ccp =
{
  GIMPLE_PASS, /* type */
  "ccp", /* name */
  OPTGROUP_NONE, /* optinfo_flags */
  TV_TREE_CCP, /* tv_id */
  ( PROP_cfg | PROP_ssa ), /* properties_required */
  0, /* properties_provided */
  0, /* properties_destroyed */
  0, /* todo_flags_start */
  TODO_update_address_taken, /* todo_flags_finish */
};

class pass_ccp : public gimple_opt_pass
{
public:
  pass_ccp (gcc::context *ctxt)
    : gimple_opt_pass (pass_data_ccp, ctxt), nonzero_p (false)
  {}

  /* opt_pass methods: */
  opt_pass * clone () final override { return new pass_ccp (m_ctxt); }
  void set_pass_param (unsigned int n, bool param) final override
    {
      gcc_assert (n == 0);
      nonzero_p = param;
    }
  bool gate (function *) final override { return flag_tree_ccp != 0; }
  unsigned int execute (function *) final override
  {
    return do_ssa_ccp (nonzero_p);
  }

 private:
  /* Determines whether the pass instance records nonzero bits.  */
  bool nonzero_p;
}; // class pass_ccp

Listing 5.3-7: the ccp pass (gcc/tree-ssa-ccp.cc:3036-3072).

Read the descriptor from the top:

  • type is GIMPLE_PASS, RTL_PASS, SIMPLE_IPA_PASS or IPA_PASS (tree-pass.h:31-37). It selects the dump prefix.
  • name is the "Terse name of the pass used as a fragment of the dump file name. If the name starts with a star, no dump happens." (tree-pass.h:45-47).
  • tv_id names the timer that the pass manager starts around the pass (passes.cc:2646-2648).
  • The three properties_* sets say what the IR must have before the pass, and what the pass adds and removes. ccp needs a CFG (control flow graph) and SSA form.
  • todo_flags_start and todo_flags_finish ask the pass manager to do common work before and after the pass. ccp asks for TODO_update_address_taken, which runs execute_update_addresses_taken when optimizing (passes.cc:2078-2079). That is the work behind the comment after the first three pass_ccp lines in passes.def: "After CCP we rewrite no longer addressed locals into SSA form if possible."

The class supplies four methods. gate decides whether the pass runs, and here it tests flag_tree_ccp, which -O1 and higher set (opts.cc:597). The GCC manual documents -ftree-ccp and that default under Optimize Options. execute does the work, and clone and set_pass_param exist because ccp occurs five times and takes one argument. The base class gives gate a default of true, and an execute that does nothing (passes.cc:94-104), so a container such as pass_all_early_optimizations overrides only gate.

The other pass kinds use the same descriptor with a different base class. gimple_opt_pass and rtl_opt_pass add nothing to opt_pass (tree-pass.h:115-133). ipa_opt_pass_d adds the hooks that split an IPA pass into a summary stage, an execute stage and a transform of each function (:141-193). Listing 5.3-3 calls the same stages Analysis, Execution and Transformation, and with -flto the summaries are streamed to disk between the first two. The internals manual describes the stages under IPA passes.

How ccp becomes tree-ccp2: register_one_dump_file

The 2 in tree-ccp2 comes from a second count, which the constructor makes on the objects, and not from the Awk script. register_one_dump_file turns that count and the name field into the strings that the options match, and the strings it makes for ccp explain the -fdisable-tree-ccp error.

next_pass_1 calls add_pass_instance, which keeps the count in static_pass_number (passes.cc:1317-1345). The first instance starts at -1 and holds the number of copies as a negative value. Each clone decrements that value and takes the negated result. After the second NEXT_PASS (pass_ccp) the first instance holds -2 and the clone holds 2, and after the fifth the first instance holds -5 and the clones hold 2 to 5. A pass whose name starts with * is not counted.

When the tree is complete, register_dump_files walks it depth first and registers each pass whose name does not start with * (passes.cc:896-910). The comment above it says that the walk is "independent of whether the pass is enabled or not" (:823-825). Each registration builds the names of one instance:

  /* See below in next_pass_1.  */
  num[0] = '\0';
  if (pass->static_pass_number != -1)
    sprintf (num, "%u", ((int) pass->static_pass_number < 0
			 ? 1 : pass->static_pass_number));
  ...
  name = strchr (pass->name, ' ');
  name = name ? name + 1 : pass->name;
  dot_name = concat (".", name, num, NULL);
  ...
  flag_name = concat (prefix, name, num, NULL);
  glob_name = concat (prefix, name, NULL);
  ...
  id = dumps->dump_register (dot_name, flag_name, glob_name, dkind,
			     optgroup_flags,
			     true);
  set_pass_for_id (id, pass);
  full_name = concat (prefix, pass->name, num, NULL);
  register_pass_name (pass, full_name);

Listing 5.3-8: the names of one pass instance, abridged (gcc/passes.cc:846-890).

The ternary prints a negative static_pass_number as 1, so the first instance is ccp1. A pass with one instance keeps -1 and gets no number, so the expand pass is rtl-expand, not rtl-expand1. The prefix comes from type, not from the position of the pass in the tree. For the second ccp instance, the listing builds four strings:

StringValueUsed by
dot_name.ccp2the end of the dump file name
flag_nametree-ccp2-fdump-tree-ccp2
glob_nametree-ccp-fdump-tree-ccp, for every instance
full_nametree-ccp2-fenable-, -fdisable- and -fdump-passes

-fdump- and -fdisable- read different strings. -fdump- first looks for an exact switch, and only if none matches does it try the glob names (dumpfile.cc:1935-1940). tree-ccp matches the glob name of all five instances. -fdisable- looks the name up in the map of full names, which holds only tree-ccp1 to tree-ccp5, and reports an unknown pass (passes.cc:1091-1097). To switch off the second instance, you write -fdisable-tree-ccp2.

The GCC manual states the rule for -fenable- and -fdisable- under Developer Options: if a pass is "statically invoked in the compiler multiple times, the pass name should be appended with a sequential number starting from 1". It does not say that a dump option without the number takes every instance.

dump_register also gives out the number in the dump file name, from a counter that increases by one for each registration (dumpfile.cc:215-221). Chapter 5.2 follows that number into the file name, and shows why it does not change with -O and why a pass added after the constructor gets a late number. Plugins add their passes that way, through the PLUGIN_PASS_MANAGER_SETUP event (plugin.cc:458-461). The plugin API is the internals manual's to document, and it covers this event under Interacting with the pass manager.

gate and execute_one_pass: one pass tree for -O0, -O2 and -Og

One pass tree serves -O0, -O2 and -Og because execute_one_pass asks each pass for its gate before it runs anything, and the answer of a container covers every pass below it. The walk over a list asks a container first, and walks its sub-list only if the answer is yes. In Listing 5.3-9, watch the if that calls execute_one_pass and then descends into pass->sub:

static void
execute_pass_list_1 (opt_pass *pass)
{
  do
    {
      gcc_assert (pass->type == GIMPLE_PASS
		  || pass->type == RTL_PASS);

      if (cfun == NULL)
	return;
      if (execute_one_pass (pass) && pass->sub)
	execute_pass_list_1 (pass->sub);
      pass = pass->next;
    }
  while (pass);
}

Listing 5.3-9: walking a list (gcc/passes.cc:2758-2773).

execute_one_pass returns false when the pass does not run, and then the sub-list does not run either. next is followed in both cases. In all_passes the walk happens once for each function, so each gate gets each function as its argument. The assertion admits GIMPLE and RTL passes only. The IPA lists use the other walker, the one that runs a GIMPLE sub-list on each function in turn.

The answer for one pass comes from three places, in the lines that Chapter 5.2 quoted for the dump files:

  /* Check whether gate check should be avoided.
     User controls the value of the gate through the parameter "gate_status". */
  gate_status = pass->gate (cfun);
  gate_status = override_gate_status (pass, current_function_decl, gate_status);

  /* Override gate with plugin.  */
  invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);

  if (!gate_status)

Listing 5.3-10: the gate decision in execute_one_pass (gcc/passes.cc:2603-2611).

First comes the gate of the pass. Then override_gate_status applies -fenable- and -fdisable- to that answer: a disabled pass does not run, and an enabled pass runs even if its own gate said no, as long as the walk reaches it (passes.cc:2434-2450). Last, a plugin can change the answer. When the answer is yes, execute_one_pass opens the dump file, runs the start TODOs, checks properties_required when checking is enabled, calls execute, runs the finish TODOs and writes the dump (passes.cc:2644-2740). A dump file exists only for an instance that ran.

The two large GIMPLE groups in all_passes are containers, and their gates test the same two variables in opposite ways. *all_optimizations returns optimize >= 1 && !optimize_debug (passes.cc:568-571), and *all_optimizations_g returns optimize >= 1 && optimize_debug (:606-609). -Og sets optimize to 1 and optimize_debug to 1 (opts.cc:795-801), so -Og selects the second group. For the ccp instances of count.c, the gates give this result:

InstanceContainer-O0-O2-Og
ccp1pass_all_early_optimizationsdoes not runrunsruns
ccp2 to ccp4*all_optimizationsdoes not runrunsdoes not run
ccp5*all_optimizations_gdoes not rundoes not runruns

At -O0 the containers say no, and the instances inside them would also say no, because -O0 does not set flag_tree_ccp. -fenable- changes only the answer of the pass it names, and a container that says no stops the walk before that pass is asked. The two container names start with a star (passes.cc:550, :588), so neither container has a dump file or an option. No -fenable- can name them, so if you compile with -O0 -fenable-tree-ccp2, ccp2 still does not run.

pass_expand in all_passes: GIMPLE and RTL passes in one list

all_passes holds both the GIMPLE passes and the RTL passes, with pass_expand as an ordinary NEXT_PASS between them. The RTL passes follow it in the same list, under pass_rest_of_compilation, so one walk of one list takes each function from GIMPLE to RTL:

  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.3-11: where all_passes changes from GIMPLE to RTL (gcc/passes.def:453-457).

cgraph_node::expand makes one call to execute_pass_list with all_passes for each function (cgraphunit.cc:1859), and that one call goes all the way to pass_final (passes.def:563). The boundary is in the descriptor of the expand pass, as properties:

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.3-12: the expand pass descriptor (gcc/cfgexpand.cc:6973-6987).

expand needs SSA form and lowered GIMPLE. It provides PROP_rtl, and it destroys PROP_ssa and PROP_gimple. After each pass, execute_one_pass asserts that an RTL pass did not run on a function that still has PROP_gimple (passes.cc:2742-2744). What expand does inside its execute is the subject of Chapter 5.5.

-flto without -ffat-lto-objects: cc1 stops after the IPA summaries

With -flto and no -ffat-lto-objects, cc1 stops after the summaries of the regular IPA passes, although a normal compile runs all three IPA lists in cc1. A plain -flto compile therefore writes no RTL dump. The execute stage runs in lto1 at link time, and Chapter 1.13 shows how the driver gets there.

The regular IPA passes first compute their summaries (cgraphunit.cc:2252-2253). With -flto, cc1 writes those summaries into the object (:2272-2276). It skips the execute stage of the regular IPA passes unless you also give -ffat-lto-objects (:2283-2286). Without that option, symbol_table::compile then returns before the late IPA list and before all_passes (:2354-2361), so cc1 runs no expand and no RTL pass.

At link time, lto1 runs the same code with in_lto_p set, and that test lets it execute the regular IPA list. The internals manual describes lto1 in its LTO chapter. The GCC manual documents -ffat-lto-objects under Optimize Options.

Checking gates with -fdump-passes: each pass asked alone

-fdump-passes prints the whole pass tree, with the answer that each pass gives alone for the current options. It gives you the names that -fdisable- accepts, but it does not apply the gate of a container, so an ON line does not prove that a pass runs. The GCC manual documents it in one sentence under Developer Options: it prints on standard error "the list of optimization passes that are turned on and off by the current command-line options".

$ gcc -O2 -fdump-passes -c count.c 2>&1 | grep -E 'ccp|all_optimizations'
$ gcc -Og -fdump-passes -c count.c 2>&1 | grep -E 'ccp|all_optimizations'
$ gcc -O2 -fdisable-tree-ccp2 -fdump-passes -c count.c 2>&1 | grep ccp

finalize_compilation_unit calls the printer once for each translation unit, before it compiles any function (cgraphunit.cc:2569-2570). The printer walks the five lists (passes.cc:1008-1024) and indents each level by three columns. For each pass it prints the full name, such as tree-ccp2, or the star name of a container, then ON or OFF (:963-983). When -fenable- or -fdisable- changed the answer, it adds (FORCED_ON) or (FORCED_OFF), so the third command marks tree-ccp2 as (FORCED_OFF).

At -O2 the printer shows tree-ccp5 as ON, because flag_tree_ccp is set, below a line that shows *all_optimizations_g as OFF, and the fifth instance still does not run. In the same way, if you add -fenable-tree-ccp2 at -O0, the printer shows tree-ccp2 as ON, marked (FORCED_ON), although ccp2 does not run. The printer also asks the gates about a dummy function with an empty body (passes.cc:1011, function.cc:4981-5003), not about count_divisible. A gate that reads the function, such as the gate of *pre_slp_scalar_cleanup (passes.cc:772-776), can give a different answer for your code.

From .ccp2 to tree-ssa-ccp.cc: finding the source of a pass

A dump file that ends in .ccp2 leads to tree-ssa-ccp.cc in four steps. The first three follow the route of Chapter 5.2, from the dump name to the pass_data string and the NEXT_PASS lines. The fourth reads the gates above the instance, because they decide whether the file appears at all:

  1. Split the name. ccp is the name field of the descriptor, and 2 is the instance number.
  2. Find the descriptor. Search the source for the name as a string. The search finds pass_data_ccp in gcc/tree-ssa-ccp.cc, and the class next to it holds the gate and the execute.
  3. Find the position. The factory of that class is make_pass_ccp, so the passes.def entry is pass_ccp. The second NEXT_PASS (pass_ccp line in the file is instance 2 (passes.def:204).
  4. Read the gates above it. Instance 2 is in the sub-list of pass_all_optimizations, so it runs only if that gate and its own gate agree.
$ grep -rn '"ccp", /\* name \*/' gcc/
$ grep -n 'NEXT_PASS (pass_ccp' gcc/passes.def

Step 2 matters because the name and the class do not always agree. evrp and vrp are two descriptors for one class, pass_vrp, and passes.def calls the first one pass_early_vrp (tree-vrp.cc:1277-1301, :1357-1367). For a target pass, search the *-passes.def file of the target as well as passes.def. Its instance number counts the lines that the Awk script wrote, not only the lines in passes.def.

Three things to carry away

The pass tree is built once, before cc1 reads its options. -O changes which gates say yes. It does not change the tree, the names or the numbers.

ccp2 is the second NEXT_PASS (pass_ccp) line, not the second run. An instance can exist in the tree and never run.

The gate of a container decides for its whole sub-list. That is how -O0, -O2 and -Og share one tree, and no -fenable- can open a container that says no.

Things that surprise people

-fdisable-tree-ccp is an error. Only the dump options fall back to the name without the instance number.

GIMPLE passes and RTL passes are in one list. all_passes runs from pass_fixup_cfg through pass_expand to pass_final, for each function.

With -flto and no -ffat-lto-objects, cc1 runs no RTL pass. It stops after the IPA summaries.

-fdump-passes can print ON for a pass that never runs. It does not apply the gate of the container above the pass.

Documentation coverage

The GCC manual documents the options. Developer Options (gcc/doc/invoke.texi:20326) gives the dump file name and its numbering rule (:20349-20362), -fdump-passes (:20879-20882), a procedure to find the dump of a pass (:20977-21003), and -fenable- and -fdisable- with examples (:21202-21273). Optimize Options gives -ftree-ccp (:14073) and -ffat-lto-objects (:15216).

The internals manual has a Passes chapter (gcc/doc/passes.texi:7). Its Pass manager node (:146-180) is short. It names tree-optimize.c, a file that does not exist in GCC 15.2.0. It states the rules for star names and for names with a space, and it ends with a TODO. The Tree SSA passes node gives the source file of each pass, ccp at :710-715. Interacting with the pass manager (gcc/doc/plugins.texi:264) documents register_pass for plugins.

What is not documented:

  • The format of passes.def, its five lists and its nesting, and the Awk script that turns it into pass-instances.def.
  • That an instance number counts NEXT_PASS lines, so an instance can exist and never run.
  • That a pass that occurs twice must implement clone, and a pass with an argument must implement set_pass_param.
  • That the gate of a container decides for its whole sub-list, and that -O0, -O2 and -Og select their passes this way.
  • That the GIMPLE passes and the RTL passes share all_passes, with expand as one pass among them.
  • Which code runs which list. Only the comment in Listing 5.3-3 says it, and some of its function names are out of date.
  • That -fdump-passes asks each gate alone, about an empty dummy function.
  • That -fdump-tree-NAME without a number matches every instance.
  • That the tree and the dump numbers exist before cc1 reads its options. The manual says that "target-specific passes" get numbers above final. In GCC 15.2.0 that is true for passes that register_pass adds at run time, such as those of SH. It is not true for the x86, Arm and AArch64 passes that PASSES_EXTRA adds.

Next: 5.4 Gimplification: from GENERIC to GIMPLE in SSA form. The gimplifier turns count.c into GIMPLE, and its && becomes more statements than the source suggests.