daopt386.pas 98 KB

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