Project Stage 2
Looping around thinking to write it down...
From 2 000 lines of red to a clean GCC build 🚦
“Nothing teaches you GCC internals faster than fixing your own pass.”
1 Context
Goal: create a GIMPLE‑level pass that prints
NOPRUNE:for the first function with a unique structure and
PRUNE:for every subsequent clone (same structure).Directory layout
~/git/gcc/ ← GCC source (trunk)
~/gcc-build-001/ ← out‑of‑tree build
~/gcc-test-001/ ← install prefix
└─ gcc/
├─ tree-my-pass.cc ← Stage 1 helper
└─ tree-cloneprune.cc ← Stage 2 pass
2 Initial implementation
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#undef optimize // avoid options.h macro
#include "tree-pass.h"
#include "context.h"
#include "tree.h" // must precede gimple.h
#include "tree-core.h"
#include "function.h"
#include "basic-block.h"
#include "gimple.h"
#include "gimple-iterator.h"
#include "cgraph.h"
#include <string>
#include <unordered_map>
#include <vector>
Generate a signature by concatenating each GIMPLE opcode;
3 Compilation hell & fixes
| # | Screenshot | GCC error | Root cause | Fix |
| 1 | wall‑of‑red | TREE_CODE / NULL_TREE / SSA_NAME_DEF_STMT not declared | tree.h was included after gimple.h | move #include "tree.h" above gimple.h |
| 2 | optimize‑macro | expected initializer before '.' token | backend.h pulls options.h; macro optimize collides with pass_data | #undef optimize right after #include "backend.h" |
| 3 | gsi_ errors | gsi_start_bb / gsi_next not declared | forgot iterator helpers | add #include "gimple-iterator.h" |



After patches, tree-cloneprune.o compiled cleanly (only the benign todo_flags_finish warning).
4 Linking the pass
# gcc/Makefile.in
OBJS = … \
tree-my-pass.o \
tree-cloneprune.o
// gcc/tree-pass.h
extern gimple_opt_pass *make_tree_my_pass(gcc::context *);
extern gimple_opt_pass *make_pass_cloneprune(gcc::context *);
// gcc/passes.def (late in pipeline)
NEXT_PASS(tree_my_pass)
NEXT_PASS(pass_cloneprune)
Full rebuild:
cd ~/gcc-build-001
make -j$(nproc)
make install
6 Key take‑aways
Header order is sacred in GCC;
tree.hmust come beforegimple.h.optimizemacro fromoptions.his a silent build killer — always#undef optimizein new passes.Incremental rebuilds (
make file.o) cut turn‑around from minutes to seconds.