daopt386.pas 98 KB

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