From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19573 invoked by alias); 12 Oct 2010 08:42:54 -0000 Received: (qmail 19563 invoked by uid 22791); 12 Oct 2010 08:42:53 -0000 X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 12 Oct 2010 08:42:48 +0000 From: "carrot at google dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/45980] New: Use not in stead of add to generate new constant X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: carrot at google dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Date: Tue, 12 Oct 2010 08:42:00 -0000 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: 2010-10/txt/msg00966.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45980 Summary: Use not in stead of add to generate new constant Product: gcc Version: 4.6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target AssignedTo: unassigned@gcc.gnu.org ReportedBy: carrot@google.com CC: carrot@google.com Host: i686-linux Target: arm-eabi Build: i686-linux Compile the following code: typedef struct { unsigned long state[5]; unsigned long count[2]; } SHA1_CTX; void SHA1Init(SHA1_CTX* context) { /* SHA1 initialization constants */ context->state[0] = 0x67452301; context->state[1] = 0xEFCDAB89; context->state[2] = 0x98BADCFE; context->state[3] = 0x10325476; context->state[4] = 0xC3D2E1F0; context->count[0] = context->count[1] = 0; } With options -march=armv7-a -mthumb -Os, gcc generates: SHA1Init: ldr r3, .L2 str r3, [r0, #0] add r3, r3, #-2004318072 str r3, [r0, #4] ldr r3, .L2+4 str r3, [r0, #8] sub r3, r3, #-2004318072 str r3, [r0, #12] ldr r3, .L2+8 str r3, [r0, #16] movs r3, #0 str r3, [r0, #24] str r3, [r0, #20] bx lr .L3: .align 2 .L2: .word 1732584193 .word -1732584194 .word -1009589776 This function needs to store 5 large constants to memory. Instead of load the 5 constants from constant pool, gcc found two of them can be computed out by a single add/sub constant instruction. But we can do better, notice that 0x67452301 + 0x98BADCFE = 0xFFFFFFFF 0xEFCDAB89 + 0x10325476 = 0xFFFFFFFF So if we have one such constant, the other one can be computed out by bitwise not. So a shorter result could be: SHA1Init: ldr r3, .L2 str r3, [r0, #0] add r2, r3, #-2004318072 str r2, [r0, #4] movns r3, r3 str r3, [r0, #8] movns r2, r2 str r2, [r0, #12] ldr r3, .L2+4 str r3, [r0, #16] movs r3, #0 str r3, [r0, #24] str r3, [r0, #20] bx lr .L3: .align 2 .L2: .word 1732584193 .word -1009589776