public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [fortran,committed] Don't check for NULL pointers before calling free()
@ 2015-08-31 11:12 FX
  0 siblings, 0 replies; only message in thread
From: FX @ 2015-08-31 11:12 UTC (permalink / raw)
  To: gcc patches; +Cc: gfortran

[-- Attachment #1: Type: text/plain, Size: 666 bytes --]

The attached patch changes the gfc_call_free() function to simply call __builtin_free() without first checking if the pointer is NULL, because free(NULL) is a nop. This simplifies code, but could lead to pessimization in cases where the pointer is likely to be NULL, and the test would have saved the function call. After auditing all the gfc_call_free() callers in the front-end, I come to the conclusion that there are no such cases (in fact, in most cases, the front-end knows for sure that the pointer is non-NULL, because it was just allocated some lines earlier).

Committed as rev. 227336 after bootstrap and regtesting on x86_64-apple-darwin15.

FX



[-- Attachment #2: free.ChangeLog --]
[-- Type: application/octet-stream, Size: 185 bytes --]

2015-08-31  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>

	PR fortran/54833
	* trans.c (gfc_call_free): Don't check if pointer is NULL.
	* trans.h (gfc_call_free): Adjust comment.


[-- Attachment #3: free.diff --]
[-- Type: application/octet-stream, Size: 1475 bytes --]

Index: gcc/fortran/trans.c
===================================================================
--- gcc/fortran/trans.c	(revision 227316)
+++ gcc/fortran/trans.c	(working copy)
@@ -850,23 +850,14 @@ gfc_allocate_allocatable (stmtblock_t * 
 }
 
 
-/* Free a given variable, if it's not NULL.  */
+/* Free a given variable.  */
 
 tree
 gfc_call_free (tree var)
 {
-  tree cond, call;
-
-  /* Only evaluate the variable once.  */
-  var = save_expr (fold_convert (pvoid_type_node, var));
-
-  cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, var,
-			  build_int_cst (pvoid_type_node, 0));
-  call = build_call_expr_loc (input_location,
+  return build_call_expr_loc (input_location,
 			      builtin_decl_explicit (BUILT_IN_FREE),
-			      1, var);
-  return fold_build3_loc (input_location, COND_EXPR, void_type_node,
-			  cond, call, build_empty_stmt (input_location));
+			      1, fold_convert (pvoid_type_node, var));
 }
 
 
Index: gcc/fortran/trans.h
===================================================================
--- gcc/fortran/trans.h	(revision 227316)
+++ gcc/fortran/trans.h	(working copy)
@@ -642,7 +642,7 @@ void gfc_trans_runtime_check (bool, bool
 void gfc_trans_same_strlen_check (const char*, locus*, tree, tree,
 				  stmtblock_t*);
 
-/* Generate a call to free() after checking that its arg is non-NULL.  */
+/* Generate a call to free().  */
 tree gfc_call_free (tree);
 
 /* Allocate memory after performing a few checks.  */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-08-31 10:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-31 11:12 [fortran,committed] Don't check for NULL pointers before calling free() FX

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).