From: Skybuck Flying on
Hello,

"multiplication illimination" is an optimization which D2007 does currently
not do:

Calculation1 multiplies each addition part with vD.
Calculation2 optimizes this by first adding them together and then doing
multiply vD which is the same thing, sort of ;) Only difference would be
with possible overflows at different instruction locations.

See test program for details/comments:

// *** Begin of Test Program ***

program TestProgram;

{$APPTYPE CONSOLE}

{

Test multiplication illimination by Delphi compiler

version 0.01 created on 8 february 2010 by Skybuck Flying.

Nope Delphi does not optimize the code:

Results with range checking off and overflow checking off and optimizations
on:

TestProgram.dpr.22: vA := 5;
00408E47 BB05000000 mov ebx,$00000005
TestProgram.dpr.23: vB := 7;
00408E4C BA07000000 mov edx,$00000007
TestProgram.dpr.24: vC := 9;
00408E51 B809000000 mov eax,$00000009
TestProgram.dpr.25: vD := 4;
00408E56 B904000000 mov ecx,$00000004

// calculation1 uses 6 muls:
TestProgram.dpr.27: vCalculation1 := (vA * vB * vC * vD) + (vB * vC * vD) +
(vC * vD);
00408E5B 8BF3 mov esi,ebx
00408E5D 0FAFF2 imul esi,edx
00408E60 0FAFF0 imul esi,eax
00408E63 0FAFF1 imul esi,ecx
00408E66 8BFA mov edi,edx
00408E68 0FAFF8 imul edi,eax
00408E6B 0FAFF9 imul edi,ecx
00408E6E 03F7 add esi,edi
00408E70 8BF8 mov edi,eax
00408E72 0FAFF9 imul edi,ecx
00408E75 03F7 add esi,edi

// calculation2 uses 4 muls:
TestProgram.dpr.28: vCalculation2 := ( (vA * vB * vC) + (vB * vC) + vC) *
vD;
00408E77 0FAFDA imul ebx,edx
00408E7A 0FAFD8 imul ebx,eax
00408E7D 0FAFD0 imul edx,eax
00408E80 03DA add ebx,edx
00408E82 03C3 add eax,ebx
00408E84 F7E9 imul ecx
00408E86 8BD8 mov ebx,eax

}

uses
SysUtils;

procedure Main;
var
vA, vB, vC, vD : integer;
vCalculation1 : integer;
vCalculation2 : integer;
begin
vA := 5;
vB := 7;
vC := 9;
vD := 4;

vCalculation1 := (vA * vB * vC * vD) + (vB * vC * vD) + (vC * vD);
vCalculation2 := ( (vA * vB * vC) + (vB * vC) + vC) * vD;

writeln( vCalculation1 );
writeln( vCalculation2 );
end;

begin
try
Main;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
ReadLn;
end.

// *** End of Test Program ***

Bye,
Skybuck.