aoptcpu.pas 42 KB

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