aoptcpu.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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) then
  265. begin
  266. CopyUsedRegs(TmpUsedRegs);
  267. if not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp1, TmpUsedRegs)) then
  268. begin
  269. case taicpu(hp1).opcode of
  270. A_CP:
  271. taicpu(hp1).opcode:=A_CPI;
  272. A_MOV:
  273. taicpu(hp1).opcode:=A_LDI;
  274. else
  275. internalerror(2016111901);
  276. end;
  277. taicpu(hp1).loadconst(1, taicpu(p).oper[1]^.val);
  278. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  279. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  280. if assigned(alloc) and assigned(dealloc) then
  281. begin
  282. asml.Remove(alloc);
  283. alloc.Free;
  284. asml.Remove(dealloc);
  285. dealloc.Free;
  286. end;
  287. DebugMsg('Peephole LdiMov/Cp2Ldi/Cpi performed', p);
  288. GetNextInstruction(p,hp1);
  289. asml.Remove(p);
  290. p.Free;
  291. p:=hp1;
  292. result:=true;
  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_CLR:
  475. begin
  476. { turn the common
  477. clr rX
  478. mov/ld rX, rY
  479. into
  480. mov/ld rX, rY
  481. }
  482. if (taicpu(p).ops=1) and
  483. (taicpu(p).oper[0]^.typ=top_reg) and
  484. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  485. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  486. (hp1.typ=ait_instruction) and
  487. (taicpu(hp1).opcode in [A_MOV,A_LD]) and
  488. (taicpu(hp1).ops>0) and
  489. (taicpu(hp1).oper[0]^.typ=top_reg) and
  490. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) then
  491. begin
  492. DebugMsg('Peephole ClrMov2Mov performed', p);
  493. asml.Remove(p);
  494. p.Free;
  495. p:=hp1;
  496. result:=true;
  497. end
  498. { turn
  499. clr rX
  500. ...
  501. adc rY, rX
  502. into
  503. ...
  504. adc rY, r1
  505. }
  506. else if (taicpu(p).ops=1) and
  507. (taicpu(p).oper[0]^.typ=top_reg) and
  508. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  509. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  510. (hp1.typ=ait_instruction) and
  511. (taicpu(hp1).opcode in [A_ADC,A_SBC]) and
  512. (taicpu(hp1).ops=2) and
  513. (taicpu(hp1).oper[1]^.typ=top_reg) and
  514. (taicpu(hp1).oper[1]^.reg=taicpu(p).oper[0]^.reg) and
  515. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[0]^.reg) and
  516. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  517. begin
  518. DebugMsg('Peephole ClrAdc2Adc performed', p);
  519. taicpu(hp1).oper[1]^.reg:=NR_R1;
  520. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  521. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  522. if assigned(alloc) and assigned(dealloc) then
  523. begin
  524. asml.Remove(alloc);
  525. alloc.Free;
  526. asml.Remove(dealloc);
  527. dealloc.Free;
  528. end;
  529. GetNextInstruction(p,hp1);
  530. asml.Remove(p);
  531. p.free;
  532. p:=hp1;
  533. result:=true;
  534. end;
  535. end;
  536. A_PUSH:
  537. begin
  538. { turn
  539. push reg0
  540. push reg1
  541. pop reg3
  542. pop reg2
  543. into
  544. movw reg2,reg0
  545. or
  546. mov reg3,reg1
  547. mov reg2,reg0
  548. }
  549. if GetNextInstruction(p,hp1) and
  550. MatchInstruction(hp1,A_PUSH) and
  551. GetNextInstruction(hp1,hp2) and
  552. MatchInstruction(hp2,A_POP) and
  553. GetNextInstruction(hp2,hp3) and
  554. MatchInstruction(hp3,A_POP) then
  555. begin
  556. if (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(p).oper[0]^.reg)+1) and
  557. ((getsupreg(taicpu(p).oper[0]^.reg) mod 2)=0) and
  558. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp3).oper[0]^.reg)+1) and
  559. ((getsupreg(taicpu(hp3).oper[0]^.reg) mod 2)=0) then
  560. begin
  561. DebugMsg('Peephole PushPushPopPop2Movw performed', p);
  562. taicpu(p).ops:=2;
  563. taicpu(p).opcode:=A_MOVW;
  564. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  565. taicpu(p).loadreg(0, taicpu(hp3).oper[0]^.reg);
  566. asml.Remove(hp1);
  567. hp1.Free;
  568. asml.Remove(hp2);
  569. hp2.Free;
  570. asml.Remove(hp3);
  571. hp3.Free;
  572. result:=true;
  573. end
  574. else
  575. begin
  576. DebugMsg('Peephole PushPushPopPop2MovMov performed', p);
  577. taicpu(p).ops:=2;
  578. taicpu(p).opcode:=A_MOV;
  579. taicpu(hp1).ops:=2;
  580. taicpu(hp1).opcode:=A_MOV;
  581. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  582. taicpu(p).loadreg(0, taicpu(hp3).oper[0]^.reg);
  583. taicpu(hp1).loadreg(1, taicpu(hp1).oper[0]^.reg);
  584. taicpu(hp1).loadreg(0, taicpu(hp2).oper[0]^.reg);
  585. asml.Remove(hp2);
  586. hp2.Free;
  587. asml.Remove(hp3);
  588. hp3.Free;
  589. result:=true;
  590. end
  591. end;
  592. end;
  593. A_CALL:
  594. if (cs_opt_level4 in current_settings.optimizerswitches) and
  595. GetNextInstruction(p,hp1) and
  596. MatchInstruction(hp1,A_RET) then
  597. begin
  598. DebugMsg('Peephole CallReg2Jmp performed', p);
  599. taicpu(p).opcode:=A_JMP;
  600. asml.Remove(hp1);
  601. hp1.Free;
  602. result:=true;
  603. end;
  604. A_MOV:
  605. begin
  606. { turn
  607. mov reg0, reg1
  608. push reg0
  609. dealloc reg0
  610. into
  611. push reg1
  612. }
  613. if (taicpu(p).ops=2) and
  614. (taicpu(p).oper[0]^.typ = top_reg) and
  615. (taicpu(p).oper[1]^.typ = top_reg) and
  616. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  617. (not RegModifiedBetween(taicpu(p).oper[1]^.reg, p, hp1)) and
  618. (hp1.typ = ait_instruction) and
  619. (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,
  620. A_STD,A_ST,
  621. A_OUT,A_IN]) and
  622. RegInInstruction(taicpu(p).oper[0]^.reg, hp1) and
  623. (not RegModifiedByInstruction(taicpu(p).oper[0]^.reg, hp1)) and
  624. {(taicpu(hp1).ops=1) and
  625. (taicpu(hp1).oper[0]^.typ = top_reg) and
  626. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and }
  627. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  628. begin
  629. DebugMsg('Peephole MovOp2Op performed', p);
  630. for i := 0 to taicpu(hp1).ops-1 do
  631. if taicpu(hp1).oper[i]^.typ=top_reg then
  632. if taicpu(hp1).oper[i]^.reg=taicpu(p).oper[0]^.reg then
  633. taicpu(hp1).oper[i]^.reg:=taicpu(p).oper[1]^.reg;
  634. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  635. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  636. if assigned(alloc) and assigned(dealloc) then
  637. begin
  638. asml.Remove(alloc);
  639. alloc.Free;
  640. asml.Remove(dealloc);
  641. dealloc.Free;
  642. end;
  643. GetNextInstruction(p,hp1);
  644. asml.Remove(p);
  645. p.free;
  646. p:=hp1;
  647. result:=true;
  648. end
  649. { remove
  650. mov reg0,reg0
  651. }
  652. else if (taicpu(p).ops=2) and
  653. (taicpu(p).oper[0]^.typ = top_reg) and
  654. (taicpu(p).oper[1]^.typ = top_reg) and
  655. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  656. begin
  657. DebugMsg('Peephole RedundantMov performed', p);
  658. GetNextInstruction(p,hp1);
  659. asml.remove(p);
  660. p.free;
  661. p:=hp1;
  662. result:=true;
  663. end
  664. {
  665. Turn
  666. mov rx,ry
  667. op rx,rz
  668. mov ry, rx
  669. Into
  670. op ry,rz
  671. }
  672. else if (taicpu(p).ops=2) and
  673. (taicpu(p).oper[0]^.typ = top_reg) and
  674. (taicpu(p).oper[1]^.typ = top_reg) and
  675. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  676. (hp1.typ=ait_instruction) and
  677. (taicpu(hp1).ops >= 1) and
  678. (taicpu(hp1).oper[0]^.typ = top_reg) and
  679. GetNextInstructionUsingReg(hp1,hp2,taicpu(hp1).oper[0]^.reg) and
  680. (hp2.typ=ait_instruction) and
  681. (taicpu(hp2).opcode=A_MOV) and
  682. (taicpu(hp2).oper[0]^.typ = top_reg) and
  683. (taicpu(hp2).oper[1]^.typ = top_reg) and
  684. (taicpu(hp2).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  685. (taicpu(hp2).oper[1]^.reg = taicpu(hp1).oper[0]^.reg) and
  686. (taicpu(hp2).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  687. (not RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp2)) and
  688. (taicpu(hp1).opcode in [A_ADD,A_ADC,A_SUB,A_SBC,A_AND,A_OR,A_EOR,
  689. A_LSL,A_LSR,A_ASR,A_ROR,A_ROL]) and
  690. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg, tai(hp2.Next))) then
  691. begin
  692. DebugMsg('Peephole MovOpMov2Op performed', p);
  693. if (taicpu(hp1).ops=2) and
  694. (taicpu(hp1).oper[1]^.typ=top_reg) and
  695. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  696. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  697. taicpu(hp1).oper[0]^.reg:=taicpu(p).oper[1]^.reg;
  698. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  699. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp2.Next));
  700. if assigned(alloc) and assigned(dealloc) then
  701. begin
  702. asml.Remove(alloc);
  703. alloc.Free;
  704. asml.Remove(dealloc);
  705. dealloc.Free;
  706. end;
  707. GetNextInstruction(p,hp1);
  708. asml.remove(p);
  709. p.free;
  710. asml.remove(hp2);
  711. hp2.free;
  712. p:=hp1;
  713. result:=true;
  714. end
  715. {
  716. Turn
  717. mov rx,ry
  718. op rx,rw
  719. mov rw,rx
  720. Into
  721. op rw,ry
  722. }
  723. else if (taicpu(p).ops=2) and
  724. (taicpu(p).oper[0]^.typ = top_reg) and
  725. (taicpu(p).oper[1]^.typ = top_reg) and
  726. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  727. (hp1.typ=ait_instruction) and
  728. (taicpu(hp1).ops = 2) and
  729. (taicpu(hp1).oper[0]^.typ = top_reg) and
  730. (taicpu(hp1).oper[1]^.typ = top_reg) and
  731. GetNextInstructionUsingReg(hp1,hp2,taicpu(hp1).oper[0]^.reg) and
  732. (hp2.typ=ait_instruction) and
  733. (taicpu(hp2).opcode=A_MOV) and
  734. (taicpu(hp2).oper[0]^.typ = top_reg) and
  735. (taicpu(hp2).oper[1]^.typ = top_reg) and
  736. (taicpu(hp2).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  737. (taicpu(hp2).oper[1]^.reg = taicpu(hp1).oper[0]^.reg) and
  738. (taicpu(hp2).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  739. (not RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  740. (taicpu(hp1).opcode in [A_ADD,A_ADC,A_AND,A_OR,A_EOR]) and
  741. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg, tai(hp2.Next))) then
  742. begin
  743. DebugMsg('Peephole MovOpMov2Op2 performed', p);
  744. taicpu(hp1).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  745. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  746. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  747. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp2.Next));
  748. if assigned(alloc) and assigned(dealloc) then
  749. begin
  750. asml.Remove(alloc);
  751. alloc.Free;
  752. asml.Remove(dealloc);
  753. dealloc.Free;
  754. end;
  755. GetNextInstruction(p,hp1);
  756. asml.remove(p);
  757. p.free;
  758. asml.remove(hp2);
  759. hp2.free;
  760. p:=hp1;
  761. result:=true;
  762. end
  763. { fold
  764. mov reg2,reg0
  765. mov reg3,reg1
  766. to
  767. movw reg2,reg0
  768. }
  769. else if (CPUAVR_HAS_MOVW in cpu_capabilities[current_settings.cputype]) and
  770. (taicpu(p).ops=2) and
  771. (taicpu(p).oper[0]^.typ = top_reg) and
  772. (taicpu(p).oper[1]^.typ = top_reg) and
  773. getnextinstruction(p,hp1) and
  774. (hp1.typ = ait_instruction) and
  775. (taicpu(hp1).opcode = A_MOV) and
  776. (taicpu(hp1).ops=2) and
  777. (taicpu(hp1).oper[0]^.typ = top_reg) and
  778. (taicpu(hp1).oper[1]^.typ = top_reg) and
  779. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(p).oper[0]^.reg)+1) and
  780. ((getsupreg(taicpu(p).oper[0]^.reg) mod 2)=0) and
  781. ((getsupreg(taicpu(p).oper[1]^.reg) mod 2)=0) and
  782. (getsupreg(taicpu(hp1).oper[1]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)+1) then
  783. begin
  784. DebugMsg('Peephole MovMov2Movw performed', p);
  785. alloc:=FindRegAllocBackward(taicpu(hp1).oper[0]^.reg,tai(hp1.Previous));
  786. if assigned(alloc) then
  787. begin
  788. asml.Remove(alloc);
  789. asml.InsertBefore(alloc,p);
  790. end;
  791. taicpu(p).opcode:=A_MOVW;
  792. asml.remove(hp1);
  793. hp1.free;
  794. result:=true;
  795. end
  796. {
  797. This removes the first mov from
  798. mov rX,...
  799. mov rX,...
  800. }
  801. else if (hp1.typ=ait_instruction) and (taicpu(hp1).opcode=A_MOV) then
  802. while (hp1.typ=ait_instruction) and (taicpu(hp1).opcode=A_MOV) and
  803. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  804. { don't remove the first mov if the second is a mov rX,rX }
  805. not(MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^)) do
  806. begin
  807. DebugMsg('Peephole MovMov2Mov performed', p);
  808. asml.remove(p);
  809. p.free;
  810. p:=hp1;
  811. GetNextInstruction(hp1,hp1);
  812. result:=true;
  813. if not assigned(hp1) then
  814. break;
  815. end;
  816. end;
  817. A_SBIC,
  818. A_SBIS:
  819. begin
  820. {
  821. Turn
  822. sbic/sbis X, y
  823. jmp .L1
  824. op
  825. .L1:
  826. into
  827. sbis/sbic X,y
  828. op
  829. .L1:
  830. }
  831. if GetNextInstruction(p, hp1) and
  832. (hp1.typ=ait_instruction) and
  833. (taicpu(hp1).opcode in [A_JMP,A_RJMP]) and
  834. (taicpu(hp1).ops>0) and
  835. (taicpu(hp1).oper[0]^.typ = top_ref) and
  836. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  837. GetNextInstruction(hp1, hp2) and
  838. (hp2.typ=ait_instruction) and
  839. (not taicpu(hp2).is_jmp) and
  840. GetNextInstruction(hp2, hp3) and
  841. (hp3.typ=ait_label) and
  842. (taicpu(hp1).oper[0]^.ref^.symbol=tai_label(hp3).labsym) then
  843. begin
  844. DebugMsg('Peephole SbiJmp2Sbi performed',p);
  845. if taicpu(p).opcode=A_SBIC then
  846. taicpu(p).opcode:=A_SBIS
  847. else
  848. taicpu(p).opcode:=A_SBIC;
  849. tai_label(hp3).labsym.decrefs;
  850. AsmL.remove(hp1);
  851. taicpu(hp1).Free;
  852. result:=true;
  853. end
  854. {
  855. Turn
  856. sbiX X, y
  857. jmp .L1
  858. jmp .L2
  859. .L1:
  860. op
  861. .L2:
  862. into
  863. sbiX X,y
  864. .L1:
  865. op
  866. .L2:
  867. }
  868. else if GetNextInstruction(p, hp1) and
  869. (hp1.typ=ait_instruction) and
  870. (taicpu(hp1).opcode in [A_JMP,A_RJMP]) and
  871. (taicpu(hp1).ops>0) and
  872. (taicpu(hp1).oper[0]^.typ = top_ref) and
  873. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  874. GetNextInstruction(hp1, hp2) and
  875. (hp2.typ=ait_instruction) and
  876. (taicpu(hp2).opcode in [A_JMP,A_RJMP]) and
  877. (taicpu(hp2).ops>0) and
  878. (taicpu(hp2).oper[0]^.typ = top_ref) and
  879. (taicpu(hp2).oper[0]^.ref^.symbol is TAsmLabel) and
  880. GetNextInstruction(hp2, hp3) and
  881. (hp3.typ=ait_label) and
  882. (taicpu(hp1).oper[0]^.ref^.symbol=tai_label(hp3).labsym) and
  883. GetNextInstruction(hp3, hp4) and
  884. (hp4.typ=ait_instruction) and
  885. GetNextInstruction(hp4, hp5) and
  886. (hp3.typ=ait_label) and
  887. (taicpu(hp2).oper[0]^.ref^.symbol=tai_label(hp5).labsym) then
  888. begin
  889. DebugMsg('Peephole SbiJmpJmp2Sbi performed',p);
  890. tai_label(hp3).labsym.decrefs;
  891. tai_label(hp5).labsym.decrefs;
  892. AsmL.remove(hp1);
  893. taicpu(hp1).Free;
  894. AsmL.remove(hp2);
  895. taicpu(hp2).Free;
  896. result:=true;
  897. end;
  898. end;
  899. end;
  900. end;
  901. end;
  902. end;
  903. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  904. begin
  905. end;
  906. begin
  907. casmoptimizer:=TCpuAsmOptimizer;
  908. End.