gcc - 什么是隔离错误路径优化

标签 gcc optimization compiler-construction compiler-optimization

什么是 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/

相关文章:

macos - OS X 10.8、llvm、带有 CMake 的 OpenMP

gcc - 如何使用 GCC C 代码与 RISC-V CSR 交互?

eclipse - Eclipse 4.2 非常慢,如何使其响应更快?

java - 异常: "Invalid action number found in internal parse table." Polyglot Exception

c - cortex-m0plus 上的浮点库

c - 弱符号和弱引用

python - 如何从 Python 中的点列表创建一个以椭圆体为中心的二进制 3 维矩阵?

php - 一对多的最优数据库结构设计

c++ - 如何修复 Turbo C++ 错误 "Cannot open include file: graphics.h: no such files or director"

python - 识别缩进 block 的词法分析器