From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20672 invoked by alias); 25 Jul 2007 15:05:48 -0000 Received: (qmail 20631 invoked by uid 48); 25 Jul 2007 15:05:37 -0000 Date: Wed, 25 Jul 2007 15:05:00 -0000 Message-ID: <20070725150537.20630.qmail@sourceware.org> X-Bugzilla-Reason: CC References: Subject: [Bug target/30354] -Os doesn't optimize a/CONST even if it saves size. In-Reply-To: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "vda dot linux at googlemail dot com" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2007-07/txt/msg02569.txt.bz2 ------- Comment #2 from vda dot linux at googlemail dot com 2007-07-25 15:05 ------- Created an attachment (id=13973) --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=13973&action=view) Fix: adjust div cost for -Os on i386 Patch was tested with 4.2.1, I guess it will apply to other versions of gcc as it is quite trivial. Test program with lots or randomly-generated constant divisions and %'s was compiled by patched gcc with different costs of division: text data bss dec hex filename 257731 0 0 257731 3eec3 421.org/t-Os.o 256788 0 0 256788 3eb14 421.7/t-Os.o 256788 0 0 256788 3eb14 421.8/t-Os.o 257377 0 0 257377 3ed61 421.9/t-Os.o 257825 0 0 257825 3ef21 421.10/t-Os.o Seems like (at least on 4.2.1) cost of 8 is giving smallest code. Among 15000 divisions in test program, only signed divisions by power-of-two grew in size (but they become MUCH faster, as they don't even use multiply now, let alone div): @@ -1703 +1703 @@ -0000000f T id_x_16 +00000012 T id_x_16 @@ -1836 +1836 @@ -0000000f T id_x_2 +0000000e T id_x_2 @@ -2030 +2030 @@ -0000000f T id_x_32 +00000012 T id_x_32 id_x_16 was: movl 4(%esp), %eax movl $16, %edx movl %edx, %ecx cltd idivl %ecx ret Now it is: movl 4(%esp), %edx movl %edx, %eax sarl $31, %eax andl $15, %eax addl %edx, %eax sarl $4, %eax ret and also unsigned_x / 28, unsigned_x / 13952, unsigned_x / 56 grew by 1 byte. The rest either were not changed (and still use div insn) or shrank (typically by 1 byte, record holders are "unsigned_x / 641" and "unsigned_x / 6700417" - shrank by 4 bytes). -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30354