daopt386.pas 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809
  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 current_procdef.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 current_procdef.rettype.def.size <> 0 then
  345. begin
  346. DoRemoveLastDeallocForFuncRes(asmL,R_EAX);
  347. { for int64/qword }
  348. if current_procdef.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 current_procdef.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 current_procdef.rettype.def.size <> 0 then
  365. begin
  366. regs := [R_EAX];
  367. { for int64/qword }
  368. if current_procdef.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_EDI]) then
  386. exit;
  387. getNoDeallocRegs(funcResRegs);
  388. { funcResRegs := funcResRegs - rg.usableregsint;}
  389. { funcResRegs := funcResRegs - [R_EDI];}
  390. funcResRegs := funcResRegs - [R_EAX,R_EBX,R_ECX,R_EDX,R_ESI];
  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]) 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 = current_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,current_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 [current_procinfo.FramePointer,R_NO,R_ESP])
  1318. Then RegSet := RegSet + [Base];
  1319. If Not(Index in [current_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,current_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 [current_procinfo.FramePointer,R_NO,R_ESP])
  1331. Then RegSet := RegSet + [Base];
  1332. If Not(Index in [current_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 [current_procinfo.FramePointer.enum, R_NO, R_ESP]) Then
  1427. RegInfo.RegsLoadedForRef := RegInfo.RegsLoadedForRef + [Base.enum];
  1428. If Not(Index.enum in [current_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 [current_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 [current_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 [current_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 = current_procinfo.framepointer.enum);
  1607. end;
  1608. function containsPointerRef(p: Tai): boolean;
  1609. { checks if an instruction contains a reference which is a pointer location }
  1610. var
  1611. hp: Taicpu;
  1612. count: longint;
  1613. begin
  1614. containsPointerRef := false;
  1615. if p.typ <> ait_instruction then
  1616. exit;
  1617. hp := Taicpu(p);
  1618. for count := low(hp.oper) to high(hp.oper) do
  1619. begin
  1620. case hp.oper[count].typ of
  1621. top_ref:
  1622. if not isSimpleRef(hp.oper[count].ref^) then
  1623. begin
  1624. containsPointerRef := true;
  1625. exit;
  1626. end;
  1627. top_none:
  1628. exit;
  1629. end;
  1630. end;
  1631. end;
  1632. function containsPointerLoad(c: tcontent): boolean;
  1633. { checks whether the contents of a register contain a pointer reference }
  1634. var
  1635. p: Tai;
  1636. count: longint;
  1637. begin
  1638. containsPointerLoad := false;
  1639. p := c.startmod;
  1640. for count := c.nrOfMods downto 1 do
  1641. begin
  1642. if containsPointerRef(p) then
  1643. begin
  1644. containsPointerLoad := true;
  1645. exit;
  1646. end;
  1647. getnextinstruction(p,p);
  1648. end;
  1649. end;
  1650. function writeToMemDestroysContents(regWritten: tregister; const ref: treference;
  1651. reg: tregister; const c: tcontent; var invalsmemwrite: boolean): boolean;
  1652. { returns whether the contents c of reg are invalid after regWritten is }
  1653. { is written to ref }
  1654. var
  1655. refsEq: trefCompare;
  1656. begin
  1657. reg := reg32(reg);
  1658. regWritten := reg32(regWritten);
  1659. if isSimpleRef(ref) then
  1660. begin
  1661. if (ref.index.enum <> R_NO) or
  1662. (assigned(ref.symbol) and
  1663. (ref.base.enum <> R_NO)) then
  1664. { local/global variable or parameter which is an array }
  1665. refsEq := {$ifdef fpc}@{$endif}arrayRefsEq
  1666. else
  1667. { local/global variable or parameter which is not an array }
  1668. refsEq := {$ifdef fpc}@{$endif}refsEqual;
  1669. invalsmemwrite :=
  1670. assigned(c.memwrite) and
  1671. ((not(cs_uncertainOpts in aktglobalswitches) and
  1672. containsPointerRef(c.memwrite)) or
  1673. refsEq(c.memwrite.oper[1].ref^,ref));
  1674. if not(c.typ in [con_ref,con_noRemoveRef,con_invalid]) then
  1675. begin
  1676. writeToMemDestroysContents := false;
  1677. exit;
  1678. end;
  1679. { write something to a parameter, a local or global variable, so }
  1680. { * with uncertain optimizations on: }
  1681. { - destroy the contents of registers whose contents have somewhere a }
  1682. { "mov?? (Ref), %reg". WhichReg (this is the register whose contents }
  1683. { are being written to memory) is not destroyed if it's StartMod is }
  1684. { of that form and NrOfMods = 1 (so if it holds ref, but is not a }
  1685. { expression based on Ref) }
  1686. { * with uncertain optimizations off: }
  1687. { - also destroy registers that contain any pointer }
  1688. with c do
  1689. writeToMemDestroysContents :=
  1690. (typ in [con_ref,con_noRemoveRef]) and
  1691. ((not(cs_uncertainOpts in aktglobalswitches) and
  1692. containsPointerLoad(c)
  1693. ) or
  1694. (refInSequence(ref,c,refsEq) and
  1695. ((reg.enum <> regWritten.enum) or
  1696. not((nrOfMods = 1) and
  1697. {StarMod is always of the type ait_instruction}
  1698. (Taicpu(StartMod).oper[0].typ = top_ref) and
  1699. refsEq(Taicpu(StartMod).oper[0].ref^, ref)
  1700. )
  1701. )
  1702. )
  1703. );
  1704. end
  1705. else
  1706. { write something to a pointer location, so }
  1707. { * with uncertain optimzations on: }
  1708. { - do not destroy registers which contain a local/global variable or }
  1709. { a parameter, except if DestroyRefs is called because of a "movsl" }
  1710. { * with uncertain optimzations off: }
  1711. { - destroy every register which contains a memory location }
  1712. begin
  1713. invalsmemwrite :=
  1714. assigned(c.memwrite) and
  1715. (not(cs_UncertainOpts in aktglobalswitches) or
  1716. containsPointerRef(c.memwrite));
  1717. if not(c.typ in [con_ref,con_noRemoveRef,con_invalid]) then
  1718. begin
  1719. writeToMemDestroysContents := false;
  1720. exit;
  1721. end;
  1722. with c do
  1723. writeToMemDestroysContents :=
  1724. (typ in [con_ref,con_noRemoveRef]) and
  1725. (not(cs_UncertainOpts in aktglobalswitches) or
  1726. { for movsl }
  1727. ((ref.base.enum = R_EDI) and (ref.index.enum = R_EDI)) or
  1728. { don't destroy if reg contains a parameter, local or global variable }
  1729. containsPointerLoad(c)
  1730. );
  1731. end;
  1732. end;
  1733. function writeToRegDestroysContents(destReg: tregister; reg: tregister;
  1734. const c: tcontent): boolean;
  1735. { returns whether the contents c of reg are invalid after destReg is }
  1736. { modified }
  1737. begin
  1738. writeToRegDestroysContents :=
  1739. (c.typ in [con_ref,con_noRemoveRef,con_invalid]) and
  1740. sequenceDependsOnReg(c,reg,reg32(destReg));
  1741. end;
  1742. function writeDestroysContents(const op: toper; reg: tregister;
  1743. const c: tcontent): boolean;
  1744. { returns whether the contents c of reg are invalid after regWritten is }
  1745. { is written to op }
  1746. var
  1747. dummy: boolean;
  1748. r:Tregister;
  1749. begin
  1750. reg := reg32(reg);
  1751. r.enum:=R_NO;
  1752. case op.typ of
  1753. top_reg:
  1754. writeDestroysContents :=
  1755. writeToRegDestroysContents(op.reg,reg,c);
  1756. top_ref:
  1757. writeDestroysContents :=
  1758. writeToMemDestroysContents(r,op.ref^,reg,c,dummy);
  1759. else
  1760. writeDestroysContents := false;
  1761. end;
  1762. end;
  1763. procedure destroyRefs(p: Tai; const ref: treference; regWritten: tregister);
  1764. { destroys all registers which possibly contain a reference to Ref, regWritten }
  1765. { is the register whose contents are being written to memory (if this proc }
  1766. { is called because of a "mov?? %reg, (mem)" instruction) }
  1767. var
  1768. counter: TRegister;
  1769. destroymemwrite: boolean;
  1770. begin
  1771. for counter.enum := R_EAX to R_EDI Do
  1772. begin
  1773. if writeToMemDestroysContents(regWritten,ref,counter,
  1774. pTaiProp(p.optInfo)^.regs[counter.enum],destroymemwrite) then
  1775. destroyReg(pTaiProp(p.optInfo), counter, false)
  1776. else if destroymemwrite then
  1777. pTaiProp(p.optinfo)^.regs[counter.enum].MemWrite := nil;
  1778. end;
  1779. End;
  1780. Procedure DestroyAllRegs(p: PTaiProp; read, written: boolean);
  1781. Var Counter: TRegister;
  1782. Begin {initializes/desrtoys all registers}
  1783. For Counter.enum := R_EAX To R_EDI Do
  1784. Begin
  1785. if read then
  1786. ReadReg(p, Counter);
  1787. DestroyReg(p, Counter, written);
  1788. p^.regs[counter.enum].MemWrite := nil;
  1789. End;
  1790. p^.DirFlag := F_Unknown;
  1791. End;
  1792. Procedure DestroyOp(TaiObj: Tai; const o:Toper);
  1793. var
  1794. {$ifdef statedebug}
  1795. hp: Tai;
  1796. {$endif statedebug}
  1797. r:Tregister;
  1798. Begin
  1799. Case o.typ Of
  1800. top_reg:
  1801. begin
  1802. {$ifdef statedebug}
  1803. hp := tai_comment.Create(strpnew('destroying '+std_reg2str[o.reg]));
  1804. hp.next := Taiobj^.next;
  1805. hp.previous := Taiobj;
  1806. Taiobj^.next := hp;
  1807. if assigned(hp.next) then
  1808. hp.next^.previous := hp;
  1809. {$endif statedebug}
  1810. DestroyReg(PTaiProp(TaiObj.OptInfo), reg32(o.reg), true);
  1811. end;
  1812. top_ref:
  1813. Begin
  1814. ReadRef(PTaiProp(TaiObj.OptInfo), o.ref);
  1815. r.enum:=R_NO;
  1816. DestroyRefs(TaiObj, o.ref^, r);
  1817. End;
  1818. top_symbol:;
  1819. End;
  1820. End;
  1821. Function DFAPass1(AsmL: TAAsmOutput; BlockStart: Tai): Tai;
  1822. {gathers the RegAlloc data... still need to think about where to store it to
  1823. avoid global vars}
  1824. Var BlockEnd: Tai;
  1825. Begin
  1826. BlockEnd := FindLoHiLabels(LoLab, HiLab, LabDif, BlockStart);
  1827. BuildLabelTableAndFixRegAlloc(AsmL, LTable, LoLab, LabDif, BlockStart, BlockEnd);
  1828. DFAPass1 := BlockEnd;
  1829. End;
  1830. Procedure AddInstr2RegContents({$ifdef statedebug} asml: TAAsmoutput; {$endif}
  1831. p: Taicpu; reg: TRegister);
  1832. {$ifdef statedebug}
  1833. var hp: Tai;
  1834. {$endif statedebug}
  1835. Begin
  1836. if reg.enum>lastreg then
  1837. internalerror(200301081);
  1838. Reg := Reg32(Reg);
  1839. With PTaiProp(p.optinfo)^.Regs[reg.enum] Do
  1840. if (typ in [con_ref,con_noRemoveRef])
  1841. Then
  1842. Begin
  1843. incState(wstate,1);
  1844. {also store how many instructions are part of the sequence in the first
  1845. instructions PTaiProp, so it can be easily accessed from within
  1846. CheckSequence}
  1847. Inc(NrOfMods, NrOfInstrSinceLastMod[Reg.enum]);
  1848. PTaiProp(Tai(StartMod).OptInfo)^.Regs[Reg.enum].NrOfMods := NrOfMods;
  1849. NrOfInstrSinceLastMod[Reg.enum] := 0;
  1850. invalidateDependingRegs(p.optinfo,reg);
  1851. pTaiprop(p.optinfo)^.regs[reg.enum].memwrite := nil;
  1852. {$ifdef StateDebug}
  1853. hp := tai_comment.Create(strpnew(std_reg2str[reg]+': '+tostr(PTaiProp(p.optinfo)^.Regs[reg].WState)
  1854. + ' -- ' + tostr(PTaiProp(p.optinfo)^.Regs[reg].nrofmods))));
  1855. InsertLLItem(AsmL, p, p.next, hp);
  1856. {$endif StateDebug}
  1857. End
  1858. Else
  1859. Begin
  1860. {$ifdef statedebug}
  1861. hp := tai_comment.Create(strpnew('destroying '+std_reg2str[reg]));
  1862. insertllitem(asml,p,p.next,hp);
  1863. {$endif statedebug}
  1864. DestroyReg(PTaiProp(p.optinfo), Reg, true);
  1865. {$ifdef StateDebug}
  1866. hp := tai_comment.Create(strpnew(std_reg2str[reg]+': '+tostr(PTaiProp(p.optinfo)^.Regs[reg.enum].WState)));
  1867. InsertLLItem(AsmL, p, p.next, hp);
  1868. {$endif StateDebug}
  1869. End
  1870. End;
  1871. Procedure AddInstr2OpContents({$ifdef statedebug} asml: TAAsmoutput; {$endif}
  1872. p: Taicpu; const oper: TOper);
  1873. Begin
  1874. If oper.typ = top_reg Then
  1875. AddInstr2RegContents({$ifdef statedebug} asml, {$endif}p, oper.reg)
  1876. Else
  1877. Begin
  1878. ReadOp(PTaiProp(p.optinfo), oper);
  1879. DestroyOp(p, oper);
  1880. End
  1881. End;
  1882. Procedure DoDFAPass2(
  1883. {$Ifdef StateDebug}
  1884. AsmL: TAAsmOutput;
  1885. {$endif statedebug}
  1886. BlockStart, BlockEnd: Tai);
  1887. {Analyzes the Data Flow of an assembler list. Starts creating the reg
  1888. contents for the instructions starting with p. Returns the last Tai which has
  1889. been processed}
  1890. Var
  1891. CurProp, LastFlagsChangeProp: PTaiProp;
  1892. Cnt, InstrCnt : Longint;
  1893. InstrProp: TInsProp;
  1894. UsedRegs: TRegSet;
  1895. prev,p : Tai;
  1896. TmpRef: TReference;
  1897. TmpReg: TRegister;
  1898. {$ifdef AnalyzeLoops}
  1899. hp : Tai;
  1900. TmpState: Byte;
  1901. {$endif AnalyzeLoops}
  1902. Begin
  1903. p := BlockStart;
  1904. LastFlagsChangeProp := nil;
  1905. prev := nil;
  1906. UsedRegs := [];
  1907. UpdateUsedregs(UsedRegs, p);
  1908. SkipHead(P);
  1909. BlockStart := p;
  1910. InstrCnt := 1;
  1911. FillChar(NrOfInstrSinceLastMod, SizeOf(NrOfInstrSinceLastMod), 0);
  1912. While (P <> BlockEnd) Do
  1913. Begin
  1914. CurProp := @TaiPropBlock^[InstrCnt];
  1915. If assigned(prev)
  1916. Then
  1917. Begin
  1918. {$ifdef JumpAnal}
  1919. If (p.Typ <> ait_label) Then
  1920. {$endif JumpAnal}
  1921. Begin
  1922. CurProp^.regs := PTaiProp(prev.OptInfo)^.Regs;
  1923. CurProp^.DirFlag := PTaiProp(prev.OptInfo)^.DirFlag;
  1924. CurProp^.FlagsUsed := false;
  1925. End
  1926. End
  1927. Else
  1928. Begin
  1929. FillChar(CurProp^, SizeOf(CurProp^), 0);
  1930. { For TmpReg := R_EAX to R_EDI Do
  1931. CurProp^.regs[TmpReg].WState := 1;}
  1932. End;
  1933. CurProp^.UsedRegs := UsedRegs;
  1934. CurProp^.CanBeRemoved := False;
  1935. UpdateUsedRegs(UsedRegs, Tai(p.Next));
  1936. For TmpReg.enum := R_EAX To R_EDI Do
  1937. if NrOfInstrSinceLastMod[TmpReg.enum] < 255 then
  1938. Inc(NrOfInstrSinceLastMod[TmpReg.enum])
  1939. else
  1940. begin
  1941. NrOfInstrSinceLastMod[TmpReg.enum] := 0;
  1942. curprop^.regs[TmpReg.enum].typ := con_unknown;
  1943. end;
  1944. Case p.typ Of
  1945. ait_marker:;
  1946. ait_label:
  1947. {$Ifndef JumpAnal}
  1948. if not labelCanBeSkipped(Tai_label(p)) then
  1949. DestroyAllRegs(CurProp,false,false);
  1950. {$Else JumpAnal}
  1951. Begin
  1952. If not labelCanBeSkipped(Tai_label(p)) Then
  1953. With LTable^[Tai_Label(p).l^.labelnr-LoLab] Do
  1954. {$IfDef AnalyzeLoops}
  1955. If (RefsFound = Tai_Label(p).l^.RefCount)
  1956. {$Else AnalyzeLoops}
  1957. If (JmpsProcessed = Tai_Label(p).l^.RefCount)
  1958. {$EndIf AnalyzeLoops}
  1959. Then
  1960. {all jumps to this label have been found}
  1961. {$IfDef AnalyzeLoops}
  1962. If (JmpsProcessed > 0)
  1963. Then
  1964. {$EndIf AnalyzeLoops}
  1965. {we've processed at least one jump to this label}
  1966. Begin
  1967. If (GetLastInstruction(p, hp) And
  1968. Not(((hp.typ = ait_instruction)) And
  1969. (Taicpu_labeled(hp).is_jmp))
  1970. Then
  1971. {previous instruction not a JMP -> the contents of the registers after the
  1972. previous intruction has been executed have to be taken into account as well}
  1973. For TmpReg.enum := R_EAX to R_EDI Do
  1974. Begin
  1975. If (CurProp^.regs[TmpReg.enum].WState <>
  1976. PTaiProp(hp.OptInfo)^.Regs[TmpReg.enum].WState)
  1977. Then DestroyReg(CurProp, TmpReg.enum, true)
  1978. End
  1979. End
  1980. {$IfDef AnalyzeLoops}
  1981. Else
  1982. {a label from a backward jump (e.g. a loop), no jump to this label has
  1983. already been processed}
  1984. If GetLastInstruction(p, hp) And
  1985. Not(hp.typ = ait_instruction) And
  1986. (Taicpu_labeled(hp).opcode = A_JMP))
  1987. Then
  1988. {previous instruction not a jmp, so keep all the registers' contents from the
  1989. previous instruction}
  1990. Begin
  1991. CurProp^.regs := PTaiProp(hp.OptInfo)^.Regs;
  1992. CurProp.DirFlag := PTaiProp(hp.OptInfo)^.DirFlag;
  1993. End
  1994. Else
  1995. {previous instruction a jmp and no jump to this label processed yet}
  1996. Begin
  1997. hp := p;
  1998. Cnt := InstrCnt;
  1999. {continue until we find a jump to the label or a label which has already
  2000. been processed}
  2001. While GetNextInstruction(hp, hp) And
  2002. Not((hp.typ = ait_instruction) And
  2003. (Taicpu(hp).is_jmp) and
  2004. (tasmlabel(Taicpu(hp).oper[0].sym).labelnr = Tai_Label(p).l^.labelnr)) And
  2005. Not((hp.typ = ait_label) And
  2006. (LTable^[Tai_Label(hp).l^.labelnr-LoLab].RefsFound
  2007. = Tai_Label(hp).l^.RefCount) And
  2008. (LTable^[Tai_Label(hp).l^.labelnr-LoLab].JmpsProcessed > 0)) Do
  2009. Inc(Cnt);
  2010. If (hp.typ = ait_label)
  2011. Then
  2012. {there's a processed label after the current one}
  2013. Begin
  2014. CurProp^.regs := TaiPropBlock^[Cnt].Regs;
  2015. CurProp.DirFlag := TaiPropBlock^[Cnt].DirFlag;
  2016. End
  2017. Else
  2018. {there's no label anymore after the current one, or they haven't been
  2019. processed yet}
  2020. Begin
  2021. GetLastInstruction(p, hp);
  2022. CurProp^.regs := PTaiProp(hp.OptInfo)^.Regs;
  2023. CurProp.DirFlag := PTaiProp(hp.OptInfo)^.DirFlag;
  2024. DestroyAllRegs(PTaiProp(hp.OptInfo),true,true)
  2025. End
  2026. End
  2027. {$EndIf AnalyzeLoops}
  2028. Else
  2029. {not all references to this label have been found, so destroy all registers}
  2030. Begin
  2031. GetLastInstruction(p, hp);
  2032. CurProp^.regs := PTaiProp(hp.OptInfo)^.Regs;
  2033. CurProp.DirFlag := PTaiProp(hp.OptInfo)^.DirFlag;
  2034. DestroyAllRegs(CurProp,true,true)
  2035. End;
  2036. End;
  2037. {$EndIf JumpAnal}
  2038. {$ifdef GDB}
  2039. ait_stabs, ait_stabn, ait_stab_function_name:;
  2040. {$endif GDB}
  2041. ait_align: ; { may destroy flags !!! }
  2042. ait_instruction:
  2043. Begin
  2044. if Taicpu(p).is_jmp or
  2045. (Taicpu(p).opcode = A_JMP) then
  2046. begin
  2047. {$IfNDef JumpAnal}
  2048. for tmpReg.enum := R_EAX to R_EDI do
  2049. with curProp^.regs[tmpReg.enum] do
  2050. case typ of
  2051. con_ref: typ := con_noRemoveRef;
  2052. con_const: typ := con_noRemoveConst;
  2053. con_invalid: typ := con_unknown;
  2054. end;
  2055. {$Else JumpAnal}
  2056. With LTable^[tasmlabel(Taicpu(p).oper[0].sym).labelnr-LoLab] Do
  2057. If (RefsFound = tasmlabel(Taicpu(p).oper[0].sym).RefCount) Then
  2058. Begin
  2059. If (InstrCnt < InstrNr)
  2060. Then
  2061. {forward jump}
  2062. If (JmpsProcessed = 0) Then
  2063. {no jump to this label has been processed yet}
  2064. Begin
  2065. TaiPropBlock^[InstrNr].Regs := CurProp^.regs;
  2066. TaiPropBlock^[InstrNr].DirFlag := CurProp.DirFlag;
  2067. Inc(JmpsProcessed);
  2068. End
  2069. Else
  2070. Begin
  2071. For TmpReg := R_EAX to R_EDI Do
  2072. If (TaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  2073. CurProp^.regs[TmpReg].WState) Then
  2074. DestroyReg(@TaiPropBlock^[InstrNr], TmpReg, true);
  2075. Inc(JmpsProcessed);
  2076. End
  2077. {$ifdef AnalyzeLoops}
  2078. Else
  2079. { backward jump, a loop for example}
  2080. { If (JmpsProcessed > 0) Or
  2081. Not(GetLastInstruction(TaiObj, hp) And
  2082. (hp.typ = ait_labeled_instruction) And
  2083. (Taicpu_labeled(hp).opcode = A_JMP))
  2084. Then}
  2085. {instruction prior to label is not a jmp, or at least one jump to the label
  2086. has yet been processed}
  2087. Begin
  2088. Inc(JmpsProcessed);
  2089. For TmpReg := R_EAX to R_EDI Do
  2090. If (TaiPropBlock^[InstrNr].Regs[TmpReg].WState <>
  2091. CurProp^.regs[TmpReg].WState)
  2092. Then
  2093. Begin
  2094. TmpState := TaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  2095. Cnt := InstrNr;
  2096. While (TmpState = TaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  2097. Begin
  2098. DestroyReg(@TaiPropBlock^[Cnt], TmpReg, true);
  2099. Inc(Cnt);
  2100. End;
  2101. While (Cnt <= InstrCnt) Do
  2102. Begin
  2103. Inc(TaiPropBlock^[Cnt].Regs[TmpReg].WState);
  2104. Inc(Cnt)
  2105. End
  2106. End;
  2107. End
  2108. { Else }
  2109. {instruction prior to label is a jmp and no jumps to the label have yet been
  2110. processed}
  2111. { Begin
  2112. Inc(JmpsProcessed);
  2113. For TmpReg := R_EAX to R_EDI Do
  2114. Begin
  2115. TmpState := TaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  2116. Cnt := InstrNr;
  2117. While (TmpState = TaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  2118. Begin
  2119. TaiPropBlock^[Cnt].Regs[TmpReg] := CurProp^.regs[TmpReg];
  2120. Inc(Cnt);
  2121. End;
  2122. TmpState := TaiPropBlock^[InstrNr].Regs[TmpReg].WState;
  2123. While (TmpState = TaiPropBlock^[Cnt].Regs[TmpReg].WState) Do
  2124. Begin
  2125. DestroyReg(@TaiPropBlock^[Cnt], TmpReg, true);
  2126. Inc(Cnt);
  2127. End;
  2128. While (Cnt <= InstrCnt) Do
  2129. Begin
  2130. Inc(TaiPropBlock^[Cnt].Regs[TmpReg].WState);
  2131. Inc(Cnt)
  2132. End
  2133. End
  2134. End}
  2135. {$endif AnalyzeLoops}
  2136. End;
  2137. {$EndIf JumpAnal}
  2138. end
  2139. else
  2140. begin
  2141. InstrProp := InsProp[Taicpu(p).opcode];
  2142. Case Taicpu(p).opcode Of
  2143. A_MOV, A_MOVZX, A_MOVSX:
  2144. Begin
  2145. Case Taicpu(p).oper[0].typ Of
  2146. top_ref, top_reg:
  2147. case Taicpu(p).oper[1].typ Of
  2148. top_reg:
  2149. Begin
  2150. {$ifdef statedebug}
  2151. hp := tai_comment.Create(strpnew('destroying '+
  2152. std_reg2str[Taicpu(p).oper[1].reg])));
  2153. insertllitem(asml,p,p.next,hp);
  2154. {$endif statedebug}
  2155. readOp(curprop, Taicpu(p).oper[0]);
  2156. tmpreg := reg32(Taicpu(p).oper[1].reg);
  2157. if tmpreg.enum>lastreg then
  2158. internalerror(200301081);
  2159. if regInOp(tmpreg, Taicpu(p).oper[0]) and
  2160. (curProp^.regs[tmpReg.enum].typ in [con_ref,con_noRemoveRef]) then
  2161. begin
  2162. with curprop^.regs[tmpreg.enum] Do
  2163. begin
  2164. incState(wstate,1);
  2165. { also store how many instructions are part of the sequence in the first }
  2166. { instruction's PTaiProp, so it can be easily accessed from within }
  2167. { CheckSequence }
  2168. inc(nrOfMods, nrOfInstrSinceLastMod[tmpreg.enum]);
  2169. pTaiprop(startmod.optinfo)^.regs[tmpreg.enum].nrOfMods := nrOfMods;
  2170. nrOfInstrSinceLastMod[tmpreg.enum] := 0;
  2171. { Destroy the contents of the registers }
  2172. { that depended on the previous value of }
  2173. { this register }
  2174. invalidateDependingRegs(curprop,tmpreg);
  2175. curprop^.regs[tmpreg.enum].memwrite := nil;
  2176. end;
  2177. end
  2178. else
  2179. begin
  2180. {$ifdef statedebug}
  2181. hp := tai_comment.Create(strpnew('destroying & initing '+std_reg2str[tmpreg.enum]));
  2182. insertllitem(asml,p,p.next,hp);
  2183. {$endif statedebug}
  2184. destroyReg(curprop, tmpreg, true);
  2185. if not(reginop(tmpreg, Taicpu(p).oper[0])) then
  2186. with curprop^.regs[tmpreg.enum] Do
  2187. begin
  2188. typ := con_ref;
  2189. startmod := p;
  2190. nrOfMods := 1;
  2191. end
  2192. end;
  2193. {$ifdef StateDebug}
  2194. hp := tai_comment.Create(strpnew(std_reg2str[TmpReg.enum]+': '+tostr(CurProp^.regs[TmpReg.enum].WState)));
  2195. InsertLLItem(AsmL, p, p.next, hp);
  2196. {$endif StateDebug}
  2197. End;
  2198. Top_Ref:
  2199. Begin
  2200. tmpreg.enum:=R_NO;
  2201. ReadRef(CurProp, Taicpu(p).oper[1].ref);
  2202. if taicpu(p).oper[0].typ = top_reg then
  2203. begin
  2204. ReadReg(CurProp, Taicpu(p).oper[0].reg);
  2205. DestroyRefs(p, Taicpu(p).oper[1].ref^, Taicpu(p).oper[0].reg);
  2206. pTaiProp(p.optinfo)^.regs[reg32(Taicpu(p).oper[0].reg).enum].memwrite :=
  2207. Taicpu(p);
  2208. end
  2209. else
  2210. DestroyRefs(p, Taicpu(p).oper[1].ref^, tmpreg);
  2211. End;
  2212. End;
  2213. top_symbol,Top_Const:
  2214. Begin
  2215. Case Taicpu(p).oper[1].typ Of
  2216. Top_Reg:
  2217. Begin
  2218. TmpReg := Reg32(Taicpu(p).oper[1].reg);
  2219. {$ifdef statedebug}
  2220. hp := tai_comment.Create(strpnew('destroying '+std_reg2str[tmpreg]));
  2221. insertllitem(asml,p,p.next,hp);
  2222. {$endif statedebug}
  2223. With CurProp^.regs[TmpReg.enum] Do
  2224. Begin
  2225. DestroyReg(CurProp, TmpReg, true);
  2226. typ := Con_Const;
  2227. StartMod := p;
  2228. End
  2229. End;
  2230. Top_Ref:
  2231. Begin
  2232. tmpreg.enum:=R_NO;
  2233. ReadRef(CurProp, Taicpu(p).oper[1].ref);
  2234. DestroyRefs(P, Taicpu(p).oper[1].ref^, tmpreg);
  2235. End;
  2236. End;
  2237. End;
  2238. End;
  2239. End;
  2240. A_DIV, A_IDIV, A_MUL:
  2241. Begin
  2242. ReadOp(Curprop, Taicpu(p).oper[0]);
  2243. tmpreg.enum:=R_EAX;
  2244. ReadReg(CurProp,tmpreg);
  2245. If (Taicpu(p).OpCode = A_IDIV) or
  2246. (Taicpu(p).OpCode = A_DIV) Then
  2247. begin
  2248. tmpreg.enum:=R_EDX;
  2249. ReadReg(CurProp,tmpreg);
  2250. end;
  2251. {$ifdef statedebug}
  2252. hp := tai_comment.Create(strpnew('destroying eax and edx'));
  2253. insertllitem(asml,p,p.next,hp);
  2254. {$endif statedebug}
  2255. { DestroyReg(CurProp, R_EAX, true);}
  2256. tmpreg.enum:=R_EAX;
  2257. AddInstr2RegContents({$ifdef statedebug}asml,{$endif}
  2258. Taicpu(p), tmpreg);
  2259. tmpreg.enum:=R_EDX;
  2260. DestroyReg(CurProp, tmpreg, true)
  2261. End;
  2262. A_IMUL:
  2263. Begin
  2264. ReadOp(CurProp,Taicpu(p).oper[0]);
  2265. ReadOp(CurProp,Taicpu(p).oper[1]);
  2266. If (Taicpu(p).oper[2].typ = top_none) Then
  2267. If (Taicpu(p).oper[1].typ = top_none) Then
  2268. Begin
  2269. tmpreg.enum:=R_EAX;
  2270. ReadReg(CurProp,tmpreg);
  2271. {$ifdef statedebug}
  2272. hp := tai_comment.Create(strpnew('destroying eax and edx'));
  2273. insertllitem(asml,p,p.next,hp);
  2274. {$endif statedebug}
  2275. { DestroyReg(CurProp, R_EAX, true); }
  2276. AddInstr2RegContents({$ifdef statedebug}asml,{$endif}
  2277. Taicpu(p), tmpreg);
  2278. tmpreg.enum:=R_EDX;
  2279. DestroyReg(CurProp,tmpreg, true)
  2280. End
  2281. Else
  2282. AddInstr2OpContents(
  2283. {$ifdef statedebug}asml,{$endif}
  2284. Taicpu(p), Taicpu(p).oper[1])
  2285. Else
  2286. AddInstr2OpContents({$ifdef statedebug}asml,{$endif}
  2287. Taicpu(p), Taicpu(p).oper[2]);
  2288. End;
  2289. A_LEA:
  2290. begin
  2291. readop(curprop,Taicpu(p).oper[0]);
  2292. if reginref(Taicpu(p).oper[1].reg,Taicpu(p).oper[0].ref^) then
  2293. AddInstr2RegContents({$ifdef statedebug}asml,{$endif}
  2294. Taicpu(p), Taicpu(p).oper[1].reg)
  2295. else
  2296. begin
  2297. {$ifdef statedebug}
  2298. hp := tai_comment.Create(strpnew('destroying & initing'+
  2299. std_reg2str[Taicpu(p).oper[1].reg])));
  2300. insertllitem(asml,p,p.next,hp);
  2301. {$endif statedebug}
  2302. destroyreg(curprop,Taicpu(p).oper[1].reg,true);
  2303. with curprop^.regs[Taicpu(p).oper[1].reg.enum] Do
  2304. begin
  2305. typ := con_ref;
  2306. startmod := p;
  2307. nrOfMods := 1;
  2308. end
  2309. end;
  2310. end;
  2311. Else
  2312. Begin
  2313. Cnt := 1;
  2314. While (Cnt <= MaxCh) And
  2315. (InstrProp.Ch[Cnt] <> Ch_None) Do
  2316. Begin
  2317. Case InstrProp.Ch[Cnt] Of
  2318. Ch_REAX..Ch_REDI:
  2319. begin
  2320. tmpreg.enum:=TCh2Reg(InstrProp.Ch[Cnt]);
  2321. ReadReg(CurProp,tmpreg);
  2322. end;
  2323. Ch_WEAX..Ch_RWEDI:
  2324. Begin
  2325. If (InstrProp.Ch[Cnt] >= Ch_RWEAX) Then
  2326. begin
  2327. tmpreg.enum:=TCh2Reg(InstrProp.Ch[Cnt]);
  2328. ReadReg(CurProp,tmpreg);
  2329. end;
  2330. {$ifdef statedebug}
  2331. hp := tai_comment.Create(strpnew('destroying '+
  2332. std_reg2str[TCh2Reg(InstrProp.Ch[Cnt])])));
  2333. insertllitem(asml,p,p.next,hp);
  2334. {$endif statedebug}
  2335. tmpreg.enum:=TCh2Reg(InstrProp.Ch[Cnt]);
  2336. DestroyReg(CurProp,tmpreg, true);
  2337. End;
  2338. Ch_MEAX..Ch_MEDI:
  2339. begin
  2340. tmpreg.enum:=TCh2Reg(InstrProp.Ch[Cnt]);
  2341. AddInstr2RegContents({$ifdef statedebug} asml,{$endif}
  2342. Taicpu(p),tmpreg);
  2343. end;
  2344. Ch_CDirFlag: CurProp^.DirFlag := F_NotSet;
  2345. Ch_SDirFlag: CurProp^.DirFlag := F_Set;
  2346. Ch_Rop1: ReadOp(CurProp, Taicpu(p).oper[0]);
  2347. Ch_Rop2: ReadOp(CurProp, Taicpu(p).oper[1]);
  2348. Ch_ROp3: ReadOp(CurProp, Taicpu(p).oper[2]);
  2349. Ch_Wop1..Ch_RWop1:
  2350. Begin
  2351. If (InstrProp.Ch[Cnt] in [Ch_RWop1]) Then
  2352. ReadOp(CurProp, Taicpu(p).oper[0]);
  2353. DestroyOp(p, Taicpu(p).oper[0]);
  2354. End;
  2355. Ch_Mop1:
  2356. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2357. Taicpu(p), Taicpu(p).oper[0]);
  2358. Ch_Wop2..Ch_RWop2:
  2359. Begin
  2360. If (InstrProp.Ch[Cnt] = Ch_RWop2) Then
  2361. ReadOp(CurProp, Taicpu(p).oper[1]);
  2362. DestroyOp(p, Taicpu(p).oper[1]);
  2363. End;
  2364. Ch_Mop2:
  2365. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2366. Taicpu(p), Taicpu(p).oper[1]);
  2367. Ch_WOp3..Ch_RWOp3:
  2368. Begin
  2369. If (InstrProp.Ch[Cnt] = Ch_RWOp3) Then
  2370. ReadOp(CurProp, Taicpu(p).oper[2]);
  2371. DestroyOp(p, Taicpu(p).oper[2]);
  2372. End;
  2373. Ch_Mop3:
  2374. AddInstr2OpContents({$ifdef statedebug} asml, {$endif}
  2375. Taicpu(p), Taicpu(p).oper[2]);
  2376. Ch_WMemEDI:
  2377. Begin
  2378. tmpreg.enum:=R_EDI;
  2379. ReadReg(CurProp, tmpreg);
  2380. FillChar(TmpRef, SizeOf(TmpRef), 0);
  2381. TmpRef.Base.enum := R_EDI;
  2382. tmpRef.index.enum := R_EDI;
  2383. tmpreg.enum:=R_NO;
  2384. DestroyRefs(p, TmpRef,tmpreg)
  2385. End;
  2386. Ch_RFlags:
  2387. if assigned(LastFlagsChangeProp) then
  2388. LastFlagsChangeProp^.FlagsUsed := true;
  2389. Ch_WFlags:
  2390. LastFlagsChangeProp := CurProp;
  2391. Ch_RWFlags:
  2392. begin
  2393. if assigned(LastFlagsChangeProp) then
  2394. LastFlagsChangeProp^.FlagsUsed := true;
  2395. LastFlagsChangeProp := CurProp;
  2396. end;
  2397. Ch_FPU:;
  2398. Else
  2399. Begin
  2400. {$ifdef statedebug}
  2401. hp := tai_comment.Create(strpnew(
  2402. 'destroying all regs for prev instruction')));
  2403. insertllitem(asml,p, p.next,hp);
  2404. {$endif statedebug}
  2405. DestroyAllRegs(CurProp,true,true);
  2406. LastFlagsChangeProp := CurProp;
  2407. End;
  2408. End;
  2409. Inc(Cnt);
  2410. End
  2411. End;
  2412. end;
  2413. End;
  2414. End
  2415. Else
  2416. Begin
  2417. {$ifdef statedebug}
  2418. hp := tai_comment.Create(strpnew(
  2419. 'destroying all regs: unknown Tai: '+tostr(ord(p.typ)))));
  2420. insertllitem(asml,p, p.next,hp);
  2421. {$endif statedebug}
  2422. DestroyAllRegs(CurProp,true,true);
  2423. End;
  2424. End;
  2425. Inc(InstrCnt);
  2426. prev := p;
  2427. GetNextInstruction(p, p);
  2428. End;
  2429. End;
  2430. Function InitDFAPass2(BlockStart, BlockEnd: Tai): Boolean;
  2431. {reserves memory for the PTaiProps in one big memory block when not using
  2432. TP, returns False if not enough memory is available for the optimizer in all
  2433. cases}
  2434. Var p: Tai;
  2435. Count: Longint;
  2436. { TmpStr: String; }
  2437. Begin
  2438. P := BlockStart;
  2439. SkipHead(P);
  2440. NrOfTaiObjs := 0;
  2441. While (P <> BlockEnd) Do
  2442. Begin
  2443. {$IfDef JumpAnal}
  2444. Case p.Typ Of
  2445. ait_label:
  2446. Begin
  2447. If not labelCanBeSkipped(Tai_label(p)) Then
  2448. LTable^[Tai_Label(p).l^.labelnr-LoLab].InstrNr := NrOfTaiObjs
  2449. End;
  2450. ait_instruction:
  2451. begin
  2452. if Taicpu(p).is_jmp then
  2453. begin
  2454. If (tasmlabel(Taicpu(p).oper[0].sym).labelnr >= LoLab) And
  2455. (tasmlabel(Taicpu(p).oper[0].sym).labelnr <= HiLab) Then
  2456. Inc(LTable^[tasmlabel(Taicpu(p).oper[0].sym).labelnr-LoLab].RefsFound);
  2457. end;
  2458. end;
  2459. { ait_instruction:
  2460. Begin
  2461. If (Taicpu(p).opcode = A_PUSH) And
  2462. (Taicpu(p).oper[0].typ = top_symbol) And
  2463. (PCSymbol(Taicpu(p).oper[0])^.offset = 0) Then
  2464. Begin
  2465. TmpStr := StrPas(PCSymbol(Taicpu(p).oper[0])^.symbol);
  2466. If}
  2467. End;
  2468. {$EndIf JumpAnal}
  2469. Inc(NrOfTaiObjs);
  2470. GetNextInstruction(p, p);
  2471. End;
  2472. {Uncomment the next line to see how much memory the reloading optimizer needs}
  2473. { Writeln(NrOfTaiObjs*SizeOf(TTaiProp));}
  2474. {no need to check mem/maxavail, we've got as much virtual memory as we want}
  2475. If NrOfTaiObjs <> 0 Then
  2476. Begin
  2477. InitDFAPass2 := True;
  2478. GetMem(TaiPropBlock, NrOfTaiObjs*SizeOf(TTaiProp));
  2479. fillchar(TaiPropBlock^,NrOfTaiObjs*SizeOf(TTaiProp),0);
  2480. p := BlockStart;
  2481. SkipHead(p);
  2482. For Count := 1 To NrOfTaiObjs Do
  2483. Begin
  2484. PTaiProp(p.OptInfo) := @TaiPropBlock^[Count];
  2485. GetNextInstruction(p, p);
  2486. End;
  2487. End
  2488. Else InitDFAPass2 := False;
  2489. End;
  2490. Function DFAPass2(
  2491. {$ifdef statedebug}
  2492. AsmL: TAAsmOutPut;
  2493. {$endif statedebug}
  2494. BlockStart, BlockEnd: Tai): Boolean;
  2495. Begin
  2496. If InitDFAPass2(BlockStart, BlockEnd) Then
  2497. Begin
  2498. DoDFAPass2(
  2499. {$ifdef statedebug}
  2500. asml,
  2501. {$endif statedebug}
  2502. BlockStart, BlockEnd);
  2503. DFAPass2 := True
  2504. End
  2505. Else DFAPass2 := False;
  2506. End;
  2507. Procedure ShutDownDFA;
  2508. Begin
  2509. If LabDif <> 0 Then
  2510. FreeMem(LTable, LabDif*SizeOf(TLabelTableItem));
  2511. End;
  2512. End.
  2513. {
  2514. $Log$
  2515. Revision 1.49 2003-04-27 11:21:35 peter
  2516. * aktprocdef renamed to current_procdef
  2517. * procinfo renamed to current_procinfo
  2518. * procinfo will now be stored in current_module so it can be
  2519. cleaned up properly
  2520. * gen_main_procsym changed to create_main_proc and release_main_proc
  2521. to also generate a tprocinfo structure
  2522. * fixed unit implicit initfinal
  2523. Revision 1.48 2003/03/28 19:16:57 peter
  2524. * generic constructor working for i386
  2525. * remove fixed self register
  2526. * esi added as address register for i386
  2527. Revision 1.47 2003/02/26 21:15:43 daniel
  2528. * Fixed the optimizer
  2529. Revision 1.46 2003/02/19 22:00:15 daniel
  2530. * Code generator converted to new register notation
  2531. - Horribily outdated todo.txt removed
  2532. Revision 1.45 2003/01/08 18:43:57 daniel
  2533. * Tregister changed into a record
  2534. Revision 1.44 2002/11/17 16:31:59 carl
  2535. * memory optimization (3-4%) : cleanup of tai fields,
  2536. cleanup of tdef and tsym fields.
  2537. * make it work for m68k
  2538. Revision 1.43 2002/08/18 20:06:29 peter
  2539. * inlining is now also allowed in interface
  2540. * renamed write/load to ppuwrite/ppuload
  2541. * tnode storing in ppu
  2542. * nld,ncon,nbas are already updated for storing in ppu
  2543. Revision 1.42 2002/08/17 09:23:44 florian
  2544. * first part of procinfo rewrite
  2545. Revision 1.41 2002/07/01 18:46:31 peter
  2546. * internal linker
  2547. * reorganized aasm layer
  2548. Revision 1.40 2002/06/24 12:43:00 jonas
  2549. * fixed errors found with new -CR code from Peter when cycling with -O2p3r
  2550. Revision 1.39 2002/06/09 12:56:04 jonas
  2551. * IDIV reads edx too (but now the div/mod optimization fails :/ )
  2552. Revision 1.38 2002/05/18 13:34:22 peter
  2553. * readded missing revisions
  2554. Revision 1.37 2002/05/16 19:46:51 carl
  2555. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2556. + try to fix temp allocation (still in ifdef)
  2557. + generic constructor calls
  2558. + start of tassembler / tmodulebase class cleanup
  2559. Revision 1.34 2002/05/12 16:53:16 peter
  2560. * moved entry and exitcode to ncgutil and cgobj
  2561. * foreach gets extra argument for passing local data to the
  2562. iterator function
  2563. * -CR checks also class typecasts at runtime by changing them
  2564. into as
  2565. * fixed compiler to cycle with the -CR option
  2566. * fixed stabs with elf writer, finally the global variables can
  2567. be watched
  2568. * removed a lot of routines from cga unit and replaced them by
  2569. calls to cgobj
  2570. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2571. u32bit then the other is typecasted also to u32bit without giving
  2572. a rangecheck warning/error.
  2573. * fixed pascal calling method with reversing also the high tree in
  2574. the parast, detected by tcalcst3 test
  2575. Revision 1.33 2002/04/21 15:32:59 carl
  2576. * changeregsize -> rg.makeregsize
  2577. Revision 1.32 2002/04/20 21:37:07 carl
  2578. + generic FPC_CHECKPOINTER
  2579. + first parameter offset in stack now portable
  2580. * rename some constants
  2581. + move some cpu stuff to other units
  2582. - remove unused constents
  2583. * fix stacksize for some targets
  2584. * fix generic size problems which depend now on EXTEND_SIZE constant
  2585. * removing frame pointer in routines is only available for : i386,m68k and vis targets
  2586. Revision 1.31 2002/04/15 19:44:20 peter
  2587. * fixed stackcheck that would be called recursively when a stack
  2588. error was found
  2589. * generic changeregsize(reg,size) for i386 register resizing
  2590. * removed some more routines from cga unit
  2591. * fixed returnvalue handling
  2592. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  2593. Revision 1.30 2002/04/15 19:12:09 carl
  2594. + target_info.size_of_pointer -> pointer_size
  2595. + some cleanup of unused types/variables
  2596. * move several constants from cpubase to their specific units
  2597. (where they are used)
  2598. + att_Reg2str -> gas_reg2str
  2599. + int_reg2str -> std_reg2str
  2600. Revision 1.29 2002/04/14 17:00:49 carl
  2601. + att_reg2str -> std_reg2str
  2602. Revision 1.28 2002/04/02 17:11:34 peter
  2603. * tlocation,treference update
  2604. * LOC_CONSTANT added for better constant handling
  2605. * secondadd splitted in multiple routines
  2606. * location_force_reg added for loading a location to a register
  2607. of a specified size
  2608. * secondassignment parses now first the right and then the left node
  2609. (this is compatible with Kylix). This saves a lot of push/pop especially
  2610. with string operations
  2611. * adapted some routines to use the new cg methods
  2612. Revision 1.27 2002/03/31 20:26:38 jonas
  2613. + a_loadfpu_* and a_loadmm_* methods in tcg
  2614. * register allocation is now handled by a class and is mostly processor
  2615. independent (+rgobj.pas and i386/rgcpu.pas)
  2616. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2617. * some small improvements and fixes to the optimizer
  2618. * some register allocation fixes
  2619. * some fpuvaroffset fixes in the unary minus node
  2620. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2621. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2622. also better optimizable)
  2623. * fixed and optimized register saving/restoring for new/dispose nodes
  2624. * LOC_FPU locations now also require their "register" field to be set to
  2625. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2626. - list field removed of the tnode class because it's not used currently
  2627. and can cause hard-to-find bugs
  2628. Revision 1.26 2002/03/04 19:10:13 peter
  2629. * removed compiler warnings
  2630. }