aoptcpu.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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,
  43. verbose,
  44. cgutils,
  45. aasmbase,aasmdata,aasmcpu;
  46. function CanBeCond(p : tai) : boolean;
  47. begin
  48. result:=
  49. (p.typ=ait_instruction) and
  50. (taicpu(p).condition=C_None) and
  51. (taicpu(p).opcode<>A_PLD) and
  52. ((taicpu(p).opcode<>A_BLX) or
  53. (taicpu(p).oper[0]^.typ=top_reg));
  54. end;
  55. function RefsEqual(const r1, r2: treference): boolean;
  56. begin
  57. refsequal :=
  58. (r1.offset = r2.offset) and
  59. (r1.base = r2.base) and
  60. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  61. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  62. (r1.relsymbol = r2.relsymbol) and
  63. (r1.signindex = r2.signindex) and
  64. (r1.shiftimm = r2.shiftimm) and
  65. (r1.addressmode = r2.addressmode) and
  66. (r1.shiftmode = r2.shiftmode);
  67. end;
  68. function MatchInstruction(const instr: tai; const op: TAsmOp; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  69. begin
  70. result :=
  71. (instr.typ = ait_instruction) and
  72. (taicpu(instr).opcode = op) and
  73. ((cond = []) or (taicpu(instr).condition in cond)) and
  74. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  75. end;
  76. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  77. begin
  78. result := oper1.typ = oper2.typ;
  79. if result then
  80. case oper1.typ of
  81. top_const:
  82. Result:=oper1.val = oper2.val;
  83. top_reg:
  84. Result:=oper1.reg = oper2.reg;
  85. top_conditioncode:
  86. Result:=oper1.cc = oper2.cc;
  87. top_ref:
  88. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  89. else Result:=false;
  90. end
  91. end;
  92. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  93. begin
  94. result := (oper.typ = top_reg) and (oper.reg = reg);
  95. end;
  96. procedure RemoveRedundantMove(const cmpp: tai; movp: tai; asml: TAsmList);
  97. begin
  98. if (taicpu(movp).condition = C_EQ) and
  99. (taicpu(cmpp).oper[0]^.reg = taicpu(movp).oper[0]^.reg) and
  100. (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then
  101. begin
  102. asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp);
  103. asml.remove(movp);
  104. movp.free;
  105. end;
  106. end;
  107. function regLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  108. var
  109. p: taicpu;
  110. begin
  111. p := taicpu(hp);
  112. regLoadedWithNewValue := false;
  113. if not ((assigned(hp)) and (hp.typ = ait_instruction)) then
  114. exit;
  115. case p.opcode of
  116. { These operands do not write into a register at all }
  117. A_CMP, A_CMN, A_TST, A_TEQ, A_B, A_BL, A_BX, A_BLX, A_SWI, A_MSR, A_PLD:
  118. exit;
  119. {Take care of post/preincremented store and loads, they will change their base register}
  120. A_STR, A_LDR:
  121. regLoadedWithNewValue :=
  122. (taicpu(p).oper[1]^.typ=top_ref) and
  123. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  124. (taicpu(p).oper[1]^.ref^.base = reg);
  125. { These four are writing into the first 2 register, UMLAL and SMLAL will also read from them }
  126. A_UMLAL, A_UMULL, A_SMLAL, A_SMULL:
  127. regLoadedWithNewValue :=
  128. (p.oper[1]^.typ = top_reg) and
  129. (p.oper[1]^.reg = reg);
  130. {Loads to oper2 from coprocessor}
  131. {
  132. MCR/MRC is currently not supported in FPC
  133. A_MRC:
  134. regLoadedWithNewValue :=
  135. (p.oper[2]^.typ = top_reg) and
  136. (p.oper[2]^.reg = reg);
  137. }
  138. {Loads to all register in the registerset}
  139. A_LDM:
  140. regLoadedWithNewValue := (getsupreg(reg) in p.oper[1]^.regset^);
  141. end;
  142. if regLoadedWithNewValue then
  143. exit;
  144. case p.oper[0]^.typ of
  145. {This is the case}
  146. top_reg:
  147. regLoadedWithNewValue := (p.oper[0]^.reg = reg);
  148. {LDM/STM might write a new value to their index register}
  149. top_ref:
  150. regLoadedWithNewValue :=
  151. (taicpu(p).oper[0]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  152. (taicpu(p).oper[0]^.ref^.base = reg);
  153. end;
  154. end;
  155. function instructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  156. var
  157. p: taicpu;
  158. i: longint;
  159. begin
  160. instructionLoadsFromReg := false;
  161. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  162. exit;
  163. p:=taicpu(hp);
  164. i:=1;
  165. {For these instructions we have to start on oper[0]}
  166. if (p.opcode in [A_STR, A_LDM, A_STM, A_PLD,
  167. A_CMP, A_CMN, A_TST, A_TEQ,
  168. A_B, A_BL, A_BX, A_BLX,
  169. A_SMLAL, A_UMLAL]) then i:=0;
  170. while(i<p.ops) do
  171. begin
  172. case p.oper[I]^.typ of
  173. top_reg:
  174. instructionLoadsFromReg := p.oper[I]^.reg = reg;
  175. top_regset:
  176. instructionLoadsFromReg := (getsupreg(reg) in p.oper[I]^.regset^);
  177. top_shifterop:
  178. instructionLoadsFromReg := p.oper[I]^.shifterop^.rs = reg;
  179. top_ref:
  180. instructionLoadsFromReg :=
  181. (p.oper[I]^.ref^.base = reg) or
  182. (p.oper[I]^.ref^.index = reg);
  183. end;
  184. if instructionLoadsFromReg then exit; {Bailout if we found something}
  185. Inc(I);
  186. end;
  187. end;
  188. function TCpuAsmOptimizer.RegUsedAfterInstruction(reg: Tregister; p: tai;
  189. var AllUsedRegs: TAllUsedRegs): Boolean;
  190. begin
  191. AllUsedRegs[getregtype(reg)].Update(tai(p.Next));
  192. RegUsedAfterInstruction :=
  193. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  194. not(regLoadedWithNewValue(reg,p)) and
  195. (
  196. not(GetNextInstruction(p,p)) or
  197. instructionLoadsFromReg(reg,p) or
  198. not(regLoadedWithNewValue(reg,p))
  199. );
  200. end;
  201. procedure TCpuAsmOptimizer.RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  202. var
  203. TmpUsedRegs: TAllUsedRegs;
  204. begin
  205. if MatchInstruction(movp, A_MOV, [taicpu(p).condition], [PF_None]) and
  206. (taicpu(movp).ops=2) and {We can't optimize if there is a shiftop}
  207. MatchOperand(taicpu(movp).oper[1]^, taicpu(p).oper[0]^.reg) and
  208. {There is a special requirement for MUL and MLA, oper[0] and oper[1] are not allowed to be the same}
  209. not (
  210. (taicpu(p).opcode in [A_MLA, A_MUL]) and
  211. (taicpu(p).oper[1]^.reg = taicpu(movp).oper[0]^.reg)
  212. ) then
  213. begin
  214. CopyUsedRegs(TmpUsedRegs);
  215. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  216. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,movp,TmpUsedRegs)) then
  217. begin
  218. asml.insertbefore(tai_comment.Create(strpnew('Peephole '+optimizer+' removed superfluous mov')), movp);
  219. taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
  220. asml.remove(movp);
  221. movp.free;
  222. end;
  223. ReleaseUsedRegs(TmpUsedRegs);
  224. end;
  225. end;
  226. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  227. var
  228. hp1,hp2: tai;
  229. i: longint;
  230. TmpUsedRegs: TAllUsedRegs;
  231. tempop: tasmop;
  232. begin
  233. result := false;
  234. case p.typ of
  235. ait_instruction:
  236. begin
  237. (* optimization proved not to be safe, see tw4768.pp
  238. {
  239. change
  240. <op> reg,x,y
  241. cmp reg,#0
  242. into
  243. <op>s reg,x,y
  244. }
  245. { this optimization can applied only to the currently enabled operations because
  246. the other operations do not update all flags and FPC does not track flag usage }
  247. 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
  248. (taicpu(p).oppostfix = PF_None) and
  249. (taicpu(p).condition = C_None) and
  250. GetNextInstruction(p, hp1) and
  251. MatchInstruction(hp1, A_CMP, [C_None], [PF_None]) and
  252. (taicpu(hp1).oper[1]^.typ = top_const) and
  253. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  254. (taicpu(hp1).oper[1]^.val = 0) { and
  255. GetNextInstruction(hp1, hp2) and
  256. (tai(hp2).typ = ait_instruction) and
  257. // be careful here, following instructions could use other flags
  258. // however after a jump fpc never depends on the value of flags
  259. (taicpu(hp2).opcode = A_B) and
  260. (taicpu(hp2).condition in [C_EQ,C_NE,C_MI,C_PL])} then
  261. begin
  262. taicpu(p).oppostfix:=PF_S;
  263. asml.remove(hp1);
  264. hp1.free;
  265. end
  266. else
  267. *)
  268. case taicpu(p).opcode of
  269. A_STR:
  270. begin
  271. { change
  272. str reg1,ref
  273. ldr reg2,ref
  274. into
  275. str reg1,ref
  276. mov reg2,reg1
  277. }
  278. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  279. (taicpu(p).oppostfix=PF_None) and
  280. GetNextInstruction(p,hp1) and
  281. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [PF_None]) and
  282. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  283. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  284. begin
  285. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  286. begin
  287. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov 1 done')), hp1);
  288. asml.remove(hp1);
  289. hp1.free;
  290. end
  291. else
  292. begin
  293. taicpu(hp1).opcode:=A_MOV;
  294. taicpu(hp1).oppostfix:=PF_None;
  295. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  296. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov 2 done')), hp1);
  297. end;
  298. result := true;
  299. end;
  300. end;
  301. A_LDR:
  302. begin
  303. { change
  304. ldr reg1,ref
  305. ldr reg2,ref
  306. into
  307. ldr reg1,ref
  308. mov reg2,reg1
  309. }
  310. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  311. GetNextInstruction(p,hp1) and
  312. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [taicpu(p).oppostfix]) and
  313. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  314. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  315. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  316. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  317. begin
  318. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  319. begin
  320. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2Ldr done')), hp1);
  321. asml.remove(hp1);
  322. hp1.free;
  323. end
  324. else
  325. begin
  326. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2LdrMov done')), hp1);
  327. taicpu(hp1).opcode:=A_MOV;
  328. taicpu(hp1).oppostfix:=PF_None;
  329. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  330. end;
  331. result := true;
  332. end;
  333. { Remove superfluous mov after ldr
  334. changes
  335. ldr reg1, ref
  336. mov reg2, reg1
  337. to
  338. ldr reg2, ref
  339. conditions are:
  340. * reg1 must be released after mov
  341. * mov can not contain shifterops
  342. * ldr+mov have the same conditions
  343. * mov does not set flags
  344. }
  345. if GetNextInstruction(p, hp1) then
  346. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr');
  347. end;
  348. A_MOV:
  349. begin
  350. { fold
  351. mov reg1,reg0, shift imm1
  352. mov reg1,reg1, shift imm2
  353. to
  354. mov reg1,reg0, shift imm1+imm2
  355. }
  356. if (taicpu(p).ops=3) and
  357. (taicpu(p).oper[2]^.typ = top_shifterop) and
  358. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  359. getnextinstruction(p,hp1) and
  360. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  361. (taicpu(hp1).ops=3) and
  362. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  363. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  364. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  365. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) then
  366. begin
  367. { fold
  368. mov reg1,reg0, lsl 16
  369. mov reg1,reg1, lsr 16
  370. strh reg1, ...
  371. dealloc reg1
  372. to
  373. strh reg1, ...
  374. dealloc reg1
  375. }
  376. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  377. (taicpu(p).oper[2]^.shifterop^.shiftimm=16) and
  378. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ASR]) and
  379. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=16) and
  380. getnextinstruction(hp1,hp2) and
  381. MatchInstruction(hp2, A_STR, [taicpu(p).condition], [PF_H]) and
  382. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^.reg) then
  383. begin
  384. CopyUsedRegs(TmpUsedRegs);
  385. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  386. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  387. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp2,TmpUsedRegs)) then
  388. begin
  389. asml.insertbefore(tai_comment.Create(strpnew('Peephole optimizer removed superfluous 16 Bit zero extension')), hp1);
  390. taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
  391. asml.remove(p);
  392. asml.remove(hp1);
  393. p.free;
  394. hp1.free;
  395. p:=hp2;
  396. end;
  397. ReleaseUsedRegs(TmpUsedRegs);
  398. end
  399. { fold
  400. mov reg1,reg0, shift imm1
  401. mov reg1,reg1, shift imm2
  402. to
  403. mov reg1,reg0, shift imm1+imm2
  404. }
  405. else if (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) then
  406. begin
  407. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  408. { avoid overflows }
  409. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  410. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  411. SM_ROR:
  412. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  413. SM_ASR:
  414. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  415. SM_LSR,
  416. SM_LSL:
  417. begin
  418. hp1:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  419. InsertLLItem(p.previous, p.next, hp1);
  420. p.free;
  421. p:=hp1;
  422. end;
  423. else
  424. internalerror(2008072803);
  425. end;
  426. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShift2Shift done')), p);
  427. asml.remove(hp1);
  428. hp1.free;
  429. result := true;
  430. end;
  431. end;
  432. {
  433. This changes the very common
  434. mov r0, #0
  435. str r0, [...]
  436. mov r0, #0
  437. str r0, [...]
  438. and removes all superfluous mov instructions
  439. }
  440. if (taicpu(p).ops = 2) and
  441. (taicpu(p).oper[1]^.typ = top_const) and
  442. GetNextInstruction(p,hp1) then
  443. begin
  444. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], []) and
  445. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  446. GetNextInstruction(hp1, hp2) and
  447. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  448. (taicpu(hp2).ops = 2) and
  449. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  450. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  451. begin
  452. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovStrMov done')), hp2);
  453. GetNextInstruction(hp2,hp1);
  454. asml.remove(hp2);
  455. hp2.free;
  456. if not assigned(hp1) then break;
  457. end;
  458. end;
  459. {
  460. change
  461. mov r1, r0
  462. add r1, r1, #1
  463. to
  464. add r1, r0, #1
  465. Todo: Make it work for mov+cmp too
  466. CAUTION! If this one is successful p might not be a mov instruction anymore!
  467. }
  468. if (taicpu(p).ops = 2) and
  469. (taicpu(p).oper[1]^.typ = top_reg) and
  470. (taicpu(p).oppostfix = PF_NONE) and
  471. GetNextInstruction(p, hp1) and
  472. (tai(hp1).typ = ait_instruction) and
  473. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  474. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN]) and
  475. {MOV and MVN might only have 2 ops}
  476. (taicpu(hp1).ops = 3) and
  477. (taicpu(hp1).condition in [C_NONE, taicpu(hp1).condition]) and
  478. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  479. (taicpu(hp1).oper[1]^.typ = top_reg) and
  480. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop]) then
  481. begin
  482. { When we get here we still don't know if the registers match}
  483. for I:=1 to 2 do
  484. {
  485. If the first loop was successful p will be replaced with hp1.
  486. The checks will still be ok, because all required information
  487. will also be in hp1 then.
  488. }
  489. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  490. begin
  491. asml.insertbefore(tai_comment.Create(strpnew('Peephole RedundantMovProcess done')), hp1);
  492. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  493. if p<>hp1 then
  494. begin
  495. asml.remove(p);
  496. p.free;
  497. p:=hp1;
  498. end;
  499. end;
  500. end;
  501. { This folds shifterops into following instructions
  502. mov r0, r1, lsl #8
  503. add r2, r3, r0
  504. to
  505. add r2, r3, r1, lsl #8
  506. CAUTION! If this one is successful p might not be a mov instruction anymore!
  507. }
  508. if (taicpu(p).opcode = A_MOV) and
  509. (taicpu(p).ops = 3) and
  510. (taicpu(p).oper[1]^.typ = top_reg) and
  511. (taicpu(p).oper[2]^.typ = top_shifterop) and
  512. (taicpu(p).oppostfix = PF_NONE) and
  513. GetNextInstruction(p, hp1) and
  514. (tai(hp1).typ = ait_instruction) and
  515. (taicpu(hp1).ops = 3) and {Currently we can't fold into another shifterop}
  516. (taicpu(hp1).oper[2]^.typ = top_reg) and
  517. (taicpu(hp1).oppostfix = PF_NONE) and
  518. (taicpu(hp1).condition = taicpu(p).condition) and
  519. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  520. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST]) and
  521. (
  522. {Only ONE of the two src operands is allowed to match}
  523. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) xor
  524. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[2]^)
  525. ) then
  526. begin
  527. CopyUsedRegs(TmpUsedRegs);
  528. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  529. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp1,TmpUsedRegs)) then
  530. for I:=1 to 2 do
  531. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  532. begin
  533. if I = 1 then
  534. begin
  535. {The SUB operators need to be changed when we swap parameters}
  536. case taicpu(hp1).opcode of
  537. A_SUB: tempop:=A_RSB;
  538. A_SBC: tempop:=A_RSC;
  539. A_RSB: tempop:=A_SUB;
  540. A_RSC: tempop:=A_SBC;
  541. else tempop:=taicpu(hp1).opcode;
  542. end;
  543. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  544. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  545. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^);
  546. end
  547. else
  548. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  549. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  550. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^);
  551. asml.insertbefore(hp2, p);
  552. asml.remove(p);
  553. asml.remove(hp1);
  554. p.free;
  555. hp1.free;
  556. p:=hp2;
  557. GetNextInstruction(p,hp1);
  558. asml.insertbefore(tai_comment.Create(strpnew('Peephole FoldShiftProcess done')), p);
  559. break;
  560. end;
  561. ReleaseUsedRegs(TmpUsedRegs);
  562. end;
  563. {
  564. Often we see shifts and then a superfluous mov to another register
  565. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  566. }
  567. if (taicpu(p).opcode = A_MOV) and
  568. GetNextInstruction(p, hp1) then
  569. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  570. end;
  571. A_ADD,
  572. A_ADC,
  573. A_RSB,
  574. A_RSC,
  575. A_SUB,
  576. A_SBC,
  577. A_AND,
  578. A_BIC,
  579. A_EOR,
  580. A_ORR,
  581. A_MLA,
  582. A_MUL:
  583. begin
  584. {
  585. change
  586. and reg2,reg1,const1
  587. and reg2,reg2,const2
  588. to
  589. and reg2,reg1,(const1 and const2)
  590. }
  591. if (taicpu(p).opcode = A_AND) and
  592. (taicpu(p).oper[1]^.typ = top_reg) and
  593. (taicpu(p).oper[2]^.typ = top_const) and
  594. GetNextInstruction(p, hp1) and
  595. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  596. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  597. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  598. (taicpu(hp1).oper[2]^.typ = top_const) then
  599. begin
  600. asml.insertbefore(tai_comment.Create(strpnew('Peephole AndAnd2And done')), p);
  601. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  602. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  603. asml.remove(hp1);
  604. hp1.free;
  605. end;
  606. {
  607. change
  608. add reg1, ...
  609. mov reg2, reg1
  610. to
  611. add reg2, ...
  612. }
  613. if GetNextInstruction(p, hp1) then
  614. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  615. end;
  616. A_CMP:
  617. begin
  618. {
  619. change
  620. cmp reg,const1
  621. moveq reg,const1
  622. movne reg,const2
  623. to
  624. cmp reg,const1
  625. movne reg,const2
  626. }
  627. if (taicpu(p).oper[1]^.typ = top_const) and
  628. GetNextInstruction(p, hp1) and
  629. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  630. (taicpu(hp1).oper[1]^.typ = top_const) and
  631. GetNextInstruction(hp1, hp2) and
  632. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  633. (taicpu(hp1).oper[1]^.typ = top_const) then
  634. begin
  635. RemoveRedundantMove(p, hp1, asml);
  636. RemoveRedundantMove(p, hp2, asml);
  637. end;
  638. end;
  639. end;
  640. end;
  641. end;
  642. end;
  643. { instructions modifying the CPSR can be only the last instruction }
  644. function MustBeLast(p : tai) : boolean;
  645. begin
  646. Result:=(p.typ=ait_instruction) and
  647. ((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
  648. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  649. (taicpu(p).oppostfix=PF_S));
  650. end;
  651. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  652. var
  653. p,hp1,hp2: tai;
  654. l : longint;
  655. condition : tasmcond;
  656. hp3: tai;
  657. WasLast: boolean;
  658. { UsedRegs, TmpUsedRegs: TRegSet; }
  659. begin
  660. p := BlockStart;
  661. { UsedRegs := []; }
  662. while (p <> BlockEnd) Do
  663. begin
  664. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  665. case p.Typ Of
  666. Ait_Instruction:
  667. begin
  668. case taicpu(p).opcode Of
  669. A_B:
  670. if taicpu(p).condition<>C_None then
  671. begin
  672. { check for
  673. Bxx xxx
  674. <several instructions>
  675. xxx:
  676. }
  677. l:=0;
  678. WasLast:=False;
  679. GetNextInstruction(p, hp1);
  680. while assigned(hp1) and
  681. (l<=4) and
  682. CanBeCond(hp1) and
  683. { stop on labels }
  684. not(hp1.typ=ait_label) do
  685. begin
  686. inc(l);
  687. if MustBeLast(hp1) then
  688. begin
  689. WasLast:=True;
  690. GetNextInstruction(hp1,hp1);
  691. break;
  692. end
  693. else
  694. GetNextInstruction(hp1,hp1);
  695. end;
  696. if assigned(hp1) then
  697. begin
  698. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  699. begin
  700. if (l<=4) and (l>0) then
  701. begin
  702. condition:=inverse_cond(taicpu(p).condition);
  703. hp2:=p;
  704. GetNextInstruction(p,hp1);
  705. p:=hp1;
  706. repeat
  707. if hp1.typ=ait_instruction then
  708. taicpu(hp1).condition:=condition;
  709. if MustBeLast(hp1) then
  710. begin
  711. GetNextInstruction(hp1,hp1);
  712. break;
  713. end
  714. else
  715. GetNextInstruction(hp1,hp1);
  716. until not(assigned(hp1)) or
  717. not(CanBeCond(hp1)) or
  718. (hp1.typ=ait_label);
  719. { wait with removing else GetNextInstruction could
  720. ignore the label if it was the only usage in the
  721. jump moved away }
  722. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  723. asml.remove(hp2);
  724. hp2.free;
  725. continue;
  726. end;
  727. end
  728. else
  729. { do not perform further optimizations if there is inctructon
  730. in block #1 which can not be optimized.
  731. }
  732. if not WasLast then
  733. begin
  734. { check further for
  735. Bcc xxx
  736. <several instructions 1>
  737. B yyy
  738. xxx:
  739. <several instructions 2>
  740. yyy:
  741. }
  742. { hp2 points to jmp yyy }
  743. hp2:=hp1;
  744. { skip hp1 to xxx }
  745. GetNextInstruction(hp1, hp1);
  746. if assigned(hp2) and
  747. assigned(hp1) and
  748. (l<=3) and
  749. (hp2.typ=ait_instruction) and
  750. (taicpu(hp2).is_jmp) and
  751. (taicpu(hp2).condition=C_None) and
  752. { real label and jump, no further references to the
  753. label are allowed }
  754. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  755. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  756. begin
  757. l:=0;
  758. { skip hp1 to <several moves 2> }
  759. GetNextInstruction(hp1, hp1);
  760. while assigned(hp1) and
  761. CanBeCond(hp1) do
  762. begin
  763. inc(l);
  764. GetNextInstruction(hp1, hp1);
  765. end;
  766. { hp1 points to yyy: }
  767. if assigned(hp1) and
  768. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  769. begin
  770. condition:=inverse_cond(taicpu(p).condition);
  771. GetNextInstruction(p,hp1);
  772. hp3:=p;
  773. p:=hp1;
  774. repeat
  775. if hp1.typ=ait_instruction then
  776. taicpu(hp1).condition:=condition;
  777. GetNextInstruction(hp1,hp1);
  778. until not(assigned(hp1)) or
  779. not(CanBeCond(hp1));
  780. { hp2 is still at jmp yyy }
  781. GetNextInstruction(hp2,hp1);
  782. { hp2 is now at xxx: }
  783. condition:=inverse_cond(condition);
  784. GetNextInstruction(hp1,hp1);
  785. { hp1 is now at <several movs 2> }
  786. repeat
  787. taicpu(hp1).condition:=condition;
  788. GetNextInstruction(hp1,hp1);
  789. until not(assigned(hp1)) or
  790. not(CanBeCond(hp1)) or
  791. (hp1.typ=ait_label);
  792. {
  793. asml.remove(hp1.next)
  794. hp1.next.free;
  795. asml.remove(hp1);
  796. hp1.free;
  797. }
  798. { remove Bcc }
  799. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  800. asml.remove(hp3);
  801. hp3.free;
  802. { remove jmp }
  803. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  804. asml.remove(hp2);
  805. hp2.free;
  806. continue;
  807. end;
  808. end;
  809. end;
  810. end;
  811. end;
  812. end;
  813. end;
  814. end;
  815. p := tai(p.next)
  816. end;
  817. end;
  818. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  819. begin
  820. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  821. Result:=true
  822. else
  823. Result:=inherited RegInInstruction(Reg, p1);
  824. end;
  825. const
  826. { set of opcode which might or do write to memory }
  827. { TODO : extend armins.dat to contain r/w info }
  828. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  829. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  830. function TCpuPreRegallocScheduler.PeepHoleOptPass1Cpu(var p: tai): boolean;
  831. { TODO : schedule also forward }
  832. { TODO : schedule distance > 1 }
  833. var
  834. hp1,hp2,hp3,hp4,hp5 : tai;
  835. list : TAsmList;
  836. begin
  837. result:=true;
  838. list:=TAsmList.Create;
  839. p := BlockStart;
  840. { UsedRegs := []; }
  841. while (p <> BlockEnd) Do
  842. begin
  843. if (p.typ=ait_instruction) and
  844. GetNextInstruction(p,hp1) and
  845. (hp1.typ=ait_instruction) and
  846. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  847. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  848. not(RegModifiedByInstruction(NR_PC,p)) and
  849. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH])
  850. ) or
  851. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  852. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  853. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  854. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  855. (taicpu(hp1).oper[1]^.ref^.offset=0)
  856. )
  857. ) or
  858. { try to prove that the memory accesses don't overlapp }
  859. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  860. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  861. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  862. (taicpu(p).oppostfix=PF_None) and
  863. (taicpu(hp1).oppostfix=PF_None) and
  864. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  865. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  866. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  867. (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)]))
  868. )
  869. )
  870. ) and
  871. GetNextInstruction(hp1,hp2) and
  872. (hp2.typ=ait_instruction) and
  873. { loaded register used by next instruction? }
  874. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  875. { loaded register not used by previous instruction? }
  876. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  877. { same condition? }
  878. (taicpu(p).condition=taicpu(hp1).condition) and
  879. { first instruction might not change the register used as base }
  880. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  881. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  882. ) and
  883. { first instruction might not change the register used as index }
  884. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  885. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  886. ) then
  887. begin
  888. hp3:=tai(p.Previous);
  889. hp5:=tai(p.next);
  890. asml.Remove(p);
  891. { if there is a reg. dealloc instruction associated with p, move it together with p }
  892. { before the instruction? }
  893. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  894. begin
  895. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  896. RegInInstruction(tai_regalloc(hp3).reg,p) then
  897. begin
  898. hp4:=hp3;
  899. hp3:=tai(hp3.Previous);
  900. asml.Remove(hp4);
  901. list.Concat(hp4);
  902. end
  903. else
  904. hp3:=tai(hp3.Previous);
  905. end;
  906. list.Concat(p);
  907. { after the instruction? }
  908. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  909. begin
  910. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  911. RegInInstruction(tai_regalloc(hp5).reg,p) then
  912. begin
  913. hp4:=hp5;
  914. hp5:=tai(hp5.next);
  915. asml.Remove(hp4);
  916. list.Concat(hp4);
  917. end
  918. else
  919. hp5:=tai(hp5.Next);
  920. end;
  921. asml.Remove(hp1);
  922. {$ifdef DEBUG_PREREGSCHEDULER}
  923. asml.InsertBefore(tai_comment.Create(strpnew('Rescheduled')),hp2);
  924. {$endif DEBUG_PREREGSCHEDULER}
  925. asml.InsertBefore(hp1,hp2);
  926. asml.InsertListBefore(hp2,list);
  927. end;
  928. p := tai(p.next)
  929. end;
  930. list.Free;
  931. end;
  932. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  933. begin
  934. { TODO: Add optimizer code }
  935. end;
  936. begin
  937. casmoptimizer:=TCpuAsmOptimizer;
  938. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  939. End.