aoptcpu.pas 38 KB

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