From patchwork Tue May 8 14:43:23 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dehao Chen X-Patchwork-Id: 157704 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id DE8C5B6EEB for ; Wed, 9 May 2012 00:43:45 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1337093026; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: MIME-Version:Received:Received:Date:Message-ID:Subject:From:To: Cc:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=EuQDcrK vtAgpEij6G8lXyiUDFvw=; b=GtxjCzqLLgd+gsYofKC55IB/jJGW5eKOyv/VAXC i+oH2yxdtkjkrMOYezEkrAIYwb90bncRdDYxh27Sy8MT/CQsVO3antKRgkD2Hg9F VKuRJabipg5+8peCkEx7QQWivVam8kOOESWxg19w3ScwYn92Pp3iYjgwpggHvnwh edEM= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:MIME-Version:Received:Received:Date:Message-ID:Subject:From:To:Cc:Content-Type:X-System-Of-Record:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=pzDTX3lGJy0DPxcV7LiyyeZQSFDskMQThyGt4ZO5YSXoruVhDVLXWqtKkHwN2W PkEd+I+5bukIhozg8ZjIs9a5EF9UmIhoCq9bgO69EJZncatYe6lw9Sv8Yw4CABU4 lawkwruMVNOPbjoDczpIIiK+IMnldMURZI/vWz/ZhwqLA=; Received: (qmail 19235 invoked by alias); 8 May 2012 14:43:40 -0000 Received: (qmail 19212 invoked by uid 22791); 8 May 2012 14:43:38 -0000 X-SWARE-Spam-Status: No, hits=-4.4 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, TW_FC, TW_FD, TW_TM, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-pz0-f47.google.com (HELO mail-pz0-f47.google.com) (209.85.210.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 08 May 2012 14:43:24 +0000 Received: by dalh21 with SMTP id h21so8403119dal.20 for ; Tue, 08 May 2012 07:43:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:date:message-id:subject:from:to:cc:content-type :x-system-of-record:x-gm-message-state; bh=gZr6kEAYV16FlXQyYoHIaE6RtQldmTJpQyV8aHcVHQ0=; b=TZV2l7H+HRRdGacSSTOC9fr1KVVbSTpni9yuO3njUI8fdsfvFXQ1h42XpiuH2CpceR CzyuPg6x8z8mftVtiZqeqX2YhdG17+5DEe5dN5fnaS8/6EOd2EBmQUo/vwd4+iR+esVo rsc2i+CPDAp8zKN0d6/NI9RH6TPvZ2AlchWQFBWBh3fX7dC+p1RckJ5Y9ci1wmvXpAOd NNxX4SXRz3rNc07dzprAlr6fhKW7iUI7aygOTB5+2JwzLIoIdiuw7T2Y2tWA4z0RNfxO rF7+ujFMOEeNad+xcPZAoUHpW1Ptj6/d/s1WvuOwj+dk1tBIoMTlQh77xafpUew8dnvr lIDw== Received: by 10.68.129.42 with SMTP id nt10mr8835717pbb.164.1336488203822; Tue, 08 May 2012 07:43:23 -0700 (PDT) MIME-Version: 1.0 Received: by 10.68.129.42 with SMTP id nt10mr8835700pbb.164.1336488203755; Tue, 08 May 2012 07:43:23 -0700 (PDT) Received: by 10.68.240.105 with HTTP; Tue, 8 May 2012 07:43:23 -0700 (PDT) Date: Tue, 8 May 2012 22:43:23 +0800 Message-ID: Subject: [PATCH] Add -feliminate-malloc to enable/disable elimination of redundant malloc/free pairs From: Dehao Chen To: gcc-patches@gcc.gnu.org Cc: David Li X-System-Of-Record: true X-Gm-Message-State: ALoCoQkpoogRRCqvwi9RBiQiD8e6YKz9EYK6WBSbGZSZX57ZdSj0jLg7hdoIYmEE2fW0CET4qSc9V5+KdLlWfGmEA0mnTh2NGO38uXlrb02xTxoCuxo5okjJQJCU0YWAP/eyliu9gw8Hr/EElltb4jD0BURuMXO+hQ== X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hello, This patch adds a flag to guard the optimization that optimize the following code away: free (malloc (4)); In some cases, we'd like this type of malloc/free pairs to remain in the optimized code. Tested with bootstrap, and no regression in the gcc testsuite. Is it ok for mainline? Thanks, Dehao gcc/ChangeLog 2012-05-08 Dehao Chen * common.opt (feliminate-malloc): New. * doc/invoke.texi: Document it. * tree-ssa-dce.c (mark_stmt_if_obviously_necessary): Honor it. gcc/testsuite/ChangeLog 2012-05-08 Dehao Chen * gcc.dg/free-malloc.c: Check if -fno-eliminate-malloc is working as expected. Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 187277) +++ gcc/doc/invoke.texi (working copy) @@ -360,7 +360,8 @@ -fcx-limited-range @gol -fdata-sections -fdce -fdelayed-branch @gol -fdelete-null-pointer-checks -fdevirtualize -fdse @gol --fearly-inlining -fipa-sra -fexpensive-optimizations -ffat-lto-objects @gol +-fearly-inlining -feliminate-malloc -fipa-sra -fexpensive-optimizations @gol +-ffat-lto-objects @gol -ffast-math -ffinite-math-only -ffloat-store -fexcess-precision=@var{style} @gol -fforward-propagate -ffp-contract=@var{style} -ffunction-sections @gol -fgcse -fgcse-after-reload -fgcse-las -fgcse-lm -fgraphite-identity @gol @@ -6238,6 +6239,7 @@ -fdefer-pop @gol -fdelayed-branch @gol -fdse @gol +-feliminate-malloc @gol -fguess-branch-probability @gol -fif-conversion2 @gol -fif-conversion @gol @@ -6762,6 +6764,11 @@ Perform dead store elimination (DSE) on RTL@. Enabled by default at @option{-O} and higher. +@item -feliminate-malloc +@opindex feliminate-malloc +Eliminate unnecessary malloc/free pairs. +Enabled by default at @option{-O} and higher. + @item -fif-conversion @opindex fif-conversion Attempt to transform conditional jumps into branch-less equivalents. This Index: gcc/testsuite/gcc.dg/free-malloc.c =================================================================== --- gcc/testsuite/gcc.dg/free-malloc.c (revision 0) +++ gcc/testsuite/gcc.dg/free-malloc.c (revision 0) @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-eliminate-malloc" } */ +/* { dg-final { scan-assembler-times "malloc" 2} } */ +/* { dg-final { scan-assembler-times "free" 2} } */ + +extern void * malloc (unsigned long); +extern void free (void *); + +void test () +{ + free (malloc (10)); +} Index: gcc/common.opt =================================================================== --- gcc/common.opt (revision 187277) +++ gcc/common.opt (working copy) @@ -1474,6 +1474,10 @@ Common Var(flag_dce) Init(1) Optimization Use the RTL dead code elimination pass +feliminate-malloc +Common Var(flag_eliminate_malloc) Init(1) Optimization +Eliminate unnecessary malloc/free pairs + fdse Common Var(flag_dse) Init(1) Optimization Use the RTL dead store elimination pass Index: gcc/tree-ssa-dce.c =================================================================== --- gcc/tree-ssa-dce.c (revision 187277) +++ gcc/tree-ssa-dce.c (working copy) @@ -309,6 +309,8 @@ case BUILT_IN_CALLOC: case BUILT_IN_ALLOCA: case BUILT_IN_ALLOCA_WITH_ALIGN: + if (!flag_eliminate_malloc) + mark_stmt_necessary (stmt, true); return; default:;