* inter-register CSE, still requires some tweaks (peepholeoptpass2, better RegAlloc)

This commit is contained in:
Jonas Maebe 1998-10-01 20:21:47 +00:00
parent 68bc49c014
commit 965c651dcc
2 changed files with 891 additions and 312 deletions

View File

@ -31,58 +31,110 @@ Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
Implementation
Uses CObjects, verbose
Uses CObjects, verbose, hcodegen, globals
{$ifdef i386}
,i386, DAOpt386
{$endif i386}
;
Function CheckSequence(p: Pai; Reg: TRegister; Var Found: Longint): Boolean;
Function PaiInSequence(P: Pai; Const Seq: TContent): Boolean;
Var P1: Pai;
Counter: Byte;
TmpResult: Boolean;
Begin
TmpResult := False;
P1 := Seq.StartMod;
Counter := 1;
While Not(TmpResult) And
(Counter <= Seq.NrOfMods) Do
Begin
If (P = P1) Then TmpResult := True;
Inc(Counter);
p1 := Pai(p1^.Next);
End;
PaiInSequence := TmpResult;
End;
Function CheckSequence(p: Pai; Reg: TRegister; Var Found: Longint; Var RegInfo: TRegInfo): Boolean;
{checks whether the current instruction sequence (starting with p) and the
one between StartMod and EndMod of Reg are the same. If so, the number of
instructions that match is stored in Found and true is returned, otherwise
Found holds the number of instructions between StartMod and EndMod and false
is returned}
Var hp2, hp3, EndMod: Pai;
Counter: Byte;
PrevNonRemovablePai: Pai;
OrgRegInfo, HighRegInfo: TRegInfo;
HighFound, OrgRegFound: Byte;
RegCounter: TRegister;
OrgRegResult: Boolean;
TmpResult: Boolean;
OldNrOfMods: Byte;
Begin {CheckSequence}
Reg := Reg32(Reg);
Found := 0;
hp3 := p;
While GetLastInstruction(hp3, hp3) And
PPAiProp(hp3^.fileinfo.line)^.CanBeRemoved Do;
hp2 := PPaiProp(hp3^.fileinfo.line)^.Regs[Reg].StartMod;
EndMod := hp2;
If (PPaiProp(hp3^.fileinfo.line)^.Regs[Reg].NrOfMods = 1)
Then Counter := 1
Else
For Counter := 2 to PPaiProp(hp3^.fileinfo.line)^.Regs[Reg].NrOfMods Do
GetNextInstruction(EndMod, EndMod);
hp3 := p;
While (Found <> Counter) And
InstructionsEqual(hp2, hp3) Do
Begin
GetNextInstruction(hp2, hp2);
GetNextInstruction(hp3, hp3);
Inc(Found)
End;
If (Found <> Counter)
Then
Begin
If ((Found+1) = Counter) And
Assigned(hp2) And
(Pai(hp2)^.typ = ait_instruction) And
(Pai386(hp2)^._operator In [A_MOV, A_MOVZX]) And
(Pai386(hp2)^.op1t = top_ref) And
(Pai386(hp2)^.op2t = top_reg) And
Assigned(hp3) And
(Pai(hp3)^.typ = ait_instruction) And
(Pai386(hp3)^._operator In [A_MOV, A_MOVZX]) And
(Pai386(hp3)^.op1t = top_ref) And
(Pai386(hp3)^.op2t = top_reg) And
(Pai386(hp2)^._operator <> Pai386(hp3)^._operator) And
RefsEqual(TReference(Pai386(hp2)^.op1^),TReference(Pai386(hp3)^.op1^))
Then
TmpResult := False;
HighFound := 0;
RegCounter := R_EAX;
GetLastInstruction(p, PrevNonRemovablePai);
While (PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].Typ <> Con_Ref) Or
{ the next check is because what a register contains is stored in the
ppaiprop of the first instruction of the loading sequence. If it's left
out,
movl 8(%ebp), %eax movl 8(%ebp), %eax
movl 8(%ebp), %edx movl %eax, %edx
movl (%edx), %edx would be changed to
movl (%eax), %eax movl (%eax), %eax
cmpl (%edx), %eax cmpl (%edx), %eax}
((Regcounter <> Reg) And
PaiInSequence(p, PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter])) Do
Inc(RegCounter);
{no need to check whether RegCounter becomes > R_EDI here, because at least
PPaiProp(p^.fileinfo.line)^.Regs[Reg].Typ = Con_Ref}
Repeat
FillChar(RegInfo, SizeOf(RegInfo), 0);
RegInfo.RegsEncountered := [ProcInfo.FramePointer, R_ESP];
RegInfo.SubstRegs[ProcInfo.FramePointer] := ProcInfo.FramePointer;
RegInfo.SubstRegs[R_ESP] := R_ESP;
Found := 0;
hp2 := PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].StartMod;
{ EndMod := hp2;
If (PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].NrOfMods = 1)
Then OldNrOfMods := 1
Else
For OldNrOfMods := 2 to PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].NrOfMods Do
GetNextInstruction(EndMod, EndMod);}
OldNrOfMods := PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].NrOfMods;
hp3 := p;
While (Found <> OldNrOfMods) And
{ old new }
InstructionsEquivalent(hp2, hp3, RegInfo) Do
Begin
GetNextInstruction(hp2, hp2);
GetNextInstruction(hp3, hp3);
Inc(Found)
End;
If (Found <> OldNrOfMods)
Then
Begin
(* If ((Found+1) = OldNrOfMods) And
Assigned(hp2) And
(Pai(hp2)^.typ = ait_instruction) And
(Pai386(hp2)^._operator In [A_MOV, A_MOVZX]) And
(Pai386(hp2)^.op1t = top_ref) And
(Pai386(hp2)^.op2t = top_reg) And
Assigned(hp3) And
(Pai(hp3)^.typ = ait_instruction) And
(Pai386(hp3)^._operator In [A_MOV, A_MOVZX]) And
(Pai386(hp3)^.op1t = top_ref) And
(Pai386(hp3)^.op2t = top_reg) And
(Pai386(hp2)^._operator <> Pai386(hp3)^._operator) And
{$IfDef RegInfo}
RefsEquivalent(TReference(Pai386(hp2)^.op1^),TReference(Pai386(hp3)^.op1^), RegInfo)
{$Else RegInfo}
RefsEqual(TReference(Pai386(hp2)^.op1^),TReference(Pai386(hp3)^.op1^))
{$EndIf RegInfo}
Then
{hack to be able to optimize
mov (mem), reg
@ -94,48 +146,59 @@ Begin {CheckSequence}
mov (reg), reg
movzx (reg), reg [*]}
If (Pai386(hp2)^._operator = A_MOV)
Then
Begin
If (Pai386(hp2)^.Size = S_B) And
(Reg8toReg32(TRegister(Pai386(hp2)^.op2)) =
TRegister(Pai386(hp3)^.op2))
Then
Begin
Pai386(hp2)^._operator := A_MOVZX;
Pai386(hp2)^.op2 := Pai386(hp3)^.op2;
Pai386(hp2)^.Size := S_BL;
Inc(Found);
CheckSequence := True;
End
Else
Begin
CheckSequence := False;
If (Found > 0) Then
Found := PPaiProp(Pai(p)^.fileinfo.line)^.Regs[Reg].NrOfMods
End
End
Else
Begin
If (Pai386(hp3)^.Size = S_B) And
(Reg8toReg32(TRegister(Pai386(hp3)^.op2)) =
TRegister(Pai386(hp2)^.op2))
Then
Begin
CheckSequence := True;
Inc(Found)
End
Else
Begin
CheckSequence := False;
If (Found > 0) Then
Found := PPaiProp(Pai(p)^.fileinfo.line)^.Regs[Reg].NrOfMods
End
End
Else
Begin
CheckSequence := False;
If (found > 0) then
If (Pai386(hp2)^._operator = A_MOV)
Then
Begin
If (Pai386(hp2)^.Size = S_B) And
{$IfDef RegInfo}
RegsEquivalent(Reg8toReg32(TRegister(Pai386(hp2)^.op2)),
TRegister(Pai386(hp3)^.op2), RegInfo)
{$Else RegInfo}
(Reg8toReg32(TRegister(Pai386(hp2)^.op2)) =
TRegister(Pai386(hp3)^.op2))
{$EndIf RegInfo}
Then
Begin
Pai386(hp2)^._operator := A_MOVZX;
Pai386(hp2)^.op2 := Pai386(hp3)^.op2;
Pai386(hp2)^.Size := S_BL;
Inc(Found);
TmpResult := True;
End
Else
Begin
TmpResult := False;
If (Found > 0) Then
Found := PPaiProp(Pai(p)^.fileinfo.line)^.Regs[Reg].NrOfMods
End
End
Else
Begin
If (Pai386(hp3)^.Size = S_B) And
{$IfDef RegInfo}
RegsEquivalent(TRegister(Pai386(hp2)^.op2),
Reg8toReg32(TRegister(Pai386(hp3)^.op2)),
RegInfo)
{$Else RegInfo}
(Reg8toReg32(TRegister(Pai386(hp3)^.op2)) =
TRegister(Pai386(hp2)^.op2))
{$EndIf RegInfo}
Then
Begin
TmpResult := True;
Inc(Found)
End
Else
Begin
TmpResult := False;
If (Found > 0) Then
Found := PPaiProp(Pai(p)^.fileinfo.line)^.Regs[Reg].NrOfMods
End
End
Else *)
Begin
TmpResult := False;
If (found > 0) then
{this is correct because we only need to turn off the CanBeRemoved flag
when an instruction has already been processed by CheckSequence
(otherwise CanBeRemoved can't be true and thus can't have to be turned off).
@ -144,27 +207,68 @@ Begin {CheckSequence}
and that it was equal (otherwise CheckSequence would have returned false
and the instruction wouldn't have been removed). If this "If found > 0"
check is left out, incorrect optimizations are performed.}
Found := PPaiProp(Pai(p)^.fileinfo.line)^.Regs[Reg].NrOfMods
End
End
Else CheckSequence := True;
Found := PPaiProp(Pai(p)^.fileinfo.line)^.Regs[Reg].NrOfMods
End
End
Else TmpResult := True;
If TmpResult And
(Found > HighFound)
Then
Begin
HighFound := Found;
HighRegInfo := RegInfo;
End;
If (RegCounter = Reg) Then
Begin
OrgRegFound := Found;
OrgRegResult := TmpResult;
OrgRegInfo := RegInfo
End;
Repeat
Inc(RegCounter);
Until (RegCounter > R_EDI) or
((PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].Typ = Con_Ref) And
((Regcounter = Reg) Or
Not(PaiInSequence(p, PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter]))));
Until (RegCounter > R_EDI);
If (HighFound > 0) And
(Not(OrgRegResult) Or
(HighFound > OrgRegFound))
Then
Begin
CheckSequence := True;
RegInfo := HighRegInfo;
Found := HighFound
End
Else
Begin
CheckSequence := OrgRegResult;
Found := OrgRegFound;
RegInfo := OrgRegInfo;
End;
End; {CheckSequence}
Procedure DoCSE(First, Last: Pai);
Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
{marks the instructions that can be removed by RemoveInstructs. They're not
removed immediately because sometimes an instruction needs to be checked in
two different sequences}
Var Cnt, Cnt2: Longint;
p, hp1, hp2: Pai;
hp3, hp4: Pai;
{$ifdef csdebug}
hp5: pai;
{$endif csdebug}
RegInfo: TRegInfo;
RegCounter: TRegister;
TmpState: Word;
Begin
p := First;
If (p^.typ in SkipInstr) Then
If (p^.typ in (SkipInstr+ait_marker)) Then
GetNextInstruction(p, p);
First := p;
While Assigned(p) Do
Begin
Case p^.typ Of
ait_label, ait_labeled_instruction:;
ait_instruction:
Begin
Case Pai386(p)^._operator Of
@ -183,11 +287,15 @@ Begin
Begin {destination is always a register in this case}
With PPaiProp(p^.fileinfo.line)^.Regs[Reg32(Tregister(Pai386(p)^.op2))] Do
Begin
If GetLastInstruction(p, hp1) And
{ hp1 := p;
While GetLastInstruction(hp1, hp1) And
PPaiProp(hp1^.fileinfo.line)^.CanBeRemoved Do;}
GetLastInstruction(p, hp1);
If Assigned(hp1) And
(PPaiProp(hp1^.fileinfo.line)^.
Regs[Reg32(TRegister(Pai386(p)^.op2))].typ = con_ref) Then
{so we don't try to check a sequence when the register only contains a constant}
If CheckSequence(p, TRegister(Pai386(p)^.op2), Cnt) And
If CheckSequence(p, TRegister(Pai386(p)^.op2), Cnt, RegInfo) And
(Cnt > 0)
Then
Begin
@ -212,12 +320,152 @@ Begin
While Cnt2 <= Cnt Do
Begin
If (hp1 = nil) And
Not(RegInInstruction(Tregister(Pai386(hp2)^.op2), p))
Not(RegInInstruction(Tregister(Pai386(hp2)^.op2), p) Or
RegInInstruction(Reg32(Tregister(Pai386(hp2)^.op2)), p))
Then hp1 := p;
{$ifndef noremove}
PPaiProp(p^.fileinfo.line)^.CanBeRemoved := True;
{$endif noremove}
Inc(Cnt2);
GetNextInstruction(p, p);
End;
hp3 := New(Pai_Marker,Init(NoPropInfoStart));
InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
{imagine the following code:
normal wrong optimized
movl 8(%ebp), %eax movl 8(%ebp), %eax
movl (%eax), %eax movl (%eax), %eax
cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax
jne l1 jne l1
movl 8(%ebp), %eax
movl (%eax), %edi movl %eax, %edi
movl %edi, -4(%ebp) movl %edi, -4(%ebp)
movl 8(%ebp), %eax
pushl 70(%eax) pushl 70(%eax)
The error is that at the moment that the last instruction is executed,
%eax doesn't contain 8(%ebp) anymore. Solution: the contents of registers
that are completely removed from a sequence, have to be changed to their
contents from before the sequence.}
{hp4 is used to get the contents of the registers before the sequence}
GetLastInstruction(hp2, hp4);
{If some registers were different in the old and the new sequence, move
the contents of those old registers to the new ones}
{$IfDef CSDebug}
For RegCounter := R_EAX To R_EDI Do
If (RegCounter in RegInfo.RegsLoadedForRef) Then
Begin
hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
att_reg2str[RegInfo.SubstRegs[RegCounter]])));
InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
End;
{$EndIf CSDebug}
For RegCounter := R_EAX To R_EDI Do
Begin
If (RegInfo.SubstRegs[RegCounter] <> R_NO) Then
If Not(RegCounter In RegInfo.RegsLoadedForRef) And
{new reg old reg}
(RegInfo.SubstRegs[RegCounter] <> RegCounter) Then
Begin
hp3 := New(Pai386,Op_Reg_Reg(A_MOV, S_L,
{old reg new reg}
RegInfo.SubstRegs[RegCounter], RegCounter));
hp3^.fileinfo := hp2^.fileinfo;
hp3^.fileinfo.line := PPaiProp(hp2^.fileinfo.line)^.LineSave;
InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
End
Else
If (RegCounter In RegInfo.RegsLoadedForRef) Then
{change the contents of this register to the its contents before the
sequence (for all instructions in and after the sequence, until the register
is reloaded)}
Begin
{load Cnt2 with the total number of instructions of this sequence}
Cnt2 := PPaiProp(hp4^.fileinfo.line)^.
Regs[RegInfo.SubstRegs[RegCounter]].NrOfMods;
{sometimes, a register can not be removed from a sequence, because it's
still used afterwards:
movl -8(%ebp), %eax movl -8(%ebp), %eax
movl 70(%eax), %eax movl 70(%eax), %eax
cmpl 74(%eax), %eax cmpl 74(%eax), %eax
jne l1 can't be changed to jne l1
movl -8(%ebp), %eax
movl 70(%eax), %edi movl %eax, %edi
boundl R_282, %edi boundl R_282, %edi
pushl 70(%eax) pushl 70(%eax)
because eax now contains the wrong value when 70(%eax) is pushed}
hp3 := hp2;
For Cnt := 1 to Pred(Cnt2) Do
GetNextInstruction(hp3, hp3);
TmpState := PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter].State;
GetNextInstruction(hp3, hp3);
If (TmpState <> PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter].State) Or
Not(RegCounter in PPaiProp(hp3^.fileinfo.line)^.UsedRegs) Then
Begin
{$ifdef csdebug}
Writeln('Cnt2: ',Cnt2);
hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
{$endif csdebug}
hp3 := hp2;
{first change the contents of the register inside the sequence}
For Cnt := 1 to Cnt2 Do
Begin
{save the state of the last pai object of the sequence for later use}
TmpState := PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter].State;
{$ifdef csdebug}
hp5 := new(pai_asm_comment,init(strpnew('State for '+att_reg2str[Regcounter]+': '
+tostr(tmpstate))));
InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
{$endif csdebug}
PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter] :=
PPaiProp(hp4^.fileinfo.line)^.Regs[RegCounter];
GetNextInstruction(hp3, hp3);
End;
{here, hp3 = p = Pai object right after the sequence, TmpState = state of
RegCounter at the last Pai object of the sequence}
GetLastInstruction(hp3, hp3);
While GetNextInstruction(hp3, hp3) And
(PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter].State
= TmpState) Do
{$ifdef csdebug}
begin
hp5 := new(pai_asm_comment,init(strpnew('State for '+att_reg2str[Regcounter]+': '+
tostr(PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter].State))));
InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
{$endif csdebug}
PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter] :=
PPaiProp(hp4^.fileinfo.line)^.Regs[RegCounter];
{$ifdef csdebug}
end;
{$endif csdebug}
End
Else
Begin
{$ifdef csdebug}
Writeln('Got there for ',att_Reg2Str[RegCounter]);
{$endif csdebug}
hp3 := hp2;
For Cnt := 1 to Cnt2 Do
Begin
If RegModifiedByInstruction(RegCounter, hp3)
Then PPaiProp(hp3^.fileinfo.line)^.CanBeRemoved := False;
GetNextInstruction(hp3, hp3);
End;
End;
{$ifdef csdebug}
hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
{$endif csdebug}
End;
End;
hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
If hp1 <> nil Then p := hp1;
Continue;
End
@ -229,7 +477,8 @@ Begin
Cnt2 := 1;
While Cnt2 <= Cnt Do
Begin
If RegInInstruction(Tregister(Pai386(hp2)^.op2), p)
If RegInInstruction(Tregister(Pai386(hp2)^.op2), p) Or
RegInInstruction(Reg32(Tregister(Pai386(hp2)^.op2)), p)
Then PPaiProp(p^.fileinfo.line)^.CanBeRemoved := False;
Inc(Cnt2);
GetNextInstruction(p, p);
@ -284,34 +533,38 @@ Var p, hp1: Pai;
InstrCnt: Longint;
Begin
p := First;
If (p^.typ in SkipInstr) Then
If (p^.typ in (SkipInstr + ait_marker)) Then
GetNextInstruction(p, p);
InstrCnt := 1;
While Assigned(p) Do
If PPaiProp(p^.fileinfo.line)^.CanBeRemoved
Then
Begin
Begin
{$ifndef noinstremove}
If PPaiProp(p^.fileinfo.line)^.CanBeRemoved
Then
Begin
{$IfDef TP}
Dispose(PPaiProp(p^.fileinfo.line));
Dispose(PPaiProp(p^.fileinfo.line));
{$EndIf}
GetNextInstruction(p, hp1);
AsmL^.Remove(p);
Dispose(p, Done);
p := hp1;
Inc(InstrCnt)
End
Else
Begin
GetNextInstruction(p, hp1);
AsmL^.Remove(p);
Dispose(p, Done);
p := hp1;
Inc(InstrCnt);
End
Else
{$endif noinstremove}
Begin
{$IfDef TP}
TmpLine := PPaiProp(p^.fileinfo.line)^.linesave;
Dispose(PPaiProp(p^.fileinfo.line));
p^.fileinfo.line := TmpLine;
TmpLine := PPaiProp(p^.fileinfo.line)^.linesave;
Dispose(PPaiProp(p^.fileinfo.line));
p^.fileinfo.line := TmpLine;
{$Else TP}
p^.fileinfo.line := PPaiProp(p^.fileinfo.line)^.linesave;
p^.fileinfo.line := PPaiProp(p^.fileinfo.line)^.linesave;
{$EndIf TP}
GetNextInstruction(p, p);
Inc(InstrCnt)
End;
GetNextInstruction(p, p);
Inc(InstrCnt);
End;
End;
{$IfNDef TP}
FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
{$EndIf TP}
@ -319,7 +572,7 @@ End;
Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
Begin
DoCSE(First, Last);
DoCSE(AsmL, First, Last);
RemoveInstructs(AsmL, First, Last);
End;
@ -327,7 +580,10 @@ End.
{
$Log$
Revision 1.8 1998-09-21 08:45:09 pierre
Revision 1.9 1998-10-01 20:21:49 jonas
* inter-register CSE, still requires some tweaks (peepholeoptpass2, better RegAlloc)
Revision 1.8 1998/09/21 08:45:09 pierre
+ added vmt_offset in tobjectdef.write for fututre use
(first steps to have objects without vmt if no virtual !!)
+ added fpu_used field for tabstractprocdef :

File diff suppressed because it is too large Load Diff