The lim pass (aka loop invariant motion) can move conditional expressions with 
possible undefined behavior out of the if statement inside a loop which may 
cause the loop optimization to silently generate wrong code as PR 
tree-optimization/58143 and PR tree-optimization/58227 demonstrate. 

This patch prevents lim from moving code that might cause undefined behavior.

This triggered a minor regression in gcc.target/i386/pr53397-1.c:
Here lim used to move the expression "2*step" out of the loop, but this may 
cause undefined behavior on case of overflow, I propose to resolve this by 
adding -fno-strict-overflow. The test case looks pretty constructed anyway.

The patch was boot-strapped and regression tested on i686-pc-linux-gnu and 
X86_64-linux-gnu

OK for trunk?

Regards
Bernd Edlinger                                    
2013-08-28  Bernd Edlinger  <bernd.edlin...@hotmail.de>

        PR tree-optimization/58143
        PR tree-optimization/58227
        Prevent lim from moving conditional expressions
        if that could trigger undefined behavior.
        * gimple.h (gimple_could_trap_p_2): New function.
        * gimple.c (gimple_could_trap_p_2): New function.
        (gimple_could_trap_p_1): Call gimple_could_trap_p_2.
        * tree-ssa-loop-im.c (movement_possibility): Likewise.


testsuite/

        * gcc.dg/pr58143.c: New test.
        * gcc.target/i386/pr53397-1.c: Added -fno-strict-overflow.
        * gcc.target/i386/pr53397-2.c: Likewise.

Attachment: patch-lim.diff
Description: Binary data

Reply via email to