DOI: 10.1145/3369754
Article development led by
queue.acm.org
A practical journey.
BY MATT GODBOLT
COMPILERS ARE A necessary technology to turn high-level, easier-to-write code into efficient machine code
for computers to execute. Their sophistication at
doing this is often overlooked. You may spend a lot
of time carefully considering algorithms and fighting
error messages but perhaps not enough time looking
at what compilers are capable of doing.
This article introduces some compiler and code generation concepts,
and then shines a torch over a few of the
very impressive feats of transformation
your compilers are doing for you, with
some practical demonstrations of my
favorite optimizations. I hope you will
gain an appreciation for what kinds
of optimizations you can expect your
compiler to do for you, and how you
might explore the subject further. Most
of all, you may learn to love looking at
the assembly output and may learn to
respect the quality of the engineering
in your compilers.
The examples shown here are in C
or C++, which are the languages I have
had the most experience with, but
many of these optimizations are also
available in other compiled languages.
Indeed, the advent of front-end-agnos-
tic compiler toolkits such as LLVM3
means most of these optimizations
work in the exact same way in languag-
es such as Rust, Swift, and D.
I have always been fascinated by
what compilers are capable of. I spent
a decade making video games where ev-
ery CPU cycle counted in the war to get
more sprites, explosions, or complicat-
ed scenes on the screen than our com-
petitors. Writing custom assembly, and
reading the compiler output to see what
it was capable of, was par for the course.
Fast-forward five years and I was at a
trading company, having switched out
Optimizations
in C++
Compilers