daopt386.pas 98 KB

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