[m-rev.] diff: tail-call improvements for GCC back-end

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Sep 17 05:39:42 AEST 2002


Estimated hours taken: 1
Branches: main

Some improvements to tail-call optimization for the GCC back-end.

mercury/compiler/mlds_to_gcc.m:
	For tail calls, generate a return. 
	This helps GCC detect tail recursion.
	Don't treat no-return calls as tail-calls, since GCC doesn't
	optimize them anyway, and since it would cause problems if
	the return type of the caller doesn't match the return type
	of the callee.

gcc/mercury/mercury-gcc.c:
	Set the CALL_EXPR_TAILCALL flag for tail calls,
	if the version of GCC that we are using supports this flag.

Workspace: /home/mars/fjh/ws-gcc-tailcall/mercury
Index: compiler/mlds_to_gcc.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_gcc.m,v
retrieving revision 1.75
diff -u -d -u -r1.75 mlds_to_gcc.m
--- mlds_to_gcc.m	16 Sep 2002 19:03:01 -0000	1.75
+++ mlds_to_gcc.m	16 Sep 2002 19:34:09 -0000
@@ -2566,7 +2566,19 @@
 	build_rval(FuncRval, DefnInfo, GCC_FuncRval),
 	{
 		CallKind = no_return_call,
-		IsTailCall = yes
+		% XXX trying to optimize these leads to
+		% problems because we don't mark such calls
+		% with __attribute__((__noreturn__)) and so
+		% GCC thinks that they are not in a tail position.
+		% Marking them as with __attribute__((__noreturn__))
+		% doesn't help because GCC (3.3 beta) inhibits tail
+		% call optimization for such functions.
+		% Also, we can't insert a return statement (below)
+		% if the return type for the caller doesn't match
+		% that for the callee, but mlds_to_gcc.m currently doesn't
+		% pass down the signature of the caller to this point.
+		% So for now, treat these as if they were not tail calls.
+		IsTailCall = no
 	;
 		CallKind = tail_call,
 		IsTailCall = yes
@@ -2574,11 +2586,14 @@
 		CallKind = ordinary_call,
 		IsTailCall = no
 	},
-	% XXX GCC currently ignores the tail call boolean
 	gcc__build_call_expr(GCC_FuncRval, GCC_ArgList, IsTailCall, GCC_Call),
 	( { Results = [ResultLval] } ->
-		build_lval(ResultLval, DefnInfo, GCC_ResultExpr),
-		gcc__gen_assign(GCC_ResultExpr, GCC_Call)
+		( { IsTailCall = yes } ->
+			gcc__gen_return(GCC_Call)
+		;
+			build_lval(ResultLval, DefnInfo, GCC_ResultExpr),
+			gcc__gen_assign(GCC_ResultExpr, GCC_Call)
+		)
 	; { Results = [] } ->
 		gcc__gen_expr_stmt(GCC_Call)
 	;
Index: gcc/mercury/mercury-gcc.c
===================================================================
RCS file: /home/mercury1/repository/gcc/mercury/mercury-gcc.c,v
retrieving revision 1.31.2.2
diff -u -d -u -r1.31.2.2 mercury-gcc.c
--- mercury-gcc/gcc/mercury/mercury-gcc.c	16 Sep 2002 19:00:41 -0000	1.31.2.2
+++ mercury-gcc/gcc/mercury/mercury-gcc.c	16 Sep 2002 19:10:33 -0000
@@ -620,7 +620,15 @@
   fntype = TREE_TYPE (fnptrtype);
   rettype = TREE_TYPE (fntype);
   call = build (CALL_EXPR, rettype, fnptr, args, NULL_TREE);
+
+  /* Pass the FORCE_TAILCALL_P flag on to the GCC back-end,
+     if the version of GCC that we're using supports it.  */
+#ifdef CALL_EXPR_TAILCALL
+  CALL_EXPR_TAILCALL (call) = force_tailcall_p;
+#endif
+
   TREE_SIDE_EFFECTS (call) = 1;
+
   return fold (call);
 }
 
-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list