daopt386.pas 84 KB

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