什么是 GCC 中的隔离错误路径优化?怎么运行的 ?
隔离去年添加到 gcc 的错误路径优化:http://gcc.1065356.n5.nabble.com/RFA-PATCH-Isolate-erroneous-paths-optimization-td980376.html
最佳答案
添加的两个标志的文档应该有助于您的理解:
-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.
考虑这个人为的代码块:
char
foo (int a)
{
char *x = "hello world";
if (a) /* (1). */
x = NULL;
else
x = x;
return *x; /* (2). */
}
在 a != 0
的情况下,我们采用标记为 (1) 的路径。这会导致 (2) 处出现未定义的行为,其中 x
等于 NULL
指针。
这种优化允许我们将上面的内容重写为:
char
foo (int a)
{
char *x = "hello world";
if (a)
abort ();
else
x = x;
return *x;
}
好处是更好的持续传播,我们不再需要考虑会触发未定义行为的值。
考虑到恒定传播现在允许我们将示例重写为:
char
foo (int a)
{
if (a)
abort ();
return 'h';
}
作为引用,此优化的另一半适用于受属性限制返回 null/非 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.
可以在 http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options 找到文档
关于gcc - 什么是隔离错误路径优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22627363/