Re: Compiler optimization of division and remainder

johnmce@world.std.com (John McEnerney)
29 Jan 1996 17:38:11 -0500

          From comp.compilers

Related articles
Compiler optimization of division and remainder hbaker@netcom.com (1996-01-27)
Re: Compiler optimization of division and remainder d.sand@ix.netcom.com (1996-01-27)
Re: Compiler optimization of division and remainder augustss@cs.chalmers.se (1996-01-27)
Re: Compiler optimization of division and remainder richard@atheist.tamu.edu (1996-01-28)
Re: Compiler optimization of division and remainder prener@watson.ibm.com (1996-01-29)
Re: Compiler optimization of division and remainder hbaker@netcom.com (1996-01-29)
Re: Compiler optimization of division and remainder Peter-Lawrence.Montgomery@cwi.nl (1996-01-29)
Re: Compiler optimization of division and remainder johnmce@world.std.com (1996-01-29)
Re: Compiler optimization of division and remainder michael.williams@armltd.co.uk (1996-01-29)
Re: Compiler optimization of division and remainder jgj@ssd.hcsc.com (1996-01-29)
Re: Compiler optimization of division and remainder dave@occl-cam.demon.co.uk (Dave Lloyd) (1996-01-29)
Re: Compiler optimization of division and remainder dave@occl-cam.demon.co.uk (Dave Lloyd) (1996-02-02)
| List of all articles for this month |
From: johnmce@world.std.com (John McEnerney)
Newsgroups: comp.compilers
Date: 29 Jan 1996 17:38:11 -0500
Organization: Metrowerks, Inc.
References: 96-01-088 96-01-101
Keywords: arithmetic, optimize

hbaker@netcom.com (Henry G. Baker) writes:
> [do any compilers actually optimize a/b and a%b into a single operation
> if both appear?]


prener@watson.ibm.com (Dan Prener) wrote:
> IBM's xlc compiler does this, when compiling for the POWER (but not
> the PowerPC) architecture.


Metrowerks CodeWarrior for PowerPC will sort of do this:


        int main(int a, int b) { return(a / b + a % b); }


                00000000: 7CA323D6 divw r5,r3,r4
                00000004: 7C0521D6 mullw r0,r5,r4
                00000008: 7C001850 sub r0,r3,r0
                0000000C: 7C650214 add r3,r5,r0
                00000010: 4E800020 blr


The PowerPC does not compute a remainder result--thus the MULLW/SUB--but
the compiler manages to avoid doing the expensive DIVW twice. It's
actually just a side-effect of performing the CSE optimization at the
machine instruction level--the compiler generates DIVW twice but the
optimizer realizes that they compute the same result and optimizes one of
them away.


--
John McEnerney (mcenerney@metrowerks.com)
Metrowerks PowerPC Compiler Architect
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.