aoptcpu.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the ARM optimizer object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit aoptcpu;
  19. {$i fpcdefs.inc}
  20. {$define DEBUG_AOPTCPU}
  21. Interface
  22. uses cpubase, cgbase, aasmtai, aopt,AoptObj, aoptcpub;
  23. Type
  24. TCpuAsmOptimizer = class(TAsmOptimizer)
  25. { outputs a debug message into the assembler file }
  26. procedure DebugMsg(const s: string; p: tai);
  27. Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
  28. function RegInInstruction(Reg: TRegister; p1: tai): Boolean; override;
  29. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  30. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  31. { uses the same constructor as TAopObj }
  32. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  33. procedure PeepHoleOptPass2;override;
  34. End;
  35. Implementation
  36. uses
  37. cutils,
  38. verbose,
  39. cpuinfo,
  40. aasmbase,aasmcpu,aasmdata,
  41. globals,globtype,
  42. cgutils;
  43. type
  44. TAsmOpSet = set of TAsmOp;
  45. function CanBeCond(p : tai) : boolean;
  46. begin
  47. result:=(p.typ=ait_instruction) and (taicpu(p).condition=C_None);
  48. end;
  49. function RefsEqual(const r1, r2: treference): boolean;
  50. begin
  51. refsequal :=
  52. (r1.offset = r2.offset) and
  53. (r1.base = r2.base) and
  54. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  55. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  56. (r1.relsymbol = r2.relsymbol) and
  57. (r1.addressmode = r2.addressmode);
  58. end;
  59. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  60. begin
  61. result:=oper1.typ=oper2.typ;
  62. if result then
  63. case oper1.typ of
  64. top_const:
  65. Result:=oper1.val = oper2.val;
  66. top_reg:
  67. Result:=oper1.reg = oper2.reg;
  68. top_ref:
  69. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  70. else Result:=false;
  71. end
  72. end;
  73. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  74. begin
  75. result := (oper.typ = top_reg) and (oper.reg = reg);
  76. end;
  77. function MatchInstruction(const instr: tai; const op: TAsmOp): boolean;
  78. begin
  79. result :=
  80. (instr.typ = ait_instruction) and
  81. (taicpu(instr).opcode = op);
  82. end;
  83. function MatchInstruction(const instr: tai; const ops: TAsmOpSet): boolean;
  84. begin
  85. result :=
  86. (instr.typ = ait_instruction) and
  87. (taicpu(instr).opcode in ops);
  88. end;
  89. function MatchInstruction(const instr: tai; const ops: TAsmOpSet;opcount : byte): boolean;
  90. begin
  91. result :=
  92. (instr.typ = ait_instruction) and
  93. (taicpu(instr).opcode in ops) and
  94. (taicpu(instr).ops=opcount);
  95. end;
  96. function MatchOpType(const instr : tai;ot0,ot1 : toptype) : Boolean;
  97. begin
  98. Result:=(taicpu(instr).ops=2) and
  99. (taicpu(instr).oper[0]^.typ=ot0) and
  100. (taicpu(instr).oper[1]^.typ=ot1);
  101. end;
  102. {$ifdef DEBUG_AOPTCPU}
  103. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  104. begin
  105. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  106. end;
  107. {$else DEBUG_AOPTCPU}
  108. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  109. begin
  110. end;
  111. {$endif DEBUG_AOPTCPU}
  112. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  113. begin
  114. If (p1.typ = ait_instruction) and (taicpu(p1).opcode in [A_MUL,A_MULS,A_FMUL,A_FMULS,A_FMULSU]) and
  115. ((getsupreg(reg)=RS_R0) or (getsupreg(reg)=RS_R1)) then
  116. Result:=true
  117. else
  118. Result:=inherited RegInInstruction(Reg, p1);
  119. end;
  120. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
  121. var Next: tai; reg: TRegister): Boolean;
  122. begin
  123. Next:=Current;
  124. repeat
  125. Result:=GetNextInstruction(Next,Next);
  126. until not(cs_opt_level3 in current_settings.optimizerswitches) or not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
  127. (is_calljmp(taicpu(Next).opcode));
  128. end;
  129. function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  130. var
  131. p: taicpu;
  132. begin
  133. if not assigned(hp) or
  134. (hp.typ <> ait_instruction) then
  135. begin
  136. Result := false;
  137. exit;
  138. end;
  139. p := taicpu(hp);
  140. Result := ((p.opcode in [A_LDI,A_MOV,A_LDS]) and (reg=p.oper[0]^.reg) and ((p.oper[1]^.typ<>top_reg) or (reg<>p.oper[0]^.reg))) or
  141. ((p.opcode in [A_LD,A_LDD,A_LPM]) and (reg=p.oper[0]^.reg) and not(RegInRef(reg,p.oper[1]^.ref^))) or
  142. ((p.opcode in [A_MOVW]) and ((reg=p.oper[0]^.reg) or (TRegister(ord(reg)+1)=p.oper[0]^.reg)) and not(reg=p.oper[1]^.reg) and not(TRegister(ord(reg)+1)=p.oper[1]^.reg)) or
  143. ((p.opcode in [A_POP]) and (reg=p.oper[0]^.reg));
  144. end;
  145. function TCpuAsmOptimizer.InstructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  146. var
  147. p: taicpu;
  148. i: longint;
  149. begin
  150. Result := false;
  151. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  152. exit;
  153. p:=taicpu(hp);
  154. i:=0;
  155. { we do not care about the stack pointer }
  156. if p.opcode in [A_POP] then
  157. exit;
  158. { first operand only written?
  159. then skip it }
  160. if p.opcode in [A_MOV,A_LD,A_LDD,A_LDS,A_LPM,A_LDI,A_MOVW] then
  161. i:=1;
  162. while(i<p.ops) do
  163. begin
  164. case p.oper[I]^.typ of
  165. top_reg:
  166. Result := (p.oper[I]^.reg = reg) or
  167. { MOVW }
  168. ((i=1) and (p.opcode=A_MOVW) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg)));
  169. top_ref:
  170. Result :=
  171. (p.oper[I]^.ref^.base = reg) or
  172. (p.oper[I]^.ref^.index = reg);
  173. end;
  174. { Bailout if we found something }
  175. if Result then
  176. exit;
  177. Inc(I);
  178. end;
  179. end;
  180. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  181. var
  182. hp1,hp2,hp3,hp4,hp5: tai;
  183. alloc, dealloc: tai_regalloc;
  184. i: integer;
  185. l: TAsmLabel;
  186. TmpUsedRegs : TAllUsedRegs;
  187. begin
  188. result := false;
  189. case p.typ of
  190. ait_instruction:
  191. begin
  192. {
  193. change
  194. <op> reg,x,y
  195. cp reg,r1
  196. into
  197. <op>s reg,x,y
  198. }
  199. { this optimization can applied only to the currently enabled operations because
  200. the other operations do not update all flags and FPC does not track flag usage }
  201. if MatchInstruction(p, [A_ADC,A_ADD,A_AND,A_ANDI,A_ASR,A_COM,A_DEC,A_EOR,
  202. A_INC,A_LSL,A_LSR,
  203. A_OR,A_ORI,A_ROL,A_ROR,A_SBC,A_SBCI,A_SUB,A_SUBI]) and
  204. GetNextInstruction(p, hp1) and
  205. ((MatchInstruction(hp1, A_CP) and
  206. (((taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  207. (taicpu(hp1).oper[1]^.reg = NR_R1)) or
  208. ((taicpu(p).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  209. (taicpu(hp1).oper[0]^.reg = NR_R1) and
  210. (taicpu(p).opcode in [A_ADC,A_ADD,A_AND,A_ANDI,A_ASR,A_COM,A_EOR,
  211. A_LSL,A_LSR,
  212. A_OR,A_ORI,A_ROL,A_ROR])))) or
  213. (MatchInstruction(hp1, A_CPI) and
  214. (taicpu(p).opcode = A_ANDI) and
  215. (taicpu(p).oper[1]^.typ=top_const) and
  216. (taicpu(hp1).oper[1]^.typ=top_const) and
  217. (taicpu(p).oper[1]^.val=taicpu(hp1).oper[1]^.val))) and
  218. GetNextInstruction(hp1, hp2) and
  219. { be careful here, following instructions could use other flags
  220. however after a jump fpc never depends on the value of flags }
  221. { All above instructions set Z and N according to the following
  222. Z := result = 0;
  223. N := result[31];
  224. EQ = Z=1; NE = Z=0;
  225. MI = N=1; PL = N=0; }
  226. MatchInstruction(hp2, A_BRxx) and
  227. (taicpu(hp2).condition in [C_EQ,C_NE,C_MI,C_PL]) { and
  228. no flag allocation tracking implemented yet on avr
  229. assigned(FindRegDealloc(NR_DEFAULTFLAGS,tai(hp2.Next)))} then
  230. begin
  231. { move flag allocation if possible }
  232. { no flag allocation tracking implemented yet on avr
  233. GetLastInstruction(hp1, hp2);
  234. hp2:=FindRegAlloc(NR_DEFAULTFLAGS,tai(hp2.Next));
  235. if assigned(hp2) then
  236. begin
  237. asml.Remove(hp2);
  238. asml.insertbefore(hp2, p);
  239. end;
  240. }
  241. // If we compare to the same value we are masking then invert the comparison
  242. if (taicpu(hp1).opcode=A_CPI) then
  243. taicpu(hp2).condition:=inverse_cond(taicpu(hp2).condition);
  244. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  245. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,hp2), hp2);
  246. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  247. DebugMsg('Peephole OpCp2Op performed', p);
  248. asml.remove(hp1);
  249. hp1.free;
  250. Result:=true;
  251. end
  252. else
  253. case taicpu(p).opcode of
  254. A_LDI:
  255. begin
  256. { turn
  257. ldi reg0, imm
  258. cp/mov reg1, reg0
  259. dealloc reg0
  260. into
  261. cpi/ldi reg1, imm
  262. }
  263. if MatchOpType(p,top_reg,top_const) and
  264. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  265. MatchInstruction(hp1,[A_CP,A_MOV],2) and
  266. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  267. MatchOpType(hp1,top_reg,top_reg) and
  268. (getsupreg(taicpu(hp1).oper[0]^.reg) in [16..31]) and
  269. (taicpu(hp1).oper[1]^.reg=taicpu(p).oper[0]^.reg) then
  270. begin
  271. CopyUsedRegs(TmpUsedRegs);
  272. if not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp1, TmpUsedRegs)) then
  273. begin
  274. case taicpu(hp1).opcode of
  275. A_CP:
  276. taicpu(hp1).opcode:=A_CPI;
  277. A_MOV:
  278. taicpu(hp1).opcode:=A_LDI;
  279. else
  280. internalerror(2016111901);
  281. end;
  282. taicpu(hp1).loadconst(1, taicpu(p).oper[1]^.val);
  283. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  284. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  285. if assigned(alloc) and assigned(dealloc) then
  286. begin
  287. asml.Remove(alloc);
  288. alloc.Free;
  289. asml.Remove(dealloc);
  290. dealloc.Free;
  291. end;
  292. DebugMsg('Peephole LdiMov/Cp2Ldi/Cpi performed', p);
  293. GetNextInstruction(p,hp1);
  294. asml.Remove(p);
  295. p.Free;
  296. p:=hp1;
  297. result:=true;
  298. end;
  299. ReleaseUsedRegs(TmpUsedRegs);
  300. end;
  301. end;
  302. A_STS:
  303. if (taicpu(p).oper[0]^.ref^.symbol=nil) and
  304. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  305. (getsupreg(taicpu(p).oper[0]^.ref^.base)=RS_NO) and
  306. (getsupreg(taicpu(p).oper[0]^.ref^.index)=RS_NO) and
  307. (taicpu(p).oper[0]^.ref^.addressmode=AM_UNCHANGED) and
  308. (taicpu(p).oper[0]^.ref^.offset>=32) and
  309. (taicpu(p).oper[0]^.ref^.offset<=95) then
  310. begin
  311. DebugMsg('Peephole Sts2Out performed', p);
  312. taicpu(p).opcode:=A_OUT;
  313. taicpu(p).loadconst(0,taicpu(p).oper[0]^.ref^.offset-32);
  314. end;
  315. A_LDS:
  316. if (taicpu(p).oper[1]^.ref^.symbol=nil) and
  317. (taicpu(p).oper[1]^.ref^.relsymbol=nil) and
  318. (getsupreg(taicpu(p).oper[1]^.ref^.base)=RS_NO) and
  319. (getsupreg(taicpu(p).oper[1]^.ref^.index)=RS_NO) and
  320. (taicpu(p).oper[1]^.ref^.addressmode=AM_UNCHANGED) and
  321. (taicpu(p).oper[1]^.ref^.offset>=32) and
  322. (taicpu(p).oper[1]^.ref^.offset<=95) then
  323. begin
  324. DebugMsg('Peephole Lds2In performed', p);
  325. taicpu(p).opcode:=A_IN;
  326. taicpu(p).loadconst(1,taicpu(p).oper[1]^.ref^.offset-32);
  327. end;
  328. A_IN:
  329. if GetNextInstruction(p,hp1) then
  330. begin
  331. {
  332. in rX,Y
  333. ori rX,n
  334. out Y,rX
  335. into
  336. sbi rX,lg(n)
  337. }
  338. if (taicpu(p).oper[1]^.val<=31) and
  339. MatchInstruction(hp1,A_ORI) and
  340. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
  341. (PopCnt(byte(taicpu(hp1).oper[1]^.val))=1) and
  342. GetNextInstruction(hp1,hp2) and
  343. MatchInstruction(hp2,A_OUT) and
  344. MatchOperand(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  345. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) then
  346. begin
  347. DebugMsg('Peephole InOriOut2Sbi performed', p);
  348. taicpu(p).opcode:=A_SBI;
  349. taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
  350. taicpu(p).loadconst(1,BsrByte(taicpu(hp1).oper[1]^.val));
  351. asml.Remove(hp1);
  352. hp1.Free;
  353. asml.Remove(hp2);
  354. hp2.Free;
  355. result:=true;
  356. end
  357. {
  358. in rX,Y
  359. andi rX,not(n)
  360. out Y,rX
  361. into
  362. cbi rX,lg(n)
  363. }
  364. else if (taicpu(p).oper[1]^.val<=31) and
  365. MatchInstruction(hp1,A_ANDI) and
  366. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
  367. (PopCnt(byte(not(taicpu(hp1).oper[1]^.val)))=1) and
  368. GetNextInstruction(hp1,hp2) and
  369. MatchInstruction(hp2,A_OUT) and
  370. MatchOperand(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  371. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) then
  372. begin
  373. DebugMsg('Peephole InAndiOut2Cbi performed', p);
  374. taicpu(p).opcode:=A_CBI;
  375. taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
  376. taicpu(p).loadconst(1,BsrByte(not(taicpu(hp1).oper[1]^.val)));
  377. asml.Remove(hp1);
  378. hp1.Free;
  379. asml.Remove(hp2);
  380. hp2.Free;
  381. result:=true;
  382. end
  383. {
  384. in rX,Y
  385. andi rX,n
  386. breq/brne L1
  387. into
  388. sbis/sbic Y,lg(n)
  389. jmp L1
  390. .Ltemp:
  391. }
  392. else if (taicpu(p).oper[1]^.val<=31) and
  393. MatchInstruction(hp1,A_ANDI) and
  394. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
  395. (PopCnt(byte(taicpu(hp1).oper[1]^.val))=1) and
  396. GetNextInstruction(hp1,hp2) and
  397. MatchInstruction(hp2,A_BRxx) and
  398. (taicpu(hp2).condition in [C_EQ,C_NE]) then
  399. begin
  400. if taicpu(hp2).condition=C_EQ then
  401. taicpu(p).opcode:=A_SBIS
  402. else
  403. taicpu(p).opcode:=A_SBIC;
  404. DebugMsg('Peephole InAndiBrx2SbixJmp performed', p);
  405. taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
  406. taicpu(p).loadconst(1,BsrByte(taicpu(hp1).oper[1]^.val));
  407. asml.Remove(hp1);
  408. hp1.Free;
  409. taicpu(hp2).condition:=C_None;
  410. if CPUAVR_HAS_JMP_CALL in cpu_capabilities[current_settings.cputype] then
  411. taicpu(hp2).opcode:=A_JMP
  412. else
  413. taicpu(hp2).opcode:=A_RJMP;
  414. current_asmdata.getjumplabel(l);
  415. l.increfs;
  416. asml.InsertAfter(tai_label.create(l), hp2);
  417. result:=true;
  418. end;
  419. end;
  420. A_ANDI:
  421. begin
  422. {
  423. Turn
  424. andi rx, #pow2
  425. brne l
  426. <op>
  427. l:
  428. Into
  429. sbrs rx, #(1 shl imm)
  430. <op>
  431. l:
  432. }
  433. if (taicpu(p).ops=2) and
  434. (taicpu(p).oper[1]^.typ=top_const) and
  435. ispowerof2(taicpu(p).oper[1]^.val,i) and
  436. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(p.next))) and
  437. GetNextInstruction(p,hp1) and
  438. (hp1.typ=ait_instruction) and
  439. (taicpu(hp1).opcode=A_BRxx) and
  440. (taicpu(hp1).condition in [C_EQ,C_NE]) and
  441. (taicpu(hp1).ops>0) and
  442. (taicpu(hp1).oper[0]^.typ = top_ref) and
  443. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  444. GetNextInstruction(hp1,hp2) and
  445. (hp2.typ=ait_instruction) and
  446. GetNextInstruction(hp2,hp3) and
  447. (hp3.typ=ait_label) and
  448. (taicpu(hp1).oper[0]^.ref^.symbol=tai_label(hp3).labsym) then
  449. begin
  450. DebugMsg('Peephole AndiBr2Sbr performed', p);
  451. taicpu(p).oper[1]^.val:=i;
  452. if taicpu(hp1).condition=C_NE then
  453. taicpu(p).opcode:=A_SBRS
  454. else
  455. taicpu(p).opcode:=A_SBRC;
  456. asml.Remove(hp1);
  457. hp1.free;
  458. result:=true;
  459. end
  460. {
  461. Remove
  462. andi rx, #y
  463. dealloc rx
  464. }
  465. else if (taicpu(p).ops=2) and
  466. (taicpu(p).oper[0]^.typ=top_reg) and
  467. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(p.next))) and
  468. (assigned(FindRegDeAlloc(NR_DEFAULTFLAGS,tai(p.Next))) or
  469. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs))) then
  470. begin
  471. DebugMsg('Redundant Andi removed', p);
  472. GetNextInstruction(p,hp1);
  473. AsmL.Remove(p);
  474. p.free;
  475. p:=hp1;
  476. result:=true;
  477. end;
  478. end;
  479. A_CLR:
  480. begin
  481. { turn the common
  482. clr rX
  483. mov/ld rX, rY
  484. into
  485. mov/ld rX, rY
  486. }
  487. if (taicpu(p).ops=1) and
  488. (taicpu(p).oper[0]^.typ=top_reg) and
  489. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  490. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  491. (hp1.typ=ait_instruction) and
  492. (taicpu(hp1).opcode in [A_MOV,A_LD]) and
  493. (taicpu(hp1).ops>0) and
  494. (taicpu(hp1).oper[0]^.typ=top_reg) and
  495. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) then
  496. begin
  497. DebugMsg('Peephole ClrMov2Mov performed', p);
  498. asml.Remove(p);
  499. p.Free;
  500. p:=hp1;
  501. result:=true;
  502. end
  503. { turn
  504. clr rX
  505. ...
  506. adc rY, rX
  507. into
  508. ...
  509. adc rY, r1
  510. }
  511. else if (taicpu(p).ops=1) and
  512. (taicpu(p).oper[0]^.typ=top_reg) and
  513. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  514. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  515. (hp1.typ=ait_instruction) and
  516. (taicpu(hp1).opcode in [A_ADC,A_SBC]) and
  517. (taicpu(hp1).ops=2) and
  518. (taicpu(hp1).oper[1]^.typ=top_reg) and
  519. (taicpu(hp1).oper[1]^.reg=taicpu(p).oper[0]^.reg) and
  520. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[0]^.reg) and
  521. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  522. begin
  523. DebugMsg('Peephole ClrAdc2Adc performed', p);
  524. taicpu(hp1).oper[1]^.reg:=NR_R1;
  525. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  526. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  527. if assigned(alloc) and assigned(dealloc) then
  528. begin
  529. asml.Remove(alloc);
  530. alloc.Free;
  531. asml.Remove(dealloc);
  532. dealloc.Free;
  533. end;
  534. GetNextInstruction(p,hp1);
  535. asml.Remove(p);
  536. p.free;
  537. p:=hp1;
  538. result:=true;
  539. end;
  540. end;
  541. A_PUSH:
  542. begin
  543. { turn
  544. push reg0
  545. push reg1
  546. pop reg3
  547. pop reg2
  548. into
  549. movw reg2,reg0
  550. or
  551. mov reg3,reg1
  552. mov reg2,reg0
  553. }
  554. if GetNextInstruction(p,hp1) and
  555. MatchInstruction(hp1,A_PUSH) and
  556. GetNextInstruction(hp1,hp2) and
  557. MatchInstruction(hp2,A_POP) and
  558. GetNextInstruction(hp2,hp3) and
  559. MatchInstruction(hp3,A_POP) then
  560. begin
  561. if (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(p).oper[0]^.reg)+1) and
  562. ((getsupreg(taicpu(p).oper[0]^.reg) mod 2)=0) and
  563. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp3).oper[0]^.reg)+1) and
  564. ((getsupreg(taicpu(hp3).oper[0]^.reg) mod 2)=0) then
  565. begin
  566. DebugMsg('Peephole PushPushPopPop2Movw performed', p);
  567. taicpu(p).ops:=2;
  568. taicpu(p).opcode:=A_MOVW;
  569. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  570. taicpu(p).loadreg(0, taicpu(hp3).oper[0]^.reg);
  571. asml.Remove(hp1);
  572. hp1.Free;
  573. asml.Remove(hp2);
  574. hp2.Free;
  575. asml.Remove(hp3);
  576. hp3.Free;
  577. result:=true;
  578. end
  579. else
  580. begin
  581. DebugMsg('Peephole PushPushPopPop2MovMov performed', p);
  582. taicpu(p).ops:=2;
  583. taicpu(p).opcode:=A_MOV;
  584. taicpu(hp1).ops:=2;
  585. taicpu(hp1).opcode:=A_MOV;
  586. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  587. taicpu(p).loadreg(0, taicpu(hp3).oper[0]^.reg);
  588. taicpu(hp1).loadreg(1, taicpu(hp1).oper[0]^.reg);
  589. taicpu(hp1).loadreg(0, taicpu(hp2).oper[0]^.reg);
  590. asml.Remove(hp2);
  591. hp2.Free;
  592. asml.Remove(hp3);
  593. hp3.Free;
  594. result:=true;
  595. end
  596. end;
  597. end;
  598. A_CALL:
  599. if (cs_opt_level4 in current_settings.optimizerswitches) and
  600. GetNextInstruction(p,hp1) and
  601. MatchInstruction(hp1,A_RET) then
  602. begin
  603. DebugMsg('Peephole CallReg2Jmp performed', p);
  604. taicpu(p).opcode:=A_JMP;
  605. asml.Remove(hp1);
  606. hp1.Free;
  607. result:=true;
  608. end;
  609. A_MOV:
  610. begin
  611. { turn
  612. mov reg0, reg1
  613. push reg0
  614. dealloc reg0
  615. into
  616. push reg1
  617. }
  618. if (taicpu(p).ops=2) and
  619. (taicpu(p).oper[0]^.typ = top_reg) and
  620. (taicpu(p).oper[1]^.typ = top_reg) and
  621. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  622. (not RegModifiedBetween(taicpu(p).oper[1]^.reg, p, hp1)) and
  623. (hp1.typ = ait_instruction) and
  624. (taicpu(hp1).opcode in [A_PUSH,A_MOV,A_CP,A_CPC,A_ADD,A_SUB,A_ADC,A_SBC,A_EOR,A_AND,A_OR,
  625. A_STD,A_ST,
  626. A_OUT,A_IN]) and
  627. RegInInstruction(taicpu(p).oper[0]^.reg, hp1) and
  628. (not RegModifiedByInstruction(taicpu(p).oper[0]^.reg, hp1)) and
  629. {(taicpu(hp1).ops=1) and
  630. (taicpu(hp1).oper[0]^.typ = top_reg) and
  631. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and }
  632. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  633. begin
  634. DebugMsg('Peephole MovOp2Op performed', p);
  635. for i := 0 to taicpu(hp1).ops-1 do
  636. if taicpu(hp1).oper[i]^.typ=top_reg then
  637. if taicpu(hp1).oper[i]^.reg=taicpu(p).oper[0]^.reg then
  638. taicpu(hp1).oper[i]^.reg:=taicpu(p).oper[1]^.reg;
  639. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  640. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  641. if assigned(alloc) and assigned(dealloc) then
  642. begin
  643. asml.Remove(alloc);
  644. alloc.Free;
  645. asml.Remove(dealloc);
  646. dealloc.Free;
  647. end;
  648. GetNextInstruction(p,hp1);
  649. asml.Remove(p);
  650. p.free;
  651. p:=hp1;
  652. result:=true;
  653. end
  654. { remove
  655. mov reg0,reg0
  656. }
  657. else if (taicpu(p).ops=2) and
  658. (taicpu(p).oper[0]^.typ = top_reg) and
  659. (taicpu(p).oper[1]^.typ = top_reg) and
  660. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  661. begin
  662. DebugMsg('Peephole RedundantMov performed', p);
  663. GetNextInstruction(p,hp1);
  664. asml.remove(p);
  665. p.free;
  666. p:=hp1;
  667. result:=true;
  668. end
  669. {
  670. Turn
  671. mov rx,ry
  672. op rx,rz
  673. mov ry, rx
  674. Into
  675. op ry,rz
  676. }
  677. else if (taicpu(p).ops=2) and
  678. (taicpu(p).oper[0]^.typ = top_reg) and
  679. (taicpu(p).oper[1]^.typ = top_reg) and
  680. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  681. (hp1.typ=ait_instruction) and
  682. (taicpu(hp1).ops >= 1) and
  683. (taicpu(hp1).oper[0]^.typ = top_reg) and
  684. GetNextInstructionUsingReg(hp1,hp2,taicpu(hp1).oper[0]^.reg) and
  685. (hp2.typ=ait_instruction) and
  686. (taicpu(hp2).opcode=A_MOV) and
  687. (taicpu(hp2).oper[0]^.typ = top_reg) and
  688. (taicpu(hp2).oper[1]^.typ = top_reg) and
  689. (taicpu(hp2).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  690. (taicpu(hp2).oper[1]^.reg = taicpu(hp1).oper[0]^.reg) and
  691. (taicpu(hp2).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  692. (not RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp2)) and
  693. (taicpu(hp1).opcode in [A_ADD,A_ADC,A_SUB,A_SBC,A_AND,A_OR,A_EOR,
  694. A_LSL,A_LSR,A_ASR,A_ROR,A_ROL]) and
  695. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg, tai(hp2.Next))) then
  696. begin
  697. DebugMsg('Peephole MovOpMov2Op performed', p);
  698. if (taicpu(hp1).ops=2) and
  699. (taicpu(hp1).oper[1]^.typ=top_reg) and
  700. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  701. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  702. taicpu(hp1).oper[0]^.reg:=taicpu(p).oper[1]^.reg;
  703. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  704. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp2.Next));
  705. if assigned(alloc) and assigned(dealloc) then
  706. begin
  707. asml.Remove(alloc);
  708. alloc.Free;
  709. asml.Remove(dealloc);
  710. dealloc.Free;
  711. end;
  712. GetNextInstruction(p,hp1);
  713. asml.remove(p);
  714. p.free;
  715. asml.remove(hp2);
  716. hp2.free;
  717. p:=hp1;
  718. result:=true;
  719. end
  720. {
  721. Turn
  722. mov rx,ry
  723. op rx,rw
  724. mov rw,rx
  725. Into
  726. op rw,ry
  727. }
  728. else if (taicpu(p).ops=2) and
  729. (taicpu(p).oper[0]^.typ = top_reg) and
  730. (taicpu(p).oper[1]^.typ = top_reg) and
  731. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  732. (hp1.typ=ait_instruction) and
  733. (taicpu(hp1).ops = 2) and
  734. (taicpu(hp1).oper[0]^.typ = top_reg) and
  735. (taicpu(hp1).oper[1]^.typ = top_reg) and
  736. GetNextInstructionUsingReg(hp1,hp2,taicpu(hp1).oper[0]^.reg) and
  737. (hp2.typ=ait_instruction) and
  738. (taicpu(hp2).opcode=A_MOV) and
  739. (taicpu(hp2).oper[0]^.typ = top_reg) and
  740. (taicpu(hp2).oper[1]^.typ = top_reg) and
  741. (taicpu(hp2).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  742. (taicpu(hp2).oper[1]^.reg = taicpu(hp1).oper[0]^.reg) and
  743. (taicpu(hp2).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  744. (not RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  745. (taicpu(hp1).opcode in [A_ADD,A_ADC,A_AND,A_OR,A_EOR]) and
  746. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg, tai(hp2.Next))) then
  747. begin
  748. DebugMsg('Peephole MovOpMov2Op2 performed', p);
  749. taicpu(hp1).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  750. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  751. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  752. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp2.Next));
  753. if assigned(alloc) and assigned(dealloc) then
  754. begin
  755. asml.Remove(alloc);
  756. alloc.Free;
  757. asml.Remove(dealloc);
  758. dealloc.Free;
  759. end;
  760. GetNextInstruction(p,hp1);
  761. asml.remove(p);
  762. p.free;
  763. asml.remove(hp2);
  764. hp2.free;
  765. p:=hp1;
  766. result:=true;
  767. end
  768. { fold
  769. mov reg2,reg0
  770. mov reg3,reg1
  771. to
  772. movw reg2,reg0
  773. }
  774. else if (CPUAVR_HAS_MOVW in cpu_capabilities[current_settings.cputype]) and
  775. (taicpu(p).ops=2) and
  776. (taicpu(p).oper[0]^.typ = top_reg) and
  777. (taicpu(p).oper[1]^.typ = top_reg) and
  778. getnextinstruction(p,hp1) and
  779. (hp1.typ = ait_instruction) and
  780. (taicpu(hp1).opcode = A_MOV) and
  781. (taicpu(hp1).ops=2) and
  782. (taicpu(hp1).oper[0]^.typ = top_reg) and
  783. (taicpu(hp1).oper[1]^.typ = top_reg) and
  784. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(p).oper[0]^.reg)+1) and
  785. ((getsupreg(taicpu(p).oper[0]^.reg) mod 2)=0) and
  786. ((getsupreg(taicpu(p).oper[1]^.reg) mod 2)=0) and
  787. (getsupreg(taicpu(hp1).oper[1]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)+1) then
  788. begin
  789. DebugMsg('Peephole MovMov2Movw performed', p);
  790. alloc:=FindRegAllocBackward(taicpu(hp1).oper[0]^.reg,tai(hp1.Previous));
  791. if assigned(alloc) then
  792. begin
  793. asml.Remove(alloc);
  794. asml.InsertBefore(alloc,p);
  795. end;
  796. taicpu(p).opcode:=A_MOVW;
  797. asml.remove(hp1);
  798. hp1.free;
  799. result:=true;
  800. end
  801. {
  802. This removes the first mov from
  803. mov rX,...
  804. mov rX,...
  805. }
  806. else if (hp1.typ=ait_instruction) and (taicpu(hp1).opcode=A_MOV) then
  807. while (hp1.typ=ait_instruction) and (taicpu(hp1).opcode=A_MOV) and
  808. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  809. { don't remove the first mov if the second is a mov rX,rX }
  810. not(MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^)) do
  811. begin
  812. DebugMsg('Peephole MovMov2Mov performed', p);
  813. asml.remove(p);
  814. p.free;
  815. p:=hp1;
  816. GetNextInstruction(hp1,hp1);
  817. result:=true;
  818. if not assigned(hp1) then
  819. break;
  820. end;
  821. end;
  822. A_SBIC,
  823. A_SBIS:
  824. begin
  825. {
  826. Turn
  827. sbic/sbis X, y
  828. jmp .L1
  829. op
  830. .L1:
  831. into
  832. sbis/sbic X,y
  833. op
  834. .L1:
  835. }
  836. if GetNextInstruction(p, hp1) and
  837. (hp1.typ=ait_instruction) and
  838. (taicpu(hp1).opcode in [A_JMP,A_RJMP]) and
  839. (taicpu(hp1).ops>0) and
  840. (taicpu(hp1).oper[0]^.typ = top_ref) and
  841. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  842. GetNextInstruction(hp1, hp2) and
  843. (hp2.typ=ait_instruction) and
  844. (not taicpu(hp2).is_jmp) and
  845. GetNextInstruction(hp2, hp3) and
  846. (hp3.typ=ait_label) and
  847. (taicpu(hp1).oper[0]^.ref^.symbol=tai_label(hp3).labsym) then
  848. begin
  849. DebugMsg('Peephole SbiJmp2Sbi performed',p);
  850. if taicpu(p).opcode=A_SBIC then
  851. taicpu(p).opcode:=A_SBIS
  852. else
  853. taicpu(p).opcode:=A_SBIC;
  854. tai_label(hp3).labsym.decrefs;
  855. AsmL.remove(hp1);
  856. taicpu(hp1).Free;
  857. result:=true;
  858. end
  859. {
  860. Turn
  861. sbiX X, y
  862. jmp .L1
  863. jmp .L2
  864. .L1:
  865. op
  866. .L2:
  867. into
  868. sbiX X,y
  869. .L1:
  870. op
  871. .L2:
  872. }
  873. else if GetNextInstruction(p, hp1) and
  874. (hp1.typ=ait_instruction) and
  875. (taicpu(hp1).opcode in [A_JMP,A_RJMP]) and
  876. (taicpu(hp1).ops>0) and
  877. (taicpu(hp1).oper[0]^.typ = top_ref) and
  878. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  879. GetNextInstruction(hp1, hp2) and
  880. (hp2.typ=ait_instruction) and
  881. (taicpu(hp2).opcode in [A_JMP,A_RJMP]) and
  882. (taicpu(hp2).ops>0) and
  883. (taicpu(hp2).oper[0]^.typ = top_ref) and
  884. (taicpu(hp2).oper[0]^.ref^.symbol is TAsmLabel) and
  885. GetNextInstruction(hp2, hp3) and
  886. (hp3.typ=ait_label) and
  887. (taicpu(hp1).oper[0]^.ref^.symbol=tai_label(hp3).labsym) and
  888. GetNextInstruction(hp3, hp4) and
  889. (hp4.typ=ait_instruction) and
  890. GetNextInstruction(hp4, hp5) and
  891. (hp3.typ=ait_label) and
  892. (taicpu(hp2).oper[0]^.ref^.symbol=tai_label(hp5).labsym) then
  893. begin
  894. DebugMsg('Peephole SbiJmpJmp2Sbi performed',p);
  895. tai_label(hp3).labsym.decrefs;
  896. tai_label(hp5).labsym.decrefs;
  897. AsmL.remove(hp1);
  898. taicpu(hp1).Free;
  899. AsmL.remove(hp2);
  900. taicpu(hp2).Free;
  901. result:=true;
  902. end;
  903. end;
  904. end;
  905. end;
  906. end;
  907. end;
  908. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  909. begin
  910. end;
  911. begin
  912. casmoptimizer:=TCpuAsmOptimizer;
  913. End.