This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] PR19917: Calls to weak functions may trap

The following patch is my proposed solution to PR tree-optimization/19917
which is a wrong-code regression on mainline that's triggered by loop
invariant motion at the tree-level.

The problem is that given a loop like the following:

	  if (foo)
	    x = foo(4);

where foo is declared both "weak" and "const" we consider it loop
invariant and pull it out of the loop.  Conceptually, because the
function is "const", it's result only depends upon the value of its
arguments, those are loop invariant, hence the call to foo gets moved.

The problem is how to indicate to tree-ssa-loop-im.c that the call to
foo() is dependent upon the outcome of the if (foo) test, yet still be

The solution I've come up with is to tweak the tree_could_trap_p to
understand that calls to "weak" functions are potentially trapping.
This is sufficient to caue tree-ssa-loop-im.c to be more careful in
moving this invariant (like the loop invariant x/y), which in turn
resolves the PR.

The following patch has been tested on i686-pc-linux-gnu with a full
"make bootstrap", all default languages, and regression tested with
a top-level "make -k check" with no new failures.  It's also been
bootstrapped, all default languages, on ia64-unknonw-linux-gnu where
I've confirmed that it disables the problematic transformation.

Does this seem reasonable?

2005-02-12  Roger Sayle  <>

	PR tree-optimization/19917
	* tree-eh.c (tree_could_trap_p): Consider calls to weak functions
	to be potentially trapping.

Index: tree-eh.c
RCS file: /cvs/gcc/gcc/gcc/tree-eh.c,v
retrieving revision 2.24
diff -c -3 -p -r2.24 tree-eh.c
*** tree-eh.c	18 Jan 2005 11:36:24 -0000	2.24
--- tree-eh.c	12 Feb 2005 19:20:44 -0000
*************** tree_could_trap_p (tree expr)
*** 1840,1845 ****
--- 1840,1852 ----
  	return true;
        return false;

+     case CALL_EXPR:
+       t = get_callee_fndecl (expr);
+       /* Assume that calls to weak functions may trap.  */
+       if (!t || !DECL_P (t) || DECL_WEAK (t))
+ 	return true;
+       return false;
        /* Any floating arithmetic may trap.  */
        if (fp_operation && flag_trapping_math)

Roger Sayle,                         E-mail:
OpenEye Scientific Software,         WWW:
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]