daopt386.pas 96 KB

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