Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, Let's say we have the following 2 examples.... how would a makefile using Make or GCC react...? 1) Would the compiler be intelligent enough to ignore this statement at ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    1

    Question Compiler intelligence question

    Hi,

    Let's say we have the following 2 examples.... how would a makefile using Make or GCC react...?

    1) Would the compiler be intelligent enough to ignore this statement at compile time?
    if (a==1) {}


    2) Would the compiler dynamically recalculate every time the "i-1" below or would it be stored in a register and always reuse the precalculated value (basically calculating i-1 only once)?
    void fonction(i)
    {
    ...
    table1[i-1] = 0;
    table2[i-1] = 0;
    table3[i-1] = 0;
    ...
    }

    PS... the above code is dumb.. i know... just willing to assess the basic compiler intelligence...

    thanks,

    simon

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Hello,

    I am reasonable confident the GCC has been smart enough to optimize this for years.

    Don't take my word for it, though. Compile it to assembler code and see for yourself.
    ( -S option)
    Debian GNU/Linux -- You know you want it.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Optimization in the C compiler is a huge, huge topic.

    For one thing, you don't always want the compiler to be "smart", especially if you're using a debugger. No, you want to be able to control what the compiler does and does not optimize.

    Here's a summary from the man page of the gcc command-line options which specify what the compiler does and does not optimize:
    Code:
    -falign-functions[=n]
    -falign-jumps[=n]
    -falign-labels[=n]
    -falign-loops[=n]
    -fmudflap
    -fmudflapth
    -fmudflapir
    -fbranch-probabilities
    -fprofile-values
    -fvpt
    -fbranch-target-load-optimize
    -fbranch-target-load-optimize2
    -fbtr-bb-exclusive
    -fcaller-saves
    -fcprop-registers
    -fcse-follow-jumps
    -fcse-skip-blocks
    -fcx-limited-range
    -fdata-sections
    -fdelayed-branch
    -fdelete-null-pointer-checks
    -fearly-inlining
    -fexpensive-optimizations
    -ffast-math
    -ffloat-store
    -fforce-addr
    -ffunction-sections
    -fgcse
    -fgcse-lm
    -fgcse-sm
    -fgcse-las
    -fgcse-after-reload
    -fcrossjumping
    -fif-conversion
    -fif-conversion2
    -finline-functions
    -finline-functions-called-once
    -finline-limit=n
    -fkeep-inline-functions
    -fkeep-static-consts
    -fmerge-constants
    -fmerge-all-constants
    -fmodulo-sched
    -fno-branch-count-reg
    -fno-default-inline
    -fno-defer-pop
    -fmove-loop-invariants
    -fno-function-cse
    -fno-guess-branch-probability
    -fno-inline
    -fno-math-errno
    -fno-peephole
    -fno-peephole2
    -funsafe-math-optimizations
    -funsafe-loop-optimizations
    -ffinite-math-only
    -fno-toplevel-reorder
    -fno-trapping-math
    -fno-zero-initialized-in-bss
    -fomit-frame-pointer
    -foptimize-register-move
    -foptimize-sibling-calls
    -fprefetch-loop-arrays
    -fprofile-generate
    -fprofile-use
    -fregmove
    -frename-registers
    -freorder-blocks
    -freorder-blocks-and-partition
    -freorder-functions
    -frerun-cse-after-loop
    -frounding-math
    -frtl-abstract-sequences
    -fschedule-insns
    -fschedule-insns2
    -fno-sched-interblock
    -fno-sched-spec
    -fsched-spec-load
    -fsched-spec-load-dangerous
    -fsched-stalled-insns[=n]
    -fsched-stalled-insns-dep[=n]
    -fsched2-use-superblocks
    -fsched2-use-traces
    -fsee
    -freschedule-modulo-scheduled-loops
    -fsection-anchors
    -fsignaling-nans
    -fsingle-precision-constant
    -fstack-protector
    -fstack-protector-all
    -fstrict-aliasing
    -fstrict-overflow
    -ftracer
    -fthread-jumps
    -funroll-all-loops
    -funroll-loops
    -fpeel-loops
    -fsplit-ivs-in-unroller
    -funswitch-loops
    -fvariable-expansion-in-unroller
    -ftree-pre
    -ftree-ccp
    -ftree-dce
    -ftree-loop-optimize
    -ftree-loop-linear
    -ftree-loop-im
    -ftree-loop-ivcanon
    -fivopts
    -ftree-dominator-opts
    -ftree-dse
    -ftree-copyrename
    -ftree-sink
    -ftree-ch
    -ftree-sra
    -ftree-ter
    -ftree-lrs
    -ftree-fre
    -ftree-vectorize
    -ftree-vect-loop-version
    -ftree-salias
    -fipa-pta
    -fweb
    -ftree-copy-prop
    -ftree-store-ccp
    -ftree-store-copy-prop
    -ftree-vrp
    -funit-at-a-time
    -fwhole-program
    --param name=value
    -O
    -O0
    -O1
    -O2
    -O3
    -Os
    So look at the man page, take GNU-Fan's advice about the -S option, and knock yourself out. :)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I agree that the best way to handle this is to generate Assembly and check for yourself.

    Having said that, assuming that we have the appropriate optimization level working, I am fairly certain that 1) will be optimized away.

    2) is a different story. When I took my Compilers course, we actually discussed this exact problem. Obviously we don't want to evaluate this statement every time, but what if we don't have enough registers available to only evaluate it once? Granted, when it's a few lines in a row, you're probably okay, but if it's spread out, the loss of a register could hurt a lot more than doing some simple arithmetic.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...