daopt386.pas 102 KB

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