Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of GCC optimization flags

I would like to define a set of optimization sequences to an input C program in order to study the impact of my applied sequences to code performance.

example:

 gcc -fauto-inc-dec -fbranch-count-reg -fcombine-stack-adjustments ... test.c -o out.o 

Does the order of these options affect the effectiveness of the produced code?

As well, applying an optimization option twice does have an impact?

Is there a better way to test thousands of optimization sequences? Like in -02 (that includes around 20 options), I would like to define my own flags

like image 939
staticx Avatar asked Oct 14 '15 05:10

staticx


1 Answers

  • Does the order of these options affect the effectiveness of the produced code?

    • No, the order of these options passed as command line arguments to the compiler does not affect the effectiveness of the produced code.
  • Is there a better way to test thousands of optimization sequences?

    • Since we don't have a optimization sequence w.r.t the optimization flags being passed to the compiler, we don't have a way to test. But, as you are aware, we have optimization levels which you can experiment with.
  • Higher optimization levels perform more global transformations on the program and apply more expensive analysis algorithms in order to generate faster and more compact code. The price in compilation time, and the resulting improvement in execution time, both depend on the particular application and the hardware environment. You should experiment to find the best level for your application. Please refer to Optimization Levels for GCC

  • I would like to define my own flags

  • Currently, gcc supports many flags which you can refer in Optimize Options. If you would like to define a flag, then the compiler needs to understand that and you might need to modify compiler code for gcc so that it can understand a new flag. Please refer to code on github, opts.c, opts.c deals with optimization flags and levels.

  • As well, applying an optimization option twice does have an impact?

    • No, applying same optimization option twice will not have impact. For example: Executing gcc -fauto-inc-dec -fauto-inc-dec test.c would have the same impact as Executing gcc -fauto-inc-dec test.c.

(Adding from comments regarding additional optimization passes - You can write a gcc optimization plugin to make additional passes. Please refer to this article: An introduction to creating GCC plugins. The article helps to create plugin to do additional optimization pass, transforming code, or analyzing information.)

like image 95
a3.14_Infinity Avatar answered Oct 12 '22 01:10

a3.14_Infinity