注意! 優化選項 -O 等同於 -O1。
GCC 簡介請見 [[http://guerby.org/ftp/gcc-toulibre-20091216.pdf|GCC Toulibre 20091216]]。
* [[https://docs.google.com/document/pub?id=1ZfyfkB62EFaR4_g4JKm4--guz3vxm9pciOBziMHTnK4#h.j7ef5ylpbb4u|GCC Architectural Goals - Towards a more hackable compiler]]
* [[http://www.mail-archive.com/gcc@gcc.gnu.org/msg61258.html|RFC - GCC Architectural Goals]]
====== 安裝 GCC ======
請見 [[http://gcc.gnu.org/install/|Installing GCC]]。安裝 GCC 之前可能需要安裝以下函式庫 (詳細請見 [[http://gcc.gnu.org/install/prerequisites.html|Prerequisites for GCC]]):
* [[http://gmplib.org/|The GNU Multiple Precision Arithmetic Library]]
* [[http://www.mpfr.org/|The GNU MPFR Library]]
* [[http://www.multiprecision.org/|multiprecision.org]]
$ wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.6.0/gcc-4.6.0.tar.gz; tar xvf gcc-4.6.0.tar.gz;
$ cd gcc-4.6.0
$ wget ftp://ftp.gmplib.org/pub/gmp-5.0.2/gmp-5.0.2.tar.bz2; tar xvf gmp-5.0.2.tar.bz2
$ mv gmp-5.0.2 gmp
$ wget http://www.mpfr.org/mpfr-current/mpfr-3.0.1.tar.gz; tar xvf mpfr-3.0.1.tar.gz
$ mv mpfr-3.0.1 mpfr
$ wget http://www.multiprecision.org/mpc/download/mpc-0.9.tar.gz; tar xvf mpc-0.9.tar.gz
$ mv mpc-0.9 mpc
# 使用 gcc -v 觀察系統 gcc 編譯時的參數
$ mkdir install build; cd build
$ ../gcc-4.6.0/configure --prefix=$INSTALL \
--enable-languages=c,c++
====== 使用 GCC ======
請見 [[http://www.network-theory.co.uk/docs/gccintro/index.html|An Introduction to GCC]]、[[http://sensperiodit.files.wordpress.com/2011/04/hagen-the-definitive-guide-to-gcc-2e-apress-2006.pdf|The Definitive Guide to GCC]]和 [[http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc.pdf|Using the GNU Compiler Collection]]。
gcc 本身是一個驅動器 (driver),它會依序呼叫其它元件例如: cpp (C 前處理器)、cc1 (C 編譯器) 進行編譯。[[https://gcc.gnu.org/onlinedocs/gccint/Collect2.html|collect2]] 蒐集資訊交給 ld。
$ /usr/libexec/cc1 --help
指定共享函式庫的路徑。假設共享函式庫為 /path/to/lib/libFoo.so。
# -Wl 把後面參數傳遞給鏈結器
$ gcc -Wl,-rpath /path/to/lib -L /path/to/lib -lFoo example.c
# 查詢優化選項。
$ gcc -Q --help=optimizers -O1
# 查詢平台特定的選項。
$ gcc -Q --target-help
# 查詢 GCC 是否有支援特定 -Wno 選項。
$ echo xx | gcc -Wno-initializer-overrides -x c - -c 2>&1 | grep 'unrecognized command line option'
* [[https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html|3.10 Options That Control Optimization]]
===== 交叉编译 =====
交叉編譯的介紹請見 [[http://www.cse.iitb.ac.in/grc/gcc-workshop-10/sources/slides/gccw10-config-build.pdf|GCC Configuration and Building]] 和 [[http://www.cse.iitb.ac.in/grc/gcc-workshop-10/sources/slides/gccw10-cross-build.pdf|GCC for Cross Compilation]]。[[http://crosstool-ng.org/|crosstool-NG]]和 [[http://en.gentoo-wiki.com/wiki/Crossdev|crossdev]] 可用來協助建立交叉工具鍊。後者需要 root 權限。有問題可寄信至 [[http://sourceware.org/ml/crossgcc/|crossgcc]] 郵件列表。
- 安裝 gperf。
$ wget http://ftp.gnu.org/pub/gnu/gperf/gperf-3.0.4.tar.gz; tar xvf gperf-3.0.4.tar.gz
$ cd gperf-3.0.4
$ ./configure --prefix=$INSTALL
$ make; make install
$ export PATH=$INSTALL:$PATH
- 下載並編譯 crosstool-ng。
$ $ wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.16.0.tar.bz2; tar xvf crosstool-ng-1.16.0.tar.bz2
$ cd crosstool-ng-1.11.4
$ ./configure --prefix=$INSTALL
$ make install
- 建置交叉工具鍊。
$ mkdir toolchain-build; cd toolchain-build
$ cp $INSTALL/lib/ct-ng-1.11.4/samples/arm-unknown-linux-gnueabi/* .
$ mv crosstool.config .config
# 於 Paths and misc options 中調整最大可工作數量,加快工具鏈的生成。
$ ct-ng menuconfig
$ ct-ng build
$ cd $HOME/x-tools/
* [[http://www.linaro.org/|Linaro]] - crosstool-ng-1.11 已加入此版本的 GCC。請見 [[ https://wiki.linaro.org/WorkingGroups/ToolChain/Using/CrosstoolNg|]]。
交叉編譯有底下術語:
* host: 運行交叉工具鏈的平台。
* build: 編譯交叉工具鏈的平台。
* target: 運行交叉工具鏈得到的執行檔,其運行的平台。
* [[http://www.ailis.de/~k/archives/19-ARM-cross-compiling-howto.html|ARM cross-compiling howto]]
* [[http://www.airs.com/blog/archives/492|Cross-compilation]]
* [[http://www.mentor.com/|Code Sourcery]]
* [[http://www.mentor.com/embedded-software/sourcery-tools/sourcery-codebench/editions/lite-edition/|Sourcery CodeBench Lite Edition]]
* [[http://stackoverflow.com/questions/1474673/building-gcc-cross-compiler-from-linux-to-windows|Building GCC cross compiler (from “Linux” to “Windows”)]]
* [[http://www.blogcompiler.com/2010/07/11/compile-for-windows-on-linux/|Compile for Windows on Linux]]
===== 反馈制导优化 =====
請見[[http://www.linezing.com/blog/?p=234|使用gcov完成代码覆盖率的测试]] 和 [[http://gcc.gnu.org/onlinedocs/gcc/Gcov.html|gcov - a Test Coverage Program]]。
# 產生 instrumented 代碼
$ gcc -fprofile-generate hello.c
# 產生 profiling data
$./a.out
# 使用 profiling data 重新編譯
$ gcc -fprofile-use hello.c
''.gcda'' 後綴的檔案其格式定義在 ''gcc/gcov-io.h'' 裡面 ([[http://gcc.gnu.org/onlinedocs/gcc/Gcov-Data-Files.html|10.4 Brief description of gcov data files]])。
* [[http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/zh-TW//pubs/archive/36576.pdf|Feedback-Directed Optimizations in GCC with Estimated Edge Profiles from Hardware Event Sampling]]
===== Link Time Optimization =====
Link Time Optimization (LTO) 的簡介請見 [[http://gcc.gnu.org/wiki/LinkTimeOptimization|Link Time Optimization]],細節請見 [[http://gcc.gnu.org/onlinedocs/gccint/LTO.html#LTO|24 Link Time Optimization]]。[[http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc.pdf|Using the GNU Compiler Collection]] 也有詳細解釋。
$ ../gcc-4.6.0/configure --prefix=$INSTALL --enable-languages=c,c++ --enable-lto; make; make install
$ gcc -o myprog -flto -O2 foo.c bar.c
# -fuse-linker-plugin 需要 GNU ld 2.21 以後或是 gold
$ gcc -o myprog -O2 -flto -fuse-linker-plugin a.o b.o -lfoo
- 編譯 gold。
$ wget http://ftp.gnu.org/gnu/binutils/binutils-2.21.tar.gz; tar xvf binutils-2.21.tar.gz
$ mkdir build install; cd build
$ ../binutils-2.21/configure --prefix=$INSTALL --enable-gold=yes
* [[http://sysrun.haifa.il.ibm.com/hrl/greps2007/papers/greps-07-lto.pdf|Rationale for Link Time Optimization in GCC]]
* [[http://www.airs.com/blog/archives/100|Link Time Optimization]]
===== Extension =====
* [[http://oreilly.com/linux/excerpts/9780596009588/gcc-extensions-to-the-c-language.html|GCC Extensions to the C Language: Appendix - Linux System Programming]]
====== Q & A ======
* [[http://gcc.gnu.org/wiki/FAQ|GCC Wiki FAQ]]
* [[http://old.nabble.com/question-on-inconsistent-generated-codes-for-builtin-calls-td33132678.html|question on inconsistent generated codes for builtin calls]][(http://people.cs.nctu.edu.tw/~chenwj/log/GCC/amker-2012-01-17.txt)][(http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51867)]
* [[http://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc/Thread_002dLocal.html|5.54 Thread-Local Storage]]
* [[wp>Thread-local storage]]
* [[http://www.codemud.net/~thinker/GinGin_CGI.py/show_id_doc/456|FreeBSD 的 Thread-Local Storage 實作]]
* [[http://www.longene.org/techdoc/0328131001224576926.html|漫谈兼容内核之二十: 关于TLS]]
* [[wp>Win32 Thread Information Block]]
* [[http://stackoverflow.com/questions/2281739/automatically-adding-enter-exit-function-logs-to-a-project/2291426#2291426|Automatically adding Enter/Exit Function Logs to a Project]]
* [[http://stackoverflow.com/questions/3564752/what-is-cfi-and-lfe-in-assembly-code-produced-by-gcc-from-c-program|What is .cfi and .LFE in assembly code produced by GCC from c++ program?]]
* [[wp>Libiberty]] 是 GNU 工具鏈的通用庫。
* 匯編檔的後綴會影響匯編器的處理。.S 會先經過前處理器,再經過匯編器; .s 則直接由匯編器處理。
* [[https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html|3.2 Options Controlling the Kind of Output]]
====== Internal ======
# svn checkout svn://gcc.gnu.org/svn/gcc/trunk gcc
$ git clone git://gcc.gnu.org/git/gcc.git
$ make STAGE1_CFLAGS="-g3 -O0" all-stage1
* [[http://gcc.gnu.org/contribute.html|Contributing to GCC]]
* [[http://simplemind.info/gcc%e5%89%8d%e7%ab%af%e5%88%b0%e4%b8%ad%e7%ab%af%e4%b8%80%ef%bc%9agcc-4-6-1-%e7%bc%96%e8%af%91%e4%b8%8e%e5%ae%89%e8%a3%85/|gcc前端到中端(1):gcc 4.6.1 编译与安装]]
===== 概觀 =====
[[http://www.cse.iitb.ac.in/grc/gcc-workshop-11/downloads/slides/gccw11-compilation-and-gcc-overview.pdf|An overview of compilation and GCC]] 中會見到 m/c Ind,這代表與機器無關 (machine code independent)。
* ''gcc'': 主要目錄。
* ''gcc/config'': 各平台相關檔案。
* ''gcc/LANG'' 和 ''gcc/libLANG'': 前端相關檔案。
GENERIC -> GIMPLE -> Tree SSA -> RTL
* [[http://www.cse.iitb.ac.in/~uday/gcc-mini-workshop/gcc-internals-1.pdf|GCC Internals: A Conceptual View – Part I]]
* [[http://www.cse.iitb.ac.in/~uday/gcc-mini-workshop/gcc-internals-2.pdf|GCC Internals: A Conceptual View – Part II]]
===== 灰盒探測 =====
[[http://www.cse.iitb.ac.in/grc/gcc-workshop-11/downloads/slides/gccw11-gbprobe-1.pdf|First level gray box probing of GCC]]。所謂 gray box probing 是指對系統有基本的認識,透過觀察結合我們對該系統的基本認識進而推論其運作流程 [(http://research.cs.wisc.edu/graybox/gb-sosp.pdf)]。
# 列出所有優化。
$ gcc -c --help=optimizers
# 列出 -O2 開啟/關閉的優化。
$ gcc -c -O2 --help=optimizers -Q
# ir 可以是 tree、ipa 或是 rtl。pass 若為 all,則會輸出該 ir 的所有傾印檔。
$ gcc -fdump--
$ gcc -fdump-tree-all test.c
# 觀察 test.c.004t.gimple。
==== 變數存取 ====
* 全域變數視為內存,本地變數視為暫存。運算時,一律先將內存搬至暫存,再做運算。
int a;
int main()
{
int x = 10;
int y = 5;
x = a + x * y;
y = y - a * x;
}
main ()
{
int D.1587;
int a.0;
int a.1;
int D.1590;
int x;
int y;
x = 10;
y = 5;
D.1587 = x * y;
a.0 = a; // 先將內存搬至暫存。
x = D.1587 + a.0;
a.1 = a;
D.1590 = a.1 * x;
y = y - D.1590;
}
* 一維陣列。
int main()
{
int a[3], x;
a[1] = a[2] = 10;
x = a[1] + a[2];
a[0] = a[1] + a[1] * x;
}
main ()
{
int D.1586;
int D.1587;
int D.1588;
int D.1589;
int D.1590;
int D.1591;
int a[3];
int x;
a[2] = 10;
D.1586 = a[2];
a[1] = D.1586;
D.1587 = a[1];
D.1588 = a[2];
x = D.1587 + D.1588;
D.1589 = x + 1;
D.1590 = a[1];
D.1591 = D.1589 * D.1590; // a[1] + a[1] * x == a[1] * (1 + x)
a[0] = D.1591;
}
* 指針。
int main()
{
int **a,*b,c;
b = &c;
a = &b;
**a = 10; /* c = 10 */
}
main ()
{
int * D.1587;
int * * a;
int * b;
int c;
b = &c;
a = &b;
D.1587 = *a;
*D.1587 = 10;
}
* ''struct''
typedef struct address {
char *name;
} ad;
typedef struct student {
int roll;
ad *ct;
} st;
int main()
{
st *s;
s = malloc(sizeof(st));
s->roll = 1;
s->ct = malloc(sizeof(ad));
s->ct->name = "Mumbai";
}
main ()
{
void * D.1593;
struct ad * D.1594;
struct st * s;
extern void * malloc (long unsigned int);
s = malloc (16);
s->roll = 1;
D.1593 = malloc (8); // s->ct = malloc(sizeof(ad));
s->ct = D.1593;
D.1594 = s->ct; // s->ct->name = "Mumbai";
D.1594->name = "Mumbai";
}
==== 控制流 ====
比較 ''test.c.004t.gimple'' 和 ''test.c.013t.cfg''。
* ''while'' 和 ''if'' 述句。
int main()
{
int a = 2, b = 3, c = 4;
while (a <= 7) {
a = a+1;
}
if (a <= 12)
a = a + b + c;
}
main ()
{
int D.1592;
int a;
int b;
int c;
a = 2;
b = 3;
c = 4;
goto ;
:
a = a + 1;
:
if (a <= 7) goto ; else goto ;
:
if (a <= 12) goto ; else goto ;
:
D.1592 = a + b;
a = D.1592 + c;
:
}
main ()
{
int c;
int b;
int a;
int D.1592;
:
a = 2;
b = 3;
c = 4;
goto ;
:
a = a + 1;
:
if (a <= 7)
goto ;
else
goto ;
:
if (a <= 12)
goto ;
else
goto ;
:
D.1592 = a + b;
a = D.1592 + c;
:
return;
}
==== 函式呼叫 ====
$ gcc -fdump-ipa-cgraph -S test.c
$ less test.c.000i.cgraph
extern int divide(int, int);
int multiply(int a, int b) {
return a*b;
}
int main() {
int x,y;
x = divide(20, 5);
y = multiply(x, 2);
printf("%d\n", y);
}
printf/3(-1) @0x801e0c9a0 // 3 代表此為進入點之底幾個函式。-1 代表此為外部函式,尚待解析。
called by: main/1 (1.00 per call)
calls:
References:
Refering this function:
divide/2(-1) @0x801e0c840
called by: main/1 (1.00 per call)
calls:
References:
Refering this function:
main/1(1) @0x801e0c6e0 (asm: main) analyzed needed reachable body finalized
called by:
calls: printf/3 (1.00 per call) multiply/0 (1.00 per call) divide/2 (1.00 per call)
References:
Refering this function:
multiply/0(0) @0x801e0c580 (asm: multiply) analyzed needed reachable body finalized
called by: main/1 (1.00 per call)
calls:
References:
Refering this function:
variable pool:
==== RTL ====
$ gcc -fdump-rtl-all test.c
$ less test.c.144r.expand
- 存取本地變數。
int main() {
int a = 0;
a = a + 1;
}
// int a = 0;
(insn 5 4 6 3 (set (mem/c/i:SI (plus:DI (reg/f:DI 54 virtual-stack-vars)
(const_int -4 [0xfffffffffffffffc])) [0 a+0 S4 A32])
(const_int 0 [0])) test.c:2 -1
(nil))
// a = a + 1;
//
// 6: 當前指令。
// 5: 前一條指令。
// 12: 後一條指令。
// 3: 所處的 basic block。
//
// parallel 表示之後的運算是同時進行。
// set 即是賦值運算。
(insn 6 5 12 3 (parallel [
(set (mem/c/i:SI (plus:DI (reg/f:DI 54 virtual-stack-vars)
(const_int -4 [0xfffffffffffffffc])) [0 a+0 S4 A32])
(plus:SI (mem/c/i:SI (plus:DI (reg/f:DI 54 virtual-stack-vars)
(const_int -4 [0xfffffffffffffffc])) [0 a+0 S4 A32])
(const_int 1 [0x1])))
(clobber (reg:CC 17 flags))
]) test.c:3 -1 // 檔案名稱:行數
(nil))
* ''(clobber (reg:CC 17 flags))'' 代表 ''a = a + 1'' 有可能會改到 (clobber) condition code。
- 存取全域變數。
int a = 0;
int main() {
a = a + 1;
}
// load a into reg59
(insn 5 4 6 3 (set (reg:SI 59 [ a.0 ])
(mem/c/i:SI (symbol_ref:DI ("a") [flags 0x2] ) [0 a+0 S4 A32])) test.c:4 -1
(nil))
// set reg60 = reg59 + 1
(insn 6 5 7 3 (parallel [
(set (reg:SI 60 [ a.1 ])
(plus:SI (reg:SI 59 [ a.0 ])
(const_int 1 [0x1])))
(clobber (reg:CC 17 flags))
]) test.c:4 -1
(nil))
// store reg60 into a
(insn 7 6 13 3 (set (mem/c/i:SI (symbol_ref:DI ("a") [flags 0x2] ) [0 a+0 S4 A32])
(reg:SI 60 [ a.1 ])) test.c:4 -1
(nil))
- 存取實參。
void foo(int a) {
a = a + 1;
}
(insn 6 5 7 3 (parallel [
(set (mem/c/i:SI (plus:DI (reg/f:DI 54 virtual-stack-vars)
(const_int -4 [0xfffffffffffffffc])) [0 a+0 S4 A32])
(plus:SI (mem/c/i:SI (plus:DI (reg/f:DI 54 virtual-stack-vars)
(const_int -4 [0xfffffffffffffffc])) [0 a+0 S4 A32])
(const_int 1 [0x1])))
(clobber (reg:CC 17 flags))
]) test.c:2 -1
(nil))
===== GCC 內部運作流程 ======
GCC 內部運作流程請見 [[http://www.cse.iitb.ac.in/grc/gcc-workshop-10/sources/slides/gccw10-code-view.pdf|GCC Control Flow and Plugins]]。GCC 解析命令行參數請見 ''gcc.c'' 和 ''toplev.c''。
- ''main'' (''main.c'') 調用 ''toplev_main''。
int
main (int argc, char **argv)
{
return toplev_main (argc, argv);
}
- ''toplev_main'' (''gcc/toplev.c'')。
int
toplev_main (int argc, char **argv)
{
... 略 ...
// 進行編譯。
if (!exit_after_options)
do_compile ();
... 略 ...
}
* ''do_compile'' (''gcc/toplev.c'') 。
static void
do_compile (void)
{
... 略 ...
/* Language-dependent initialization. Returns true on success. */
if (lang_dependent_init (main_input_filename))
{
... 略 ...
// 編譯檔案。
compile_file ();
}
... 略 ...
}
* ''compile_file'' (''gcc/toplev.c'') 編譯檔案 (translation unit)。
static void
compile_file (void)
{
... 略 ...
// 不同語言會定義不同的 lang_hooks,以便在此調用對應的 parser。
lang_hooks.parse_file ();
... 略 ...
}
- 以 C 語言為例,''c-objc-common.h'' 定義其 parser 為 ''c_common_parse_file'' (''c-family/c-opts.c'')。
#undef LANG_HOOKS_PARSE_FILE
#define LANG_HOOKS_PARSE_FILE c_common_parse_file
void
c_common_parse_file (void)
{
for (;;)
{
c_finish_options ();
pch_init ();
push_file_scope ();
// 調用此函式開始解析。
c_parse_file ();
pop_file_scope ();
... 略 ...
}
}
* ''c_parse_file'' (''c-parser.c'')。
void
c_parse_file (void)
{
c_parser_translation_unit (the_parser);
}
- ''c_write_global_declarations'' (''c-decl.c'')
void
c_write_global_declarations (void)
{
// AST
tree t;
FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
c_write_global_declarations_1 (BLOCK_VARS (ext_block));
finalize_compilation_unit ();
... 略 ...
}
* [[http://www.ibm.com/developerworks/cn/linux/l-gccrtl/#s1|GCC 的编译流程及中间表示层 RTL 的初步探索]]
* [[http://simplemind.info/gcc%E5%89%8D%E7%AB%AF%E5%88%B0%E4%B8%AD%E7%AB%AF3%EF%BC%9Agcc-%E5%89%8D%E7%AB%AF%E7%9A%84%E5%90%84%E8%AF%AD%E8%A8%80%E9%9B%86%E5%90%88/|gcc前端到中端(3):GCC 前端的各语言集合]]
* [[http://wenku.baidu.com/view/50407a0e4a7302768e9939bf.html|基于GCC的C++静态分析器的开发]]
* [[http://www.aka-kernel.org/news/hellogcc/GCC%20Internals%20and%20Porting.pdf|GCC Internals and Porting]]
* [[http://www.airs.com/blog/archives/287|A Gcc Frontend]]
===== 優化 =====
GCC 內部所有的轉換和優化過程皆以 Pass 稱之,請見 [[http://www.cse.iitb.ac.in/grc/gcc-workshop-10/sources/slides/gccw10-gimple-rtl.pdf|Manipulating GIMPLE and RTL IRs]] 和 [[http://gcc.gnu.org/onlinedocs/gccint/Passes.html#Passes|9 Passes and Files of the Compiler]]。主要檔案為 ''gcc/passes.c''。請見 ''gcc/tree-pass.h'' 中的 ''struct opt_pass''。''gcc/basic-block.h'' 定義了處理基本塊的巨集。Pass 基本上分為 GIMPLE 和 RTL pass。
- ''init_optimization_passes'' (''gcc/passes.c'') 排定優化序列。
void
init_optimization_passes (void)
{
struct opt_pass **p;
#define NEXT_PASS(PASS) (p = next_pass_1 (p, &((PASS).pass)))
/* All passes needed to lower the function into shape optimizers can
operate on. These passes are always run first on the function, but
backend might produce already lowered functions that are not processed
by these passes. */
/* Interprocedural optimization passes. */
... 略 ...
}
===== RTL =====
* [[wp>Register transfer language]]
* [[http://www.cse.iitb.ac.in/grc/gcc-workshop-09/downloads/gccw09-rtl.pdf|Introduction to RTL]]
* [[http://www.cse.iitb.ac.in/grc/gcc-workshop-10/sources/slides/gccw10-gimple-rtl.pdf|Manipulating GIMPLE and RTL IRs]]
* [[http://gcc.gnu.org/onlinedocs/gccint/RTL.html#RTL|10 RTL Representation]]
* [[http://gcc.gnu.org/onlinedocs/gccint/Machine-Modes.html|10.6 Machine Modes]]
* [[http://kitoslab.blogspot.tw/2012/06/gcc-rtl-insn.html|GCC RTL INSN 簡介]]
檢視 RTL 語句請見 [[http://www.cse.iitb.ac.in/grc/gcc-workshop-10/sources/slides/gccw10-gbprobe-1.pdf|First Level Gray Box Probing]]。
===== 傾印內部資訊 =====
==== GCC 自帶選項 ====
''-da'' 可以傾印 RTL 级别每个 pass 的 dump 文件。
# -Wa 代表之後的參數餵給匯編器。
$ gcc -da -Wa,--keep-locals hello.c
$ objdump -t a.out
0000000000000000 l df *ABS* 0000000000000000 hello.c
00000000004004e4 l .text 0000000000000000 .LFB0
00000000004004ea l .text 0000000000000000 .LFE0
00000000004004ea l .text 0000000000000000 .LFB1
00000000004004fa l .text 0000000000000000 .LFE1
''--keep-locals'' 大致可代表基本塊的位置[(http://sourceware.org/ml/binutils/2011-06/msg00117.html)]。關於 CFG 的討論請見 [[http://gcc.gnu.org/ml/gcc/2011-06/msg00094.html|Generate annotations for a binary translator]][(http://www.cs.nctu.edu.tw/~chenwj/log/iant-2011-06-15.txt)]。
==== Loop ====
''cfgloop.*'' 是整個過程中會用到。''tree-ssa-loop.*'' 處理 GIMPLE。''loop-.*'' 處理 RTL。
==== 分支預測 ====
''predict.*''
==== OpenMP ====
GCC 有關 OpenMP 的文件請見 [[http://gcc.gnu.org/onlinedocs/libgomp/|GNU libgomp]] 和 [[http://gcc.gnu.org/wiki/openmp|OpenMP]]。源碼請見 ''gcc/omp-low.c'' 和 ''libgomp/''。文件不見得符合現狀,例如: [[http://gcc.gnu.org/ml/gcc-help/2011-01/msg00239.html|OpenMP loop implementation]]。
===== 插件 =====
請見 [[http://gcc.gnu.org/onlinedocs/gccint/Plugins.html#Plugins|23 Plugins]] 和 [[http://gcc.gnu.org/wiki/plugins|plugins]]。
編譯開啟插件功能的 GCC。
$ wget http://mirrors.kernel.org/gnu/gcc/gcc-4.6.0/gcc-4.6.0.tar.gz; tar xvf gcc-4.6.0.tar.gz
$ mkdir install build; cd build
$ ../gcc-4.6.0/configure --prefix=$INSTALL --enable-languages=c,c++ --enable-plugin --enable-lto
$ make && make install
* [[http://www.codesynthesis.com/~boris/blog/2010/05/03/parsing-cxx-with-gcc-plugin-part-1/|Parsing C++ with GCC plugins, Part 1]]
* [[http://codesynthesis.com/~boris/blog/2010/05/10/parsing-cxx-with-gcc-plugin-part-2/|Parsing C++ with GCC plugins, Part 2]]
* [[http://ehren.wordpress.com/2009/11/04/creating-a-gcc-optimization-plugin/|Creating a GCC optimization plugin]]
==== 現有插件 ====
請見 [[http://code.google.com/p/gcc-vcg-plugin/issues/detail?id=1]]。
[[http://code.google.com/p/gcc-vcg-plugin/|gcc-vcg-plugin]] 是一個 gcc 插件,用來在運行 gdb 執行 gcc 時,傾印 gcc 內部資料結構。其它介紹請見 [[http://blog.linux.org.tw/~jserv/archives/2010/05/gcc-vcg-plugin.html|gcc-vcg-plugin : 視覺化 GCC 內部結構]]。
$ wget http://gcc-vcg-plugin.googlecode.com/files/gcc-vcg-plugin-1.1.tar.gz; tar xvf gcc-vcg-plugin-1.1.tar.gz
$ ../gcc-vcg-plugin-1.1/configure --prefix=$INSTALL
$ GCC=/path/to/just_installed_gcc make
$ wget http://vcgviewer.googlecode.com/files/vcgview-2.0.tar.gz; tar xvf vcgview-2.0.tar.gz
$ cd vcgview-2.0; ./autogen.sh
$ cd ../; mkdir install build; cd build
$ ../vcgview-2.0/configure --prefix=/tmp/chenwj/install
* [[http://libplugin.sourceforge.net/|libplugin]]
$ git clone http://git.fedorahosted.org/git/gcc-python-plugin.git
* [[https://fedorahosted.org/gcc-python-plugin/|GCC Python Plugin]]
* [[http://readthedocs.org/docs/gcc-python-plugin/en/latest/index.html|GCC Python plugin]]
===== GTY =====
GTY 是 gcc 內部的內存管理器。
* [[http://gcc.gnu.org/onlinedocs/gccint/Type-Information.html|22 Memory Management and Type Information]]
* [[http://gcc.gnu.org/wiki/Memory_management|Memory_management]]
* [[http://www.airs.com/blog/archives/185|GCC Garbage]]
====== Submitted Patch ======
* [[http://gcc.gnu.org/ml/gcc-patches/2011-06/msg01003.html|[PATCH] Docs: cfg.texi: Correct statement about CFG]]
* [[http://gcc.gnu.org/ml/gcc-patches/2012-08/msg01564.html|[doc] Fix typo in gty.texi]]
* [[http://gcc.gnu.org/ml/gcc-patches/2012-09/msg01435.html|[PATCH] Docs: lto.texi: Fix typo]]
====== 其它 ======
* [[http://cloc.sourceforge.net/|CLOC]]
$ ./cloc --force-lang="C",c --by-file-by-lang --csv folder/
* 挑選檔案,比較 IAR 和 GNU GCC 的 code size。
* [[https://www.iar.com/iar-embedded-workbench/atmel/avr/benchmark-results-for-avr/|IAR Benchmark results]]
* ''-Ohs'' 和 ''-O3'' 比較。
* ''-Ohz'' 和 ''-Os'' 比較。
* IAR: Project -> Options -> Optimizations ([[http://supp.iar.com/FilesPublic/UPDINFO/004916/arm/doc/EWARM_DevelopmentGuide.ENU.pdf#nameddest=G18.1047631|IAR C/C++ Development]])
* Low: 應該是比 None 多加上暫存器優化。
* High: common subexpression elimination, loop unrolling, function inlining, code motion, type-based alias analysis, static clustering, instruction scheduling。
* [[http://supp.iar.com/FilesPublic/UPDINFO/004916/arm/doc/EWARM_DevelopmentGuide.ENU.pdf#nameddest=G18.1047772|FINE-TUNING ENABLED TRANSFORMATIONS]]
* ''-fgcse'' ''-funroll-loops'' ''-finline-functions'' ''-fmove-loop-invariants'' ''-fstrict-aliasing'' ''-fschedule-insns''
* 最後會將 *.out 轉換成 *.bin 檔。
* [[http://www.avrfreaks.net/forum/compiler-efficiency-iar-versus-gnu|Compiler efficiency: IAR versus GNU]]
* [[http://www.avrfreaks.net/forum/code-size-and-memory-use-iar|code size and memory use in IAR]]
* [[https://www.silabs.com/Support%20Documents/TechnicalDocs/AN720.pdf|PRECISION32™ OPTIMIZATION CONSIDERATIONS FOR CODE SIZE AND SPEED]]
* [[http://embeddedgurus.com/stack-overflow/2010/02/is-gcc-a-good-compiler/|Is GCC a 'good' compiler?]]
* 3.18 Options for Code Generation Conventions
* ''--fshort-enums''
* ''--fno-jump-tables''
====== 外部連結 =======
* [[http://gcc.gnu.org/|GCC, the GNU Compiler Collection]]
* [[http://gcc.gnu.org/wiki|GCC Wiki]]
* [[http://gcc.gnu.org/wiki#GCC_Summit_Proceedings|GCC Summit Proceedings]]
* [[http://gcc.gnu.org/onlinedocs/gccint/|GNU Compiler Collection (GCC) Internals]]
* [[http://www.lingcc.com/gccint/|GNU Compiler Collection (GCC) Internals (中文)]]
* [[wb>GNU C Compiler Internals]]
* [[http://research.alexeysmirnov.name/gem/index.php|GEM: GCC Extension Modules]]
* Workshop on GCC Research Opportunities (GROW)
* [[https://www.debian-administration.org/article/672/Optimizing_code_via_compiler_flags|Optimizing code via compiler flags]]
* [[http://ds9a.nl/gcc-simd/index.html|SIMD and other techniques for fast numerical programming with gcc]]
* [[http://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html|6.49 Using vector instructions through built-in functions]]
===== 資源中心 =====
* [[http://www.cse.iitb.ac.in/grc/|GCC Resource Center]]
* [[http://www.hipeac.net/gcc-tutorial|HiPEAC GCC Tutorial]]
* [[http://www.airs.com/dnovillo/200711-GCC-Internals/|GCC Internals Course - November 2007]]
* [[http://ccckmit.wikidot.com/gcc:ref|gcc 編譯器移植]]
===== 文章 =====
* [[http://www.slideshare.net/lusecheng/hcsm-lect20120913|FROM SOURCE TO BINARY]]
* [[http://www.ibm.com/developerworks/cn/linux/l-gcc4/|认识 GCC 4]]
* [[http://www.ibm.com/developerworks/cn/linux/l-gcc/part1/|GNU 编译器家族 GCC 内部探密: 探索 GCC 前端的内部结构]]
* [[http://www.airs.com/blog/archives/518|Executable stack]]
* [[http://blog.lxgcc.net/wp-content/uploads/2011/03/GCC_frontend.pdf|GCC Front-End Internals]]
* [[http://www.linuxjournal.com/article/7884|Writing a GCC Front End]]
* [[http://eli.thegreenplace.net/2012/07/12/computed-goto-for-efficient-dispatch-tables/|Computed goto for efficient dispatch tables]]
* [[http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html|6.3 Labels as Values]]
* [[http://gcc.gnu.org/ml/gcc-patches/2012-07/msg00512.html|[Patch/RFC] SEH exceptions for Win64]]
* [[http://lwn.net/Articles/84888/|GCC gets a new Optimizer Framework]]
* [[http://lwn.net/Articles/517237/|An Introduction to GCC Compiler Intrinsics in Vector Processing (Linux Journal)]]
===== 其它 =====
* [[http://ccache.samba.org/|ccache - a fast C/C++ compiler cache]]
* [[http://code.google.com/p/distcc/|distcc - Distributed compilation for faster C/C++ builds]]
* [[http://gcc.gnu.org/wiki/GCCGathering2011|GCC Gathering 2011]]
* [[http://moxielogic.org/blog/|Moxie]]
* [[Diable]]
* [[http://zh.wikipedia.org/wiki/User:Shiva0725]]