aoptcpu.pas 38 KB

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