rgcpu.pas 19 KB

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