GCC 최적화 옵션 -O1 -O2 -O3

sjlee96의 이미지

제가 GCC로 일을 하나 하고 있는데, 최적화 옵션에 대해 잘 이해가 가지 않는 것이 있어 이렇게 질문하게 되었습니다.

GCC로 컴파일할 때, 최적화 옵션을 주지 않거나, -O1을 주면 제대로 컴파일이 되는데,
-O2 나 -O3같이 높은 수준으로 최적화 하려는 경우, 에러가 발생하면서 컴파일이 되지 않습니다.

gdb로 문제가 뭔지 보려고 해도 최적화 옵션을 줘서 컴파일하는 경우 워낙 코드 안에서 이상하게 돌고 돌아서 따라가기가 힘들더군요.
혹시 이유를 알고 계신 분이 있으시면 도와주실 수 없으신지요?

미리 감사드립니다.

vacancy의 이미지


컴파일 자체가 안될 것 같지는 않은데,
만일 그렇다면 gcc의 버그겠지요.

-O2, -O3 에서 동작하지 않는다면
많은 경우는 프로그램을 잘못 작성한 경우입니다.
주로 memory leak이 있다거나 pointer를 잘못 썼다거나
그런 경우가 많은 것 같더군요.
-O0, -O1 에서 '간신히 운이 좋아' 동작하고 있는 경우랄까요. ;;

가끔 gcc의 버그로 인해 그런 경우도 있습니다만,
드문 경우인 것 같네요.

slc의 이미지

최적화 옵션-O1, -O2, -O3는 여러 최적화 옵션의 조합입니다.
아마도

-O1 Optimize. Optimizing compilation takes somewhat more time, and a lot more memory for a large
function.

With -O, the compiler tries to reduce code size and execution time, without performing any opti-
mizations that take a great deal of compilation time.

-O turns on the following optimization flags: -fdefer-pop -fdelayed-branch -fguess-branch-proba-
bility -fcprop-registers -floop-optimize -fif-conversion -fif-conversion2 -ftree-ccp -ftree-dce
-ftree-dominator-opts -ftree-dse -ftree-ter -ftree-lrs -ftree-sra -ftree-copyrename -ftree-fre
-ftree-ch -funit-at-a-time -fmerge-constants

-O also turns on -fomit-frame-pointer on machines where doing so does not interfere with debug-
ging.

-O doesn’t turn on -ftree-sra for the Ada compiler. This option must be explicitly specified on
the command line to be enabled for 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 spec-
ify -O2. As compared to -O, this option increases both compilation time and the performance of
the generated code.

-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 -fre-
run-cse-after-loop -frerun-loop-opt -fcaller-saves -fpeephole2 -fschedule-insns -fsched-
ule-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 -fin-
line-functions, -funswitch-loops and -fgcse-after-reload options.

-O0 Do not optimize. This is the default.

-Os Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size.
It also performs further optimizations designed to reduce code size.

-Os disables the following optimization flags: -falign-functions -falign-jumps -falign-loops
-falign-labels -freorder-blocks -freorder-blocks-and-partition -fprefetch-loop-arrays
-ftree-vect-loop-version

그러니까.. -O2를 사용한다면,, 위의 최적화 옵션 중 하나또는 일부를 빼가면서 컴파일을
해보는 것도 하나의 방법으로 보입니다.
컴파일시간이 길다면 어렵겠지만.. 약간의 삽질로 최적화 옵션을 찾을 수 있지 않을까요.. ^^.

지나가다.. 삽질 방법이 생각나서 글을 올립니다.

------------------------------------------------------------
개인 메일 서버를 만들어 사용합시다..
언제부터 e-메일이 포털의 전유물이 된거지??
home

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.