public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Disable optimizations on one function (was: 'pragma optimize' ...)
@ 2015-07-16  5:58 Jeffrey Walton
  2015-07-16  9:00 ` Andrew Haley
  2015-07-16 16:32 ` Martin Sebor
  0 siblings, 2 replies; 6+ messages in thread
From: Jeffrey Walton @ 2015-07-16  5:58 UTC (permalink / raw)
  To: Markus Trippelsdorf; +Cc: gcc-help

>> I have one more question related to the use of '#pragma GCC optimize'.
>> I have a zeroizer that must execute due to Certification and
>> Accreditation (C&A) requirements. When the zeroizer is removed as a
>> dead store, the compiler folks usually say, "but you asked for
>> optimizations...".
>>
>> If we cannot use '#pragma GCC optimize' to turn off the optimizer for
>> a function, then how do we tell the compiler to *not* remove the code?
>
> It is hard to tell without seeing the code. But see noclone, noinline
> entries at:
> http://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
>

As a simplified example, imagine I have a function ClearAndFree:

    void ClearAndFree(void* ptr, size_t n)
    {
        if(!ptr) return;

        memset(ptr, 0x00, n);
        free(ptr);
    }

We've seen these type of functions have the memset optimized away.

Many folks try and cast ptr to volatile, but that's an abuse because
GCC considers volatile something for memory mapped hardware. Volatile
should not be used in an attempt to tame the optimizer.

The trick mentioned with noinlne looks promising. Can I use 'asm
("");' to stop the optimizer from removing the call to memset? If so,
does the trick work with GCC 4.2.1 and above?

(GCC 4.2.1 and above is required because OpenBSD still provides it).

Jeff

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-07-16 16:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-16  5:58 Disable optimizations on one function (was: 'pragma optimize' ...) Jeffrey Walton
2015-07-16  9:00 ` Andrew Haley
2015-07-16  9:40   ` Jeffrey Walton
2015-07-16  9:59     ` Andrew Haley
2015-07-16 10:05       ` Jeffrey Walton
2015-07-16 16:32 ` Martin Sebor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).