12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061 |
- {
- Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
- Development Team
- This unit implements the SPC32 optimizer object
- 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 aoptcpu;
- {$i fpcdefs.inc}
- {$define DEBUG_AOPTCPU}
- Interface
- uses cpubase, cgbase, aasmtai, aopt, aoptcpub;
- Type
- { TCpuAsmOptimizer }
- TCpuAsmOptimizer = class(TAsmOptimizer)
- private
- function SkipMarkers(var p: tai): boolean;
- public
- Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
- { uses the same constructor as TAopObj }
- function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
- function PeepHoleOptPass2Cpu(var p: tai): boolean; override;
- function PostPeepHoleOptsCpu(var p: tai): boolean; override;
- procedure DebugMsg(const s: string; p: tai);
- End;
- Implementation
- uses
- cpuinfo,
- aasmbase,aasmcpu,
- globals,globtype,
- cutils;
- function CanBeCond(p : tai) : boolean;
- begin
- result:=(p.typ=ait_instruction) and (taicpu(p).condition=C_None);
- end;
- function TCpuAsmOptimizer.SkipMarkers(var p: tai): boolean;
- begin
- result:=assigned(p);
- while result and
- assigned(p) and
- (p.typ in [ait_marker,ait_force_line]) do
- begin
- p:=tai(p.next);
- result:=assigned(p);
- end;
- end;
- function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
- var Next: tai; reg: TRegister): Boolean;
- begin
- Next:=Current;
- repeat
- Result:=GetNextInstruction(Next,Next);
- until not(cs_opt_level3 in current_settings.optimizerswitches) or not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
- (is_calljmp(taicpu(Next).opcode));
- end;
- {$ifdef DEBUG_AOPTCPU}
- procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
- begin
- asml.insertbefore(tai_comment.Create(strpnew(s)), p);
- end;
- {$else DEBUG_AOPTCPU}
- procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
- begin
- end;
- {$endif DEBUG_AOPTCPU}
- function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
- var
- hp1,hp2,hp3: tai;
- alloc, dealloc: tai_regalloc;
- i: integer;
- begin
- result := false;
- case p.typ of
- ait_instruction:
- begin
- case taicpu(p).opcode of
- A_LD:
- begin
- {
- turn
- ld rx
- st rx
- into
- ld rx
- }
- if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode=A_ST) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_reg) and
- (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) then
- begin
- DebugMsg('LdSt2Ld', p);
- AsmL.Remove(hp1);
- hp1.free;
- result:=true;
- end
- {
- turn
- ld *
- ld rx
- into
- ld rx
- }
- else if (taicpu(p).ops=1) and
- GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode in [A_LD,A_NUL,A_GS]) then
- begin
- DebugMsg('LdLd2Ld', p);
- AsmL.Remove(p);
- p.free;
- p:=hp1;
- result:=true;
- end
- {
- turn
- ld rx
- ldX #0
- into
- nul
- ldX rx
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode in [A_LDB,A_LDH,A_LDW]) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_const) and
- (taicpu(hp1).oper[0]^.val=0) then
- begin
- DebugMsg('LdLdx2Ldx', p);
- taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
- taicpu(p).ops:=0;
- taicpu(p).opcode:=A_NUL;
- result:=true;
- end
- {
- turn
- ld rx
- alloc ry
- st ry
- ...
- op ry
- dealloc ry
- into
- ld rx
- ...
- op rx
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode=A_ST) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_reg) and
- GetNextInstructionUsingReg(hp1,hp2,taicpu(hp1).oper[0]^.reg) and
- (hp2.typ=ait_instruction) and
- (taicpu(hp2).ops=1) and
- (taicpu(hp2).opcode<>A_ST) and
- (taicpu(hp2).oper[0]^.typ=top_reg) and
- (taicpu(hp2).oper[0]^.reg=taicpu(hp1).oper[0]^.reg) and
- assigned(FindRegDeAlloc(taicpu(hp1).oper[0]^.reg,tai(hp2.Next))) and
- (not RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp2)) then
- begin
- DebugMsg('LdStOp2LdOp', p);
- alloc:=FindRegAllocBackward(taicpu(hp1).oper[0]^.reg, tai(hp1.Previous));
- dealloc:=FindRegDeAlloc(taicpu(hp1).oper[0]^.reg,tai(hp2.next));
- if assigned(alloc) and assigned(dealloc) then
- begin
- asml.Remove(alloc);
- alloc.free;
- asml.Remove(dealloc);
- dealloc.free;
- end;
- AsmL.Remove(hp1);
- hp1.free;
- taicpu(hp2).oper[0]^.reg:=taicpu(p).oper[0]^.reg;
- result:=true;
- end
- {
- turn
- ld rx
- stX *
- ld rx
- into
- ld rx
- stX *
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode in [A_STB,A_STH,A_STW]) and
- (taicpu(hp1).ops=1) and
- GetNextInstruction(hp1,hp2) and
- (hp2.typ=ait_instruction) and
- (taicpu(hp2).opcode=A_LD) and
- (taicpu(hp2).ops=1) and
- (taicpu(hp2).oper[0]^.typ=top_reg) and
- (taicpu(hp2).oper[0]^.reg=taicpu(p).oper[0]^.reg) then
- begin
- DebugMsg('LdStxLd2LdStx', p);
- asml.remove(hp2);
- hp2.free;
- result:=true;
- end
- {
- turn
- ld #x
- ldu #y
- ldX #0
- into
- nul
- ldu #y+$8000
- ldX #x
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_ref) and
- (taicpu(p).oper[0]^.ref^.refaddr=addr_lo16) and
- GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode in [A_LDU]) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_ref) and
- (taicpu(hp1).oper[0]^.ref^.refaddr=addr_hi16) and
- GetNextInstruction(hp1,hp2) and
- (hp2.typ=ait_instruction) and
- (taicpu(hp2).opcode in [A_LDB,A_LDH,A_LDW]) and
- (taicpu(hp2).ops=1) and
- (taicpu(hp2).oper[0]^.typ=top_const) and
- (taicpu(hp2).oper[0]^.val=0) then
- begin
- DebugMsg('LdLduLdx2NulLduLdx', p);
- inc(taicpu(hp1).oper[0]^.ref^.offset, $8000);
- taicpu(hp2).loadref(0, taicpu(p).oper[0]^.ref^);
- taicpu(p).ops:=0;
- taicpu(p).opcode:=A_NUL;
- result:=true;
- end
- {
- turn
- ld 0
- into
- nul
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_const) and
- (taicpu(p).oper[0]^.val=0) then
- begin
- DebugMsg('Ld2Nul', p);
- taicpu(p).ops:=0;
- taicpu(p).opcode:=A_NUL;
- result:=true;
- end
- {
- turn
- ld rX
- ss 6
- ...
- gs 6
- into
- ...
- ld rX
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode=A_SS) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_const) and
- (taicpu(hp1).oper[0]^.val=6) then
- begin
- hp2:=hp1;
- for i:=0 to 4 do
- begin
- if not GetNextInstruction(hp2,hp2) then
- exit;
- if (hp2.typ=ait_instruction) and
- taicpu(hp2).is_jmp then
- exit;
- if (hp2.typ=ait_instruction) and
- (taicpu(hp2).opcode=A_GS) and
- (taicpu(hp2).ops=1) and
- (taicpu(hp2).oper[0]^.typ=top_const) and
- (taicpu(hp2).oper[0]^.val=6) then
- begin
- DebugMsg('LdSS6_2_...Ld', p);
- if RegModifiedBetween(taicpu(p).oper[0]^.reg,hp1,hp2) then
- exit;
- asml.Remove(p);
- asml.InsertAfter(p,hp2);
- asml.Remove(hp2);
- hp2.Free;
- GetNextInstruction(hp1,p);
- asml.Remove(hp1);
- hp1.Free;
- result:=true;
- break;
- end;
- end;
- end;
- end;
- A_ST:
- begin
- {
- turn
- st rx
- ld rx
- dealloc rx
- into
- ...
- }
- if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- SkipMarkers(hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode=A_LD) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_reg) and
- (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
- assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.next))) then
- begin
- DebugMsg('StLd2*', p);
- alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg, tai(p.Previous));
- dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.next));
- if assigned(alloc) and assigned(dealloc) then
- begin
- asml.Remove(alloc);
- alloc.free;
- asml.Remove(dealloc);
- dealloc.free;
- end;
- AsmL.Remove(hp1);
- hp1.free;
- GetNextInstruction(p,hp1);
- AsmL.Remove(p);
- p.free;
- p:=hp1;
- result:=true;
- end
- {
- turn
- st rx
- ld rx
- into
- st rx
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- SkipMarkers(hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode=A_LD) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_reg) and
- (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) then
- begin
- DebugMsg('StLd2St', p);
- AsmL.Remove(hp1);
- hp1.free;
- result:=true;
- end
- {
- turn
- st rx
- st rx
- into
- st rx
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- SkipMarkers(hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode=A_ST) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_reg) and
- (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) then
- begin
- DebugMsg('StSt2St', p);
- AsmL.Remove(hp1);
- hp1.free;
- result:=true;
- end
- {
- turn
- st rx
- ...
- st rx
- into
- st rx
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
- SkipMarkers(hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode=A_ST) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_reg) and
- (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
- (not RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp1)) and
- (not RegUsedBetween(taicpu(p).oper[0]^.reg,p,hp1)) then
- begin
- DebugMsg('St...St2...St', p);
- GetNextInstruction(p,hp1);
- AsmL.Remove(p);
- p.free;
- p:=hp1;
- result:=true;
- end
- {
- turn
- st rx
- nul
- ldw rx
- dealloc rx
- into
- ldw 0
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode=A_NUL) and
- GetNextInstruction(hp1,hp2) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp2).opcode in [A_LDW,A_LDH,A_LDB]) and
- (taicpu(hp2).ops=1) and
- (taicpu(hp2).oper[0]^.typ=top_reg) and
- (taicpu(hp2).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
- Assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp2.next))) then
- begin
- DebugMsg('StNulLdx2Ldx', p);
- alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg, tai(p.Previous));
- dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp2.next));
- if assigned(alloc) and assigned(dealloc) then
- begin
- asml.Remove(alloc);
- alloc.free;
- asml.Remove(dealloc);
- dealloc.free;
- end;
- AsmL.Remove(p);
- p.free;
- AsmL.Remove(hp1);
- hp1.free;
- taicpu(hp2).loadconst(0,0);
- p:=taicpu(hp2);
- if p.Previous<>nil then
- p:=tai(p.Previous); // try to enable some extra optimizations
- result:=true;
- end
- {
- turn
- st rx
- ld ry
- [dealloc ry]
- op rx
- dealloc rx
- into
- op ry
- [dealloc ry]
- }
- else if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- SkipMarkers(hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode=A_LD) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_reg) and
- GetNextInstruction(hp1,hp2) and
- SkipMarkers(hp2) and
- (hp2.typ=ait_instruction) and
- (taicpu(hp2).opcode in [A_ADD,A_ADC,A_MUL,
- A_AND,A_ORR,A_XOR]) and
- (taicpu(hp2).ops=1) and
- (taicpu(hp2).oper[0]^.typ=top_reg) and
- (taicpu(hp2).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
- assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp2.next))) then
- begin
- DebugMsg('StLdOp2Op', p);
- dealloc:=FindRegDeAlloc(taicpu(hp1).oper[0]^.reg,tai(hp1.next));
- if assigned(dealloc) then
- begin
- asml.Remove(dealloc);
- asml.InsertAfter(dealloc,hp2);
- end;
- alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg, tai(p.Previous));
- dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp2.next));
- if assigned(alloc) and assigned(dealloc) then
- begin
- asml.Remove(alloc);
- alloc.free;
- asml.Remove(dealloc);
- dealloc.free;
- end;
- taicpu(hp2).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
- AsmL.Remove(hp1);
- hp1.free;
- AsmL.Remove(p);
- p.free;
- p:=hp2;
- result:=true;
- end;
- end;
- A_GS:
- begin
- {
- turn
- gs pc
- j...
- gs pc
- ...
- into
- gs pc
- j...
- ...
- }
- if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_const) and
- (taicpu(p).oper[0]^.val=SS_PC) then
- begin
- hp1:=p;
- {while GetNextInstruction(hp1,hp2) and
- (hp2.typ=ait_instruction) and
- (taicpu(hp2).is_jmp) do
- begin
- if GetNextInstruction(hp2,hp3) and
- (hp3.typ=ait_instruction) and
- (taicpu(hp3).opcode=A_GS) and
- (taicpu(hp3).ops=1) and
- (taicpu(hp3).oper[0]^.typ=top_const) and
- (taicpu(hp3).oper[0]^.val=SS_PC) then
- begin
- DebugMsg('GsJGs2GsJ', hp1);
- asml.Remove(hp3);
- hp3.free;
- result:=true;
- end
- else
- break;
- hp1:=hp2;
- end;}
- end;
- end;
- A_SS:
- begin
- {
- turn
- ss 6
- ...
- gs 6
- ld rX
- into
- ...
- ld rX
- }
- if (taicpu(p).opcode=A_SS) and
- (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_const) and
- (taicpu(p).oper[0]^.val=6) then
- begin
- hp1:=p;
- hp2:=p;
- for i:=0 to 4 do
- begin
- if not GetNextInstruction(hp2,hp2) then
- exit;
- if (hp2.typ=ait_instruction) and
- taicpu(hp2).is_jmp then
- exit;
- if (hp2.typ=ait_instruction) and
- (taicpu(hp2).opcode=A_GS) and
- (taicpu(hp2).ops=1) and
- (taicpu(hp2).oper[0]^.typ=top_const) and
- (taicpu(hp2).oper[0]^.val=6) then
- begin
- if not GetNextInstruction(hp2,p) then
- exit;
- if (p.typ=ait_instruction) and
- (taicpu(p).opcode in [A_LD,A_NUL,A_GS]) then
- begin
- DebugMsg('Ss6 Removal', p);
- GetNextInstruction(hp1,p);
- asml.Remove(hp2);
- hp2.Free;
- asml.Remove(hp1);
- hp1.Free;
- result:=true;
- end;
- break;
- {if RegModifiedBetween(taicpu(p).oper[0]^.reg,hp1,hp2) then
- exit;
- asml.Remove(p);
- asml.InsertAfter(p,hp2);
- asml.Remove(hp2);
- hp2.Free;
- GetNextInstruction(hp1,p);
- asml.Remove(hp1);
- hp1.Free;
- break;}
- end;
- end;
- end;
- end;
- A_ADD:
- begin
- if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_reg) and
- GetNextInstruction(p,hp1) and
- (taicpu(hp1).opcode in [A_LDW,A_LDH,A_LDB]) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_const) and
- (taicpu(hp1).oper[0]^.val=0) then
- begin
- DebugMsg('AddLdw2Ldw', p);
- taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
- asml.remove(p);
- p.free;
- p:=hp1;
- result:=true;
- end;
- end;
- A_MOV:
- begin
- {
- turn
- mov rx,ry
- info
- ld ry
- st rx
- }
- dealloc:=FindRegDeAlloc(taicpu(p).oper[1]^.reg,tai(p.next));
- hp1:=taicpu.op_reg(A_ST,taicpu(p).oper[0]^.reg);
- if assigned(dealloc) then
- AsmL.InsertAfter(hp1, dealloc)
- else
- AsmL.InsertAfter(hp1, p);
- taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
- taicpu(p).ops:=1;
- taicpu(p).opcode:=A_LD;
- result:=true;
- end;
- A_NUL:
- begin
- {
- turn
- nul
- alloc ry
- st ry
- ...
- op ry
- dealloc ry
- into
- ...
- op 0
- }
- if GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode=A_ST) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_reg) and
- GetNextInstructionUsingReg(hp1,hp2,taicpu(hp1).oper[0]^.reg) and
- (hp2.typ=ait_instruction) and
- (taicpu(hp2).ops=1) and
- (taicpu(hp2).opcode<>A_ST) and
- (taicpu(hp2).oper[0]^.typ=top_reg) and
- (taicpu(hp2).oper[0]^.reg=taicpu(hp1).oper[0]^.reg) and
- assigned(FindRegDeAlloc(taicpu(hp1).oper[0]^.reg,tai(hp2.Next))) then
- begin
- alloc:=FindRegAllocBackward(taicpu(hp1).oper[0]^.reg, tai(hp1.Previous));
- dealloc:=FindRegDeAlloc(taicpu(hp1).oper[0]^.reg,tai(hp2.next));
- if assigned(alloc) and assigned(dealloc) then
- begin
- asml.Remove(alloc);
- alloc.free;
- asml.Remove(dealloc);
- dealloc.free;
- end;
- DebugMsg('NulStOp2Op0', p);
- AsmL.Remove(hp1);
- hp1.free;
- taicpu(hp2).loadconst(0,0);
- result:=true;
- end
- {
- turn
- nul
- ld/nul
- into
- ld/nul
- }
- else if GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode in [A_NUL,A_LD]) then
- begin
- DebugMsg('Dup nul removed', p);
- asml.remove(p);
- p.free;
- p:=hp1;
- result:=true;
- end
- {
- turn
- nul
- add *
- into
- ld *
- }
- else if GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode in [A_ADD]) then
- begin
- DebugMsg('NulAdd2Ld', p);
- asml.remove(p);
- p.free;
- p:=hp1;
- taicpu(p).opcode:=A_LD;
- result:=true;
- end
- {
- turn
- nul
- sub #x
- into
- ld #x
- }
- else if GetNextInstruction(p,hp1) and
- (hp1.typ=ait_instruction) and
- (taicpu(hp1).opcode in [A_SUB]) and
- (taicpu(hp1).ops=1) and
- (taicpu(hp1).oper[0]^.typ=top_const) and
- (abs(taicpu(hp1).oper[0]^.val)<$8000) then
- begin
- DebugMsg('NulSub2Ld', p);
- asml.remove(p);
- p.free;
- p:=hp1;
- taicpu(p).opcode:=A_LD;
- taicpu(p).oper[0]^.val:=-taicpu(p).oper[0]^.val;
- result:=true;
- end;
- end;
- A_LOAD:
- begin
- {
- ss 6
- ld ref.offset
- add ref.index
- ldw ref.base
- st reg
- gs 6
- ss 6
- ld ref.offset
- ldw ref.base
- st reg
- gs 6
- }
- if GetNextInstruction(p,hp1) and
- (tai(hp1).typ=ait_instruction) and
- (not (taicpu(hp1).opcode in [A_LD,A_NUL,A_GS])) then
- begin
- AsmL.InsertBefore(taicpu.op_const(A_SS,6),p);
- AsmL.InsertAfter(taicpu.op_const(A_GS,6),p);
- end;
- AsmL.InsertAfter(taicpu.op_reg(A_ST,taicpu(p).oper[0]^.reg),p);
- AsmL.InsertAfter(taicpu.op_reg(A_LDW,taicpu(p).oper[1]^.ref^.base),p);
- if taicpu(p).oper[1]^.ref^.index<>NR_NO then
- AsmL.InsertAfter(taicpu.op_reg(A_ADD,taicpu(p).oper[1]^.ref^.index),p);
- taicpu(p).opcode:=A_LD;
- taicpu(p).loadconst(0, taicpu(p).oper[1]^.ref^.offset);
- taicpu(p).ops:=1;
- result:=true;
- end;
- A_STORE:
- begin
- {
- ss 6
- ld ref.offset
- add ref.index
- add ref.base
- stw reg
- gs 6
- }
- if GetNextInstruction(p,hp1) and
- (tai(hp1).typ=ait_instruction) and
- (not (taicpu(hp1).opcode in [A_LD,A_NUL,A_GS])) then
- begin
- AsmL.InsertBefore(taicpu.op_const(A_SS,6),p);
- AsmL.InsertAfter(taicpu.op_const(A_GS,6),p);
- end;
- //AsmL.InsertBefore(taicpu.op_const(A_SS,6),p);
- //AsmL.InsertAfter(taicpu.op_const(A_GS,6),p);
- AsmL.InsertAfter(taicpu.op_reg(A_STW,taicpu(p).oper[0]^.reg),p);
- AsmL.InsertAfter(taicpu.op_reg(A_ADD,taicpu(p).oper[1]^.ref^.base),p);
- if taicpu(p).oper[1]^.ref^.index<>NR_NO then
- AsmL.InsertAfter(taicpu.op_reg(A_ADD,taicpu(p).oper[1]^.ref^.index),p);
- taicpu(p).opcode:=A_LD;
- taicpu(p).loadconst(0, taicpu(p).oper[1]^.ref^.offset);
- taicpu(p).ops:=1;
- result:=true;
- end;
- end;
- end;
- end;
- end;
- function TCpuAsmOptimizer.PeepHoleOptPass2Cpu(var p: tai): boolean;
- var
- hp1,hp2,hp3: tai;
- alloc, dealloc: tai_regalloc;
- i: integer;
- begin
- result := false;
- case p.typ of
- ait_instruction:
- begin
- case taicpu(p).opcode of
- A_PJMP:
- begin
- AsmL.InsertBefore(taicpu.op_const(A_GS,SS_PC),p);
- taicpu(p).opcode:=A_JMP;
- result:=true;
- end;
- A_PJxx:
- begin
- AsmL.InsertBefore(taicpu.op_const(A_GS,SS_PC),p);
- taicpu(p).opcode:=A_Jxx;
- result:=true;
- end;
- A_PCALL:
- begin
- AsmL.InsertBefore(taicpu.op_const(A_GS,SS_PC),p);
- taicpu(p).opcode:=A_CALL;
- result:=true;
- end;
- end;
- end;
- end;
- end;
- function TCpuAsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
- var
- hp1,hp2,hp3, hp4: tai;
- alloc, dealloc: tai_regalloc;
- i: integer;
- begin
- result := false;
- case p.typ of
- ait_instruction:
- begin
- case taicpu(p).opcode of
- A_GS:
- begin
- if (taicpu(p).ops=1) and
- (taicpu(p).oper[0]^.typ=top_const) and
- (taicpu(p).oper[0]^.val=SS_PC) then
- begin
- hp1:=p;
- while GetNextInstruction(hp1,hp2) and
- (hp2.typ=ait_instruction) and
- (taicpu(hp2).is_jmp) do
- begin
- if GetNextInstruction(hp2,hp3) and
- (hp3.typ=ait_label) and
- GetNextInstruction(hp3,hp4) and
- (hp4.typ=ait_instruction) and
- (taicpu(hp4).opcode=A_GS) and
- (taicpu(hp4).ops=1) and
- (taicpu(hp4).oper[0]^.typ=top_const) and
- (taicpu(hp4).oper[0]^.val=SS_PC) then
- begin
- DebugMsg('GsJGs2GsJ', hp1);
- asml.Remove(hp3);
- asml.InsertBefore(hp3,p);
- asml.Remove(hp4);
- hp4.free;
- result:=true;
- end
- else
- break;
- hp1:=hp2;
- end;
- end;
- end;
- end;
- end;
- end;
- end;
- begin
- casmoptimizer:=TCpuAsmOptimizer;
- End.
|