How to avoid constant propagation into functions?

Segher Boessenkool
Wed Dec 7 12:43:00 GMT 2016

On Wed, Dec 07, 2016 at 01:35:59PM +0100, Georg-Johann Lay wrote:
> >>So the constant 317 made its way into the code of mod_mul (insn 14),
> >>and insn 9 also uses this value.
> >>
> >>From my understanding "noclone" should avoid any such propagations?
> >
> >noclone prevents cloning the function, and it indeed has not been cloned.
> >
> >Because the function is static, GCC knows all callers, and it determines
> >all callers have n=317, so it optimises with that knowledge.
> I understand this.  What I am after is a means to get a generated
> code that matches the source, no matter what context.

Don't make the function static, or preferable compile it completely
separately (in its own translation unit), and make sure there is no LTO
done on it either?

> Imagine you called that function from assembler, then the code
> would crash.

You cannot (validly) call it from assembler code the way it is now, the
function is local to its translation unit.

> And yes, when I am adding some other usage of
> mod_mul or let escape its address, then the propagation is not
> performed.
> For example, this might have impact on writing test for GCC:
> When I am writing a test with noinline + noclone then my
> expectation is that no such propagation happens, because
> otherwise a test might turn trivial...

The usual ways to prevent that are to add some volatile, or an
asm("" : "+g"(some_var));   etc.


More information about the Gcc-help mailing list