4

Recently, I am compiling the ffmpeg codes under windows use the VS2010 with intel compiler. For the following codes:

void ff_dcadsp_init(DCADSPContext *s)
{
    s->lfe_fir = dca_lfe_fir_c;
    if (ARCH_ARM) ff_dcadsp_init_arm(s);
}

the macro ARCH_ARM is defined as 0.

When I compile it under linux, there is no function in ff_dcadsp_init_arm() in the object file, while it does under windows? So I want to make sure if the compiler will do something about the useless codes and how to set it for INTEL compiler.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
xkfz007
  • 185
  • 1
  • 1
  • 8
  • After searching, I learned some of compiler optimization. This is called Dead codes elimination(http://en.wikipedia.org/wiki/Dead_code_elimination). Most compiler will do this. In my case, the gcc does it under linux with -o option and I compile the codes with VS2010 under DEBUG modes which do not do the dead code elimination. ps: -fdce is the actual option in gcc to do the dead code elimination. – xkfz007 Jun 08 '15 at 09:17
  • @xhfz007 Usually compilers don't perform (almost) any optimization in DEBUG builds (that's the _meaning_ of "debug" configuration, a set of options to make debugging easier,optimizations usually mess-up generated code enough to make debug much much harder). – Adriano Repetti Jun 08 '15 at 11:21
  • Make sure proper optimization options are set for your project. This will possibly recognize and eliminate the dead code. – Mohit Jain Jul 22 '15 at 07:22

2 Answers2

5

Usually, most compilers are capable of taking care of that kind of the dead code, effectively removing that particular instruction / block. However, AFAIK it is not guaranteed and may differ between compilers and / or supplied optimization level settings.

However, if you want this to be handled in the preprocessing state itself (should be a better approach, IMHO, as ARCH_ARM is a MACRO), you can make use of #if preprocessor statements, to remove the dependency on compiler optimization level.

For example, in general

void ff_dcadsp_init(DCADSPContext *s)
{
    //something common code

    #if ARCH_ARM                 //this {#if...#endif} block 
        ff_dcadsp_init_arm(s);   //only get compiled if ARCH_ARM evaluates to non-zero
    #endif

   //something more common code
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • @onemasse well, `#if` as used in currect form, should also work, IMHO. – Sourav Ghosh Jun 08 '15 at 08:25
  • @onemasse: No, this should work. Code between `#if ARCH_ARM` and `#endif` will be compiled only if `ARCH_ARM` is `#define`d to non-zero value. And in case of `#ifdef ARCH_ARM` `#endif`, the code will be executed even if `ARCH_ARM` is `#define`d to `0` – WedaPashi Jun 08 '15 at 08:31
  • Can anybody be kind enough to explain the reason behind the downvote? What did I miss, please? (except the `#ifdef` part, which may be confusing in _this_ scenario, already removed). – Sourav Ghosh Jun 08 '15 at 09:08
2

If ARCH_ARM is a macro, you'd probably be better off with something like:

void ff_dcadsp_init(DCADSPContext *s)
{
    s->lfe_fir = dca_lfe_fir_c;
    #if ARCH_ARM != 0
        ff_dcadsp_init_arm(s);
    #endif
}

Then you don't have to concern yourself with what an optimiser will do. The C standard itself doesn't mandate that "dead" code is removed, this is totally down to how each implementation wants to do it.

The use of standard C functionality like #if is mandated by the standard, and therefore much more portable.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • yeah, #if is a good choice, but that codes come from the ffmpeg source codes, and it goes well under linux. – xkfz007 Jun 08 '15 at 08:10
  • This is from the ffmpeg source code, see https://www.ffmpeg.org/doxygen/1.1/dcadsp_8c_source.html. I don't know why it hasn't been done properly, I traced the commit that added that part back to April 2010, from Måns Rullgård, one of the major FFmpeg contributors. – SirDarius Jun 08 '15 at 08:23
  • 1
    Not using a `#if` will ensure the code compiles whatever the value of the macro is. Any decent compiler will generate no code for the if statement. – ouah Jun 08 '15 at 08:32
  • @ouah, your definition of "any decent compiler" seems to not match the definition that ISO gives to a "C compiler". There is _zero_ requirement for a compiler to remove dead code, there is 100% requirement for it to remove code inside the `#if/#endif` directives. The whole _point_ of the question is predicated on that fact. – paxdiablo Jun 08 '15 at 09:35
  • @paxdiablo I agree with you it's not mandatory for a compiler to perform any _optimization_. That said it's reasonable to assume compiler does them and in this case it's a **micro-optimization** we usually rant to avoid (removing an unnecessary condition, and removing a function body for a function used for **initialization**) that I'd don't even bother if compiler let it there. BTW OP seems surprised because ff_dcadsp_init_arm() is in object file (he is not checking if dead-code is compiled or not) and **preprocessor directives won't change compiler behavior** here. – Adriano Repetti Jun 08 '15 at 11:01