aoptcpu.pas 43 KB

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