aoptcpu.pas 43 KB

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