[backport from gcc-4.7/trunk r179188, fixes PR45771 ] gcc/ 2011-09-26 Eric Botcazou * ifcvt.c (noce_try_cmove_arith): Use may_trap_or_fault_p in lieu of may_trap_p to detect loads that may trap of fault. gcc/testsuite/ 2011-09-26 Eric Botcazou * gnat.dg/opt21.adb: New test. * gnat.dg/opt21_pkg.ad[sb]: New helper. --- gcc-4.6.2/gcc/ifcvt.c.~1~ 2010-12-14 01:23:40.000000000 +0100 +++ gcc-4.6.2/gcc/ifcvt.c 2012-02-05 22:36:16.000000000 +0100 @@ -1512,9 +1512,9 @@ noce_try_cmove_arith (struct noce_if_inf } /* ??? We could handle this if we knew that a load from A or B could - not fault. This is also true if we've already loaded + not trap or fault. This is also true if we've already loaded from the address along the path from ENTRY. */ - else if (may_trap_p (a) || may_trap_p (b)) + else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b)) return FALSE; /* if (test) x = a + b; else x = c - d; --- gcc-4.6.2/gcc/testsuite/gnat.dg/opt21.adb.~1~ 1970-01-01 01:00:00.000000000 +0100 +++ gcc-4.6.2/gcc/testsuite/gnat.dg/opt21.adb 2012-02-05 22:36:16.000000000 +0100 @@ -0,0 +1,11 @@ +-- { dg-do run } +-- { dg-options "-O2" } + +with System; +with Opt21_Pkg; use Opt21_Pkg; + +procedure Opt21 is + V : System.Address := Convert (null); +begin + null; +end; --- gcc-4.6.2/gcc/testsuite/gnat.dg/opt21_pkg.adb.~1~ 1970-01-01 01:00:00.000000000 +0100 +++ gcc-4.6.2/gcc/testsuite/gnat.dg/opt21_pkg.adb 2012-02-05 22:36:16.000000000 +0100 @@ -0,0 +1,17 @@ +package body Opt21_Pkg is + + function Get_Object (Object : not null access R) return System.Address is + begin + return Object.Ptr; + end; + + function Convert (W : Obj) return System.Address is + begin + if W = null then + return System.Null_Address; + else + return Get_Object (W); + end if; + end; + +end Opt21_Pkg; --- gcc-4.6.2/gcc/testsuite/gnat.dg/opt21_pkg.ads.~1~ 1970-01-01 01:00:00.000000000 +0100 +++ gcc-4.6.2/gcc/testsuite/gnat.dg/opt21_pkg.ads 2012-02-05 22:36:16.000000000 +0100 @@ -0,0 +1,15 @@ +with System; + +package Opt21_Pkg is + + type R is record + Ptr : System.Address := System.Null_Address; + end record; + + type Obj is access all R; + + function Get_Object (Object : not null access R) return System.Address; + + function Convert (W : Obj) return System.Address; + +end Opt21_Pkg;