gcc都做了什麼最佳化

來源:互聯網
上載者:User

直接上程式:

setjmp和longjmp是處理函數嵌套調用的,goto語句不能跨越函數,所以不選擇goto。

#include <setjmp.h> setjmp(jmp_buf env);  longjmp(jmp_buf env,  val);

對程式進行不帶最佳化編譯:

[henry@localhost c]$  -g youhua.c -o youhua

對程式進行帶最佳化的編譯:

[henry@localhost c]$  -g -O youhua.c -o youhua_after

 

對比上面結果可以看到,全域、靜態、volatile變數不受最佳化的影響。

  • 不進行最佳化時,上面定義的5個變數包括register變數都直接從記憶體中取值。
  • 進行最佳化後,register變數和局部變數gcc都是從寄存器中取的值。 

gcc都做了什麼最佳化呢?首先可以看到變數從記憶體取值最佳化到從寄存器取值。一下是manual的部分翻譯。

gcc有幾個最佳化等級:

O0,O1,O2,O3

-O0表示沒有最佳化,-O1為預設值,-O3最佳化層級最高

'-O  '-O1                  Optimize.      Optimizing   compilation   takes   somewhat         memory      a   large   -O                 -O                                -fdefer--fdelayed--fguess-branch--fcprop--floop--fif--fif--ftree--ftree--ftree-dominator--ftree--ftree--ftree--ftree--ftree--ftree--ftree--funit-at-a-                                   -fmerge--O -fomit-frame-pointer標誌當機器                    so does not interfere with debugging.                                                  這樣做不會影響幹涉調試。

         `-O t turn on `-ftree-sra
 the Ada compiler.
 `-O2 '  Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff.The compiler does not perform loop unrolling or function inlining when you specify `-O2 '.As compared to `-O ',this option increases both compilation time and the performance of the generated code.  進一步的最佳化。GCC會支援所有不涉及時間空間交換的所有支援的最佳化選項。當你加入-o2選項時,編譯器不會進行迴圈展開和函數內聯。與-O選項相比,這個選項會增加編輯時間和合成碼的效能。   `-O2' turns on all optimization flags specified by `-O'. It also turns on the following optimization flags:    -fthread-jumps        -fcrossjumping        -foptimize-sibling-calls        -fcse-follow-jumps    -fcse-skip-blocks        -fgcse    -fgcse-lm           -fexpensive-optimizations        -fstrength-reduce        -frerun-cse-after-loop    -frerun-loop-opt        -fcaller-saves        -fpeephole2        -fschedule-insns          -fschedule-insns2        -fsched-interblock    -fsched-spec        -fregmove        -fstrict-aliasing        -fdelete-null-pointer-checks        -freorder-blocks    -freorder-functions        -falign-functions    -falign-jumps        -falign-loops    -falign-labels        -ftree-vrp        -ftree-pre     Please note the warning under `-fgcse' about invoking `-O2' on programs that use computed gotos.        `-O3'     Optimize yet more.`-O3 ' turns on all optimizations specified by `-O2' and also turns on the `-finline-functions ',`-funswitch-loops' and `-fgcse-after-reload' options.    再一次的最佳化,-O3選項會添加所有-O2中添加的選項,並且添加`-finline-functions ',`-funswitch-loops' and `-fgcse-after-reload' 這三個選項    `-O0'     Do not optimize.This is the default.          -Os相當於-O2.5。是使用了所有-O2的最佳化選項,但又不縮減代碼尺寸的方法。   詳細的說明如下:Level 2.5 (-Os) The special optimization level (-Os or size) enables all -O2 optimizations that do not increase code size; it puts the emphasis on size over speed. This includes all second-level optimizations, except for the alignment optimizations. The alignment optimizations skip space to align functions, loops, jumps and labels to an address that is a multiple of a power of two, in an architecture-dependent manner. Skipping to these boundaries can increase performance as well as the size of the resulting code and data spaces; therefore, these particular optimizations are disabled. The size optimization level is enabled as:   -Os這個特殊的最佳化等級,能夠實現-O2的全部不增加程式碼片段大小最佳化,他強調程式的大小而不是程式的運行速度,他包含了所有第二等級的最佳化,除了對齊最佳化,這些對齊最佳化在體繫結構的依賴性的程式中,跳過一些線性結構,迴圈,跳轉和標籤的空間,到一個指數為2的多項式和的地址。跳過這些界限可以提高效能,以及由此產生的代碼和資料空間的大小,因此,這些特定的最佳化被禁用。

 

完!

 

參考:·[1] 

          apue

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.