rropt386.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  4. development team
  5. This unit contains register renaming functionality
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. Unit rrOpt386;
  20. {$i fpcdefs.inc}
  21. Interface
  22. Uses aasmbase,aasmtai,aasmcpu;
  23. procedure doRenaming(asml: TAAsmoutput; first, last: Tai);
  24. Implementation
  25. Uses
  26. {$ifdef replaceregdebug}cutils,{$endif}
  27. verbose,globals,cpubase,daopt386,csopt386,cginfo,rgobj;
  28. function canBeFirstSwitch(p: Taicpu; reg: tregister): boolean;
  29. { checks whether an operation on reg can be switched to another reg without an }
  30. { additional mov, e.g. "addl $4,%reg1" can be changed to "leal 4(%reg1),%reg2" }
  31. begin
  32. canBeFirstSwitch := false;
  33. case p.opcode of
  34. A_MOV,A_MOVZX,A_MOVSX,A_LEA:
  35. canBeFirstSwitch :=
  36. (p.oper[1].typ = top_reg) and
  37. (reg32(p.oper[1].reg).enum = reg.enum);
  38. A_IMUL:
  39. canBeFirstSwitch :=
  40. (p.ops >= 2) and
  41. (reg32(p.oper[p.ops-1].reg).enum = reg.enum) and
  42. (p.oper[0].typ <> top_ref) and
  43. (not pTaiprop(p.optinfo)^.FlagsUsed);
  44. A_INC,A_DEC,A_SUB,A_ADD:
  45. canBeFirstSwitch :=
  46. (p.oper[1].typ = top_reg) and
  47. (p.opsize = S_L) and
  48. (reg32(p.oper[1].reg).enum = reg.enum) and
  49. (p.oper[0].typ <> top_ref) and
  50. ((p.opcode <> A_SUB) or
  51. (p.oper[0].typ = top_const)) and
  52. (not pTaiprop(p.optinfo)^.FlagsUsed);
  53. A_SHL:
  54. canBeFirstSwitch :=
  55. (p.opsize = S_L) and
  56. (p.oper[1].typ = top_reg) and
  57. (p.oper[1].reg.enum = reg.enum) and
  58. (p.oper[0].typ = top_const) and
  59. (p.oper[0].val in [1,2,3]) and
  60. (not pTaiprop(p.optinfo)^.FlagsUsed);
  61. end;
  62. end;
  63. procedure switchReg(var reg: tregister; reg1, reg2: tregister);
  64. begin
  65. if reg1.enum>lastreg then
  66. internalerror(2003010801);
  67. if reg2.enum>lastreg then
  68. internalerror(2003010801);
  69. if reg.enum>lastreg then
  70. internalerror(2003010801);
  71. if reg.enum = reg1.enum then
  72. reg := reg2
  73. else if reg.enum = reg2.enum then
  74. reg := reg1
  75. else if (reg.enum in regset8bit) then
  76. begin
  77. if (reg.enum = changeregsize(reg1,S_B).enum) then
  78. reg := changeregsize(reg2,S_B)
  79. else if reg.enum = changeregsize(reg2,S_B).enum then
  80. reg := changeregsize(reg1,S_B);
  81. end
  82. else if (reg.enum in regset16bit) then
  83. begin
  84. if reg.enum = changeregsize(reg1,S_W).enum then
  85. reg := changeregsize(reg2,S_W)
  86. else if reg.enum = changeregsize(reg2,S_W).enum then
  87. reg := changeregsize(reg1,S_W);
  88. end;
  89. end;
  90. procedure switchOp(var op: toper; reg1, reg2: tregister);
  91. begin
  92. case op.typ of
  93. top_reg:
  94. switchReg(op.reg,reg1,reg2);
  95. top_ref:
  96. begin
  97. switchReg(op.ref^.base,reg1,reg2);
  98. switchReg(op.ref^.index,reg1,reg2);
  99. end;
  100. end;
  101. end;
  102. procedure doSwitchReg(hp: Taicpu; reg1,reg2: tregister);
  103. var
  104. opCount: longint;
  105. begin
  106. for opCount := 0 to hp.ops-1 do
  107. switchOp(hp.oper[opCount],reg1,reg2);
  108. end;
  109. procedure doFirstSwitch(p: Taicpu; reg1, reg2: tregister);
  110. var
  111. tmpRef: treference;
  112. begin
  113. case p.opcode of
  114. A_MOV,A_MOVZX,A_MOVSX,A_LEA:
  115. begin
  116. changeOp(p.oper[1],reg1,reg2);
  117. changeOp(p.oper[0],reg2,reg1);
  118. end;
  119. A_IMUL:
  120. begin
  121. p.ops := 3;
  122. p.loadreg(2,p.oper[1].reg);
  123. changeOp(p.oper[2],reg1,reg2);
  124. end;
  125. A_INC,A_DEC:
  126. begin
  127. reference_reset(tmpref);
  128. tmpref.base := reg1;
  129. case p.opcode of
  130. A_INC:
  131. tmpref.offset := 1;
  132. A_DEC:
  133. tmpref.offset := -1;
  134. end;
  135. p.ops := 2;
  136. p.opcode := A_LEA;
  137. p.loadreg(1,reg2);
  138. p.loadref(0,tmpref);
  139. end;
  140. A_SUB,A_ADD:
  141. begin
  142. reference_reset(tmpref);
  143. tmpref.base := reg1;
  144. case p.oper[0].typ of
  145. top_const:
  146. begin
  147. tmpref.offset := p.oper[0].val;
  148. if p.opcode = A_SUB then
  149. tmpref.offset := - tmpRef.offset;
  150. end;
  151. top_symbol:
  152. tmpref.symbol := p.oper[0].sym;
  153. top_reg:
  154. begin
  155. tmpref.index := p.oper[0].reg;
  156. tmpref.scalefactor := 1;
  157. end;
  158. else internalerror(200010031);
  159. end;
  160. p.opcode := A_LEA;
  161. p.loadref(0,tmpref);
  162. p.loadreg(1,reg2);
  163. end;
  164. A_SHL:
  165. begin
  166. reference_reset(tmpref);
  167. tmpref.index := reg1;
  168. tmpref.scalefactor := 1 shl p.oper[0].val;
  169. p.opcode := A_LEA;
  170. p.loadref(0,tmpref);
  171. p.loadreg(1,reg2);
  172. end;
  173. else internalerror(200010032);
  174. end;
  175. end;
  176. function switchRegs(asml: TAAsmoutput; reg1, reg2: tregister; start: Tai): Boolean;
  177. { change movl %reg1,%reg2 ... bla ... to ... bla with reg1 and reg2 switched }
  178. var
  179. endP, hp, lastreg1,lastreg2: Tai;
  180. switchDone, switchLast, tmpResult, sequenceEnd, reg1Modified, reg2Modified: boolean;
  181. reg1StillUsed, reg2StillUsed, isInstruction: boolean;
  182. begin
  183. switchRegs := false;
  184. tmpResult := true;
  185. sequenceEnd := false;
  186. reg1Modified := false;
  187. reg2Modified := false;
  188. endP := start;
  189. while tmpResult and not sequenceEnd do
  190. begin
  191. tmpResult :=
  192. getNextInstruction(endP,endP);
  193. If tmpResult and
  194. not pTaiprop(endp.optinfo)^.canBeRemoved then
  195. begin
  196. { if the newReg gets stored back to the oldReg, we can change }
  197. { "mov %oldReg,%newReg; <operations on %newReg>; mov %newReg, }
  198. { %oldReg" to "<operations on %oldReg>" }
  199. switchLast := storeBack(endP,reg1,reg2);
  200. reg1StillUsed := reg1.enum in pTaiprop(endp.optinfo)^.usedregs;
  201. reg2StillUsed := reg2.enum in pTaiprop(endp.optinfo)^.usedregs;
  202. isInstruction := endp.typ = ait_instruction;
  203. sequenceEnd :=
  204. switchLast or
  205. { if both registers are released right before an instruction }
  206. { that contains hardcoded regs, it's ok too }
  207. (not reg1StillUsed and not reg2StillUsed) or
  208. { no support for (i)div, mul and imul with hardcoded operands }
  209. (((not isInstruction) or
  210. noHardCodedRegs(Taicpu(endP),reg1,reg2)) and
  211. (not reg1StillUsed or
  212. (isInstruction and findRegDealloc(reg1,endP) and
  213. regLoadedWithNewValue(reg1,false,Taicpu(endP)))) and
  214. (not reg2StillUsed or
  215. (isInstruction and findRegDealloc(reg2,endP) and
  216. regLoadedWithNewValue(reg2,false,Taicpu(endP)))));
  217. { we can't switch reg1 and reg2 in something like }
  218. { movl %reg1,%reg2 }
  219. { movl (%reg2),%reg2 }
  220. { movl 4(%reg1),%reg1 }
  221. if reg2Modified and not(reg1Modified) and
  222. regReadByInstruction(reg1,endP) then
  223. begin
  224. tmpResult := false;
  225. break
  226. end;
  227. if not reg1Modified then
  228. begin
  229. reg1Modified := regModifiedByInstruction(reg1,endP);
  230. if reg1Modified and not canBeFirstSwitch(Taicpu(endP),reg1) then
  231. begin
  232. tmpResult := false;
  233. break;
  234. end;
  235. end;
  236. if not reg2Modified then
  237. reg2Modified := regModifiedByInstruction(reg2,endP);
  238. if sequenceEnd then
  239. break;
  240. tmpResult :=
  241. (endp.typ <> ait_label) and
  242. ((not isInstruction) or
  243. (NoHardCodedRegs(Taicpu(endP),reg1,reg2) and
  244. RegSizesOk(reg1,reg2,Taicpu(endP)) and
  245. (Taicpu(endp).opcode <> A_JMP)));
  246. end;
  247. end;
  248. if tmpResult and sequenceEnd then
  249. begin
  250. switchRegs := true;
  251. reg1Modified := false;
  252. reg2Modified := false;
  253. lastreg1 := start;
  254. lastreg2 := start;
  255. getNextInstruction(start,hp);
  256. while hp <> endP do
  257. begin
  258. if (not pTaiprop(hp.optinfo)^.canberemoved) and
  259. (hp.typ = ait_instruction) then
  260. begin
  261. switchDone := false;
  262. if not reg1Modified then
  263. begin
  264. reg1Modified := regModifiedByInstruction(reg1,hp);
  265. if reg1Modified then
  266. begin
  267. doFirstSwitch(Taicpu(hp),reg1,reg2);
  268. switchDone := true;
  269. end;
  270. end;
  271. if not switchDone then
  272. if reg1Modified then
  273. doSwitchReg(Taicpu(hp),reg1,reg2)
  274. else
  275. doReplaceReg(Taicpu(hp),reg2,reg1);
  276. end;
  277. if regininstruction(reg1.enum,hp) then
  278. lastreg1 := hp;
  279. if regininstruction(reg2.enum,hp) then
  280. lastreg2 := hp;
  281. getNextInstruction(hp,hp);
  282. end;
  283. if switchLast then
  284. doSwitchReg(Taicpu(hp),reg1,reg2)
  285. else getLastInstruction(hp,hp);
  286. allocRegBetween(asmL,reg1,start,lastreg1);
  287. allocRegBetween(asmL,reg2,start,lastreg2);
  288. end;
  289. end;
  290. procedure doRenaming(asml: TAAsmoutput; first, last: Tai);
  291. var
  292. p: Tai;
  293. begin
  294. p := First;
  295. SkipHead(p);
  296. while p <> last do
  297. begin
  298. case p.typ of
  299. ait_instruction:
  300. begin
  301. case Taicpu(p).opcode of
  302. A_MOV:
  303. begin
  304. if not(pTaiprop(p.optinfo)^.canBeRemoved) and
  305. (Taicpu(p).oper[0].typ = top_reg) and
  306. (Taicpu(p).oper[1].typ = top_reg) and
  307. (Taicpu(p).opsize = S_L) and
  308. { (Taicpu(p).oper[0].reg.enum in (rg.usableregsint+[R_EDI])) and
  309. (Taicpu(p).oper[1].reg.enum in (rg.usableregsint+[R_EDI])) then}
  310. (Taicpu(p).oper[0].reg.enum in ([R_EDI])) and
  311. (Taicpu(p).oper[1].reg.enum in ([R_EDI])) then
  312. if switchRegs(asml,Taicpu(p).oper[0].reg,
  313. Taicpu(p).oper[1].reg,p) then
  314. begin
  315. { getnextinstruction(p,hp);
  316. asmL^.remove(p);
  317. dispose(p,done);
  318. p := hp;
  319. continue }
  320. pTaiprop(p.optinfo)^.canBeRemoved := true;
  321. end;
  322. end;
  323. end;
  324. end;
  325. end;
  326. getNextInstruction(p,p);
  327. end;
  328. end;
  329. End.
  330. {
  331. $Log$
  332. Revision 1.22 2003-06-03 21:09:05 peter
  333. * internal changeregsize for optimizer
  334. * fix with a hack to not remove the first instruction of a block
  335. which will leave blockstart pointing to invalid memory
  336. Revision 1.21 2003/03/28 19:16:57 peter
  337. * generic constructor working for i386
  338. * remove fixed self register
  339. * esi added as address register for i386
  340. Revision 1.20 2003/02/19 22:00:16 daniel
  341. * Code generator converted to new register notation
  342. - Horribily outdated todo.txt removed
  343. Revision 1.19 2003/01/08 18:43:57 daniel
  344. * Tregister changed into a record
  345. Revision 1.18 2002/07/01 18:46:34 peter
  346. * internal linker
  347. * reorganized aasm layer
  348. Revision 1.17 2002/05/18 13:34:26 peter
  349. * readded missing revisions
  350. Revision 1.16 2002/05/16 19:46:52 carl
  351. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  352. + try to fix temp allocation (still in ifdef)
  353. + generic constructor calls
  354. + start of tassembler / tmodulebase class cleanup
  355. Revision 1.14 2002/05/12 16:53:18 peter
  356. * moved entry and exitcode to ncgutil and cgobj
  357. * foreach gets extra argument for passing local data to the
  358. iterator function
  359. * -CR checks also class typecasts at runtime by changing them
  360. into as
  361. * fixed compiler to cycle with the -CR option
  362. * fixed stabs with elf writer, finally the global variables can
  363. be watched
  364. * removed a lot of routines from cga unit and replaced them by
  365. calls to cgobj
  366. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  367. u32bit then the other is typecasted also to u32bit without giving
  368. a rangecheck warning/error.
  369. * fixed pascal calling method with reversing also the high tree in
  370. the parast, detected by tcalcst3 test
  371. Revision 1.13 2002/04/21 15:42:17 carl
  372. * changeregsize -> changeregsize
  373. Revision 1.12 2002/04/20 21:37:08 carl
  374. + generic FPC_CHECKPOINTER
  375. + first parameter offset in stack now portable
  376. * rename some constants
  377. + move some cpu stuff to other units
  378. - remove unused constents
  379. * fix stacksize for some targets
  380. * fix generic size problems which depend now on EXTEND_SIZE constant
  381. * removing frame pointer in routines is only available for : i386,m68k and vis targets
  382. Revision 1.11 2002/04/15 19:44:22 peter
  383. * fixed stackcheck that would be called recursively when a stack
  384. error was found
  385. * generic changeregsize(reg,size) for i386 register resizing
  386. * removed some more routines from cga unit
  387. * fixed returnvalue handling
  388. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  389. Revision 1.10 2002/04/02 17:11:39 peter
  390. * tlocation,treference update
  391. * LOC_CONSTANT added for better constant handling
  392. * secondadd splitted in multiple routines
  393. * location_force_reg added for loading a location to a register
  394. of a specified size
  395. * secondassignment parses now first the right and then the left node
  396. (this is compatible with Kylix). This saves a lot of push/pop especially
  397. with string operations
  398. * adapted some routines to use the new cg methods
  399. Revision 1.9 2002/03/31 20:26:41 jonas
  400. + a_loadfpu_* and a_loadmm_* methods in tcg
  401. * register allocation is now handled by a class and is mostly processor
  402. independent (+rgobj.pas and i386/rgcpu.pas)
  403. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  404. * some small improvements and fixes to the optimizer
  405. * some register allocation fixes
  406. * some fpuvaroffset fixes in the unary minus node
  407. * push/popusedregisters is now called rg.save/restoreusedregisters and
  408. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  409. also better optimizable)
  410. * fixed and optimized register saving/restoring for new/dispose nodes
  411. * LOC_FPU locations now also require their "register" field to be set to
  412. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  413. - list field removed of the tnode class because it's not used currently
  414. and can cause hard-to-find bugs
  415. }