123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146 |
- {
- $Id$
- Copyright (c) 1998-2000 by Jonas Maebe, member of the Free Pascal
- development team
- This unit contains the common subexpression elimination procedure.
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- Unit CSOpt386;
- {$ifdef newOptimizations}
- {$define foropt}
- {$define replacereg}
- {$define arithopt}
- {$define foldarithops}
- {$endif newOptimizations}
- Interface
- Uses aasm;
- {Procedure CSOpt386(First, Last: Pai);}
- Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
- Implementation
- Uses
- CObjects, verbose, hcodegen, globals,cpubase,cpuasm,DAOpt386, tgeni386;
- {
- 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;
- PrevNonRemovablePai: Pai;
- {Cnt,} OldNrOfMods: Longint;
- OrgRegInfo, HighRegInfo: TRegInfo;
- HighFound, OrgRegFound: Byte;
- RegCounter: TRegister;
- OrgRegResult: Boolean;
- TmpResult: Boolean;
- {TmpState: Byte;}
- Begin {CheckSequence}
- Reg := Reg32(Reg);
- TmpResult := False;
- FillChar(OrgRegInfo, SizeOf(OrgRegInfo), 0);
- OrgRegFound := 0;
- HighFound := 0;
- OrgRegResult := False;
- RegCounter := R_EAX;
- GetLastInstruction(p, PrevNonRemovablePai);
- While (RegCounter <= R_EDI) And
- (PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].Typ <> Con_Ref) Do
- Inc(RegCounter);
- While (RegCounter <= R_EDI) Do
- Begin
- FillChar(RegInfo, SizeOf(RegInfo), 0);
- RegInfo.NewRegsEncountered := [procinfo^.FramePointer, R_ESP];
- RegInfo.OldRegsEncountered := RegInfo.NewRegsEncountered;
- RegInfo.New2OldReg[procinfo^.FramePointer] := procinfo^.FramePointer;
- RegInfo.New2OldReg[R_ESP] := R_ESP;
- Found := 0;
- hp2 := PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].StartMod;
- If (PrevNonRemovablePai <> PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].StartMod)
- Then OldNrOfMods := PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].NrOfMods
- Else OldNrOfMods := 1;
- 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) or
- { the following is to avoid problems with rangecheck code (see testcse2) }
- (assigned(hp3) and
- ((reg in regInfo.regsLoadedForRef) and
- (reg in PPaiProp(hp3^.optInfo)^.usedRegs) and
- not regLoadedWithNewValue(reg,false,hp3))) then
- 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).
- If it has already been processed by CheckSequence and flagged to be
- removed, it means that it has been checked against a previous sequence
- 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)^.OptInfo)^.Regs[Reg].NrOfMods
- 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^.OptInfo)^.Regs[RegCounter].Typ = Con_Ref) {And
- ((Regcounter = Reg) Or
- Not(PaiInSequence(p, PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter]))) }
- );
- End;
- If (HighFound > 0) And
- (Not(OrgRegResult) Or
- (HighFound > OrgRegFound))
- Then
- Begin
- {$ifndef fpc}
- TmpResult := True;
- {$else fpc}
- CheckSequence := True;
- {$endif fpc}
- RegInfo := HighRegInfo;
- Found := HighFound
- End
- Else
- Begin
- {$ifndef fpc}
- TmpResult := OrgRegResult;
- {$else fpc}
- CheckSequence := OrgRegResult;
- {$endif fpc}
- Found := OrgRegFound;
- RegInfo := OrgRegInfo;
- End;
- {$ifndef fpc}
- CheckSequence := TmpResult;
- {$endif fpc}
- End; {CheckSequence}
- Procedure SetAlignReg(p: Pai);
- Const alignSearch = 12;
- var regsUsable: TRegSet;
- prevInstrCount, nextInstrCount: Longint;
- prevState, nextWState,nextRState: Array[R_EAX..R_EDI] of byte;
- regCounter, lastRemoved: TRegister;
- prev, next: Pai;
- {$ifdef alignregdebug}
- temp: Pai;
- {$endif alignregdebug}
- begin
- regsUsable := [R_EAX,R_ECX,R_EDX,R_EBX,{R_ESP,R_EBP,}R_ESI,R_EDI];
- for regCounter := R_EAX to R_EDI do
- begin
- prevState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
- nextWState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
- nextRState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].rState;
- end;
- getLastInstruction(p,prev);
- getNextInstruction(p,next);
- lastRemoved := pai_align(p)^.reg;
- nextInstrCount := 0;
- prevInstrCount := 0;
- while ((assigned(prev) and
- assigned(prev^.optInfo) and
- (prevInstrCount < alignSearch)) or
- (assigned(next) and
- assigned(next^.optInfo) and
- (nextInstrCount < alignSearch))) And
- (regsUsable <> []) do
- begin
- {$ifdef alignregdebug}
- if assigned(prev) then
- begin
- temp := new(pai_asm_comment,init(strpnew('got here')));
- temp^.next := prev^.next;
- temp^.previous := prev;
- prev^.next := temp;
- if assigned(temp^.next) then
- temp^.next^.previous := temp;
- end;
- {$endif alignregdebug}
- if assigned(prev) and assigned(prev^.optinfo) and
- (prevInstrCount < alignSearch) then
- begin
- if (prev^.typ = ait_instruction) And
- (insProp[PaiCpu(prev)^.opcode].ch[1] <> Ch_ALL) and
- (PaiCpu(prev)^.opcode <> A_JMP) then
- begin
- inc(prevInstrCount);
- for regCounter := R_EAX to R_EDI do
- begin
- if (regCounter in regsUsable) And
- (PPaiProp(prev^.optInfo)^.Regs[regCounter].wState <>
- prevState[regCounter]) then
- begin
- lastRemoved := regCounter;
- exclude(regsUsable,regCounter);
- {$ifdef alignregdebug}
- temp := new(pai_asm_comment,init(strpnew(
- att_reg2str[regCounter]+' removed')));
- temp^.next := prev^.next;
- temp^.previous := prev;
- prev^.next := temp;
- if assigned(temp^.next) then
- temp^.next^.previous := temp;
- if regsUsable = [] then
- begin
- temp := new(pai_asm_comment,init(strpnew(
- 'regsUsable empty here')));
- temp^.next := prev^.next;
- temp^.previous := prev;
- prev^.next := temp;
- if assigned(temp^.next) then
- temp^.next^.previous := temp;
- end;
- {$endif alignregdebug}
- end;
- prevState[regCounter] :=
- PPaiProp(prev^.optInfo)^.Regs[regCounter].wState;
- end;
- getLastInstruction(prev,prev);
- end
- else
- If GetLastInstruction(prev,prev) and
- assigned(prev^.optinfo) then
- for regCounter := R_EAX to R_EDI do
- prevState[regCounter] :=
- PPaiProp(prev^.optInfo)^.Regs[regCounter].wState
- end;
- if assigned(next) and assigned(next^.optInfo) and
- (nextInstrCount < alignSearch) then
- begin
- if (next^.typ = ait_instruction) and
- (insProp[PaiCpu(next)^.opcode].ch[1] <> Ch_ALL) and
- (PaiCpu(next)^.opcode <> A_JMP) then
- begin
- inc(nextInstrCount);
- for regCounter := R_EAX to R_EDI do
- begin
- if (regCounter in regsUsable) And
- ((PPaiProp(next^.optInfo)^.Regs[regCounter].wState <>
- nextWState[regCounter]) or
- (PPaiProp(next^.optInfo)^.Regs[regCounter].rState <>
- nextRState[regCounter])) Then
- begin
- lastRemoved := regCounter;
- exclude(regsUsable,regCounter);
- {$ifdef alignregdebug}
- temp := new(pai_asm_comment,init(strpnew(
- att_reg2str[regCounter]+' removed')));
- temp^.next := next^.next;
- temp^.previous := next;
- next^.next := temp;
- if assigned(temp^.next) then
- temp^.next^.previous := temp;
- if regsUsable = [] then
- begin
- temp := new(pai_asm_comment,init(strpnew(
- 'regsUsable empty here')));
- temp^.next := next^.next;
- temp^.previous := next;
- next^.next := temp;
- if assigned(temp^.next) then
- temp^.next^.previous := temp;
- end;
- {$endif alignregdebug}
- end;
- nextWState[regCounter] :=
- PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
- nextRState[regCounter] :=
- PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
- end
- end
- else
- for regCounter := R_EAX to R_EDI do
- begin
- nextWState[regCounter] :=
- PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
- nextRState[regCounter] :=
- PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
- end;
- getNextInstruction(next,next);
- end;
- end;
- if regsUsable <> [] then
- for regCounter := R_EAX to R_EDI do
- if regCounter in regsUsable then
- begin
- lastRemoved := regCounter;
- break
- end;
- {$ifdef alignregdebug}
- next := new(pai_asm_comment,init(strpnew(att_reg2str[lastRemoved]+
- ' chosen as alignment register')));
- next^.next := p^.next;
- next^.previous := p;
- p^.next := next;
- if assigned(next^.next) then
- next^.next^.previous := next;
- {$endif alignregdebug}
- pai_align(p)^.reg := lastRemoved;
- End;
- Procedure RestoreRegContentsTo(reg: TRegister; const c: TContent; p, endP: pai);
- var
- {$ifdef replaceregdebug}
- hp: pai;
- {$endif replaceregdebug}
- tmpState: byte;
- begin
- {$ifdef replaceregdebug}
- hp := new(pai_asm_comment,init(strpnew(
- 'restored '+att_reg2str[reg]+' with data from here...')));
- hp^.next := p;
- hp^.previous := p^.previous;
- p^.previous := hp;
- if assigned(hp^.previous) then
- hp^.previous^.next := hp;
- {$endif replaceregdebug}
- PPaiProp(p^.optInfo)^.Regs[reg] := c;
- While (p <> endP) Do
- Begin
- PPaiProp(p^.optInfo)^.Regs[reg] := c;
- getNextInstruction(p,p);
- end;
- tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
- repeat
- PPaiProp(p^.optInfo)^.Regs[reg] := c;
- until not getNextInstruction(p,p) or
- (PPaiProp(p^.optInfo)^.Regs[reg].wState <> tmpState);
- {$ifdef replaceregdebug}
- if assigned(p) then
- begin
- hp := new(pai_asm_comment,init(strpnew(
- 'restored '+att_reg2str[reg]+' till here...')));
- hp^.next := p;
- hp^.previous := p^.previous;
- p^.previous := hp;
- if assigned(hp^.previous) then
- hp^.previous^.next := hp;
- end;
- {$endif replaceregdebug}
- end;
- {$ifdef replacereg}
- function FindRegDealloc(reg: tregister; p: pai): boolean;
- { assumes reg is a 32bit register }
- begin
- findregdealloc := false;
- while assigned(p^.previous) and
- ((Pai(p^.previous)^.typ in (skipinstr+[ait_align])) or
- ((Pai(p^.previous)^.typ = ait_label) and
- not(Pai_Label(p^.previous)^.l^.is_used))) do
- begin
- p := pai(p^.previous);
- if (p^.typ = ait_regalloc) and
- (pairegalloc(p)^.reg = reg) then
- begin
- findregdealloc := not(pairegalloc(p)^.allocation);
- break;
- end;
- end
- end;
- Procedure ClearRegContentsFrom(reg: TRegister; p, endP: pai);
- { first clears the contents of reg from p till endP. Then the contents are }
- { cleared until the first instruction that changes reg }
- var
- {$ifdef replaceregdebug}
- hp: pai;
- {$endif replaceregdebug}
- tmpState: byte;
- begin
- PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
- While (p <> endP) Do
- Begin
- PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
- getNextInstruction(p,p);
- end;
- tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
- repeat
- PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
- until not getNextInstruction(p,p) or
- (PPaiProp(p^.optInfo)^.Regs[reg].wState <> tmpState);
- {$ifdef replaceregdebug}
- if assigned(p) then
- begin
- hp := new(pai_asm_comment,init(strpnew(
- 'cleared '+att_reg2str[reg]+' till here...')));
- hp^.next := p;
- hp^.previous := p^.previous;
- p^.previous := hp;
- if assigned(hp^.previous) then
- hp^.previous^.next := hp;
- end;
- {$endif replaceregdebug}
- end;
- function NoHardCodedRegs(p: paicpu; orgReg, newReg: tRegister): boolean;
- var chCount: byte;
- begin
- case p^.opcode of
- A_IMUL: noHardCodedRegs := p^.ops <> 1;
- A_SHL,A_SHR,A_SHLD,A_SHRD: noHardCodedRegs :=
- (p^.oper[0].typ <> top_reg) or
- ((orgReg <> R_ECX) and (newReg <> R_ECX));
- else
- begin
- NoHardCodedRegs := true;
- with InsProp[p^.opcode] do
- for chCount := 1 to MaxCh do
- if Ch[chCount] in ([Ch_REAX..Ch_MEDI,Ch_WMemEDI,Ch_All]-[Ch_RESP,Ch_WESP,Ch_RWESP]) then
- begin
- NoHardCodedRegs := false;
- break
- end;
- end;
- end;
- end;
- Procedure ChangeReg(var Reg: TRegister; orgReg, newReg: TRegister);
- begin
- if reg = newReg then
- reg := orgReg
- else if reg = regtoreg8(newReg) then
- reg := regtoreg8(orgReg)
- else if reg = regtoreg16(newReg) then
- reg := regtoreg16(orgReg);
- end;
- procedure changeOp(var o: toper; orgReg, newReg: tregister);
- begin
- case o.typ of
- top_reg: changeReg(o.reg,orgReg,newReg);
- top_ref:
- begin
- changeReg(o.ref^.base,orgReg,newReg);
- changeReg(o.ref^.index,orgReg,newReg);
- end;
- end;
- end;
- Procedure DoReplaceReg(orgReg,newReg: tregister; hp: paicpu);
- var opCount: byte;
- begin
- for opCount := 0 to 2 do
- changeOp(hp^.oper[opCount],orgReg,newReg)
- end;
- function RegSizesOK(oldReg,newReg: TRegister; p: paicpu): boolean;
- { oldreg and newreg must be 32bit components }
- var opCount: byte;
- begin
- RegSizesOK := true;
- { if only one of them is a general purpose register ... }
- if (IsGP32reg(oldReg) xor IsGP32Reg(newReg)) then
- begin
- for opCount := 0 to 2 do
- if (p^.oper[opCount].typ = top_reg) and
- (p^.oper[opCount].reg in [R_AL..R_DH]) then
- begin
- RegSizesOK := false;
- break
- end
- end;
- end;
- procedure DoReplaceReadReg(orgReg,newReg: tregister; p: paicpu);
- var opCount: byte;
- begin
- { handle special case }
- case p^.opcode of
- A_IMUL:
- begin
- case p^.ops of
- 1: internalerror(1301001);
- 2,3:
- begin
- changeOp(p^.oper[0],orgReg,newReg);
- if p^.ops = 3 then
- changeOp(p^.oper[1],orgReg,newReg);
- end;
- end;
- end;
- A_DIV,A_IDIV,A_MUL: internalerror(1301002);
- else
- begin
- for opCount := 0 to 2 do
- if p^.oper[opCount].typ = top_ref then
- changeOp(p^.oper[opCount],orgReg,newReg);
- for opCount := 1 to MaxCh do
- case InsProp[p^.opcode].Ch[opCount] of
- Ch_ROp1:
- if p^.oper[0].typ = top_reg then
- ChangeReg(p^.oper[0].reg,orgReg,newReg);
- Ch_ROp2:
- if p^.oper[1].typ = top_reg then
- ChangeReg(p^.oper[1].reg,orgReg,newReg);
- Ch_ROp3:
- if p^.oper[2].typ = top_reg then
- ChangeReg(p^.oper[2].reg,orgReg,newReg);
- end;
- end;
- end;
- end;
- function ReplaceReg(asmL: PaasmOutput; orgReg, newReg: TRegister; p: pai;
- const c: TContent; orgRegCanBeModified: Boolean;
- var returnEndP: pai): Boolean;
- { Tries to replace orgreg with newreg in all instructions coming after p }
- { until orgreg gets loaded with a new value. Returns true if successful, }
- { false otherwise. If successful, the contents of newReg are set to c, }
- { which should hold the contents of newReg before the current sequence }
- { started }
- { if the function returns true, returnEndP holds the last instruction }
- { where newReg was replaced by orgReg }
- var endP, hp: Pai;
- removeLast, sequenceEnd, tmpResult, newRegModified, orgRegRead: Boolean;
- function storeBack(p1: pai): boolean;
- { returns true if p1 contains an instruction that stores the contents }
- { of newReg back to orgReg }
- begin
- storeBack :=
- (p1^.typ = ait_instruction) and
- (paicpu(p1)^.opcode = A_MOV) and
- (paicpu(p1)^.oper[0].typ = top_reg) and
- (paicpu(p1)^.oper[0].reg = newReg) and
- (paicpu(p1)^.oper[1].typ = top_reg) and
- (paicpu(p1)^.oper[1].reg = orgReg);
- end;
- begin
- ReplaceReg := false;
- tmpResult := true;
- sequenceEnd := false;
- newRegModified := false;
- orgRegRead := false;
- removeLast := false;
- endP := p;
- while tmpResult and not sequenceEnd do
- begin
- tmpResult :=
- getNextInstruction(endP,endP) and
- (endP^.typ = ait_instruction);
- if tmpresult and not assigned(endP^.optInfo) then
- begin
- { hp := new(pai_asm_comment,init(strpnew('next no optinfo')));
- hp^.next := endp;
- hp^.previous := endp^.previous;
- endp^.previous := hp;
- if assigned(hp^.previous) then
- hp^.previous^.next := hp;}
- exit;
- end;
- If tmpResult and
- { don't take into account instructions that will be removed }
- Not (PPaiProp(endP^.optInfo)^.canBeRemoved) then
- begin
- { if the newReg gets stored back to the oldReg, we can change }
- { "mov %oldReg,%newReg; <operations on %newReg>; mov %newReg, }
- { %oldReg" to "<operations on %oldReg>" }
- removeLast := storeBack(endP);
- sequenceEnd :=
- { no support for (i)div, mul and imul with hardcoded operands }
- (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
- { if newReg gets loaded with a new value, we can stop }
- { replacing newReg with oldReg here (possibly keeping }
- { the original contents of oldReg so we still know them }
- { afterwards) }
- RegLoadedWithNewValue(newReg,true,paicpu(endP)) or
- { we can also stop if we reached the end of the use of }
- { newReg's current contents }
- (GetNextInstruction(endp,hp) and
- FindRegDealloc(newReg,hp)));
- { to be able to remove the first and last instruction of }
- { movl %reg1, %reg2 }
- { <operations on %reg2> (replacing reg2 with reg1 here) }
- { movl %reg2, %reg1 }
- { %reg2 must not be use afterwards (it can be as the }
- { result of a peepholeoptimization) }
- removeLast := removeLast and sequenceEnd;
- newRegModified :=
- newRegModified or
- (not(regLoadedWithNewValue(newReg,true,paicpu(endP))) and
- RegModifiedByInstruction(newReg,endP));
- orgRegRead := newRegModified and RegReadByInstruction(orgReg,endP);
- sequenceEnd := SequenceEnd and
- (removeLast or
- { since newReg will be replaced by orgReg, we can't allow that newReg }
- { gets modified if orgReg is still read afterwards (since after }
- { replacing, this would mean that orgReg first gets modified and then }
- { gets read in the assumption it still contains the unmodified value) }
- not(newRegModified and orgRegRead)) (* and
- { since newReg will be replaced by orgReg, we can't allow that newReg }
- { gets modified if orgRegCanBeModified = false }
- (orgRegCanBeModified or not(newRegModified)) *);
- tmpResult :=
- not(removeLast) and
- not(newRegModified and orgRegRead) and
- (* (orgRegCanBeModified or not(newRegModified)) and *)
- (endP^.typ = ait_instruction) and
- not(paicpu(endP)^.is_jmp) and
- NoHardCodedRegs(paicpu(endP),orgReg,newReg) and
- RegSizesOk(orgReg,newReg,paicpu(endP)) and
- not RegModifiedByInstruction(orgReg,endP);
- end;
- end;
- sequenceEnd := sequenceEnd and
- (removeLast or
- (orgRegCanBeModified or not(newRegModified))) and
- (not(assigned(endp)) or
- not(endp^.typ = ait_instruction) or
- (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
- RegSizesOk(orgReg,newReg,paicpu(endP)) and
- not(newRegModified and
- (orgReg in PPaiProp(endP^.optInfo)^.usedRegs) and
- not(RegLoadedWithNewValue(orgReg,true,paicpu(endP))))));
- if SequenceEnd then
- begin
- {$ifdef replaceregdebug}
- hp := new(pai_asm_comment,init(strpnew(
- 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
- ' from here...')));
- hp^.next := p;
- hp^.previous := p^.previous;
- p^.previous := hp;
- if assigned(hp^.previous) then
- hp^.previous^.next := hp;
- hp := new(pai_asm_comment,init(strpnew(
- 'replaced '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
- ' till here')));
- hp^.next := endp^.next;
- hp^.previous := endp;
- endp^.next := hp;
- if assigned(hp^.next) then
- hp^.next^.previous := hp;
- {$endif replaceregdebug}
- replaceReg := true;
- returnEndP := endP;
- getNextInstruction(p,hp);
- while hp <> endP do
- begin
- if not(PPaiProp(hp^.optInfo)^.canBeRemoved) and
- (hp^.typ = ait_instruction) then
- DoReplaceReg(orgReg,newReg,paicpu(hp));
- GetNextInstruction(hp,hp)
- end;
- if assigned(endp) and (endp^.typ = ait_instruction) then
- DoReplaceReadReg(orgReg,newReg,paicpu(endP));
- { the replacing stops either at the moment that }
- { a) the newreg gets loaded with a new value (one not depending on the }
- { current value of newreg) }
- { b) newreg is completely replaced in this sequence and it's current value }
- { isn't used anymore }
- { In case b, the newreg was completely replaced by oldreg, so it's contents }
- { are unchanged compared the start of this sequence, so restore them }
- If removeLast or
- RegLoadedWithNewValue(newReg,true,endP) then
- GetLastInstruction(endP,hp)
- else hp := endP;
- if removeLast or
- (p <> endp) or
- not RegLoadedWithNewValue(newReg,true,endP) then
- RestoreRegContentsTo(newReg, c ,p, hp);
- { In both case a and b, it is possible that the new register was modified }
- { (e.g. an add/sub), so if it was replaced by oldreg in that instruction, }
- { oldreg's contents have been changed. To take this into account, we simply }
- { set the contents of orgreg to "unknown" after this sequence }
- if newRegModified then
- ClearRegContentsFrom(orgReg,p,hp);
- if removeLast then
- ppaiprop(endP^.optinfo)^.canBeRemoved := true;
- allocRegBetween(asml,orgReg,p,endP);
- end
- {$ifdef replaceregdebug}
- else
- begin
- hp := new(pai_asm_comment,init(strpnew(
- 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
- ' from here...')));
- hp^.previous := p^.previous;
- hp^.next := p;
- p^.previous := hp;
- if assigned(hp^.previous) then
- hp^.previous^.next := hp;
- hp := new(pai_asm_comment,init(strpnew(
- 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
- ' failed here')));
- hp^.next := endp^.next;
- hp^.previous := endp;
- endp^.next := hp;
- if assigned(hp^.next) then
- hp^.next^.previous := hp;
- end;
- {$endif replaceregdebug}
- End;
- {$endif replacereg}
- {$ifdef arithopt}
- Function FindRegWithConst(p: Pai; size: topsize; l: longint; Var Res: TRegister): Boolean;
- {Finds a register which contains the constant l}
- Var Counter: TRegister;
- {$ifdef testing}
- hp: pai;
- {$endif testing}
- tmpresult: boolean;
- Begin
- Counter := R_NO;
- repeat
- inc(counter);
- tmpresult := (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) and
- (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.opsize = size) and
- (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].typ = top_const) and
- (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].val = l);
- {$ifdef testing}
- if (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) then
- begin
- hp := new(pai_asm_comment,init(strpnew(
- 'checking const load of '+tostr(l)+' here...')));
- hp^.next := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod;
- hp^.previous := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous;
- PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous := hp;
- if assigned(hp^.previous) then
- hp^.previous^.next := hp;
- end;
- {$endif testing}
- until tmpresult or (Counter = R_EDI);
- res := counter;
- FindRegWithConst := tmpResult;
- End;
- {$endif arithopt}
- 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 replacereg}
- hp5 : pai;
- {$else}
- {$ifdef csdebug}
- hp5 : pai;
- {$endif}
- {$endif}
- RegInfo: TRegInfo;
- RegCounter: TRegister;
- TmpState: Byte;
- Begin
- p := First;
- SkipHead(p);
- First := p;
- While (p <> Last) Do
- Begin
- Case p^.typ Of
- ait_align:
- if not(pai_align(p)^.use_op) then
- SetAlignReg(p);
- ait_instruction:
- Begin
- Case Paicpu(p)^.opcode Of
- A_CLD: If GetLastInstruction(p, hp1) And
- (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
- PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
- A_MOV, A_MOVZX, A_MOVSX:
- Begin
- Case Paicpu(p)^.oper[0].typ Of
- Top_Ref:
- Begin {destination is always a register in this case}
- With PPaiProp(p^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
- Begin
- If (p = StartMod) And
- GetLastInstruction (p, hp1) And
- (hp1^.typ <> ait_marker) Then
- {so we don't try to check a sequence when p is the first instruction of the block}
- begin
- {$ifdef csdebug}
- hp5 := new(pai_asm_comment,init(strpnew(
- 'cse checking '+att_reg2str[Reg32(Paicpu(p)^.oper[1].reg)])));
- insertLLItem(asml,p,p^.next,hp5);
- {$endif csdebug}
- If CheckSequence(p, Paicpu(p)^.oper[1].reg, Cnt, RegInfo) And
- (Cnt > 0) Then
- Begin
- hp1 := nil;
- { although it's perfectly ok to remove an instruction which doesn't contain }
- { the register that we've just checked (CheckSequence takes care of that), }
- { the sequence containing this other register should also be completely }
- { checked and removed, otherwise we may get situations like this: }
- { }
- { movl 12(%ebp), %edx movl 12(%ebp), %edx }
- { movl 16(%ebp), %eax movl 16(%ebp), %eax }
- { movl 8(%edx), %edx movl 8(%edx), %edx }
- { movl (%eax), eax movl (%eax), eax }
- { cmpl %eax, %edx cmpl %eax, %edx }
- { jnz l123 getting converted to jnz l123 }
- { movl 12(%ebp), %edx movl 4(%eax), eax }
- { movl 16(%ebp), %eax }
- { movl 8(%edx), %edx }
- { movl 4(%eax), eax }
- hp2 := p;
- Cnt2 := 1;
- While Cnt2 <= Cnt Do
- Begin
- If (hp1 = nil) And
- Not(RegInInstruction(Paicpu(hp2)^.oper[1].reg, p)) And
- ((p^.typ = ait_instruction) And
- ((paicpu(p)^.OpCode = A_MOV) or
- (paicpu(p)^.opcode = A_MOVZX) or
- (paicpu(p)^.opcode = A_MOVSX)) And
- (paicpu(p)^.Oper[0].typ = top_ref)) Then
- hp1 := p;
- {$ifndef noremove}
- if regInInstruction(Paicpu(hp2)^.oper[1].reg,p) then
- PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
- {$endif noremove}
- Inc(Cnt2);
- GetNextInstruction(p, p);
- End;
- hp3 := New(Pai_Marker,Init(NoPropInfoStart));
- InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
- {hp4 is used to get the contents of the registers before the sequence}
- GetLastInstruction(hp2, hp4);
- {$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.New2OldReg[RegCounter]])));
- InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
- End;
- {$EndIf CSDebug}
- { If some registers were different in the old and the new sequence, move }
- { the contents of those old registers to the new ones }
- For RegCounter := R_EAX To R_EDI Do
- If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
- (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
- Begin
- AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
- PPaiProp(hp4^.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,hp2);
- If Not(RegCounter In RegInfo.RegsLoadedForRef) And
- {old reg new reg}
- (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
- Begin
- {$ifdef replacereg}
- getLastInstruction(p,hp3);
- If not(regCounter in usableRegs + [R_EDI,R_ESI]) or
- not ReplaceReg(asmL,RegInfo.New2OldReg[RegCounter],
- regCounter,hp3,
- PPaiProp(hp4^.optInfo)^.Regs[regCounter],true,hp5) then
- begin
- {$endif replacereg}
- hp3 := New(Paicpu,Op_Reg_Reg(A_MOV, S_L,
- {old reg new reg}
- RegInfo.New2OldReg[RegCounter], RegCounter));
- InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
- {$ifdef replacereg}
- end
- {$endif replacereg}
- End
- Else
- { 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 (= registers in }
- { RegLoadedForRef, have to be changed to their contents from before the }
- { sequence. }
- If RegCounter in RegInfo.RegsLoadedForRef Then
- Begin
- {load Cnt2 with the total number of instructions of this sequence}
- Cnt2 := PPaiProp(hp4^.OptInfo)^.
- Regs[RegInfo.New2OldReg[RegCounter]].NrOfMods;
- hp3 := hp2;
- For Cnt := 1 to Pred(Cnt2) Do
- GetNextInstruction(hp3, hp3);
- TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
- GetNextInstruction(hp3, hp3);
- {$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 WState of the last pai object of the sequence for later use}
- TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
- {$ifdef csdebug}
- hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '
- +tostr(tmpstate))));
- InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
- {$endif csdebug}
- PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
- PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
- GetNextInstruction(hp3, hp3);
- End;
- {here, hp3 = p = Pai object right after the sequence, TmpState = WState of
- RegCounter at the last Pai object of the sequence}
- GetLastInstruction(hp3, hp3);
- While GetNextInstruction(hp3, hp3) And
- (PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState
- = TmpState) Do
- {$ifdef csdebug}
- begin
- hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '+
- tostr(PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState))));
- InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
- {$endif csdebug}
- PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
- PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
- {$ifdef csdebug}
- end;
- {$endif csdebug}
- {$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
- Else
- If (PPaiProp(p^.OptInfo)^.
- Regs[Reg32(Paicpu(p)^.oper[1].reg)].Typ = Con_Ref) And
- (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
- if (cnt > 0) then
- begin
- hp2 := p;
- Cnt2 := 1;
- While Cnt2 <= Cnt Do
- Begin
- If RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Then
- PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
- Inc(Cnt2);
- GetNextInstruction(p, p);
- End;
- Continue;
- End
- else
- begin
- { Fix for web bug 972 }
- regCounter := Reg32(Paicpu(p)^.oper[1].reg);
- cnt := PPaiProp(p^.optInfo)^.Regs[regCounter].nrOfMods;
- hp3 := p;
- for cnt2 := 1 to cnt do
- if not(regModifiedByInstruction(regCounter,hp3) and
- not(PPaiProp(hp3^.optInfo)^.canBeRemoved)) then
- getNextInstruction(hp3,hp3)
- else
- break;
- getLastInstruction(p,hp4);
- RestoreRegContentsTo(regCounter,
- PPaiProp(hp4^.optInfo)^.Regs[regCounter],
- p,hp3);
- end;
- End;
- End;
- End;
- {$ifdef replacereg}
- top_Reg:
- { try to replace the new reg with the old reg }
- if not(PPaiProp(p^.optInfo)^.canBeRemoved) and
- { only remove if we're not storing something in a regvar }
- (paicpu(p)^.oper[1].reg in (usableregs+[R_EDI])) and
- (paicpu(p)^.opcode = A_MOV) and
- getLastInstruction(p,hp4) then
- begin
- case paicpu(p)^.oper[1].typ of
- top_Reg:
- { we only have to start replacing from the instruction after the mov, }
- { but replacereg only starts with getnextinstruction(p,p) }
- if ReplaceReg(asmL,paicpu(p)^.oper[0].reg,
- paicpu(p)^.oper[1].reg,p,
- PPaiProp(hp4^.optInfo)^.Regs[paicpu(p)^.oper[1].reg],false,hp1) then
- begin
- PPaiProp(p^.optInfo)^.canBeRemoved := true;
- allocRegBetween(asmL,paicpu(p)^.oper[0].reg,
- PPaiProp(p^.optInfo)^.regs[paicpu(p)^.oper[0].reg].startMod,
- hp1);
- end;
- end
- end;
- {$endif replacereg}
- top_symbol,Top_Const:
- Begin
- Case Paicpu(p)^.oper[1].typ Of
- Top_Reg:
- Begin
- regCounter := Reg32(Paicpu(p)^.oper[1].reg);
- If GetLastInstruction(p, hp1) Then
- With PPaiProp(hp1^.OptInfo)^.Regs[regCounter] Do
- If (Typ = Con_Const) And
- (paicpu(startMod)^.opsize >= paicpu(p)^.opsize) and
- opsequal(paicpu(StartMod)^.oper[0],paicpu(p)^.oper[0]) Then
- begin
- PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
- allocRegBetween(asmL,regCounter,startMod,p);
- end;
- End;
- {$ifdef arithopt}
- Top_Ref:
- if (paicpu(p)^.oper[0].typ = top_const) and
- getLastInstruction(p,hp1) and
- findRegWithConst(hp1,paicpu(p)^.opsize,paicpu(p)^.oper[0].val,regCounter) then
- begin
- paicpu(p)^.loadreg(0,regCounter);
- allocRegBetween(AsmL,reg32(regCounter),
- PPaiProp(hp1^.optinfo)^.regs[regCounter].startMod,p);
- end;
- {$endif arithopt}
- End;
- End;
- End;
- End;
- A_STD: If GetLastInstruction(p, hp1) And
- (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
- PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
- End
- End;
- End;
- GetNextInstruction(p, p);
- End;
- End;
- Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
- { Removes the marked instructions and disposes the PPaiProps of the other }
- { instructions }
- Var p, hp1: Pai;
- begin
- p := First;
- While (p <> Last) Do
- Begin
- If (p^.typ = ait_marker) and
- (pai_marker(p)^.kind in [noPropInfoStart,noPropInfoEnd]) then
- begin
- hp1 := pai(p^.next);
- asmL^.remove(p);
- dispose(p,done);
- p := hp1
- end
- else
- {$ifndef noinstremove}
- if assigned(p^.optInfo) and
- PPaiProp(p^.optInfo)^.canBeRemoved then
- begin
- {$IfDef TP}
- Dispose(PPaiProp(p^.OptInfo));
- {$EndIf}
- hp1 := pai(p^.next);
- AsmL^.Remove(p);
- Dispose(p, Done);
- p := hp1;
- End
- Else
- {$endif noinstremove}
- Begin
- {$IfDef TP}
- if assigned(p^.optInfo) then
- Dispose(PPaiProp(p^.OptInfo));
- {$EndIf TP}
- p^.OptInfo := nil;
- p := pai(p^.next);;
- End;
- End;
- {$IfNDef TP}
- FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
- {$EndIf TP}
- End;
- Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
- Begin
- DoCSE(AsmL, First, Last);
- RemoveInstructs(AsmL, First, Last);
- End;
- End.
- {
- $Log$
- Revision 1.2 2000-07-13 11:32:39 michael
- + removed logs
- }
|