SlideShare ist ein Scribd-Unternehmen logo
1 von 46
GCC/G++
OPERATING SYSTEMS
What is GNU?
The GNU project was started in 1984 to create a complete Unix-like
operating system as free software, in order to promote freedom and
cooperation among computer users and programmers. Every Unix-like
operating system needs a C compiler, and as there were no free
compilers in existence at that time, the GNU Project had to develop one
from scratch. The work was funded by donations from individuals and
companies to the Free Software Foundation, a non-profit organization set
up to support the work of the GNU Project.
What is GCC?
GCC stands for “GNU Compiler Collection”. It is an integrated distribution of compilers
for several major programming languages: C, C++, Objective-C, Objective-
C++, Java, Fortran, Ada, and Go. The abbreviation GCC has multiple meanings in
common use. The current official meaning is “GNU Compiler Collection”, which refers
generically to the complete suite of tools. The name historically stood for “GNU C
Compiler”. The name is also used when speaking of the language-
independent component of GCC: code shared among the compilers for all supported
languages. The language-independent component of GCC includes the majority of the
optimizers, as well as the “back ends” that generate machine code for various
processors. The part of a compiler that is specific to a particular language is called the
“front end”.
What is G++?
Most of the compilers for languages other than C have their own names. The “G++” is
the compiler of C++. When compiling these languages, we might refer to that compiler
by its own name, or as “GCC”. Either is correct.
G++ is a compiler, not merely a preprocessor. G++ builds object code directly from C++
program source. There is no intermediate C version of the program. Avoiding an
intermediate C representation of the program means that it is possible to get better
object code, and better debugging information. The GNU debugger, GDB, works with
this information in the object code to give a comprehensive C++ source-level editing
capabilities.
GCC Command Options
When GCC invoked, it normally does preprocessing, compilation, assembly and linking.
The “overall options” allows to stop this process at an intermediate stage. For example,
the -c option says not to run the linker. Then the output consists of object files output by
the assembler. Other options are passed on to one stage of processing. Some options
control the preprocessor and others the compiler itself. Yet other options control the
assembler and linker.
Most of the command line options those used with GCC are useful for C programs; when
an option is only useful with another language (usually C++), the explanation says so
explicitly. If the description for a particular option does not mention a source language, it
is possible to use that option with all supported languages.
GCC Command Options (2)
The GCC compiler accepts both single-letter options, such as -o, and multi-letters, such
as -ansi. Because it accepts both types of options, it is not possible to group multiple
single-letter options together as it may be done in many GNU and Unix/Linux programs.
For example, the multi-letter option -pg is not the same as the two single-letter options -p
-g. The -pg option creates extra code in the final binary that outputs profile information
for the GNU code profiler, gprof. On the other hand, the -p -g options generate extra
code in the resulting binary that produces profiling information for use by the prof code
profiler (-p) and causes GCC to generate debugging information using the operating
system’s normal format (-g).
GCC Command Options (3)
Despite its sensitivity to the grouping of multiple single-letter options, it is possible to mix
the order of options and compiler arguments on the GCC command line. That is,
invoking GCC as
gcc -pg -fno-strength-reduce -g myprog.c -o myprog
has the same result as
gcc myprog.c -o myprog -g -fno-strength-reduce –pg
In some situations, order does matter if several options of the same kind been used. For
example, the -I option specifies the directory or directories to search for include files. So
if -I specified several times, GCC searches the listed directories in the order specified.
GCC Command Options (4)
To compile a single source file, invoke GCC passing the name of the source file as the
argument.
$ gcc myprog.c
$ ls –l
By default, the result on Linux and Unix systems is an executable file named a.out in the
current directory, which you execute by typing ./a.out. On Cygwin systems, you will wind
up with a file named a.exe that you can execute by typing either ./a or ./a.exe. To define
the name of the output file that GCC produces, use the -o option, as illustrated in the
following example:
$ gcc myprog.c -o runme
GCC Command Options (5)
For compiling multiple source files, they may simply specified on the GCC command
line, as in the following example, which leaves the compiled and linked executable in the
file named showdate:
$ gcc showdate.c helper.c –o showdate
To compile these files incrementally and eventually to link them into a binary, it is
possible to use the –c option to halt compilation after producing an object file, as in the
following example:
$ gcc -c showdate.c
$ gcc -c helper.c
$ gcc showdate.o helper.o –o showdate
G++ Command Options
The G++ compiler also accepts both single-letter options and multi-letter options as
GCC. And also it is not possible to group multiple single-letter options due to fact that it
accepts both types of options together.
Despite its sensitivity to the grouping of multiple single-letter options, it is free to mix the
order of options and compiler arguments on the command line. That is, invoking G++ as
g++ -pg -fno-strength-reduce -g myprog.c -o myprog
has the same result as
g++ myprog.c -o myprog -g -fno-strength-reduce -pg
G++ Command Options (2)
In some situations, order does matter if several options of the same kind been used. For
example, the -I option specifies the directory or directories to search for include files. So
if -I specified several times, it searches the listed directories in the order specified.
To compile a single source file, myprog.cc, just invoke G++, passing the name of the
source file as the argument.
$ g++ myprog.cc
$ ls -l
G++ Command Options (3)
By default, the result on Linux and Unix systems is an executable file named a.out in the
current directory, which may be executed by typing ./a.out. On Cygwin systems, result is
a file named a.exe that you can execute by typing either ./a or ./a.exe. To define the
name of the output file that G++ produces, use the -o option, as illustrated in the
following example:
$ g++ myprog.cc -o runme
$ ls –l
For compiling multiple source files using G++, and to get file named showdate:
$ g++ showdate.cc helper.cc –o showdate
G++ Command Options (4)
If you want to compile these files incrementally and eventually link them into a binary,
you can use the –c option to halt compilation after producing an object file, as in the
following example:
$ g++ -c showdate.cc
$ g++ -c helper.cc
$ g++ showdate.o helper.o –o showdate
$ ls –l
G++ Command Options (5)
All GCC compilers evaluate filename suffixes to identify the type of compilation that they will
perform.
Suffix Operation
.C C++ source code to preprocess.
.cc C++ source code to preprocess. (Standard extension for C++ source files.)
.cpp C++ source code to preprocess.
.cp C++ source code to preprocess.
.c++ C++ source code to preprocess
.cxx C++ source code to preprocess
.ii C++ source code not to preprocess.
Options Summary
Here is a summary of all the options, grouped by type.
Overall Options: Controlling the kind of output: an executable, object files, assembler
files, or preprocessed source.
C Dialect Options: Controlling the variant of C language compiled.
C++ Dialect Options: Variations on C++.
Objective-C Dialect Options: Variations on Objective-C.
Language Independent Options: Controlling how diagnostics should be formatted.
Warning Options: How picky should the compiler be?
Options Summary (2)
Debugging Options: Symbol tables, measurements, and debugging dumps.
Optimize Options: How much optimization?
Preprocessor Options: Controlling header files and macro definitions. Also, getting
dependency information for Make.
Assembler Options: Passing options to the assembler.
Link Options: Specifying libraries and so on.
Directory Options: Where to find header files and libraries. Where to find the compiler
executable files.
Spec Files: How to pass switches to sub-processes.
Target Options: Running a cross-compiler, or an old version of GCC.
Optimization of Code with GCC
These days, compilers are pretty smart. They can perform all sorts of code
transformations make compiled code run faster. In most situations, faster is better than
smaller, because disk space and memory are quite cheap for desktop users.
However, for embedded systems small is often at least as important as fast because of a
commonplace environment consisting of extreme memory constraints and no disk
space, making code optimization a very important task.
In the absence of optimization, GCC’s goal, besides compiling code that works, is to
keep compilation time to a minimum and generate code that runs predictably in a
debugging environment.
Optimization Theory
Optimization refers to analyzing a straightforward compilation’s resulting code to
determine how to transform it to run faster, consume fewer resources, or both. Compilers
that do this are known as optimizing compilers, and the resulting code is known as
optimized code.
To produce optimized code, an optimizing compiler performs one or more
transformations on the source code provided as input. The purpose is to replace less
efficient code with more efficient code while at the same time preserving the meaning
and ultimate results of the code.
Optimization Theory (2)
Optimizing compilers use several methods to determine where generated code can be
improved. One method is control flow analysis, which examines loops and other
control constructs, such as if-then and case statements, to identify the execution paths a
program might take and, based on this analysis, determine how the execution path can
be streamlined.
Another typical optimization technique examines how data is used in a program, a
procedure known as data flow analysis. Data flow analysis examines how and when
variables are used (or not) in a program and then applies various set equations to these
usage patterns to derive optimization opportunities.
Optimization Techniques – Code
Motion
It is an optimization technique to eliminate redundant code through common sub-
expression elimination. It does not entirely remove common sub-expressions, but
attempts to reduce the number of times they occur by relocating them to more optimal
locations in an intermediate representation of the code. For example, in nested loops or
other control constructs, intermediate value calculations may be performed more times
than necessary. In order to optimize, compilers can move these calculations to locations
where they will be executed less frequently, but will still produce the same result. Code
motion that specifically moves redundant calculations outside the scope of a loop is
referred to as loop-invariant code motion. It is also used in a technique that is related to
common sub-expression elimination known as partial redundancy elimination.
Optimization Techniques – Common
Sub-expression Elimination
Eliminating redundant computations is a standard optimization mechanism because it
reduces the number of instructions that a program has to execute in order to arrive at the
same result.
For example, given an expression whose value is computed only once, each subsequent
occurrence of that expression can be eliminated and its value used instead, if the values
of the variables in the expression have not changed since first computed. The
subsequent occurrences of the expression are called common sub-expressions.
Optimization Techniques – Common
Sub-expression Elimination (2)
Example:
#define STEP 3
#define SIZE 100
int i, j, k;
int p[SIZE];
for (i = 0; i < SIZE; ++i) {
j = 2 * STEP;
k = p[i] * j; }
The expression j = 2 * STEP in the for loop is a common sub-expression because its
value is computed before entering the loop and its constituent variable, STEP (actually a
predefined value), has not changed.
Optimization Techniques – Common
Sub-expression Elimination (3)
Common sub-expression elimination (CSE) removes the repeated computation of j in the
for loop. After CSE, the for loop might look like the following:
j = 2 * STEP;
for (i = 0; i < 100; ++i) {
k = p[i] * j; }
Admittedly, the example is contrived, but it makes the point: CSE has eliminated 100
redundant computations of j. CSE improves performance by eliminating unnecessary
computations and typically also reduces the size of the resulting binary by eliminating
unnecessary instructions.
Optimization Techniques – Constant
Folding
It is an optimization technique that eliminates expressions that calculate a value that can
already be determined when the program is compiled. These are typically calculations
that only reference constant values or expressions that reference variables whose
values are constant. For example:
n = 10 * 20 * 400;
i = 10;
j = 20;
ij = i * j;
In the latter case, the assignments to the variables i and j could also be eliminated if
these variables were not used elsewhere in the code.
Optimization Techniques – Copy
Propagation Transformations
It eliminate cases in which values are copied from one location or variable to another in
order to simply assign their value to another variable. Given an assignment of the form f
= g, subsequent uses of f, called copies, use g instead. Consider a code fragment such
as the following:
i = 10;
x[j] = i;
y[MIN] = b;
b = i; //After CPT it looks like “b=10”
Optimization Techniques – Dead
Code Elimination
Dead code elimination (DCE) is the term used for optimizations that remove code that
doesn’t actually do anything permanent or useful. Unreachable code elimination is a
related optimization that removes code that the compiler can identify as impossible to
reach. For example:
if ( i == 10 ) { . . . }
else { . . .
if ( i == 10) { . . . }
}
The nested test for whether i is equal to 10 can be eliminated because it can never be
reached.
Optimization Techniques – If-
Conversion
If-conversion is a technique where branch constructs, such as large if-then-else if-else
constructs, are broken into separate if statements to simplify generated code, provide
opportunities for further optimization, and eliminate jumps and branches wherever
possible.
Optimization Techniques – Inlining
It is an optimization technique where code performance can improve by replacing
complex constructs and even function calls with inline representations of the construct or
function call. Code inlining, or loop unrolling, are terms for replacing all or portions of a
loop with an explicit series of instructions. Function inlining is the term for replacing
function calls with the explicit set of instructions that are performed by the function. In
general, inlining can reduce code complexity and provide higher performance than would
be required by branching that would be done otherwise. It often also provides
opportunities for common sub-expression elimination and code motion optimization.
GCC Optimization Basics
GCC uses a sequence of different intermediate representations of code before
generating binaries. Converting language-specific code into simpler forms provides
several primary advantages:
- Breaking code into simpler, more low-level constructs exposes opportunities for
optimization that may not be directly visible in source code.
- Using intermediate representations enables the simpler form to more easily and
readably represent parse trees that are otherwise at varying levels of complexity.
- Using a simpler form enables the GCC compilers to share optimization mechanisms,
regardless of the language in which your code was originally written.
GCC Optimization Basics (2)
Traditionally, GCC has always internally used an intermediate form known as Register
Transfer Language (RTL), which is a very low-level language into which all code
compiled with a GCC compiler (in any high-level language) is transformed before object
code generation begins. Transforming high-level input languages into an intermediate,
internal form is a time-tested mechanism for exposing opportunities for optimization. As
a very low-level representation of code, GCC’s RTL lends itself well to optimizations that
are similarly low-level, such as register allocation and stack and data optimizations.
Because it is relatively low-level, RTL is not as good as one would like in terms of
supporting higher-level optimizations related to data types, array and variable
references, and overall data and control flow within code.
GCC Optimization Basics (3)
With GCC 4.0, the fine makers of the GCC compilers introduced a new intermediate
form, static single assignment (SSA), which operates on the parse trees that are
produced from code by GCC compilers, and is thus known as Tree SSA. It is interesting
to note that 4.0 and later GCC compilers use two intermediate forms before arriving at a
Tree SSA representation, known as GENERIC and GIMPLE. A GENERIC representation
is produced by eliminating language-specific constructs from the parse tree that is
generated from code, and a GIMPLE representation is then produced from the
GENERIC representation by simplifying address references within the code. It provides
several additional points at which optimizations can occur at as high a level as possible
before zooming into the depths of the RTL.
Architecture-Independent
Optimizations
GCC’s most well-known optimization switches are –O; its variant -On, where n is an
integer between 0 and 3; and -Os. -OO turns off optimization. -O and -O1 (which are
level 1) are equivalent, telling GCC to optimize code. With -O or -O1, the compiler
attempts to minimize both code size and execution time without dramatically increasing
compilation time. Using -O2 and -O3 will increase the level from that requested by -
O1, while still invoking the optimization options requested by -O1. To minimize code
size, use option -Os. To disable one of them while leaving the others enabled, negate the
option using no- between -f and the optimization name. For example, to disable deferred
stack pops, the command line might resemble this:
$ gcc myprog.c -o myprog -O1 -fno-defer-pop
Architecture-Independent Level 1
GCC Optimizations
Architecture-Independent Level 1
GCC Optimizations (2)
Architecture-Independent Level 1
GCC Optimizations (3)
Architecture-Independent Level 2
GCC Optimizations
-O2 optimization (level 2 optimization) includes all level 1 optimizations plus the
additional optimizations listed below. Applying these optimizations will lengthen the
compile time, but as a result a measurable increase should be seen in the resulting
code’s performance, or, rather, a measurable decrease in execution time.
Architecture-Independent Level 2
GCC Optimizations (2)
Architecture-Independent Level 2
GCC Optimizations (3)
Architecture-Independent Level 2
GCC Optimizations (4)
Architecture-Independent GCC
Optimizations for Code Size
The -Os option is becoming increasingly popular because it applies all of the level 2
optimizations except those known to increase the code size. -Os also applies additional
techniques to attempt to reduce the code size. Code size, in this context, refers to a
program’s memory footprint at runtime rather than its on-disk storage requirement. In
particular, -Os disables the following optimization flags (meaning they will be ignored if
specified in conjunction with -Os):
• -falign-functions
• -falign-jumps
• -falign-labels
• -falign-loops
Architecture-Independent GCC
Optimizations for Code Size (2)
• -fprefetch-loop-arrays
• -freorder-blocks
• -freorder-blocks-and-partition
• -ftree-ch
You might find it instructive to compile a program with -O2 and -Os and compare runtime
performance and memory footprint. For example, I have found that recent versions of the
Linux kernel have nearly the same runtime performance when compiled with -O2 and -
Os, but the runtime memory footprint is 15 percent smaller when the kernel is compiled
with -Os. Naturally, your mileage may vary and, as always, if it breaks, you get to keep all
of the pieces.
Architecture-Independent Level 3
GCC Optimizations
Specifying the –O3 optimization option enables all level 1 and level 2 optimizations plus
the following:
• -fgcse-after-reload: Performs an extra load elimination pass after reloading
• -finline-functions: Integrates all “simple” functions into their callers
• -funswitch-loops: Moves branches with loop invariant conditions out of loops
Note: If you specify multiple -O options, the last one encountered takes precedence.
Thus, given the command gcc -O3 foo.c bar.c -O0 -o baz, no optimization will be
performed because -O0 overrides the earlier -O3.
Architecture-Independent Manuel
GCC Optimization Flags
In addition to the optimizations enabled using one of the -O options, GCC has a number
of specialized optimizations that can only be enabled by specifically requesting them
using -f.
Architecture-Independent Manuel
GCC Optimization Flags (2)
Note: Not all of GCC’s optimizations can be controlled using a flag. GCC performs some
optimizations automatically and we cannot disable these optimizations when you request
optimization using -O.
Processor-Specific Optimizations
In reality, though, the target-specific switches are not properly optimizations in the traditional
sense. Rather, they are options that give GCC more information about the type of system
on which the code being compiled will run. GCC can use this additional information to
generate code that takes advantage of specific features or that works around known
misfeatures of the given processor.
Thanks for listening…

Weitere ähnliche Inhalte

Was ist angesagt?

Conan a C/C++ Package Manager
Conan a C/C++ Package ManagerConan a C/C++ Package Manager
Conan a C/C++ Package ManagerUilian Ries
 
GDB Rocks!
GDB Rocks!GDB Rocks!
GDB Rocks!Kent Chen
 
Advanced Debugging with GDB
Advanced Debugging with GDBAdvanced Debugging with GDB
Advanced Debugging with GDBDavid Khosid
 
SystemV vs systemd
SystemV vs systemdSystemV vs systemd
SystemV vs systemdAll Things Open
 
Implementation of k-means clustering algorithm in C
Implementation of k-means clustering algorithm in CImplementation of k-means clustering algorithm in C
Implementation of k-means clustering algorithm in CKasun Ranga Wijeweera
 
Git real slides
Git real slidesGit real slides
Git real slidesLucas Couto
 
Introduction to open_sbi
Introduction to open_sbiIntroduction to open_sbi
Introduction to open_sbiNylon
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesCharles Nutter
 
How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)SĹ‚awomir Zborowski
 
BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2 BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2 Linaro
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageShih-Hsiang Lin
 
U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New HardwareRuggedBoardGroup
 
Introduction to gdb
Introduction to gdbIntroduction to gdb
Introduction to gdbOwen Hsu
 
Build your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto projectBuild your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto projectYen-Chin Lee
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchlinuxlab_conf
 
Gitlab flow solo
Gitlab flow soloGitlab flow solo
Gitlab flow soloviniciusban
 
Grub2 Booting Process
Grub2 Booting ProcessGrub2 Booting Process
Grub2 Booting ProcessMike Wang
 

Was ist angesagt? (20)

Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Conan a C/C++ Package Manager
Conan a C/C++ Package ManagerConan a C/C++ Package Manager
Conan a C/C++ Package Manager
 
eBPF maps 101
eBPF maps 101eBPF maps 101
eBPF maps 101
 
GDB Rocks!
GDB Rocks!GDB Rocks!
GDB Rocks!
 
Advanced Debugging with GDB
Advanced Debugging with GDBAdvanced Debugging with GDB
Advanced Debugging with GDB
 
SystemV vs systemd
SystemV vs systemdSystemV vs systemd
SystemV vs systemd
 
Implementation of k-means clustering algorithm in C
Implementation of k-means clustering algorithm in CImplementation of k-means clustering algorithm in C
Implementation of k-means clustering algorithm in C
 
Git real slides
Git real slidesGit real slides
Git real slides
 
Introduction to open_sbi
Introduction to open_sbiIntroduction to open_sbi
Introduction to open_sbi
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for Dummies
 
How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)
 
BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2 BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming Language
 
U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New Hardware
 
Introduction to gdb
Introduction to gdbIntroduction to gdb
Introduction to gdb
 
Build your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto projectBuild your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto project
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratch
 
Gitlab flow solo
Gitlab flow soloGitlab flow solo
Gitlab flow solo
 
Grub2 Booting Process
Grub2 Booting ProcessGrub2 Booting Process
Grub2 Booting Process
 
GCC
GCCGCC
GCC
 

Andere mochten auch

Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler designJanani Parthiban
 
Compiling Under Linux
Compiling Under LinuxCompiling Under Linux
Compiling Under LinuxPierreMASURE
 
NetBeans para Java, C, C++
NetBeans para Java, C, C++NetBeans para Java, C, C++
NetBeans para Java, C, C++Manuel Antonio
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1Dave Cross
 
Gccgdb
GccgdbGccgdb
Gccgdbselva raj
 
GCC Compiler as a Performance Testing tool for C programs
GCC Compiler as a Performance Testing tool for C programsGCC Compiler as a Performance Testing tool for C programs
GCC Compiler as a Performance Testing tool for C programsDaniel Ilunga
 
GEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkGEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkAlexey Smirnov
 
MinGw Compiler
MinGw CompilerMinGw Compiler
MinGw CompilerAvnish Patel
 
GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005Saleem Ansari
 

Andere mochten auch (13)

Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler design
 
Compiling Under Linux
Compiling Under LinuxCompiling Under Linux
Compiling Under Linux
 
NetBeans para Java, C, C++
NetBeans para Java, C, C++NetBeans para Java, C, C++
NetBeans para Java, C, C++
 
Gcc opt
Gcc optGcc opt
Gcc opt
 
C compilation process
C compilation processC compilation process
C compilation process
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
Gccgdb
GccgdbGccgdb
Gccgdb
 
GCC Compiler as a Performance Testing tool for C programs
GCC Compiler as a Performance Testing tool for C programsGCC Compiler as a Performance Testing tool for C programs
GCC Compiler as a Performance Testing tool for C programs
 
HRM - PM in GCC
HRM - PM in GCCHRM - PM in GCC
HRM - PM in GCC
 
GEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkGEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions Framework
 
MinGw Compiler
MinGw CompilerMinGw Compiler
MinGw Compiler
 
GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005
 
Deep C
Deep CDeep C
Deep C
 

Ă„hnlich wie G++ & GCC

GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?Saket Pathak
 
Comparing C and Go
Comparing C and GoComparing C and Go
Comparing C and GoMarcin Pasinski
 
Autotools pratical training
Autotools pratical trainingAutotools pratical training
Autotools pratical trainingThierry Gayet
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxNEHARAJPUT239591
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJmeharikiros2
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docMayurWagh46
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptxDanielle780357
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...bhargavi804095
 
Process (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oSProcess (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oSHarrytoye2
 
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019corehard_by
 
02 c++g3 d (1)
02 c++g3 d (1)02 c++g3 d (1)
02 c++g3 d (1)Mohammed Ali
 
Life of a Chromium Developer
Life of a Chromium DeveloperLife of a Chromium Developer
Life of a Chromium Developermpaproductions
 
Debugging of (C)Python applications
Debugging of (C)Python applicationsDebugging of (C)Python applications
Debugging of (C)Python applicationsRoman Podoliaka
 
From gcc to the autotools
From gcc to the autotoolsFrom gcc to the autotools
From gcc to the autotoolsThierry Gayet
 

Ă„hnlich wie G++ & GCC (20)

GNU Debugger
GNU DebuggerGNU Debugger
GNU Debugger
 
Gnu debugger
Gnu debuggerGnu debugger
Gnu debugger
 
GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?
 
Comparing C and Go
Comparing C and GoComparing C and Go
Comparing C and Go
 
Autotools pratical training
Autotools pratical trainingAutotools pratical training
Autotools pratical training
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
 
Process (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oSProcess (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oS
 
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
 
02 c++g3 d (1)
02 c++g3 d (1)02 c++g3 d (1)
02 c++g3 d (1)
 
GCC
GCCGCC
GCC
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
 
Life of a Chromium Developer
Life of a Chromium DeveloperLife of a Chromium Developer
Life of a Chromium Developer
 
Debugging of (C)Python applications
Debugging of (C)Python applicationsDebugging of (C)Python applications
Debugging of (C)Python applications
 
From gcc to the autotools
From gcc to the autotoolsFrom gcc to the autotools
From gcc to the autotools
 

KĂĽrzlich hochgeladen

Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 

KĂĽrzlich hochgeladen (20)

Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 

G++ & GCC

  • 2. What is GNU? The GNU project was started in 1984 to create a complete Unix-like operating system as free software, in order to promote freedom and cooperation among computer users and programmers. Every Unix-like operating system needs a C compiler, and as there were no free compilers in existence at that time, the GNU Project had to develop one from scratch. The work was funded by donations from individuals and companies to the Free Software Foundation, a non-profit organization set up to support the work of the GNU Project.
  • 3. What is GCC? GCC stands for “GNU Compiler Collection”. It is an integrated distribution of compilers for several major programming languages: C, C++, Objective-C, Objective- C++, Java, Fortran, Ada, and Go. The abbreviation GCC has multiple meanings in common use. The current official meaning is “GNU Compiler Collection”, which refers generically to the complete suite of tools. The name historically stood for “GNU C Compiler”. The name is also used when speaking of the language- independent component of GCC: code shared among the compilers for all supported languages. The language-independent component of GCC includes the majority of the optimizers, as well as the “back ends” that generate machine code for various processors. The part of a compiler that is specific to a particular language is called the “front end”.
  • 4. What is G++? Most of the compilers for languages other than C have their own names. The “G++” is the compiler of C++. When compiling these languages, we might refer to that compiler by its own name, or as “GCC”. Either is correct. G++ is a compiler, not merely a preprocessor. G++ builds object code directly from C++ program source. There is no intermediate C version of the program. Avoiding an intermediate C representation of the program means that it is possible to get better object code, and better debugging information. The GNU debugger, GDB, works with this information in the object code to give a comprehensive C++ source-level editing capabilities.
  • 5. GCC Command Options When GCC invoked, it normally does preprocessing, compilation, assembly and linking. The “overall options” allows to stop this process at an intermediate stage. For example, the -c option says not to run the linker. Then the output consists of object files output by the assembler. Other options are passed on to one stage of processing. Some options control the preprocessor and others the compiler itself. Yet other options control the assembler and linker. Most of the command line options those used with GCC are useful for C programs; when an option is only useful with another language (usually C++), the explanation says so explicitly. If the description for a particular option does not mention a source language, it is possible to use that option with all supported languages.
  • 6. GCC Command Options (2) The GCC compiler accepts both single-letter options, such as -o, and multi-letters, such as -ansi. Because it accepts both types of options, it is not possible to group multiple single-letter options together as it may be done in many GNU and Unix/Linux programs. For example, the multi-letter option -pg is not the same as the two single-letter options -p -g. The -pg option creates extra code in the final binary that outputs profile information for the GNU code profiler, gprof. On the other hand, the -p -g options generate extra code in the resulting binary that produces profiling information for use by the prof code profiler (-p) and causes GCC to generate debugging information using the operating system’s normal format (-g).
  • 7. GCC Command Options (3) Despite its sensitivity to the grouping of multiple single-letter options, it is possible to mix the order of options and compiler arguments on the GCC command line. That is, invoking GCC as gcc -pg -fno-strength-reduce -g myprog.c -o myprog has the same result as gcc myprog.c -o myprog -g -fno-strength-reduce –pg In some situations, order does matter if several options of the same kind been used. For example, the -I option specifies the directory or directories to search for include files. So if -I specified several times, GCC searches the listed directories in the order specified.
  • 8. GCC Command Options (4) To compile a single source file, invoke GCC passing the name of the source file as the argument. $ gcc myprog.c $ ls –l By default, the result on Linux and Unix systems is an executable file named a.out in the current directory, which you execute by typing ./a.out. On Cygwin systems, you will wind up with a file named a.exe that you can execute by typing either ./a or ./a.exe. To define the name of the output file that GCC produces, use the -o option, as illustrated in the following example: $ gcc myprog.c -o runme
  • 9. GCC Command Options (5) For compiling multiple source files, they may simply specified on the GCC command line, as in the following example, which leaves the compiled and linked executable in the file named showdate: $ gcc showdate.c helper.c –o showdate To compile these files incrementally and eventually to link them into a binary, it is possible to use the –c option to halt compilation after producing an object file, as in the following example: $ gcc -c showdate.c $ gcc -c helper.c $ gcc showdate.o helper.o –o showdate
  • 10. G++ Command Options The G++ compiler also accepts both single-letter options and multi-letter options as GCC. And also it is not possible to group multiple single-letter options due to fact that it accepts both types of options together. Despite its sensitivity to the grouping of multiple single-letter options, it is free to mix the order of options and compiler arguments on the command line. That is, invoking G++ as g++ -pg -fno-strength-reduce -g myprog.c -o myprog has the same result as g++ myprog.c -o myprog -g -fno-strength-reduce -pg
  • 11. G++ Command Options (2) In some situations, order does matter if several options of the same kind been used. For example, the -I option specifies the directory or directories to search for include files. So if -I specified several times, it searches the listed directories in the order specified. To compile a single source file, myprog.cc, just invoke G++, passing the name of the source file as the argument. $ g++ myprog.cc $ ls -l
  • 12. G++ Command Options (3) By default, the result on Linux and Unix systems is an executable file named a.out in the current directory, which may be executed by typing ./a.out. On Cygwin systems, result is a file named a.exe that you can execute by typing either ./a or ./a.exe. To define the name of the output file that G++ produces, use the -o option, as illustrated in the following example: $ g++ myprog.cc -o runme $ ls –l For compiling multiple source files using G++, and to get file named showdate: $ g++ showdate.cc helper.cc –o showdate
  • 13. G++ Command Options (4) If you want to compile these files incrementally and eventually link them into a binary, you can use the –c option to halt compilation after producing an object file, as in the following example: $ g++ -c showdate.cc $ g++ -c helper.cc $ g++ showdate.o helper.o –o showdate $ ls –l
  • 14. G++ Command Options (5) All GCC compilers evaluate filename suffixes to identify the type of compilation that they will perform. Suffix Operation .C C++ source code to preprocess. .cc C++ source code to preprocess. (Standard extension for C++ source files.) .cpp C++ source code to preprocess. .cp C++ source code to preprocess. .c++ C++ source code to preprocess .cxx C++ source code to preprocess .ii C++ source code not to preprocess.
  • 15. Options Summary Here is a summary of all the options, grouped by type. Overall Options: Controlling the kind of output: an executable, object files, assembler files, or preprocessed source. C Dialect Options: Controlling the variant of C language compiled. C++ Dialect Options: Variations on C++. Objective-C Dialect Options: Variations on Objective-C. Language Independent Options: Controlling how diagnostics should be formatted. Warning Options: How picky should the compiler be?
  • 16. Options Summary (2) Debugging Options: Symbol tables, measurements, and debugging dumps. Optimize Options: How much optimization? Preprocessor Options: Controlling header files and macro definitions. Also, getting dependency information for Make. Assembler Options: Passing options to the assembler. Link Options: Specifying libraries and so on. Directory Options: Where to find header files and libraries. Where to find the compiler executable files. Spec Files: How to pass switches to sub-processes. Target Options: Running a cross-compiler, or an old version of GCC.
  • 17. Optimization of Code with GCC These days, compilers are pretty smart. They can perform all sorts of code transformations make compiled code run faster. In most situations, faster is better than smaller, because disk space and memory are quite cheap for desktop users. However, for embedded systems small is often at least as important as fast because of a commonplace environment consisting of extreme memory constraints and no disk space, making code optimization a very important task. In the absence of optimization, GCC’s goal, besides compiling code that works, is to keep compilation time to a minimum and generate code that runs predictably in a debugging environment.
  • 18. Optimization Theory Optimization refers to analyzing a straightforward compilation’s resulting code to determine how to transform it to run faster, consume fewer resources, or both. Compilers that do this are known as optimizing compilers, and the resulting code is known as optimized code. To produce optimized code, an optimizing compiler performs one or more transformations on the source code provided as input. The purpose is to replace less efficient code with more efficient code while at the same time preserving the meaning and ultimate results of the code.
  • 19. Optimization Theory (2) Optimizing compilers use several methods to determine where generated code can be improved. One method is control flow analysis, which examines loops and other control constructs, such as if-then and case statements, to identify the execution paths a program might take and, based on this analysis, determine how the execution path can be streamlined. Another typical optimization technique examines how data is used in a program, a procedure known as data flow analysis. Data flow analysis examines how and when variables are used (or not) in a program and then applies various set equations to these usage patterns to derive optimization opportunities.
  • 20. Optimization Techniques – Code Motion It is an optimization technique to eliminate redundant code through common sub- expression elimination. It does not entirely remove common sub-expressions, but attempts to reduce the number of times they occur by relocating them to more optimal locations in an intermediate representation of the code. For example, in nested loops or other control constructs, intermediate value calculations may be performed more times than necessary. In order to optimize, compilers can move these calculations to locations where they will be executed less frequently, but will still produce the same result. Code motion that specifically moves redundant calculations outside the scope of a loop is referred to as loop-invariant code motion. It is also used in a technique that is related to common sub-expression elimination known as partial redundancy elimination.
  • 21. Optimization Techniques – Common Sub-expression Elimination Eliminating redundant computations is a standard optimization mechanism because it reduces the number of instructions that a program has to execute in order to arrive at the same result. For example, given an expression whose value is computed only once, each subsequent occurrence of that expression can be eliminated and its value used instead, if the values of the variables in the expression have not changed since first computed. The subsequent occurrences of the expression are called common sub-expressions.
  • 22. Optimization Techniques – Common Sub-expression Elimination (2) Example: #define STEP 3 #define SIZE 100 int i, j, k; int p[SIZE]; for (i = 0; i < SIZE; ++i) { j = 2 * STEP; k = p[i] * j; } The expression j = 2 * STEP in the for loop is a common sub-expression because its value is computed before entering the loop and its constituent variable, STEP (actually a predefined value), has not changed.
  • 23. Optimization Techniques – Common Sub-expression Elimination (3) Common sub-expression elimination (CSE) removes the repeated computation of j in the for loop. After CSE, the for loop might look like the following: j = 2 * STEP; for (i = 0; i < 100; ++i) { k = p[i] * j; } Admittedly, the example is contrived, but it makes the point: CSE has eliminated 100 redundant computations of j. CSE improves performance by eliminating unnecessary computations and typically also reduces the size of the resulting binary by eliminating unnecessary instructions.
  • 24. Optimization Techniques – Constant Folding It is an optimization technique that eliminates expressions that calculate a value that can already be determined when the program is compiled. These are typically calculations that only reference constant values or expressions that reference variables whose values are constant. For example: n = 10 * 20 * 400; i = 10; j = 20; ij = i * j; In the latter case, the assignments to the variables i and j could also be eliminated if these variables were not used elsewhere in the code.
  • 25. Optimization Techniques – Copy Propagation Transformations It eliminate cases in which values are copied from one location or variable to another in order to simply assign their value to another variable. Given an assignment of the form f = g, subsequent uses of f, called copies, use g instead. Consider a code fragment such as the following: i = 10; x[j] = i; y[MIN] = b; b = i; //After CPT it looks like “b=10”
  • 26. Optimization Techniques – Dead Code Elimination Dead code elimination (DCE) is the term used for optimizations that remove code that doesn’t actually do anything permanent or useful. Unreachable code elimination is a related optimization that removes code that the compiler can identify as impossible to reach. For example: if ( i == 10 ) { . . . } else { . . . if ( i == 10) { . . . } } The nested test for whether i is equal to 10 can be eliminated because it can never be reached.
  • 27. Optimization Techniques – If- Conversion If-conversion is a technique where branch constructs, such as large if-then-else if-else constructs, are broken into separate if statements to simplify generated code, provide opportunities for further optimization, and eliminate jumps and branches wherever possible.
  • 28. Optimization Techniques – Inlining It is an optimization technique where code performance can improve by replacing complex constructs and even function calls with inline representations of the construct or function call. Code inlining, or loop unrolling, are terms for replacing all or portions of a loop with an explicit series of instructions. Function inlining is the term for replacing function calls with the explicit set of instructions that are performed by the function. In general, inlining can reduce code complexity and provide higher performance than would be required by branching that would be done otherwise. It often also provides opportunities for common sub-expression elimination and code motion optimization.
  • 29. GCC Optimization Basics GCC uses a sequence of different intermediate representations of code before generating binaries. Converting language-specific code into simpler forms provides several primary advantages: - Breaking code into simpler, more low-level constructs exposes opportunities for optimization that may not be directly visible in source code. - Using intermediate representations enables the simpler form to more easily and readably represent parse trees that are otherwise at varying levels of complexity. - Using a simpler form enables the GCC compilers to share optimization mechanisms, regardless of the language in which your code was originally written.
  • 30. GCC Optimization Basics (2) Traditionally, GCC has always internally used an intermediate form known as Register Transfer Language (RTL), which is a very low-level language into which all code compiled with a GCC compiler (in any high-level language) is transformed before object code generation begins. Transforming high-level input languages into an intermediate, internal form is a time-tested mechanism for exposing opportunities for optimization. As a very low-level representation of code, GCC’s RTL lends itself well to optimizations that are similarly low-level, such as register allocation and stack and data optimizations. Because it is relatively low-level, RTL is not as good as one would like in terms of supporting higher-level optimizations related to data types, array and variable references, and overall data and control flow within code.
  • 31. GCC Optimization Basics (3) With GCC 4.0, the fine makers of the GCC compilers introduced a new intermediate form, static single assignment (SSA), which operates on the parse trees that are produced from code by GCC compilers, and is thus known as Tree SSA. It is interesting to note that 4.0 and later GCC compilers use two intermediate forms before arriving at a Tree SSA representation, known as GENERIC and GIMPLE. A GENERIC representation is produced by eliminating language-specific constructs from the parse tree that is generated from code, and a GIMPLE representation is then produced from the GENERIC representation by simplifying address references within the code. It provides several additional points at which optimizations can occur at as high a level as possible before zooming into the depths of the RTL.
  • 32. Architecture-Independent Optimizations GCC’s most well-known optimization switches are –O; its variant -On, where n is an integer between 0 and 3; and -Os. -OO turns off optimization. -O and -O1 (which are level 1) are equivalent, telling GCC to optimize code. With -O or -O1, the compiler attempts to minimize both code size and execution time without dramatically increasing compilation time. Using -O2 and -O3 will increase the level from that requested by - O1, while still invoking the optimization options requested by -O1. To minimize code size, use option -Os. To disable one of them while leaving the others enabled, negate the option using no- between -f and the optimization name. For example, to disable deferred stack pops, the command line might resemble this: $ gcc myprog.c -o myprog -O1 -fno-defer-pop
  • 36. Architecture-Independent Level 2 GCC Optimizations -O2 optimization (level 2 optimization) includes all level 1 optimizations plus the additional optimizations listed below. Applying these optimizations will lengthen the compile time, but as a result a measurable increase should be seen in the resulting code’s performance, or, rather, a measurable decrease in execution time.
  • 40. Architecture-Independent GCC Optimizations for Code Size The -Os option is becoming increasingly popular because it applies all of the level 2 optimizations except those known to increase the code size. -Os also applies additional techniques to attempt to reduce the code size. Code size, in this context, refers to a program’s memory footprint at runtime rather than its on-disk storage requirement. In particular, -Os disables the following optimization flags (meaning they will be ignored if specified in conjunction with -Os): • -falign-functions • -falign-jumps • -falign-labels • -falign-loops
  • 41. Architecture-Independent GCC Optimizations for Code Size (2) • -fprefetch-loop-arrays • -freorder-blocks • -freorder-blocks-and-partition • -ftree-ch You might find it instructive to compile a program with -O2 and -Os and compare runtime performance and memory footprint. For example, I have found that recent versions of the Linux kernel have nearly the same runtime performance when compiled with -O2 and - Os, but the runtime memory footprint is 15 percent smaller when the kernel is compiled with -Os. Naturally, your mileage may vary and, as always, if it breaks, you get to keep all of the pieces.
  • 42. Architecture-Independent Level 3 GCC Optimizations Specifying the –O3 optimization option enables all level 1 and level 2 optimizations plus the following: • -fgcse-after-reload: Performs an extra load elimination pass after reloading • -finline-functions: Integrates all “simple” functions into their callers • -funswitch-loops: Moves branches with loop invariant conditions out of loops Note: If you specify multiple -O options, the last one encountered takes precedence. Thus, given the command gcc -O3 foo.c bar.c -O0 -o baz, no optimization will be performed because -O0 overrides the earlier -O3.
  • 43. Architecture-Independent Manuel GCC Optimization Flags In addition to the optimizations enabled using one of the -O options, GCC has a number of specialized optimizations that can only be enabled by specifically requesting them using -f.
  • 44. Architecture-Independent Manuel GCC Optimization Flags (2) Note: Not all of GCC’s optimizations can be controlled using a flag. GCC performs some optimizations automatically and we cannot disable these optimizations when you request optimization using -O.
  • 45. Processor-Specific Optimizations In reality, though, the target-specific switches are not properly optimizations in the traditional sense. Rather, they are options that give GCC more information about the type of system on which the code being compiled will run. GCC can use this additional information to generate code that takes advantage of specific features or that works around known misfeatures of the given processor.