Question

What is Isolate erroneous paths optimization in GCC? How it works ?

Isolate erroneous paths optimization added to gcc last year : http://gcc.1065356.n5.nabble.com/RFA-PATCH-Isolate-erroneous-paths-optimization-td980376.html

Was it helpful?

Solution

The documentation for the two flags added should help your understanding:

-fisolate-erroneous-paths-dereference Detect paths which trigger erroneous or undefined behaviour due to dereferencing a NULL pointer. Isolate those paths from the main control flow and turn the statement with erroneous or undefined behaviour into a trap.

Consider this contrived code block:

char
foo (int a)
  {
    char *x = "hello world";
    if (a)      /* (1).  */
      x = NULL; 
    else
      x = x;
    return *x;  /* (2).  */
 }

In the case where a != 0 we take the path labelled (1). This leads to undefined behaviour at (2), where x would be equal to the NULL pointer.

This optimisation allows us to rewrite the above as:

char
foo (int a)
  {
    char *x = "hello world";
    if (a)
      abort ();
    else
      x = x;
    return *x;
 }

The benefits are in better constant propagation, we no longer have to consider values which would trigger undefined behaviour.

Consider that constant propagation now allows us to rewrite the example as:

char
foo (int a)
  {
    if (a)
      abort ();
    return 'h';
 }

For reference, the other half of this optimisation applies to functions restricted by attributes to return null/non-null.

-fisolate-erroneous-paths-attribute Detect paths which trigger erroneous or undefined behaviour due a NULL value being used in a way which is forbidden by a returns_nonnull or nonnull attribute. Isolate those paths from the main control flow and turn the statement with erroneous or undefined behaviour into a trap. This is not currently enabled, but may be enabled by -O2 in the future.

The documentation can be found at http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top