aoptcpu.pas 41 KB

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