aoptcpu.pas 43 KB

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