2
0

aoptcpu.pas 49 KB

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