rropt386.pas 12 KB

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