Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the specific GCC flag that turns on immediate value propagation for inline assembly parameters?

Consider the following x86 code example:

#include <stdlib.h>

static int i;

static inline __attribute__((always_inline)) test(int x)
{
    asm volatile("mov %1, %0" : "=r"(i): "i"(x));
}

int main(void)
{
    test(5);

    return i;
}

If I build it with:

gcc -O test.c

It builds fine.

If I build it with (no optimization):

gcc test.c

It fails during assembly phase, because the value '5' is not propagated as an immediate value to the inline function test, so we fail the constraint.

I wish to be able to compile this code without turning on other non related optimization, in order to make debugging easier.

In theory, -O is simply a short cut to enable a bunch of GCC optimization options at once that are documented in the fine GCC manual. Unfortunately, I was not able to find the specific GCC flag that turns this behavior on.

Any ideas?

Clarification: To relieve any doubt, the code snippet is just an example. It does not make much sense by itself except to show what I am trying to do. The actual use case involves an instruction on a custom processor that can only take an immediate as an argument which I am trying to wrap in a C construct. A macro will indeed do the trick, but suffers from all the usual draw backs of a macro, hence I am trying to avoid it.

Update: For those who wondered, a macro wont work either. It seems the inline function doesn't play a part here at all. E.g., this doesn't work either:

void foo (void)
{
  int i = 6;

  asm volatile ("" : : "i" (i));
}

I also fixed the question title to reflect this.

like image 917
gby Avatar asked Dec 27 '22 21:12

gby


1 Answers

Looks like -ftree-ter (Replace temporary expressions in the SSA->normal pass - whatever that is) does the trick:

gcc -ftree-ter test.c   # no errors

Here's how I determined that:

  1. gcc -Q --help=optimizers tells you what optimizations are enabled/disabled by default (some are enabled)
  2. gcc -O -Q --help=optimizers tells you what optimizations are enabled/disabled for -O
  3. redirect the output of those commands to files and diff them.
  4. try the optimizations that are enabled only when -O is specified until one works.
like image 56
Michael Burr Avatar answered Jan 04 '23 23:01

Michael Burr