aoptcpu.pas 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. function InvertSkipInstruction(var p: tai): boolean;
  32. { uses the same constructor as TAopObj }
  33. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  34. procedure PeepHoleOptPass2;override;
  35. End;
  36. Implementation
  37. uses
  38. cutils,
  39. verbose,
  40. cpuinfo,
  41. aasmbase,aasmcpu,aasmdata,
  42. aoptutils,
  43. globals,globtype,
  44. cgutils;
  45. type
  46. TAsmOpSet = set of TAsmOp;
  47. function CanBeCond(p : tai) : boolean;
  48. begin
  49. result:=(p.typ=ait_instruction) and (taicpu(p).condition=C_None);
  50. end;
  51. function RefsEqual(const r1, r2: treference): boolean;
  52. begin
  53. refsequal :=
  54. (r1.offset = r2.offset) and
  55. (r1.base = r2.base) and
  56. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  57. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  58. (r1.relsymbol = r2.relsymbol) and
  59. (r1.addressmode = r2.addressmode);
  60. end;
  61. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  62. begin
  63. result:=oper1.typ=oper2.typ;
  64. if result then
  65. case oper1.typ of
  66. top_const:
  67. Result:=oper1.val = oper2.val;
  68. top_reg:
  69. Result:=oper1.reg = oper2.reg;
  70. top_ref:
  71. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  72. else Result:=false;
  73. end
  74. end;
  75. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  76. begin
  77. result := (oper.typ = top_reg) and (oper.reg = reg);
  78. end;
  79. function MatchInstruction(const instr: tai; const op: TAsmOp): boolean;
  80. begin
  81. result :=
  82. (instr.typ = ait_instruction) and
  83. (taicpu(instr).opcode = op);
  84. end;
  85. function MatchInstruction(const instr: tai; const ops: TAsmOpSet): boolean;
  86. begin
  87. result :=
  88. (instr.typ = ait_instruction) and
  89. (taicpu(instr).opcode in ops);
  90. end;
  91. function MatchInstruction(const instr: tai; const ops: TAsmOpSet;opcount : byte): boolean;
  92. begin
  93. result :=
  94. (instr.typ = ait_instruction) and
  95. (taicpu(instr).opcode in ops) and
  96. (taicpu(instr).ops=opcount);
  97. end;
  98. {$ifdef DEBUG_AOPTCPU}
  99. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  100. begin
  101. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  102. end;
  103. {$else DEBUG_AOPTCPU}
  104. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  105. begin
  106. end;
  107. {$endif DEBUG_AOPTCPU}
  108. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  109. begin
  110. If (p1.typ = ait_instruction) and (taicpu(p1).opcode in [A_MUL,A_MULS,A_FMUL,A_FMULS,A_FMULSU]) and
  111. ((getsupreg(reg)=RS_R0) or (getsupreg(reg)=RS_R1)) then
  112. Result:=true
  113. else if (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_MOVW) and
  114. ((TRegister(ord(taicpu(p1).oper[0]^.reg)+1)=reg) or (TRegister(ord(taicpu(p1).oper[1]^.reg)+1)=reg) or
  115. (taicpu(p1).oper[0]^.reg=reg) or (taicpu(p1).oper[1]^.reg=reg)) 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[1]^.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. {
  181. Turns
  182. sbis ?
  183. jmp .Lx
  184. op
  185. .Lx:
  186. Into
  187. sbic ?
  188. op
  189. For all types of skip instructions
  190. }
  191. function TCpuAsmOptimizer.InvertSkipInstruction(var p: tai): boolean;
  192. function GetNextInstructionWithoutLabel(p: tai; var next: tai): boolean;
  193. begin
  194. repeat
  195. result:=GetNextInstruction(p,next);
  196. p:=next;
  197. until
  198. (not result) or
  199. (not assigned(next)) or
  200. (next.typ in [ait_instruction]);
  201. result:=assigned(next) and (next.typ in [ait_instruction]);
  202. end;
  203. var
  204. hp1, hp2, hp3: tai;
  205. s: string;
  206. begin
  207. result:=false;
  208. if GetNextInstruction(taicpu(p),hp1) and
  209. (hp1.typ=ait_instruction) and
  210. (taicpu(hp1).opcode in [A_RJMP,A_JMP]) and
  211. (taicpu(hp1).ops=1) and
  212. (taicpu(hp1).oper[0]^.typ=top_ref) and
  213. (taicpu(hp1).oper[0]^.ref^.offset=0) and
  214. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  215. GetNextInstructionWithoutLabel(hp1,hp2) and
  216. (hp2.typ=ait_instruction) and
  217. (not taicpu(hp2).is_jmp) and
  218. GetNextInstruction(hp2,hp3) and
  219. FindLabel(TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol),hp3) then
  220. begin
  221. DebugMsg('SkipJump2InvertedSkip', p);
  222. case taicpu(p).opcode of
  223. A_SBIS: taicpu(p).opcode:=A_SBIC;
  224. A_SBIC: taicpu(p).opcode:=A_SBIS;
  225. A_SBRS: taicpu(p).opcode:=A_SBRC;
  226. A_SBRC: taicpu(p).opcode:=A_SBRS;
  227. end;
  228. TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
  229. asml.remove(hp1);
  230. hp1.free;
  231. end;
  232. end;
  233. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  234. var
  235. hp1,hp2,hp3,hp4,hp5: tai;
  236. alloc, dealloc: tai_regalloc;
  237. i: integer;
  238. l: TAsmLabel;
  239. TmpUsedRegs : TAllUsedRegs;
  240. begin
  241. result := false;
  242. case p.typ of
  243. ait_instruction:
  244. begin
  245. {
  246. change
  247. <op> reg,x,y
  248. cp reg,r1
  249. into
  250. <op>s reg,x,y
  251. }
  252. { this optimization can applied only to the currently enabled operations because
  253. the other operations do not update all flags and FPC does not track flag usage }
  254. if MatchInstruction(p, [A_ADC,A_ADD,A_AND,A_ANDI,A_ASR,A_COM,A_DEC,A_EOR,
  255. A_INC,A_LSL,A_LSR,
  256. A_OR,A_ORI,A_ROL,A_ROR,A_SBC,A_SBCI,A_SUB,A_SUBI]) and
  257. GetNextInstruction(p, hp1) and
  258. ((MatchInstruction(hp1, A_CP) and
  259. (((taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  260. (taicpu(hp1).oper[1]^.reg = GetDefaultZeroReg)) or
  261. ((taicpu(p).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  262. (taicpu(hp1).oper[0]^.reg = GetDefaultZeroReg) and
  263. (taicpu(p).opcode in [A_ADC,A_ADD,A_AND,A_ANDI,A_ASR,A_COM,A_EOR,
  264. A_LSL,A_LSR,
  265. A_OR,A_ORI,A_ROL,A_ROR,A_SUB,A_SBI])))) or
  266. (MatchInstruction(hp1, A_CPI) and
  267. (taicpu(p).opcode = A_ANDI) and
  268. (taicpu(p).oper[1]^.typ=top_const) and
  269. (taicpu(hp1).oper[1]^.typ=top_const) and
  270. (taicpu(p).oper[1]^.val=taicpu(hp1).oper[1]^.val))) and
  271. GetNextInstruction(hp1, hp2) and
  272. { be careful here, following instructions could use other flags
  273. however after a jump fpc never depends on the value of flags }
  274. { All above instructions set Z and N according to the following
  275. Z := result = 0;
  276. N := result[31];
  277. EQ = Z=1; NE = Z=0;
  278. MI = N=1; PL = N=0; }
  279. MatchInstruction(hp2, A_BRxx) and
  280. ((taicpu(hp2).condition in [C_EQ,C_NE,C_MI,C_PL]) or
  281. { sub/sbc set all flags }
  282. (taicpu(p).opcode in [A_SUB,A_SBI])){ and
  283. no flag allocation tracking implemented yet on avr
  284. assigned(FindRegDealloc(NR_DEFAULTFLAGS,tai(hp2.Next)))} then
  285. begin
  286. { move flag allocation if possible }
  287. { no flag allocation tracking implemented yet on avr
  288. GetLastInstruction(hp1, hp2);
  289. hp2:=FindRegAlloc(NR_DEFAULTFLAGS,tai(hp2.Next));
  290. if assigned(hp2) then
  291. begin
  292. asml.Remove(hp2);
  293. asml.insertbefore(hp2, p);
  294. end;
  295. }
  296. // If we compare to the same value we are masking then invert the comparison
  297. if (taicpu(hp1).opcode=A_CPI) or
  298. { sub/sbc with reverted? }
  299. ((taicpu(hp1).oper[0]^.reg = GetDefaultZeroReg) and (taicpu(p).opcode in [A_SUB,A_SBI])) then
  300. taicpu(hp2).condition:=inverse_cond(taicpu(hp2).condition);
  301. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  302. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,hp2), hp2);
  303. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  304. DebugMsg('Peephole OpCp2Op performed', p);
  305. asml.remove(hp1);
  306. hp1.free;
  307. Result:=true;
  308. end
  309. else
  310. case taicpu(p).opcode of
  311. A_LDI:
  312. begin
  313. { turn
  314. ldi reg0, imm
  315. cp/mov reg1, reg0
  316. dealloc reg0
  317. into
  318. cpi/ldi reg1, imm
  319. }
  320. if MatchOpType(taicpu(p),top_reg,top_const) and
  321. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  322. MatchInstruction(hp1,[A_CP,A_MOV],2) and
  323. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  324. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  325. (getsupreg(taicpu(hp1).oper[0]^.reg) in [16..31]) and
  326. (taicpu(hp1).oper[1]^.reg=taicpu(p).oper[0]^.reg) and
  327. not(MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^)) then
  328. begin
  329. CopyUsedRegs(TmpUsedRegs);
  330. if not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp1, TmpUsedRegs)) then
  331. begin
  332. case taicpu(hp1).opcode of
  333. A_CP:
  334. taicpu(hp1).opcode:=A_CPI;
  335. A_MOV:
  336. taicpu(hp1).opcode:=A_LDI;
  337. else
  338. internalerror(2016111901);
  339. end;
  340. taicpu(hp1).loadconst(1, taicpu(p).oper[1]^.val);
  341. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  342. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  343. if assigned(alloc) and assigned(dealloc) then
  344. begin
  345. asml.Remove(alloc);
  346. alloc.Free;
  347. asml.Remove(dealloc);
  348. dealloc.Free;
  349. end;
  350. DebugMsg('Peephole LdiMov/Cp2Ldi/Cpi performed', p);
  351. RemoveCurrentP(p);
  352. end;
  353. ReleaseUsedRegs(TmpUsedRegs);
  354. end;
  355. end;
  356. A_STS:
  357. if (taicpu(p).oper[0]^.ref^.symbol=nil) and
  358. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  359. (getsupreg(taicpu(p).oper[0]^.ref^.base)=RS_NO) and
  360. (getsupreg(taicpu(p).oper[0]^.ref^.index)=RS_NO) and
  361. (taicpu(p).oper[0]^.ref^.addressmode=AM_UNCHANGED) and
  362. (((CPUAVR_NOMEMMAPPED_REGS in cpu_capabilities[current_settings.cputype]) and
  363. (taicpu(p).oper[0]^.ref^.offset>=0) and
  364. (taicpu(p).oper[0]^.ref^.offset<=63)) or
  365. (not(CPUAVR_NOMEMMAPPED_REGS in cpu_capabilities[current_settings.cputype]) and
  366. (taicpu(p).oper[0]^.ref^.offset>=32) and
  367. (taicpu(p).oper[0]^.ref^.offset<=95))) then
  368. begin
  369. DebugMsg('Peephole Sts2Out performed', p);
  370. taicpu(p).opcode:=A_OUT;
  371. if CPUAVR_NOMEMMAPPED_REGS in cpu_capabilities[current_settings.cputype] then
  372. taicpu(p).loadconst(0,taicpu(p).oper[0]^.ref^.offset)
  373. else
  374. taicpu(p).loadconst(0,taicpu(p).oper[0]^.ref^.offset-32);
  375. end;
  376. A_LDS:
  377. if (taicpu(p).oper[1]^.ref^.symbol=nil) and
  378. (taicpu(p).oper[1]^.ref^.relsymbol=nil) and
  379. (getsupreg(taicpu(p).oper[1]^.ref^.base)=RS_NO) and
  380. (getsupreg(taicpu(p).oper[1]^.ref^.index)=RS_NO) and
  381. (taicpu(p).oper[1]^.ref^.addressmode=AM_UNCHANGED) and
  382. (((CPUAVR_NOMEMMAPPED_REGS in cpu_capabilities[current_settings.cputype]) and
  383. (taicpu(p).oper[1]^.ref^.offset>=0) and
  384. (taicpu(p).oper[1]^.ref^.offset<=63)) or
  385. (not(CPUAVR_NOMEMMAPPED_REGS in cpu_capabilities[current_settings.cputype]) and
  386. (taicpu(p).oper[1]^.ref^.offset>=32) and
  387. (taicpu(p).oper[1]^.ref^.offset<=95))) then
  388. begin
  389. DebugMsg('Peephole Lds2In performed', p);
  390. taicpu(p).opcode:=A_IN;
  391. if CPUAVR_NOMEMMAPPED_REGS in cpu_capabilities[current_settings.cputype] then
  392. taicpu(p).loadconst(1,taicpu(p).oper[1]^.ref^.offset)
  393. else
  394. taicpu(p).loadconst(1,taicpu(p).oper[1]^.ref^.offset-32);
  395. end;
  396. A_IN:
  397. if GetNextInstruction(p,hp1) then
  398. begin
  399. {
  400. in rX,Y
  401. ori rX,n
  402. out Y,rX
  403. into
  404. sbi rX,lg(n)
  405. }
  406. if (taicpu(p).oper[1]^.val<=31) and
  407. MatchInstruction(hp1,A_ORI) and
  408. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
  409. (PopCnt(byte(taicpu(hp1).oper[1]^.val))=1) and
  410. GetNextInstruction(hp1,hp2) and
  411. MatchInstruction(hp2,A_OUT) and
  412. MatchOperand(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  413. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) then
  414. begin
  415. DebugMsg('Peephole InOriOut2Sbi performed', p);
  416. taicpu(p).opcode:=A_SBI;
  417. taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
  418. taicpu(p).loadconst(1,BsrByte(taicpu(hp1).oper[1]^.val));
  419. asml.Remove(hp1);
  420. hp1.Free;
  421. asml.Remove(hp2);
  422. hp2.Free;
  423. result:=true;
  424. end
  425. {
  426. in rX,Y
  427. andi rX,not(n)
  428. out Y,rX
  429. into
  430. cbi rX,lg(n)
  431. }
  432. else if (taicpu(p).oper[1]^.val<=31) and
  433. MatchInstruction(hp1,A_ANDI) and
  434. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
  435. (PopCnt(byte(not(taicpu(hp1).oper[1]^.val)))=1) and
  436. GetNextInstruction(hp1,hp2) and
  437. MatchInstruction(hp2,A_OUT) and
  438. MatchOperand(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  439. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) then
  440. begin
  441. DebugMsg('Peephole InAndiOut2Cbi performed', p);
  442. taicpu(p).opcode:=A_CBI;
  443. taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
  444. taicpu(p).loadconst(1,BsrByte(not(taicpu(hp1).oper[1]^.val)));
  445. asml.Remove(hp1);
  446. hp1.Free;
  447. asml.Remove(hp2);
  448. hp2.Free;
  449. result:=true;
  450. end
  451. {
  452. in rX,Y
  453. andi rX,n
  454. breq/brne L1
  455. into
  456. sbis/sbic Y,lg(n)
  457. jmp L1
  458. .Ltemp:
  459. }
  460. else if (taicpu(p).oper[1]^.val<=31) and
  461. MatchInstruction(hp1,A_ANDI) and
  462. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
  463. (PopCnt(byte(taicpu(hp1).oper[1]^.val))=1) and
  464. GetNextInstruction(hp1,hp2) and
  465. MatchInstruction(hp2,A_BRxx) and
  466. (taicpu(hp2).condition in [C_EQ,C_NE]) then
  467. begin
  468. if taicpu(hp2).condition=C_EQ then
  469. taicpu(p).opcode:=A_SBIS
  470. else
  471. taicpu(p).opcode:=A_SBIC;
  472. DebugMsg('Peephole InAndiBrx2SbixJmp performed', p);
  473. taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
  474. taicpu(p).loadconst(1,BsrByte(taicpu(hp1).oper[1]^.val));
  475. asml.Remove(hp1);
  476. hp1.Free;
  477. taicpu(hp2).condition:=C_None;
  478. if CPUAVR_HAS_JMP_CALL in cpu_capabilities[current_settings.cputype] then
  479. taicpu(hp2).opcode:=A_JMP
  480. else
  481. taicpu(hp2).opcode:=A_RJMP;
  482. current_asmdata.getjumplabel(l);
  483. l.increfs;
  484. asml.InsertAfter(tai_label.create(l), hp2);
  485. result:=true;
  486. end;
  487. end;
  488. A_SBRS,
  489. A_SBRC:
  490. begin
  491. {
  492. Turn
  493. in rx, y
  494. sbr* rx, z
  495. Into
  496. sbi* y, z
  497. }
  498. if (taicpu(p).ops=2) and
  499. (taicpu(p).oper[0]^.typ=top_reg) and
  500. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(p.next))) and
  501. GetLastInstruction(p,hp1) and
  502. (hp1.typ=ait_instruction) and
  503. (taicpu(hp1).opcode=A_IN) and
  504. (taicpu(hp1).ops=2) and
  505. (taicpu(hp1).oper[1]^.typ=top_const) and
  506. (taicpu(hp1).oper[1]^.val in [0..31]) and
  507. MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[0]^.reg) and
  508. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, hp1, p)) then
  509. begin
  510. if taicpu(p).opcode=A_SBRS then
  511. taicpu(p).opcode:=A_SBIS
  512. else
  513. taicpu(p).opcode:=A_SBIC;
  514. taicpu(p).loadconst(0, taicpu(hp1).oper[1]^.val);
  515. DebugMsg('Peephole InSbrx2Sbix performed', p);
  516. asml.Remove(hp1);
  517. hp1.free;
  518. result:=true;
  519. end;
  520. if InvertSkipInstruction(p) then
  521. result:=true;
  522. end;
  523. A_ANDI:
  524. begin
  525. {
  526. Turn
  527. andi rx, #pow2
  528. brne l
  529. <op>
  530. l:
  531. Into
  532. sbrs rx, #(1 shl imm)
  533. <op>
  534. l:
  535. }
  536. if (taicpu(p).ops=2) and
  537. (taicpu(p).oper[1]^.typ=top_const) and
  538. ispowerof2(taicpu(p).oper[1]^.val,i) and
  539. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(p.next))) and
  540. GetNextInstruction(p,hp1) and
  541. (hp1.typ=ait_instruction) and
  542. (taicpu(hp1).opcode=A_BRxx) and
  543. (taicpu(hp1).condition in [C_EQ,C_NE]) and
  544. (taicpu(hp1).ops>0) and
  545. (taicpu(hp1).oper[0]^.typ = top_ref) and
  546. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  547. GetNextInstruction(hp1,hp2) and
  548. (hp2.typ=ait_instruction) and
  549. GetNextInstruction(hp2,hp3) and
  550. (hp3.typ=ait_label) and
  551. (taicpu(hp1).oper[0]^.ref^.symbol=tai_label(hp3).labsym) then
  552. begin
  553. DebugMsg('Peephole AndiBr2Sbr performed', p);
  554. taicpu(p).oper[1]^.val:=i;
  555. if taicpu(hp1).condition=C_NE then
  556. taicpu(p).opcode:=A_SBRS
  557. else
  558. taicpu(p).opcode:=A_SBRC;
  559. asml.Remove(hp1);
  560. hp1.free;
  561. result:=true;
  562. end
  563. {
  564. Remove
  565. andi rx, #y
  566. dealloc rx
  567. }
  568. else if (taicpu(p).ops=2) and
  569. (taicpu(p).oper[0]^.typ=top_reg) and
  570. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(p.next))) and
  571. (assigned(FindRegDeAlloc(NR_DEFAULTFLAGS,tai(p.Next))) or
  572. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs))) then
  573. begin
  574. DebugMsg('Redundant Andi removed', p);
  575. result:=RemoveCurrentP(p);
  576. end;
  577. end;
  578. A_ADD:
  579. begin
  580. if (taicpu(p).oper[1]^.reg=GetDefaultZeroReg) and
  581. GetNextInstruction(p, hp1) and
  582. MatchInstruction(hp1,A_ADC) then
  583. begin
  584. DebugMsg('Peephole AddAdc2Add performed', p);
  585. result:=RemoveCurrentP(p);
  586. end;
  587. end;
  588. A_SUB:
  589. begin
  590. if (taicpu(p).oper[1]^.reg=GetDefaultZeroReg) and
  591. GetNextInstruction(p, hp1) and
  592. MatchInstruction(hp1,A_SBC) then
  593. begin
  594. DebugMsg('Peephole SubSbc2Sub performed', p);
  595. taicpu(hp1).opcode:=A_SUB;
  596. result:=RemoveCurrentP(p);
  597. end;
  598. end;
  599. A_CLR:
  600. begin
  601. { turn the common
  602. clr rX
  603. mov/ld rX, rY
  604. into
  605. mov/ld rX, rY
  606. }
  607. if (taicpu(p).ops=1) and
  608. (taicpu(p).oper[0]^.typ=top_reg) and
  609. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  610. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  611. (hp1.typ=ait_instruction) and
  612. (taicpu(hp1).opcode in [A_MOV,A_LD]) and
  613. (taicpu(hp1).ops>0) and
  614. (taicpu(hp1).oper[0]^.typ=top_reg) and
  615. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) then
  616. begin
  617. DebugMsg('Peephole ClrMov2Mov performed', p);
  618. result:=RemoveCurrentP(p);
  619. end
  620. { turn
  621. clr rX
  622. ...
  623. adc rY, rX
  624. into
  625. ...
  626. adc rY, r1
  627. }
  628. else if (taicpu(p).ops=1) and
  629. (taicpu(p).oper[0]^.typ=top_reg) and
  630. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  631. (not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)) and
  632. (hp1.typ=ait_instruction) and
  633. (taicpu(hp1).opcode in [A_ADC,A_SBC]) and
  634. (taicpu(hp1).ops=2) and
  635. (taicpu(hp1).oper[1]^.typ=top_reg) and
  636. (taicpu(hp1).oper[1]^.reg=taicpu(p).oper[0]^.reg) and
  637. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[0]^.reg) and
  638. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  639. begin
  640. DebugMsg('Peephole ClrAdc2Adc performed', p);
  641. taicpu(hp1).oper[1]^.reg:=GetDefaultZeroReg;
  642. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  643. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  644. if assigned(alloc) and assigned(dealloc) then
  645. begin
  646. asml.Remove(alloc);
  647. alloc.Free;
  648. asml.Remove(dealloc);
  649. dealloc.Free;
  650. end;
  651. result:=RemoveCurrentP(p);
  652. end;
  653. end;
  654. A_PUSH:
  655. begin
  656. { turn
  657. push reg0
  658. push reg1
  659. pop reg3
  660. pop reg2
  661. into
  662. movw reg2,reg0
  663. or
  664. mov reg3,reg1
  665. mov reg2,reg0
  666. }
  667. if GetNextInstruction(p,hp1) and
  668. MatchInstruction(hp1,A_PUSH) and
  669. GetNextInstruction(hp1,hp2) and
  670. MatchInstruction(hp2,A_POP) and
  671. GetNextInstruction(hp2,hp3) and
  672. MatchInstruction(hp3,A_POP) then
  673. begin
  674. if (CPUAVR_HAS_MOVW in cpu_capabilities[current_settings.cputype]) and
  675. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(p).oper[0]^.reg)+1) and
  676. ((getsupreg(taicpu(p).oper[0]^.reg) mod 2)=0) and
  677. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp3).oper[0]^.reg)+1) and
  678. ((getsupreg(taicpu(hp3).oper[0]^.reg) mod 2)=0) then
  679. begin
  680. DebugMsg('Peephole PushPushPopPop2Movw performed', p);
  681. taicpu(hp3).ops:=2;
  682. taicpu(hp3).opcode:=A_MOVW;
  683. taicpu(hp3).loadreg(1, taicpu(p).oper[0]^.reg);
  684. RemoveCurrentP(p);
  685. RemoveCurrentP(p);
  686. result:=RemoveCurrentP(p);
  687. end
  688. else
  689. begin
  690. DebugMsg('Peephole PushPushPopPop2MovMov performed', p);
  691. taicpu(p).ops:=2;
  692. taicpu(p).opcode:=A_MOV;
  693. taicpu(hp1).ops:=2;
  694. taicpu(hp1).opcode:=A_MOV;
  695. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  696. taicpu(p).loadreg(0, taicpu(hp3).oper[0]^.reg);
  697. taicpu(hp1).loadreg(1, taicpu(hp1).oper[0]^.reg);
  698. taicpu(hp1).loadreg(0, taicpu(hp2).oper[0]^.reg);
  699. { life range of reg2 and reg3 is increased, fix register allocation entries }
  700. CopyUsedRegs(TmpUsedRegs);
  701. UpdateUsedRegs(TmpUsedRegs,tai(p.Next));
  702. AllocRegBetween(taicpu(hp2).oper[0]^.reg,hp1,hp2,TmpUsedRegs);
  703. ReleaseUsedRegs(TmpUsedRegs);
  704. CopyUsedRegs(TmpUsedRegs);
  705. AllocRegBetween(taicpu(hp3).oper[0]^.reg,p,hp3,TmpUsedRegs);
  706. ReleaseUsedRegs(TmpUsedRegs);
  707. IncludeRegInUsedRegs(taicpu(hp3).oper[0]^.reg,UsedRegs);
  708. UpdateUsedRegs(tai(p.Next));
  709. asml.Remove(hp2);
  710. hp2.Free;
  711. asml.Remove(hp3);
  712. hp3.Free;
  713. result:=true;
  714. end
  715. end;
  716. end;
  717. A_CALL:
  718. if (cs_opt_level4 in current_settings.optimizerswitches) and
  719. GetNextInstruction(p,hp1) and
  720. MatchInstruction(hp1,A_RET) then
  721. begin
  722. DebugMsg('Peephole CallReg2Jmp performed', p);
  723. taicpu(p).opcode:=A_JMP;
  724. asml.Remove(hp1);
  725. hp1.Free;
  726. result:=true;
  727. end;
  728. A_RCALL:
  729. if (cs_opt_level4 in current_settings.optimizerswitches) and
  730. GetNextInstruction(p,hp1) and
  731. MatchInstruction(hp1,A_RET) then
  732. begin
  733. DebugMsg('Peephole RCallReg2RJmp performed', p);
  734. taicpu(p).opcode:=A_RJMP;
  735. asml.Remove(hp1);
  736. hp1.Free;
  737. result:=true;
  738. end;
  739. A_MOV:
  740. begin
  741. { change
  742. mov reg0, reg1
  743. dealloc reg0
  744. into
  745. dealloc reg0
  746. }
  747. if MatchOpType(taicpu(p),top_reg,top_reg) then
  748. begin
  749. CopyUsedRegs(TmpUsedRegs);
  750. UpdateUsedRegs(TmpUsedRegs,tai(p.Next));
  751. if not(RegInUsedRegs(taicpu(p).oper[0]^.reg,TmpUsedRegs)) and
  752. { reg. allocation information before calls is not perfect, so don't do this before
  753. calls/icalls }
  754. GetNextInstruction(p,hp1) and
  755. not(MatchInstruction(hp1,[A_CALL,A_RCALL])) then
  756. begin
  757. DebugMsg('Peephole Mov2Nop performed', p);
  758. result:=RemoveCurrentP(p);
  759. ReleaseUsedRegs(TmpUsedRegs);
  760. exit;
  761. end;
  762. ReleaseUsedRegs(TmpUsedRegs);
  763. end;
  764. { turn
  765. mov reg0, reg1
  766. <op> reg2,reg0
  767. dealloc reg0
  768. into
  769. <op> reg2,reg1
  770. }
  771. if MatchOpType(taicpu(p),top_reg,top_reg) and
  772. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  773. (not RegModifiedBetween(taicpu(p).oper[1]^.reg, p, hp1)) and
  774. (MatchInstruction(hp1,[A_PUSH,A_MOV,A_CP,A_CPC,A_ADD,A_SUB,A_ADC,A_SBC,A_EOR,A_AND,A_OR,
  775. A_OUT,A_IN]) or
  776. { the reference register of ST/STD cannot be replaced }
  777. (MatchInstruction(hp1,[A_STD,A_ST,A_STS]) and (MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^)))) and
  778. (not RegModifiedByInstruction(taicpu(p).oper[0]^.reg, hp1)) and
  779. {(taicpu(hp1).ops=1) and
  780. (taicpu(hp1).oper[0]^.typ = top_reg) and
  781. (taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg) and }
  782. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  783. begin
  784. DebugMsg('Peephole MovOp2Op performed', p);
  785. for i := 0 to taicpu(hp1).ops-1 do
  786. if taicpu(hp1).oper[i]^.typ=top_reg then
  787. if taicpu(hp1).oper[i]^.reg=taicpu(p).oper[0]^.reg then
  788. taicpu(hp1).oper[i]^.reg:=taicpu(p).oper[1]^.reg;
  789. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  790. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  791. if assigned(alloc) and assigned(dealloc) then
  792. begin
  793. asml.Remove(alloc);
  794. alloc.Free;
  795. asml.Remove(dealloc);
  796. dealloc.Free;
  797. end;
  798. { life range of reg1 is increased }
  799. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,usedregs);
  800. { p will be removed, update used register as we continue
  801. with the next instruction after p }
  802. result:=RemoveCurrentP(p);
  803. end
  804. { remove
  805. mov reg0,reg0
  806. }
  807. else if (taicpu(p).ops=2) and
  808. (taicpu(p).oper[0]^.typ = top_reg) and
  809. (taicpu(p).oper[1]^.typ = top_reg) and
  810. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  811. begin
  812. DebugMsg('Peephole RedundantMov performed', p);
  813. result:=RemoveCurrentP(p);
  814. end
  815. {
  816. Turn
  817. mov rx,ry
  818. op rx,rz
  819. mov ry, rx
  820. Into
  821. op ry,rz
  822. }
  823. else if (taicpu(p).ops=2) and
  824. MatchOpType(taicpu(p),top_reg,top_reg) and
  825. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  826. (hp1.typ=ait_instruction) and
  827. (taicpu(hp1).ops >= 1) and
  828. (taicpu(hp1).oper[0]^.typ = top_reg) and
  829. GetNextInstructionUsingReg(hp1,hp2,taicpu(hp1).oper[0]^.reg) and
  830. MatchInstruction(hp2,A_MOV) and
  831. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  832. (taicpu(hp2).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  833. (taicpu(hp2).oper[1]^.reg = taicpu(hp1).oper[0]^.reg) and
  834. (taicpu(hp2).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  835. (not RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp2)) and
  836. (taicpu(hp1).opcode in [A_ADD,A_ADC,A_SUB,A_SBC,A_AND,A_OR,A_EOR,
  837. A_INC,A_DEC,
  838. A_LSL,A_LSR,A_ASR,A_ROR,A_ROL]) and
  839. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg, tai(hp2.Next))) then
  840. begin
  841. DebugMsg('Peephole MovOpMov2Op performed', p);
  842. if (taicpu(hp1).ops=2) and
  843. (taicpu(hp1).oper[1]^.typ=top_reg) and
  844. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  845. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  846. taicpu(hp1).oper[0]^.reg:=taicpu(p).oper[1]^.reg;
  847. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  848. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp2.Next));
  849. if assigned(alloc) and assigned(dealloc) then
  850. begin
  851. asml.Remove(alloc);
  852. alloc.Free;
  853. asml.Remove(dealloc);
  854. dealloc.Free;
  855. end;
  856. asml.remove(hp2);
  857. hp2.free;
  858. result:=RemoveCurrentP(p);
  859. end
  860. {
  861. Turn
  862. mov rx,ry
  863. op rx,rw
  864. mov rw,rx
  865. Into
  866. op rw,ry
  867. }
  868. else if (taicpu(p).ops=2) and
  869. MatchOpType(taicpu(p),top_reg,top_reg) and
  870. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  871. (hp1.typ=ait_instruction) and
  872. (taicpu(hp1).ops = 2) and
  873. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  874. GetNextInstructionUsingReg(hp1,hp2,taicpu(hp1).oper[0]^.reg) and
  875. (hp2.typ=ait_instruction) and
  876. (taicpu(hp2).opcode=A_MOV) and
  877. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  878. (taicpu(hp2).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  879. (taicpu(hp2).oper[1]^.reg = taicpu(hp1).oper[0]^.reg) and
  880. (taicpu(hp2).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  881. (not RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  882. (taicpu(hp1).opcode in [A_ADD,A_ADC,A_AND,A_OR,A_EOR]) and
  883. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg, tai(hp2.Next))) then
  884. begin
  885. DebugMsg('Peephole MovOpMov2Op2 performed', p);
  886. taicpu(hp1).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  887. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  888. alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.Previous));
  889. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp2.Next));
  890. if assigned(alloc) and assigned(dealloc) then
  891. begin
  892. asml.Remove(alloc);
  893. alloc.Free;
  894. asml.Remove(dealloc);
  895. dealloc.Free;
  896. end;
  897. result:=RemoveCurrentP(p);
  898. asml.remove(hp2);
  899. hp2.free;
  900. end
  901. { fold
  902. mov reg2,reg0
  903. mov reg3,reg1
  904. to
  905. movw reg2,reg0
  906. }
  907. else if (CPUAVR_HAS_MOVW in cpu_capabilities[current_settings.cputype]) and
  908. (taicpu(p).ops=2) and
  909. (taicpu(p).oper[0]^.typ = top_reg) and
  910. (taicpu(p).oper[1]^.typ = top_reg) and
  911. getnextinstruction(p,hp1) and
  912. (hp1.typ = ait_instruction) and
  913. (taicpu(hp1).opcode = A_MOV) and
  914. (taicpu(hp1).ops=2) and
  915. (taicpu(hp1).oper[0]^.typ = top_reg) and
  916. (taicpu(hp1).oper[1]^.typ = top_reg) and
  917. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(p).oper[0]^.reg)+1) and
  918. ((getsupreg(taicpu(p).oper[0]^.reg) mod 2)=0) and
  919. ((getsupreg(taicpu(p).oper[1]^.reg) mod 2)=0) and
  920. (getsupreg(taicpu(hp1).oper[1]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)+1) then
  921. begin
  922. DebugMsg('Peephole MovMov2Movw performed', p);
  923. alloc:=FindRegAllocBackward(taicpu(hp1).oper[0]^.reg,tai(hp1.Previous));
  924. if assigned(alloc) then
  925. begin
  926. asml.Remove(alloc);
  927. asml.InsertBefore(alloc,p);
  928. { proper book keeping of currently used registers }
  929. IncludeRegInUsedRegs(taicpu(hp1).oper[0]^.reg,UsedRegs);
  930. end;
  931. taicpu(p).opcode:=A_MOVW;
  932. asml.remove(hp1);
  933. hp1.free;
  934. result:=true;
  935. end
  936. {
  937. This removes the first mov from
  938. mov rX,...
  939. mov rX,...
  940. }
  941. else if GetNextInstruction(p,hp1) and MatchInstruction(hp1,A_MOV) then
  942. while MatchInstruction(hp1,A_MOV) and
  943. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  944. { don't remove the first mov if the second is a mov rX,rX }
  945. not(MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^)) do
  946. begin
  947. DebugMsg('Peephole MovMov2Mov performed', p);
  948. result:=RemoveCurrentP(p);
  949. GetNextInstruction(hp1,hp1);
  950. if not assigned(hp1) then
  951. break;
  952. end;
  953. end;
  954. A_SBIC,
  955. A_SBIS:
  956. begin
  957. {
  958. Turn
  959. sbic/sbis X, y
  960. jmp .L1
  961. op
  962. .L1:
  963. into
  964. sbis/sbic X,y
  965. op
  966. .L1:
  967. }
  968. if InvertSkipInstruction(p) then
  969. result:=true
  970. {
  971. Turn
  972. sbiX X, y
  973. jmp .L1
  974. jmp .L2
  975. .L1:
  976. op
  977. .L2:
  978. into
  979. sbiX X,y
  980. .L1:
  981. op
  982. .L2:
  983. }
  984. else if GetNextInstruction(p, hp1) and
  985. (hp1.typ=ait_instruction) and
  986. (taicpu(hp1).opcode in [A_JMP,A_RJMP]) and
  987. (taicpu(hp1).ops>0) and
  988. (taicpu(hp1).oper[0]^.typ = top_ref) and
  989. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  990. GetNextInstruction(hp1, hp2) and
  991. (hp2.typ=ait_instruction) and
  992. (taicpu(hp2).opcode in [A_JMP,A_RJMP]) and
  993. (taicpu(hp2).ops>0) and
  994. (taicpu(hp2).oper[0]^.typ = top_ref) and
  995. (taicpu(hp2).oper[0]^.ref^.symbol is TAsmLabel) and
  996. GetNextInstruction(hp2, hp3) and
  997. (hp3.typ=ait_label) and
  998. (taicpu(hp1).oper[0]^.ref^.symbol=tai_label(hp3).labsym) and
  999. GetNextInstruction(hp3, hp4) and
  1000. (hp4.typ=ait_instruction) and
  1001. GetNextInstruction(hp4, hp5) and
  1002. (hp3.typ=ait_label) and
  1003. (taicpu(hp2).oper[0]^.ref^.symbol=tai_label(hp5).labsym) then
  1004. begin
  1005. DebugMsg('Peephole SbiJmpJmp2Sbi performed',p);
  1006. tai_label(hp3).labsym.decrefs;
  1007. tai_label(hp5).labsym.decrefs;
  1008. AsmL.remove(hp1);
  1009. taicpu(hp1).Free;
  1010. AsmL.remove(hp2);
  1011. taicpu(hp2).Free;
  1012. result:=true;
  1013. end;
  1014. end;
  1015. end;
  1016. end;
  1017. end;
  1018. end;
  1019. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  1020. begin
  1021. end;
  1022. begin
  1023. casmoptimizer:=TCpuAsmOptimizer;
  1024. End.