{ Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal Development Team This unit implements the ARM 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} Interface uses cgbase, cpubase, aasmtai, aopt, aoptcpub, aoptobj; Type { TCpuAsmOptimizer } TCpuAsmOptimizer = class(TAsmOptimizer) { uses the same constructor as TAopObj } function PeepHoleOptPass1Cpu(var p: tai): boolean; override; procedure PeepHoleOptPass2;override; Function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override; procedure RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string); function RegUsedAfterInstruction(reg: Tregister; p: tai; var AllUsedRegs: TAllUsedRegs): Boolean; End; TCpuPreRegallocScheduler = class(TAsmOptimizer) function PeepHoleOptPass1Cpu(var p: tai): boolean;override; end; TCpuThumb2AsmOptimizer = class(TCpuAsmOptimizer) { uses the same constructor as TAopObj } procedure PeepHoleOptPass2;override; End; Implementation uses cutils, verbose, cgutils, aasmbase,aasmdata,aasmcpu; function CanBeCond(p : tai) : boolean; begin result:= (p.typ=ait_instruction) and (taicpu(p).condition=C_None) and ((taicpu(p).opcode<>A_BLX) or (taicpu(p).oper[0]^.typ=top_reg)); end; function RefsEqual(const r1, r2: treference): boolean; begin refsequal := (r1.offset = r2.offset) and (r1.base = r2.base) and (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and (r1.relsymbol = r2.relsymbol) and (r1.signindex = r2.signindex) and (r1.shiftimm = r2.shiftimm) and (r1.addressmode = r2.addressmode) and (r1.shiftmode = r2.shiftmode); end; function MatchInstruction(const instr: tai; const op: TAsmOp; const cond: TAsmConds; const postfix: TOpPostfixes): boolean; begin result := (instr.typ = ait_instruction) and (taicpu(instr).opcode = op) and ((cond = []) or (taicpu(instr).condition in cond)) and ((postfix = []) or (taicpu(instr).oppostfix in postfix)); end; function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline; begin result := (oper1.typ = oper2.typ) and ( ((oper1.typ = top_const) and (oper1.val = oper2.val)) or ((oper1.typ = top_reg) and (oper1.reg = oper2.reg)) or ((oper1.typ = top_conditioncode) and (oper1.cc = oper2.cc)) ); end; function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline; begin result := (oper.typ = top_reg) and (oper.reg = reg); end; procedure RemoveRedundantMove(const cmpp: tai; movp: tai; asml: TAsmList); begin if (taicpu(movp).condition = C_EQ) and (taicpu(cmpp).oper[0]^.reg = taicpu(movp).oper[0]^.reg) and (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then begin asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp); asml.remove(movp); movp.free; end; end; function regLoadedWithNewValue(reg: tregister; hp: tai): boolean; var p: taicpu; begin p := taicpu(hp); regLoadedWithNewValue := false; if not ((assigned(hp)) and (hp.typ = ait_instruction)) then exit; {These are not writing to their first oper} if p.opcode in [A_STR, A_STRB, A_STRH, A_CMP, A_CMN, A_TST, A_TEQ, A_B, A_BL, A_BX, A_BLX] then exit; { These four are writing into the first 2 register, UMLAL and SMLAL will also read from them } if (p.opcode in [A_UMLAL, A_UMULL, A_SMLAL, A_SMULL]) and (p.oper[1]^.typ = top_reg) and (p.oper[1]^.reg = reg) then begin regLoadedWithNewValue := true; exit end; {All other instructions use oper[0] as destination} regLoadedWithNewValue := (p.oper[0]^.typ = top_reg) and (p.oper[0]^.reg = reg); end; function instructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean; var p: taicpu; i: longint; begin instructionLoadsFromReg := false; if not (assigned(hp) and (hp.typ = ait_instruction)) then exit; p:=taicpu(hp); i:=1; {For these instructions we have to start on oper[0]} if (p.opcode in [A_STR, A_STRB, A_STRH, A_CMP, A_CMN, A_TST, A_TEQ, A_B, A_BL, A_BX, A_BLX, A_SMLAL, A_UMLAL]) then i:=0; while(i