{ Copyright (c) 1998-2004 by Jonas Maebe This unit calls the optimization procedures to optimize the assembler code for sparc 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 cgbase, cpubase, aoptobj, aoptcpub, aopt, aasmtai, aasmcpu; Type TAsmOpSet = set of TAsmOp; TCpuAsmOptimizer = class(TAsmOptimizer) function RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean; override; function GetNextInstructionUsingReg(Current: tai; var Next: tai; reg: TRegister): Boolean; function TryRemoveMov(var p: tai; opcode: TAsmOp): boolean; function TryRemoveMovToRefIndex(var p: tai; next: taicpu): boolean; function TryRemoveMovBeforeStore(var p: tai; next: taicpu; const storeops: TAsmOpSet): boolean; function PeepHoleOptPass1Cpu(var p: tai): boolean; override; procedure PeepHoleOptPass2; override; function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override; function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override; { outputs a debug message into the assembler file } procedure DebugMsg(const s: string; p: tai); End; Implementation uses cutils,globtype,globals,aasmbase,cpuinfo,verbose; function MatchInstruction(const instr: tai; const op: TAsmOp): boolean; begin result := (instr.typ = ait_instruction) and (taicpu(instr).opcode = op); end; function MatchOperand(const oper: TOper; reg: TRegister): boolean; begin result:=(oper.typ=top_reg) and (oper.reg=reg); end; function IsSameReg(this,next: taicpu): boolean; begin result:=(next.oper[0]^.typ=top_reg) and (next.oper[1]^.typ=top_reg) and (next.oper[0]^.reg=next.oper[1]^.reg) and (next.oper[0]^.reg=this.oper[0]^.reg); end; function CanBeCMOV(p: tai; condreg: tregister): boolean; begin result:=assigned(p) and (p.typ=ait_instruction) and ((taicpu(p).opcode in [A_MOV_D,A_MOV_S]) or ( { register with condition must not be overwritten } (taicpu(p).opcode=A_MOVE) and (taicpu(p).oper[0]^.reg<>condreg) )); end; procedure ChangeToCMOV(p: taicpu; cond: tasmcond; reg: tregister); begin case cond of C_COP1TRUE: case p.opcode of A_MOV_D: p.opcode:=A_MOVT_D; A_MOV_S: p.opcode:=A_MOVT_S; A_MOVE: p.opcode:=A_MOVT; else InternalError(2014061701); end; C_COP1FALSE: case p.opcode of A_MOV_D: p.opcode:=A_MOVF_D; A_MOV_S: p.opcode:=A_MOVF_S; A_MOVE: p.opcode:=A_MOVF; else InternalError(2014061702); end; C_EQ: case p.opcode of A_MOV_D: p.opcode:=A_MOVZ_D; A_MOV_S: p.opcode:=A_MOVZ_S; A_MOVE: p.opcode:=A_MOVZ; else InternalError(2014061703); end; C_NE: case p.opcode of A_MOV_D: p.opcode:=A_MOVN_D; A_MOV_S: p.opcode:=A_MOVN_S; A_MOVE: p.opcode:=A_MOVN; else InternalError(2014061704); end; else InternalError(2014061705); end; p.ops:=3; p.loadreg(2,reg); 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.InstructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean; var p: taicpu; i: longint; begin result:=false; if not (assigned(hp) and (hp.typ=ait_instruction)) then exit; p:=taicpu(hp); i:=0; while(i