Sender: nate AT cartsys DOT com Message-ID: <3612EDF6.54113777@cartsys.com> Date: Wed, 30 Sep 1998 19:50:30 -0700 From: Nate Eldredge X-Mailer: Mozilla 4.05 [en] (X11; I; Linux 2.0.35 i486) MIME-Version: 1.0 To: "David A. Scott" CC: djgpp AT delorie DOT com Subject: Re: bad code optimizations References: <6us48p$t1u$1 AT nnrp1 DOT dejanews DOT com> <6us6c0$4i2$1 AT news DOT ysu DOT edu> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit David A. Scott wrote: > > In a previous article, tomstdenis AT my-dejanews DOT com () says: > > >Sometimes I see things like: > > > >movl %eax,%eax > > This might be done to set flags. I'm fairly sure that `mov' doesn't set flags; `or' or `and' is usually used for that. > Also no > C compiler can come close to what any average program > can do if he takes his time. Since the programer is really > using addtional information that the complier can never > really know. And that is true of any high level language. > > > > >in the outputed code? That's really bad. > > > >Also, the following code: > > > >int test() > >{ > > return test2() % 4; > >} > > > >makes: > > > >_test: > > pushl %ebp > > movl %esp,%ebp > > call _test2 > > movl %eax,%edx > > testl %edx,%edx > > jge L2 > > leal 15(%edx),%eax > >L2: > > andb $240,%al > > subl %eax,%edx > > movl %edx,%eax > > leave > > ret > > > > > >The % 16, could be simplified into one bitshift, ala > > > >_test: > > pushl %ebp > > movl %esp,%ebp > > call _test2 > > shll $4,%eax You're mistaken; that would do a *division* by 16. You probably mean `andl $15, %eax'-- but that would come out wrong if the number was negative. Signed integers, remember? This is where a lot the extra work comes in. Change it to unsigned and I bet you'll see a difference. > most compilers never really take advantage of the > full instruction set. [snip discussion] That's true, but it's mainly because the instruction sets on CISCy processors like the x86 weren't designed for compilers, but for assembly programmers. A prime example are the BCD math instructions-- a godsend if you're doing BCD math in assembly, but nearly useless to a compiler. Stuff like `div' shows it too-- if they'd used the silicon on speeding up the regular math instead of all that microcode, a compiler might have been able to do a division just as fast. With RISC processors, the instruction sets are small but compiler-friendly, and it's often very difficult for a mere human to do as well at generating code. And in fact, the Pentia are converging to this-- some instructions that are easily definable in terms of others (such as `loop' and `leave') are now slower than their multi-instruction equivalents, since research has shown that compilers use the simpler forms more. -- Nate Eldredge nate AT cartsys DOT com