aoptcpu.pas 37 KB

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