aoptcpu.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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. Interface
  21. uses cgbase, cpubase, aasmtai, aopt, aoptcpub, aoptobj;
  22. Type
  23. { TCpuAsmOptimizer }
  24. TCpuAsmOptimizer = class(TAsmOptimizer)
  25. { uses the same constructor as TAopObj }
  26. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  27. procedure PeepHoleOptPass2;override;
  28. Function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override;
  29. procedure RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  30. function RegUsedAfterInstruction(reg: Tregister; p: tai;
  31. var AllUsedRegs: TAllUsedRegs): Boolean;
  32. End;
  33. TCpuPreRegallocScheduler = class(TAsmOptimizer)
  34. function PeepHoleOptPass1Cpu(var p: tai): boolean;override;
  35. end;
  36. TCpuThumb2AsmOptimizer = class(TCpuAsmOptimizer)
  37. { uses the same constructor as TAopObj }
  38. procedure PeepHoleOptPass2;override;
  39. End;
  40. Implementation
  41. uses
  42. cutils,verbose,globals,
  43. systems,
  44. cpuinfo,
  45. cgutils,procinfo,
  46. aasmbase,aasmdata,aasmcpu;
  47. function CanBeCond(p : tai) : boolean;
  48. begin
  49. result:=
  50. (p.typ=ait_instruction) and
  51. (taicpu(p).condition=C_None) and
  52. (taicpu(p).opcode<>A_PLD) and
  53. ((taicpu(p).opcode<>A_BLX) or
  54. (taicpu(p).oper[0]^.typ=top_reg));
  55. end;
  56. function RefsEqual(const r1, r2: treference): boolean;
  57. begin
  58. refsequal :=
  59. (r1.offset = r2.offset) and
  60. (r1.base = r2.base) and
  61. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  62. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  63. (r1.relsymbol = r2.relsymbol) and
  64. (r1.signindex = r2.signindex) and
  65. (r1.shiftimm = r2.shiftimm) and
  66. (r1.addressmode = r2.addressmode) and
  67. (r1.shiftmode = r2.shiftmode);
  68. end;
  69. function MatchInstruction(const instr: tai; const op: TAsmOp; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  70. begin
  71. result :=
  72. (instr.typ = ait_instruction) and
  73. (taicpu(instr).opcode = op) and
  74. ((cond = []) or (taicpu(instr).condition in cond)) and
  75. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  76. end;
  77. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  78. begin
  79. result := oper1.typ = oper2.typ;
  80. if result then
  81. case oper1.typ of
  82. top_const:
  83. Result:=oper1.val = oper2.val;
  84. top_reg:
  85. Result:=oper1.reg = oper2.reg;
  86. top_conditioncode:
  87. Result:=oper1.cc = oper2.cc;
  88. top_ref:
  89. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  90. else Result:=false;
  91. end
  92. end;
  93. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  94. begin
  95. result := (oper.typ = top_reg) and (oper.reg = reg);
  96. end;
  97. procedure RemoveRedundantMove(const cmpp: tai; movp: tai; asml: TAsmList);
  98. begin
  99. if (taicpu(movp).condition = C_EQ) and
  100. (taicpu(cmpp).oper[0]^.reg = taicpu(movp).oper[0]^.reg) and
  101. (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then
  102. begin
  103. asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp);
  104. asml.remove(movp);
  105. movp.free;
  106. end;
  107. end;
  108. function regLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  109. var
  110. p: taicpu;
  111. begin
  112. p := taicpu(hp);
  113. regLoadedWithNewValue := false;
  114. if not ((assigned(hp)) and (hp.typ = ait_instruction)) then
  115. exit;
  116. case p.opcode of
  117. { These operands do not write into a register at all }
  118. A_CMP, A_CMN, A_TST, A_TEQ, A_B, A_BL, A_BX, A_BLX, A_SWI, A_MSR, A_PLD:
  119. exit;
  120. {Take care of post/preincremented store and loads, they will change their base register}
  121. A_STR, A_LDR:
  122. regLoadedWithNewValue :=
  123. (taicpu(p).oper[1]^.typ=top_ref) and
  124. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  125. (taicpu(p).oper[1]^.ref^.base = reg);
  126. { These four are writing into the first 2 register, UMLAL and SMLAL will also read from them }
  127. A_UMLAL, A_UMULL, A_SMLAL, A_SMULL:
  128. regLoadedWithNewValue :=
  129. (p.oper[1]^.typ = top_reg) and
  130. (p.oper[1]^.reg = reg);
  131. {Loads to oper2 from coprocessor}
  132. {
  133. MCR/MRC is currently not supported in FPC
  134. A_MRC:
  135. regLoadedWithNewValue :=
  136. (p.oper[2]^.typ = top_reg) and
  137. (p.oper[2]^.reg = reg);
  138. }
  139. {Loads to all register in the registerset}
  140. A_LDM:
  141. regLoadedWithNewValue := (getsupreg(reg) in p.oper[1]^.regset^);
  142. end;
  143. if regLoadedWithNewValue then
  144. exit;
  145. case p.oper[0]^.typ of
  146. {This is the case}
  147. top_reg:
  148. regLoadedWithNewValue := (p.oper[0]^.reg = reg) or
  149. { LDRD }
  150. (p.opcode=A_LDR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg));
  151. {LDM/STM might write a new value to their index register}
  152. top_ref:
  153. regLoadedWithNewValue :=
  154. (taicpu(p).oper[0]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  155. (taicpu(p).oper[0]^.ref^.base = reg);
  156. end;
  157. end;
  158. function AlignedToQWord(const ref : treference) : boolean;
  159. begin
  160. { (safe) heuristics to ensure alignment }
  161. result:=(target_info.abi in [abi_eabi,abi_armeb,abi_eabihf]) and
  162. (((ref.offset>=0) and
  163. ((ref.offset mod 8)=0) and
  164. ((ref.base=NR_R13) or
  165. (ref.index=NR_R13))
  166. ) or
  167. ((ref.offset<=0) and
  168. { when using NR_R11, it has always a value of <qword align>+4 }
  169. ((abs(ref.offset+4) mod 8)=0) and
  170. (current_procinfo.framepointer=NR_R11) and
  171. ((ref.base=NR_R11) or
  172. (ref.index=NR_R11))
  173. )
  174. );
  175. end;
  176. function instructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  177. var
  178. p: taicpu;
  179. i: longint;
  180. begin
  181. instructionLoadsFromReg := false;
  182. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  183. exit;
  184. p:=taicpu(hp);
  185. i:=1;
  186. {For these instructions we have to start on oper[0]}
  187. if (p.opcode in [A_STR, A_LDM, A_STM, A_PLD,
  188. A_CMP, A_CMN, A_TST, A_TEQ,
  189. A_B, A_BL, A_BX, A_BLX,
  190. A_SMLAL, A_UMLAL]) then i:=0;
  191. while(i<p.ops) do
  192. begin
  193. case p.oper[I]^.typ of
  194. top_reg:
  195. instructionLoadsFromReg := (p.oper[I]^.reg = reg) or
  196. { STRD }
  197. ((i=0) and (p.opcode=A_STR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg)));
  198. top_regset:
  199. instructionLoadsFromReg := (getsupreg(reg) in p.oper[I]^.regset^);
  200. top_shifterop:
  201. instructionLoadsFromReg := p.oper[I]^.shifterop^.rs = reg;
  202. top_ref:
  203. instructionLoadsFromReg :=
  204. (p.oper[I]^.ref^.base = reg) or
  205. (p.oper[I]^.ref^.index = reg);
  206. end;
  207. if instructionLoadsFromReg then exit; {Bailout if we found something}
  208. Inc(I);
  209. end;
  210. end;
  211. function TCpuAsmOptimizer.RegUsedAfterInstruction(reg: Tregister; p: tai;
  212. var AllUsedRegs: TAllUsedRegs): Boolean;
  213. begin
  214. AllUsedRegs[getregtype(reg)].Update(tai(p.Next));
  215. RegUsedAfterInstruction :=
  216. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  217. not(regLoadedWithNewValue(reg,p)) and
  218. (
  219. not(GetNextInstruction(p,p)) or
  220. instructionLoadsFromReg(reg,p) or
  221. not(regLoadedWithNewValue(reg,p))
  222. );
  223. end;
  224. procedure TCpuAsmOptimizer.RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  225. var
  226. TmpUsedRegs: TAllUsedRegs;
  227. begin
  228. if MatchInstruction(movp, A_MOV, [taicpu(p).condition], [PF_None]) and
  229. (taicpu(movp).ops=2) and {We can't optimize if there is a shiftop}
  230. MatchOperand(taicpu(movp).oper[1]^, taicpu(p).oper[0]^.reg) and
  231. {There is a special requirement for MUL and MLA, oper[0] and oper[1] are not allowed to be the same}
  232. not (
  233. (taicpu(p).opcode in [A_MLA, A_MUL]) and
  234. (taicpu(p).oper[1]^.reg = taicpu(movp).oper[0]^.reg)
  235. ) then
  236. begin
  237. CopyUsedRegs(TmpUsedRegs);
  238. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  239. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,movp,TmpUsedRegs)) then
  240. begin
  241. asml.insertbefore(tai_comment.Create(strpnew('Peephole '+optimizer+' removed superfluous mov')), movp);
  242. taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
  243. asml.remove(movp);
  244. movp.free;
  245. end;
  246. ReleaseUsedRegs(TmpUsedRegs);
  247. end;
  248. end;
  249. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  250. var
  251. hp1,hp2: tai;
  252. i, i2: longint;
  253. TmpUsedRegs: TAllUsedRegs;
  254. tempop: tasmop;
  255. function IsPowerOf2(const value: DWord): boolean; inline;
  256. begin
  257. Result:=(value and (value - 1)) = 0;
  258. end;
  259. begin
  260. result := false;
  261. case p.typ of
  262. ait_instruction:
  263. begin
  264. (* optimization proved not to be safe, see tw4768.pp
  265. {
  266. change
  267. <op> reg,x,y
  268. cmp reg,#0
  269. into
  270. <op>s reg,x,y
  271. }
  272. { this optimization can applied only to the currently enabled operations because
  273. the other operations do not update all flags and FPC does not track flag usage }
  274. if (taicpu(p).opcode in [A_ADC,A_ADD,A_SUB {A_UDIV,A_SDIV,A_MUL,A_MVN,A_MOV,A_ORR,A_EOR,A_AND}]) and
  275. (taicpu(p).oppostfix = PF_None) and
  276. (taicpu(p).condition = C_None) and
  277. GetNextInstruction(p, hp1) and
  278. MatchInstruction(hp1, A_CMP, [C_None], [PF_None]) and
  279. (taicpu(hp1).oper[1]^.typ = top_const) and
  280. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  281. (taicpu(hp1).oper[1]^.val = 0) { and
  282. GetNextInstruction(hp1, hp2) and
  283. (tai(hp2).typ = ait_instruction) and
  284. // be careful here, following instructions could use other flags
  285. // however after a jump fpc never depends on the value of flags
  286. (taicpu(hp2).opcode = A_B) and
  287. (taicpu(hp2).condition in [C_EQ,C_NE,C_MI,C_PL])} then
  288. begin
  289. taicpu(p).oppostfix:=PF_S;
  290. asml.remove(hp1);
  291. hp1.free;
  292. end
  293. else
  294. *)
  295. case taicpu(p).opcode of
  296. A_STR:
  297. begin
  298. { change
  299. str reg1,ref
  300. ldr reg2,ref
  301. into
  302. str reg1,ref
  303. mov reg2,reg1
  304. }
  305. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  306. (taicpu(p).oppostfix=PF_None) and
  307. GetNextInstruction(p,hp1) and
  308. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [PF_None]) and
  309. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  310. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  311. begin
  312. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  313. begin
  314. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov 1 done')), hp1);
  315. asml.remove(hp1);
  316. hp1.free;
  317. end
  318. else
  319. begin
  320. taicpu(hp1).opcode:=A_MOV;
  321. taicpu(hp1).oppostfix:=PF_None;
  322. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  323. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov 2 done')), hp1);
  324. end;
  325. result := true;
  326. end
  327. { change
  328. str reg1,ref
  329. str reg2,ref
  330. into
  331. strd reg1,ref
  332. }
  333. else if (CPUARM_HAS_LDRDSTRD in cpu_capabilities[current_settings.cputype]) and
  334. (taicpu(p).oppostfix=PF_None) and
  335. (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  336. GetNextInstruction(p,hp1) and
  337. MatchInstruction(hp1, A_STR, [taicpu(p).condition, C_None], [PF_None]) and
  338. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  339. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  340. { str ensures that either base or index contain no register, else ldr wouldn't
  341. use an offset either
  342. }
  343. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  344. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  345. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  346. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  347. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  348. begin
  349. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrStr2Strd done')), p);
  350. taicpu(p).oppostfix:=PF_D;
  351. asml.remove(hp1);
  352. hp1.free;
  353. end;
  354. end;
  355. A_LDR:
  356. begin
  357. { change
  358. ldr reg1,ref
  359. ldr reg2,ref
  360. into ...
  361. }
  362. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  363. GetNextInstruction(p,hp1) and
  364. { ldrd is not allowed here }
  365. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [taicpu(p).oppostfix,PF_None]-[PF_D]) then
  366. begin
  367. {
  368. ...
  369. ldr reg1,ref
  370. mov reg2,reg1
  371. }
  372. if RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  373. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  374. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  375. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  376. begin
  377. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  378. begin
  379. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2Ldr done')), hp1);
  380. asml.remove(hp1);
  381. hp1.free;
  382. end
  383. else
  384. begin
  385. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2LdrMov done')), hp1);
  386. taicpu(hp1).opcode:=A_MOV;
  387. taicpu(hp1).oppostfix:=PF_None;
  388. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  389. end;
  390. result := true;
  391. end
  392. {
  393. ...
  394. ldrd reg1,ref
  395. }
  396. else if (CPUARM_HAS_LDRDSTRD in cpu_capabilities[current_settings.cputype]) and
  397. { ldrd does not allow any postfixes ... }
  398. (taicpu(p).oppostfix=PF_None) and
  399. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  400. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  401. { ldr ensures that either base or index contain no register, else ldr wouldn't
  402. use an offset either
  403. }
  404. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  405. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  406. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  407. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  408. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  409. begin
  410. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2Ldrd done')), p);
  411. taicpu(p).oppostfix:=PF_D;
  412. asml.remove(hp1);
  413. hp1.free;
  414. end;
  415. end;
  416. { Remove superfluous mov after ldr
  417. changes
  418. ldr reg1, ref
  419. mov reg2, reg1
  420. to
  421. ldr reg2, ref
  422. conditions are:
  423. * no ldrd usage
  424. * reg1 must be released after mov
  425. * mov can not contain shifterops
  426. * ldr+mov have the same conditions
  427. * mov does not set flags
  428. }
  429. if (taicpu(p).oppostfix<>PF_D) and GetNextInstruction(p, hp1) then
  430. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr');
  431. end;
  432. A_MOV:
  433. begin
  434. { fold
  435. mov reg1,reg0, shift imm1
  436. mov reg1,reg1, shift imm2
  437. }
  438. if (taicpu(p).ops=3) and
  439. (taicpu(p).oper[2]^.typ = top_shifterop) and
  440. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  441. getnextinstruction(p,hp1) and
  442. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  443. (taicpu(hp1).ops=3) and
  444. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  445. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  446. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  447. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) then
  448. begin
  449. { fold
  450. mov reg1,reg0, lsl 16
  451. mov reg1,reg1, lsr 16
  452. strh reg1, ...
  453. dealloc reg1
  454. to
  455. strh reg1, ...
  456. dealloc reg1
  457. }
  458. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  459. (taicpu(p).oper[2]^.shifterop^.shiftimm=16) and
  460. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ASR]) and
  461. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=16) and
  462. getnextinstruction(hp1,hp2) and
  463. MatchInstruction(hp2, A_STR, [taicpu(p).condition], [PF_H]) and
  464. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^.reg) then
  465. begin
  466. CopyUsedRegs(TmpUsedRegs);
  467. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  468. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  469. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp2,TmpUsedRegs)) then
  470. begin
  471. asml.insertbefore(tai_comment.Create(strpnew('Peephole optimizer removed superfluous 16 Bit zero extension')), hp1);
  472. taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
  473. asml.remove(p);
  474. asml.remove(hp1);
  475. p.free;
  476. hp1.free;
  477. p:=hp2;
  478. end;
  479. ReleaseUsedRegs(TmpUsedRegs);
  480. end
  481. { fold
  482. mov reg1,reg0, shift imm1
  483. mov reg1,reg1, shift imm2
  484. to
  485. mov reg1,reg0, shift imm1+imm2
  486. }
  487. else if (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) or
  488. { asr makes no use after a lsr, the asr can be foled into the lsr }
  489. ((taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSR) and (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_ASR) ) then
  490. begin
  491. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  492. { avoid overflows }
  493. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  494. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  495. SM_ROR:
  496. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  497. SM_ASR:
  498. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  499. SM_LSR,
  500. SM_LSL:
  501. begin
  502. hp1:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  503. InsertLLItem(p.previous, p.next, hp1);
  504. p.free;
  505. p:=hp1;
  506. end;
  507. else
  508. internalerror(2008072803);
  509. end;
  510. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShift2Shift 1 done')), p);
  511. asml.remove(hp1);
  512. hp1.free;
  513. result := true;
  514. end
  515. { fold
  516. mov reg1,reg0, shift imm1
  517. mov reg1,reg1, shift imm2
  518. mov reg1,reg1, shift imm3 ...
  519. }
  520. else if getnextinstruction(hp1,hp2) and
  521. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  522. (taicpu(hp2).ops=3) and
  523. MatchOperand(taicpu(hp2).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  524. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  525. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  526. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) then
  527. begin
  528. { mov reg1,reg0, lsl imm1
  529. mov reg1,reg1, lsr/asr imm2
  530. mov reg1,reg1, lsl imm3 ...
  531. if imm3<=imm1 and imm2>=imm3
  532. to
  533. mov reg1,reg0, lsl imm1
  534. mov reg1,reg1, lsr/asr imm2-imm3
  535. }
  536. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  537. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  538. (taicpu(hp2).oper[2]^.shifterop^.shiftimm<=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  539. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(hp2).oper[2]^.shifterop^.shiftimm) then
  540. begin
  541. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
  542. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShiftShift2ShiftShift 1 done')), p);
  543. asml.remove(hp2);
  544. hp2.free;
  545. result := true;
  546. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  547. begin
  548. asml.remove(hp1);
  549. hp1.free;
  550. end;
  551. end
  552. { mov reg1,reg0, lsr/asr imm1
  553. mov reg1,reg1, lsl imm2
  554. mov reg1,reg1, lsr/asr imm3 ...
  555. if imm3>=imm1 and imm2>=imm1
  556. to
  557. mov reg1,reg0, lsl imm2-imm1
  558. mov reg1,reg1, lsr/asr imm3 ...
  559. }
  560. else if (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  561. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  562. (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  563. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) then
  564. begin
  565. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
  566. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  567. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShiftShift2ShiftShift 2 done')), p);
  568. asml.remove(p);
  569. p.free;
  570. p:=hp2;
  571. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  572. begin
  573. taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
  574. asml.remove(hp1);
  575. hp1.free;
  576. p:=hp2;
  577. end;
  578. result := true;
  579. end;
  580. end;
  581. end;
  582. { Change the common
  583. mov r0, r0, lsr #24
  584. and r0, r0, #255
  585. and remove the superfluous and
  586. This could be extended to handle more cases.
  587. }
  588. if (taicpu(p).ops=3) and
  589. (taicpu(p).oper[2]^.typ = top_shifterop) and
  590. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  591. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  592. (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  593. getnextinstruction(p,hp1) and
  594. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  595. (taicpu(hp1).ops=3) and
  596. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  597. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  598. (taicpu(hp1).oper[2]^.typ = top_const) and
  599. { Check if the AND actually would only mask out bits beeing already zero because of the shift
  600. For LSR #25 and an AndConst of 255 that whould go like this:
  601. 255 and ((2 shl (32-25))-1)
  602. which results in 127, which is one less a power-of-2, meaning all lower bits are set.
  603. LSR #25 and AndConst of 254:
  604. 254 and ((2 shl (32-25))-1) = 126 -> lowest bit is clear, so we can't remove it.
  605. }
  606. ispowerof2((taicpu(hp1).oper[2]^.val and ((2 shl (32-taicpu(p).oper[2]^.shifterop^.shiftimm))-1))+1) then
  607. begin
  608. asml.insertbefore(tai_comment.Create(strpnew('Peephole LsrAnd2Lsr done')), hp1);
  609. asml.remove(hp1);
  610. hp1.free;
  611. end;
  612. {
  613. optimize
  614. mov rX, yyyy
  615. ....
  616. }
  617. if (taicpu(p).ops = 2) and
  618. GetNextInstruction(p,hp1) and
  619. (tai(hp1).typ = ait_instruction) then
  620. begin
  621. {
  622. This changes the very common
  623. mov r0, #0
  624. str r0, [...]
  625. mov r0, #0
  626. str r0, [...]
  627. and removes all superfluous mov instructions
  628. }
  629. if (taicpu(p).oper[1]^.typ = top_const) and
  630. (taicpu(hp1).opcode=A_STR) then
  631. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], [PF_None]) and
  632. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  633. GetNextInstruction(hp1, hp2) and
  634. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  635. (taicpu(hp2).ops = 2) and
  636. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  637. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  638. begin
  639. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovStrMov done')), hp2);
  640. GetNextInstruction(hp2,hp1);
  641. asml.remove(hp2);
  642. hp2.free;
  643. if not assigned(hp1) then break;
  644. end
  645. {
  646. This removes the first mov from
  647. mov rX,...
  648. mov rX,...
  649. }
  650. else if taicpu(hp1).opcode=A_MOV then
  651. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  652. (taicpu(hp1).ops = 2) and
  653. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) do
  654. begin
  655. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovMov done')), p);
  656. asml.remove(p);
  657. p.free;
  658. p:=hp1;
  659. GetNextInstruction(hp1,hp1);
  660. if not assigned(hp1) then
  661. break;
  662. end;
  663. end;
  664. {
  665. change
  666. mov r1, r0
  667. add r1, r1, #1
  668. to
  669. add r1, r0, #1
  670. Todo: Make it work for mov+cmp too
  671. CAUTION! If this one is successful p might not be a mov instruction anymore!
  672. }
  673. if (taicpu(p).ops = 2) and
  674. (taicpu(p).oper[1]^.typ = top_reg) and
  675. (taicpu(p).oppostfix = PF_NONE) and
  676. GetNextInstruction(p, hp1) and
  677. (tai(hp1).typ = ait_instruction) and
  678. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  679. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN]) and
  680. {MOV and MVN might only have 2 ops}
  681. (taicpu(hp1).ops = 3) and
  682. (taicpu(hp1).condition in [C_NONE, taicpu(hp1).condition]) and
  683. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  684. (taicpu(hp1).oper[1]^.typ = top_reg) and
  685. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop]) then
  686. begin
  687. { When we get here we still don't know if the registers match}
  688. for I:=1 to 2 do
  689. {
  690. If the first loop was successful p will be replaced with hp1.
  691. The checks will still be ok, because all required information
  692. will also be in hp1 then.
  693. }
  694. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  695. begin
  696. asml.insertbefore(tai_comment.Create(strpnew('Peephole RedundantMovProcess done')), hp1);
  697. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  698. if p<>hp1 then
  699. begin
  700. asml.remove(p);
  701. p.free;
  702. p:=hp1;
  703. end;
  704. end;
  705. end;
  706. { This folds shifterops into following instructions
  707. mov r0, r1, lsl #8
  708. add r2, r3, r0
  709. to
  710. add r2, r3, r1, lsl #8
  711. CAUTION! If this one is successful p might not be a mov instruction anymore!
  712. }
  713. if (taicpu(p).opcode = A_MOV) and
  714. (taicpu(p).ops = 3) and
  715. (taicpu(p).oper[1]^.typ = top_reg) and
  716. (taicpu(p).oper[2]^.typ = top_shifterop) and
  717. (taicpu(p).oppostfix = PF_NONE) and
  718. GetNextInstruction(p, hp1) and
  719. (tai(hp1).typ = ait_instruction) and
  720. (taicpu(hp1).ops >= 2) and {Currently we can't fold into another shifterop}
  721. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  722. (taicpu(hp1).oppostfix = PF_NONE) and
  723. (taicpu(hp1).condition = taicpu(p).condition) and
  724. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  725. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  726. A_CMP, A_CMN]) and
  727. (
  728. {Only ONE of the two src operands is allowed to match}
  729. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  730. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  731. ) then
  732. begin
  733. CopyUsedRegs(TmpUsedRegs);
  734. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  735. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  736. I2:=0
  737. else
  738. I2:=1;
  739. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp1,TmpUsedRegs)) then
  740. for I:=I2 to taicpu(hp1).ops-1 do
  741. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  742. begin
  743. { If the parameter matched on the second op from the RIGHT
  744. we have to switch the parameters, this will not happen for CMP
  745. were we're only evaluating the most right parameter
  746. }
  747. if I <> taicpu(hp1).ops-1 then
  748. begin
  749. {The SUB operators need to be changed when we swap parameters}
  750. case taicpu(hp1).opcode of
  751. A_SUB: tempop:=A_RSB;
  752. A_SBC: tempop:=A_RSC;
  753. A_RSB: tempop:=A_SUB;
  754. A_RSC: tempop:=A_SBC;
  755. else tempop:=taicpu(hp1).opcode;
  756. end;
  757. if taicpu(hp1).ops = 3 then
  758. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  759. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  760. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  761. else
  762. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  763. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  764. taicpu(p).oper[2]^.shifterop^);
  765. end
  766. else
  767. if taicpu(hp1).ops = 3 then
  768. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  769. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  770. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  771. else
  772. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  773. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  774. taicpu(p).oper[2]^.shifterop^);
  775. asml.insertbefore(hp2, p);
  776. asml.remove(p);
  777. asml.remove(hp1);
  778. p.free;
  779. hp1.free;
  780. p:=hp2;
  781. GetNextInstruction(p,hp1);
  782. asml.insertbefore(tai_comment.Create(strpnew('Peephole FoldShiftProcess done')), p);
  783. break;
  784. end;
  785. ReleaseUsedRegs(TmpUsedRegs);
  786. end;
  787. {
  788. Often we see shifts and then a superfluous mov to another register
  789. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  790. }
  791. if (taicpu(p).opcode = A_MOV) and
  792. GetNextInstruction(p, hp1) then
  793. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  794. end;
  795. A_ADD,
  796. A_ADC,
  797. A_RSB,
  798. A_RSC,
  799. A_SUB,
  800. A_SBC,
  801. A_AND,
  802. A_BIC,
  803. A_EOR,
  804. A_ORR,
  805. A_MLA,
  806. A_MUL:
  807. begin
  808. {
  809. change
  810. and reg2,reg1,const1
  811. and reg2,reg2,const2
  812. to
  813. and reg2,reg1,(const1 and const2)
  814. }
  815. if (taicpu(p).opcode = A_AND) and
  816. (taicpu(p).oper[1]^.typ = top_reg) and
  817. (taicpu(p).oper[2]^.typ = top_const) and
  818. GetNextInstruction(p, hp1) and
  819. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  820. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  821. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  822. (taicpu(hp1).oper[2]^.typ = top_const) then
  823. begin
  824. asml.insertbefore(tai_comment.Create(strpnew('Peephole AndAnd2And done')), p);
  825. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  826. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  827. asml.remove(hp1);
  828. hp1.free;
  829. end;
  830. {
  831. change
  832. add reg1, ...
  833. mov reg2, reg1
  834. to
  835. add reg2, ...
  836. }
  837. if GetNextInstruction(p, hp1) then
  838. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  839. end;
  840. A_CMP:
  841. begin
  842. {
  843. change
  844. cmp reg,const1
  845. moveq reg,const1
  846. movne reg,const2
  847. to
  848. cmp reg,const1
  849. movne reg,const2
  850. }
  851. if (taicpu(p).oper[1]^.typ = top_const) and
  852. GetNextInstruction(p, hp1) and
  853. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  854. (taicpu(hp1).oper[1]^.typ = top_const) and
  855. GetNextInstruction(hp1, hp2) and
  856. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  857. (taicpu(hp1).oper[1]^.typ = top_const) then
  858. begin
  859. RemoveRedundantMove(p, hp1, asml);
  860. RemoveRedundantMove(p, hp2, asml);
  861. end;
  862. end;
  863. end;
  864. end;
  865. end;
  866. end;
  867. { instructions modifying the CPSR can be only the last instruction }
  868. function MustBeLast(p : tai) : boolean;
  869. begin
  870. Result:=(p.typ=ait_instruction) and
  871. ((taicpu(p).opcode in [A_BL,A_BLX,A_CMP,A_CMN,A_SWI,A_TEQ,A_TST,A_CMF,A_CMFE {,A_MSR}]) or
  872. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  873. (taicpu(p).oppostfix=PF_S));
  874. end;
  875. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  876. var
  877. p,hp1,hp2: tai;
  878. l : longint;
  879. condition : tasmcond;
  880. hp3: tai;
  881. WasLast: boolean;
  882. { UsedRegs, TmpUsedRegs: TRegSet; }
  883. begin
  884. p := BlockStart;
  885. { UsedRegs := []; }
  886. while (p <> BlockEnd) Do
  887. begin
  888. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  889. case p.Typ Of
  890. Ait_Instruction:
  891. begin
  892. case taicpu(p).opcode Of
  893. A_B:
  894. if taicpu(p).condition<>C_None then
  895. begin
  896. { check for
  897. Bxx xxx
  898. <several instructions>
  899. xxx:
  900. }
  901. l:=0;
  902. WasLast:=False;
  903. GetNextInstruction(p, hp1);
  904. while assigned(hp1) and
  905. (l<=4) and
  906. CanBeCond(hp1) and
  907. { stop on labels }
  908. not(hp1.typ=ait_label) do
  909. begin
  910. inc(l);
  911. if MustBeLast(hp1) then
  912. begin
  913. WasLast:=True;
  914. GetNextInstruction(hp1,hp1);
  915. break;
  916. end
  917. else
  918. GetNextInstruction(hp1,hp1);
  919. end;
  920. if assigned(hp1) then
  921. begin
  922. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  923. begin
  924. if (l<=4) and (l>0) then
  925. begin
  926. condition:=inverse_cond(taicpu(p).condition);
  927. hp2:=p;
  928. GetNextInstruction(p,hp1);
  929. p:=hp1;
  930. repeat
  931. if hp1.typ=ait_instruction then
  932. taicpu(hp1).condition:=condition;
  933. if MustBeLast(hp1) then
  934. begin
  935. GetNextInstruction(hp1,hp1);
  936. break;
  937. end
  938. else
  939. GetNextInstruction(hp1,hp1);
  940. until not(assigned(hp1)) or
  941. not(CanBeCond(hp1)) or
  942. (hp1.typ=ait_label);
  943. { wait with removing else GetNextInstruction could
  944. ignore the label if it was the only usage in the
  945. jump moved away }
  946. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  947. asml.remove(hp2);
  948. hp2.free;
  949. continue;
  950. end;
  951. end
  952. else
  953. { do not perform further optimizations if there is inctructon
  954. in block #1 which can not be optimized.
  955. }
  956. if not WasLast then
  957. begin
  958. { check further for
  959. Bcc xxx
  960. <several instructions 1>
  961. B yyy
  962. xxx:
  963. <several instructions 2>
  964. yyy:
  965. }
  966. { hp2 points to jmp yyy }
  967. hp2:=hp1;
  968. { skip hp1 to xxx }
  969. GetNextInstruction(hp1, hp1);
  970. if assigned(hp2) and
  971. assigned(hp1) and
  972. (l<=3) and
  973. (hp2.typ=ait_instruction) and
  974. (taicpu(hp2).is_jmp) and
  975. (taicpu(hp2).condition=C_None) and
  976. { real label and jump, no further references to the
  977. label are allowed }
  978. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  979. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  980. begin
  981. l:=0;
  982. { skip hp1 to <several moves 2> }
  983. GetNextInstruction(hp1, hp1);
  984. while assigned(hp1) and
  985. CanBeCond(hp1) do
  986. begin
  987. inc(l);
  988. GetNextInstruction(hp1, hp1);
  989. end;
  990. { hp1 points to yyy: }
  991. if assigned(hp1) and
  992. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  993. begin
  994. condition:=inverse_cond(taicpu(p).condition);
  995. GetNextInstruction(p,hp1);
  996. hp3:=p;
  997. p:=hp1;
  998. repeat
  999. if hp1.typ=ait_instruction then
  1000. taicpu(hp1).condition:=condition;
  1001. GetNextInstruction(hp1,hp1);
  1002. until not(assigned(hp1)) or
  1003. not(CanBeCond(hp1));
  1004. { hp2 is still at jmp yyy }
  1005. GetNextInstruction(hp2,hp1);
  1006. { hp2 is now at xxx: }
  1007. condition:=inverse_cond(condition);
  1008. GetNextInstruction(hp1,hp1);
  1009. { hp1 is now at <several movs 2> }
  1010. repeat
  1011. taicpu(hp1).condition:=condition;
  1012. GetNextInstruction(hp1,hp1);
  1013. until not(assigned(hp1)) or
  1014. not(CanBeCond(hp1)) or
  1015. (hp1.typ=ait_label);
  1016. {
  1017. asml.remove(hp1.next)
  1018. hp1.next.free;
  1019. asml.remove(hp1);
  1020. hp1.free;
  1021. }
  1022. { remove Bcc }
  1023. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  1024. asml.remove(hp3);
  1025. hp3.free;
  1026. { remove jmp }
  1027. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1028. asml.remove(hp2);
  1029. hp2.free;
  1030. continue;
  1031. end;
  1032. end;
  1033. end;
  1034. end;
  1035. end;
  1036. end;
  1037. end;
  1038. end;
  1039. p := tai(p.next)
  1040. end;
  1041. end;
  1042. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  1043. begin
  1044. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  1045. Result:=true
  1046. else
  1047. Result:=inherited RegInInstruction(Reg, p1);
  1048. end;
  1049. const
  1050. { set of opcode which might or do write to memory }
  1051. { TODO : extend armins.dat to contain r/w info }
  1052. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  1053. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  1054. function TCpuPreRegallocScheduler.PeepHoleOptPass1Cpu(var p: tai): boolean;
  1055. { TODO : schedule also forward }
  1056. { TODO : schedule distance > 1 }
  1057. var
  1058. hp1,hp2,hp3,hp4,hp5 : tai;
  1059. list : TAsmList;
  1060. begin
  1061. result:=true;
  1062. list:=TAsmList.Create;
  1063. p := BlockStart;
  1064. { UsedRegs := []; }
  1065. while (p <> BlockEnd) Do
  1066. begin
  1067. if (p.typ=ait_instruction) and
  1068. GetNextInstruction(p,hp1) and
  1069. (hp1.typ=ait_instruction) and
  1070. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  1071. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  1072. not(RegModifiedByInstruction(NR_PC,p)) and
  1073. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH])
  1074. ) or
  1075. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  1076. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  1077. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  1078. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  1079. (taicpu(hp1).oper[1]^.ref^.offset=0)
  1080. )
  1081. ) or
  1082. { try to prove that the memory accesses don't overlapp }
  1083. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  1084. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  1085. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  1086. (taicpu(p).oppostfix=PF_None) and
  1087. (taicpu(hp1).oppostfix=PF_None) and
  1088. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  1089. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1090. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  1091. (abs(taicpu(p).oper[1]^.ref^.offset-taicpu(hp1).oper[1]^.ref^.offset)>=max(tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)],tcgsize2size[reg_cgsize(taicpu(hp1).oper[0]^.reg)]))
  1092. )
  1093. )
  1094. ) and
  1095. GetNextInstruction(hp1,hp2) and
  1096. (hp2.typ=ait_instruction) and
  1097. { loaded register used by next instruction? }
  1098. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  1099. { loaded register not used by previous instruction? }
  1100. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  1101. { same condition? }
  1102. (taicpu(p).condition=taicpu(hp1).condition) and
  1103. { first instruction might not change the register used as base }
  1104. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  1105. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  1106. ) and
  1107. { first instruction might not change the register used as index }
  1108. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  1109. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  1110. ) then
  1111. begin
  1112. hp3:=tai(p.Previous);
  1113. hp5:=tai(p.next);
  1114. asml.Remove(p);
  1115. { if there is a reg. dealloc instruction associated with p, move it together with p }
  1116. { before the instruction? }
  1117. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  1118. begin
  1119. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  1120. RegInInstruction(tai_regalloc(hp3).reg,p) then
  1121. begin
  1122. hp4:=hp3;
  1123. hp3:=tai(hp3.Previous);
  1124. asml.Remove(hp4);
  1125. list.Concat(hp4);
  1126. end
  1127. else
  1128. hp3:=tai(hp3.Previous);
  1129. end;
  1130. list.Concat(p);
  1131. { after the instruction? }
  1132. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  1133. begin
  1134. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  1135. RegInInstruction(tai_regalloc(hp5).reg,p) then
  1136. begin
  1137. hp4:=hp5;
  1138. hp5:=tai(hp5.next);
  1139. asml.Remove(hp4);
  1140. list.Concat(hp4);
  1141. end
  1142. else
  1143. hp5:=tai(hp5.Next);
  1144. end;
  1145. asml.Remove(hp1);
  1146. {$ifdef DEBUG_PREREGSCHEDULER}
  1147. asml.InsertBefore(tai_comment.Create(strpnew('Rescheduled')),hp2);
  1148. {$endif DEBUG_PREREGSCHEDULER}
  1149. asml.InsertBefore(hp1,hp2);
  1150. asml.InsertListBefore(hp2,list);
  1151. end;
  1152. p := tai(p.next)
  1153. end;
  1154. list.Free;
  1155. end;
  1156. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  1157. begin
  1158. { TODO: Add optimizer code }
  1159. end;
  1160. begin
  1161. casmoptimizer:=TCpuAsmOptimizer;
  1162. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  1163. End.