From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12263 invoked by alias); 2 Aug 2011 06:47:36 -0000 Received: (qmail 12254 invoked by uid 22791); 2 Aug 2011 06:47:35 -0000 X-SWARE-Spam-Status: No, hits=-1.4 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE X-Spam-Check-By: sourceware.org Received: from mo-p00-ob.rzone.de (HELO mo-p00-ob.rzone.de) (81.169.146.160) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 02 Aug 2011 06:47:15 +0000 X-RZG-AUTH: :LXoWVUeid/7A29J/hMvvT2k715jHQaJercGObUOFkj18odoYNahU4Q== X-RZG-CLASS-ID: mo00 Received: from [192.168.0.22] (business-188-111-022-002.static.arcor-ip.net [188.111.22.2]) by smtp.strato.de (jimi mo50) (RZmta 26.2) with ESMTPA id z04e34n726ZM4a ; Tue, 2 Aug 2011 08:47:03 +0200 (MEST) Message-ID: <4E379D67.3050305@gjlay.de> Date: Tue, 02 Aug 2011 06:47:00 -0000 From: Georg-Johann Lay User-Agent: Thunderbird 2.0.0.24 (X11/20100302) MIME-Version: 1.0 To: Michael Walle CC: gcc@gcc.gnu.org Subject: Re: libgcc: strange optimization References: <201108012230.29989.michael@walle.cc> <4E37116F.1050608@gjlay.de> <201108012313.44407.michael@walle.cc> In-Reply-To: <201108012313.44407.michael@walle.cc> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2011-08/txt/msg00035.txt.bz2 Michael Walle wrote: > Hi, > > That was quick :) > >> Your asm has no output operands and no side effects, with more >> aggressive optimization the whole ask would disappear. > Sorry, that was just a small test file, the original code has output operands. > > The new test code: > static int inline f1(int arg) > { > register int ret asm("r1"); > register int a1 asm("r8") = 10; > register int a2 asm("r1") = arg; > > asm volatile ("scall" : "=r"(ret) : "r"(a1), "r"(a2) : "memory"); > > return ret; > } > > int f2(int arg1, int arg2) > { > return f1(arg1 >> 10); > } > > translates to the same assembly: > f2: > addi sp, sp, -4 > sw (sp+4), ra > addi r2, r0, 10 > calli __ashrsi3 > scall > lw ra, (sp+4) > addi sp, sp, 4 > b ra > > PS. R1 is the return register in the target architecture ABI. I'd guess you ran into http://gcc.gnu.org/onlinedocs/gcc/Local-Reg-Vars.html#Local-Reg-Vars A common pitfall is to initialize multiple call-clobbered registers with arbitrary expressions, where a function call or library call for an arithmetic operator will overwrite a register value from a previous assignment, for example r0 below: register int *p1 asm ("r0") = ...; register int *p2 asm ("r1") = ...; In those cases, a solution is to use a temporary variable for each arbitrary expression. So I'd try to rewrite it as static int inline f1 (int arg0) { int arg = arg0; register int ret asm("r1"); register int a1 asm("r8") = 10; register int a2 asm("r1") = arg; asm volatile ("scall" : "=r"(ret) : "r"(a1), "r"(a2) : "memory"); return ret; } and if that does not help the rather hackish static int inline f1 (int arg0) { int arg = arg0; register int ret asm("r1"); register int a1 asm("r8"); register int a2 asm("r1"); asm ("" : "+r" (arg)); a1 = 10; a2 = arg; asm volatile ("scall" : "=r"(ret) : "r"(a1), "r"(a2) : "memory"); return ret; }