rropt386.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  21. Interface
  22. Uses aasm;
  23. procedure doRenaming(asml: TAAsmoutput; first, last: Tai);
  24. Implementation
  25. Uses
  26. {$ifdef replaceregdebug}cutils,{$endif}
  27. verbose,globals,cpubase,cpuasm,daopt386,csopt386,tgcpu;
  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) = reg);
  38. A_IMUL:
  39. canBeFirstSwitch :=
  40. (p.ops >= 2) and
  41. (reg32(p.oper[p.ops-1].reg) = reg) and
  42. (p.oper[0].typ <> top_ref);
  43. A_INC,A_DEC,A_SUB,A_ADD:
  44. canBeFirstSwitch :=
  45. (p.oper[1].typ = top_reg) and
  46. (p.opsize = S_L) and
  47. (reg32(p.oper[1].reg) = reg) and
  48. (p.oper[0].typ <> top_ref) and
  49. ((p.opcode <> A_SUB) or
  50. (p.oper[0].typ = top_const));
  51. A_SHL:
  52. canBeFirstSwitch :=
  53. (p.opsize = S_L) and
  54. (p.oper[1].typ = top_reg) and
  55. (p.oper[1].reg = reg) and
  56. (p.oper[0].typ = top_const) and
  57. (p.oper[0].val in [1,2,3]);
  58. end;
  59. end;
  60. procedure switchReg(var reg: tregister; reg1, reg2: tregister);
  61. begin
  62. if reg = reg1 then
  63. reg := reg2
  64. else if reg = reg2 then
  65. reg := reg1
  66. else if reg = regtoreg8(reg1) then
  67. reg := regtoreg8(reg2)
  68. else if reg = regtoreg8(reg2) then
  69. reg := regtoreg8(reg1)
  70. else if reg = regtoreg16(reg1) then
  71. reg := regtoreg16(reg2)
  72. else if reg = regtoreg16(reg2) then
  73. reg := regtoreg16(reg1)
  74. end;
  75. procedure switchOp(var op: toper; reg1, reg2: tregister);
  76. begin
  77. case op.typ of
  78. top_reg:
  79. switchReg(op.reg,reg1,reg2);
  80. top_ref:
  81. begin
  82. switchReg(op.ref^.base,reg1,reg2);
  83. switchReg(op.ref^.index,reg1,reg2);
  84. end;
  85. end;
  86. end;
  87. procedure doSwitchReg(hp: Taicpu; reg1,reg2: tregister);
  88. var
  89. opCount: longint;
  90. begin
  91. for opCount := 0 to hp.ops-1 do
  92. switchOp(hp.oper[opCount],reg1,reg2);
  93. end;
  94. procedure doFirstSwitch(p: Taicpu; reg1, reg2: tregister);
  95. var
  96. tmpRef: treference;
  97. begin
  98. case p.opcode of
  99. A_MOV,A_MOVZX,A_MOVSX,A_LEA:
  100. begin
  101. changeOp(p.oper[1],reg1,reg2);
  102. changeOp(p.oper[0],reg2,reg1);
  103. end;
  104. A_IMUL:
  105. begin
  106. p.ops := 3;
  107. p.loadreg(2,p.oper[1].reg);
  108. changeOp(p.oper[2],reg1,reg2);
  109. end;
  110. A_INC,A_DEC:
  111. begin
  112. reset_reference(tmpref);
  113. tmpref.base := reg1;
  114. case p.opcode of
  115. A_INC:
  116. tmpref.offset := 1;
  117. A_DEC:
  118. tmpref.offset := -1;
  119. end;
  120. p.ops := 2;
  121. p.opcode := A_LEA;
  122. p.loadreg(1,reg2);
  123. p.loadref(0,newreference(tmpref));
  124. end;
  125. A_SUB,A_ADD:
  126. begin
  127. reset_reference(tmpref);
  128. tmpref.base := reg1;
  129. case p.oper[0].typ of
  130. top_const:
  131. begin
  132. tmpref.offset := p.oper[0].val;
  133. if p.opcode = A_SUB then
  134. tmpref.offset := - tmpRef.offset;
  135. end;
  136. top_symbol:
  137. tmpref.symbol := p.oper[0].sym;
  138. top_reg:
  139. begin
  140. tmpref.index := p.oper[0].reg;
  141. tmpref.scalefactor := 1;
  142. end;
  143. else internalerror(200010031);
  144. end;
  145. p.opcode := A_LEA;
  146. p.loadref(0,newreference(tmpref));
  147. p.loadreg(1,reg2);
  148. end;
  149. A_SHL:
  150. begin
  151. reset_reference(tmpref);
  152. tmpref.index := reg1;
  153. tmpref.scalefactor := 1 shl p.oper[0].val;
  154. p.opcode := A_LEA;
  155. p.loadref(0,newreference(tmpref));
  156. p.loadreg(1,reg2);
  157. end;
  158. else internalerror(200010032);
  159. end;
  160. end;
  161. function switchRegs(asml: TAAsmoutput; reg1, reg2: tregister; start: Tai): Boolean;
  162. { change movl %reg1,%reg2 ... bla ... to ... bla with reg1 and reg2 switched }
  163. var
  164. endP, hp: Tai;
  165. switchDone, switchLast, tmpResult, sequenceEnd, reg1Modified, reg2Modified: boolean;
  166. reg1StillUsed, reg2StillUsed, isInstruction: boolean;
  167. begin
  168. switchRegs := false;
  169. tmpResult := true;
  170. sequenceEnd := false;
  171. reg1Modified := false;
  172. reg2Modified := false;
  173. endP := start;
  174. while tmpResult and not sequenceEnd do
  175. begin
  176. tmpResult :=
  177. getNextInstruction(endP,endP);
  178. If tmpResult and
  179. not pTaiprop(endp.optinfo)^.canBeRemoved then
  180. begin
  181. { if the newReg gets stored back to the oldReg, we can change }
  182. { "mov %oldReg,%newReg; <operations on %newReg>; mov %newReg, }
  183. { %oldReg" to "<operations on %oldReg>" }
  184. switchLast := storeBack(endP,reg1,reg2);
  185. reg1StillUsed := reg1 in pTaiprop(endp.optinfo)^.usedregs;
  186. reg2StillUsed := reg2 in pTaiprop(endp.optinfo)^.usedregs;
  187. isInstruction := endp.typ = ait_instruction;
  188. sequenceEnd :=
  189. switchLast or
  190. { if both registers are released right before an instruction }
  191. { that contains hardcoded regs, it's ok too }
  192. (not reg1StillUsed and not reg2StillUsed) or
  193. { no support for (i)div, mul and imul with hardcoded operands }
  194. (((not isInstruction) or
  195. noHardCodedRegs(Taicpu(endP),reg1,reg2)) and
  196. (not reg1StillUsed or
  197. (isInstruction and findRegDealloc(reg1,endP) and
  198. regLoadedWithNewValue(reg1,false,Taicpu(endP)))) and
  199. (not reg2StillUsed or
  200. (isInstruction and findRegDealloc(reg2,endP) and
  201. regLoadedWithNewValue(reg2,false,Taicpu(endP)))));
  202. { we can't switch reg1 and reg2 in something like }
  203. { movl %reg1,%reg2 }
  204. { movl (%reg2),%reg2 }
  205. { movl 4(%reg1),%reg1 }
  206. if reg2Modified and not(reg1Modified) and
  207. regReadByInstruction(reg1,endP) then
  208. begin
  209. tmpResult := false;
  210. break
  211. end;
  212. if not reg1Modified then
  213. begin
  214. reg1Modified := regModifiedByInstruction(reg1,endP);
  215. if reg1Modified and not canBeFirstSwitch(Taicpu(endP),reg1) then
  216. begin
  217. tmpResult := false;
  218. break;
  219. end;
  220. end;
  221. if not reg2Modified then
  222. reg2Modified := regModifiedByInstruction(reg2,endP);
  223. if sequenceEnd then
  224. break;
  225. tmpResult :=
  226. (endp.typ <> ait_label) and
  227. ((not isInstruction) or
  228. (NoHardCodedRegs(Taicpu(endP),reg1,reg2) and
  229. RegSizesOk(reg1,reg2,Taicpu(endP)) and
  230. (Taicpu(endp).opcode <> A_JMP)));
  231. end;
  232. end;
  233. if tmpResult and sequenceEnd then
  234. begin
  235. switchRegs := true;
  236. reg1Modified := false;
  237. reg2Modified := false;
  238. getNextInstruction(start,hp);
  239. while hp <> endP do
  240. begin
  241. if (not pTaiprop(hp.optinfo)^.canberemoved) and
  242. (hp.typ = ait_instruction) then
  243. begin
  244. switchDone := false;
  245. if not reg1Modified then
  246. begin
  247. reg1Modified := regModifiedByInstruction(reg1,hp);
  248. if reg1Modified then
  249. begin
  250. doFirstSwitch(Taicpu(hp),reg1,reg2);
  251. switchDone := true;
  252. end;
  253. end;
  254. if not switchDone then
  255. if reg1Modified then
  256. doSwitchReg(Taicpu(hp),reg1,reg2)
  257. else
  258. doReplaceReg(Taicpu(hp),reg2,reg1);
  259. end;
  260. getNextInstruction(hp,hp);
  261. end;
  262. if switchLast then
  263. doSwitchReg(Taicpu(hp),reg1,reg2)
  264. else getLastInstruction(hp,hp);
  265. allocRegBetween(asmL,reg1,start,hp);
  266. allocRegBetween(asmL,reg2,start,hp);
  267. end;
  268. end;
  269. procedure doRenaming(asml: TAAsmoutput; first, last: Tai);
  270. var
  271. p: Tai;
  272. begin
  273. p := First;
  274. SkipHead(p);
  275. while p <> last do
  276. begin
  277. case p.typ of
  278. ait_instruction:
  279. begin
  280. case Taicpu(p).opcode of
  281. A_MOV:
  282. begin
  283. if not(pTaiprop(p.optinfo)^.canBeRemoved) and
  284. (Taicpu(p).oper[0].typ = top_reg) and
  285. (Taicpu(p).oper[1].typ = top_reg) and
  286. (Taicpu(p).opsize = S_L) and
  287. (Taicpu(p).oper[0].reg in (usableregs+[R_EDI])) and
  288. (Taicpu(p).oper[1].reg in (usableregs+[R_EDI])) then
  289. if switchRegs(asml,Taicpu(p).oper[0].reg,
  290. Taicpu(p).oper[1].reg,p) then
  291. begin
  292. { getnextinstruction(p,hp);
  293. asmL^.remove(p);
  294. dispose(p,done);
  295. p := hp;
  296. continue }
  297. pTaiprop(p.optinfo)^.canBeRemoved := true;
  298. end;
  299. end;
  300. end;
  301. end;
  302. end;
  303. getNextInstruction(p,p);
  304. end;
  305. end;
  306. End.
  307. {
  308. $Log$
  309. Revision 1.6 2001-01-06 23:35:06 jonas
  310. * fixed webbug 1323
  311. Revision 1.5 2000/12/25 00:07:34 peter
  312. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  313. tlinkedlist objects)
  314. Revision 1.4 2000/12/05 09:32:47 jonas
  315. * fixed bug where "shl $1,%reg" was changed to "leal (%reg),%reg2"
  316. instread of to "leal (,%reg,2),%reg2"
  317. Revision 1.3 2000/11/29 00:30:51 florian
  318. * unused units removed from uses clause
  319. * some changes for widestrings
  320. Revision 1.2 2000/11/22 16:30:04 jonas
  321. * fixed bug where "imul mem32,reg,reg" could be generated
  322. Revision 1.1 2000/10/24 10:40:54 jonas
  323. + register renaming ("fixes" bug1088)
  324. * changed command line options meanings for optimizer:
  325. O2 now means peepholopts, CSE and register renaming in 1 pass
  326. O3 is the same, but repeated until no further optimizations are
  327. possible or until 5 passes have been done (to avoid endless loops)
  328. * changed aopt386 so it does this looping
  329. * added some procedures from csopt386 to the interface because they're
  330. used by rropt386 as well
  331. * some changes to csopt386 and daopt386 so that newly added instructions
  332. by the CSE get optimizer info (they were simply skipped previously),
  333. this fixes some bugs
  334. }