选项 -fchrec-mul-fold-strict-overflow

说明

乘法折叠优化,正确处理可能引发未定义行为(如整数溢出)的乘法运算。

使用说法

需开启 -O2 或以上优化等级,同时增加编译选项 -fchrec-mul-fold-strict-overflow

结果

测试用例如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
typedef int int32_t __attribute__((mode (__SI__)));
 
void bar (int32_t);
 
void
foo ()
{
    int32_t index = 0;
 
    for (index; index <= 10; index--)
    /* Result of the following multiply will overflow
       when converted to signed int32_t.  */
    bar ((0xcafe + index) * 0xdead);
}

测试命令:

1
gcc -O2 -fdisable-tree-ethread -fchrec-mul-fold-strict-overflow test.c -S -o test.s
图1 选项未打开时
图2 选项打开后