daopt386.pas 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Jonas Maebe, member of the Freepascal
  4. development team
  5. This unit contains the data flow analyzer and several helper procedures
  6. and functions.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. Unit DAOpt386;
  21. {$i defines.inc}
  22. Interface
  23. Uses
  24. GlobType,
  25. CClasses,Aasm,
  26. cpubase,cpuasm;
  27. {******************************* Constants *******************************}
  28. Const
  29. {Possible register content types}
  30. con_Unknown = 0;
  31. con_ref = 1;
  32. con_const = 2;
  33. { The contents aren't usable anymore for CSE, but they may still be }
  34. { usefull for detecting whether the result of a load is actually used }
  35. con_invalid = 3;
  36. { the reverse of the above (in case a (conditional) jump is encountered): }
  37. { CSE is still possible, but the original instruction can't be removed }
  38. con_noRemoveRef = 4;
  39. { same, but for constants }
  40. con_noRemoveConst = 5;
  41. {********************************* Types *********************************}
  42. type
  43. TRegArray = Array[R_EAX..R_BL] of TRegister;
  44. TRegSet = Set of R_EAX..R_BL;
  45. TRegInfo = Record
  46. NewRegsEncountered, OldRegsEncountered: TRegSet;
  47. RegsLoadedForRef: TRegSet;
  48. regsStillUsedAfterSeq: TRegSet;
  49. lastReload: array[R_EAX..R_EDI] of Tai;
  50. New2OldReg: TRegArray;
  51. End;
  52. {possible actions on an operand: read, write or modify (= read & write)}
  53. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  54. {the possible states of a flag}
  55. TFlagContents = (F_Unknown, F_NotSet, F_Set);
  56. TContent = Packed Record
  57. {start and end of block instructions that defines the
  58. content of this register.}
  59. StartMod: Tai;
  60. MemWrite: Taicpu;
  61. {how many instructions starting with StarMod does the block consist of}
  62. NrOfMods: Byte;
  63. {the type of the content of the register: unknown, memory, constant}
  64. Typ: Byte;
  65. case byte of
  66. {starts at 0, gets increased everytime the register is written to}
  67. 1: (WState: Byte;
  68. {starts at 0, gets increased everytime the register is read from}
  69. RState: Byte);
  70. { to compare both states in one operation }
  71. 2: (state: word);
  72. End;
  73. {Contents of the integer registers}
  74. TRegContent = Array[R_EAX..R_EDI] Of TContent;
  75. {contents of the FPU registers}
  76. TRegFPUContent = Array[R_ST..R_ST7] Of TContent;
  77. {$ifdef tempOpts}
  78. { linked list which allows searching/deleting based on value, no extra frills}
  79. PSearchLinkedListItem = ^TSearchLinkedListItem;
  80. TSearchLinkedListItem = object(TLinkedList_Item)
  81. constructor init;
  82. function equals(p: PSearchLinkedListItem): boolean; virtual;
  83. end;
  84. PSearchDoubleIntItem = ^TSearchDoubleInttem;
  85. TSearchDoubleIntItem = object(TLinkedList_Item)
  86. constructor init(_int1,_int2: longint);
  87. function equals(p: PSearchLinkedListItem): boolean; virtual;
  88. private
  89. int1, int2: longint;
  90. end;
  91. PSearchLinkedList = ^TSearchLinkedList;
  92. TSearchLinkedList = object(TLinkedList)
  93. function searchByValue(p: PSearchLinkedListItem): boolean;
  94. procedure removeByValue(p: PSearchLinkedListItem);
  95. end;
  96. {$endif tempOpts}
  97. {information record with the contents of every register. Every Tai object
  98. gets one of these assigned: a pointer to it is stored in the OptInfo field}
  99. TTaiProp = Record
  100. Regs: TRegContent;
  101. { FPURegs: TRegFPUContent;} {currently not yet used}
  102. { allocated Registers }
  103. UsedRegs: TRegSet;
  104. { status of the direction flag }
  105. DirFlag: TFlagContents;
  106. {$ifdef tempOpts}
  107. { currently used temps }
  108. tempAllocs: PSearchLinkedList;
  109. {$endif tempOpts}
  110. { can this instruction be removed? }
  111. CanBeRemoved: Boolean;
  112. { are the resultflags set by this instruction used? }
  113. FlagsUsed: Boolean;
  114. End;
  115. PTaiProp = ^TTaiProp;
  116. TTaiPropBlock = Array[1..250000] Of TTaiProp;
  117. PTaiPropBlock = ^TTaiPropBlock;
  118. TInstrSinceLastMod = Array[R_EAX..R_EDI] Of Byte;
  119. TLabelTableItem = Record
  120. TaiObj: Tai;
  121. {$IfDef JumpAnal}
  122. InstrNr: Longint;
  123. RefsFound: Word;
  124. JmpsProcessed: Word
  125. {$EndIf JumpAnal}
  126. End;
  127. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  128. PLabelTable = ^TLabelTable;
  129. {*********************** Procedures and Functions ************************}
  130. Procedure InsertLLItem(AsmL: TAAsmOutput; prev, foll, new_one: TLinkedListItem);
  131. Function Reg32(Reg: TRegister): TRegister;
  132. Function RefsEquivalent(Const R1, R2: TReference; Var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  133. Function RefsEqual(Const R1, R2: TReference): Boolean;
  134. Function IsGP32Reg(Reg: TRegister): Boolean;
  135. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  136. function RegReadByInstruction(reg: TRegister; hp: Tai): boolean;
  137. function RegModifiedByInstruction(Reg: TRegister; p1: Tai): Boolean;
  138. function RegInInstruction(Reg: TRegister; p1: Tai): Boolean;
  139. function RegInOp(Reg: TRegister; const o:toper): Boolean;
  140. function instrWritesFlags(p: Tai): boolean;
  141. function instrReadsFlags(p: Tai): boolean;
  142. function writeToMemDestroysContents(regWritten: tregister; const ref: treference;
  143. reg: tregister; const c: tcontent; var invalsmemwrite: boolean): boolean;
  144. function writeToRegDestroysContents(destReg: tregister; reg: tregister;
  145. const c: tcontent): boolean;
  146. function writeDestroysContents(const op: toper; reg: tregister;
  147. const c: tcontent): boolean;
  148. Function GetNextInstruction(Current: Tai; Var Next: Tai): Boolean;
  149. Function GetLastInstruction(Current: Tai; Var Last: Tai): Boolean;
  150. Procedure SkipHead(var P: Tai);
  151. function labelCanBeSkipped(p: Tai_label): boolean;
  152. Procedure RemoveLastDeallocForFuncRes(asmL: TAAsmOutput; p: Tai);
  153. Function regLoadedWithNewValue(reg: tregister; canDependOnPrevValue: boolean;
  154. hp: Tai): boolean;
  155. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Tai);
  156. Procedure AllocRegBetween(AsmL: TAAsmOutput; Reg: TRegister; p1, p2: Tai);
  157. function FindRegDealloc(reg: tregister; p: Tai): boolean;
  158. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  159. Function InstructionsEquivalent(p1, p2: Tai; Var RegInfo: TRegInfo): Boolean;
  160. function sizescompatible(loadsize,newsize: topsize): boolean;
  161. Function OpsEqual(const o1,o2:toper): Boolean;
  162. Function DFAPass1(AsmL: TAAsmOutput; BlockStart: Tai): Tai;
  163. Function DFAPass2(
  164. {$ifdef statedebug}
  165. AsmL: TAAsmOutPut;
  166. {$endif statedebug}
  167. BlockStart, BlockEnd: Tai): Boolean;
  168. Procedure ShutDownDFA;
  169. Function FindLabel(L: tasmlabel; Var hp: Tai): Boolean;
  170. Procedure IncState(Var S: Byte; amount: longint);
  171. {******************************* Variables *******************************}
  172. Var
  173. {the amount of TaiObjects in the current assembler list}
  174. NrOfTaiObjs: Longint;
  175. {Array which holds all TTaiProps}
  176. TaiPropBlock: PTaiPropBlock;
  177. LoLab, HiLab, LabDif: Longint;
  178. LTable: PLabelTable;
  179. {*********************** End of Interface section ************************}
  180. Implementation
  181. Uses
  182. globals, systems, verbose, cgbase, symconst, symsym, tainst, rgobj;
  183. Type
  184. TRefCompare = function(const r1, r2: TReference): Boolean;
  185. Var
  186. {How many instructions are between the current instruction and the last one
  187. that modified the register}
  188. NrOfInstrSinceLastMod: TInstrSinceLastMod;
  189. {$ifdef tempOpts}
  190. constructor TSearchLinkedListItem.init;
  191. begin
  192. end;
  193. function TSearchLinkedListItem.equals(p: PSearchLinkedListItem): boolean;
  194. begin
  195. equals := false;
  196. end;
  197. constructor TSearchDoubleIntItem.init(_int1,_int2: longint);
  198. begin
  199. int1 := _int1;
  200. int2 := _int2;
  201. end;
  202. function TSearchDoubleIntItem.equals(p: PSearchLinkedListItem): boolean;
  203. begin
  204. equals := (TSearchDoubleIntItem(p).int1 = int1) and
  205. (TSearchDoubleIntItem(p).int2 = int2);
  206. end;
  207. function TSearchLinkedList.searchByValue(p: PSearchLinkedListItem): boolean;
  208. var temp: PSearchLinkedListItem;
  209. begin
  210. temp := first;
  211. while (temp <> last.next) and
  212. not(temp.equals(p)) do
  213. temp := temp.next;
  214. searchByValue := temp <> last.next;
  215. end;
  216. procedure TSearchLinkedList.removeByValue(p: PSearchLinkedListItem);
  217. begin
  218. temp := first;
  219. while (temp <> last.next) and
  220. not(temp.equals(p)) do
  221. temp := temp.next;
  222. if temp <> last.next then
  223. begin
  224. remove(temp);
  225. dispose(temp,done);
  226. end;
  227. end;
  228. Procedure updateTempAllocs(Var UsedRegs: TRegSet; p: Tai);
  229. {updates UsedRegs with the RegAlloc Information coming after P}
  230. Begin
  231. Repeat
  232. While Assigned(p) And
  233. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  234. ((p.typ = ait_label) And
  235. labelCanBeSkipped(Tai_label(current)))) Do
  236. p := Tai(p.next);
  237. While Assigned(p) And
  238. (p.typ=ait_RegAlloc) Do
  239. Begin
  240. if Tairegalloc(p).allocation then
  241. UsedRegs := UsedRegs + [TaiRegAlloc(p).Reg]
  242. else
  243. UsedRegs := UsedRegs - [TaiRegAlloc(p).Reg];
  244. p := Tai(p.next);
  245. End;
  246. Until Not(Assigned(p)) Or
  247. (Not(p.typ in SkipInstr) And
  248. Not((p.typ = ait_label) And
  249. labelCanBeSkipped(Tai_label(current))));
  250. End;
  251. {$endif tempOpts}
  252. {************************ Create the Label table ************************}
  253. Function FindLoHiLabels(Var LowLabel, HighLabel, LabelDif: Longint; BlockStart: Tai): Tai;
  254. {Walks through the TAAsmlist to find the lowest and highest label number}
  255. Var LabelFound: Boolean;
  256. P, lastP: Tai;
  257. Begin
  258. LabelFound := False;
  259. LowLabel := MaxLongint;
  260. HighLabel := 0;
  261. P := BlockStart;
  262. lastP := p;
  263. While Assigned(P) Do
  264. Begin
  265. If (Tai(p).typ = ait_label) Then
  266. If not labelCanBeSkipped(Tai_label(p))
  267. Then
  268. Begin
  269. LabelFound := True;
  270. If (Tai_Label(p).l.labelnr < LowLabel) Then
  271. LowLabel := Tai_Label(p).l.labelnr;
  272. If (Tai_Label(p).l.labelnr > HighLabel) Then
  273. HighLabel := Tai_Label(p).l.labelnr;
  274. End;
  275. lastP := p;
  276. GetNextInstruction(p, p);
  277. End;
  278. if (lastP.typ = ait_marker) and
  279. (Tai_marker(lastp).kind = asmBlockStart) then
  280. FindLoHiLabels := lastP
  281. else FindLoHiLabels := nil;
  282. If LabelFound
  283. Then LabelDif := HighLabel+1-LowLabel
  284. Else LabelDif := 0;
  285. End;
  286. Function FindRegAlloc(Reg: TRegister; StartTai: Tai; alloc: boolean): Boolean;
  287. { Returns true if a ait_alloc object for Reg is found in the block of Tai's }
  288. { starting with StartTai and ending with the next "real" instruction }
  289. Begin
  290. FindRegAlloc := false;
  291. Repeat
  292. While Assigned(StartTai) And
  293. ((StartTai.typ in (SkipInstr - [ait_regAlloc])) Or
  294. ((StartTai.typ = ait_label) and
  295. labelCanBeSkipped(Tai_label(startTai)))) Do
  296. StartTai := Tai(StartTai.Next);
  297. If Assigned(StartTai) and
  298. (StartTai.typ = ait_regAlloc) then
  299. begin
  300. if (TairegAlloc(StartTai).allocation = alloc) and
  301. (TairegAlloc(StartTai).Reg = Reg) then
  302. begin
  303. FindRegAlloc:=true;
  304. break;
  305. end;
  306. StartTai := Tai(StartTai.Next);
  307. end
  308. else
  309. break;
  310. Until false;
  311. End;
  312. Procedure RemoveLastDeallocForFuncRes(asmL: TAAsmOutput; p: Tai);
  313. Procedure DoRemoveLastDeallocForFuncRes(asmL: TAAsmOutput; reg: TRegister);
  314. var
  315. hp2: Tai;
  316. begin
  317. hp2 := p;
  318. repeat
  319. hp2 := Tai(hp2.previous);
  320. if assigned(hp2) and
  321. (hp2.typ = ait_regalloc) and
  322. not(Tairegalloc(hp2).allocation) and
  323. (Tairegalloc(hp2).reg = reg) then
  324. begin
  325. asml.remove(hp2);
  326. hp2.free;
  327. break;
  328. end;
  329. until not(assigned(hp2)) or
  330. regInInstruction(reg,hp2);
  331. end;
  332. begin
  333. case aktprocdef.rettype.def.deftype of
  334. arraydef,recorddef,pointerdef,
  335. stringdef,enumdef,procdef,objectdef,errordef,
  336. filedef,setdef,procvardef,
  337. classrefdef,forwarddef:
  338. DoRemoveLastDeallocForFuncRes(asmL,R_EAX);
  339. orddef:
  340. if aktprocdef.rettype.def.size <> 0 then
  341. begin
  342. DoRemoveLastDeallocForFuncRes(asmL,R_EAX);
  343. { for int64/qword }
  344. if aktprocdef.rettype.def.size = 8 then
  345. DoRemoveLastDeallocForFuncRes(asmL,R_EDX);
  346. end;
  347. end;
  348. end;
  349. procedure getNoDeallocRegs(var regs: TRegSet);
  350. var regCounter: TRegister;
  351. begin
  352. regs := [];
  353. case aktprocdef.rettype.def.deftype of
  354. arraydef,recorddef,pointerdef,
  355. stringdef,enumdef,procdef,objectdef,errordef,
  356. filedef,setdef,procvardef,
  357. classrefdef,forwarddef:
  358. regs := [R_EAX];
  359. orddef:
  360. if aktprocdef.rettype.def.size <> 0 then
  361. begin
  362. regs := [R_EAX];
  363. { for int64/qword }
  364. if aktprocdef.rettype.def.size = 8 then
  365. regs := regs + [R_EDX];
  366. end;
  367. end;
  368. for regCounter := R_EAX to R_EBX do
  369. if not(regCounter in rg.usableregsint) then
  370. include(regs,regCounter);
  371. end;
  372. Procedure AddRegDeallocFor(asmL: TAAsmOutput; reg: TRegister; p: Tai);
  373. var hp1: Tai;
  374. funcResRegs: TRegset;
  375. funcResReg: boolean;
  376. begin
  377. if not(reg in rg.usableregsint) then
  378. exit;
  379. getNoDeallocRegs(funcResRegs);
  380. funcResRegs := funcResRegs - rg.usableregsint;
  381. funcResReg := reg in funcResRegs;
  382. hp1 := p;
  383. while not(funcResReg and
  384. (p.typ = ait_instruction) and
  385. (Taicpu(p).opcode = A_JMP) and
  386. (tasmlabel(Taicpu(p).oper[0].sym) = aktexit2label)) and
  387. getLastInstruction(p, p) And
  388. not(regInInstruction(reg, p)) Do
  389. hp1 := p;
  390. { don't insert a dealloc for registers which contain the function result }
  391. { if they are followed by a jump to the exit label (for exit(...)) }
  392. if not(funcResReg) or
  393. not((hp1.typ = ait_instruction) and
  394. (Taicpu(hp1).opcode = A_JMP) and
  395. (tasmlabel(Taicpu(hp1).oper[0].sym) = aktexit2label)) then
  396. begin
  397. p := TaiRegAlloc.deAlloc(reg);
  398. insertLLItem(AsmL, hp1.previous, hp1, p);
  399. end;
  400. end;
  401. Procedure BuildLabelTableAndFixRegAlloc(asmL: TAAsmOutput; Var LabelTable: PLabelTable; LowLabel: Longint;
  402. Var LabelDif: Longint; BlockStart, BlockEnd: Tai);
  403. {Builds a table with the locations of the labels in the TAAsmoutput.
  404. Also fixes some RegDeallocs like "# %eax released; push (%eax)"}
  405. Var p, hp1, hp2, lastP: Tai;
  406. regCounter: TRegister;
  407. UsedRegs, noDeallocRegs: TRegSet;
  408. Begin
  409. UsedRegs := [];
  410. If (LabelDif <> 0) Then
  411. Begin
  412. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  413. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  414. End;
  415. p := BlockStart;
  416. lastP := p;
  417. While (P <> BlockEnd) Do
  418. Begin
  419. Case p.typ Of
  420. ait_Label:
  421. If not labelCanBeSkipped(Tai_label(p)) Then
  422. LabelTable^[Tai_Label(p).l.labelnr-LowLabel].TaiObj := p;
  423. ait_regAlloc:
  424. { ESI and EDI are (de)allocated manually, don't mess with them }
  425. if not(TaiRegAlloc(p).Reg in [R_EDI,R_ESI]) then
  426. begin
  427. if TairegAlloc(p).Allocation then
  428. Begin
  429. If Not(TaiRegAlloc(p).Reg in UsedRegs) Then
  430. UsedRegs := UsedRegs + [TaiRegAlloc(p).Reg]
  431. Else
  432. addRegDeallocFor(asmL, TaiRegAlloc(p).reg, p);
  433. End
  434. else
  435. begin
  436. UsedRegs := UsedRegs - [TaiRegAlloc(p).Reg];
  437. hp1 := p;
  438. hp2 := nil;
  439. While Not(FindRegAlloc(TaiRegAlloc(p).Reg, Tai(hp1.Next),true)) And
  440. GetNextInstruction(hp1, hp1) And
  441. RegInInstruction(TaiRegAlloc(p).Reg, hp1) Do
  442. hp2 := hp1;
  443. If hp2 <> nil Then
  444. Begin
  445. hp1 := Tai(p.previous);
  446. AsmL.Remove(p);
  447. InsertLLItem(AsmL, hp2, Tai(hp2.Next), p);
  448. p := hp1;
  449. end;
  450. end;
  451. end;
  452. end;
  453. repeat
  454. lastP := p;
  455. P := Tai(P.Next);
  456. until not(Assigned(p)) or
  457. not(p.typ in (SkipInstr - [ait_regalloc]));
  458. End;
  459. { don't add deallocation for function result variable or for regvars}
  460. getNoDeallocRegs(noDeallocRegs);
  461. usedRegs := usedRegs - noDeallocRegs;
  462. for regCounter := R_EAX to R_EDI do
  463. if regCounter in usedRegs then
  464. addRegDeallocFor(asmL,regCounter,lastP);
  465. End;
  466. {************************ Search the Label table ************************}
  467. Function FindLabel(L: tasmlabel; Var hp: Tai): Boolean;
  468. {searches for the specified label starting from hp as long as the
  469. encountered instructions are labels, to be able to optimize constructs like
  470. jne l2 jmp l2
  471. jmp l3 and l1:
  472. l1: l2:
  473. l2:}
  474. Var TempP: Tai;
  475. Begin
  476. TempP := hp;
  477. While Assigned(TempP) and
  478. (Tempp.typ In SkipInstr + [ait_label,ait_align]) Do
  479. If (Tempp.typ <> ait_Label) Or
  480. (Tai_label(Tempp).l <> L)
  481. Then GetNextInstruction(TempP, TempP)
  482. Else
  483. Begin
  484. hp := TempP;
  485. FindLabel := True;
  486. exit
  487. End;
  488. FindLabel := False;
  489. End;
  490. {************************ Some general functions ************************}
  491. Function TCh2Reg(Ch: TInsChange): TRegister;
  492. {converts a TChange variable to a TRegister}
  493. Begin
  494. If (Ch <= Ch_REDI) Then
  495. TCh2Reg := TRegister(Byte(Ch))
  496. Else
  497. If (Ch <= Ch_WEDI) Then
  498. TCh2Reg := TRegister(Byte(Ch) - Byte(Ch_REDI))
  499. Else
  500. If (Ch <= Ch_RWEDI) Then
  501. TCh2Reg := TRegister(Byte(Ch) - Byte(Ch_WEDI))
  502. Else
  503. If (Ch <= Ch_MEDI) Then
  504. TCh2Reg := TRegister(Byte(Ch) - Byte(Ch_RWEDI))
  505. Else InternalError($db)
  506. End;
  507. Function Reg32(Reg: TRegister): TRegister;
  508. {Returns the 32 bit component of Reg if it exists, otherwise Reg is returned}
  509. Begin
  510. Reg32 := Reg;
  511. If (Reg >= R_AX)
  512. Then
  513. If (Reg <= R_DI)
  514. Then Reg32 := Reg16ToReg32(Reg)
  515. Else
  516. If (Reg <= R_BL)
  517. Then Reg32 := Reg8toReg32(Reg);
  518. End;
  519. { inserts new_one between prev and foll }
  520. Procedure InsertLLItem(AsmL: TAAsmOutput; prev, foll, new_one: TLinkedListItem);
  521. Begin
  522. If Assigned(prev) Then
  523. If Assigned(foll) Then
  524. Begin
  525. If Assigned(new_one) Then
  526. Begin
  527. new_one.previous := prev;
  528. new_one.next := foll;
  529. prev.next := new_one;
  530. foll.previous := new_one;
  531. Tai(new_one).fileinfo := Tai(foll).fileinfo;
  532. End;
  533. End
  534. Else asml.Concat(new_one)
  535. Else If Assigned(Foll) Then asml.Insert(new_one)
  536. End;
  537. {********************* Compare parts of Tai objects *********************}
  538. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  539. {returns true if Reg1 and Reg2 are of the same size (so if they're both
  540. 8bit, 16bit or 32bit)}
  541. Begin
  542. If (Reg1 <= R_EDI)
  543. Then RegsSameSize := (Reg2 <= R_EDI)
  544. Else
  545. If (Reg1 <= R_DI)
  546. Then RegsSameSize := (Reg2 in [R_AX..R_DI])
  547. Else
  548. If (Reg1 <= R_BL)
  549. Then RegsSameSize := (Reg2 in [R_AL..R_BL])
  550. Else RegsSameSize := False
  551. End;
  552. Procedure AddReg2RegInfo(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo);
  553. {updates the ???RegsEncountered and ???2???Reg fields of RegInfo. Assumes that
  554. OldReg and NewReg have the same size (has to be chcked in advance with
  555. RegsSameSize) and that neither equals R_NO}
  556. Begin
  557. With RegInfo Do
  558. Begin
  559. NewRegsEncountered := NewRegsEncountered + [NewReg];
  560. OldRegsEncountered := OldRegsEncountered + [OldReg];
  561. New2OldReg[NewReg] := OldReg;
  562. Case OldReg Of
  563. R_EAX..R_EDI:
  564. Begin
  565. NewRegsEncountered := NewRegsEncountered + [Reg32toReg16(NewReg)];
  566. OldRegsEncountered := OldRegsEncountered + [Reg32toReg16(OldReg)];
  567. New2OldReg[Reg32toReg16(NewReg)] := Reg32toReg16(OldReg);
  568. If (NewReg in [R_EAX..R_EBX]) And
  569. (OldReg in [R_EAX..R_EBX]) Then
  570. Begin
  571. NewRegsEncountered := NewRegsEncountered + [Reg32toReg8(NewReg)];
  572. OldRegsEncountered := OldRegsEncountered + [Reg32toReg8(OldReg)];
  573. New2OldReg[Reg32toReg8(NewReg)] := Reg32toReg8(OldReg);
  574. End;
  575. End;
  576. R_AX..R_DI:
  577. Begin
  578. NewRegsEncountered := NewRegsEncountered + [Reg16toReg32(NewReg)];
  579. OldRegsEncountered := OldRegsEncountered + [Reg16toReg32(OldReg)];
  580. New2OldReg[Reg16toReg32(NewReg)] := Reg16toReg32(OldReg);
  581. If (NewReg in [R_AX..R_BX]) And
  582. (OldReg in [R_AX..R_BX]) Then
  583. Begin
  584. NewRegsEncountered := NewRegsEncountered + [Reg16toReg8(NewReg)];
  585. OldRegsEncountered := OldRegsEncountered + [Reg16toReg8(OldReg)];
  586. New2OldReg[Reg16toReg8(NewReg)] := Reg16toReg8(OldReg);
  587. End;
  588. End;
  589. R_AL..R_BL:
  590. Begin
  591. NewRegsEncountered := NewRegsEncountered + [Reg8toReg32(NewReg)]
  592. + [Reg8toReg16(NewReg)];
  593. OldRegsEncountered := OldRegsEncountered + [Reg8toReg32(OldReg)]
  594. + [Reg8toReg16(OldReg)];
  595. New2OldReg[Reg8toReg32(NewReg)] := Reg8toReg32(OldReg);
  596. End;
  597. End;
  598. End;
  599. End;
  600. Procedure AddOp2RegInfo(const o:Toper; Var RegInfo: TRegInfo);
  601. Begin
  602. Case o.typ Of
  603. Top_Reg:
  604. If (o.reg <> R_NO) Then
  605. AddReg2RegInfo(o.reg, o.reg, RegInfo);
  606. Top_Ref:
  607. Begin
  608. If o.ref^.base <> R_NO Then
  609. AddReg2RegInfo(o.ref^.base, o.ref^.base, RegInfo);
  610. If o.ref^.index <> R_NO Then
  611. AddReg2RegInfo(o.ref^.index, o.ref^.index, RegInfo);
  612. End;
  613. End;
  614. End;
  615. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OPAct: TOpAction): Boolean;
  616. Begin
  617. If Not((OldReg = R_NO) Or (NewReg = R_NO)) Then
  618. If RegsSameSize(OldReg, NewReg) Then
  619. With RegInfo Do
  620. {here we always check for the 32 bit component, because it is possible that
  621. the 8 bit component has not been set, event though NewReg already has been
  622. processed. This happens if it has been compared with a register that doesn't
  623. have an 8 bit component (such as EDI). In that case the 8 bit component is
  624. still set to R_NO and the comparison in the Else-part will fail}
  625. If (Reg32(OldReg) in OldRegsEncountered) Then
  626. If (Reg32(NewReg) in NewRegsEncountered) Then
  627. RegsEquivalent := (OldReg = New2OldReg[NewReg])
  628. { If we haven't encountered the new register yet, but we have encountered the
  629. old one already, the new one can only be correct if it's being written to
  630. (and consequently the old one is also being written to), otherwise
  631. movl -8(%ebp), %eax and movl -8(%ebp), %eax
  632. movl (%eax), %eax movl (%edx), %edx
  633. are considered equivalent}
  634. Else
  635. If (OpAct = OpAct_Write) Then
  636. Begin
  637. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  638. RegsEquivalent := True
  639. End
  640. Else Regsequivalent := False
  641. Else
  642. If Not(Reg32(NewReg) in NewRegsEncountered) and
  643. ((OpAct = OpAct_Write) or
  644. (newReg = oldReg)) Then
  645. Begin
  646. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  647. RegsEquivalent := True
  648. End
  649. Else RegsEquivalent := False
  650. Else RegsEquivalent := False
  651. Else RegsEquivalent := OldReg = NewReg
  652. End;
  653. Function RefsEquivalent(Const R1, R2: TReference; var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  654. Begin
  655. RefsEquivalent := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  656. RegsEquivalent(R1.Base, R2.Base, RegInfo, OpAct) And
  657. RegsEquivalent(R1.Index, R2.Index, RegInfo, OpAct) And
  658. (R1.Segment = R2.Segment) And (R1.ScaleFactor = R2.ScaleFactor) And
  659. (R1.Symbol = R2.Symbol);
  660. End;
  661. Function RefsEqual(Const R1, R2: TReference): Boolean;
  662. Begin
  663. RefsEqual := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  664. (R1.Segment = R2.Segment) And (R1.Base = R2.Base) And
  665. (R1.Index = R2.Index) And (R1.ScaleFactor = R2.ScaleFactor) And
  666. (R1.Symbol=R2.Symbol);
  667. End;
  668. Function IsGP32Reg(Reg: TRegister): Boolean;
  669. {Checks if the register is a 32 bit general purpose register}
  670. Begin
  671. If (Reg >= R_EAX) and (Reg <= R_EBX)
  672. Then IsGP32Reg := True
  673. Else IsGP32reg := False
  674. End;
  675. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  676. Begin {checks whether Ref contains a reference to Reg}
  677. Reg := Reg32(Reg);
  678. RegInRef := (Ref.Base = Reg) Or (Ref.Index = Reg)
  679. End;
  680. function RegReadByInstruction(reg: TRegister; hp: Tai): boolean;
  681. var p: Taicpu;
  682. opCount: byte;
  683. begin
  684. RegReadByInstruction := false;
  685. reg := reg32(reg);
  686. p := Taicpu(hp);
  687. if hp.typ <> ait_instruction then
  688. exit;
  689. case p.opcode of
  690. A_IMUL:
  691. case p.ops of
  692. 1: regReadByInstruction := (reg = R_EAX) or reginOp(reg,p.oper[0]);
  693. 2,3:
  694. regReadByInstruction := regInOp(reg,p.oper[0]) or
  695. regInOp(reg,p.oper[1]);
  696. end;
  697. A_IDIV,A_DIV,A_MUL:
  698. begin
  699. regReadByInstruction :=
  700. regInOp(reg,p.oper[0]) or (reg = R_EAX);
  701. end;
  702. else
  703. begin
  704. for opCount := 0 to 2 do
  705. if (p.oper[opCount].typ = top_ref) and
  706. RegInRef(reg,p.oper[opCount].ref^) then
  707. begin
  708. RegReadByInstruction := true;
  709. exit
  710. end;
  711. for opCount := 1 to MaxCh do
  712. case InsProp[p.opcode].Ch[opCount] of
  713. Ch_REAX..CH_REDI,CH_RWEAX..Ch_MEDI:
  714. if reg = TCh2Reg(InsProp[p.opcode].Ch[opCount]) then
  715. begin
  716. RegReadByInstruction := true;
  717. exit
  718. end;
  719. Ch_RWOp1,Ch_ROp1,Ch_MOp1:
  720. if (p.oper[0].typ = top_reg) and
  721. (reg32(p.oper[0].reg) = reg) then
  722. begin
  723. RegReadByInstruction := true;
  724. exit
  725. end;
  726. Ch_RWOp2,Ch_ROp2,Ch_MOp2:
  727. if (p.oper[1].typ = top_reg) and
  728. (reg32(p.oper[1].reg) = reg) then
  729. begin
  730. RegReadByInstruction := true;
  731. exit
  732. end;
  733. Ch_RWOp3,Ch_ROp3,Ch_MOp3:
  734. if (p.oper[2].typ = top_reg) and
  735. (reg32(p.oper[2].reg) = reg) then
  736. begin
  737. RegReadByInstruction := true;
  738. exit
  739. end;
  740. end;
  741. end;
  742. end;
  743. end;
  744. function regInInstruction(Reg: TRegister; p1: Tai): Boolean;
  745. { Checks if Reg is used by the instruction p1 }
  746. { Difference with "regReadBysinstruction() or regModifiedByInstruction()": }
  747. { this one ignores CH_ALL opcodes, while regModifiedByInstruction doesn't }
  748. var p: Taicpu;
  749. opCount: byte;
  750. begin
  751. reg := reg32(reg);
  752. regInInstruction := false;
  753. p := Taicpu(p1);
  754. if p1.typ <> ait_instruction then
  755. exit;
  756. case p.opcode of
  757. A_IMUL:
  758. case p.ops of
  759. 1: regInInstruction := (reg = R_EAX) or reginOp(reg,p.oper[0]);
  760. 2,3:
  761. regInInstruction := regInOp(reg,p.oper[0]) or
  762. regInOp(reg,p.oper[1]) or regInOp(reg,p.oper[2]);
  763. end;
  764. A_IDIV,A_DIV,A_MUL:
  765. regInInstruction :=
  766. regInOp(reg,p.oper[0]) or
  767. (reg = R_EAX) or (reg = R_EDX)
  768. else
  769. begin
  770. for opCount := 1 to MaxCh do
  771. case InsProp[p.opcode].Ch[opCount] of
  772. CH_REAX..CH_MEDI:
  773. if tch2reg(InsProp[p.opcode].Ch[opCount]) = reg then
  774. begin
  775. regInInstruction := true;
  776. exit;
  777. end;
  778. Ch_ROp1..Ch_MOp1:
  779. if regInOp(reg,p.oper[0]) then
  780. begin
  781. regInInstruction := true;
  782. exit
  783. end;
  784. Ch_ROp2..Ch_MOp2:
  785. if regInOp(reg,p.oper[1]) then
  786. begin
  787. regInInstruction := true;
  788. exit
  789. end;
  790. Ch_ROp3..Ch_MOp3:
  791. if regInOp(reg,p.oper[2]) then
  792. begin
  793. regInInstruction := true;
  794. exit
  795. end;
  796. end;
  797. end;
  798. end;
  799. end;
  800. Function RegInOp(Reg: TRegister; const o:toper): Boolean;
  801. Begin
  802. RegInOp := False;
  803. reg := reg32(reg);
  804. Case o.typ Of
  805. top_reg: RegInOp := Reg = reg32(o.reg);
  806. top_ref: RegInOp := (Reg = o.ref^.Base) Or
  807. (Reg = o.ref^.Index);
  808. End;
  809. End;
  810. Function RegModifiedByInstruction(Reg: TRegister; p1: Tai): Boolean;
  811. Var InstrProp: TInsProp;
  812. TmpResult: Boolean;
  813. Cnt: Byte;
  814. Begin
  815. TmpResult := False;
  816. Reg := Reg32(Reg);
  817. If (p1.typ = ait_instruction) Then
  818. Case Taicpu(p1).opcode of
  819. A_IMUL:
  820. With Taicpu(p1) Do
  821. TmpResult :=
  822. ((ops = 1) and (reg in [R_EAX,R_EDX])) or
  823. ((ops = 2) and (Reg32(oper[1].reg) = reg)) or
  824. ((ops = 3) and (Reg32(oper[2].reg) = reg));
  825. A_DIV, A_IDIV, A_MUL:
  826. With Taicpu(p1) Do
  827. TmpResult :=
  828. (Reg = R_EAX) or
  829. (Reg = R_EDX);
  830. Else
  831. Begin
  832. Cnt := 1;
  833. InstrProp := InsProp[Taicpu(p1).OpCode];
  834. While (Cnt <= MaxCh) And
  835. (InstrProp.Ch[Cnt] <> Ch_None) And
  836. Not(TmpResult) Do
  837. Begin
  838. Case InstrProp.Ch[Cnt] Of
  839. Ch_WEAX..Ch_MEDI:
  840. TmpResult := Reg = TCh2Reg(InstrProp.Ch[Cnt]);
  841. Ch_RWOp1,Ch_WOp1,Ch_Mop1:
  842. TmpResult := (Taicpu(p1).oper[0].typ = top_reg) and
  843. (Reg32(Taicpu(p1).oper[0].reg) = reg);
  844. Ch_RWOp2,Ch_WOp2,Ch_Mop2:
  845. TmpResult := (Taicpu(p1).oper[1].typ = top_reg) and
  846. (Reg32(Taicpu(p1).oper[1].reg) = reg);
  847. Ch_RWOp3,Ch_WOp3,Ch_Mop3:
  848. TmpResult := (Taicpu(p1).oper[2].typ = top_reg) and
  849. (Reg32(Taicpu(p1).oper[2].reg) = reg);
  850. Ch_FPU: TmpResult := Reg in [R_ST..R_ST7,R_MM0..R_MM7];
  851. Ch_ALL: TmpResult := true;
  852. End;
  853. Inc(Cnt)
  854. End
  855. End
  856. End;
  857. RegModifiedByInstruction := TmpResult
  858. End;
  859. function instrWritesFlags(p: Tai): boolean;
  860. var
  861. l: longint;
  862. begin
  863. instrWritesFlags := true;
  864. case p.typ of
  865. ait_instruction:
  866. begin
  867. for l := 1 to MaxCh do
  868. if InsProp[Taicpu(p).opcode].Ch[l] in [Ch_WFlags,Ch_RWFlags,Ch_All] then
  869. exit;
  870. end;
  871. ait_label:
  872. exit;
  873. else
  874. instrWritesFlags := false;
  875. end;
  876. end;
  877. function instrReadsFlags(p: Tai): boolean;
  878. var
  879. l: longint;
  880. begin
  881. instrReadsFlags := true;
  882. case p.typ of
  883. ait_instruction:
  884. begin
  885. for l := 1 to MaxCh do
  886. if InsProp[Taicpu(p).opcode].Ch[l] in [Ch_RFlags,Ch_RWFlags,Ch_All] then
  887. exit;
  888. end;
  889. ait_label:
  890. exit;
  891. else
  892. instrReadsFlags := false;
  893. end;
  894. end;
  895. {********************* GetNext and GetLastInstruction *********************}
  896. Function GetNextInstruction(Current: Tai; Var Next: Tai): Boolean;
  897. { skips ait_regalloc, ait_regdealloc and ait_stab* objects and puts the }
  898. { next Tai object in Next. Returns false if there isn't any }
  899. Begin
  900. Repeat
  901. If (Current.typ = ait_marker) And
  902. (Tai_Marker(current).Kind = AsmBlockStart) Then
  903. Begin
  904. GetNextInstruction := False;
  905. Next := Nil;
  906. Exit
  907. End;
  908. Current := Tai(current.Next);
  909. While Assigned(Current) And
  910. ((current.typ In skipInstr) or
  911. ((current.typ = ait_label) and
  912. labelCanBeSkipped(Tai_label(current)))) do
  913. Current := Tai(current.Next);
  914. { If Assigned(Current) And
  915. (current.typ = ait_Marker) And
  916. (Tai_Marker(current).Kind = NoPropInfoStart) Then
  917. Begin
  918. While Assigned(Current) And
  919. ((current.typ <> ait_Marker) Or
  920. (Tai_Marker(current).Kind <> NoPropInfoEnd)) Do
  921. Current := Tai(current.Next);
  922. End;}
  923. Until Not(Assigned(Current)) Or
  924. (current.typ <> ait_Marker) Or
  925. not(Tai_Marker(current).Kind in [NoPropInfoStart,NoPropInfoEnd]);
  926. Next := Current;
  927. If Assigned(Current) And
  928. Not((current.typ In SkipInstr) or
  929. ((current.typ = ait_label) And
  930. labelCanBeSkipped(Tai_label(current))))
  931. Then
  932. GetNextInstruction :=
  933. not((current.typ = ait_marker) and
  934. (Tai_marker(current).kind = asmBlockStart))
  935. Else
  936. Begin
  937. GetNextInstruction := False;
  938. Next := nil;
  939. End;
  940. End;
  941. Function GetLastInstruction(Current: Tai; Var Last: Tai): Boolean;
  942. {skips the ait-types in SkipInstr puts the previous Tai object in
  943. Last. Returns false if there isn't any}
  944. Begin
  945. Repeat
  946. Current := Tai(current.previous);
  947. While Assigned(Current) And
  948. (((current.typ = ait_Marker) And
  949. Not(Tai_Marker(current).Kind in [AsmBlockEnd{,NoPropInfoEnd}])) or
  950. (current.typ In SkipInstr) or
  951. ((current.typ = ait_label) And
  952. labelCanBeSkipped(Tai_label(current)))) Do
  953. Current := Tai(current.previous);
  954. { If Assigned(Current) And
  955. (current.typ = ait_Marker) And
  956. (Tai_Marker(current).Kind = NoPropInfoEnd) Then
  957. Begin
  958. While Assigned(Current) And
  959. ((current.typ <> ait_Marker) Or
  960. (Tai_Marker(current).Kind <> NoPropInfoStart)) Do
  961. Current := Tai(current.previous);
  962. End;}
  963. Until Not(Assigned(Current)) Or
  964. (current.typ <> ait_Marker) Or
  965. not(Tai_Marker(current).Kind in [NoPropInfoStart,NoPropInfoEnd]);
  966. If Not(Assigned(Current)) or
  967. (current.typ In SkipInstr) or
  968. ((current.typ = ait_label) And
  969. labelCanBeSkipped(Tai_label(current))) or
  970. ((current.typ = ait_Marker) And
  971. (Tai_Marker(current).Kind = AsmBlockEnd))
  972. Then
  973. Begin
  974. Last := nil;
  975. GetLastInstruction := False
  976. End
  977. Else
  978. Begin
  979. Last := Current;
  980. GetLastInstruction := True;
  981. End;
  982. End;
  983. Procedure SkipHead(var P: Tai);
  984. Var OldP: Tai;
  985. Begin
  986. Repeat
  987. OldP := P;
  988. If (p.typ in SkipInstr) Or
  989. ((p.typ = ait_marker) And
  990. (Tai_Marker(p).Kind in [AsmBlockEnd,inlinestart,inlineend])) Then
  991. GetNextInstruction(P, P)
  992. Else If ((p.Typ = Ait_Marker) And
  993. (Tai_Marker(p).Kind = nopropinfostart)) Then
  994. {a marker of the NoPropInfoStart can't be the first instruction of a
  995. TAAsmoutput list}
  996. GetNextInstruction(Tai(p.Previous),P);
  997. Until P = OldP
  998. End;
  999. function labelCanBeSkipped(p: Tai_label): boolean;
  1000. begin
  1001. labelCanBeSkipped := not(p.l.is_used) or p.l.is_addr;
  1002. end;
  1003. {******************* The Data Flow Analyzer functions ********************}
  1004. function regLoadedWithNewValue(reg: tregister; canDependOnPrevValue: boolean;
  1005. hp: Tai): boolean;
  1006. { assumes reg is a 32bit register }
  1007. var p: Taicpu;
  1008. begin
  1009. p := Taicpu(hp);
  1010. regLoadedWithNewValue :=
  1011. assigned(hp) and
  1012. (hp.typ = ait_instruction) and
  1013. (((p.opcode = A_MOV) or
  1014. (p.opcode = A_MOVZX) or
  1015. (p.opcode = A_MOVSX) or
  1016. (p.opcode = A_LEA)) and
  1017. (p.oper[1].typ = top_reg) and
  1018. (Reg32(p.oper[1].reg) = reg) and
  1019. (canDependOnPrevValue or
  1020. (p.oper[0].typ <> top_ref) or
  1021. not regInRef(reg,p.oper[0].ref^)) or
  1022. ((p.opcode = A_POP) and
  1023. (Reg32(p.oper[0].reg) = reg)));
  1024. end;
  1025. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Tai);
  1026. {updates UsedRegs with the RegAlloc Information coming after P}
  1027. Begin
  1028. Repeat
  1029. While Assigned(p) And
  1030. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  1031. ((p.typ = ait_label) And
  1032. labelCanBeSkipped(Tai_label(p)))) Do
  1033. p := Tai(p.next);
  1034. While Assigned(p) And
  1035. (p.typ=ait_RegAlloc) Do
  1036. Begin
  1037. if Tairegalloc(p).allocation then
  1038. UsedRegs := UsedRegs + [TaiRegAlloc(p).Reg]
  1039. else
  1040. UsedRegs := UsedRegs - [TaiRegAlloc(p).Reg];
  1041. p := Tai(p.next);
  1042. End;
  1043. Until Not(Assigned(p)) Or
  1044. (Not(p.typ in SkipInstr) And
  1045. Not((p.typ = ait_label) And
  1046. labelCanBeSkipped(Tai_label(p))));
  1047. End;
  1048. Procedure AllocRegBetween(AsmL: TAAsmOutput; Reg: TRegister; p1, p2: Tai);
  1049. { allocates register Reg between (and including) instructions p1 and p2 }
  1050. { the type of p1 and p2 must not be in SkipInstr }
  1051. var
  1052. hp, start: Tai;
  1053. lastRemovedWasDealloc, firstRemovedWasAlloc, first: boolean;
  1054. Begin
  1055. If not(reg in rg.usableregsint+[R_EDI,R_ESI]) or
  1056. not(assigned(p1)) then
  1057. { this happens with registers which are loaded implicitely, outside the }
  1058. { current block (e.g. esi with self) }
  1059. exit;
  1060. { make sure we allocate it for this instruction }
  1061. if p1 = p2 then
  1062. getnextinstruction(p2,p2);
  1063. lastRemovedWasDealloc := false;
  1064. firstRemovedWasAlloc := false;
  1065. first := true;
  1066. {$ifdef allocregdebug}
  1067. hp := Tai_asm_comment.Create(strpnew('allocating '+gas_reg2str[reg]+
  1068. ' from here...')));
  1069. insertllitem(asml,p1.previous,p1,hp);
  1070. hp := Tai_asm_comment.Create(strpnew('allocated '+gas_reg2str[reg]+
  1071. ' till here...')));
  1072. insertllitem(asml,p2,p1.next,hp);
  1073. {$endif allocregdebug}
  1074. start := p1;
  1075. Repeat
  1076. If Assigned(p1.OptInfo) Then
  1077. Include(PTaiProp(p1.OptInfo)^.UsedRegs,Reg);
  1078. p1 := Tai(p1.next);
  1079. Repeat
  1080. While assigned(p1) and
  1081. (p1.typ in (SkipInstr-[ait_regalloc])) Do
  1082. p1 := Tai(p1.next);
  1083. { remove all allocation/deallocation info about the register in between }
  1084. If assigned(p1) and
  1085. (p1.typ = ait_regalloc) Then
  1086. If (TaiRegAlloc(p1).Reg = Reg) Then
  1087. Begin
  1088. if first then
  1089. begin
  1090. firstRemovedWasAlloc := TaiRegAlloc(p1).allocation;
  1091. first := false;
  1092. end;
  1093. lastRemovedWasDealloc := not TaiRegAlloc(p1).allocation;
  1094. hp := Tai(p1.Next);
  1095. asml.Remove(p1);
  1096. p1.free;
  1097. p1 := hp;
  1098. End
  1099. Else p1 := Tai(p1.next);
  1100. Until not(assigned(p1)) or
  1101. Not(p1.typ in SkipInstr);
  1102. Until not(assigned(p1)) or
  1103. (p1 = p2);
  1104. if assigned(p1) then
  1105. begin
  1106. if assigned(p1.optinfo) then
  1107. include(PTaiProp(p1.OptInfo)^.UsedRegs,Reg);
  1108. if lastRemovedWasDealloc then
  1109. begin
  1110. hp := TaiRegalloc.DeAlloc(reg);
  1111. insertLLItem(asmL,p1,p1.next,hp);
  1112. end;
  1113. end;
  1114. if firstRemovedWasAlloc then
  1115. begin
  1116. hp := TaiRegalloc.Alloc(reg);
  1117. insertLLItem(asmL,start.previous,start,hp);
  1118. end;
  1119. End;
  1120. function FindRegDealloc(reg: tregister; p: Tai): boolean;
  1121. { assumes reg is a 32bit register }
  1122. var
  1123. hp: Tai;
  1124. first: boolean;
  1125. begin
  1126. findregdealloc := false;
  1127. first := true;
  1128. while assigned(p.previous) and
  1129. ((Tai(p.previous).typ in (skipinstr+[ait_align])) or
  1130. ((Tai(p.previous).typ = ait_label) and
  1131. labelCanBeSkipped(Tai_label(p.previous)))) do
  1132. begin
  1133. p := Tai(p.previous);
  1134. if (p.typ = ait_regalloc) and
  1135. (Tairegalloc(p).reg = reg) then
  1136. if not(Tairegalloc(p).allocation) then
  1137. if first then
  1138. begin
  1139. findregdealloc := true;
  1140. break;
  1141. end
  1142. else
  1143. begin
  1144. findRegDealloc :=
  1145. getNextInstruction(p,hp) and
  1146. regLoadedWithNewValue(reg,false,hp);
  1147. break
  1148. end
  1149. else
  1150. first := false;
  1151. end
  1152. end;
  1153. Procedure IncState(Var S: Byte; amount: longint);
  1154. {Increases S by 1, wraps around at $ffff to 0 (so we won't get overflow
  1155. errors}
  1156. Begin
  1157. if (s <= $ff - amount) then
  1158. inc(s, amount)
  1159. else s := longint(s) + amount - $ff;
  1160. End;
  1161. Function sequenceDependsonReg(Const Content: TContent; seqReg, Reg: TRegister): Boolean;
  1162. { Content is the sequence of instructions that describes the contents of }
  1163. { seqReg. Reg is being overwritten by the current instruction. If the }
  1164. { content of seqReg depends on reg (ie. because of a }
  1165. { "movl (seqreg,reg), seqReg" instruction), this function returns true }
  1166. Var p: Tai;
  1167. Counter: Byte;
  1168. TmpResult: Boolean;
  1169. RegsChecked: TRegSet;
  1170. Begin
  1171. RegsChecked := [];
  1172. p := Content.StartMod;
  1173. TmpResult := False;
  1174. Counter := 1;
  1175. While Not(TmpResult) And
  1176. (Counter <= Content.NrOfMods) Do
  1177. Begin
  1178. If (p.typ = ait_instruction) and
  1179. ((Taicpu(p).opcode = A_MOV) or
  1180. (Taicpu(p).opcode = A_MOVZX) or
  1181. (Taicpu(p).opcode = A_MOVSX) or
  1182. (Taicpu(p).opcode = A_LEA)) and
  1183. (Taicpu(p).oper[0].typ = top_ref) Then
  1184. With Taicpu(p).oper[0].ref^ Do
  1185. If ((Base = procinfo^.FramePointer) or
  1186. (assigned(symbol) and (base = R_NO))) And
  1187. (Index = R_NO) Then
  1188. Begin
  1189. RegsChecked := RegsChecked + [Reg32(Taicpu(p).oper[1].reg)];
  1190. If Reg = Reg32(Taicpu(p).oper[1].reg) Then
  1191. Break;
  1192. End
  1193. Else
  1194. tmpResult :=
  1195. regReadByInstruction(reg,p) and
  1196. regModifiedByInstruction(seqReg,p)
  1197. Else
  1198. tmpResult :=
  1199. regReadByInstruction(reg,p) and
  1200. regModifiedByInstruction(seqReg,p);
  1201. Inc(Counter);
  1202. GetNextInstruction(p,p)
  1203. End;
  1204. sequenceDependsonReg := TmpResult
  1205. End;
  1206. procedure invalidateDependingRegs(p1: pTaiProp; reg: tregister);
  1207. var
  1208. counter: tregister;
  1209. begin
  1210. for counter := R_EAX to R_EDI Do
  1211. if counter <> reg then
  1212. with p1^.regs[counter] Do
  1213. begin
  1214. if (typ in [con_ref,con_noRemoveRef]) and
  1215. sequenceDependsOnReg(p1^.Regs[counter],counter,reg) then
  1216. if typ in [con_ref,con_invalid] then
  1217. typ := con_invalid
  1218. { con_invalid and con_noRemoveRef = con_unknown }
  1219. else typ := con_unknown;
  1220. if assigned(memwrite) and
  1221. regInRef(counter,memwrite.oper[1].ref^) then
  1222. memwrite := nil;
  1223. end;
  1224. end;
  1225. Procedure DestroyReg(p1: PTaiProp; Reg: TRegister; doIncState:Boolean);
  1226. {Destroys the contents of the register Reg in the PTaiProp p1, as well as the
  1227. contents of registers are loaded with a memory location based on Reg.
  1228. doIncState is false when this register has to be destroyed not because
  1229. it's contents are directly modified/overwritten, but because of an indirect
  1230. action (e.g. this register holds the contents of a variable and the value
  1231. of the variable in memory is changed) }
  1232. Begin
  1233. Reg := Reg32(Reg);
  1234. { the following happens for fpu registers }
  1235. if (reg < low(NrOfInstrSinceLastMod)) or
  1236. (reg > high(NrOfInstrSinceLastMod)) then
  1237. exit;
  1238. NrOfInstrSinceLastMod[Reg] := 0;
  1239. with p1^.regs[reg] do
  1240. begin
  1241. if doIncState then
  1242. begin
  1243. incState(wstate,1);
  1244. typ := con_unknown;
  1245. startmod := nil;
  1246. end
  1247. else
  1248. if typ in [con_ref,con_const,con_invalid] then
  1249. typ := con_invalid
  1250. { con_invalid and con_noRemoveRef = con_unknown }
  1251. else typ := con_unknown;
  1252. memwrite := nil;
  1253. end;
  1254. invalidateDependingRegs(p1,reg);
  1255. End;
  1256. {Procedure AddRegsToSet(p: Tai; Var RegSet: TRegSet);
  1257. Begin
  1258. If (p.typ = ait_instruction) Then
  1259. Begin
  1260. Case Taicpu(p).oper[0].typ Of
  1261. top_reg:
  1262. If Not(Taicpu(p).oper[0].reg in [R_NO,R_ESP,procinfo^.FramePointer]) Then
  1263. RegSet := RegSet + [Taicpu(p).oper[0].reg];
  1264. top_ref:
  1265. With TReference(Taicpu(p).oper[0]^) Do
  1266. Begin
  1267. If Not(Base in [procinfo^.FramePointer,R_NO,R_ESP])
  1268. Then RegSet := RegSet + [Base];
  1269. If Not(Index in [procinfo^.FramePointer,R_NO,R_ESP])
  1270. Then RegSet := RegSet + [Index];
  1271. End;
  1272. End;
  1273. Case Taicpu(p).oper[1].typ Of
  1274. top_reg:
  1275. If Not(Taicpu(p).oper[1].reg in [R_NO,R_ESP,procinfo^.FramePointer]) Then
  1276. If RegSet := RegSet + [TRegister(TwoWords(Taicpu(p).oper[1]).Word1];
  1277. top_ref:
  1278. With TReference(Taicpu(p).oper[1]^) Do
  1279. Begin
  1280. If Not(Base in [procinfo^.FramePointer,R_NO,R_ESP])
  1281. Then RegSet := RegSet + [Base];
  1282. If Not(Index in [procinfo^.FramePointer,R_NO,R_ESP])
  1283. Then RegSet := RegSet + [Index];
  1284. End;
  1285. End;
  1286. End;
  1287. End;}
  1288. Function OpsEquivalent(const o1, o2: toper; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  1289. Begin {checks whether the two ops are equivalent}
  1290. OpsEquivalent := False;
  1291. if o1.typ=o2.typ then
  1292. Case o1.typ Of
  1293. Top_Reg:
  1294. OpsEquivalent :=RegsEquivalent(o1.reg,o2.reg, RegInfo, OpAct);
  1295. Top_Ref:
  1296. OpsEquivalent := RefsEquivalent(o1.ref^, o2.ref^, RegInfo, OpAct);
  1297. Top_Const:
  1298. OpsEquivalent := o1.val = o2.val;
  1299. Top_None:
  1300. OpsEquivalent := True
  1301. End;
  1302. End;
  1303. Function OpsEqual(const o1,o2:toper): Boolean;
  1304. Begin {checks whether the two ops are equal}
  1305. OpsEqual := False;
  1306. if o1.typ=o2.typ then
  1307. Case o1.typ Of
  1308. Top_Reg :
  1309. OpsEqual:=o1.reg=o2.reg;
  1310. Top_Ref :
  1311. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  1312. Top_Const :
  1313. OpsEqual:=o1.val=o2.val;
  1314. Top_Symbol :
  1315. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  1316. Top_None :
  1317. OpsEqual := True
  1318. End;
  1319. End;
  1320. function sizescompatible(loadsize,newsize: topsize): boolean;
  1321. begin
  1322. case loadsize of
  1323. S_B,S_BW,S_BL:
  1324. sizescompatible := (newsize = loadsize) or (newsize = S_B);
  1325. S_W,S_WL:
  1326. sizescompatible := (newsize = loadsize) or (newsize = S_W);
  1327. else
  1328. sizescompatible := newsize = S_L;
  1329. end;
  1330. end;
  1331. function opscompatible(p1,p2: Taicpu): boolean;
  1332. begin
  1333. case p1.opcode of
  1334. A_MOVZX,A_MOVSX:
  1335. opscompatible :=
  1336. ((p2.opcode = p1.opcode) or (p2.opcode = A_MOV)) and
  1337. sizescompatible(p1.opsize,p2.opsize);
  1338. else
  1339. opscompatible :=
  1340. (p1.opcode = p2.opcode) and
  1341. (p1.opsize = p2.opsize);
  1342. end;
  1343. end;
  1344. Function InstructionsEquivalent(p1, p2: Tai; Var RegInfo: TRegInfo): Boolean;
  1345. {$ifdef csdebug}
  1346. var
  1347. hp: Tai;
  1348. {$endif csdebug}
  1349. Begin {checks whether two Taicpu instructions are equal}
  1350. If Assigned(p1) And Assigned(p2) And
  1351. (Tai(p1).typ = ait_instruction) And
  1352. (Tai(p1).typ = ait_instruction) And
  1353. opscompatible(Taicpu(p1),Taicpu(p2)) and
  1354. (Taicpu(p1).oper[0].typ = Taicpu(p2).oper[0].typ) And
  1355. (Taicpu(p1).oper[1].typ = Taicpu(p2).oper[1].typ) And
  1356. (Taicpu(p1).oper[2].typ = Taicpu(p2).oper[2].typ)
  1357. Then
  1358. {both instructions have the same structure:
  1359. "<operator> <operand of type1>, <operand of type 2>"}
  1360. If ((Taicpu(p1).opcode = A_MOV) or
  1361. (Taicpu(p1).opcode = A_MOVZX) or
  1362. (Taicpu(p1).opcode = A_MOVSX) or
  1363. (Taicpu(p1).opcode = A_LEA)) And
  1364. (Taicpu(p1).oper[0].typ = top_ref) {then .oper[1]t = top_reg} Then
  1365. If Not(RegInRef(Taicpu(p1).oper[1].reg, Taicpu(p1).oper[0].ref^)) Then
  1366. {the "old" instruction is a load of a register with a new value, not with
  1367. a value based on the contents of this register (so no "mov (reg), reg")}
  1368. If Not(RegInRef(Taicpu(p2).oper[1].reg, Taicpu(p2).oper[0].ref^)) And
  1369. RefsEqual(Taicpu(p1).oper[0].ref^, Taicpu(p2).oper[0].ref^)
  1370. Then
  1371. {the "new" instruction is also a load of a register with a new value, and
  1372. this value is fetched from the same memory location}
  1373. Begin
  1374. With Taicpu(p2).oper[0].ref^ Do
  1375. Begin
  1376. If Not(Base in [procinfo^.FramePointer, R_NO, R_ESP]) Then
  1377. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1378. If Not(Index in [procinfo^.FramePointer, R_NO, R_ESP]) Then
  1379. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1380. End;
  1381. {add the registers from the reference (.oper[0]) to the RegInfo, all registers
  1382. from the reference are the same in the old and in the new instruction
  1383. sequence}
  1384. AddOp2RegInfo(Taicpu(p1).oper[0], RegInfo);
  1385. {the registers from .oper[1] have to be equivalent, but not necessarily equal}
  1386. InstructionsEquivalent :=
  1387. RegsEquivalent(reg32(Taicpu(p1).oper[1].reg),
  1388. reg32(Taicpu(p2).oper[1].reg), RegInfo, OpAct_Write);
  1389. End
  1390. {the registers are loaded with values from different memory locations. If
  1391. this was allowed, the instructions "mov -4(esi),eax" and "mov -4(ebp),eax"
  1392. would be considered equivalent}
  1393. Else InstructionsEquivalent := False
  1394. Else
  1395. {load register with a value based on the current value of this register}
  1396. Begin
  1397. With Taicpu(p2).oper[0].ref^ Do
  1398. Begin
  1399. If Not(Base in [procinfo^.FramePointer,
  1400. Reg32(Taicpu(p2).oper[1].reg),R_NO,R_ESP]) Then
  1401. {it won't do any harm if the register is already in RegsLoadedForRef}
  1402. Begin
  1403. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1404. {$ifdef csdebug}
  1405. Writeln(gas_reg2str[base], ' added');
  1406. {$endif csdebug}
  1407. end;
  1408. If Not(Index in [procinfo^.FramePointer,
  1409. Reg32(Taicpu(p2).oper[1].reg),R_NO,R_ESP]) Then
  1410. Begin
  1411. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1412. {$ifdef csdebug}
  1413. Writeln(gas_reg2str[index], ' added');
  1414. {$endif csdebug}
  1415. end;
  1416. End;
  1417. If Not(Reg32(Taicpu(p2).oper[1].reg) In [procinfo^.FramePointer,R_NO,R_ESP])
  1418. Then
  1419. Begin
  1420. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef -
  1421. [Reg32(Taicpu(p2).oper[1].reg)];
  1422. {$ifdef csdebug}
  1423. Writeln(gas_reg2str[Reg32(Taicpu(p2).oper[1].reg)], ' removed');
  1424. {$endif csdebug}
  1425. end;
  1426. InstructionsEquivalent :=
  1427. OpsEquivalent(Taicpu(p1).oper[0], Taicpu(p2).oper[0], RegInfo, OpAct_Read) And
  1428. OpsEquivalent(Taicpu(p1).oper[1], Taicpu(p2).oper[1], RegInfo, OpAct_Write)
  1429. End
  1430. Else
  1431. {an instruction <> mov, movzx, movsx}
  1432. begin
  1433. {$ifdef csdebug}
  1434. hp := Tai_asm_comment.Create(strpnew('checking if equivalent'));
  1435. hp.previous := p2;
  1436. hp.next := p2^.next;
  1437. p2^.next^.previous := hp;
  1438. p2^.next := hp;
  1439. {$endif csdebug}
  1440. InstructionsEquivalent :=
  1441. OpsEquivalent(Taicpu(p1).oper[0], Taicpu(p2).oper[0], RegInfo, OpAct_Unknown) And
  1442. OpsEquivalent(Taicpu(p1).oper[1], Taicpu(p2).oper[1], RegInfo, OpAct_Unknown) And
  1443. OpsEquivalent(Taicpu(p1).oper[2], Taicpu(p2).oper[2], RegInfo, OpAct_Unknown)
  1444. end
  1445. {the instructions haven't even got the same structure, so they're certainly
  1446. not equivalent}
  1447. Else
  1448. begin
  1449. {$ifdef csdebug}
  1450. hp := Tai_asm_comment.Create(strpnew('different opcodes/format'));
  1451. hp.previous := p2;
  1452. hp.next := p2^.next;
  1453. p2^.next^.previous := hp;
  1454. p2^.next := hp;
  1455. {$endif csdebug}
  1456. InstructionsEquivalent := False;
  1457. end;
  1458. {$ifdef csdebug}
  1459. hp := Tai_asm_comment.Create(strpnew('instreq: '+tostr(byte(instructionsequivalent))));
  1460. hp.previous := p2;
  1461. hp.next := p2^.next;
  1462. p2^.next^.previous := hp;
  1463. p2^.next := hp;
  1464. {$endif csdebug}
  1465. End;
  1466. (*
  1467. Function InstructionsEqual(p1, p2: Tai): Boolean;
  1468. Begin {checks whether two Taicpu instructions are equal}
  1469. InstructionsEqual :=
  1470. Assigned(p1) And Assigned(p2) And
  1471. ((Tai(p1).typ = ait_instruction) And
  1472. (Tai(p1).typ = ait_instruction) And
  1473. (Taicpu(p1).opcode = Taicpu(p2).opcode) And
  1474. (Taicpu(p1).oper[0].typ = Taicpu(p2).oper[0].typ) And
  1475. (Taicpu(p1).oper[1].typ = Taicpu(p2).oper[1].typ) And
  1476. OpsEqual(Taicpu(p1).oper[0].typ, Taicpu(p1).oper[0], Taicpu(p2).oper[0]) And
  1477. OpsEqual(Taicpu(p1).oper[1].typ, Taicpu(p1).oper[1], Taicpu(p2).oper[1]))
  1478. End;
  1479. *)
  1480. Procedure ReadReg(p: PTaiProp; Reg: TRegister);
  1481. Begin
  1482. Reg := Reg32(Reg);
  1483. If Reg in [R_EAX..R_EDI] Then
  1484. incState(p^.regs[Reg].rstate,1)
  1485. End;
  1486. Procedure ReadRef(p: PTaiProp; Const Ref: POperReference);
  1487. Begin
  1488. If Ref^.Base <> R_NO Then
  1489. ReadReg(p, Ref^.Base);
  1490. If Ref^.Index <> R_NO Then
  1491. ReadReg(p, Ref^.Index);
  1492. End;
  1493. Procedure ReadOp(P: PTaiProp;const o:toper);
  1494. Begin
  1495. Case o.typ Of
  1496. top_reg: ReadReg(P, o.reg);
  1497. top_ref: ReadRef(P, o.ref);
  1498. top_symbol : ;
  1499. End;
  1500. End;
  1501. Function RefInInstruction(Const Ref: TReference; p: Tai;
  1502. RefsEq: TRefCompare): Boolean;
  1503. {checks whehter Ref is used in P}
  1504. Var TmpResult: Boolean;
  1505. Begin
  1506. TmpResult := False;
  1507. If (p.typ = ait_instruction) Then
  1508. Begin
  1509. If (Taicpu(p).oper[0].typ = Top_Ref) Then
  1510. TmpResult := RefsEq(Ref, Taicpu(p).oper[0].ref^);
  1511. If Not(TmpResult) And (Taicpu(p).oper[1].typ = Top_Ref) Then
  1512. TmpResult := RefsEq(Ref, Taicpu(p).oper[1].ref^);
  1513. If Not(TmpResult) And (Taicpu(p).oper[2].typ = Top_Ref) Then
  1514. TmpResult := RefsEq(Ref, Taicpu(p).oper[2].ref^);
  1515. End;
  1516. RefInInstruction := TmpResult;
  1517. End;
  1518. Function RefInSequence(Const Ref: TReference; Content: TContent;
  1519. RefsEq: TRefCompare): Boolean;
  1520. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1521. Tai objects) to see whether Ref is used somewhere}
  1522. Var p: Tai;
  1523. Counter: Byte;
  1524. TmpResult: Boolean;
  1525. Begin
  1526. p := Content.StartMod;
  1527. TmpResult := False;
  1528. Counter := 1;
  1529. While Not(TmpResult) And
  1530. (Counter <= Content.NrOfMods) Do
  1531. Begin
  1532. If (p.typ = ait_instruction) And
  1533. RefInInstruction(Ref, p, RefsEq)
  1534. Then TmpResult := True;
  1535. Inc(Counter);
  1536. GetNextInstruction(p,p)
  1537. End;
  1538. RefInSequence := TmpResult
  1539. End;
  1540. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  1541. Begin
  1542. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  1543. (R1.Segment = R2.Segment) And
  1544. (R1.Symbol=R2.Symbol) And
  1545. (R1.Base = R2.Base)
  1546. End;
  1547. function isSimpleRef(const ref: treference): boolean;
  1548. { returns true if ref is reference to a local or global variable, to a }
  1549. { parameter or to an object field (this includes arrays). Returns false }
  1550. { otherwise. }
  1551. begin
  1552. isSimpleRef :=
  1553. assigned(ref.symbol) or
  1554. (ref.base = procinfo^.framepointer) or
  1555. (assigned(procinfo^._class) and
  1556. (ref.base = R_ESI));
  1557. end;
  1558. function containsPointerRef(p: Tai): boolean;
  1559. { checks if an instruction contains a reference which is a pointer location }
  1560. var
  1561. hp: Taicpu;
  1562. count: longint;
  1563. begin
  1564. containsPointerRef := false;
  1565. if p.typ <> ait_instruction then
  1566. exit;
  1567. hp := Taicpu(p);
  1568. for count := low(hp.oper) to high(hp.oper) do
  1569. begin
  1570. case hp.oper[count].typ of
  1571. top_ref:
  1572. if not isSimpleRef(hp.oper[count].ref^) then
  1573. begin
  1574. containsPointerRef := true;
  1575. exit;
  1576. end;
  1577. top_none:
  1578. exit;
  1579. end;
  1580. end;
  1581. end;
  1582. function containsPointerLoad(c: tcontent): boolean;
  1583. { checks whether the contents of a register contain a pointer reference }
  1584. var
  1585. p: Tai;
  1586. count: longint;
  1587. begin
  1588. containsPointerLoad := false;
  1589. p := c.startmod;
  1590. for count := c.nrOfMods downto 1 do
  1591. begin
  1592. if containsPointerRef(p) then
  1593. begin
  1594. containsPointerLoad := true;
  1595. exit;
  1596. end;
  1597. getnextinstruction(p,p);
  1598. end;
  1599. end;
  1600. function writeToMemDestroysContents(regWritten: tregister; const ref: treference;
  1601. reg: tregister; const c: tcontent; var invalsmemwrite: boolean): boolean;
  1602. { returns whether the contents c of reg are invalid after regWritten is }
  1603. { is written to ref }
  1604. var
  1605. refsEq: trefCompare;
  1606. begin
  1607. reg := reg32(reg);
  1608. regWritten := reg32(regWritten);
  1609. if isSimpleRef(ref) then
  1610. begin
  1611. if (ref.index <> R_NO) or
  1612. (assigned(ref.symbol) and
  1613. (ref.base <> R_NO)) then
  1614. { local/global variable or parameter which is an array }
  1615. refsEq := {$ifdef fpc}@{$endif}arrayRefsEq
  1616. else
  1617. { local/global variable or parameter which is not an array }
  1618. refsEq := {$ifdef fpc}@{$endif}refsEqual;
  1619. invalsmemwrite :=
  1620. assigned(c.memwrite) and
  1621. ((not(cs_uncertainOpts in aktglobalswitches) and
  1622. containsPointerRef(c.memwrite)) or
  1623. refsEq(c.memwrite.oper[1].ref^,ref));
  1624. if not(c.typ in [con_ref,con_noRemoveRef,con_invalid]) then
  1625. begin
  1626. writeToMemDestroysContents := false;
  1627. exit;
  1628. end;
  1629. { write something to a parameter, a local or global variable, so }
  1630. { * with uncertain optimizations on: }
  1631. { - destroy the contents of registers whose contents have somewhere a }
  1632. { "mov?? (Ref), %reg". WhichReg (this is the register whose contents }
  1633. { are being written to memory) is not destroyed if it's StartMod is }
  1634. { of that form and NrOfMods = 1 (so if it holds ref, but is not a }
  1635. { expression based on Ref) }
  1636. { * with uncertain optimizations off: }
  1637. { - also destroy registers that contain any pointer }
  1638. with c do
  1639. writeToMemDestroysContents :=
  1640. (typ in [con_ref,con_noRemoveRef]) and
  1641. ((not(cs_uncertainOpts in aktglobalswitches) and
  1642. containsPointerLoad(c)
  1643. ) or
  1644. (refInSequence(ref,c,refsEq) and
  1645. ((reg <> regWritten) or
  1646. not((nrOfMods = 1) and
  1647. {StarMod is always of the type ait_instruction}
  1648. (Taicpu(StartMod).oper[0].typ = top_ref) and
  1649. refsEq(Taicpu(StartMod).oper[0].ref^, ref)
  1650. )
  1651. )
  1652. )
  1653. );
  1654. end
  1655. else
  1656. { write something to a pointer location, so }
  1657. { * with uncertain optimzations on: }
  1658. { - do not destroy registers which contain a local/global variable or }
  1659. { a parameter, except if DestroyRefs is called because of a "movsl" }
  1660. { * with uncertain optimzations off: }
  1661. { - destroy every register which contains a memory location }
  1662. begin
  1663. invalsmemwrite :=
  1664. assigned(c.memwrite) and
  1665. (not(cs_UncertainOpts in aktglobalswitches) or
  1666. containsPointerRef(c.memwrite));
  1667. if not(c.typ in [con_ref,con_noRemoveRef,con_invalid]) then
  1668. begin
  1669. writeToMemDestroysContents := false;
  1670. exit;
  1671. end;
  1672. with c do
  1673. writeToMemDestroysContents :=
  1674. (typ in [con_ref,con_noRemoveRef]) and
  1675. (not(cs_UncertainOpts in aktglobalswitches) or
  1676. { for movsl }
  1677. ((ref.base = R_EDI) and (ref.index = R_EDI)) or
  1678. { don't destroy if reg contains a parameter, local or global variable }
  1679. containsPointerLoad(c)
  1680. );
  1681. end;
  1682. end;
  1683. function writeToRegDestroysContents(destReg: tregister; reg: tregister;
  1684. const c: tcontent): boolean;
  1685. { returns whether the contents c of reg are invalid after destReg is }
  1686. { modified }
  1687. begin
  1688. writeToRegDestroysContents :=
  1689. (c.typ in [con_ref,con_noRemoveRef,con_invalid]) and
  1690. sequenceDependsOnReg(c,reg,reg32(destReg));
  1691. end;
  1692. function writeDestroysContents(const op: toper; reg: tregister;
  1693. const c: tcontent): boolean;
  1694. { returns whether the contents c of reg are invalid after regWritten is }
  1695. { is written to op }
  1696. var
  1697. dummy: boolean;
  1698. begin
  1699. reg := reg32(reg);
  1700. case op.typ of
  1701. top_reg:
  1702. writeDestroysContents :=
  1703. writeToRegDestroysContents(op.reg,reg,c);
  1704. top_ref:
  1705. writeDestroysContents :=
  1706. writeToMemDestroysContents(R_NO,op.ref^,reg,c,dummy);
  1707. else
  1708. writeDestroysContents := false;
  1709. end;
  1710. end;
  1711. procedure destroyRefs(p: Tai; const ref: treference; regWritten: tregister);
  1712. { destroys all registers which possibly contain a reference to Ref, regWritten }
  1713. { is the register whose contents are being written to memory (if this proc }
  1714. { is called because of a "mov?? %reg, (mem)" instruction) }
  1715. var
  1716. counter: TRegister;
  1717. destroymemwrite: boolean;
  1718. begin
  1719. for counter := R_EAX to R_EDI Do
  1720. begin
  1721. if writeToMemDestroysContents(regWritten,ref,counter,
  1722. pTaiProp(p.optInfo)^.regs[counter],destroymemwrite) then
  1723. destroyReg(pTaiProp(p.optInfo), counter, false)
  1724. else if destroymemwrite then
  1725. pTaiProp(p.optinfo)^.regs[counter].MemWrite := nil;
  1726. end;
  1727. End;
  1728. Procedure DestroyAllRegs(p: PTaiProp; read, written: boolean);
  1729. Var Counter: TRegister;
  1730. Begin {initializes/desrtoys all registers}
  1731. For Counter := R_EAX To R_EDI Do
  1732. Begin
  1733. if read then
  1734. ReadReg(p, Counter);
  1735. DestroyReg(p, Counter, written);
  1736. p^.regs[counter].MemWrite := nil;
  1737. End;
  1738. p^.DirFlag := F_Unknown;
  1739. End;
  1740. Procedure DestroyOp(TaiObj: Tai; const o:Toper);
  1741. {$ifdef statedebug}
  1742. var hp: Tai;
  1743. {$endif statedebug}
  1744. Begin
  1745. Case o.typ Of
  1746. top_reg:
  1747. begin
  1748. {$ifdef statedebug}
  1749. hp := Tai_asm_comment.Create(strpnew('destroying '+gas_reg2str[o.reg]));
  1750. hp.next := Taiobj^.next;
  1751. hp.previous := Taiobj;
  1752. Taiobj^.next := hp;
  1753. if assigned(hp.next) then
  1754. hp.next^.previous := hp;
  1755. {$endif statedebug}
  1756. DestroyReg(PTaiProp(TaiObj.OptInfo), reg32(o.reg), true);
  1757. end;
  1758. top_ref:
  1759. Begin
  1760. ReadRef(PTaiProp(TaiObj.OptInfo), o.ref);
  1761. DestroyRefs(TaiObj, o.ref^, R_NO);
  1762. End;
  1763. top_symbol:;
  1764. End;
  1765. End;
  1766. Function DFAPass1(AsmL: TAAsmOutput; BlockStart: Tai): Tai;
  1767. {gathers the RegAlloc data... still need to think about where to store it to
  1768. avoid global vars}
  1769. Var BlockEnd: Tai;
  1770. Begin
  1771. BlockEnd := FindLoHiLabels(LoLab, HiLab, LabDif, BlockStart);
  1772. BuildLabelTableAndFixRegAlloc(AsmL, LTable, LoLab, LabDif, BlockStart, BlockEnd);
  1773. DFAPass1 := BlockEnd;
  1774. End;
  1775. Procedure AddInstr2RegContents({$ifdef statedebug} asml: TAAsmoutput; {$endif}
  1776. p: Taicpu; reg: TRegister);
  1777. {$ifdef statedebug}
  1778. var hp: Tai;
  1779. {$endif statedebug}
  1780. Begin
  1781. Reg := Reg32(Reg);
  1782. With PTaiProp(p.optinfo)^.Regs[reg] Do
  1783. if (typ in [con_ref,con_noRemoveRef])
  1784. Then
  1785. Begin
  1786. incState(wstate,1);
  1787. {also store how many instructions are part of the sequence in the first
  1788. instructions PTaiProp, so it can be easily accessed from within
  1789. CheckSequence}
  1790. Inc(NrOfMods, NrOfInstrSinceLastMod[Reg]);
  1791. PTaiProp(Tai(StartMod).OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  1792. NrOfInstrSinceLastMod[Reg] := 0;
  1793. invalidateDependingRegs(p.optinfo,reg);
  1794. pTaiprop(p.optinfo)^.regs[reg].memwrite := nil;
  1795. {$ifdef StateDebug}
  1796. hp := Tai_asm_comment.Create(strpnew(gas_reg2str[reg]+': '+tostr(PTaiProp(p.optinfo)^.Regs[reg].WState)
  1797. + ' -- ' + tostr(PTaiProp(p.optinfo)^.Regs[reg].nrofmods))));
  1798. InsertLLItem(AsmL, p, p.next, hp);
  1799. {$endif StateDebug}
  1800. End
  1801. Else
  1802. Begin
  1803. {$ifdef statedebug}
  1804. hp := Tai_asm_comment.Create(strpnew('destroying '+gas_reg2str[reg]));
  1805. insertllitem(asml,p,p.next,hp);
  1806. {$endif statedebug}
  1807. DestroyReg(PTaiProp(p.optinfo), Reg, true);
  1808. {$ifdef StateDebug}
  1809. hp := Tai_asm_comment.Create(strpnew(gas_reg2str[reg]+': '+tostr(PTaiProp(p.optinfo)^.Regs[reg].WState)));
  1810. InsertLLItem(AsmL, p, p.next, hp);
  1811. {$endif StateDebug}
  1812. End
  1813. End;
  1814. Procedure AddInstr2OpContents({$ifdef statedebug} asml: TAAsmoutput; {$endif}
  1815. p: Taicpu; const oper: TOper);
  1816. Begin
  1817. If oper.typ = top_reg Then
  1818. AddInstr2RegContents({$ifdef statedebug} asml, {$endif}p, oper.reg)
  1819. Else
  1820. Begin
  1821. ReadOp(PTaiProp(p.optinfo), oper);
  1822. DestroyOp(p, oper);
  1823. End
  1824. End;
  1825. Procedure DoDFAPass2(
  1826. {$Ifdef StateDebug}
  1827. AsmL: TAAsmOutput;
  1828. {$endif statedebug}
  1829. BlockStart, BlockEnd: Tai);
  1830. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  1831. contents for the instructions starting with p. Returns the last Tai which has
  1832. been processed}
  1833. Var
  1834. CurProp, LastFlagsChangeProp: PTaiProp;
  1835. Cnt, InstrCnt : Longint;
  1836. InstrProp: TInsProp;
  1837. UsedRegs: TRegSet;
  1838. prev,p : Tai;
  1839. TmpRef: TReference;
  1840. TmpReg: TRegister;
  1841. {$ifdef AnalyzeLoops}
  1842. hp : Tai;
  1843. TmpState: Byte;
  1844. {$endif AnalyzeLoops}
  1845. Begin
  1846. p := BlockStart;
  1847. LastFlagsChangeProp := nil;
  1848. prev := nil;
  1849. UsedRegs := [];
  1850. UpdateUsedregs(UsedRegs, p);
  1851. SkipHead(P);
  1852. BlockStart := p;
  1853. InstrCnt := 1;
  1854. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  1855. While (P <> BlockEnd) Do
  1856. Begin
  1857. CurProp := @TaiPropBlock^[InstrCnt];
  1858. If assigned(prev)
  1859. Then
  1860. Begin
  1861. {$ifdef JumpAnal}
  1862. If (p.Typ <> ait_label) Then
  1863. {$endif JumpAnal}
  1864. Begin
  1865. CurProp^.regs := PTaiProp(prev.OptInfo)^.Regs;
  1866. CurProp^.DirFlag := PTaiProp(prev.OptInfo)^.DirFlag;
  1867. CurProp^.FlagsUsed := false;
  1868. End
  1869. End
  1870. Else
  1871. Begin
  1872. FillChar(CurProp^, SizeOf(CurProp^), 0);
  1873. { For TmpReg := R_EAX to R_EDI Do
  1874. CurProp^.regs[TmpReg].WState := 1;}
  1875. End;
  1876. CurProp^.UsedRegs := UsedRegs;
  1877. CurProp^.CanBeRemoved := False;
  1878. UpdateUsedRegs(UsedRegs, Tai(p.Next));
  1879. For TmpReg := R_EAX To R_EDI Do
  1880. if NrOfInstrSinceLastMod[TmpReg] < 255 then
  1881. Inc(NrOfInstrSinceLastMod[TmpReg])
  1882. else
  1883. begin
  1884. NrOfInstrSinceLastMod[TmpReg] := 0;
  1885. curprop^.regs[TmpReg].typ := con_unknown;
  1886. end;
  1887. Case p.typ Of
  1888. ait_marker:;
  1889. ait_label:
  1890. {$Ifndef JumpAnal}
  1891. if not labelCanBeSkipped(Tai_label(p)) then
  1892. DestroyAllRegs(CurProp,false,false);
  1893. {$Else JumpAnal}
  1894. Begin
  1895. If not labelCanBeSkipped(Tai_label(p)) Then
  1896. With LTable^[Tai_Label(p).l^.labelnr-LoLab] Do
  1897. {$IfDef AnalyzeLoops}
  1898. If (RefsFound = Tai_Label(p).l^.RefCount)
  1899. {$Else AnalyzeLoops}
  1900. If (JmpsProcessed = Tai_Label(p).l^.RefCount)
  1901. {$EndIf AnalyzeLoops}
  1902. Then
  1903. {all jumps to this label have been found}
  1904. {$IfDef AnalyzeLoops}
  1905. If (JmpsProcessed > 0)
  1906. Then
  1907. {$EndIf AnalyzeLoops}
  1908. {we've processed at least one jump to this label}
  1909. Begin
  1910. If (GetLastInstruction(p, hp) And
  1911. Not(((hp.typ = ait_instruction)) And
  1912. (Taicpu_labeled(hp).is_jmp))
  1913. Then
  1914. {previous instruction not a JMP -> the contents of the registers after the
  1915. previous intruction has been executed have to be taken into account as well}
  1916. For TmpReg := R_EAX to R_EDI Do
  1917. Begin
  1918. If (CurProp^.regs[TmpReg].WState <>
  1919. PTaiProp(hp.OptInfo)^.Regs[TmpReg].WState)
  1920. Then DestroyReg(CurProp, TmpReg, true)
  1921. End
  1922. End
  1923. {$IfDef AnalyzeLoops}
  1924. Else
  1925. {a label from a backward jump (e.g. a loop), no jump to this label has
  1926. already been processed}
  1927. If GetLastInstruction(p, hp) And
  1928. Not(hp.typ = ait_instruction) And
  1929. (Taicpu_labeled(hp).opcode = A_JMP))
  1930. Then
  1931. {previous instruction not a jmp, so keep all the registers' contents from the
  1932. previous instruction}
  1933. Begin
  1934. CurProp^.regs := PTaiProp(hp.OptInfo)^.Regs;
  1935. CurProp.DirFlag := PTaiProp(hp.OptInfo)^.DirFlag;
  1936. End
  1937. Else
  1938. {previous instruction a jmp and no jump to this label processed yet}
  1939. Begin
  1940. hp := p;
  1941. Cnt := InstrCnt;
  1942. {continue until we find a jump to the label or a label which has already
  1943. been processed}
  1944. While GetNextInstruction(hp, hp) And
  1945. Not((hp.typ = ait_instruction) And
  1946. (Taicpu(hp).is_jmp) and
  1947. (tasmlabel(Taicpu(hp).oper[0].sym).labelnr = Tai_Label(p).l^.labelnr)) And
  1948. Not((hp.typ = ait_label) And
  1949. (LTable^[Tai_Label(hp).l^.labelnr-LoLab].RefsFound
  1950. = Tai_Label(hp).l^.RefCount) And
  1951. (LTable^[Tai_Label(hp).l^.labelnr-LoLab].JmpsProcessed > 0)) Do
  1952. Inc(Cnt);
  1953. If (hp.typ = ait_label)
  1954. Then
  1955. {there's a processed label after the current one}
  1956. Begin
  1957. CurProp^.regs := TaiPropBlock^[Cnt].Regs;
  1958. CurProp.DirFlag := TaiPropBlock^[Cnt].DirFlag;
  1959. End
  1960. Else
  1961. {there's no label anymore after the current one, or they haven't been
  1962. processed yet}
  1963. Begin
  1964. GetLastInstruction(p, hp);
  1965. CurProp^.regs := PTaiProp(hp.OptInfo)^.Regs;
  1966. CurProp.DirFlag := PTaiProp(hp.OptInfo)^.DirFlag;
  1967. DestroyAllRegs(PTaiProp(hp.OptInfo),true,true)
  1968. End
  1969. End
  1970. {$EndIf AnalyzeLoops}
  1971. Else
  1972. {not all references to this label have been found, so destroy all registers}
  1973. Begin
  1974. GetLastInstruction(p, hp);
  1975. CurProp^.regs := PTaiProp(hp.OptInfo)^.Regs;
  1976. CurProp.DirFlag := PTaiProp(hp.OptInfo)^.DirFlag;
  1977. DestroyAllRegs(CurProp,true,true)
  1978. End;
  1979. End;
  1980. {$EndIf JumpAnal}
  1981. {$ifdef GDB}
  1982. ait_stabs, ait_stabn, ait_stab_function_name:;
  1983. {$endif GDB}
  1984. ait_align: ; { may destroy flags !!! }
  1985. ait_instruction:
  1986. Begin
  1987. if Taicpu(p).is_jmp or
  1988. (Taicpu(p).opcode = A_JMP) then
  1989. begin
  1990. {$IfNDef JumpAnal}
  1991. for tmpReg := R_EAX to R_EDI do
  1992. with curProp^.regs[tmpReg] do
  1993. case typ of
  1994. con_ref: typ := con_noRemoveRef;
  1995. con_const: typ := con_noRemoveConst;
  1996. con_invalid: typ := con_unknown;
  1997. end;
  1998. {$Else JumpAnal}
  1999. With LTable^[tasmlabel(Taicpu(p).oper[0].sym).labelnr-LoLab] Do
  2000. If (RefsFound = tasmlabel(Taicpu(p).oper[0].sym).RefCount) Then
  2001. Begin
  2002. If (InstrCnt < InstrNr)
  2003. Then
  2004. {forward jump}
  2005. If (JmpsProcessed = 0) Then
  2006. {no jump to this label has been processed yet}
  2007. Begin
  2008. TaiPropBlock^[InstrNr].Regs := CurProp^.regs;
  2009. TaiPropBlock^[InstrNr].DirFlag := CurProp.DirFlag;
  2010. Inc(JmpsProcessed);
  2011. End
  2012. Else
  2013. Begin
  2014. For TmpReg := R_EAX to R_EDI Do
  2015. If (TaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  2016. CurProp^.regs[TmpReg].WState) Then
  2017. DestroyReg(@TaiPropBlock^[InstrNr], TmpReg, true);
  2018. Inc(JmpsProcessed);
  2019. End
  2020. {$ifdef AnalyzeLoops}
  2021. Else
  2022. { backward jump, a loop for example}
  2023. { If (JmpsProcessed > 0) Or
  2024. Not(GetLastInstruction(TaiObj, hp) And
  2025. (hp.typ = ait_labeled_instruction) And
  2026. (Taicpu_labeled(hp).opcode = A_JMP))
  2027. Then}
  2028. {instruction prior to label is not a jmp, or at least one jump to the label
  2029. has yet been processed}
  2030. Begin
  2031. Inc(JmpsProcessed);
  2032. For TmpReg := R_EAX to R_EDI Do
  2033. If (TaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  2034. CurProp^.regs[TmpReg].WState)
  2035. Then
  2036. Begin
  2037. TmpState := TaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  2038. Cnt := InstrNr;
  2039. While (TmpState = TaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  2040. Begin
  2041. DestroyReg(@TaiPropBlock^[Cnt], TmpReg, true);
  2042. Inc(Cnt);
  2043. End;
  2044. While (Cnt <= InstrCnt) Do
  2045. Begin
  2046. Inc(TaiPropBlock^[Cnt].Regs[TmpReg].WState);
  2047. Inc(Cnt)
  2048. End
  2049. End;
  2050. End
  2051. { Else }
  2052. {instruction prior to label is a jmp and no jumps to the label have yet been
  2053. processed}
  2054. { Begin
  2055. Inc(JmpsProcessed);
  2056. For TmpReg := R_EAX to R_EDI Do
  2057. Begin
  2058. TmpState := TaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  2059. Cnt := InstrNr;
  2060. While (TmpState = TaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  2061. Begin
  2062. TaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.regs[TmpReg];
  2063. Inc(Cnt);
  2064. End;
  2065. TmpState := TaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  2066. While (TmpState = TaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  2067. Begin
  2068. DestroyReg(@TaiPropBlock^[Cnt], TmpReg, true);
  2069. Inc(Cnt);
  2070. End;
  2071. While (Cnt <= InstrCnt) Do
  2072. Begin
  2073. Inc(TaiPropBlock^[Cnt].Regs[TmpReg].WState);
  2074. Inc(Cnt)
  2075. End
  2076. End
  2077. End}
  2078. {$endif AnalyzeLoops}
  2079. End;
  2080. {$EndIf JumpAnal}
  2081. end
  2082. else
  2083. begin
  2084. InstrProp := InsProp[Taicpu(p).opcode];
  2085. Case Taicpu(p).opcode Of
  2086. A_MOV, A_MOVZX, A_MOVSX:
  2087. Begin
  2088. Case Taicpu(p).oper[0].typ Of
  2089. top_ref, top_reg:
  2090. case Taicpu(p).oper[1].typ Of
  2091. top_reg:
  2092. Begin
  2093. {$ifdef statedebug}
  2094. hp := Tai_asm_comment.Create(strpnew('destroying '+
  2095. gas_reg2str[Taicpu(p).oper[1].reg])));
  2096. insertllitem(asml,p,p.next,hp);
  2097. {$endif statedebug}
  2098. readOp(curprop, Taicpu(p).oper[0]);
  2099. tmpreg := reg32(Taicpu(p).oper[1].reg);
  2100. if regInOp(tmpreg, Taicpu(p).oper[0]) and
  2101. (curProp^.regs[tmpReg].typ in [con_ref,con_noRemoveRef]) then
  2102. begin
  2103. with curprop^.regs[tmpreg] Do
  2104. begin
  2105. incState(wstate,1);
  2106. { also store how many instructions are part of the sequence in the first }
  2107. { instruction's PTaiProp, so it can be easily accessed from within }
  2108. { CheckSequence }
  2109. inc(nrOfMods, nrOfInstrSinceLastMod[tmpreg]);
  2110. pTaiprop(startmod.optinfo)^.regs[tmpreg].nrOfMods := nrOfMods;
  2111. nrOfInstrSinceLastMod[tmpreg] := 0;
  2112. { Destroy the contents of the registers }
  2113. { that depended on the previous value of }
  2114. { this register }
  2115. invalidateDependingRegs(curprop,tmpreg);
  2116. curprop^.regs[tmpreg].memwrite := nil;
  2117. end;
  2118. end
  2119. else
  2120. begin
  2121. {$ifdef statedebug}
  2122. hp := Tai_asm_comment.Create(strpnew('destroying & initing '+gas_reg2str[tmpreg]));
  2123. insertllitem(asml,p,p.next,hp);
  2124. {$endif statedebug}
  2125. destroyReg(curprop, tmpreg, true);
  2126. if not(reginop(tmpreg, Taicpu(p).oper[0])) then
  2127. with curprop^.regs[tmpreg] Do
  2128. begin
  2129. typ := con_ref;
  2130. startmod := p;
  2131. nrOfMods := 1;
  2132. end
  2133. end;
  2134. {$ifdef StateDebug}
  2135. hp := Tai_asm_comment.Create(strpnew(gas_reg2str[TmpReg]+': '+tostr(CurProp^.regs[TmpReg].WState)));
  2136. InsertLLItem(AsmL, p, p.next, hp);
  2137. {$endif StateDebug}
  2138. End;
  2139. Top_Ref:
  2140. Begin
  2141. ReadRef(CurProp, Taicpu(p).oper[1].ref);
  2142. if taicpu(p).oper[0].typ = top_reg then
  2143. begin
  2144. ReadReg(CurProp, Taicpu(p).oper[0].reg);
  2145. DestroyRefs(p, Taicpu(p).oper[1].ref^, Taicpu(p).oper[0].reg);
  2146. pTaiProp(p.optinfo)^.regs[reg32(Taicpu(p).oper[0].reg)].memwrite :=
  2147. Taicpu(p);
  2148. end
  2149. else
  2150. DestroyRefs(p, Taicpu(p).oper[1].ref^, R_NO);
  2151. End;
  2152. End;
  2153. top_symbol,Top_Const:
  2154. Begin
  2155. Case Taicpu(p).oper[1].typ Of
  2156. Top_Reg:
  2157. Begin
  2158. TmpReg := Reg32(Taicpu(p).oper[1].reg);
  2159. {$ifdef statedebug}
  2160. hp := Tai_asm_comment.Create(strpnew('destroying '+gas_reg2str[tmpreg]));
  2161. insertllitem(asml,p,p.next,hp);
  2162. {$endif statedebug}
  2163. With CurProp^.regs[TmpReg] Do
  2164. Begin
  2165. DestroyReg(CurProp, TmpReg, true);
  2166. typ := Con_Const;
  2167. StartMod := p;
  2168. End
  2169. End;
  2170. Top_Ref:
  2171. Begin
  2172. ReadRef(CurProp, Taicpu(p).oper[1].ref);
  2173. DestroyRefs(P, Taicpu(p).oper[1].ref^, R_NO);
  2174. End;
  2175. End;
  2176. End;
  2177. End;
  2178. End;
  2179. A_DIV, A_IDIV, A_MUL:
  2180. Begin
  2181. ReadOp(Curprop, Taicpu(p).oper[0]);
  2182. ReadReg(CurProp,R_EAX);
  2183. If (Taicpu(p).OpCode = A_IDIV) or
  2184. (Taicpu(p).OpCode = A_DIV) Then
  2185. ReadReg(CurProp,R_EDX);
  2186. {$ifdef statedebug}
  2187. hp := Tai_asm_comment.Create(strpnew('destroying eax and edx'));
  2188. insertllitem(asml,p,p.next,hp);
  2189. {$endif statedebug}
  2190. { DestroyReg(CurProp, R_EAX, true);}
  2191. AddInstr2RegContents({$ifdef statedebug}asml,{$endif}
  2192. Taicpu(p), R_EAX);
  2193. DestroyReg(CurProp, R_EDX, true)
  2194. End;
  2195. A_IMUL:
  2196. Begin
  2197. ReadOp(CurProp,Taicpu(p).oper[0]);
  2198. ReadOp(CurProp,Taicpu(p).oper[1]);
  2199. If (Taicpu(p).oper[2].typ = top_none) Then
  2200. If (Taicpu(p).oper[1].typ = top_none) Then
  2201. Begin
  2202. ReadReg(CurProp,R_EAX);
  2203. {$ifdef statedebug}
  2204. hp := Tai_asm_comment.Create(strpnew('destroying eax and edx'));
  2205. insertllitem(asml,p,p.next,hp);
  2206. {$endif statedebug}
  2207. { DestroyReg(CurProp, R_EAX, true); }
  2208. AddInstr2RegContents({$ifdef statedebug}asml,{$endif}
  2209. Taicpu(p), R_EAX);
  2210. DestroyReg(CurProp, R_EDX, true)
  2211. End
  2212. Else
  2213. AddInstr2OpContents(
  2214. {$ifdef statedebug}asml,{$endif}
  2215. Taicpu(p), Taicpu(p).oper[1])
  2216. Else
  2217. AddInstr2OpContents({$ifdef statedebug}asml,{$endif}
  2218. Taicpu(p), Taicpu(p).oper[2]);
  2219. End;
  2220. A_LEA:
  2221. begin
  2222. readop(curprop,Taicpu(p).oper[0]);
  2223. if reginref(Taicpu(p).oper[1].reg,Taicpu(p).oper[0].ref^) then
  2224. AddInstr2RegContents({$ifdef statedebug}asml,{$endif}
  2225. Taicpu(p), Taicpu(p).oper[1].reg)
  2226. else
  2227. begin
  2228. {$ifdef statedebug}
  2229. hp := Tai_asm_comment.Create(strpnew('destroying & initing'+
  2230. gas_reg2str[Taicpu(p).oper[1].reg])));
  2231. insertllitem(asml,p,p.next,hp);
  2232. {$endif statedebug}
  2233. destroyreg(curprop,Taicpu(p).oper[1].reg,true);
  2234. with curprop^.regs[Taicpu(p).oper[1].reg] Do
  2235. begin
  2236. typ := con_ref;
  2237. startmod := p;
  2238. nrOfMods := 1;
  2239. end
  2240. end;
  2241. end;
  2242. Else
  2243. Begin
  2244. Cnt := 1;
  2245. While (Cnt <= MaxCh) And
  2246. (InstrProp.Ch[Cnt] <> Ch_None) Do
  2247. Begin
  2248. Case InstrProp.Ch[Cnt] Of
  2249. Ch_REAX..Ch_REDI: ReadReg(CurProp,TCh2Reg(InstrProp.Ch[Cnt]));
  2250. Ch_WEAX..Ch_RWEDI:
  2251. Begin
  2252. If (InstrProp.Ch[Cnt] >= Ch_RWEAX) Then
  2253. ReadReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  2254. {$ifdef statedebug}
  2255. hp := Tai_asm_comment.Create(strpnew('destroying '+
  2256. gas_reg2str[TCh2Reg(InstrProp.Ch[Cnt])])));
  2257. insertllitem(asml,p,p.next,hp);
  2258. {$endif statedebug}
  2259. DestroyReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]), true);
  2260. End;
  2261. Ch_MEAX..Ch_MEDI:
  2262. AddInstr2RegContents({$ifdef statedebug} asml,{$endif}
  2263. Taicpu(p),TCh2Reg(InstrProp.Ch[Cnt]));
  2264. Ch_CDirFlag: CurProp^.DirFlag := F_NotSet;
  2265. Ch_SDirFlag: CurProp^.DirFlag := F_Set;
  2266. Ch_Rop1: ReadOp(CurProp, Taicpu(p).oper[0]);
  2267. Ch_Rop2: ReadOp(CurProp, Taicpu(p).oper[1]);
  2268. Ch_ROp3: ReadOp(CurProp, Taicpu(p).oper[2]);
  2269. Ch_Wop1..Ch_RWop1:
  2270. Begin
  2271. If (InstrProp.Ch[Cnt] in [Ch_RWop1]) Then
  2272. ReadOp(CurProp, Taicpu(p).oper[0]);
  2273. DestroyOp(p, Taicpu(p).oper[0]);
  2274. End;
  2275. Ch_Mop1:
  2276. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2277. Taicpu(p), Taicpu(p).oper[0]);
  2278. Ch_Wop2..Ch_RWop2:
  2279. Begin
  2280. If (InstrProp.Ch[Cnt] = Ch_RWop2) Then
  2281. ReadOp(CurProp, Taicpu(p).oper[1]);
  2282. DestroyOp(p, Taicpu(p).oper[1]);
  2283. End;
  2284. Ch_Mop2:
  2285. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2286. Taicpu(p), Taicpu(p).oper[1]);
  2287. Ch_WOp3..Ch_RWOp3:
  2288. Begin
  2289. If (InstrProp.Ch[Cnt] = Ch_RWOp3) Then
  2290. ReadOp(CurProp, Taicpu(p).oper[2]);
  2291. DestroyOp(p, Taicpu(p).oper[2]);
  2292. End;
  2293. Ch_Mop3:
  2294. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2295. Taicpu(p), Taicpu(p).oper[2]);
  2296. Ch_WMemEDI:
  2297. Begin
  2298. ReadReg(CurProp, R_EDI);
  2299. FillChar(TmpRef, SizeOf(TmpRef), 0);
  2300. TmpRef.Base := R_EDI;
  2301. tmpRef.index := R_EDI;
  2302. DestroyRefs(p, TmpRef, R_NO)
  2303. End;
  2304. Ch_RFlags:
  2305. if assigned(LastFlagsChangeProp) then
  2306. LastFlagsChangeProp^.FlagsUsed := true;
  2307. Ch_WFlags:
  2308. LastFlagsChangeProp := CurProp;
  2309. Ch_RWFlags:
  2310. begin
  2311. if assigned(LastFlagsChangeProp) then
  2312. LastFlagsChangeProp^.FlagsUsed := true;
  2313. LastFlagsChangeProp := CurProp;
  2314. end;
  2315. Ch_FPU:;
  2316. Else
  2317. Begin
  2318. {$ifdef statedebug}
  2319. hp := Tai_asm_comment.Create(strpnew(
  2320. 'destroying all regs for prev instruction')));
  2321. insertllitem(asml,p, p.next,hp);
  2322. {$endif statedebug}
  2323. DestroyAllRegs(CurProp,true,true);
  2324. LastFlagsChangeProp := CurProp;
  2325. End;
  2326. End;
  2327. Inc(Cnt);
  2328. End
  2329. End;
  2330. end;
  2331. End;
  2332. End
  2333. Else
  2334. Begin
  2335. {$ifdef statedebug}
  2336. hp := Tai_asm_comment.Create(strpnew(
  2337. 'destroying all regs: unknown Tai: '+tostr(ord(p.typ)))));
  2338. insertllitem(asml,p, p.next,hp);
  2339. {$endif statedebug}
  2340. DestroyAllRegs(CurProp,true,true);
  2341. End;
  2342. End;
  2343. Inc(InstrCnt);
  2344. prev := p;
  2345. GetNextInstruction(p, p);
  2346. End;
  2347. End;
  2348. Function InitDFAPass2(BlockStart, BlockEnd: Tai): Boolean;
  2349. {reserves memory for the PTaiProps in one big memory block when not using
  2350. TP, returns False if not enough memory is available for the optimizer in all
  2351. cases}
  2352. Var p: Tai;
  2353. Count: Longint;
  2354. { TmpStr: String; }
  2355. Begin
  2356. P := BlockStart;
  2357. SkipHead(P);
  2358. NrOfTaiObjs := 0;
  2359. While (P <> BlockEnd) Do
  2360. Begin
  2361. {$IfDef JumpAnal}
  2362. Case p.Typ Of
  2363. ait_label:
  2364. Begin
  2365. If not labelCanBeSkipped(Tai_label(p)) Then
  2366. LTable^[Tai_Label(p).l^.labelnr-LoLab].InstrNr := NrOfTaiObjs
  2367. End;
  2368. ait_instruction:
  2369. begin
  2370. if Taicpu(p).is_jmp then
  2371. begin
  2372. If (tasmlabel(Taicpu(p).oper[0].sym).labelnr >= LoLab) And
  2373. (tasmlabel(Taicpu(p).oper[0].sym).labelnr <= HiLab) Then
  2374. Inc(LTable^[tasmlabel(Taicpu(p).oper[0].sym).labelnr-LoLab].RefsFound);
  2375. end;
  2376. end;
  2377. { ait_instruction:
  2378. Begin
  2379. If (Taicpu(p).opcode = A_PUSH) And
  2380. (Taicpu(p).oper[0].typ = top_symbol) And
  2381. (PCSymbol(Taicpu(p).oper[0])^.offset = 0) Then
  2382. Begin
  2383. TmpStr := StrPas(PCSymbol(Taicpu(p).oper[0])^.symbol);
  2384. If}
  2385. End;
  2386. {$EndIf JumpAnal}
  2387. Inc(NrOfTaiObjs);
  2388. GetNextInstruction(p, p);
  2389. End;
  2390. {Uncomment the next line to see how much memory the reloading optimizer needs}
  2391. { Writeln(NrOfTaiObjs*SizeOf(TTaiProp));}
  2392. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  2393. If NrOfTaiObjs <> 0 Then
  2394. Begin
  2395. InitDFAPass2 := True;
  2396. GetMem(TaiPropBlock, NrOfTaiObjs*SizeOf(TTaiProp));
  2397. fillchar(TaiPropBlock^,NrOfTaiObjs*SizeOf(TTaiProp),0);
  2398. p := BlockStart;
  2399. SkipHead(p);
  2400. For Count := 1 To NrOfTaiObjs Do
  2401. Begin
  2402. PTaiProp(p.OptInfo) := @TaiPropBlock^[Count];
  2403. GetNextInstruction(p, p);
  2404. End;
  2405. End
  2406. Else InitDFAPass2 := False;
  2407. End;
  2408. Function DFAPass2(
  2409. {$ifdef statedebug}
  2410. AsmL: TAAsmOutPut;
  2411. {$endif statedebug}
  2412. BlockStart, BlockEnd: Tai): Boolean;
  2413. Begin
  2414. If InitDFAPass2(BlockStart, BlockEnd) Then
  2415. Begin
  2416. DoDFAPass2(
  2417. {$ifdef statedebug}
  2418. asml,
  2419. {$endif statedebug}
  2420. BlockStart, BlockEnd);
  2421. DFAPass2 := True
  2422. End
  2423. Else DFAPass2 := False;
  2424. End;
  2425. Procedure ShutDownDFA;
  2426. Begin
  2427. If LabDif <> 0 Then
  2428. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  2429. End;
  2430. End.
  2431. {
  2432. $Log$
  2433. Revision 1.29 2002-04-14 17:00:49 carl
  2434. + att_reg2str -> gas_reg2str
  2435. Revision 1.28 2002/04/02 17:11:34 peter
  2436. * tlocation,treference update
  2437. * LOC_CONSTANT added for better constant handling
  2438. * secondadd splitted in multiple routines
  2439. * location_force_reg added for loading a location to a register
  2440. of a specified size
  2441. * secondassignment parses now first the right and then the left node
  2442. (this is compatible with Kylix). This saves a lot of push/pop especially
  2443. with string operations
  2444. * adapted some routines to use the new cg methods
  2445. Revision 1.27 2002/03/31 20:26:38 jonas
  2446. + a_loadfpu_* and a_loadmm_* methods in tcg
  2447. * register allocation is now handled by a class and is mostly processor
  2448. independent (+rgobj.pas and i386/rgcpu.pas)
  2449. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2450. * some small improvements and fixes to the optimizer
  2451. * some register allocation fixes
  2452. * some fpuvaroffset fixes in the unary minus node
  2453. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2454. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2455. also better optimizable)
  2456. * fixed and optimized register saving/restoring for new/dispose nodes
  2457. * LOC_FPU locations now also require their "register" field to be set to
  2458. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2459. - list field removed of the tnode class because it's not used currently
  2460. and can cause hard-to-find bugs
  2461. Revision 1.26 2002/03/04 19:10:13 peter
  2462. * removed compiler warnings
  2463. Revision 1.25 2001/12/29 15:29:59 jonas
  2464. * powerpc/cgcpu.pas compiles :)
  2465. * several powerpc-related fixes
  2466. * cpuasm unit is now based on common tainst unit
  2467. + nppcmat unit for powerpc (almost complete)
  2468. Revision 1.24 2001/11/02 22:58:09 peter
  2469. * procsym definition rewrite
  2470. Revision 1.23 2001/10/27 10:20:43 jonas
  2471. + replace mem accesses to locations to which a reg was stored recently with that reg
  2472. Revision 1.22 2001/10/12 13:58:05 jonas
  2473. + memory references are now replaced by register reads in "regular"
  2474. instructions (e.g. "addl ref1,%eax" will be replaced by "addl %ebx,%eax"
  2475. if %ebx contains ref1). Previously only complete load sequences were
  2476. optimized away, but not such small accesses in other instructions than
  2477. mov/movzx/movsx
  2478. Revision 1.21 2001/09/04 14:01:04 jonas
  2479. * commented out some inactive code in csopt386
  2480. + small improvement: lea is now handled the same as mov/zx/sx
  2481. Revision 1.20 2001/08/29 14:07:43 jonas
  2482. * the optimizer now keeps track of flags register usage. This fixes some
  2483. optimizer bugs with int64 calculations (because of the carry flag usage)
  2484. * fixed another bug which caused wrong optimizations with complex
  2485. array expressions
  2486. Revision 1.19 2001/08/26 13:36:55 florian
  2487. * some cg reorganisation
  2488. * some PPC updates
  2489. Revision 1.18 2001/08/06 21:40:50 peter
  2490. * funcret moved from tprocinfo to tprocdef
  2491. Revision 1.17 2001/04/13 01:22:18 peter
  2492. * symtable change to classes
  2493. * range check generation and errors fixed, make cycle DEBUG=1 works
  2494. * memory leaks fixed
  2495. Revision 1.16 2001/04/02 21:20:36 peter
  2496. * resulttype rewrite
  2497. Revision 1.15 2000/12/31 11:00:31 jonas
  2498. * fixed potential bug in writeToMemDestroysContents
  2499. Revision 1.14 2000/12/25 00:07:32 peter
  2500. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2501. tlinkedlist objects)
  2502. Revision 1.13 2000/12/21 12:22:53 jonas
  2503. * fixed range error
  2504. Revision 1.12 2000/12/04 17:00:09 jonas
  2505. * invalidate regs that depend on a modified register
  2506. Revision 1.11 2000/11/29 00:30:44 florian
  2507. * unused units removed from uses clause
  2508. * some changes for widestrings
  2509. Revision 1.10 2000/11/28 16:32:11 jonas
  2510. + support for optimizing simple sequences with div/idiv/mul opcodes
  2511. Revision 1.9 2000/11/23 14:20:18 jonas
  2512. * fixed stupid bug in previous commit
  2513. Revision 1.8 2000/11/23 13:26:33 jonas
  2514. * fix for webbug 1066/1126
  2515. Revision 1.7 2000/11/17 15:22:04 jonas
  2516. * fixed another bug in allocregbetween (introduced by the previous fix)
  2517. ("merged")
  2518. Revision 1.6 2000/11/14 13:26:10 jonas
  2519. * fixed bug in allocregbetween
  2520. Revision 1.5 2000/11/08 16:04:34 sg
  2521. * Fix for containsPointerRef: Loop now runs in the correct range
  2522. Revision 1.4 2000/11/03 18:06:26 jonas
  2523. * fixed bug in arrayRefsEq
  2524. * object/class fields are now handled the same as local/global vars and
  2525. parameters (ie. a write to a local var can now never destroy a class
  2526. field)
  2527. Revision 1.3 2000/10/24 10:40:53 jonas
  2528. + register renaming ("fixes" bug1088)
  2529. * changed command line options meanings for optimizer:
  2530. O2 now means peepholopts, CSE and register renaming in 1 pass
  2531. O3 is the same, but repeated until no further optimizations are
  2532. possible or until 5 passes have been done (to avoid endless loops)
  2533. * changed aopt386 so it does this looping
  2534. * added some procedures from csopt386 to the interface because they're
  2535. used by rropt386 as well
  2536. * some changes to csopt386 and daopt386 so that newly added instructions
  2537. by the CSE get optimizer info (they were simply skipped previously),
  2538. this fixes some bugs
  2539. Revision 1.2 2000/10/19 15:59:40 jonas
  2540. * fixed bug in allocregbetween (the register wasn't added to the
  2541. usedregs set of the last instruction of the chain) ("merged")
  2542. Revision 1.1 2000/10/15 09:47:43 peter
  2543. * moved to i386/
  2544. Revision 1.16 2000/10/14 10:14:47 peter
  2545. * moehrendorf oct 2000 rewrite
  2546. Revision 1.15 2000/09/30 13:07:23 jonas
  2547. * fixed support for -Or with new features of CSE
  2548. Revision 1.14 2000/09/29 23:14:11 jonas
  2549. + writeToMemDestroysContents() and writeDestroysContents() to support the
  2550. new features of the CSE
  2551. Revision 1.13 2000/09/25 09:50:30 jonas
  2552. - removed TP conditional code
  2553. Revision 1.12 2000/09/24 21:19:50 peter
  2554. * delphi compile fixes
  2555. Revision 1.11 2000/09/24 15:06:15 peter
  2556. * use defines.inc
  2557. Revision 1.10 2000/09/22 15:00:20 jonas
  2558. * fixed bug in regsEquivalent (in some rare cases, registers with
  2559. completely unrelated content were considered equivalent) (merged
  2560. from fixes branch)
  2561. Revision 1.9 2000/09/20 15:00:58 jonas
  2562. + much improved CSE: the CSE now searches further back for sequences it
  2563. can reuse. After I've also implemented register renaming, the effect
  2564. should be even better (afaik web bug 1088 will then even be optimized
  2565. properly). I don't know about the slow down factor this adds. Maybe
  2566. a new optimization level should be introduced?
  2567. Revision 1.8 2000/08/25 19:39:18 jonas
  2568. * bugfix to FindRegAlloc function (caused wrong regalloc info in
  2569. some cases) (merged from fixes branch)
  2570. Revision 1.7 2000/08/23 12:55:10 jonas
  2571. * fix for web bug 1112 and a bit of clean up in csopt386 (merged from
  2572. fixes branch)
  2573. Revision 1.6 2000/08/19 17:53:29 jonas
  2574. * fixed a potential bug in destroyregs regarding the removal of
  2575. unused loads
  2576. * added destroyDependingRegs() procedure and use it for the fix in
  2577. the previous commit (safer/more complete than what was done before)
  2578. Revision 1.5 2000/08/19 09:08:59 jonas
  2579. * fixed bug where the contents of a register would not be destroyed
  2580. if another register on which these contents depend is modified
  2581. (not really merged, but same idea as fix in fixes branch,
  2582. LAST_MERGE tag is updated)
  2583. Revision 1.4 2000/07/21 15:19:54 jonas
  2584. * daopt386: changes to getnextinstruction/getlastinstruction so they
  2585. ignore labels who have is_addr set
  2586. + daopt386/csopt386: remove loads of registers which are overwritten
  2587. before their contents are used (especially usefull for removing superfluous
  2588. maybe_loadself outputs and push/pops transformed by below optimization
  2589. + popt386: transform pop/pop/pop/.../push/push/push to sequences of
  2590. 'movl x(%esp),%reg' (only active when compiling a go32v2 compiler
  2591. currently because I don't know whether it's safe to do this under Win32/
  2592. Linux (because of problems we had when using esp as frame pointer on
  2593. those os'es)
  2594. Revision 1.3 2000/07/14 05:11:48 michael
  2595. + Patch to 1.1
  2596. Revision 1.2 2000/07/13 11:32:40 michael
  2597. + removed logs
  2598. }