Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!agate!shelby!neon!kaufman From: kaufman@Neon.Stanford.EDU (Marc T. Kaufman) Newsgroups: comp.sys.mac.programmer Subject: Re: Increment (Was Re: Pascal deficiency)? Message-ID: <1990Dec18.015258.8631@Neon.Stanford.EDU> Date: 18 Dec 90 01:52:58 GMT References: <1990Dec18.001753.3756@Neon.Stanford.EDU> <1990Dec18.004838.5623@Neon.Stanford.EDU> Organization: Computer Science Department, Stanford University Lines: 50 In article <1990Dec18.004838.5623@Neon.Stanford.EDU> philip@pescadero.stanford.edu writes: >In article <1990Dec18.001753.3756@Neon.Stanford.EDU>, kaufman@Neon.Stanford.EDU (Marc T. Kaufman) writes: |>> Well, since we ARE in a Mac group, lets just look at the code MPW C generates |>> for just such constructs: |>> |>> i = i+1; |>> MOVE.L i,D2 |>> ADDQ.L #$1,D2 |>> MOVE.L D2,i |>> |>> i++; |>> MOVE.L i,D2 |>> ADDQ.L #$1,i |>> |>> ++i; |>> ADDQ.L #$1,i |>> |>> Behold. The 68K does, indeed, have an instruction to increment variables in |>> memory. >Interesting - but remember looking at "toy" examples doesn't tell you much. >In "real" code, where performance really matters, I would hope the compiler >would have loaded the variable into a register for as long as possbile. Well, to generate the above code, I declared 'external int i'. The C compiler DOES put things in registers. However, MPW C still generates pretty crufty code for i = i+1: MOVE.L D2,D0 ADDQ.L #$1,D0 MOVE.L D0,D2 and this is with optimization ON! The MOVE.L i,D2 in the i++ case is because the value of the expression (i++) is (i) before the +1. D2 is dead because we never use the expression value, and its a shame that the compiler doesn't remove it. I'm going to look at gcc and see if doesn't generate better code. With the complexity of today's applications, every little 5% helps. Marc Kaufman (kaufman@Neon.stanford.edu) >Still, it's bad that the compiler doesn't pick up i=i+small constant as a >special case - it would be even worse if the Pascal compiler also did this >since (the original point) you have no option of asking for i++. > >Many of the programmer-directed "optimizations" in C, like register >variables, ought to be unnecessary with a modern optimizing compiler. >-- >Philip Machanick >philip@pescadero.stanford.edu