csopt386.pas 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Jonas Maebe, member of the Free Pascal
  4. development team
  5. This unit contains the common subexpression elimination procedure.
  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 CSOpt386;
  20. {$i defines.inc}
  21. Interface
  22. Uses aasm;
  23. {Procedure CSOpt386(First, Last: Pai);}
  24. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  25. Implementation
  26. Uses
  27. {$ifdef replaceregdebug}cutils,{$endif}
  28. verbose, hcodegen, globals,cpubase,cpuasm,DAOpt386, tgeni386;
  29. {
  30. Function PaiInSequence(P: Pai; Const Seq: TContent): Boolean;
  31. Var P1: Pai;
  32. Counter: Byte;
  33. TmpResult: Boolean;
  34. Begin
  35. TmpResult := False;
  36. P1 := Seq.StartMod;
  37. Counter := 1;
  38. While Not(TmpResult) And
  39. (Counter <= Seq.NrOfMods) Do
  40. Begin
  41. If (P = P1) Then TmpResult := True;
  42. Inc(Counter);
  43. p1 := Pai(p1^.Next);
  44. End;
  45. PaiInSequence := TmpResult;
  46. End;
  47. }
  48. function modifiesMemLocation(p1: pai): boolean;
  49. var p: paicpu;
  50. opCount: byte;
  51. begin
  52. modifiesMemLocation := false;
  53. if p1^.typ <> ait_instruction then
  54. exit;
  55. p := paicpu(p1);
  56. for opCount := 1 to MaxCh do
  57. case InsProp[p^.opcode].Ch[opCount] of
  58. Ch_MOp1,CH_WOp1,CH_RWOp1:
  59. if p^.oper[0].typ = top_ref then
  60. begin
  61. modifiesMemLocation := true;
  62. exit
  63. end;
  64. Ch_MOp2,CH_WOp2,CH_RWOp2:
  65. if p^.oper[1].typ = top_ref then
  66. begin
  67. modifiesMemLocation := true;
  68. exit
  69. end;
  70. Ch_MOp3,CH_WOp3,CH_RWOp3:
  71. if p^.oper[2].typ = top_ref then
  72. begin
  73. modifiesMemLocation := true;
  74. exit
  75. end;
  76. Ch_WMemEDI:
  77. begin
  78. modifiesMemLocation := true;
  79. exit;
  80. end;
  81. end;
  82. end;
  83. function getPrevSequence(reg: tregister; current: pai; var prev: pai; var passedJump: boolean):
  84. tregister;
  85. function stillValid(p: pai): boolean;
  86. begin
  87. stillValid :=
  88. (p^.typ = ait_instruction) and
  89. (paicpu(p)^.opcode <> a_jmp) and
  90. (ppaiprop(p^.optinfo)^.regs[reg].state =
  91. ppaiprop(current^.optinfo)^.regs[reg].state) and
  92. { in case destroyreg is called with doIncState = false }
  93. (ppaiprop(p^.optinfo)^.regs[reg].typ =
  94. ppaiprop(current^.optinfo)^.regs[reg].typ);
  95. passedJump :=
  96. (p^.typ = ait_instruction) and
  97. (paicpu(p)^.is_jmp);
  98. end;
  99. function findChangedRegister(p: pai): tregister;
  100. var
  101. regCounter: tregister;
  102. begin
  103. for regCounter := R_EAX to R_EDI do
  104. with ppaiprop(p^.optinfo)^.regs[regCounter] do
  105. if ((startmod <>
  106. ppaiprop(current^.optinfo)^.regs[regCounter].startmod) or
  107. (nrOfMods <>
  108. ppaiprop(current^.optinfo)^.regs[regCounter].nrOfMods)) and
  109. (not ppaiprop(p^.optinfo)^.canBeRemoved) and
  110. (ppaiprop(p^.optinfo)^.regs[regCounter].typ in
  111. [con_ref,con_noRemoveRef]) then
  112. begin
  113. findChangedRegister := regCounter;
  114. exit;
  115. end;
  116. findChangedRegister := R_NO;
  117. end;
  118. var
  119. hp, prevFound: pai;
  120. tmpResult: tregister;
  121. begin
  122. getPrevSequence := R_NO;
  123. { no memory writes (could be refined further) }
  124. passedJump := passedJump or
  125. ((current^.typ = ait_instruction) and
  126. (paicpu(current)^.is_jmp));
  127. if modifiesMemLocation(current) or
  128. (passedJump and not(reg in (usableregs+[R_EDI]))) or
  129. not getLastInstruction(current,hp) then
  130. exit;
  131. tmpResult := R_NO;
  132. while (tmpResult = R_NO) and
  133. stillValid(hp) do
  134. begin
  135. { in case getPreviousInstruction fails and sets hp to nil in the }
  136. { next iteration }
  137. prevFound := hp;
  138. tmpResult := findChangedRegister(hp);
  139. if modifiesMemLocation(hp) or
  140. { do not load the self pointer or a regvar before a (conditional) }
  141. { jump with a new value, since if the jump is taken, the old value }
  142. { is (probably) still necessary }
  143. (passedJump and not(reg in (usableregs+[R_EDI]))) or
  144. not getLastInstruction(hp,hp) then
  145. break;
  146. end;
  147. getPrevSequence := tmpResult;
  148. if tmpResult <> R_NO then
  149. prev := prevFound;
  150. end;
  151. {checks whether the current instruction sequence (starting with p) and the
  152. one between StartMod and EndMod of Reg are the same. If so, the number of
  153. instructions that match is stored in Found and true is returned, otherwise
  154. Found holds the number of instructions between StartMod and EndMod and false
  155. is returned}
  156. Function CheckSequence(p: Pai; var prev: pai; Reg: TRegister; Var Found: Longint;
  157. Var RegInfo: TRegInfo): Boolean;
  158. function getNextRegToTest(var orgP: pai; currentReg: tregister): tregister;
  159. const
  160. checkingPrevSequences: boolean = false;
  161. passedJump: boolean = false;
  162. begin
  163. if currentReg = R_NO then
  164. checkingPrevSequences := false;
  165. if not checkingPrevSequences then
  166. begin
  167. Repeat
  168. Inc(currentReg);
  169. Until (currentReg > R_EDI) or
  170. (ppaiprop(orgP^.optInfo)^.regs[currentReg].typ
  171. in [con_ref,con_noRemoveRef]);
  172. if currentReg > R_EDI then
  173. begin
  174. if not modifiesMemLocation(orgP) and
  175. (ppaiprop(orgP^.optinfo)^.regs[reg].rstate =
  176. ppaiprop(p^.optinfo)^.regs[reg].rstate) then
  177. begin
  178. checkingPrevSequences := true;
  179. passedJump := false;
  180. end
  181. else
  182. getNextRegToTest := R_NO;
  183. end
  184. else getNextRegToTest := currentReg;
  185. end;
  186. if checkingPrevSequences then
  187. getNextRegToTest := getPrevSequence(reg,orgP,orgP, passedJump);
  188. end;
  189. Var hp2, hp3{, EndMod},highPrev, orgPrev: Pai;
  190. {Cnt,} OldNrOfMods: Longint;
  191. startRegInfo, OrgRegInfo, HighRegInfo: TRegInfo;
  192. HighFound, OrgRegFound: Byte;
  193. RegCounter, regCounter2: TRegister;
  194. OrgRegResult: Boolean;
  195. TmpResult: Boolean;
  196. {TmpState: Byte;}
  197. Begin {CheckSequence}
  198. Reg := Reg32(Reg);
  199. TmpResult := False;
  200. FillChar(OrgRegInfo, SizeOf(OrgRegInfo), 0);
  201. FillChar(startRegInfo, sizeof(startRegInfo), 0);
  202. OrgRegFound := 0;
  203. HighFound := 0;
  204. OrgRegResult := False;
  205. with startRegInfo do
  206. begin
  207. newRegsEncountered := [procinfo^.FramePointer, stack_pointer];
  208. new2OldReg[procinfo^.FramePointer] := procinfo^.FramePointer;
  209. new2OldReg[stack_pointer] := stack_pointer;
  210. oldRegsEncountered := newRegsEncountered;
  211. end;
  212. GetLastInstruction(p, prev);
  213. regCounter := getNextRegToTest(prev,R_NO);
  214. While (RegCounter <> R_NO) Do
  215. Begin
  216. regInfo := startRegInfo;
  217. Found := 0;
  218. hp2 := PPaiProp(prev^.OptInfo)^.Regs[RegCounter].StartMod;
  219. If (prev <> PPaiProp(prev^.OptInfo)^.Regs[RegCounter].StartMod)
  220. Then OldNrOfMods := PPaiProp(prev^.OptInfo)^.Regs[RegCounter].NrOfMods
  221. Else OldNrOfMods := 1;
  222. hp3 := p;
  223. While (Found <> OldNrOfMods) And
  224. { old new }
  225. InstructionsEquivalent(hp2, hp3, RegInfo) Do
  226. Begin
  227. if (hp3^.typ = ait_instruction) and
  228. ((paicpu(hp3)^.opcode = A_MOV) or
  229. (paicpu(hp3)^.opcode = A_MOVZX) or
  230. (paicpu(hp3)^.opcode = A_MOVSX)) and
  231. (paicpu(hp3)^.oper[0].typ in
  232. [top_const,top_ref,top_symbol]) and
  233. (paicpu(hp3)^.oper[1].typ = top_reg) and
  234. not(regInRef(reg32(paicpu(hp3)^.oper[1].reg),
  235. paicpu(hp3)^.oper[0].ref^)) then
  236. regInfo.lastReload
  237. [reg32(paicpu(hp3)^.oper[1].reg)] := hp3;
  238. GetNextInstruction(hp2, hp2);
  239. GetNextInstruction(hp3, hp3);
  240. Inc(Found)
  241. End;
  242. for regCounter2 := R_EAX to R_EDX do
  243. if (regInfo.new2OldReg[regCounter2] <> R_NO) and
  244. (regCounter2 in PPaiProp(hp3^.optInfo)^.usedRegs) and
  245. not regLoadedWithNewValue(regCounter2,false,hp3) then
  246. include(regInfo.regsStillUsedAfterSeq,regCounter2);
  247. If (Found <> OldNrOfMods) or
  248. { the following is to avoid problems with rangecheck code (see testcse2) }
  249. (assigned(hp3) and
  250. ((reg in regInfo.regsLoadedForRef) and
  251. (reg in PPaiProp(hp3^.optInfo)^.usedRegs) and
  252. not regLoadedWithNewValue(reg,false,hp3))) then
  253. Begin
  254. TmpResult := False;
  255. If (found > 0) then
  256. {this is correct because we only need to turn off the CanBeRemoved flag
  257. when an instruction has already been processed by CheckSequence
  258. (otherwise CanBeRemoved can't be true and thus can't have to be turned off).
  259. If it has already been processed by CheckSequence and flagged to be
  260. removed, it means that it has been checked against a previous sequence
  261. and that it was equal (otherwise CheckSequence would have returned false
  262. and the instruction wouldn't have been removed). If this "If found > 0"
  263. check is left out, incorrect optimizations are performed.}
  264. Found := PPaiProp(Pai(p)^.OptInfo)^.Regs[Reg].NrOfMods
  265. End
  266. Else TmpResult := True;
  267. If TmpResult And
  268. (Found > HighFound)
  269. Then
  270. Begin
  271. highPrev := prev;
  272. HighFound := Found;
  273. HighRegInfo := RegInfo;
  274. End;
  275. If (RegCounter = Reg) Then
  276. Begin
  277. orgPrev := prev;
  278. OrgRegFound := Found;
  279. OrgRegResult := TmpResult;
  280. OrgRegInfo := RegInfo
  281. End;
  282. regCounter := getNextRegToTest(prev,regCounter);
  283. End;
  284. If (HighFound > 0) And
  285. (Not(OrgRegResult) Or
  286. (HighFound > OrgRegFound))
  287. Then
  288. Begin
  289. {$ifndef fpc}
  290. TmpResult := True;
  291. {$else fpc}
  292. CheckSequence := True;
  293. {$endif fpc}
  294. prev := highPrev;
  295. RegInfo := HighRegInfo;
  296. Found := HighFound
  297. End
  298. Else
  299. Begin
  300. {$ifndef fpc}
  301. TmpResult := OrgRegResult;
  302. {$else fpc}
  303. CheckSequence := OrgRegResult;
  304. {$endif fpc}
  305. prev := orgPrev;
  306. Found := OrgRegFound;
  307. RegInfo := OrgRegInfo;
  308. End;
  309. {$ifndef fpc}
  310. CheckSequence := TmpResult;
  311. {$endif fpc}
  312. End; {CheckSequence}
  313. Procedure SetAlignReg(p: Pai);
  314. Const alignSearch = 12;
  315. var regsUsable: TRegSet;
  316. prevInstrCount, nextInstrCount: Longint;
  317. prevState, nextWState,nextRState: Array[R_EAX..R_EDI] of byte;
  318. regCounter, lastRemoved: TRegister;
  319. prev, next: Pai;
  320. {$ifdef alignregdebug}
  321. temp: Pai;
  322. {$endif alignregdebug}
  323. begin
  324. regsUsable := [R_EAX,R_ECX,R_EDX,R_EBX,{R_ESP,R_EBP,}R_ESI,R_EDI];
  325. for regCounter := R_EAX to R_EDI do
  326. begin
  327. prevState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
  328. nextWState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
  329. nextRState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].rState;
  330. end;
  331. getLastInstruction(p,prev);
  332. getNextInstruction(p,next);
  333. lastRemoved := pai_align(p)^.reg;
  334. nextInstrCount := 0;
  335. prevInstrCount := 0;
  336. while ((assigned(prev) and
  337. assigned(prev^.optInfo) and
  338. (prevInstrCount < alignSearch)) or
  339. (assigned(next) and
  340. assigned(next^.optInfo) and
  341. (nextInstrCount < alignSearch))) And
  342. (regsUsable <> []) do
  343. begin
  344. {$ifdef alignregdebug}
  345. if assigned(prev) then
  346. begin
  347. temp := new(pai_asm_comment,init(strpnew('got here')));
  348. temp^.next := prev^.next;
  349. temp^.previous := prev;
  350. prev^.next := temp;
  351. if assigned(temp^.next) then
  352. temp^.next^.previous := temp;
  353. end;
  354. {$endif alignregdebug}
  355. if assigned(prev) and assigned(prev^.optinfo) and
  356. (prevInstrCount < alignSearch) then
  357. begin
  358. if (prev^.typ = ait_instruction) And
  359. (insProp[PaiCpu(prev)^.opcode].ch[1] <> Ch_ALL) and
  360. (PaiCpu(prev)^.opcode <> A_JMP) then
  361. begin
  362. inc(prevInstrCount);
  363. for regCounter := R_EAX to R_EDI do
  364. begin
  365. if (regCounter in regsUsable) And
  366. (PPaiProp(prev^.optInfo)^.Regs[regCounter].wState <>
  367. prevState[regCounter]) then
  368. begin
  369. lastRemoved := regCounter;
  370. exclude(regsUsable,regCounter);
  371. {$ifdef alignregdebug}
  372. temp := new(pai_asm_comment,init(strpnew(
  373. att_reg2str[regCounter]+' removed')));
  374. temp^.next := prev^.next;
  375. temp^.previous := prev;
  376. prev^.next := temp;
  377. if assigned(temp^.next) then
  378. temp^.next^.previous := temp;
  379. if regsUsable = [] then
  380. begin
  381. temp := new(pai_asm_comment,init(strpnew(
  382. 'regsUsable empty here')));
  383. temp^.next := prev^.next;
  384. temp^.previous := prev;
  385. prev^.next := temp;
  386. if assigned(temp^.next) then
  387. temp^.next^.previous := temp;
  388. end;
  389. {$endif alignregdebug}
  390. end;
  391. prevState[regCounter] :=
  392. PPaiProp(prev^.optInfo)^.Regs[regCounter].wState;
  393. end;
  394. getLastInstruction(prev,prev);
  395. end
  396. else
  397. If GetLastInstruction(prev,prev) and
  398. assigned(prev^.optinfo) then
  399. for regCounter := R_EAX to R_EDI do
  400. prevState[regCounter] :=
  401. PPaiProp(prev^.optInfo)^.Regs[regCounter].wState
  402. end;
  403. if assigned(next) and assigned(next^.optInfo) and
  404. (nextInstrCount < alignSearch) then
  405. begin
  406. if (next^.typ = ait_instruction) and
  407. (insProp[PaiCpu(next)^.opcode].ch[1] <> Ch_ALL) and
  408. (PaiCpu(next)^.opcode <> A_JMP) then
  409. begin
  410. inc(nextInstrCount);
  411. for regCounter := R_EAX to R_EDI do
  412. begin
  413. if (regCounter in regsUsable) And
  414. ((PPaiProp(next^.optInfo)^.Regs[regCounter].wState <>
  415. nextWState[regCounter]) or
  416. (PPaiProp(next^.optInfo)^.Regs[regCounter].rState <>
  417. nextRState[regCounter])) Then
  418. begin
  419. lastRemoved := regCounter;
  420. exclude(regsUsable,regCounter);
  421. {$ifdef alignregdebug}
  422. temp := new(pai_asm_comment,init(strpnew(
  423. att_reg2str[regCounter]+' removed')));
  424. temp^.next := next^.next;
  425. temp^.previous := next;
  426. next^.next := temp;
  427. if assigned(temp^.next) then
  428. temp^.next^.previous := temp;
  429. if regsUsable = [] then
  430. begin
  431. temp := new(pai_asm_comment,init(strpnew(
  432. 'regsUsable empty here')));
  433. temp^.next := next^.next;
  434. temp^.previous := next;
  435. next^.next := temp;
  436. if assigned(temp^.next) then
  437. temp^.next^.previous := temp;
  438. end;
  439. {$endif alignregdebug}
  440. end;
  441. nextWState[regCounter] :=
  442. PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
  443. nextRState[regCounter] :=
  444. PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
  445. end
  446. end
  447. else
  448. for regCounter := R_EAX to R_EDI do
  449. begin
  450. nextWState[regCounter] :=
  451. PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
  452. nextRState[regCounter] :=
  453. PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
  454. end;
  455. getNextInstruction(next,next);
  456. end;
  457. end;
  458. if regsUsable <> [] then
  459. for regCounter := R_EAX to R_EDI do
  460. if regCounter in regsUsable then
  461. begin
  462. lastRemoved := regCounter;
  463. break
  464. end;
  465. {$ifdef alignregdebug}
  466. next := new(pai_asm_comment,init(strpnew(att_reg2str[lastRemoved]+
  467. ' chosen as alignment register')));
  468. next^.next := p^.next;
  469. next^.previous := p;
  470. p^.next := next;
  471. if assigned(next^.next) then
  472. next^.next^.previous := next;
  473. {$endif alignregdebug}
  474. pai_align(p)^.reg := lastRemoved;
  475. End;
  476. Procedure RestoreRegContentsTo(reg: TRegister; const c: TContent; p, endP: pai);
  477. var
  478. {$ifdef replaceregdebug}
  479. hp: pai;
  480. l: longint;
  481. {$endif replaceregdebug}
  482. tmpState: byte;
  483. begin
  484. {$ifdef replaceregdebug}
  485. l := random(1000);
  486. hp := new(pai_asm_comment,init(strpnew(
  487. 'restored '+att_reg2str[reg]+' with data from here... '+tostr(l))));
  488. hp^.next := p;
  489. hp^.previous := p^.previous;
  490. p^.previous := hp;
  491. if assigned(hp^.previous) then
  492. hp^.previous^.next := hp;
  493. {$endif replaceregdebug}
  494. { PPaiProp(p^.optInfo)^.Regs[reg] := c;}
  495. While (p <> endP) Do
  496. Begin
  497. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  498. getNextInstruction(p,p);
  499. end;
  500. tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
  501. repeat
  502. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  503. until not getNextInstruction(p,p) or
  504. (PPaiProp(p^.optInfo)^.Regs[reg].wState <> tmpState);
  505. {$ifdef replaceregdebug}
  506. if assigned(p) then
  507. begin
  508. hp := new(pai_asm_comment,init(strpnew(
  509. 'restored '+att_reg2str[reg]+' till here... '+tostr(l))));
  510. hp^.next := p;
  511. hp^.previous := p^.previous;
  512. p^.previous := hp;
  513. if assigned(hp^.previous) then
  514. hp^.previous^.next := hp;
  515. end;
  516. {$endif replaceregdebug}
  517. end;
  518. function FindRegDealloc(reg: tregister; p: pai): boolean;
  519. { assumes reg is a 32bit register }
  520. var
  521. hp: pai;
  522. first: boolean;
  523. begin
  524. findregdealloc := false;
  525. first := true;
  526. while assigned(p^.previous) and
  527. ((Pai(p^.previous)^.typ in (skipinstr+[ait_align])) or
  528. ((Pai(p^.previous)^.typ = ait_label) and
  529. labelCanBeSkipped(pai_label(p^.previous)))) do
  530. begin
  531. p := pai(p^.previous);
  532. if (p^.typ = ait_regalloc) and
  533. (pairegalloc(p)^.reg = reg) then
  534. if not(pairegalloc(p)^.allocation) then
  535. if first then
  536. begin
  537. findregdealloc := true;
  538. break;
  539. end
  540. else
  541. begin
  542. findRegDealloc :=
  543. getNextInstruction(p,hp) and
  544. regLoadedWithNewValue(reg,false,hp);
  545. break
  546. end
  547. else
  548. first := false;
  549. end
  550. end;
  551. Procedure ClearRegContentsFrom(reg: TRegister; p, endP: pai);
  552. { first clears the contents of reg from p till endP. Then the contents are }
  553. { cleared until the first instruction that changes reg }
  554. var
  555. {$ifdef replaceregdebug}
  556. hp: pai;
  557. l: longint;
  558. {$endif replaceregdebug}
  559. oldStartmod: pai;
  560. begin
  561. {$ifdef replaceregdebug}
  562. l := random(1000);
  563. hp := new(pai_asm_comment,init(strpnew(
  564. 'cleared '+att_reg2str[reg]+' from here... '+tostr(l))));
  565. hp^.next := p;
  566. hp^.previous := p^.previous;
  567. p^.previous := hp;
  568. if assigned(hp^.previous) then
  569. hp^.previous^.next := hp;
  570. {$endif replaceregdebug}
  571. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  572. While (p <> endP) Do
  573. Begin
  574. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  575. getNextInstruction(p,p);
  576. end;
  577. oldStartmod := PPaiProp(p^.optInfo)^.Regs[reg].startmod;
  578. repeat
  579. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  580. until not getNextInstruction(p,p) or
  581. (PPaiProp(p^.optInfo)^.Regs[reg].startmod <> oldStartmod);
  582. {$ifdef replaceregdebug}
  583. if assigned(p) then
  584. begin
  585. hp := new(pai_asm_comment,init(strpnew(
  586. 'cleared '+att_reg2str[reg]+' till here... '+tostr(l))));
  587. hp^.next := p;
  588. hp^.previous := p^.previous;
  589. p^.previous := hp;
  590. if assigned(hp^.previous) then
  591. hp^.previous^.next := hp;
  592. end;
  593. {$endif replaceregdebug}
  594. end;
  595. function NoHardCodedRegs(p: paicpu; orgReg, newReg: tRegister): boolean;
  596. var chCount: byte;
  597. begin
  598. case p^.opcode of
  599. A_IMUL: noHardCodedRegs := p^.ops <> 1;
  600. A_SHL,A_SHR,A_SHLD,A_SHRD: noHardCodedRegs :=
  601. (p^.oper[0].typ <> top_reg) or
  602. ((orgReg <> R_ECX) and (newReg <> R_ECX));
  603. else
  604. begin
  605. NoHardCodedRegs := true;
  606. with InsProp[p^.opcode] do
  607. for chCount := 1 to MaxCh do
  608. if Ch[chCount] in ([Ch_REAX..Ch_MEDI,Ch_WMemEDI,Ch_All]-[Ch_RESP,Ch_WESP,Ch_RWESP]) then
  609. begin
  610. NoHardCodedRegs := false;
  611. break
  612. end;
  613. end;
  614. end;
  615. end;
  616. function ChangeReg(var Reg: TRegister; orgReg, newReg: TRegister): boolean;
  617. begin
  618. changeReg := true;
  619. if reg = newReg then
  620. reg := orgReg
  621. else if reg = regtoreg8(newReg) then
  622. reg := regtoreg8(orgReg)
  623. else if reg = regtoreg16(newReg) then
  624. reg := regtoreg16(orgReg)
  625. else changeReg := false;
  626. end;
  627. function changeOp(var o: toper; orgReg, newReg: tregister): boolean;
  628. begin
  629. case o.typ of
  630. top_reg: changeOp := changeReg(o.reg,orgReg,newReg);
  631. top_ref:
  632. begin
  633. changeOp :=
  634. changeReg(o.ref^.base,orgReg,newReg) or
  635. changeReg(o.ref^.index,orgReg,newReg);
  636. end;
  637. end;
  638. end;
  639. procedure updateStates(orgReg,newReg: tregister; hp: pai; writeStateToo: boolean);
  640. var
  641. prev: pai;
  642. newOrgRegRState, newOrgRegWState: byte;
  643. begin
  644. if getLastInstruction(hp,prev) then
  645. with ppaiprop(prev^.optinfo)^ do
  646. begin
  647. newOrgRegRState := regs[orgReg].rState +
  648. ppaiprop(hp^.optinfo)^.regs[newReg].rState - regs[newReg].rstate;
  649. if writeStateToo then
  650. newOrgRegWState := regs[orgReg].wState +
  651. ppaiprop(hp^.optinfo)^.regs[newReg].wState - regs[newReg].wstate;
  652. end
  653. else
  654. with ppaiprop(hp^.optinfo)^.regs[newReg] do
  655. begin
  656. newOrgRegRState := rState;
  657. if writeStateToo then
  658. newOrgRegWState := wState;
  659. end;
  660. with ppaiprop(hp^.optinfo)^.regs[orgReg] do
  661. begin
  662. rState := newOrgRegRState;
  663. if writeStateToo then
  664. wState := newOrgRegwState;
  665. end;
  666. end;
  667. function doReplaceReg(orgReg,newReg: tregister; hp: paicpu): boolean;
  668. var
  669. opCount: byte;
  670. tmpResult: boolean;
  671. begin
  672. for opCount := 0 to 2 do
  673. tmpResult :=
  674. changeOp(hp^.oper[opCount],orgReg,newReg) or tmpResult;
  675. doReplaceReg := tmpResult;
  676. end;
  677. function RegSizesOK(oldReg,newReg: TRegister; p: paicpu): boolean;
  678. { oldreg and newreg must be 32bit components }
  679. var opCount: byte;
  680. begin
  681. RegSizesOK := true;
  682. { if only one of them is a general purpose register ... }
  683. if (IsGP32reg(oldReg) xor IsGP32Reg(newReg)) then
  684. begin
  685. for opCount := 0 to 2 do
  686. if (p^.oper[opCount].typ = top_reg) and
  687. (p^.oper[opCount].reg in [R_AL..R_DH]) then
  688. begin
  689. RegSizesOK := false;
  690. break
  691. end
  692. end;
  693. end;
  694. function doReplaceReadReg(orgReg,newReg: tregister; p: paicpu): boolean;
  695. var opCount: byte;
  696. begin
  697. doReplaceReadReg := false;
  698. { handle special case }
  699. case p^.opcode of
  700. A_IMUL:
  701. begin
  702. case p^.ops of
  703. 1: internalerror(1301001);
  704. 2,3:
  705. begin
  706. if changeOp(p^.oper[0],orgReg,newReg) then
  707. begin
  708. { updateStates(orgReg,newReg,p,false);}
  709. doReplaceReadReg := true;
  710. end;
  711. if p^.ops = 3 then
  712. if changeOp(p^.oper[1],orgReg,newReg) then
  713. begin
  714. { updateStates(orgReg,newReg,p,false);}
  715. doReplaceReadReg := true;
  716. end;
  717. end;
  718. end;
  719. end;
  720. A_DIV,A_IDIV,A_MUL: internalerror(1301002);
  721. else
  722. begin
  723. for opCount := 0 to 2 do
  724. if p^.oper[opCount].typ = top_ref then
  725. if changeOp(p^.oper[opCount],orgReg,newReg) then
  726. begin
  727. { updateStates(orgReg,newReg,p,false);}
  728. doReplaceReadReg := true;
  729. end;
  730. for opCount := 1 to MaxCh do
  731. case InsProp[p^.opcode].Ch[opCount] of
  732. Ch_ROp1:
  733. if p^.oper[0].typ = top_reg then
  734. if changeReg(p^.oper[0].reg,orgReg,newReg) then
  735. begin
  736. { updateStates(orgReg,newReg,p,false);}
  737. doReplaceReadReg := true;
  738. end;
  739. Ch_ROp2:
  740. if p^.oper[1].typ = top_reg then
  741. if changeReg(p^.oper[1].reg,orgReg,newReg) then
  742. begin
  743. { updateStates(orgReg,newReg,p,false);}
  744. doReplaceReadReg := true;
  745. end;
  746. Ch_ROp3:
  747. if p^.oper[2].typ = top_reg then
  748. if changeReg(p^.oper[2].reg,orgReg,newReg) then
  749. begin
  750. { updateStates(orgReg,newReg,p,false);}
  751. doReplaceReadReg := true;
  752. end;
  753. end;
  754. end;
  755. end;
  756. end;
  757. procedure updateState(reg: tregister; p: pai);
  758. { this procedure updates the read and write states of the instructions }
  759. { coming after p. It's called when the read/write state of p has been }
  760. { changed and this change has to be propagated to the following }
  761. { instructions as well }
  762. var
  763. newRState, newWState: byte;
  764. prevRState, prevWState: byte;
  765. doRState, doWState: boolean;
  766. begin
  767. { get the new read/write states from p }
  768. with ppaiprop(p^.optinfo)^.regs[reg] do
  769. begin
  770. newRState := rState;
  771. newWState := wState;
  772. end;
  773. if not GetNextInstruction(p,p) then
  774. exit;
  775. { get the old read/write states from the next instruction, to know }
  776. { when we can stop updating }
  777. with ppaiprop(p^.optinfo)^.regs[reg] do
  778. begin
  779. prevRState := rState;
  780. prevWState := wState;
  781. end;
  782. { adjust the states if this next instruction reads/writes the register }
  783. if regReadByInstruction(reg,p) then
  784. incState(newRState,1);
  785. if regModifiedByInstruction(reg,p) then
  786. incState(newWState,1);
  787. { do we still have to update the read and/or write states? }
  788. doRState := true;
  789. doWState := true;
  790. repeat
  791. { update the states }
  792. with ppaiprop(p^.optinfo)^.regs[reg] do
  793. begin
  794. if doRState then
  795. rState := newRState;
  796. if doWState then
  797. wState := newWState;
  798. end;
  799. if not getNextInstruction(p,p) then
  800. break;
  801. with ppaiprop(p^.optinfo)^.regs[reg] do
  802. begin
  803. { stop updating the read state if it changes }
  804. doRState :=
  805. doRState and (rState = prevRState);
  806. { if, by accident, this changed state is the same as the one }
  807. { we've been using, change it to a value that's definitely }
  808. { different from the previous and next state }
  809. if not doRState and
  810. (rState = newRState) then
  811. begin
  812. incState(newRState,1);
  813. prevRState := rState;
  814. doRState := true;
  815. end;
  816. { ditto for the write state }
  817. doWState :=
  818. doWState and (WState = prevWState);
  819. if not doWState and
  820. (wState = newWState) then
  821. begin
  822. incState(newWState,1);
  823. prevWState := wState;
  824. doWState := true;
  825. end;
  826. end;
  827. { stop when we don't have to update either state anymore }
  828. until not(doRState or doWState);
  829. end;
  830. function ReplaceReg(asmL: PaasmOutput; orgReg, newReg: TRegister; p: pai;
  831. const c: TContent; orgRegCanBeModified: Boolean;
  832. var returnEndP: pai): Boolean;
  833. { Tries to replace orgreg with newreg in all instructions coming after p }
  834. { until orgreg gets loaded with a new value. Returns true if successful, }
  835. { false otherwise. If successful, the contents of newReg are set to c, }
  836. { which should hold the contents of newReg before the current sequence }
  837. { started }
  838. { if the function returns true, returnEndP holds the last instruction }
  839. { where newReg was replaced by orgReg }
  840. var endP, hp: Pai;
  841. removeLast, sequenceEnd, tmpResult, newRegModified, orgRegRead,
  842. stateChanged, readStateChanged: Boolean;
  843. function storeBack(p1: pai): boolean;
  844. { returns true if p1 contains an instruction that stores the contents }
  845. { of newReg back to orgReg }
  846. begin
  847. storeBack :=
  848. (p1^.typ = ait_instruction) and
  849. (paicpu(p1)^.opcode = A_MOV) and
  850. (paicpu(p1)^.oper[0].typ = top_reg) and
  851. (paicpu(p1)^.oper[0].reg = newReg) and
  852. (paicpu(p1)^.oper[1].typ = top_reg) and
  853. (paicpu(p1)^.oper[1].reg = orgReg);
  854. end;
  855. begin
  856. ReplaceReg := false;
  857. tmpResult := true;
  858. sequenceEnd := false;
  859. newRegModified := false;
  860. orgRegRead := false;
  861. removeLast := false;
  862. endP := p;
  863. while tmpResult and not sequenceEnd do
  864. begin
  865. tmpResult :=
  866. getNextInstruction(endP,endP) and
  867. (endP^.typ = ait_instruction);
  868. if tmpresult and not assigned(endP^.optInfo) then
  869. begin
  870. { hp := new(pai_asm_comment,init(strpnew('next no optinfo')));
  871. hp^.next := endp;
  872. hp^.previous := endp^.previous;
  873. endp^.previous := hp;
  874. if assigned(hp^.previous) then
  875. hp^.previous^.next := hp;}
  876. exit;
  877. end;
  878. If tmpResult and
  879. { don't take into account instructions that will be removed }
  880. Not (PPaiProp(endP^.optInfo)^.canBeRemoved) then
  881. begin
  882. { if the newReg gets stored back to the oldReg, we can change }
  883. { "mov %oldReg,%newReg; <operations on %newReg>; mov %newReg, }
  884. { %oldReg" to "<operations on %oldReg>" }
  885. removeLast := storeBack(endP);
  886. sequenceEnd :=
  887. { no support for (i)div, mul and imul with hardcoded operands }
  888. (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
  889. { if newReg gets loaded with a new value, we can stop }
  890. { replacing newReg with oldReg here (possibly keeping }
  891. { the original contents of oldReg so we still know them }
  892. { afterwards) }
  893. RegLoadedWithNewValue(newReg,true,paicpu(endP)) or
  894. { we can also stop if we reached the end of the use of }
  895. { newReg's current contents }
  896. (GetNextInstruction(endp,hp) and
  897. FindRegDealloc(newReg,hp)));
  898. { to be able to remove the first and last instruction of }
  899. { movl %reg1, %reg2 }
  900. { <operations on %reg2> (replacing reg2 with reg1 here) }
  901. { movl %reg2, %reg1 }
  902. { %reg2 must not be use afterwards (it can be as the }
  903. { result of a peepholeoptimization) }
  904. removeLast := removeLast and sequenceEnd;
  905. newRegModified :=
  906. newRegModified or
  907. (not(regLoadedWithNewValue(newReg,true,paicpu(endP))) and
  908. RegModifiedByInstruction(newReg,endP));
  909. orgRegRead := newRegModified and RegReadByInstruction(orgReg,endP);
  910. sequenceEnd := SequenceEnd and
  911. (removeLast or
  912. { since newReg will be replaced by orgReg, we can't allow that newReg }
  913. { gets modified if orgReg is still read afterwards (since after }
  914. { replacing, this would mean that orgReg first gets modified and then }
  915. { gets read in the assumption it still contains the unmodified value) }
  916. not(newRegModified and orgRegRead)) (* and
  917. { since newReg will be replaced by orgReg, we can't allow that newReg }
  918. { gets modified if orgRegCanBeModified = false }
  919. { this now gets checked after the loop (JM) }
  920. (orgRegCanBeModified or not(newRegModified)) *);
  921. tmpResult :=
  922. not(removeLast) and
  923. not(newRegModified and orgRegRead) and
  924. (* (orgRegCanBeModified or not(newRegModified)) and *)
  925. (endP^.typ = ait_instruction) and
  926. not(paicpu(endP)^.is_jmp) and
  927. NoHardCodedRegs(paicpu(endP),orgReg,newReg) and
  928. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  929. not RegModifiedByInstruction(orgReg,endP);
  930. end;
  931. end;
  932. sequenceEnd := sequenceEnd and
  933. (removeLast or
  934. (orgRegCanBeModified or not(newRegModified))) and
  935. (not(assigned(endp)) or
  936. not(endp^.typ = ait_instruction) or
  937. (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
  938. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  939. not(newRegModified and
  940. (orgReg in PPaiProp(endP^.optInfo)^.usedRegs) and
  941. not(RegLoadedWithNewValue(orgReg,true,paicpu(endP))))));
  942. if SequenceEnd then
  943. begin
  944. {$ifdef replaceregdebug}
  945. hp := new(pai_asm_comment,init(strpnew(
  946. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  947. ' from here...')));
  948. hp^.next := p;
  949. hp^.previous := p^.previous;
  950. p^.previous := hp;
  951. if assigned(hp^.previous) then
  952. hp^.previous^.next := hp;
  953. hp := new(pai_asm_comment,init(strpnew(
  954. 'replaced '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  955. ' till here')));
  956. hp^.next := endp^.next;
  957. hp^.previous := endp;
  958. endp^.next := hp;
  959. if assigned(hp^.next) then
  960. hp^.next^.previous := hp;
  961. {$endif replaceregdebug}
  962. replaceReg := true;
  963. returnEndP := endP;
  964. getNextInstruction(p,hp);
  965. stateChanged := false;
  966. while hp <> endP do
  967. begin
  968. if {not(PPaiProp(hp^.optInfo)^.canBeRemoved) and }
  969. (hp^.typ = ait_instruction) then
  970. stateChanged :=
  971. doReplaceReg(orgReg,newReg,paicpu(hp)) or stateChanged;
  972. if stateChanged then
  973. updateStates(orgReg,newReg,hp,true);
  974. getNextInstruction(hp,hp)
  975. end;
  976. if assigned(endp) and (endp^.typ = ait_instruction) then
  977. readStateChanged :=
  978. DoReplaceReadReg(orgReg,newReg,paicpu(endP));
  979. if stateChanged or readStateChanged then
  980. updateStates(orgReg,newReg,endP,stateChanged);
  981. if stateChanged or readStateChanged then
  982. updateState(orgReg,endP);
  983. { the replacing stops either at the moment that }
  984. { a) the newreg gets loaded with a new value (one not depending on the }
  985. { current value of newreg) }
  986. { b) newreg is completely replaced in this sequence and it's current value }
  987. { isn't used anymore }
  988. { In case b, the newreg was completely replaced by oldreg, so it's contents }
  989. { are unchanged compared the start of this sequence, so restore them }
  990. If removeLast or
  991. RegLoadedWithNewValue(newReg,true,endP) then
  992. GetLastInstruction(endP,hp)
  993. else hp := endP;
  994. if removeLast or
  995. (p <> endp) or
  996. not RegLoadedWithNewValue(newReg,true,endP) then
  997. RestoreRegContentsTo(newReg,c,p,hp);
  998. { In both case a and b, it is possible that the new register was modified }
  999. { (e.g. an add/sub), so if it was replaced by oldreg in that instruction, }
  1000. { oldreg's contents have been changed. To take this into account, we simply }
  1001. { set the contents of orgreg to "unknown" after this sequence }
  1002. if newRegModified then
  1003. ClearRegContentsFrom(orgReg,p,hp);
  1004. if removeLast then
  1005. ppaiprop(endP^.optinfo)^.canBeRemoved := true;
  1006. allocRegBetween(asml,orgReg,p,endP);
  1007. end
  1008. {$ifdef replaceregdebug}
  1009. else
  1010. begin
  1011. hp := new(pai_asm_comment,init(strpnew(
  1012. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  1013. ' from here...')));
  1014. hp^.previous := p^.previous;
  1015. hp^.next := p;
  1016. p^.previous := hp;
  1017. if assigned(hp^.previous) then
  1018. hp^.previous^.next := hp;
  1019. hp := new(pai_asm_comment,init(strpnew(
  1020. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  1021. ' failed here')));
  1022. hp^.next := endp^.next;
  1023. hp^.previous := endp;
  1024. endp^.next := hp;
  1025. if assigned(hp^.next) then
  1026. hp^.next^.previous := hp;
  1027. end;
  1028. {$endif replaceregdebug}
  1029. End;
  1030. Function FindRegWithConst(p: Pai; size: topsize; l: longint; Var Res: TRegister): Boolean;
  1031. {Finds a register which contains the constant l}
  1032. Var Counter: TRegister;
  1033. {$ifdef testing}
  1034. hp: pai;
  1035. {$endif testing}
  1036. tmpresult: boolean;
  1037. Begin
  1038. Counter := R_NO;
  1039. repeat
  1040. inc(counter);
  1041. tmpresult := (ppaiprop(p^.optInfo)^.regs[counter].typ in
  1042. [con_const,con_noRemoveConst]) and
  1043. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.opsize = size) and
  1044. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].typ = top_const) and
  1045. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].val = l);
  1046. {$ifdef testing}
  1047. if (ppaiprop(p^.optInfo)^.regs[counter].typ in [con_const,con_noRemoveConst]) then
  1048. begin
  1049. hp := new(pai_asm_comment,init(strpnew(
  1050. 'checking const load of '+tostr(l)+' here...')));
  1051. hp^.next := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod;
  1052. hp^.previous := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous;
  1053. PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous := hp;
  1054. if assigned(hp^.previous) then
  1055. hp^.previous^.next := hp;
  1056. end;
  1057. {$endif testing}
  1058. until tmpresult or (Counter = R_EDI);
  1059. res := counter;
  1060. FindRegWithConst := tmpResult;
  1061. End;
  1062. procedure removePrevNotUsedLoad(p: pai; reg: tRegister; check: boolean);
  1063. { If check = true, it means the procedure has to check whether it isn't }
  1064. { possible that the contents are still used after p (used when removing }
  1065. { instructions because of a "call"), otherwise this is not necessary }
  1066. { (e.g. when you have a "mov 8(%ebp),%eax", you can be sure the previous }
  1067. { value of %eax isn't used anymore later on) }
  1068. var
  1069. hp1: pai;
  1070. begin
  1071. if getLastInstruction(p,hp1) then
  1072. with ppaiprop(hp1^.optInfo)^.regs[reg] do
  1073. if (typ in [con_ref,con_invalid]) and
  1074. (nrOfMods = 1) and
  1075. (rState = ppaiprop(startmod^.optInfo)^.regs[reg].rState) and
  1076. (not(check) or
  1077. (not(regInInstruction(reg,p)) and
  1078. (not(reg in usableregs) and
  1079. (startmod^.typ = ait_instruction) and
  1080. ((paicpu(startmod)^.opcode = A_MOV) or
  1081. (paicpu(startmod)^.opcode = A_MOVZX) or
  1082. (paicpu(startmod)^.opcode = A_MOVSX)) and
  1083. (paicpu(startmod)^.oper[0].typ = top_ref) and
  1084. (paicpu(startmod)^.oper[0].ref^.base = stack_pointer)) or
  1085. not(reg in ppaiprop(hp1^.optInfo)^.usedRegs) or
  1086. findRegDealloc(reg,p))) then
  1087. ppaiprop(startMod^.optInfo)^.canBeRemoved := true;
  1088. end;
  1089. Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
  1090. {marks the instructions that can be removed by RemoveInstructs. They're not
  1091. removed immediately because sometimes an instruction needs to be checked in
  1092. two different sequences}
  1093. var cnt, cnt2, cnt3: longint;
  1094. p, hp1, hp2, prevSeq, prevSeq_next: Pai;
  1095. hp3, hp4: pai;
  1096. hp5 : pai;
  1097. RegInfo: TRegInfo;
  1098. RegCounter: TRegister;
  1099. Begin
  1100. p := First;
  1101. SkipHead(p);
  1102. First := p;
  1103. While (p <> Last) Do
  1104. Begin
  1105. Case p^.typ Of
  1106. ait_align:
  1107. if not(pai_align(p)^.use_op) then
  1108. SetAlignReg(p);
  1109. ait_instruction:
  1110. Begin
  1111. Case Paicpu(p)^.opcode Of
  1112. A_CALL:
  1113. for regCounter := R_EAX to R_EBX do
  1114. removePrevNotUsedLoad(p,regCounter,true);
  1115. A_CLD: If GetLastInstruction(p, hp1) And
  1116. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
  1117. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  1118. A_MOV, A_MOVZX, A_MOVSX:
  1119. Begin
  1120. Case Paicpu(p)^.oper[0].typ Of
  1121. Top_Ref:
  1122. Begin {destination is always a register in this case}
  1123. With PPaiProp(p^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  1124. Begin
  1125. If (p = StartMod) And
  1126. GetLastInstruction (p, hp1) And
  1127. (hp1^.typ <> ait_marker) Then
  1128. {so we don't try to check a sequence when p is the first instruction of the block}
  1129. begin
  1130. {$ifdef csdebug}
  1131. hp5 := new(pai_asm_comment,init(strpnew(
  1132. 'cse checking '+att_reg2str[Reg32(Paicpu(p)^.oper[1].reg)])));
  1133. insertLLItem(asml,p,p^.next,hp5);
  1134. {$endif csdebug}
  1135. If CheckSequence(p,prevSeq,Paicpu(p)^.oper[1].reg, Cnt, RegInfo) And
  1136. (Cnt > 0) Then
  1137. Begin
  1138. hp1 := nil;
  1139. { although it's perfectly ok to remove an instruction which doesn't contain }
  1140. { the register that we've just checked (CheckSequence takes care of that), }
  1141. { the sequence containing this other register should also be completely }
  1142. { checked and removed, otherwise we may get situations like this: }
  1143. { }
  1144. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  1145. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  1146. { movl 8(%edx), %edx movl 8(%edx), %edx }
  1147. { movl (%eax), eax movl (%eax), eax }
  1148. { cmpl %eax, %edx cmpl %eax, %edx }
  1149. { jnz l123 getting converted to jnz l123 }
  1150. { movl 12(%ebp), %edx movl 4(%eax), eax }
  1151. { movl 16(%ebp), %eax }
  1152. { movl 8(%edx), %edx }
  1153. { movl 4(%eax), eax }
  1154. hp2 := p;
  1155. Cnt2 := 1;
  1156. While Cnt2 <= Cnt Do
  1157. Begin
  1158. If Not(RegInInstruction(Paicpu(hp2)^.oper[1].reg, p)) then
  1159. begin
  1160. if ((p^.typ = ait_instruction) And
  1161. ((paicpu(p)^.OpCode = A_MOV) or
  1162. (paicpu(p)^.opcode = A_MOVZX) or
  1163. (paicpu(p)^.opcode = A_MOVSX)) And
  1164. (paicpu(p)^.Oper[0].typ in
  1165. [top_const,top_ref,top_symbol])) and
  1166. (paicpu(p)^.oper[1].typ = top_reg) then
  1167. begin
  1168. regCounter := reg32(paicpu(p)^.oper[1].reg);
  1169. if (regCounter in reginfo.regsStillUsedAfterSeq) then
  1170. begin
  1171. if (hp1 = nil) then
  1172. hp1 := reginfo.lastReload[regCounter];
  1173. end
  1174. {$ifndef noremove}
  1175. else
  1176. begin
  1177. hp5 := p;
  1178. for cnt3 := ppaiprop(p^.optinfo)^.regs[regCounter].nrofmods downto 1 do
  1179. begin
  1180. if regModifiedByInstruction(regCounter,hp5) then
  1181. PPaiProp(hp5^.OptInfo)^.CanBeRemoved := True;
  1182. getNextInstruction(hp5,hp5);
  1183. end;
  1184. end
  1185. {$endif noremove}
  1186. end
  1187. end
  1188. {$ifndef noremove}
  1189. else
  1190. PPaiProp(p^.OptInfo)^.CanBeRemoved := True
  1191. {$endif noremove}
  1192. ; Inc(Cnt2);
  1193. GetNextInstruction(p, p);
  1194. End;
  1195. {hp4 is used to get the contents of the registers before the sequence}
  1196. GetLastInstruction(hp2, hp4);
  1197. getNextInstruction(prevSeq,prevSeq_next);
  1198. {$IfDef CSDebug}
  1199. For RegCounter := R_EAX To R_EDI Do
  1200. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  1201. Begin
  1202. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  1203. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  1204. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  1205. End;
  1206. {$EndIf CSDebug}
  1207. { If some registers were different in the old and the new sequence, move }
  1208. { the contents of those old registers to the new ones }
  1209. For RegCounter := R_EAX To R_EDI Do
  1210. If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
  1211. (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  1212. Begin
  1213. AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
  1214. PPaiProp(prevSeq^.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,prevSeq_next);
  1215. if hp4 <> prevSeq then
  1216. begin
  1217. if assigned(reginfo.lastReload[regCounter]) then
  1218. getLastInstruction(reginfo.lastReload[regCounter],hp3)
  1219. else hp3 := hp4;
  1220. if prevSeq <> hp3 then
  1221. clearRegContentsFrom(regCounter,prevSeq_next,
  1222. hp3);
  1223. allocRegBetween(asmL,regCounter,prevSeq,hp3);
  1224. end;
  1225. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  1226. {old reg new reg}
  1227. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  1228. Begin
  1229. getLastInstruction(p,hp3);
  1230. If (hp4 <> prevSeq) or
  1231. not(regCounter in usableRegs + [R_EDI,R_ESI]) or
  1232. not ReplaceReg(asmL,RegInfo.New2OldReg[RegCounter],
  1233. regCounter,hp3,
  1234. PPaiProp(PrevSeq^.optInfo)^.Regs[regCounter],true,hp5) then
  1235. begin
  1236. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  1237. InsertLLItem(AsmL, prevSeq, Pai(prevSeq^.next), hp3);
  1238. hp3 := New(Paicpu,Op_Reg_Reg(A_MOV, S_L,
  1239. {old reg new reg}
  1240. RegInfo.New2OldReg[RegCounter], RegCounter));
  1241. InsertLLItem(AsmL, prevSeq, Pai(prevSeq^.next), hp3);
  1242. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  1243. InsertLLItem(AsmL, prevSeq, Pai(prevSeq^.next), hp3);
  1244. { adjusts states in previous instruction so that it will }
  1245. { definitely be different from the previous or next state }
  1246. incstate(ppaiprop(prevSeq_next^.optinfo)^.
  1247. regs[RegInfo.New2OldReg[RegCounter]].rstate,20);
  1248. incstate(ppaiprop(prevSeq_next^.optinfo)^.
  1249. regs[regCounter].wstate,20);
  1250. updateState(RegInfo.New2OldReg[RegCounter],
  1251. prevSeq_next);
  1252. end
  1253. End
  1254. Else
  1255. { imagine the following code: }
  1256. { normal wrong optimized }
  1257. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  1258. { movl (%eax), %eax movl (%eax), %eax }
  1259. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  1260. { jne l1 jne l1 }
  1261. { movl 8(%ebp), %eax }
  1262. { movl (%eax), %edi movl %eax, %edi }
  1263. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  1264. { movl 8(%ebp), %eax }
  1265. { pushl 70(%eax) pushl 70(%eax) }
  1266. { }
  1267. { The error is that at the moment that the last instruction is executed, }
  1268. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  1269. { registers that are completely removed from a sequence (= registers in }
  1270. { RegLoadedForRef, have to be changed to their contents from before the }
  1271. { sequence. }
  1272. If RegCounter in RegInfo.RegsLoadedForRef Then
  1273. Begin
  1274. hp3 := hp2;
  1275. { cnt still holds the number of instructions }
  1276. { of the sequence, so go to the end of it }
  1277. for cnt2 := 1 to pred(cnt) Do
  1278. getNextInstruction(hp3,hp3);
  1279. { hp4 = instruction prior to start of sequence }
  1280. restoreRegContentsTo(regCounter,
  1281. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter],
  1282. hp2,hp3);
  1283. End;
  1284. End;
  1285. If hp1 <> nil Then
  1286. p := hp1;
  1287. Continue;
  1288. End
  1289. Else
  1290. If (PPaiProp(p^.OptInfo)^.
  1291. regs[reg32(paicpu(p)^.oper[1].reg)].typ
  1292. in [con_ref,con_noRemoveRef]) and
  1293. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  1294. if (cnt > 0) then
  1295. begin
  1296. hp2 := p;
  1297. Cnt2 := 1;
  1298. While Cnt2 <= Cnt Do
  1299. Begin
  1300. If RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Then
  1301. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  1302. Inc(Cnt2);
  1303. GetNextInstruction(p, p);
  1304. End;
  1305. Continue;
  1306. End
  1307. else
  1308. begin
  1309. { Fix for web bug 972 }
  1310. regCounter := Reg32(Paicpu(p)^.oper[1].reg);
  1311. cnt := PPaiProp(p^.optInfo)^.Regs[regCounter].nrOfMods;
  1312. hp3 := p;
  1313. for cnt2 := 1 to cnt do
  1314. if not(regModifiedByInstruction(regCounter,hp3) and
  1315. not(PPaiProp(hp3^.optInfo)^.canBeRemoved)) then
  1316. getNextInstruction(hp3,hp3)
  1317. else
  1318. break;
  1319. getLastInstruction(p,hp4);
  1320. RestoreRegContentsTo(regCounter,
  1321. PPaiProp(hp4^.optInfo)^.Regs[regCounter],
  1322. p,hp3);
  1323. end;
  1324. End;
  1325. End;
  1326. if not ppaiprop(p^.optinfo)^.canBeRemoved and
  1327. not regInRef(reg32(paicpu(p)^.oper[1].reg),
  1328. paicpu(p)^.oper[0].ref^) then
  1329. removePrevNotUsedLoad(p,reg32(paicpu(p)^.oper[1].reg),false);
  1330. End;
  1331. top_Reg:
  1332. { try to replace the new reg with the old reg }
  1333. if not(PPaiProp(p^.optInfo)^.canBeRemoved) and
  1334. { only remove if we're not storing something in a regvar }
  1335. (paicpu(p)^.oper[1].reg in (usableregs+[R_EDI])) and
  1336. (paicpu(p)^.opcode = A_MOV) and
  1337. getLastInstruction(p,hp4) then
  1338. begin
  1339. case paicpu(p)^.oper[1].typ of
  1340. top_Reg:
  1341. { we only have to start replacing from the instruction after the mov, }
  1342. { but replacereg only starts with getnextinstruction(p,p) }
  1343. if ReplaceReg(asmL,paicpu(p)^.oper[0].reg,
  1344. paicpu(p)^.oper[1].reg,p,
  1345. PPaiProp(hp4^.optInfo)^.Regs[paicpu(p)^.oper[1].reg],false,hp1) then
  1346. begin
  1347. PPaiProp(p^.optInfo)^.canBeRemoved := true;
  1348. allocRegBetween(asmL,paicpu(p)^.oper[0].reg,
  1349. PPaiProp(p^.optInfo)^.regs[paicpu(p)^.oper[0].reg].startMod,
  1350. hp1);
  1351. end;
  1352. end
  1353. end;
  1354. top_symbol,Top_Const:
  1355. Begin
  1356. Case Paicpu(p)^.oper[1].typ Of
  1357. Top_Reg:
  1358. Begin
  1359. regCounter := Reg32(Paicpu(p)^.oper[1].reg);
  1360. If GetLastInstruction(p, hp1) Then
  1361. With PPaiProp(hp1^.OptInfo)^.Regs[regCounter] Do
  1362. if (typ in [con_const,con_noRemoveConst]) and
  1363. (paicpu(startMod)^.opsize >= paicpu(p)^.opsize) and
  1364. opsequal(paicpu(StartMod)^.oper[0],paicpu(p)^.oper[0]) Then
  1365. begin
  1366. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  1367. allocRegBetween(asmL,regCounter,startMod,p);
  1368. end;
  1369. End;
  1370. Top_Ref:
  1371. if (paicpu(p)^.oper[0].typ = top_const) and
  1372. getLastInstruction(p,hp1) and
  1373. findRegWithConst(hp1,paicpu(p)^.opsize,paicpu(p)^.oper[0].val,regCounter) then
  1374. begin
  1375. paicpu(p)^.loadreg(0,regCounter);
  1376. allocRegBetween(AsmL,reg32(regCounter),
  1377. PPaiProp(hp1^.optinfo)^.regs[regCounter].startMod,p);
  1378. end;
  1379. End;
  1380. End;
  1381. End;
  1382. End;
  1383. A_STD: If GetLastInstruction(p, hp1) And
  1384. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  1385. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  1386. End
  1387. End;
  1388. End;
  1389. GetNextInstruction(p, p);
  1390. End;
  1391. End;
  1392. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  1393. { Removes the marked instructions and disposes the PPaiProps of the other }
  1394. { instructions }
  1395. Var p, hp1: Pai;
  1396. begin
  1397. p := First;
  1398. While (p <> Last) Do
  1399. Begin
  1400. If (p^.typ = ait_marker) and
  1401. (pai_marker(p)^.kind in [noPropInfoStart,noPropInfoEnd]) then
  1402. begin
  1403. hp1 := pai(p^.next);
  1404. asmL^.remove(p);
  1405. dispose(p,done);
  1406. p := hp1
  1407. end
  1408. else
  1409. {$ifndef noinstremove}
  1410. if assigned(p^.optInfo) and
  1411. PPaiProp(p^.optInfo)^.canBeRemoved then
  1412. begin
  1413. hp1 := pai(p^.next);
  1414. AsmL^.Remove(p);
  1415. Dispose(p, Done);
  1416. p := hp1;
  1417. End
  1418. Else
  1419. {$endif noinstremove}
  1420. Begin
  1421. p^.OptInfo := nil;
  1422. p := pai(p^.next);;
  1423. End;
  1424. End;
  1425. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  1426. End;
  1427. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  1428. Begin
  1429. DoCSE(AsmL, First, Last);
  1430. RemoveInstructs(AsmL, First, Last);
  1431. End;
  1432. End.
  1433. {
  1434. $Log$
  1435. Revision 1.11 2000-09-25 09:50:29 jonas
  1436. - removed TP conditional code
  1437. Revision 1.10 2000/09/24 15:06:14 peter
  1438. * use defines.inc
  1439. Revision 1.9 2000/09/22 15:01:59 jonas
  1440. * fixed some bugs in the previous improvements: in some cases, esi was
  1441. still being replaced before a conditional jump (the code that
  1442. detected conditional jumps sometimes skipped over them)
  1443. Revision 1.8 2000/09/20 15:00:58 jonas
  1444. + much improved CSE: the CSE now searches further back for sequences it
  1445. can reuse. After I've also implemented register renaming, the effect
  1446. should be even better (afaik web bug 1088 will then even be optimized
  1447. properly). I don't know about the slow down factor this adds. Maybe
  1448. a new optimization level should be introduced?
  1449. Revision 1.7 2000/08/25 19:40:45 jonas
  1450. * refined previous fix a bit, some instructions weren't being removed
  1451. while they could (merged from fixes branch)
  1452. * made checksequence a bit faster
  1453. Revision 1.6 2000/08/23 12:55:10 jonas
  1454. * fix for web bug 1112 and a bit of clean up in csopt386 (merged from
  1455. fixes branch)
  1456. Revision 1.5 2000/08/04 20:08:03 jonas
  1457. * improved detection of range of instructions which use a register
  1458. (merged from fixes branch)
  1459. Revision 1.4 2000/07/21 15:19:54 jonas
  1460. * daopt386: changes to getnextinstruction/getlastinstruction so they
  1461. ignore labels who have is_addr set
  1462. + daopt386/csopt386: remove loads of registers which are overwritten
  1463. before their contents are used (especially usefull for removing superfluous
  1464. maybe_loadesi outputs and push/pops transformed by below optimization
  1465. + popt386: transform pop/pop/pop/.../push/push/push to sequences of
  1466. 'movl x(%esp),%reg' (only active when compiling a go32v2 compiler
  1467. currently because I don't know whether it's safe to do this under Win32/
  1468. Linux (because of problems we had when using esp as frame pointer on
  1469. those os'es)
  1470. Revision 1.3 2000/07/14 05:11:48 michael
  1471. + Patch to 1.1
  1472. Revision 1.2 2000/07/13 11:32:39 michael
  1473. + removed logs
  1474. }