rgcpu.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. {
  2. Copyright (c) 1998-2003 by Florian Klaempfl
  3. This unit implements the arm specific class for the register
  4. allocator
  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 rgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. aasmbase,aasmtai,aasmdata,aasmcpu,
  23. cgbase,cgutils,
  24. cpubase,
  25. rgobj;
  26. type
  27. trgcpu = class(trgobj)
  28. private
  29. procedure spilling_create_load_store(list: TAsmList; pos: tai; const spilltemp:treference;tempreg:tregister; is_store: boolean);
  30. public
  31. procedure do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  32. procedure do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  33. procedure add_constraints(reg:tregister);override;
  34. function get_spill_subreg(r:tregister) : tsubregister;override;
  35. end;
  36. trgcputhumb2 = class(trgobj)
  37. private
  38. procedure SplitITBlock(list:TAsmList;pos:tai);
  39. public
  40. procedure do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  41. procedure do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  42. end;
  43. trgintcputhumb2 = class(trgcputhumb2)
  44. procedure add_cpu_interferences(p : tai);override;
  45. end;
  46. trgintcpu = class(trgcpu)
  47. procedure add_cpu_interferences(p : tai);override;
  48. end;
  49. implementation
  50. uses
  51. verbose,globtype,globals,cpuinfo,
  52. cgobj,
  53. procinfo;
  54. procedure trgintcputhumb2.add_cpu_interferences(p: tai);
  55. var
  56. r : tregister;
  57. hr : longint;
  58. begin
  59. if p.typ=ait_instruction then
  60. begin
  61. case taicpu(p).opcode of
  62. A_CBNZ,
  63. A_CBZ:
  64. begin
  65. for hr := RS_R8 to RS_R15 do
  66. add_edge(getsupreg(taicpu(p).oper[0]^.reg), hr);
  67. end;
  68. A_ADD:
  69. begin
  70. if taicpu(p).ops = 3 then
  71. begin
  72. if (taicpu(p).oper[0]^.typ = top_reg) and
  73. (taicpu(p).oper[1]^.typ = top_reg) and
  74. (taicpu(p).oper[2]^.typ in [top_reg, top_shifterop]) then
  75. begin
  76. { if d == 13 || (d == 15 && S == ‘0’) || n == 15 || m IN [13,15] then UNPREDICTABLE; }
  77. add_edge(getsupreg(taicpu(p).oper[0]^.reg), RS_R13);
  78. if taicpu(p).oppostfix <> PF_S then
  79. add_edge(getsupreg(taicpu(p).oper[0]^.reg), RS_R15);
  80. add_edge(getsupreg(taicpu(p).oper[1]^.reg), RS_R15);
  81. if (taicpu(p).oper[2]^.typ = top_shifterop) and
  82. (taicpu(p).oper[2]^.shifterop^.rs <> NR_NO) then
  83. begin
  84. add_edge(getsupreg(taicpu(p).oper[2]^.shifterop^.rs), RS_R13);
  85. add_edge(getsupreg(taicpu(p).oper[2]^.shifterop^.rs), RS_R15);
  86. end
  87. else if (taicpu(p).oper[2]^.typ = top_reg) then
  88. begin
  89. add_edge(getsupreg(taicpu(p).oper[2]^.reg), RS_R13);
  90. add_edge(getsupreg(taicpu(p).oper[2]^.reg), RS_R15);
  91. end;
  92. end;
  93. end;
  94. end;
  95. A_LDRB,
  96. A_STRB,
  97. A_STR,
  98. A_LDR,
  99. A_LDRH,
  100. A_STRH,
  101. A_LDRSB,
  102. A_LDRSH,
  103. A_LDRD,
  104. A_STRD:
  105. { don't mix up the framepointer and stackpointer with pre/post indexed operations }
  106. if (taicpu(p).oper[1]^.typ=top_ref) and
  107. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  108. begin
  109. add_edge(getsupreg(taicpu(p).oper[1]^.ref^.base),getsupreg(current_procinfo.framepointer));
  110. { FIXME: temp variable r is needed here to avoid Internal error 20060521 }
  111. { while compiling the compiler. }
  112. r:=NR_STACK_POINTER_REG;
  113. if current_procinfo.framepointer<>r then
  114. add_edge(getsupreg(taicpu(p).oper[1]^.ref^.base),getsupreg(r));
  115. end;
  116. end;
  117. end;
  118. end;
  119. procedure trgcpu.spilling_create_load_store(list: TAsmList; pos: tai; const spilltemp:treference;tempreg:tregister; is_store: boolean);
  120. var
  121. tmpref : treference;
  122. helplist : TAsmList;
  123. l : tasmlabel;
  124. hreg : tregister;
  125. immshift: byte;
  126. a: aint;
  127. begin
  128. helplist:=TAsmList.create;
  129. { load consts entry }
  130. if getregtype(tempreg)=R_INTREGISTER then
  131. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  132. else
  133. hreg:=cg.getintregister(helplist,OS_ADDR);
  134. { Lets remove the bits we can fold in later and check if the result can be easily with an add or sub }
  135. a:=abs(spilltemp.offset);
  136. if is_shifter_const(a and not($FFF), immshift) then
  137. if spilltemp.offset > 0 then
  138. begin
  139. {$ifdef DEBUG_SPILLING}
  140. helplist.concat(tai_comment.create(strpnew('Spilling: Use ADD to fix spill offset')));
  141. {$endif}
  142. helplist.concat(taicpu.op_reg_reg_const(A_ADD, hreg, current_procinfo.framepointer,
  143. a and not($FFF)));
  144. reference_reset_base(tmpref, hreg, a and $FFF, sizeof(aint));
  145. end
  146. else
  147. begin
  148. {$ifdef DEBUG_SPILLING}
  149. helplist.concat(tai_comment.create(strpnew('Spilling: Use SUB to fix spill offset')));
  150. {$endif}
  151. helplist.concat(taicpu.op_reg_reg_const(A_SUB, hreg, current_procinfo.framepointer,
  152. a and not($FFF)));
  153. reference_reset_base(tmpref, hreg, -(a and $FFF), sizeof(aint));
  154. end
  155. else
  156. begin
  157. {$ifdef DEBUG_SPILLING}
  158. helplist.concat(tai_comment.create(strpnew('Spilling: Use a_load_const_reg to fix spill offset')));
  159. {$endif}
  160. cg.a_load_const_reg(helplist,OS_ADDR,spilltemp.offset,hreg);
  161. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(aint));
  162. tmpref.index:=hreg;
  163. end;
  164. if spilltemp.index<>NR_NO then
  165. internalerror(200401263);
  166. if is_store then
  167. helplist.concat(spilling_create_store(tempreg,tmpref))
  168. else
  169. helplist.concat(spilling_create_load(tmpref,tempreg));
  170. if getregtype(tempreg)=R_INTREGISTER then
  171. ungetregisterinline(helplist,hreg);
  172. list.insertlistafter(pos,helplist);
  173. helplist.free;
  174. end;
  175. procedure trgcpu.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  176. begin
  177. { don't load spilled register between
  178. mov lr,pc
  179. mov pc,r4
  180. but befure the mov lr,pc
  181. }
  182. if assigned(pos.previous) and
  183. (pos.typ=ait_instruction) and
  184. (taicpu(pos).opcode=A_MOV) and
  185. (taicpu(pos).oper[0]^.typ=top_reg) and
  186. (taicpu(pos).oper[0]^.reg=NR_R14) and
  187. (taicpu(pos).oper[1]^.typ=top_reg) and
  188. (taicpu(pos).oper[1]^.reg=NR_PC) then
  189. pos:=tai(pos.previous);
  190. if abs(spilltemp.offset)>4095 then
  191. spilling_create_load_store(list, pos, spilltemp, tempreg, false)
  192. else
  193. inherited do_spill_read(list,pos,spilltemp,tempreg);
  194. end;
  195. procedure trgcpu.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  196. begin
  197. if abs(spilltemp.offset)>4095 then
  198. spilling_create_load_store(list, pos, spilltemp, tempreg, true)
  199. else
  200. inherited do_spill_written(list,pos,spilltemp,tempreg);
  201. end;
  202. procedure trgcpu.add_constraints(reg:tregister);
  203. var
  204. supreg,i : Tsuperregister;
  205. begin
  206. case getsubreg(reg) of
  207. { Let 32bit floats conflict with all double precision regs > 15
  208. (since these don't have 32 bit equivalents) }
  209. R_SUBFS:
  210. begin
  211. supreg:=getsupreg(reg);
  212. for i:=RS_D16 to RS_D31 do
  213. add_edge(supreg,i);
  214. end;
  215. end;
  216. end;
  217. function trgcpu.get_spill_subreg(r:tregister) : tsubregister;
  218. begin
  219. if (getregtype(r)<>R_MMREGISTER) then
  220. result:=defaultsub
  221. else
  222. result:=getsubreg(r);
  223. end;
  224. function GetITRemainderOp(originalOp:TAsmOp;remLevels:longint;var newOp: TAsmOp;var NeedsCondSwap:boolean) : TAsmOp;
  225. const
  226. remOps : array[1..3] of array[A_ITE..A_ITTTT] of TAsmOp = (
  227. (A_IT,A_IT, A_IT,A_IT,A_IT,A_IT, A_IT,A_IT,A_IT,A_IT,A_IT,A_IT,A_IT,A_IT),
  228. (A_NONE,A_NONE, A_ITT,A_ITE,A_ITE,A_ITT, A_ITT,A_ITT,A_ITE,A_ITE,A_ITE,A_ITE,A_ITT,A_ITT),
  229. (A_NONE,A_NONE, A_NONE,A_NONE,A_NONE,A_NONE, A_ITTT,A_ITEE,A_ITET,A_ITTE,A_ITTE,A_ITET,A_ITEE,A_ITTT));
  230. newOps : array[1..3] of array[A_ITE..A_ITTTT] of TAsmOp = (
  231. (A_IT,A_IT, A_ITE,A_ITT,A_ITE,A_ITT, A_ITEE,A_ITTE,A_ITET,A_ITTT,A_ITEE,A_ITTE,A_ITET,A_ITTT),
  232. (A_NONE,A_NONE, A_IT,A_IT,A_IT,A_IT, A_ITE,A_ITT,A_ITE,A_ITT,A_ITE,A_ITT,A_ITE,A_ITT),
  233. (A_NONE,A_NONE, A_NONE,A_NONE,A_NONE,A_NONE, A_IT,A_IT,A_IT,A_IT,A_IT,A_IT,A_IT,A_IT));
  234. needsSwap: array[1..3] of array[A_ITE..A_ITTTT] of Boolean = (
  235. (true ,false, true ,true ,false,false, true ,true ,true ,true ,false,false,false,false),
  236. (false,false, true ,false,true ,false, true ,true ,false,false,true ,true ,false,false),
  237. (false,false, false,false,false,false, true ,false,true ,false,true ,false,true ,false));
  238. begin
  239. result:=remOps[remLevels][originalOp];
  240. newOp:=newOps[remLevels][originalOp];
  241. NeedsCondSwap:=needsSwap[remLevels][originalOp];
  242. end;
  243. procedure trgcputhumb2.SplitITBlock(list: TAsmList; pos: tai);
  244. var
  245. hp : tai;
  246. level,itLevel : LongInt;
  247. remOp,newOp : TAsmOp;
  248. needsSwap : boolean;
  249. begin
  250. hp:=pos;
  251. level := 0;
  252. while assigned(hp) do
  253. begin
  254. if IsIT(taicpu(hp).opcode) then
  255. break
  256. else if hp.typ=ait_instruction then
  257. inc(level);
  258. hp:=tai(hp.Previous);
  259. end;
  260. if not assigned(hp) then
  261. internalerror(2012100801); // We are supposed to have found the ITxxx instruction here
  262. if (hp.typ<>ait_instruction) or
  263. (not IsIT(taicpu(hp).opcode)) then
  264. internalerror(2012100802); // Sanity check
  265. itLevel := GetITLevels(taicpu(hp).opcode);
  266. if level=itLevel then
  267. exit; // pos was the last instruction in the IT block anyway
  268. remOp:=GetITRemainderOp(taicpu(hp).opcode,itLevel-level,newOp,needsSwap);
  269. if (remOp=A_NONE) or
  270. (newOp=A_NONE) then
  271. Internalerror(2012100803);
  272. taicpu(hp).opcode:=newOp;
  273. if needsSwap then
  274. list.InsertAfter(taicpu.op_cond(remOp,inverse_cond(taicpu(hp).oper[0]^.cc)), pos)
  275. else
  276. list.InsertAfter(taicpu.op_cond(remOp,taicpu(hp).oper[0]^.cc), pos);
  277. end;
  278. procedure trgcputhumb2.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  279. var
  280. tmpref : treference;
  281. helplist : TAsmList;
  282. l : tasmlabel;
  283. hreg : tregister;
  284. begin
  285. { don't load spilled register between
  286. mov lr,pc
  287. mov pc,r4
  288. but befure the mov lr,pc
  289. }
  290. if assigned(pos.previous) and
  291. (pos.typ=ait_instruction) and
  292. (taicpu(pos).opcode=A_MOV) and
  293. (taicpu(pos).oper[0]^.typ=top_reg) and
  294. (taicpu(pos).oper[0]^.reg=NR_R14) and
  295. (taicpu(pos).oper[1]^.typ=top_reg) and
  296. (taicpu(pos).oper[1]^.reg=NR_PC) then
  297. pos:=tai(pos.previous);
  298. if (pos.typ=ait_instruction) and
  299. (taicpu(pos).condition<>C_None) and
  300. (taicpu(pos).opcode<>A_B) then
  301. SplitITBlock(list, pos)
  302. else if (pos.typ=ait_instruction) and
  303. IsIT(taicpu(pos).opcode) then
  304. begin
  305. if not assigned(pos.Previous) then
  306. list.InsertBefore(tai_comment.Create('Dummy'), pos);
  307. pos:=tai(pos.Previous);
  308. end;
  309. if (spilltemp.offset>4095) or (spilltemp.offset<-255) then
  310. begin
  311. helplist:=TAsmList.create;
  312. reference_reset(tmpref,sizeof(aint));
  313. { create consts entry }
  314. current_asmdata.getjumplabel(l);
  315. cg.a_label(current_procinfo.aktlocaldata,l);
  316. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  317. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  318. { load consts entry }
  319. if getregtype(tempreg)=R_INTREGISTER then
  320. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  321. else
  322. hreg:=cg.getintregister(helplist,OS_ADDR);
  323. tmpref.symbol:=l;
  324. tmpref.base:=NR_R15;
  325. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  326. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(aint));
  327. tmpref.index:=hreg;
  328. if spilltemp.index<>NR_NO then
  329. internalerror(200401263);
  330. helplist.concat(spilling_create_load(tmpref,tempreg));
  331. if getregtype(tempreg)=R_INTREGISTER then
  332. ungetregisterinline(helplist,hreg);
  333. list.insertlistafter(pos,helplist);
  334. helplist.free;
  335. end
  336. else
  337. inherited do_spill_read(list,pos,spilltemp,tempreg);
  338. end;
  339. procedure trgcputhumb2.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  340. var
  341. tmpref : treference;
  342. helplist : TAsmList;
  343. l : tasmlabel;
  344. hreg : tregister;
  345. begin
  346. if (pos.typ=ait_instruction) and
  347. (taicpu(pos).condition<>C_None) and
  348. (taicpu(pos).opcode<>A_B) then
  349. SplitITBlock(list, pos)
  350. else if (pos.typ=ait_instruction) and
  351. IsIT(taicpu(pos).opcode) then
  352. begin
  353. if not assigned(pos.Previous) then
  354. list.InsertBefore(tai_comment.Create('Dummy'), pos);
  355. pos:=tai(pos.Previous);
  356. end;
  357. if (spilltemp.offset>4095) or (spilltemp.offset<-255) then
  358. begin
  359. helplist:=TAsmList.create;
  360. reference_reset(tmpref,sizeof(aint));
  361. { create consts entry }
  362. current_asmdata.getjumplabel(l);
  363. cg.a_label(current_procinfo.aktlocaldata,l);
  364. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  365. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  366. { load consts entry }
  367. if getregtype(tempreg)=R_INTREGISTER then
  368. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  369. else
  370. hreg:=cg.getintregister(helplist,OS_ADDR);
  371. tmpref.symbol:=l;
  372. tmpref.base:=NR_R15;
  373. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  374. if spilltemp.index<>NR_NO then
  375. internalerror(200401263);
  376. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(pint));
  377. tmpref.index:=hreg;
  378. helplist.concat(spilling_create_store(tempreg,tmpref));
  379. if getregtype(tempreg)=R_INTREGISTER then
  380. ungetregisterinline(helplist,hreg);
  381. list.insertlistafter(pos,helplist);
  382. helplist.free;
  383. end
  384. else
  385. inherited do_spill_written(list,pos,spilltemp,tempreg);
  386. end;
  387. procedure trgintcpu.add_cpu_interferences(p : tai);
  388. var
  389. r : tregister;
  390. begin
  391. if p.typ=ait_instruction then
  392. begin
  393. case taicpu(p).opcode of
  394. A_MLA,
  395. A_MUL:
  396. if current_settings.cputype<cpu_armv6 then
  397. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[1]^.reg));
  398. A_UMULL,
  399. A_UMLAL,
  400. A_SMULL,
  401. A_SMLAL:
  402. begin
  403. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[1]^.reg));
  404. add_edge(getsupreg(taicpu(p).oper[1]^.reg),getsupreg(taicpu(p).oper[2]^.reg));
  405. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[2]^.reg));
  406. end;
  407. A_LDRB,
  408. A_STRB,
  409. A_STR,
  410. A_LDR,
  411. A_LDRH,
  412. A_STRH:
  413. { don't mix up the framepointer and stackpointer with pre/post indexed operations }
  414. if (taicpu(p).oper[1]^.typ=top_ref) and
  415. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  416. begin
  417. add_edge(getsupreg(taicpu(p).oper[1]^.ref^.base),getsupreg(current_procinfo.framepointer));
  418. { FIXME: temp variable r is needed here to avoid Internal error 20060521 }
  419. { while compiling the compiler. }
  420. r:=NR_STACK_POINTER_REG;
  421. if current_procinfo.framepointer<>r then
  422. add_edge(getsupreg(taicpu(p).oper[1]^.ref^.base),getsupreg(r));
  423. end;
  424. end;
  425. end;
  426. end;
  427. end.