daopt386.pas 101 KB

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