aoptcpu.pas 43 KB

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