daopt386.pas 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436
  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. {$ifDef TP}
  21. {$UnDef JumpAnal}
  22. {$Endif TP}
  23. Unit DAOpt386;
  24. {$ifdef newOptimizations}
  25. {$define foropt}
  26. {$define replacereg}
  27. {$define arithopt}
  28. {$define foldarithops}
  29. {$endif newOptimizations}
  30. Interface
  31. Uses
  32. GlobType,
  33. CObjects,Aasm,
  34. cpubase,cpuasm;
  35. Type
  36. TRegArray = Array[R_EAX..R_BL] of TRegister;
  37. TRegSet = Set of R_EAX..R_BL;
  38. TRegInfo = Record
  39. NewRegsEncountered, OldRegsEncountered: TRegSet;
  40. RegsLoadedForRef: TRegSet;
  41. New2OldReg: TRegArray;
  42. End;
  43. {possible actions on an operand: read, write or modify (= read & write)}
  44. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  45. {*********************** Procedures and Functions ************************}
  46. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  47. Function Reg32(Reg: TRegister): TRegister;
  48. Function RefsEquivalent(Const R1, R2: TReference; Var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  49. Function RefsEqual(Const R1, R2: TReference): Boolean;
  50. Function IsGP32Reg(Reg: TRegister): Boolean;
  51. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  52. function RegReadByInstruction(reg: TRegister; hp: pai): boolean;
  53. function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  54. function RegInInstruction(Reg: TRegister; p1: Pai): Boolean;
  55. function RegInOp(Reg: TRegister; const o:toper): Boolean;
  56. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  57. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  58. Procedure SkipHead(var P: Pai);
  59. Procedure RemoveLastDeallocForFuncRes(asmL: PAasmOutput; p: pai);
  60. Function regLoadedWithNewValue(reg: tregister; canDependOnPrevValue: boolean;
  61. hp: pai): boolean;
  62. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Pai);
  63. Procedure AllocRegBetween(AsmL: PAasmOutput; Reg: TRegister; p1, p2: Pai);
  64. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  65. Function InstructionsEquivalent(p1, p2: Pai; Var RegInfo: TRegInfo): Boolean;
  66. Function OpsEqual(const o1,o2:toper): Boolean;
  67. Function DFAPass1(AsmL: PAasmOutput; BlockStart: Pai): Pai;
  68. Function DFAPass2(
  69. {$ifdef statedebug}
  70. AsmL: PAasmOutPut;
  71. {$endif statedebug}
  72. BlockStart, BlockEnd: Pai): Boolean;
  73. Procedure ShutDownDFA;
  74. Function FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  75. {******************************* Constants *******************************}
  76. Const
  77. {Possible register content types}
  78. con_Unknown = 0;
  79. con_ref = 1;
  80. con_const = 2;
  81. {********************************* Types *********************************}
  82. type
  83. {the possible states of a flag}
  84. TFlagContents = (F_Unknown, F_NotSet, F_Set);
  85. TContent = Packed Record
  86. {start and end of block instructions that defines the
  87. content of this register.}
  88. StartMod: pai;
  89. {starts at 0, gets increased everytime the register is written to}
  90. WState: Byte;
  91. {starts at 0, gets increased everytime the register is read from}
  92. RState: Byte;
  93. {how many instructions starting with StarMod does the block consist of}
  94. NrOfMods: Byte;
  95. {the type of the content of the register: unknown, memory, constant}
  96. Typ: Byte;
  97. End;
  98. {Contents of the integer registers}
  99. TRegContent = Array[R_EAX..R_EDI] Of TContent;
  100. {contents of the FPU registers}
  101. TRegFPUContent = Array[R_ST..R_ST7] Of TContent;
  102. {$ifdef tempOpts}
  103. { linked list which allows searching/deleting based on value, no extra frills}
  104. PSearchLinkedListItem = ^TSearchLinkedListItem;
  105. TSearchLinkedListItem = object(TLinkedList_Item)
  106. constructor init;
  107. function equals(p: PSearchLinkedListItem): boolean; virtual;
  108. end;
  109. PSearchDoubleIntItem = ^TSearchDoubleInttem;
  110. TSearchDoubleIntItem = object(TLinkedList_Item)
  111. constructor init(_int1,_int2: longint);
  112. function equals(p: PSearchLinkedListItem): boolean; virtual;
  113. private
  114. int1, int2: longint;
  115. end;
  116. PSearchLinkedList = ^TSearchLinkedList;
  117. TSearchLinkedList = object(TLinkedList)
  118. function searchByValue(p: PSearchLinkedListItem): boolean;
  119. procedure removeByValue(p: PSearchLinkedListItem);
  120. end;
  121. {$endif tempOpts}
  122. {information record with the contents of every register. Every Pai object
  123. gets one of these assigned: a pointer to it is stored in the OptInfo field}
  124. TPaiProp = Record
  125. Regs: TRegContent;
  126. { FPURegs: TRegFPUContent;} {currently not yet used}
  127. { allocated Registers }
  128. UsedRegs: TRegSet;
  129. { status of the direction flag }
  130. DirFlag: TFlagContents;
  131. {$ifdef tempOpts}
  132. { currently used temps }
  133. tempAllocs: PSearchLinkedList;
  134. {$endif tempOpts}
  135. { can this instruction be removed? }
  136. CanBeRemoved: Boolean;
  137. End;
  138. PPaiProp = ^TPaiProp;
  139. {$IfNDef TP}
  140. TPaiPropBlock = Array[1..250000] Of TPaiProp;
  141. PPaiPropBlock = ^TPaiPropBlock;
  142. {$EndIf TP}
  143. TInstrSinceLastMod = Array[R_EAX..R_EDI] Of Byte;
  144. TLabelTableItem = Record
  145. PaiObj: Pai;
  146. {$IfDef JumpAnal}
  147. InstrNr: Longint;
  148. RefsFound: Word;
  149. JmpsProcessed: Word
  150. {$EndIf JumpAnal}
  151. End;
  152. {$IfDef tp}
  153. TLabelTable = Array[0..10000] Of TLabelTableItem;
  154. {$Else tp}
  155. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  156. {$Endif tp}
  157. PLabelTable = ^TLabelTable;
  158. {******************************* Variables *******************************}
  159. Var
  160. {the amount of PaiObjects in the current assembler list}
  161. NrOfPaiObjs: Longint;
  162. {$IfNDef TP}
  163. {Array which holds all TPaiProps}
  164. PaiPropBlock: PPaiPropBlock;
  165. {$EndIf TP}
  166. LoLab, HiLab, LabDif: Longint;
  167. LTable: PLabelTable;
  168. {*********************** End of Interface section ************************}
  169. Implementation
  170. Uses
  171. globals, systems, strings, verbose, hcodegen, symconst, tgeni386;
  172. Type
  173. TRefCompare = function(const r1, r2: TReference): Boolean;
  174. Var
  175. {How many instructions are between the current instruction and the last one
  176. that modified the register}
  177. NrOfInstrSinceLastMod: TInstrSinceLastMod;
  178. {$ifdef tempOpts}
  179. constructor TSearchLinkedListItem.init;
  180. begin
  181. end;
  182. function TSearchLinkedListItem.equals(p: PSearchLinkedListItem): boolean;
  183. begin
  184. equals := false;
  185. end;
  186. constructor TSearchDoubleIntItem.init(_int1,_int2: longint);
  187. begin
  188. int1 := _int1;
  189. int2 := _int2;
  190. end;
  191. function TSearchDoubleIntItem.equals(p: PSearchLinkedListItem): boolean;
  192. begin
  193. equals := (TSearchDoubleIntItem(p).int1 = int1) and
  194. (TSearchDoubleIntItem(p).int2 = int2);
  195. end;
  196. function TSearchLinkedList.searchByValue(p: PSearchLinkedListItem): boolean;
  197. var temp: PSearchLinkedListItem;
  198. begin
  199. temp := first;
  200. while (temp <> last^.next) and
  201. not(temp^.equals(p)) do
  202. temp := temp^.next;
  203. searchByValue := temp <> last^.next;
  204. end;
  205. procedure TSearchLinkedList.removeByValue(p: PSearchLinkedListItem);
  206. begin
  207. temp := first;
  208. while (temp <> last^.next) and
  209. not(temp^.equals(p)) do
  210. temp := temp^.next;
  211. if temp <> last^.next then
  212. begin
  213. remove(temp);
  214. dispose(temp,done);
  215. end;
  216. end;
  217. Procedure updateTempAllocs(Var UsedRegs: TRegSet; p: Pai);
  218. {updates UsedRegs with the RegAlloc Information coming after P}
  219. Begin
  220. Repeat
  221. While Assigned(p) And
  222. ((p^.typ in (SkipInstr - [ait_RegAlloc])) or
  223. ((p^.typ = ait_label) And
  224. Not(Pai_Label(p)^.l^.is_used))) Do
  225. p := Pai(p^.next);
  226. While Assigned(p) And
  227. (p^.typ=ait_RegAlloc) Do
  228. Begin
  229. if pairegalloc(p)^.allocation then
  230. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  231. else
  232. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  233. p := pai(p^.next);
  234. End;
  235. Until Not(Assigned(p)) Or
  236. (Not(p^.typ in SkipInstr) And
  237. Not((p^.typ = ait_label) And
  238. Not(Pai_Label(p)^.l^.is_used)));
  239. End;
  240. {$endif tempOpts}
  241. {************************ Create the Label table ************************}
  242. Function FindLoHiLabels(Var LowLabel, HighLabel, LabelDif: Longint; BlockStart: Pai): Pai;
  243. {Walks through the paasmlist to find the lowest and highest label number}
  244. Var LabelFound: Boolean;
  245. P, lastP: Pai;
  246. Begin
  247. LabelFound := False;
  248. LowLabel := MaxLongint;
  249. HighLabel := 0;
  250. P := BlockStart;
  251. lastP := p;
  252. While Assigned(P) Do
  253. Begin
  254. If (Pai(p)^.typ = ait_label) Then
  255. If (Pai_Label(p)^.l^.is_used)
  256. Then
  257. Begin
  258. LabelFound := True;
  259. If (Pai_Label(p)^.l^.labelnr < LowLabel) Then
  260. LowLabel := Pai_Label(p)^.l^.labelnr;
  261. If (Pai_Label(p)^.l^.labelnr > HighLabel) Then
  262. HighLabel := Pai_Label(p)^.l^.labelnr;
  263. End;
  264. lastP := p;
  265. GetNextInstruction(p, p);
  266. End;
  267. if (lastP^.typ = ait_marker) and
  268. (pai_marker(lastP)^.kind = asmBlockStart) then
  269. FindLoHiLabels := lastP
  270. else FindLoHiLabels := nil;
  271. If LabelFound
  272. Then LabelDif := HighLabel+1-LowLabel
  273. Else LabelDif := 0;
  274. End;
  275. Function FindRegAlloc(Reg: TRegister; StartPai: Pai; alloc: boolean): Boolean;
  276. { Returns true if a ait_alloc object for Reg is found in the block of Pai's }
  277. { starting with StartPai and ending with the next "real" instruction }
  278. Begin
  279. FindRegAlloc := false;
  280. Repeat
  281. While Assigned(StartPai) And
  282. ((StartPai^.typ in (SkipInstr - [ait_regAlloc])) Or
  283. ((StartPai^.typ = ait_label) and
  284. Not(Pai_Label(StartPai)^.l^.Is_Used))) Do
  285. StartPai := Pai(StartPai^.Next);
  286. If Assigned(StartPai) And
  287. (StartPai^.typ = ait_regAlloc) and (PairegAlloc(StartPai)^.allocation = alloc) Then
  288. Begin
  289. if PairegAlloc(StartPai)^.Reg = Reg then
  290. begin
  291. FindRegAlloc:=true;
  292. break;
  293. end;
  294. StartPai := Pai(StartPai^.Next);
  295. End
  296. else
  297. break;
  298. Until false;
  299. End;
  300. Procedure RemoveLastDeallocForFuncRes(asmL: PAasmOutput; p: pai);
  301. Procedure DoRemoveLastDeallocForFuncRes(asmL: PAasmOutput; reg: TRegister);
  302. var
  303. hp2: pai;
  304. begin
  305. hp2 := p;
  306. repeat
  307. hp2 := pai(hp2^.previous);
  308. if assigned(hp2) and
  309. (hp2^.typ = ait_regalloc) and
  310. not(pairegalloc(hp2)^.allocation) and
  311. (pairegalloc(hp2)^.reg = reg) then
  312. begin
  313. asml^.remove(hp2);
  314. dispose(hp2,done);
  315. break;
  316. end;
  317. until not(assigned(hp2)) or
  318. regInInstruction(reg,hp2);
  319. end;
  320. begin
  321. if assigned(procinfo^.returntype.def) then
  322. case procinfo^.returntype.def^.deftype of
  323. arraydef,recorddef,pointerdef,
  324. stringdef,enumdef,procdef,objectdef,errordef,
  325. filedef,setdef,procvardef,
  326. classrefdef,forwarddef:
  327. DoRemoveLastDeallocForFuncRes(asmL,R_EAX);
  328. orddef:
  329. if procinfo^.returntype.def^.size <> 0 then
  330. begin
  331. DoRemoveLastDeallocForFuncRes(asmL,R_EAX);
  332. { for int64/qword }
  333. if procinfo^.returntype.def^.size = 8 then
  334. DoRemoveLastDeallocForFuncRes(asmL,R_EDX);
  335. end;
  336. end;
  337. end;
  338. procedure getNoDeallocRegs(var regs: TRegSet);
  339. var regCounter: TRegister;
  340. begin
  341. regs := [];
  342. if assigned(procinfo^.returntype.def) then
  343. case procinfo^.returntype.def^.deftype of
  344. arraydef,recorddef,pointerdef,
  345. stringdef,enumdef,procdef,objectdef,errordef,
  346. filedef,setdef,procvardef,
  347. classrefdef,forwarddef:
  348. regs := [R_EAX];
  349. orddef:
  350. if procinfo^.returntype.def^.size <> 0 then
  351. begin
  352. regs := [R_EAX];
  353. { for int64/qword }
  354. if procinfo^.returntype.def^.size = 8 then
  355. regs := regs + [R_EDX];
  356. end;
  357. end;
  358. for regCounter := R_EAX to R_EBX do
  359. if not(regCounter in usableregs) then
  360. regs := regs + [regCounter];
  361. end;
  362. Procedure AddRegDeallocFor(asmL: paasmOutput; reg: TRegister; p: pai);
  363. var hp1: pai;
  364. funcResRegs: TRegset;
  365. funcResReg: boolean;
  366. begin
  367. if not(reg in usableregs) then
  368. exit;
  369. getNoDeallocRegs(funcResRegs);
  370. funcResRegs := funcResRegs - usableregs;
  371. funcResReg := reg in funcResRegs;
  372. hp1 := p;
  373. while not(funcResReg and
  374. (p^.typ = ait_instruction) and
  375. (paicpu(p)^.opcode = A_JMP) and
  376. (pasmlabel(paicpu(p)^.oper[0].sym) = aktexit2label)) and
  377. getLastInstruction(p, p) And
  378. not(regInInstruction(reg, p)) Do
  379. hp1 := p;
  380. { don't insert a dealloc for registers which contain the function result }
  381. { if they are followed by a jump to the exit label (for exit(...)) }
  382. if not(funcResReg) or
  383. not((hp1^.typ = ait_instruction) and
  384. (paicpu(hp1)^.opcode = A_JMP) and
  385. (pasmlabel(paicpu(hp1)^.oper[0].sym) = aktexit2label)) then
  386. begin
  387. p := new(paiRegAlloc, deAlloc(reg));
  388. insertLLItem(AsmL, hp1^.previous, hp1, p);
  389. end;
  390. end;
  391. Procedure BuildLabelTableAndFixRegAlloc(asmL: PAasmOutput; Var LabelTable: PLabelTable; LowLabel: Longint;
  392. Var LabelDif: Longint; BlockStart, BlockEnd: Pai);
  393. {Builds a table with the locations of the labels in the paasmoutput.
  394. Also fixes some RegDeallocs like "# %eax released; push (%eax)"}
  395. Var p, hp1, hp2, lastP: Pai;
  396. regCounter: TRegister;
  397. UsedRegs, noDeallocRegs: TRegSet;
  398. Begin
  399. UsedRegs := [];
  400. If (LabelDif <> 0) Then
  401. Begin
  402. {$IfDef TP}
  403. If (MaxAvail >= LabelDif*SizeOf(Pai))
  404. Then
  405. Begin
  406. {$EndIf TP}
  407. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  408. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  409. {$IfDef TP}
  410. End
  411. Else LabelDif := 0;
  412. {$EndIf TP}
  413. End;
  414. p := BlockStart;
  415. lastP := p;
  416. While (P <> BlockEnd) Do
  417. Begin
  418. Case p^.typ Of
  419. ait_Label:
  420. If Pai_Label(p)^.l^.is_used Then
  421. LabelTable^[Pai_Label(p)^.l^.labelnr-LowLabel].PaiObj := p;
  422. ait_regAlloc:
  423. { ESI and EDI are (de)allocated manually, don't mess with them }
  424. if not(paiRegAlloc(p)^.Reg in [R_EDI,R_ESI]) then
  425. begin
  426. if PairegAlloc(p)^.Allocation then
  427. Begin
  428. If Not(paiRegAlloc(p)^.Reg in UsedRegs) Then
  429. UsedRegs := UsedRegs + [paiRegAlloc(p)^.Reg]
  430. Else
  431. addRegDeallocFor(asmL, paiRegAlloc(p)^.reg, p);
  432. End
  433. else
  434. begin
  435. UsedRegs := UsedRegs - [paiRegAlloc(p)^.Reg];
  436. hp1 := p;
  437. hp2 := nil;
  438. While Not(FindRegAlloc(paiRegAlloc(p)^.Reg, Pai(hp1^.Next),true)) And
  439. GetNextInstruction(hp1, hp1) And
  440. RegInInstruction(paiRegAlloc(p)^.Reg, hp1) Do
  441. hp2 := hp1;
  442. If hp2 <> nil Then
  443. Begin
  444. hp1 := Pai(p^.previous);
  445. AsmL^.Remove(p);
  446. InsertLLItem(AsmL, hp2, Pai(hp2^.Next), p);
  447. p := hp1;
  448. end;
  449. end;
  450. end;
  451. end;
  452. repeat
  453. lastP := p;
  454. P := Pai(P^.Next);
  455. until not(Assigned(p)) or
  456. not(p^.typ in (SkipInstr - [ait_regalloc]));
  457. End;
  458. { don't add deallocation for function result variable or for regvars}
  459. getNoDeallocRegs(noDeallocRegs);
  460. usedRegs := usedRegs - noDeallocRegs;
  461. for regCounter := R_EAX to R_EDI do
  462. if regCounter in usedRegs then
  463. addRegDeallocFor(asmL,regCounter,lastP);
  464. End;
  465. {************************ Search the Label table ************************}
  466. Function FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  467. {searches for the specified label starting from hp as long as the
  468. encountered instructions are labels, to be able to optimize constructs like
  469. jne l2 jmp l2
  470. jmp l3 and l1:
  471. l1: l2:
  472. l2:}
  473. Var TempP: Pai;
  474. Begin
  475. TempP := hp;
  476. While Assigned(TempP) and
  477. (TempP^.typ In SkipInstr + [ait_label,ait_align]) Do
  478. If (TempP^.typ <> ait_Label) Or
  479. (pai_label(TempP)^.l <> L)
  480. Then GetNextInstruction(TempP, TempP)
  481. Else
  482. Begin
  483. hp := TempP;
  484. FindLabel := True;
  485. exit
  486. End;
  487. FindLabel := False;
  488. End;
  489. {************************ Some general functions ************************}
  490. Function TCh2Reg(Ch: TInsChange): TRegister;
  491. {converts a TChange variable to a TRegister}
  492. Begin
  493. If (Ch <= Ch_REDI) Then
  494. TCh2Reg := TRegister(Byte(Ch))
  495. Else
  496. If (Ch <= Ch_WEDI) Then
  497. TCh2Reg := TRegister(Byte(Ch) - Byte(Ch_REDI))
  498. Else
  499. If (Ch <= Ch_RWEDI) Then
  500. TCh2Reg := TRegister(Byte(Ch) - Byte(Ch_WEDI))
  501. Else
  502. If (Ch <= Ch_MEDI) Then
  503. TCh2Reg := TRegister(Byte(Ch) - Byte(Ch_RWEDI))
  504. Else InternalError($db)
  505. End;
  506. Function Reg32(Reg: TRegister): TRegister;
  507. {Returns the 32 bit component of Reg if it exists, otherwise Reg is returned}
  508. Begin
  509. Reg32 := Reg;
  510. If (Reg >= R_AX)
  511. Then
  512. If (Reg <= R_DI)
  513. Then Reg32 := Reg16ToReg32(Reg)
  514. Else
  515. If (Reg <= R_BL)
  516. Then Reg32 := Reg8toReg32(Reg);
  517. End;
  518. { inserts new_one between prev and foll }
  519. Procedure InsertLLItem(AsmL: PAasmOutput; prev, foll, new_one: PLinkedList_Item);
  520. Begin
  521. If Assigned(prev) Then
  522. If Assigned(foll) Then
  523. Begin
  524. If Assigned(new_one) Then
  525. Begin
  526. new_one^.previous := prev;
  527. new_one^.next := foll;
  528. prev^.next := new_one;
  529. foll^.previous := new_one;
  530. Pai(new_one)^.fileinfo := Pai(foll)^.fileinfo;
  531. End;
  532. End
  533. Else AsmL^.Concat(new_one)
  534. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  535. End;
  536. {********************* Compare parts of Pai objects *********************}
  537. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  538. {returns true if Reg1 and Reg2 are of the same size (so if they're both
  539. 8bit, 16bit or 32bit)}
  540. Begin
  541. If (Reg1 <= R_EDI)
  542. Then RegsSameSize := (Reg2 <= R_EDI)
  543. Else
  544. If (Reg1 <= R_DI)
  545. Then RegsSameSize := (Reg2 in [R_AX..R_DI])
  546. Else
  547. If (Reg1 <= R_BL)
  548. Then RegsSameSize := (Reg2 in [R_AL..R_BL])
  549. Else RegsSameSize := False
  550. End;
  551. Procedure AddReg2RegInfo(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo);
  552. {updates the ???RegsEncountered and ???2???Reg fields of RegInfo. Assumes that
  553. OldReg and NewReg have the same size (has to be chcked in advance with
  554. RegsSameSize) and that neither equals R_NO}
  555. Begin
  556. With RegInfo Do
  557. Begin
  558. NewRegsEncountered := NewRegsEncountered + [NewReg];
  559. OldRegsEncountered := OldRegsEncountered + [OldReg];
  560. New2OldReg[NewReg] := OldReg;
  561. Case OldReg Of
  562. R_EAX..R_EDI:
  563. Begin
  564. NewRegsEncountered := NewRegsEncountered + [Reg32toReg16(NewReg)];
  565. OldRegsEncountered := OldRegsEncountered + [Reg32toReg16(OldReg)];
  566. New2OldReg[Reg32toReg16(NewReg)] := Reg32toReg16(OldReg);
  567. If (NewReg in [R_EAX..R_EBX]) And
  568. (OldReg in [R_EAX..R_EBX]) Then
  569. Begin
  570. NewRegsEncountered := NewRegsEncountered + [Reg32toReg8(NewReg)];
  571. OldRegsEncountered := OldRegsEncountered + [Reg32toReg8(OldReg)];
  572. New2OldReg[Reg32toReg8(NewReg)] := Reg32toReg8(OldReg);
  573. End;
  574. End;
  575. R_AX..R_DI:
  576. Begin
  577. NewRegsEncountered := NewRegsEncountered + [Reg16toReg32(NewReg)];
  578. OldRegsEncountered := OldRegsEncountered + [Reg16toReg32(OldReg)];
  579. New2OldReg[Reg16toReg32(NewReg)] := Reg16toReg32(OldReg);
  580. If (NewReg in [R_AX..R_BX]) And
  581. (OldReg in [R_AX..R_BX]) Then
  582. Begin
  583. NewRegsEncountered := NewRegsEncountered + [Reg16toReg8(NewReg)];
  584. OldRegsEncountered := OldRegsEncountered + [Reg16toReg8(OldReg)];
  585. New2OldReg[Reg16toReg8(NewReg)] := Reg16toReg8(OldReg);
  586. End;
  587. End;
  588. R_AL..R_BL:
  589. Begin
  590. NewRegsEncountered := NewRegsEncountered + [Reg8toReg32(NewReg)]
  591. + [Reg8toReg16(NewReg)];
  592. OldRegsEncountered := OldRegsEncountered + [Reg8toReg32(OldReg)]
  593. + [Reg8toReg16(OldReg)];
  594. New2OldReg[Reg8toReg32(NewReg)] := Reg8toReg32(OldReg);
  595. End;
  596. End;
  597. End;
  598. End;
  599. Procedure AddOp2RegInfo(const o:Toper; Var RegInfo: TRegInfo);
  600. Begin
  601. Case o.typ Of
  602. Top_Reg:
  603. If (o.reg <> R_NO) Then
  604. AddReg2RegInfo(o.reg, o.reg, RegInfo);
  605. Top_Ref:
  606. Begin
  607. If o.ref^.base <> R_NO Then
  608. AddReg2RegInfo(o.ref^.base, o.ref^.base, RegInfo);
  609. If o.ref^.index <> R_NO Then
  610. AddReg2RegInfo(o.ref^.index, o.ref^.index, RegInfo);
  611. End;
  612. End;
  613. End;
  614. Function RegsEquivalent(OldReg, NewReg: TRegister; Var RegInfo: TRegInfo; OPAct: TOpAction): Boolean;
  615. Begin
  616. If Not((OldReg = R_NO) Or (NewReg = R_NO)) Then
  617. If RegsSameSize(OldReg, NewReg) Then
  618. With RegInfo Do
  619. {here we always check for the 32 bit component, because it is possible that
  620. the 8 bit component has not been set, event though NewReg already has been
  621. processed. This happens if it has been compared with a register that doesn't
  622. have an 8 bit component (such as EDI). In that case the 8 bit component is
  623. still set to R_NO and the comparison in the Else-part will fail}
  624. If (Reg32(OldReg) in OldRegsEncountered) Then
  625. If (Reg32(NewReg) in NewRegsEncountered) Then
  626. RegsEquivalent := (OldReg = New2OldReg[NewReg])
  627. { If we haven't encountered the new register yet, but we have encountered the
  628. old one already, the new one can only be correct if it's being written to
  629. (and consequently the old one is also being written to), otherwise
  630. movl -8(%ebp), %eax and movl -8(%ebp), %eax
  631. movl (%eax), %eax movl (%edx), %edx
  632. are considered equivalent}
  633. Else
  634. If (OpAct = OpAct_Write) Then
  635. Begin
  636. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  637. RegsEquivalent := True
  638. End
  639. Else Regsequivalent := False
  640. Else
  641. If Not(Reg32(NewReg) in NewRegsEncountered) Then
  642. Begin
  643. AddReg2RegInfo(OldReg, NewReg, RegInfo);
  644. RegsEquivalent := True
  645. End
  646. Else RegsEquivalent := False
  647. Else RegsEquivalent := False
  648. Else RegsEquivalent := OldReg = NewReg
  649. End;
  650. Function RefsEquivalent(Const R1, R2: TReference; var RegInfo: TRegInfo; OpAct: TOpAction): Boolean;
  651. Begin
  652. If R1.is_immediate Then
  653. RefsEquivalent := R2.is_immediate and (R1.Offset = R2.Offset)
  654. Else
  655. RefsEquivalent := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  656. RegsEquivalent(R1.Base, R2.Base, RegInfo, OpAct) And
  657. RegsEquivalent(R1.Index, R2.Index, RegInfo, OpAct) And
  658. (R1.Segment = R2.Segment) And (R1.ScaleFactor = R2.ScaleFactor) And
  659. (R1.Symbol = R2.Symbol);
  660. End;
  661. Function RefsEqual(Const R1, R2: TReference): Boolean;
  662. Begin
  663. If R1.is_immediate Then
  664. RefsEqual := R2.is_immediate and (R1.Offset = R2.Offset)
  665. Else
  666. RefsEqual := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  667. (R1.Segment = R2.Segment) And (R1.Base = R2.Base) And
  668. (R1.Index = R2.Index) And (R1.ScaleFactor = R2.ScaleFactor) And
  669. (R1.Symbol=R2.Symbol);
  670. End;
  671. Function IsGP32Reg(Reg: TRegister): Boolean;
  672. {Checks if the register is a 32 bit general purpose register}
  673. Begin
  674. If (Reg >= R_EAX) and (Reg <= R_EBX)
  675. Then IsGP32Reg := True
  676. Else IsGP32reg := False
  677. End;
  678. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  679. Begin {checks whether Ref contains a reference to Reg}
  680. Reg := Reg32(Reg);
  681. RegInRef := (Ref.Base = Reg) Or (Ref.Index = Reg)
  682. End;
  683. function RegReadByInstruction(reg: TRegister; hp: pai): boolean;
  684. var p: paicpu;
  685. opCount: byte;
  686. begin
  687. RegReadByInstruction := false;
  688. reg := reg32(reg);
  689. p := paicpu(hp);
  690. if hp^.typ <> ait_instruction then
  691. exit;
  692. case p^.opcode of
  693. A_IMUL:
  694. case p^.ops of
  695. 1: regReadByInstruction := (reg = R_EAX) or reginOp(reg,p^.oper[0]);
  696. 2,3:
  697. regReadByInstruction := regInOp(reg,p^.oper[0]) or
  698. regInOp(reg,p^.oper[1]);
  699. end;
  700. A_IDIV,A_DIV,A_MUL:
  701. begin
  702. regReadByInstruction :=
  703. regInOp(reg,p^.oper[0]) or (reg = R_EAX);
  704. end;
  705. else
  706. begin
  707. for opCount := 0 to 2 do
  708. if (p^.oper[opCount].typ = top_ref) and
  709. RegInRef(reg,p^.oper[opCount].ref^) then
  710. begin
  711. RegReadByInstruction := true;
  712. exit
  713. end;
  714. for opCount := 1 to MaxCh do
  715. case InsProp[p^.opcode].Ch[opCount] of
  716. Ch_REAX..CH_REDI,CH_RWEAX..Ch_MEDI:
  717. if reg = TCh2Reg(InsProp[p^.opcode].Ch[opCount]) then
  718. begin
  719. RegReadByInstruction := true;
  720. exit
  721. end;
  722. Ch_RWOp1,Ch_ROp1{$ifdef arithopt},Ch_MOp1{$endif}:
  723. if (p^.oper[0].typ = top_reg) and
  724. (reg32(p^.oper[0].reg) = reg) then
  725. begin
  726. RegReadByInstruction := true;
  727. exit
  728. end;
  729. Ch_RWOp2,Ch_ROp2{$ifdef arithopt},Ch_MOp2{$endif}:
  730. if (p^.oper[1].typ = top_reg) and
  731. (reg32(p^.oper[1].reg) = reg) then
  732. begin
  733. RegReadByInstruction := true;
  734. exit
  735. end;
  736. Ch_RWOp3,Ch_ROp3{$ifdef arithopt},Ch_MOp3{$endif}:
  737. if (p^.oper[2].typ = top_reg) and
  738. (reg32(p^.oper[2].reg) = reg) then
  739. begin
  740. RegReadByInstruction := true;
  741. exit
  742. end;
  743. end;
  744. end;
  745. end;
  746. end;
  747. function regInInstruction(Reg: TRegister; p1: Pai): Boolean;
  748. { Checks if Reg is used by the instruction p1 }
  749. { Difference with "regReadBysinstruction() or regModifiedByInstruction()": }
  750. { this one ignores CH_ALL opcodes, while regModifiedByInstruction doesn't }
  751. var p: paicpu;
  752. opCount: byte;
  753. begin
  754. reg := reg32(reg);
  755. regInInstruction := false;
  756. p := paicpu(p1);
  757. if p1^.typ <> ait_instruction then
  758. exit;
  759. case p^.opcode of
  760. A_IMUL:
  761. case p^.ops of
  762. 1: regInInstruction := (reg = R_EAX) or reginOp(reg,p^.oper[0]);
  763. 2,3:
  764. regInInstruction := regInOp(reg,p^.oper[0]) or
  765. regInOp(reg,p^.oper[1]) or regInOp(reg,p^.oper[2]);
  766. end;
  767. A_IDIV,A_DIV,A_MUL:
  768. regInInstruction :=
  769. regInOp(reg,p^.oper[0]) or
  770. (reg = R_EAX) or (reg = R_EDX)
  771. else
  772. begin
  773. for opCount := 1 to MaxCh do
  774. case InsProp[p^.opcode].Ch[opCount] of
  775. CH_REAX..CH_MEDI:
  776. if tch2reg(InsProp[p^.opcode].Ch[opCount]) = reg then
  777. begin
  778. regInInstruction := true;
  779. exit;
  780. end;
  781. Ch_ROp1..Ch_MOp1:
  782. if regInOp(reg,p^.oper[0]) then
  783. begin
  784. regInInstruction := true;
  785. exit
  786. end;
  787. Ch_ROp2..Ch_MOp2:
  788. if regInOp(reg,p^.oper[1]) then
  789. begin
  790. regInInstruction := true;
  791. exit
  792. end;
  793. Ch_ROp3..Ch_MOp3:
  794. if regInOp(reg,p^.oper[2]) then
  795. begin
  796. regInInstruction := true;
  797. exit
  798. end;
  799. end;
  800. end;
  801. end;
  802. end;
  803. Function RegInOp(Reg: TRegister; const o:toper): Boolean;
  804. Begin
  805. RegInOp := False;
  806. reg := reg32(reg);
  807. Case o.typ Of
  808. top_reg: RegInOp := Reg = reg32(o.reg);
  809. top_ref: RegInOp := (Reg = o.ref^.Base) Or
  810. (Reg = o.ref^.Index);
  811. End;
  812. End;
  813. Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  814. Var InstrProp: TInsProp;
  815. TmpResult: Boolean;
  816. Cnt: Byte;
  817. Begin
  818. TmpResult := False;
  819. Reg := Reg32(Reg);
  820. If (p1^.typ = ait_instruction) Then
  821. Case paicpu(p1)^.opcode of
  822. A_IMUL:
  823. With paicpu(p1)^ Do
  824. TmpResult :=
  825. ((ops = 1) and (reg in [R_EAX,R_EDX])) or
  826. ((ops = 2) and (Reg32(oper[1].reg) = reg)) or
  827. ((ops = 3) and (Reg32(oper[2].reg) = reg));
  828. A_DIV, A_IDIV, A_MUL:
  829. With paicpu(p1)^ Do
  830. TmpResult :=
  831. (Reg = R_EAX) or
  832. (Reg = R_EDX);
  833. Else
  834. Begin
  835. Cnt := 1;
  836. InstrProp := InsProp[paicpu(p1)^.OpCode];
  837. While (Cnt <= MaxCh) And
  838. (InstrProp.Ch[Cnt] <> Ch_None) And
  839. Not(TmpResult) Do
  840. Begin
  841. Case InstrProp.Ch[Cnt] Of
  842. Ch_WEAX..Ch_MEDI:
  843. TmpResult := Reg = TCh2Reg(InstrProp.Ch[Cnt]);
  844. Ch_RWOp1,Ch_WOp1{$ifdef arithopt},Ch_Mop1{$endif arithopt}:
  845. TmpResult := (paicpu(p1)^.oper[0].typ = top_reg) and
  846. (Reg32(paicpu(p1)^.oper[0].reg) = reg);
  847. Ch_RWOp2,Ch_WOp2{$ifdef arithopt},Ch_Mop2{$endif arithopt}:
  848. TmpResult := (paicpu(p1)^.oper[1].typ = top_reg) and
  849. (Reg32(paicpu(p1)^.oper[1].reg) = reg);
  850. Ch_RWOp3,Ch_WOp3{$ifdef arithopt},Ch_Mop3{$endif arithopt}:
  851. TmpResult := (paicpu(p1)^.oper[2].typ = top_reg) and
  852. (Reg32(paicpu(p1)^.oper[2].reg) = reg);
  853. Ch_FPU: TmpResult := Reg in [R_ST..R_ST7,R_MM0..R_MM7];
  854. Ch_ALL: TmpResult := true;
  855. End;
  856. Inc(Cnt)
  857. End
  858. End
  859. End;
  860. RegModifiedByInstruction := TmpResult
  861. End;
  862. {********************* GetNext and GetLastInstruction *********************}
  863. Function GetNextInstruction(Current: Pai; Var Next: Pai): Boolean;
  864. { skips ait_regalloc, ait_regdealloc and ait_stab* objects and puts the }
  865. { next pai object in Next. Returns false if there isn't any }
  866. Begin
  867. Repeat
  868. If (Current^.typ = ait_marker) And
  869. (Pai_Marker(Current)^.Kind = AsmBlockStart) Then
  870. Begin
  871. GetNextInstruction := False;
  872. Next := Nil;
  873. Exit
  874. End;
  875. Current := Pai(Current^.Next);
  876. While Assigned(Current) And
  877. ((Current^.typ In SkipInstr) or
  878. ((Current^.typ = ait_label) And
  879. Not(Pai_Label(Current)^.l^.is_used))) Do
  880. Current := Pai(Current^.Next);
  881. If Assigned(Current) And
  882. (Current^.typ = ait_Marker) And
  883. (Pai_Marker(Current)^.Kind = NoPropInfoStart) Then
  884. Begin
  885. While Assigned(Current) And
  886. ((Current^.typ <> ait_Marker) Or
  887. (Pai_Marker(Current)^.Kind <> NoPropInfoEnd)) Do
  888. Current := Pai(Current^.Next);
  889. End;
  890. Until Not(Assigned(Current)) Or
  891. (Current^.typ <> ait_Marker) Or
  892. (Pai_Marker(Current)^.Kind <> NoPropInfoEnd);
  893. Next := Current;
  894. If Assigned(Current) And
  895. Not((Current^.typ In SkipInstr) or
  896. ((Current^.typ = ait_label) And
  897. Not(Pai_Label(Current)^.l^.is_used)))
  898. Then
  899. GetNextInstruction :=
  900. not((current^.typ = ait_marker) and
  901. (pai_marker(current)^.kind = asmBlockStart))
  902. Else
  903. Begin
  904. GetNextInstruction := False;
  905. Next := nil;
  906. End;
  907. End;
  908. Function GetLastInstruction(Current: Pai; Var Last: Pai): Boolean;
  909. {skips the ait-types in SkipInstr puts the previous pai object in
  910. Last. Returns false if there isn't any}
  911. Begin
  912. Repeat
  913. Current := Pai(Current^.previous);
  914. While Assigned(Current) And
  915. (((Current^.typ = ait_Marker) And
  916. Not(Pai_Marker(Current)^.Kind in [AsmBlockEnd,NoPropInfoEnd])) or
  917. (Current^.typ In SkipInstr) or
  918. ((Current^.typ = ait_label) And
  919. Not(Pai_Label(Current)^.l^.is_used))) Do
  920. Current := Pai(Current^.previous);
  921. If Assigned(Current) And
  922. (Current^.typ = ait_Marker) And
  923. (Pai_Marker(Current)^.Kind = NoPropInfoEnd) Then
  924. Begin
  925. While Assigned(Current) And
  926. ((Current^.typ <> ait_Marker) Or
  927. (Pai_Marker(Current)^.Kind <> NoPropInfoStart)) Do
  928. Current := Pai(Current^.previous);
  929. End;
  930. Until Not(Assigned(Current)) Or
  931. (Current^.typ <> ait_Marker) Or
  932. (Pai_Marker(Current)^.Kind <> NoPropInfoStart);
  933. If Not(Assigned(Current)) or
  934. (Current^.typ In SkipInstr) or
  935. ((Current^.typ = ait_label) And
  936. Not(Pai_Label(Current)^.l^.is_used)) or
  937. ((Current^.typ = ait_Marker) And
  938. (Pai_Marker(Current)^.Kind = AsmBlockEnd))
  939. Then
  940. Begin
  941. Last := nil;
  942. GetLastInstruction := False
  943. End
  944. Else
  945. Begin
  946. Last := Current;
  947. GetLastInstruction := True;
  948. End;
  949. End;
  950. Procedure SkipHead(var P: Pai);
  951. Var OldP: Pai;
  952. Begin
  953. Repeat
  954. OldP := P;
  955. If (P^.typ in SkipInstr) Or
  956. ((P^.typ = ait_marker) And
  957. (Pai_Marker(P)^.Kind = AsmBlockEnd)) Then
  958. GetNextInstruction(P, P)
  959. Else If ((P^.Typ = Ait_Marker) And
  960. (Pai_Marker(P)^.Kind = NoPropInfoStart)) Then
  961. {a marker of the NoPropInfoStart can't be the first instruction of a
  962. paasmoutput list}
  963. GetNextInstruction(Pai(P^.Previous),P);
  964. { If (P^.Typ = Ait_Marker) And
  965. (Pai_Marker(P)^.Kind = AsmBlockStart) Then
  966. Begin
  967. P := Pai(P^.Next);
  968. While (P^.typ <> Ait_Marker) Or
  969. (Pai_Marker(P)^.Kind <> AsmBlockEnd) Do
  970. P := Pai(P^.Next)
  971. End;}
  972. Until P = OldP
  973. End;
  974. {******************* The Data Flow Analyzer functions ********************}
  975. function regLoadedWithNewValue(reg: tregister; canDependOnPrevValue: boolean;
  976. hp: pai): boolean;
  977. { assumes reg is a 32bit register }
  978. var p: paicpu;
  979. begin
  980. p := paicpu(hp);
  981. regLoadedWithNewValue :=
  982. assigned(hp) and
  983. (hp^.typ = ait_instruction) and
  984. (((p^.opcode = A_MOV) or
  985. (p^.opcode = A_MOVZX) or
  986. (p^.opcode = A_MOVSX) or
  987. (p^.opcode = A_LEA)) and
  988. (p^.oper[1].typ = top_reg) and
  989. (Reg32(p^.oper[1].reg) = reg) and
  990. (canDependOnPrevValue or
  991. (p^.oper[0].typ <> top_ref) or
  992. not regInRef(reg,p^.oper[0].ref^)) or
  993. ((p^.opcode = A_POP) and
  994. (Reg32(p^.oper[0].reg) = reg)));
  995. end;
  996. Procedure UpdateUsedRegs(Var UsedRegs: TRegSet; p: Pai);
  997. {updates UsedRegs with the RegAlloc Information coming after P}
  998. Begin
  999. Repeat
  1000. While Assigned(p) And
  1001. ((p^.typ in (SkipInstr - [ait_RegAlloc])) or
  1002. ((p^.typ = ait_label) And
  1003. Not(Pai_Label(p)^.l^.is_used))) Do
  1004. p := Pai(p^.next);
  1005. While Assigned(p) And
  1006. (p^.typ=ait_RegAlloc) Do
  1007. Begin
  1008. if pairegalloc(p)^.allocation then
  1009. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  1010. else
  1011. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  1012. p := pai(p^.next);
  1013. End;
  1014. Until Not(Assigned(p)) Or
  1015. (Not(p^.typ in SkipInstr) And
  1016. Not((p^.typ = ait_label) And
  1017. Not(Pai_Label(p)^.l^.is_used)));
  1018. End;
  1019. Procedure AllocRegBetween(AsmL: PAasmOutput; Reg: TRegister; p1, p2: Pai);
  1020. { allocates register Reg between (and including) instructions p1 and p2 }
  1021. { the type of p1 and p2 must not be in SkipInstr }
  1022. var
  1023. hp: pai;
  1024. lastRemovedWasDealloc: boolean;
  1025. Begin
  1026. If not(reg in usableregs+[R_EDI,R_ESI]) or
  1027. not(assigned(p1)) Then
  1028. { this happens with registers which are loaded implicitely, outside the }
  1029. { current block (e.g. esi with self) }
  1030. exit;
  1031. lastRemovedWasDealloc := false;
  1032. {$ifdef allocregdebug}
  1033. hp := new(pai_asm_comment,init(strpnew('allocating '+att_reg2str[reg]+
  1034. ' from here...')));
  1035. insertllitem(asml,p1^.previous,p1,hp);
  1036. hp := new(pai_asm_comment,init(strpnew('allocated '+att_reg2str[reg]+
  1037. ' till here...')));
  1038. insertllitem(asml,p2,p1^.next,hp);
  1039. {$endif allocregdebug}
  1040. if Assigned(p1^.optInfo) and
  1041. not (reg in PPaiProp(p1^.OptInfo)^.UsedRegs) then
  1042. begin
  1043. hp := new(paiRegalloc,alloc(reg));
  1044. insertLLItem(asmL,p1^.previous,p1,hp);
  1045. end;
  1046. Repeat
  1047. If Assigned(p1^.OptInfo) Then
  1048. Include(PPaiProp(p1^.OptInfo)^.UsedRegs,Reg);
  1049. p1 := Pai(p1^.next);
  1050. Repeat
  1051. While assigned(p1) and
  1052. (p1^.typ in (SkipInstr-[ait_regalloc])) Do
  1053. p1 := Pai(p1^.next);
  1054. { remove all allocation/deallocation info about the register in between }
  1055. If assigned(p1) and
  1056. (p1^.typ = ait_regalloc) Then
  1057. If (PaiRegAlloc(p1)^.Reg = Reg) Then
  1058. Begin
  1059. lastRemovedWasDealloc := not PaiRegAlloc(p1)^.allocation;
  1060. hp := Pai(p1^.Next);
  1061. AsmL^.Remove(p1);
  1062. Dispose(p1, Done);
  1063. p1 := hp;
  1064. End
  1065. Else p1 := Pai(p1^.next);
  1066. Until not(assigned(p1)) or
  1067. Not(p1^.typ in SkipInstr);
  1068. Until not(assigned(p1)) or
  1069. (p1 = p2);
  1070. if assigned(p1) and lastRemovedWasDealloc then
  1071. begin
  1072. hp := new(paiRegalloc,dealloc(reg));
  1073. insertLLItem(asmL,p1,p1^.next,hp);
  1074. end;
  1075. End;
  1076. Procedure IncState(Var S: Byte);
  1077. {Increases S by 1, wraps around at $ffff to 0 (so we won't get overflow
  1078. errors}
  1079. Begin
  1080. If (s <> $ff)
  1081. Then Inc(s)
  1082. Else s := 0
  1083. End;
  1084. Function sequenceDependsonReg(Const Content: TContent; seqReg, Reg: TRegister): Boolean;
  1085. { Content is the sequence of instructions that describes the contents of }
  1086. { seqReg. Reg is being overwritten by the current instruction. If the }
  1087. { content of seqReg depends on reg (ie. because of a }
  1088. { "movl (seqreg,reg), seqReg" instruction), this function returns true }
  1089. Var p: Pai;
  1090. Counter: Byte;
  1091. TmpResult: Boolean;
  1092. RegsChecked: TRegSet;
  1093. Begin
  1094. RegsChecked := [];
  1095. p := Content.StartMod;
  1096. TmpResult := False;
  1097. Counter := 1;
  1098. While Not(TmpResult) And
  1099. (Counter <= Content.NrOfMods) Do
  1100. Begin
  1101. If (p^.typ = ait_instruction) and
  1102. ((Paicpu(p)^.opcode = A_MOV) or
  1103. (Paicpu(p)^.opcode = A_MOVZX) or
  1104. (Paicpu(p)^.opcode = A_MOVSX) or
  1105. (paicpu(p)^.opcode = A_LEA)) and
  1106. (Paicpu(p)^.oper[0].typ = top_ref) Then
  1107. With Paicpu(p)^.oper[0].ref^ Do
  1108. If ((Base = procinfo^.FramePointer) or
  1109. (assigned(symbol) and (base = R_NO))) And
  1110. (Index = R_NO) Then
  1111. Begin
  1112. RegsChecked := RegsChecked + [Reg32(Paicpu(p)^.oper[1].reg)];
  1113. If Reg = Reg32(Paicpu(p)^.oper[1].reg) Then
  1114. Break;
  1115. End
  1116. Else
  1117. tmpResult :=
  1118. regReadByInstruction(reg,p) and
  1119. regModifiedByInstruction(seqReg,p)
  1120. Else
  1121. tmpResult :=
  1122. regReadByInstruction(reg,p) and
  1123. regModifiedByInstruction(seqReg,p);
  1124. Inc(Counter);
  1125. GetNextInstruction(p,p)
  1126. End;
  1127. sequenceDependsonReg := TmpResult
  1128. End;
  1129. Procedure DestroyReg(p1: PPaiProp; Reg: TRegister; doIncState:Boolean);
  1130. {Destroys the contents of the register Reg in the PPaiProp p1, as well as the
  1131. contents of registers are loaded with a memory location based on Reg.
  1132. doIncState is false when this register has to be destroyed not because
  1133. it's contents are directly modified/overwritten, but because of an indirect
  1134. action (ie. this register holds the contents of a variable and the value
  1135. of the variable in memory is changed }
  1136. Var TmpWState, TmpRState: Byte;
  1137. Counter: TRegister;
  1138. Begin
  1139. Reg := Reg32(Reg);
  1140. { the following happens for fpu registers }
  1141. if (reg < low(NrOfInstrSinceLastMod)) or
  1142. (reg > high(NrOfInstrSinceLastMod)) then
  1143. exit;
  1144. NrOfInstrSinceLastMod[Reg] := 0;
  1145. If (Reg >= R_EAX) And (Reg <= R_EDI)
  1146. Then
  1147. Begin
  1148. With p1^.Regs[Reg] Do
  1149. Begin
  1150. if doIncState then
  1151. IncState(WState);
  1152. TmpWState := WState;
  1153. TmpRState := RState;
  1154. FillChar(p1^.Regs[Reg], SizeOf(TContent), 0);
  1155. WState := TmpWState;
  1156. RState := TmpRState;
  1157. End;
  1158. For counter := R_EAX to R_EDI Do
  1159. With p1^.Regs[counter] Do
  1160. If (Typ = Con_Ref) And
  1161. sequenceDependsOnReg(p1^.Regs[counter],counter,reg) Then
  1162. Begin
  1163. if doIncState then
  1164. IncState(WState);
  1165. TmpWState := WState;
  1166. TmpRState := RState;
  1167. FillChar(p1^.Regs[Counter], SizeOf(TContent), 0);
  1168. WState := TmpWState;
  1169. RState := TmpRState;
  1170. End;
  1171. End;
  1172. End;
  1173. {Procedure AddRegsToSet(p: Pai; Var RegSet: TRegSet);
  1174. Begin
  1175. If (p^.typ = ait_instruction) Then
  1176. Begin
  1177. Case Paicpu(p)^.oper[0].typ Of
  1178. top_reg:
  1179. If Not(Paicpu(p)^.oper[0].reg in [R_NO,R_ESP,procinfo^.FramePointer]) Then
  1180. RegSet := RegSet + [Paicpu(p)^.oper[0].reg];
  1181. top_ref:
  1182. With TReference(Paicpu(p)^.oper[0]^) Do
  1183. Begin
  1184. If Not(Base in [procinfo^.FramePointer,R_NO,R_ESP])
  1185. Then RegSet := RegSet + [Base];
  1186. If Not(Index in [procinfo^.FramePointer,R_NO,R_ESP])
  1187. Then RegSet := RegSet + [Index];
  1188. End;
  1189. End;
  1190. Case Paicpu(p)^.oper[1].typ Of
  1191. top_reg:
  1192. If Not(Paicpu(p)^.oper[1].reg in [R_NO,R_ESP,procinfo^.FramePointer]) Then
  1193. If RegSet := RegSet + [TRegister(TwoWords(Paicpu(p)^.oper[1]).Word1];
  1194. top_ref:
  1195. With TReference(Paicpu(p)^.oper[1]^) Do
  1196. Begin
  1197. If Not(Base in [procinfo^.FramePointer,R_NO,R_ESP])
  1198. Then RegSet := RegSet + [Base];
  1199. If Not(Index in [procinfo^.FramePointer,R_NO,R_ESP])
  1200. Then RegSet := RegSet + [Index];
  1201. End;
  1202. End;
  1203. End;
  1204. End;}
  1205. Function OpsEquivalent(const o1, o2: toper; Var RegInfo: TRegInfo; OpAct: TopAction): Boolean;
  1206. Begin {checks whether the two ops are equivalent}
  1207. OpsEquivalent := False;
  1208. if o1.typ=o2.typ then
  1209. Case o1.typ Of
  1210. Top_Reg:
  1211. OpsEquivalent :=RegsEquivalent(o1.reg,o2.reg, RegInfo, OpAct);
  1212. Top_Ref:
  1213. OpsEquivalent := RefsEquivalent(o1.ref^, o2.ref^, RegInfo, OpAct);
  1214. Top_Const:
  1215. OpsEquivalent := o1.val = o2.val;
  1216. Top_None:
  1217. OpsEquivalent := True
  1218. End;
  1219. End;
  1220. Function OpsEqual(const o1,o2:toper): Boolean;
  1221. Begin {checks whether the two ops are equal}
  1222. OpsEqual := False;
  1223. if o1.typ=o2.typ then
  1224. Case o1.typ Of
  1225. Top_Reg :
  1226. OpsEqual:=o1.reg=o2.reg;
  1227. Top_Ref :
  1228. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  1229. Top_Const :
  1230. OpsEqual:=o1.val=o2.val;
  1231. Top_Symbol :
  1232. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  1233. Top_None :
  1234. OpsEqual := True
  1235. End;
  1236. End;
  1237. Function InstructionsEquivalent(p1, p2: Pai; Var RegInfo: TRegInfo): Boolean;
  1238. var
  1239. hp: pai;
  1240. Begin {checks whether two Paicpu instructions are equal}
  1241. If Assigned(p1) And Assigned(p2) And
  1242. (Pai(p1)^.typ = ait_instruction) And
  1243. (Pai(p1)^.typ = ait_instruction) And
  1244. (Paicpu(p1)^.opcode = Paicpu(p2)^.opcode) And
  1245. (Paicpu(p1)^.oper[0].typ = Paicpu(p2)^.oper[0].typ) And
  1246. (Paicpu(p1)^.oper[1].typ = Paicpu(p2)^.oper[1].typ) And
  1247. (Paicpu(p1)^.oper[2].typ = Paicpu(p2)^.oper[2].typ)
  1248. Then
  1249. {both instructions have the same structure:
  1250. "<operator> <operand of type1>, <operand of type 2>"}
  1251. If ((Paicpu(p1)^.opcode = A_MOV) or
  1252. (Paicpu(p1)^.opcode = A_MOVZX) or
  1253. (Paicpu(p1)^.opcode = A_MOVSX)) And
  1254. (Paicpu(p1)^.oper[0].typ = top_ref) {then .oper[1]t = top_reg} Then
  1255. If Not(RegInRef(Paicpu(p1)^.oper[1].reg, Paicpu(p1)^.oper[0].ref^)) Then
  1256. {the "old" instruction is a load of a register with a new value, not with
  1257. a value based on the contents of this register (so no "mov (reg), reg")}
  1258. If Not(RegInRef(Paicpu(p2)^.oper[1].reg, Paicpu(p2)^.oper[0].ref^)) And
  1259. RefsEqual(Paicpu(p1)^.oper[0].ref^, Paicpu(p2)^.oper[0].ref^)
  1260. Then
  1261. {the "new" instruction is also a load of a register with a new value, and
  1262. this value is fetched from the same memory location}
  1263. Begin
  1264. With Paicpu(p2)^.oper[0].ref^ Do
  1265. Begin
  1266. If Not(Base in [procinfo^.FramePointer, R_NO, R_ESP]) Then
  1267. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1268. If Not(Index in [procinfo^.FramePointer, R_NO, R_ESP]) Then
  1269. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1270. End;
  1271. {add the registers from the reference (.oper[0]) to the RegInfo, all registers
  1272. from the reference are the same in the old and in the new instruction
  1273. sequence}
  1274. AddOp2RegInfo(Paicpu(p1)^.oper[0], RegInfo);
  1275. {the registers from .oper[1] have to be equivalent, but not necessarily equal}
  1276. InstructionsEquivalent :=
  1277. RegsEquivalent(Paicpu(p1)^.oper[1].reg, Paicpu(p2)^.oper[1].reg, RegInfo, OpAct_Write);
  1278. End
  1279. {the registers are loaded with values from different memory locations. If
  1280. this was allowed, the instructions "mov -4(esi),eax" and "mov -4(ebp),eax"
  1281. would be considered equivalent}
  1282. Else InstructionsEquivalent := False
  1283. Else
  1284. {load register with a value based on the current value of this register}
  1285. Begin
  1286. With Paicpu(p2)^.oper[0].ref^ Do
  1287. Begin
  1288. If Not(Base in [procinfo^.FramePointer,
  1289. Reg32(Paicpu(p2)^.oper[1].reg),R_NO,R_ESP]) Then
  1290. {it won't do any harm if the register is already in RegsLoadedForRef}
  1291. Begin
  1292. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base];
  1293. {$ifdef csdebug}
  1294. Writeln(att_reg2str[base], ' added');
  1295. {$endif csdebug}
  1296. end;
  1297. If Not(Index in [procinfo^.FramePointer,
  1298. Reg32(Paicpu(p2)^.oper[1].reg),R_NO,R_ESP]) Then
  1299. Begin
  1300. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Index];
  1301. {$ifdef csdebug}
  1302. Writeln(att_reg2str[index], ' added');
  1303. {$endif csdebug}
  1304. end;
  1305. End;
  1306. If Not(Reg32(Paicpu(p2)^.oper[1].reg) In [procinfo^.FramePointer,R_NO,R_ESP])
  1307. Then
  1308. Begin
  1309. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef -
  1310. [Reg32(Paicpu(p2)^.oper[1].reg)];
  1311. {$ifdef csdebug}
  1312. Writeln(att_reg2str[Reg32(Paicpu(p2)^.oper[1].reg)], ' removed');
  1313. {$endif csdebug}
  1314. end;
  1315. InstructionsEquivalent :=
  1316. OpsEquivalent(Paicpu(p1)^.oper[0], Paicpu(p2)^.oper[0], RegInfo, OpAct_Read) And
  1317. OpsEquivalent(Paicpu(p1)^.oper[1], Paicpu(p2)^.oper[1], RegInfo, OpAct_Write)
  1318. End
  1319. Else
  1320. {an instruction <> mov, movzx, movsx}
  1321. begin
  1322. {$ifdef csdebug}
  1323. hp := new(pai_asm_comment,init(strpnew('checking if equivalent')));
  1324. hp^.previous := p2;
  1325. hp^.next := p2^.next;
  1326. p2^.next^.previous := hp;
  1327. p2^.next := hp;
  1328. {$endif csdebug}
  1329. InstructionsEquivalent :=
  1330. OpsEquivalent(Paicpu(p1)^.oper[0], Paicpu(p2)^.oper[0], RegInfo, OpAct_Unknown) And
  1331. OpsEquivalent(Paicpu(p1)^.oper[1], Paicpu(p2)^.oper[1], RegInfo, OpAct_Unknown) And
  1332. OpsEquivalent(Paicpu(p1)^.oper[2], Paicpu(p2)^.oper[2], RegInfo, OpAct_Unknown)
  1333. end
  1334. {the instructions haven't even got the same structure, so they're certainly
  1335. not equivalent}
  1336. Else
  1337. begin
  1338. {$ifdef csdebug}
  1339. hp := new(pai_asm_comment,init(strpnew('different opcodes/format')));
  1340. hp^.previous := p2;
  1341. hp^.next := p2^.next;
  1342. p2^.next^.previous := hp;
  1343. p2^.next := hp;
  1344. {$endif csdebug}
  1345. InstructionsEquivalent := False;
  1346. end;
  1347. {$ifdef csdebug}
  1348. hp := new(pai_asm_comment,init(strpnew('instreq: '+tostr(byte(instructionsequivalent)))));
  1349. hp^.previous := p2;
  1350. hp^.next := p2^.next;
  1351. p2^.next^.previous := hp;
  1352. p2^.next := hp;
  1353. {$endif csdebug}
  1354. End;
  1355. (*
  1356. Function InstructionsEqual(p1, p2: Pai): Boolean;
  1357. Begin {checks whether two Paicpu instructions are equal}
  1358. InstructionsEqual :=
  1359. Assigned(p1) And Assigned(p2) And
  1360. ((Pai(p1)^.typ = ait_instruction) And
  1361. (Pai(p1)^.typ = ait_instruction) And
  1362. (Paicpu(p1)^.opcode = Paicpu(p2)^.opcode) And
  1363. (Paicpu(p1)^.oper[0].typ = Paicpu(p2)^.oper[0].typ) And
  1364. (Paicpu(p1)^.oper[1].typ = Paicpu(p2)^.oper[1].typ) And
  1365. OpsEqual(Paicpu(p1)^.oper[0].typ, Paicpu(p1)^.oper[0], Paicpu(p2)^.oper[0]) And
  1366. OpsEqual(Paicpu(p1)^.oper[1].typ, Paicpu(p1)^.oper[1], Paicpu(p2)^.oper[1]))
  1367. End;
  1368. *)
  1369. Procedure ReadReg(p: PPaiProp; Reg: TRegister);
  1370. Begin
  1371. Reg := Reg32(Reg);
  1372. If Reg in [R_EAX..R_EDI] Then
  1373. IncState(p^.Regs[Reg].RState)
  1374. End;
  1375. Procedure ReadRef(p: PPaiProp; Ref: PReference);
  1376. Begin
  1377. If Ref^.Base <> R_NO Then
  1378. ReadReg(p, Ref^.Base);
  1379. If Ref^.Index <> R_NO Then
  1380. ReadReg(p, Ref^.Index);
  1381. End;
  1382. Procedure ReadOp(P: PPaiProp;const o:toper);
  1383. Begin
  1384. Case o.typ Of
  1385. top_reg: ReadReg(P, o.reg);
  1386. top_ref: ReadRef(P, o.ref);
  1387. top_symbol : ;
  1388. End;
  1389. End;
  1390. Function RefInInstruction(Const Ref: TReference; p: Pai;
  1391. RefsEq: TRefCompare): Boolean;
  1392. {checks whehter Ref is used in P}
  1393. Var TmpResult: Boolean;
  1394. Begin
  1395. TmpResult := False;
  1396. If (p^.typ = ait_instruction) Then
  1397. Begin
  1398. If (Paicpu(p)^.oper[0].typ = Top_Ref) Then
  1399. TmpResult := RefsEq(Ref, Paicpu(p)^.oper[0].ref^);
  1400. If Not(TmpResult) And (Paicpu(p)^.oper[1].typ = Top_Ref) Then
  1401. TmpResult := RefsEq(Ref, Paicpu(p)^.oper[1].ref^);
  1402. If Not(TmpResult) And (Paicpu(p)^.oper[2].typ = Top_Ref) Then
  1403. TmpResult := RefsEq(Ref, Paicpu(p)^.oper[2].ref^);
  1404. End;
  1405. RefInInstruction := TmpResult;
  1406. End;
  1407. Function RefInSequence(Const Ref: TReference; Content: TContent;
  1408. RefsEq: TRefCompare): Boolean;
  1409. {checks the whole sequence of Content (so StartMod and and the next NrOfMods
  1410. Pai objects) to see whether Ref is used somewhere}
  1411. Var p: Pai;
  1412. Counter: Byte;
  1413. TmpResult: Boolean;
  1414. Begin
  1415. p := Content.StartMod;
  1416. TmpResult := False;
  1417. Counter := 1;
  1418. While Not(TmpResult) And
  1419. (Counter <= Content.NrOfMods) Do
  1420. Begin
  1421. If (p^.typ = ait_instruction) And
  1422. RefInInstruction(Ref, p, RefsEq)
  1423. Then TmpResult := True;
  1424. Inc(Counter);
  1425. GetNextInstruction(p,p)
  1426. End;
  1427. RefInSequence := TmpResult
  1428. End;
  1429. Function ArrayRefsEq(const r1, r2: TReference): Boolean;{$ifdef tp}far;{$endif}
  1430. Begin
  1431. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  1432. (R1.Segment = R2.Segment) And
  1433. (R1.Symbol=R2.Symbol) And
  1434. ((Assigned(R1.Symbol)) Or
  1435. (R1.Base = R2.Base))
  1436. End;
  1437. Procedure DestroyRefs(p: pai; Const Ref: TReference; WhichReg: TRegister);
  1438. {destroys all registers which possibly contain a reference to Ref, WhichReg
  1439. is the register whose contents are being written to memory (if this proc
  1440. is called because of a "mov?? %reg, (mem)" instruction)}
  1441. Var RefsEq: TRefCompare;
  1442. Counter: TRegister;
  1443. Begin
  1444. WhichReg := Reg32(WhichReg);
  1445. If (Ref.base = procinfo^.FramePointer) or
  1446. Assigned(Ref.Symbol) Then
  1447. Begin
  1448. If (Ref.Index = R_NO) And
  1449. (Not(Assigned(Ref.Symbol)) or
  1450. (Ref.base = R_NO)) Then
  1451. { local variable which is not an array }
  1452. RefsEq := {$ifdef fpc}@{$endif}RefsEqual
  1453. Else
  1454. { local variable which is an array }
  1455. RefsEq := {$ifdef fpc}@{$endif}ArrayRefsEq;
  1456. {write something to a parameter, a local or global variable, so
  1457. * with uncertain optimizations on:
  1458. - destroy the contents of registers whose contents have somewhere a
  1459. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  1460. are being written to memory) is not destroyed if it's StartMod is
  1461. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  1462. pointer based on Ref)
  1463. * with uncertain optimizations off:
  1464. - also destroy registers that contain any pointer}
  1465. For Counter := R_EAX to R_EDI Do
  1466. With PPaiProp(p^.OptInfo)^.Regs[Counter] Do
  1467. Begin
  1468. If (typ = Con_Ref) And
  1469. ((Not(cs_UncertainOpts in aktglobalswitches) And
  1470. (NrOfMods <> 1)
  1471. ) Or
  1472. (RefInSequence(Ref,PPaiProp(p^.OptInfo)^.Regs[Counter],RefsEq) And
  1473. ((Counter <> WhichReg) Or
  1474. ((NrOfMods <> 1) And
  1475. {StarMod is always of the type ait_instruction}
  1476. (Paicpu(StartMod)^.oper[0].typ = top_ref) And
  1477. RefsEq(Paicpu(StartMod)^.oper[0].ref^, Ref)
  1478. )
  1479. )
  1480. )
  1481. )
  1482. Then
  1483. DestroyReg(PPaiProp(p^.OptInfo), Counter, false)
  1484. End
  1485. End
  1486. Else
  1487. {write something to a pointer location, so
  1488. * with uncertain optimzations on:
  1489. - do not destroy registers which contain a local/global variable or a
  1490. parameter, except if DestroyRefs is called because of a "movsl"
  1491. * with uncertain optimzations off:
  1492. - destroy every register which contains a memory location
  1493. }
  1494. For Counter := R_EAX to R_EDI Do
  1495. With PPaiProp(p^.OptInfo)^.Regs[Counter] Do
  1496. If (typ = Con_Ref) And
  1497. (Not(cs_UncertainOpts in aktglobalswitches) Or
  1498. {for movsl}
  1499. (Ref.Base = R_EDI) Or
  1500. {don't destroy if reg contains a parameter, local or global variable}
  1501. Not((NrOfMods = 1) And
  1502. (Paicpu(StartMod)^.oper[0].typ = top_ref) And
  1503. ((Paicpu(StartMod)^.oper[0].ref^.base = procinfo^.FramePointer) Or
  1504. Assigned(Paicpu(StartMod)^.oper[0].ref^.Symbol)
  1505. )
  1506. )
  1507. )
  1508. Then DestroyReg(PPaiProp(p^.OptInfo), Counter, false)
  1509. End;
  1510. Procedure DestroyAllRegs(p: PPaiProp);
  1511. Var Counter: TRegister;
  1512. Begin {initializes/desrtoys all registers}
  1513. For Counter := R_EAX To R_EDI Do
  1514. Begin
  1515. ReadReg(p, Counter);
  1516. DestroyReg(p, Counter, true);
  1517. End;
  1518. p^.DirFlag := F_Unknown;
  1519. End;
  1520. Procedure DestroyOp(PaiObj: Pai; const o:Toper);
  1521. {$ifdef statedebug}
  1522. var hp: pai;
  1523. {$endif statedebug}
  1524. Begin
  1525. Case o.typ Of
  1526. top_reg:
  1527. begin
  1528. {$ifdef statedebug}
  1529. hp := new(pai_asm_comment,init(strpnew('destroying '+att_reg2str[o.reg])));
  1530. hp^.next := paiobj^.next;
  1531. hp^.previous := paiobj;
  1532. paiobj^.next := hp;
  1533. if assigned(hp^.next) then
  1534. hp^.next^.previous := hp;
  1535. {$endif statedebug}
  1536. DestroyReg(PPaiProp(PaiObj^.OptInfo), reg32(o.reg), true);
  1537. end;
  1538. top_ref:
  1539. Begin
  1540. ReadRef(PPaiProp(PaiObj^.OptInfo), o.ref);
  1541. DestroyRefs(PaiObj, o.ref^, R_NO);
  1542. End;
  1543. top_symbol:;
  1544. End;
  1545. End;
  1546. Function DFAPass1(AsmL: PAasmOutput; BlockStart: Pai): Pai;
  1547. {gathers the RegAlloc data... still need to think about where to store it to
  1548. avoid global vars}
  1549. Var BlockEnd: Pai;
  1550. Begin
  1551. BlockEnd := FindLoHiLabels(LoLab, HiLab, LabDif, BlockStart);
  1552. BuildLabelTableAndFixRegAlloc(AsmL, LTable, LoLab, LabDif, BlockStart, BlockEnd);
  1553. DFAPass1 := BlockEnd;
  1554. End;
  1555. {$ifdef arithopt}
  1556. Procedure AddInstr2RegContents({$ifdef statedebug} asml: paasmoutput; {$endif}
  1557. p: paicpu; reg: TRegister);
  1558. {$ifdef statedebug}
  1559. var hp: pai;
  1560. {$endif statedebug}
  1561. Begin
  1562. Reg := Reg32(Reg);
  1563. With PPaiProp(p^.optinfo)^.Regs[reg] Do
  1564. If (Typ = Con_Ref)
  1565. Then
  1566. Begin
  1567. IncState(WState);
  1568. {also store how many instructions are part of the sequence in the first
  1569. instructions PPaiProp, so it can be easily accessed from within
  1570. CheckSequence}
  1571. Inc(NrOfMods, NrOfInstrSinceLastMod[Reg]);
  1572. PPaiProp(Pai(StartMod)^.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  1573. NrOfInstrSinceLastMod[Reg] := 0;
  1574. {$ifdef StateDebug}
  1575. hp := new(pai_asm_comment,init(strpnew(att_reg2str[reg]+': '+tostr(PPaiProp(p^.optinfo)^.Regs[reg].WState)
  1576. + ' -- ' + tostr(PPaiProp(p^.optinfo)^.Regs[reg].nrofmods))));
  1577. InsertLLItem(AsmL, p, p^.next, hp);
  1578. {$endif StateDebug}
  1579. End
  1580. Else
  1581. Begin
  1582. {$ifdef statedebug}
  1583. hp := new(pai_asm_comment,init(strpnew('destroying '+att_reg2str[reg])));
  1584. insertllitem(asml,p,p^.next,hp);
  1585. {$endif statedebug}
  1586. DestroyReg(PPaiProp(p^.optinfo), Reg, true);
  1587. {$ifdef StateDebug}
  1588. hp := new(pai_asm_comment,init(strpnew(att_reg2str[reg]+': '+tostr(PPaiProp(p^.optinfo)^.Regs[reg].WState))));
  1589. InsertLLItem(AsmL, p, p^.next, hp);
  1590. {$endif StateDebug}
  1591. End
  1592. End;
  1593. Procedure AddInstr2OpContents({$ifdef statedebug} asml: paasmoutput; {$endif}
  1594. p: paicpu; const oper: TOper);
  1595. Begin
  1596. If oper.typ = top_reg Then
  1597. AddInstr2RegContents({$ifdef statedebug} asml, {$endif}p, oper.reg)
  1598. Else
  1599. Begin
  1600. ReadOp(PPaiProp(p^.optinfo), oper);
  1601. DestroyOp(p, oper);
  1602. End
  1603. End;
  1604. {$endif arithopt}
  1605. Procedure DoDFAPass2(
  1606. {$Ifdef StateDebug}
  1607. AsmL: PAasmOutput;
  1608. {$endif statedebug}
  1609. BlockStart, BlockEnd: Pai);
  1610. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  1611. contents for the instructions starting with p. Returns the last pai which has
  1612. been processed}
  1613. Var
  1614. CurProp: PPaiProp;
  1615. {$ifdef AnalyzeLoops}
  1616. TmpState: Byte;
  1617. {$endif AnalyzeLoops}
  1618. Cnt, InstrCnt : Longint;
  1619. InstrProp: TInsProp;
  1620. UsedRegs: TRegSet;
  1621. p, hp : Pai;
  1622. TmpRef: TReference;
  1623. TmpReg: TRegister;
  1624. Begin
  1625. p := BlockStart;
  1626. UsedRegs := [];
  1627. UpdateUsedregs(UsedRegs, p);
  1628. SkipHead(P);
  1629. BlockStart := p;
  1630. InstrCnt := 1;
  1631. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  1632. While (P <> BlockEnd) Do
  1633. Begin
  1634. {$IfDef TP}
  1635. New(CurProp);
  1636. {$Else TP}
  1637. CurProp := @PaiPropBlock^[InstrCnt];
  1638. {$EndIf TP}
  1639. If (p <> BlockStart)
  1640. Then
  1641. Begin
  1642. {$ifdef JumpAnal}
  1643. If (p^.Typ <> ait_label) Then
  1644. {$endif JumpAnal}
  1645. Begin
  1646. GetLastInstruction(p, hp);
  1647. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1648. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1649. End
  1650. End
  1651. Else
  1652. Begin
  1653. FillChar(CurProp^, SizeOf(CurProp^), 0);
  1654. { For TmpReg := R_EAX to R_EDI Do
  1655. CurProp^.Regs[TmpReg].WState := 1;}
  1656. End;
  1657. CurProp^.UsedRegs := UsedRegs;
  1658. CurProp^.CanBeRemoved := False;
  1659. UpdateUsedRegs(UsedRegs, Pai(p^.Next));
  1660. {$ifdef TP}
  1661. PPaiProp(p^.OptInfo) := CurProp;
  1662. {$Endif TP}
  1663. For TmpReg := R_EAX To R_EDI Do
  1664. Inc(NrOfInstrSinceLastMod[TmpReg]);
  1665. Case p^.typ Of
  1666. ait_label:
  1667. {$Ifndef JumpAnal}
  1668. If (Pai_label(p)^.l^.is_used) Then
  1669. DestroyAllRegs(CurProp);
  1670. {$Else JumpAnal}
  1671. Begin
  1672. If (Pai_Label(p)^.is_used) Then
  1673. With LTable^[Pai_Label(p)^.l^.labelnr-LoLab] Do
  1674. {$IfDef AnalyzeLoops}
  1675. If (RefsFound = Pai_Label(p)^.l^.RefCount)
  1676. {$Else AnalyzeLoops}
  1677. If (JmpsProcessed = Pai_Label(p)^.l^.RefCount)
  1678. {$EndIf AnalyzeLoops}
  1679. Then
  1680. {all jumps to this label have been found}
  1681. {$IfDef AnalyzeLoops}
  1682. If (JmpsProcessed > 0)
  1683. Then
  1684. {$EndIf AnalyzeLoops}
  1685. {we've processed at least one jump to this label}
  1686. Begin
  1687. If (GetLastInstruction(p, hp) And
  1688. Not(((hp^.typ = ait_instruction)) And
  1689. (paicpu_labeled(hp)^.is_jmp))
  1690. Then
  1691. {previous instruction not a JMP -> the contents of the registers after the
  1692. previous intruction has been executed have to be taken into account as well}
  1693. For TmpReg := R_EAX to R_EDI Do
  1694. Begin
  1695. If (CurProp^.Regs[TmpReg].WState <>
  1696. PPaiProp(hp^.OptInfo)^.Regs[TmpReg].WState)
  1697. Then DestroyReg(CurProp, TmpReg, true)
  1698. End
  1699. End
  1700. {$IfDef AnalyzeLoops}
  1701. Else
  1702. {a label from a backward jump (e.g. a loop), no jump to this label has
  1703. already been processed}
  1704. If GetLastInstruction(p, hp) And
  1705. Not(hp^.typ = ait_instruction) And
  1706. (paicpu_labeled(hp)^.opcode = A_JMP))
  1707. Then
  1708. {previous instruction not a jmp, so keep all the registers' contents from the
  1709. previous instruction}
  1710. Begin
  1711. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1712. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1713. End
  1714. Else
  1715. {previous instruction a jmp and no jump to this label processed yet}
  1716. Begin
  1717. hp := p;
  1718. Cnt := InstrCnt;
  1719. {continue until we find a jump to the label or a label which has already
  1720. been processed}
  1721. While GetNextInstruction(hp, hp) And
  1722. Not((hp^.typ = ait_instruction) And
  1723. (paicpu(hp)^.is_jmp) and
  1724. (pasmlabel(paicpu(hp)^.oper[0].sym)^.labelnr = Pai_Label(p)^.l^.labelnr)) And
  1725. Not((hp^.typ = ait_label) And
  1726. (LTable^[Pai_Label(hp)^.l^.labelnr-LoLab].RefsFound
  1727. = Pai_Label(hp)^.l^.RefCount) And
  1728. (LTable^[Pai_Label(hp)^.l^.labelnr-LoLab].JmpsProcessed > 0)) Do
  1729. Inc(Cnt);
  1730. If (hp^.typ = ait_label)
  1731. Then
  1732. {there's a processed label after the current one}
  1733. Begin
  1734. CurProp^.Regs := PaiPropBlock^[Cnt].Regs;
  1735. CurProp^.DirFlag := PaiPropBlock^[Cnt].DirFlag;
  1736. End
  1737. Else
  1738. {there's no label anymore after the current one, or they haven't been
  1739. processed yet}
  1740. Begin
  1741. GetLastInstruction(p, hp);
  1742. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1743. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1744. DestroyAllRegs(PPaiProp(hp^.OptInfo))
  1745. End
  1746. End
  1747. {$EndIf AnalyzeLoops}
  1748. Else
  1749. {not all references to this label have been found, so destroy all registers}
  1750. Begin
  1751. GetLastInstruction(p, hp);
  1752. CurProp^.Regs := PPaiProp(hp^.OptInfo)^.Regs;
  1753. CurProp^.DirFlag := PPaiProp(hp^.OptInfo)^.DirFlag;
  1754. DestroyAllRegs(CurProp)
  1755. End;
  1756. End;
  1757. {$EndIf JumpAnal}
  1758. {$ifdef GDB}
  1759. ait_stabs, ait_stabn, ait_stab_function_name:;
  1760. {$endif GDB}
  1761. ait_align: ; { may destroy flags !!! }
  1762. ait_instruction:
  1763. Begin
  1764. if paicpu(p)^.is_jmp then
  1765. begin
  1766. {$IfNDef JumpAnal}
  1767. ;
  1768. {$Else JumpAnal}
  1769. With LTable^[pasmlabel(paicpu(p)^.oper[0].sym)^.labelnr-LoLab] Do
  1770. If (RefsFound = pasmlabel(paicpu(p)^.oper[0].sym)^.RefCount) Then
  1771. Begin
  1772. If (InstrCnt < InstrNr)
  1773. Then
  1774. {forward jump}
  1775. If (JmpsProcessed = 0) Then
  1776. {no jump to this label has been processed yet}
  1777. Begin
  1778. PaiPropBlock^[InstrNr].Regs := CurProp^.Regs;
  1779. PaiPropBlock^[InstrNr].DirFlag := CurProp^.DirFlag;
  1780. Inc(JmpsProcessed);
  1781. End
  1782. Else
  1783. Begin
  1784. For TmpReg := R_EAX to R_EDI Do
  1785. If (PaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  1786. CurProp^.Regs[TmpReg].WState) Then
  1787. DestroyReg(@PaiPropBlock^[InstrNr], TmpReg, true);
  1788. Inc(JmpsProcessed);
  1789. End
  1790. {$ifdef AnalyzeLoops}
  1791. Else
  1792. { backward jump, a loop for example}
  1793. { If (JmpsProcessed > 0) Or
  1794. Not(GetLastInstruction(PaiObj, hp) And
  1795. (hp^.typ = ait_labeled_instruction) And
  1796. (paicpu_labeled(hp)^.opcode = A_JMP))
  1797. Then}
  1798. {instruction prior to label is not a jmp, or at least one jump to the label
  1799. has yet been processed}
  1800. Begin
  1801. Inc(JmpsProcessed);
  1802. For TmpReg := R_EAX to R_EDI Do
  1803. If (PaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  1804. CurProp^.Regs[TmpReg].WState)
  1805. Then
  1806. Begin
  1807. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1808. Cnt := InstrNr;
  1809. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1810. Begin
  1811. DestroyReg(@PaiPropBlock^[Cnt], TmpReg, true);
  1812. Inc(Cnt);
  1813. End;
  1814. While (Cnt <= InstrCnt) Do
  1815. Begin
  1816. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].WState);
  1817. Inc(Cnt)
  1818. End
  1819. End;
  1820. End
  1821. { Else }
  1822. {instruction prior to label is a jmp and no jumps to the label have yet been
  1823. processed}
  1824. { Begin
  1825. Inc(JmpsProcessed);
  1826. For TmpReg := R_EAX to R_EDI Do
  1827. Begin
  1828. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1829. Cnt := InstrNr;
  1830. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1831. Begin
  1832. PaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.Regs[TmpReg];
  1833. Inc(Cnt);
  1834. End;
  1835. TmpState := PaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  1836. While (TmpState = PaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  1837. Begin
  1838. DestroyReg(@PaiPropBlock^[Cnt], TmpReg, true);
  1839. Inc(Cnt);
  1840. End;
  1841. While (Cnt <= InstrCnt) Do
  1842. Begin
  1843. Inc(PaiPropBlock^[Cnt].Regs[TmpReg].WState);
  1844. Inc(Cnt)
  1845. End
  1846. End
  1847. End}
  1848. {$endif AnalyzeLoops}
  1849. End;
  1850. {$EndIf JumpAnal}
  1851. end
  1852. else
  1853. begin
  1854. InstrProp := InsProp[Paicpu(p)^.opcode];
  1855. Case Paicpu(p)^.opcode Of
  1856. A_MOV, A_MOVZX, A_MOVSX:
  1857. Begin
  1858. Case Paicpu(p)^.oper[0].typ Of
  1859. Top_Reg:
  1860. Case Paicpu(p)^.oper[1].typ Of
  1861. Top_Reg:
  1862. Begin
  1863. {$ifdef statedebug}
  1864. hp := new(pai_asm_comment,init(strpnew('destroying '+
  1865. att_reg2str[Paicpu(p)^.oper[1].reg])));
  1866. insertllitem(asml,p,p^.next,hp);
  1867. {$endif statedebug}
  1868. DestroyReg(CurProp, Paicpu(p)^.oper[1].reg, true);
  1869. ReadReg(CurProp, Paicpu(p)^.oper[0].reg);
  1870. { CurProp^.Regs[Paicpu(p)^.oper[1].reg] :=
  1871. CurProp^.Regs[Paicpu(p)^.oper[0].reg];
  1872. If (CurProp^.Regs[Paicpu(p)^.oper[1].reg].ModReg = R_NO) Then
  1873. CurProp^.Regs[Paicpu(p)^.oper[1].reg].ModReg :=
  1874. Paicpu(p)^.oper[0].reg;}
  1875. End;
  1876. Top_Ref:
  1877. Begin
  1878. ReadReg(CurProp, Paicpu(p)^.oper[0].reg);
  1879. ReadRef(CurProp, Paicpu(p)^.oper[1].ref);
  1880. DestroyRefs(p, Paicpu(p)^.oper[1].ref^, Paicpu(p)^.oper[0].reg);
  1881. End;
  1882. End;
  1883. Top_Ref:
  1884. Begin {destination is always a register in this case}
  1885. ReadRef(CurProp, Paicpu(p)^.oper[0].ref);
  1886. TmpReg := Reg32(Paicpu(p)^.oper[1].reg);
  1887. If RegInRef(TmpReg, Paicpu(p)^.oper[0].ref^) And
  1888. (CurProp^.Regs[TmpReg].Typ = Con_Ref)
  1889. Then
  1890. Begin
  1891. With CurProp^.Regs[TmpReg] Do
  1892. Begin
  1893. IncState(WState);
  1894. {also store how many instructions are part of the sequence in the first
  1895. instructions PPaiProp, so it can be easily accessed from within
  1896. CheckSequence}
  1897. Inc(NrOfMods, NrOfInstrSinceLastMod[TmpReg]);
  1898. PPaiProp(Pai(StartMod)^.OptInfo)^.Regs[TmpReg].NrOfMods := NrOfMods;
  1899. NrOfInstrSinceLastMod[TmpReg] := 0;
  1900. End;
  1901. End
  1902. Else
  1903. Begin
  1904. {$ifdef statedebug}
  1905. hp := new(pai_asm_comment,init(strpnew('destroying & initing '+att_reg2str[tmpreg])));
  1906. insertllitem(asml,p,p^.next,hp);
  1907. {$endif statedebug}
  1908. DestroyReg(CurProp, TmpReg, true);
  1909. If Not(RegInRef(TmpReg, Paicpu(p)^.oper[0].ref^)) Then
  1910. With CurProp^.Regs[TmpReg] Do
  1911. Begin
  1912. Typ := Con_Ref;
  1913. StartMod := p;
  1914. NrOfMods := 1;
  1915. End
  1916. End;
  1917. {$ifdef StateDebug}
  1918. hp := new(pai_asm_comment,init(strpnew(att_reg2str[TmpReg]+': '+tostr(CurProp^.Regs[TmpReg].WState))));
  1919. InsertLLItem(AsmL, p, p^.next, hp);
  1920. {$endif StateDebug}
  1921. End;
  1922. top_symbol,Top_Const:
  1923. Begin
  1924. Case Paicpu(p)^.oper[1].typ Of
  1925. Top_Reg:
  1926. Begin
  1927. TmpReg := Reg32(Paicpu(p)^.oper[1].reg);
  1928. {$ifdef statedebug}
  1929. hp := new(pai_asm_comment,init(strpnew('destroying '+att_reg2str[tmpreg])));
  1930. insertllitem(asml,p,p^.next,hp);
  1931. {$endif statedebug}
  1932. With CurProp^.Regs[TmpReg] Do
  1933. Begin
  1934. DestroyReg(CurProp, TmpReg, true);
  1935. typ := Con_Const;
  1936. StartMod := p;
  1937. End
  1938. End;
  1939. Top_Ref:
  1940. Begin
  1941. ReadRef(CurProp, Paicpu(p)^.oper[1].ref);
  1942. DestroyRefs(P, Paicpu(p)^.oper[1].ref^, R_NO);
  1943. End;
  1944. End;
  1945. End;
  1946. End;
  1947. End;
  1948. A_DIV, A_IDIV, A_MUL:
  1949. Begin
  1950. ReadOp(Curprop, Paicpu(p)^.oper[0]);
  1951. ReadReg(CurProp,R_EAX);
  1952. If (Paicpu(p)^.OpCode = A_IDIV) or
  1953. (Paicpu(p)^.OpCode = A_DIV) Then
  1954. ReadReg(CurProp,R_EDX);
  1955. {$ifdef statedebug}
  1956. hp := new(pai_asm_comment,init(strpnew('destroying eax and edx')));
  1957. insertllitem(asml,p,p^.next,hp);
  1958. {$endif statedebug}
  1959. DestroyReg(CurProp, R_EAX, true);
  1960. DestroyReg(CurProp, R_EDX, true)
  1961. End;
  1962. A_IMUL:
  1963. Begin
  1964. ReadOp(CurProp,Paicpu(p)^.oper[0]);
  1965. ReadOp(CurProp,Paicpu(p)^.oper[1]);
  1966. If (Paicpu(p)^.oper[2].typ = top_none) Then
  1967. If (Paicpu(p)^.oper[1].typ = top_none) Then
  1968. Begin
  1969. ReadReg(CurProp,R_EAX);
  1970. {$ifdef statedebug}
  1971. hp := new(pai_asm_comment,init(strpnew('destroying eax and edx')));
  1972. insertllitem(asml,p,p^.next,hp);
  1973. {$endif statedebug}
  1974. DestroyReg(CurProp, R_EAX, true);
  1975. DestroyReg(CurProp, R_EDX, true)
  1976. End
  1977. Else
  1978. {$ifdef arithopt}
  1979. AddInstr2OpContents(
  1980. {$ifdef statedebug}asml,{$endif}
  1981. Paicpu(p), Paicpu(p)^.oper[1])
  1982. {$else arithopt}
  1983. DestroyOp(p, Paicpu(p)^.oper[1])
  1984. {$endif arithopt}
  1985. Else
  1986. {$ifdef arithopt}
  1987. AddInstr2OpContents({$ifdef statedebug}asml,{$endif}
  1988. Paicpu(p), Paicpu(p)^.oper[2]);
  1989. {$else arithopt}
  1990. DestroyOp(p, Paicpu(p)^.oper[2]);
  1991. {$endif arithopt}
  1992. End;
  1993. {$ifdef arithopt}
  1994. A_LEA:
  1995. begin
  1996. readop(curprop,paicpu(p)^.oper[0]);
  1997. if reginref(paicpu(p)^.oper[1].reg,paicpu(p)^.oper[0].ref^) then
  1998. AddInstr2RegContents({$ifdef statedebug}asml,{$endif}
  1999. paicpu(p), paicpu(p)^.oper[1].reg)
  2000. else
  2001. begin
  2002. {$ifdef statedebug}
  2003. hp := new(pai_asm_comment,init(strpnew('destroying '+
  2004. att_reg2str[paicpu(p)^.oper[1].reg])));
  2005. insertllitem(asml,p,p^.next,hp);
  2006. {$endif statedebug}
  2007. destroyreg(curprop,paicpu(p)^.oper[1].reg,true);
  2008. end;
  2009. end;
  2010. {$endif arithopt}
  2011. Else
  2012. Begin
  2013. Cnt := 1;
  2014. While (Cnt <= MaxCh) And
  2015. (InstrProp.Ch[Cnt] <> Ch_None) Do
  2016. Begin
  2017. Case InstrProp.Ch[Cnt] Of
  2018. Ch_REAX..Ch_REDI: ReadReg(CurProp,TCh2Reg(InstrProp.Ch[Cnt]));
  2019. Ch_WEAX..Ch_RWEDI:
  2020. Begin
  2021. If (InstrProp.Ch[Cnt] >= Ch_RWEAX) Then
  2022. ReadReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]));
  2023. {$ifdef statedebug}
  2024. hp := new(pai_asm_comment,init(strpnew('destroying '+
  2025. att_reg2str[TCh2Reg(InstrProp.Ch[Cnt])])));
  2026. insertllitem(asml,p,p^.next,hp);
  2027. {$endif statedebug}
  2028. DestroyReg(CurProp, TCh2Reg(InstrProp.Ch[Cnt]), true);
  2029. End;
  2030. {$ifdef arithopt}
  2031. Ch_MEAX..Ch_MEDI:
  2032. AddInstr2RegContents({$ifdef statedebug} asml,{$endif}
  2033. Paicpu(p),TCh2Reg(InstrProp.Ch[Cnt]));
  2034. {$endif arithopt}
  2035. Ch_CDirFlag: CurProp^.DirFlag := F_NotSet;
  2036. Ch_SDirFlag: CurProp^.DirFlag := F_Set;
  2037. Ch_Rop1: ReadOp(CurProp, Paicpu(p)^.oper[0]);
  2038. Ch_Rop2: ReadOp(CurProp, Paicpu(p)^.oper[1]);
  2039. Ch_ROp3: ReadOp(CurProp, Paicpu(p)^.oper[2]);
  2040. Ch_Wop1..Ch_RWop1:
  2041. Begin
  2042. If (InstrProp.Ch[Cnt] in [Ch_RWop1]) Then
  2043. ReadOp(CurProp, Paicpu(p)^.oper[0]);
  2044. DestroyOp(p, Paicpu(p)^.oper[0]);
  2045. End;
  2046. {$ifdef arithopt}
  2047. Ch_Mop1:
  2048. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2049. Paicpu(p), Paicpu(p)^.oper[0]);
  2050. {$endif arithopt}
  2051. Ch_Wop2..Ch_RWop2:
  2052. Begin
  2053. If (InstrProp.Ch[Cnt] = Ch_RWop2) Then
  2054. ReadOp(CurProp, Paicpu(p)^.oper[1]);
  2055. DestroyOp(p, Paicpu(p)^.oper[1]);
  2056. End;
  2057. {$ifdef arithopt}
  2058. Ch_Mop2:
  2059. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2060. Paicpu(p), Paicpu(p)^.oper[1]);
  2061. {$endif arithopt}
  2062. Ch_WOp3..Ch_RWOp3:
  2063. Begin
  2064. If (InstrProp.Ch[Cnt] = Ch_RWOp3) Then
  2065. ReadOp(CurProp, Paicpu(p)^.oper[2]);
  2066. DestroyOp(p, Paicpu(p)^.oper[2]);
  2067. End;
  2068. {$ifdef arithopt}
  2069. Ch_Mop3:
  2070. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2071. Paicpu(p), Paicpu(p)^.oper[2]);
  2072. {$endif arithopt}
  2073. Ch_WMemEDI:
  2074. Begin
  2075. ReadReg(CurProp, R_EDI);
  2076. FillChar(TmpRef, SizeOf(TmpRef), 0);
  2077. TmpRef.Base := R_EDI;
  2078. DestroyRefs(p, TmpRef, R_NO)
  2079. End;
  2080. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU:
  2081. Else
  2082. Begin
  2083. {$ifdef statedebug}
  2084. hp := new(pai_asm_comment,init(strpnew(
  2085. 'destroying all regs for prev instruction')));
  2086. insertllitem(asml,p, p^.next,hp);
  2087. {$endif statedebug}
  2088. DestroyAllRegs(CurProp);
  2089. End;
  2090. End;
  2091. Inc(Cnt);
  2092. End
  2093. End;
  2094. end;
  2095. End;
  2096. End
  2097. Else
  2098. Begin
  2099. {$ifdef statedebug}
  2100. hp := new(pai_asm_comment,init(strpnew(
  2101. 'destroying all regs: unknown pai: '+tostr(ord(p^.typ)))));
  2102. insertllitem(asml,p, p^.next,hp);
  2103. {$endif statedebug}
  2104. DestroyAllRegs(CurProp);
  2105. End;
  2106. End;
  2107. Inc(InstrCnt);
  2108. GetNextInstruction(p, p);
  2109. End;
  2110. End;
  2111. Function InitDFAPass2(BlockStart, BlockEnd: Pai): Boolean;
  2112. {reserves memory for the PPaiProps in one big memory block when not using
  2113. TP, returns False if not enough memory is available for the optimizer in all
  2114. cases}
  2115. Var p: Pai;
  2116. Count: Longint;
  2117. { TmpStr: String; }
  2118. Begin
  2119. P := BlockStart;
  2120. SkipHead(P);
  2121. NrOfPaiObjs := 0;
  2122. While (P <> BlockEnd) Do
  2123. Begin
  2124. {$IfDef JumpAnal}
  2125. Case P^.Typ Of
  2126. ait_label:
  2127. Begin
  2128. If (Pai_Label(p)^.l^.is_used) Then
  2129. LTable^[Pai_Label(P)^.l^.labelnr-LoLab].InstrNr := NrOfPaiObjs
  2130. End;
  2131. ait_instruction:
  2132. begin
  2133. if paicpu(p)^.is_jmp then
  2134. begin
  2135. If (pasmlabel(paicpu(P)^.oper[0].sym)^.labelnr >= LoLab) And
  2136. (pasmlabel(paicpu(P)^.oper[0].sym)^.labelnr <= HiLab) Then
  2137. Inc(LTable^[pasmlabel(paicpu(P)^.oper[0].sym)^.labelnr-LoLab].RefsFound);
  2138. end;
  2139. end;
  2140. { ait_instruction:
  2141. Begin
  2142. If (Paicpu(p)^.opcode = A_PUSH) And
  2143. (Paicpu(p)^.oper[0].typ = top_symbol) And
  2144. (PCSymbol(Paicpu(p)^.oper[0])^.offset = 0) Then
  2145. Begin
  2146. TmpStr := StrPas(PCSymbol(Paicpu(p)^.oper[0])^.symbol);
  2147. If}
  2148. End;
  2149. {$EndIf JumpAnal}
  2150. Inc(NrOfPaiObjs);
  2151. GetNextInstruction(p, p);
  2152. End;
  2153. {$IfDef TP}
  2154. If (MemAvail < (SizeOf(TPaiProp)*NrOfPaiObjs))
  2155. Or (NrOfPaiObjs = 0)
  2156. {this doesn't have to be one contiguous block}
  2157. Then InitDFAPass2 := False
  2158. Else InitDFAPass2 := True;
  2159. {$Else}
  2160. {Uncomment the next line to see how much memory the reloading optimizer needs}
  2161. { Writeln((NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4)));}
  2162. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  2163. If NrOfPaiObjs <> 0 Then
  2164. Begin
  2165. InitDFAPass2 := True;
  2166. GetMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4));
  2167. p := BlockStart;
  2168. SkipHead(p);
  2169. For Count := 1 To NrOfPaiObjs Do
  2170. Begin
  2171. PPaiProp(p^.OptInfo) := @PaiPropBlock^[Count];
  2172. GetNextInstruction(p, p);
  2173. End;
  2174. End
  2175. Else InitDFAPass2 := False;
  2176. {$EndIf TP}
  2177. End;
  2178. Function DFAPass2(
  2179. {$ifdef statedebug}
  2180. AsmL: PAasmOutPut;
  2181. {$endif statedebug}
  2182. BlockStart, BlockEnd: Pai): Boolean;
  2183. Begin
  2184. If InitDFAPass2(BlockStart, BlockEnd) Then
  2185. Begin
  2186. DoDFAPass2(
  2187. {$ifdef statedebug}
  2188. asml,
  2189. {$endif statedebug}
  2190. BlockStart, BlockEnd);
  2191. DFAPass2 := True
  2192. End
  2193. Else DFAPass2 := False;
  2194. End;
  2195. Procedure ShutDownDFA;
  2196. Begin
  2197. If LabDif <> 0 Then
  2198. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  2199. End;
  2200. End.
  2201. {
  2202. $Log$
  2203. Revision 1.87 2000-04-29 16:56:45 jonas
  2204. * destroyreg overwrote some memory if the reg was an FPU register
  2205. Revision 1.86 2000/04/10 12:45:56 jonas
  2206. * fixed a serious bug in the CSE which (I think) only showed with
  2207. -dnewoptimizations when using multi-dimensional arrays with
  2208. elements of a size different from 1, 2 or 4 (especially strings).
  2209. * made the DFA/CSE more robust (much less dependent on specifics of the
  2210. code generator)
  2211. Revision 1.85 2000/03/25 18:58:00 jonas
  2212. * moved AllocRegBetween() from csopt386 to this unit because it's now
  2213. also used by popt386
  2214. Revision 1.84 2000/02/24 18:41:38 peter
  2215. * removed warnings/notes
  2216. Revision 1.83 2000/02/10 14:57:14 jonas
  2217. * fixed bug due to lack of support for top_symbol operands
  2218. Revision 1.82 2000/02/09 13:22:51 peter
  2219. * log truncated
  2220. Revision 1.81 2000/02/04 13:52:17 jonas
  2221. * better support for regvars (still needs a move of the call to the optimize
  2222. procedure to a place where resetusableregisters is not yet called to work)
  2223. * small regallocation fixes for -dnewoptimizations
  2224. Revision 1.80 2000/01/28 15:15:31 jonas
  2225. * moved skipinstr from daopt386 to aasm
  2226. * fixed crashing bug with -dreplacereg in csopt386.pas
  2227. Revision 1.79 2000/01/22 16:08:06 jonas
  2228. * better handling of exit(func_result) (no release of register that
  2229. holds the function result added)
  2230. * several other small improvements for reg allocation fixes
  2231. Revision 1.78 2000/01/13 13:07:06 jonas
  2232. * released -dalignreg
  2233. * some small fixes to -dnewOptimizations helper procedures
  2234. Revision 1.77 2000/01/09 01:44:21 jonas
  2235. + (de)allocation info for EDI to fix reported bug on mailinglist.
  2236. Also some (de)allocation info for ESI added. Between -dallocEDI
  2237. because at this time of the night bugs could easily slip in ;)
  2238. Revision 1.76 2000/01/07 01:14:23 peter
  2239. * updated copyright to 2000
  2240. Revision 1.75 1999/12/05 16:48:43 jonas
  2241. * CSE of constant loading in regs works properly again
  2242. + if a constant is stored into memory using "mov const, ref" and
  2243. there is a reg that contains this const, it is changed into
  2244. "mov reg, ref"
  2245. Revision 1.74 1999/12/02 11:26:41 peter
  2246. * newoptimizations define added
  2247. Revision 1.73 1999/11/27 23:45:43 jonas
  2248. * even more missing register deallocations are added!
  2249. Revision 1.72 1999/11/21 13:06:30 jonas
  2250. * improved fixing of missing regallocs (they're almost all correct
  2251. now!)
  2252. Revision 1.71 1999/11/20 12:50:32 jonas
  2253. * fixed small typo (C_M* -> Ch_M*) so -darithopt compiles again
  2254. Revision 1.70 1999/11/14 11:25:38 jonas
  2255. * fixed stupid typo in previous commit :(
  2256. Revision 1.69 1999/11/13 19:01:51 jonas
  2257. * div, idiv and mul destroy edx!!
  2258. Revision 1.68 1999/11/07 14:57:09 jonas
  2259. * much more complete/waterproof RegModifiedByInstruction()
  2260. Revision 1.67 1999/11/06 14:34:20 peter
  2261. * truncated log to 20 revs
  2262. Revision 1.66 1999/11/05 16:01:46 jonas
  2263. + first implementation of choosing least used register for alignment code
  2264. (not yet working, between ifdef alignreg)
  2265. Revision 1.65 1999/10/27 16:11:28 peter
  2266. * insns.dat is used to generate all i386*.inc files
  2267. Revision 1.64 1999/10/23 14:44:24 jonas
  2268. * finally got around making GetNextInstruction return false when
  2269. the current pai object is a AsmBlockStart marker
  2270. * changed a loop in aopt386 which was incompatible with this change
  2271. Revision 1.63 1999/10/14 14:57:52 florian
  2272. - removed the hcodegen use in the new cg, use cgbase instead
  2273. Revision 1.62 1999/10/07 16:07:35 jonas
  2274. * small bugfix in ArrayRefsEq
  2275. }