rgcpu.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. 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 IsIT(op: TAsmOp) : boolean;
  225. begin
  226. case op of
  227. A_IT,
  228. A_ITE, A_ITT,
  229. A_ITEE, A_ITTE, A_ITET, A_ITTT,
  230. A_ITEEE, A_ITTEE, A_ITETE, A_ITTTE,
  231. A_ITEET, A_ITTET, A_ITETT, A_ITTTT:
  232. result:=true;
  233. else
  234. result:=false;
  235. end;
  236. end;
  237. function GetITLevels(op: TAsmOp) : longint;
  238. begin
  239. case op of
  240. A_IT:
  241. result:=1;
  242. A_ITE, A_ITT:
  243. result:=2;
  244. A_ITEE, A_ITTE, A_ITET, A_ITTT:
  245. result:=3;
  246. A_ITEEE, A_ITTEE, A_ITETE, A_ITTTE,
  247. A_ITEET, A_ITTET, A_ITETT, A_ITTTT:
  248. result:=4;
  249. else
  250. result:=0;
  251. end;
  252. end;
  253. function GetITRemainderOp(originalOp:TAsmOp;remLevels:longint;var newOp: TAsmOp;var NeedsCondSwap:boolean) : TAsmOp;
  254. const
  255. remOps : array[1..3] of array[A_ITE..A_ITTTT] of TAsmOp = (
  256. (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),
  257. (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),
  258. (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));
  259. newOps : array[1..3] of array[A_ITE..A_ITTTT] of TAsmOp = (
  260. (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),
  261. (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),
  262. (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));
  263. needsSwap: array[1..3] of array[A_ITE..A_ITTTT] of Boolean = (
  264. (true ,false, true ,true ,false,false, true ,true ,true ,true ,false,false,false,false),
  265. (false,false, true ,false,true ,false, true ,true ,false,false,true ,true ,false,false),
  266. (false,false, false,false,false,false, true ,false,true ,false,true ,false,true ,false));
  267. begin
  268. result:=remOps[remLevels][originalOp];
  269. newOp:=newOps[remLevels][originalOp];
  270. NeedsCondSwap:=needsSwap[remLevels][originalOp];
  271. end;
  272. procedure trgcputhumb2.SplitITBlock(list: TAsmList; pos: tai);
  273. var
  274. hp : tai;
  275. level,itLevel : LongInt;
  276. remOp,newOp : TAsmOp;
  277. needsSwap : boolean;
  278. begin
  279. hp:=pos;
  280. level := 0;
  281. while assigned(hp) do
  282. begin
  283. if IsIT(taicpu(hp).opcode) then
  284. break
  285. else if hp.typ=ait_instruction then
  286. inc(level);
  287. hp:=tai(hp.Previous);
  288. end;
  289. if not assigned(hp) then
  290. internalerror(2012100801); // We are supposed to have found the ITxxx instruction here
  291. if (hp.typ<>ait_instruction) or
  292. (not IsIT(taicpu(hp).opcode)) then
  293. internalerror(2012100802); // Sanity check
  294. itLevel := GetITLevels(taicpu(hp).opcode);
  295. if level=itLevel then
  296. exit; // pos was the last instruction in the IT block anyway
  297. remOp:=GetITRemainderOp(taicpu(hp).opcode,itLevel-level,newOp,needsSwap);
  298. if (remOp=A_NONE) or
  299. (newOp=A_NONE) then
  300. Internalerror(2012100803);
  301. taicpu(hp).opcode:=newOp;
  302. if needsSwap then
  303. list.InsertAfter(taicpu.op_cond(remOp,inverse_cond(taicpu(hp).oper[0]^.cc)), pos)
  304. else
  305. list.InsertAfter(taicpu.op_cond(remOp,taicpu(hp).oper[0]^.cc), pos);
  306. end;
  307. procedure trgcputhumb2.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  308. var
  309. tmpref : treference;
  310. helplist : TAsmList;
  311. l : tasmlabel;
  312. hreg : tregister;
  313. begin
  314. { don't load spilled register between
  315. mov lr,pc
  316. mov pc,r4
  317. but befure the mov lr,pc
  318. }
  319. if assigned(pos.previous) and
  320. (pos.typ=ait_instruction) and
  321. (taicpu(pos).opcode=A_MOV) and
  322. (taicpu(pos).oper[0]^.typ=top_reg) and
  323. (taicpu(pos).oper[0]^.reg=NR_R14) and
  324. (taicpu(pos).oper[1]^.typ=top_reg) and
  325. (taicpu(pos).oper[1]^.reg=NR_PC) then
  326. pos:=tai(pos.previous);
  327. if (pos.typ=ait_instruction) and
  328. (taicpu(pos).condition<>C_None) and
  329. (taicpu(pos).opcode<>A_B) then
  330. SplitITBlock(list, pos)
  331. else if (pos.typ=ait_instruction) and
  332. IsIT(taicpu(pos).opcode) then
  333. begin
  334. if not assigned(pos.Previous) then
  335. list.InsertBefore(tai_comment.Create('Dummy'), pos);
  336. pos:=tai(pos.Previous);
  337. end;
  338. if (spilltemp.offset>4095) or (spilltemp.offset<-255) then
  339. begin
  340. helplist:=TAsmList.create;
  341. reference_reset(tmpref,sizeof(aint));
  342. { create consts entry }
  343. current_asmdata.getjumplabel(l);
  344. cg.a_label(current_procinfo.aktlocaldata,l);
  345. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  346. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  347. { load consts entry }
  348. if getregtype(tempreg)=R_INTREGISTER then
  349. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  350. else
  351. hreg:=cg.getintregister(helplist,OS_ADDR);
  352. tmpref.symbol:=l;
  353. tmpref.base:=NR_R15;
  354. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  355. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(aint));
  356. tmpref.index:=hreg;
  357. if spilltemp.index<>NR_NO then
  358. internalerror(200401263);
  359. helplist.concat(spilling_create_load(tmpref,tempreg));
  360. if getregtype(tempreg)=R_INTREGISTER then
  361. ungetregisterinline(helplist,hreg);
  362. list.insertlistafter(pos,helplist);
  363. helplist.free;
  364. end
  365. else
  366. inherited do_spill_read(list,pos,spilltemp,tempreg);
  367. end;
  368. procedure trgcputhumb2.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  369. var
  370. tmpref : treference;
  371. helplist : TAsmList;
  372. l : tasmlabel;
  373. hreg : tregister;
  374. begin
  375. if (pos.typ=ait_instruction) and
  376. (taicpu(pos).condition<>C_None) and
  377. (taicpu(pos).opcode<>A_B) then
  378. SplitITBlock(list, pos)
  379. else if (pos.typ=ait_instruction) and
  380. IsIT(taicpu(pos).opcode) then
  381. begin
  382. if not assigned(pos.Previous) then
  383. list.InsertBefore(tai_comment.Create('Dummy'), pos);
  384. pos:=tai(pos.Previous);
  385. end;
  386. if (spilltemp.offset>4095) or (spilltemp.offset<-255) then
  387. begin
  388. helplist:=TAsmList.create;
  389. reference_reset(tmpref,sizeof(aint));
  390. { create consts entry }
  391. current_asmdata.getjumplabel(l);
  392. cg.a_label(current_procinfo.aktlocaldata,l);
  393. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  394. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  395. { load consts entry }
  396. if getregtype(tempreg)=R_INTREGISTER then
  397. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  398. else
  399. hreg:=cg.getintregister(helplist,OS_ADDR);
  400. tmpref.symbol:=l;
  401. tmpref.base:=NR_R15;
  402. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  403. if spilltemp.index<>NR_NO then
  404. internalerror(200401263);
  405. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(pint));
  406. tmpref.index:=hreg;
  407. helplist.concat(spilling_create_store(tempreg,tmpref));
  408. if getregtype(tempreg)=R_INTREGISTER then
  409. ungetregisterinline(helplist,hreg);
  410. list.insertlistafter(pos,helplist);
  411. helplist.free;
  412. end
  413. else
  414. inherited do_spill_written(list,pos,spilltemp,tempreg);
  415. end;
  416. procedure trgintcpu.add_cpu_interferences(p : tai);
  417. var
  418. r : tregister;
  419. begin
  420. if p.typ=ait_instruction then
  421. begin
  422. case taicpu(p).opcode of
  423. A_MLA,
  424. A_MUL:
  425. if current_settings.cputype<cpu_armv6 then
  426. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[1]^.reg));
  427. A_UMULL,
  428. A_UMLAL,
  429. A_SMULL,
  430. A_SMLAL:
  431. begin
  432. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[1]^.reg));
  433. add_edge(getsupreg(taicpu(p).oper[1]^.reg),getsupreg(taicpu(p).oper[2]^.reg));
  434. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[2]^.reg));
  435. end;
  436. A_LDRB,
  437. A_STRB,
  438. A_STR,
  439. A_LDR,
  440. A_LDRH,
  441. A_STRH:
  442. { don't mix up the framepointer and stackpointer with pre/post indexed operations }
  443. if (taicpu(p).oper[1]^.typ=top_ref) and
  444. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  445. begin
  446. add_edge(getsupreg(taicpu(p).oper[1]^.ref^.base),getsupreg(current_procinfo.framepointer));
  447. { FIXME: temp variable r is needed here to avoid Internal error 20060521 }
  448. { while compiling the compiler. }
  449. r:=NR_STACK_POINTER_REG;
  450. if current_procinfo.framepointer<>r then
  451. add_edge(getsupreg(taicpu(p).oper[1]^.ref^.base),getsupreg(r));
  452. end;
  453. end;
  454. end;
  455. end;
  456. end.