aoptcpu.pas 48 KB

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